From bd06738d934b42d7b9585beb3d570db0735a1f0d Mon Sep 17 00:00:00 2001 From: Dimitrios Naikopoulos <48590504+DimitriosNaikopoulos@users.noreply.github.com> Date: Mon, 2 Dec 2024 17:33:05 +0000 Subject: [PATCH] RE-3259-add-gap-auth (#15463) * RE-3246-add-gap-auth * RE-3246: Add jwt token without prefix * Add multiple grpc interceptors * clean up --- deployment/environment/devenv/jd.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/deployment/environment/devenv/jd.go b/deployment/environment/devenv/jd.go index 48150340cae..818f9b09400 100644 --- a/deployment/environment/devenv/jd.go +++ b/deployment/environment/devenv/jd.go @@ -20,6 +20,7 @@ type JDConfig struct { WSRPC string Creds credentials.TransportCredentials Auth oauth2.TokenSource + GAP string NodeInfo []NodeInfo } @@ -44,14 +45,40 @@ func authTokenInterceptor(source oauth2.TokenSource) grpc.UnaryClientInterceptor } } +func gapTokenInterceptor(token string) grpc.UnaryClientInterceptor { + return func( + ctx context.Context, + method string, + req, reply any, + cc *grpc.ClientConn, + invoker grpc.UnaryInvoker, + opts ...grpc.CallOption, + ) error { + return invoker( + metadata.AppendToOutgoingContext(ctx, "x-authorization-github-jwt", "Bearer "+token), + method, req, reply, cc, opts..., + ) + } +} + func NewJDConnection(cfg JDConfig) (*grpc.ClientConn, error) { opts := []grpc.DialOption{} + interceptors := []grpc.UnaryClientInterceptor{} + if cfg.Creds != nil { opts = append(opts, grpc.WithTransportCredentials(cfg.Creds)) } if cfg.Auth != nil { - opts = append(opts, grpc.WithUnaryInterceptor(authTokenInterceptor(cfg.Auth))) + interceptors = append(interceptors, authTokenInterceptor(cfg.Auth)) } + if cfg.GAP != "" { + interceptors = append(interceptors, gapTokenInterceptor(cfg.GAP)) + } + + if len(interceptors) > 0 { + opts = append(opts, grpc.WithChainUnaryInterceptor(interceptors...)) + } + conn, err := grpc.NewClient(cfg.GRPC, opts...) if err != nil { return nil, fmt.Errorf("failed to connect Job Distributor service. Err: %w", err)