Skip to content

Commit

Permalink
hooks: Add option to forward HTTP header to gRPC requests (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut authored Sep 18, 2024
1 parent 061eb11 commit 3f6e8b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
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

0 comments on commit 3f6e8b7

Please sign in to comment.