Skip to content

Commit

Permalink
RE-3259-add-gap-auth (#15463)
Browse files Browse the repository at this point in the history
* RE-3246-add-gap-auth

* RE-3246: Add jwt token without prefix

* Add multiple grpc interceptors

* clean up
  • Loading branch information
DimitriosNaikopoulos authored Dec 2, 2024
1 parent dcc6a36 commit 53f2761
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion deployment/environment/devenv/jd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type JDConfig struct {
WSRPC string
Creds credentials.TransportCredentials
Auth oauth2.TokenSource
GAP string
NodeInfo []NodeInfo
}

Expand All @@ -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)
Expand Down

0 comments on commit 53f2761

Please sign in to comment.