Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hooks: Add option to forward HTTP header to gRPC requests #1188

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/tusd/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var Flags struct {
GrpcHooksServerTLSCertFile string
GrpcHooksClientTLSCertFile string
GrpcHooksClientTLSKeyFile string
GrpcHooksForwardHeaders string
EnabledHooks []hooks.HookType
ProgressHooksInterval time.Duration
ShowVersion bool
Expand Down Expand Up @@ -173,6 +174,7 @@ func ParseFlags() {
f.StringVar(&Flags.GrpcHooksServerTLSCertFile, "hooks-grpc-server-tls-certificate", "", "Path to the file containing the TLS certificate of the remote gRPC server")
f.StringVar(&Flags.GrpcHooksClientTLSCertFile, "hooks-grpc-client-tls-certificate", "", "Path to the file containing the client certificate for mTLS")
f.StringVar(&Flags.GrpcHooksClientTLSKeyFile, "hooks-grpc-client-tls-key", "", "Path to the file containing the client key for mTLS")
f.StringVar(&Flags.GrpcHooksForwardHeaders, "hooks-grpc-forward-headers", "", "List of HTTP request headers to be forwarded from the client request to the hook endpoint")
})

fs.AddGroup("Plugin hook options", func(f *flag.FlagSet) {
Expand Down
1 change: 1 addition & 0 deletions cmd/tusd/cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func getHookHandler(config *handler.Config) hooks.HookHandler {
ServerTLSCertificateFilePath: Flags.GrpcHooksServerTLSCertFile,
ClientTLSCertificateFilePath: Flags.GrpcHooksClientTLSCertFile,
ClientTLSCertificateKeyFilePath: Flags.GrpcHooksClientTLSKeyFile,
ForwardHeaders: strings.Split(Flags.GrpcHooksForwardHeaders, ","),
}
} else if Flags.PluginHookPath != "" {
stdout.Printf("Using '%s' to load plugin for hooks", Flags.PluginHookPath)
Expand Down
10 changes: 10 additions & 0 deletions pkg/hooks/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)

type GrpcHook struct {
Expand All @@ -29,6 +30,7 @@ type GrpcHook struct {
ServerTLSCertificateFilePath string
ClientTLSCertificateFilePath string
ClientTLSCertificateKeyFilePath string
ForwardHeaders []string
}

func (g *GrpcHook) Setup() error {
Expand Down Expand Up @@ -87,6 +89,14 @@ func (g *GrpcHook) Setup() error {

func (g *GrpcHook) InvokeHook(hookReq hooks.HookRequest) (hookRes hooks.HookResponse, err error) {
ctx := context.Background()

for _, header := range g.ForwardHeaders {
value := hookReq.Event.HTTPRequest.Header.Get(header)
if value != "" {
ctx = metadata.AppendToOutgoingContext(ctx, header, value)
}
}

req := marshal(hookReq)
res, err := g.Client.InvokeHook(ctx, req)
if err != nil {
Expand Down
Loading