diff --git a/internal/cmd/protoc-gen-go-flipt-sdk/main.go b/internal/cmd/protoc-gen-go-flipt-sdk/main.go index 37b4aeebd4..f019b371c2 100644 --- a/internal/cmd/protoc-gen-go-flipt-sdk/main.go +++ b/internal/cmd/protoc-gen-go-flipt-sdk/main.go @@ -68,7 +68,10 @@ func generateSDK(gen *protogen.Plugin) { for _, t := range types { g.P("func (s SDK) ", t[0], "() *", t[0], "{") - g.P("return &", t[0], "{transport: s.transport.", t[1], "()}") + g.P("return &", t[0], "{") + g.P("transport: s.transport.", t[1], "(),") + g.P("tokenProvider: s.tokenProvider,") + g.P("}") g.P("}\n") } } @@ -110,7 +113,10 @@ func generateGRPC(gen *protogen.Plugin) { continue } - groupType := variableCase(method) + // the following handles bundling together packages containing more than + // one service definition into a single unexported type which implements + // the combined client interface the SDK generator produces. + groupType := unexport(method) g.P("type ", groupType, " struct {") g.P("cc ", grpc("ClientConnInterface")) g.P("}\n") @@ -161,6 +167,7 @@ func generateSubSDK(gen *protogen.Plugin, file *protogen.File) (typ, client stri g.P("type ", typ, " struct {") g.P("transport ", typ, "Client") + g.P("tokenProvider ", "ClientTokenProvider") g.P("}\n") } @@ -172,11 +179,15 @@ func generateSubSDK(gen *protogen.Plugin, file *protogen.File) (typ, client stri g.P("type ", serviceName, " struct {") g.P("transport ", relativeImport(g, file, srv.GoName+"Client")) + g.P("tokenProvider ", "ClientTokenProvider") g.P("}\n") if !oneServicePackage { g.P("func (s ", typ, ") ", srv.GoName, "()", srv.GoName, "{") - g.P("return ", srv.GoName, "{ transport: s.transport.", srv.GoName+"Client", "()}") + g.P("return ", srv.GoName, "{") + g.P("transport: s.transport.", srv.GoName+"Client", "(),") + g.P("tokenProvider: ", "s.tokenProvider,") + g.P("}") g.P("}") } @@ -195,9 +206,11 @@ func generateSubSDK(gen *protogen.Plugin, file *protogen.File) (typ, client stri if method.Output.GoIdent.GoImportPath != emptyImport { g.P(append(signature, ") (*", method.Output.GoIdent, ", error) {")...) + threadAuth(g, "x", "return nil, err") g.P(append([]any{"return "}, returnStatement...)...) } else { g.P(append(signature, ") error {")...) + threadAuth(g, "x", "return err") g.P(append([]any{"_, err := "}, returnStatement...)...) g.P("return err") } @@ -208,7 +221,18 @@ func generateSubSDK(gen *protogen.Plugin, file *protogen.File) (typ, client stri return } -func variableCase(v string) string { +func threadAuth(g *protogen.GeneratedFile, receiver, returnErr string) { + metadata := importPackage(g, "google.golang.org/grpc/metadata") + g.P("if ", receiver, ".tokenProvider != nil {") + g.P("token, err := ", receiver, ".tokenProvider.ClientToken()") + g.P("if err != nil { ", returnErr, " }") + g.P() + g.P("ctx = ", metadata("AppendToOutgoingContext"), `(ctx, "authorization", "Bearer "+token)`) + g.P("}") + g.P() +} + +func unexport(v string) string { return strings.ToLower(v[:1]) + v[1:] } diff --git a/sdk/go/auth.sdk.gen.go b/sdk/go/auth.sdk.gen.go index 6d6afc176a..d6ba193a08 100644 --- a/sdk/go/auth.sdk.gen.go +++ b/sdk/go/auth.sdk.gen.go @@ -5,6 +5,7 @@ package sdk import ( context "context" auth "go.flipt.io/flipt/rpc/flipt/auth" + metadata "google.golang.org/grpc/metadata" emptypb "google.golang.org/protobuf/types/known/emptypb" ) @@ -17,82 +18,193 @@ type AuthClient interface { } type Auth struct { - transport AuthClient + transport AuthClient + tokenProvider ClientTokenProvider } type PublicAuthenticationService struct { - transport auth.PublicAuthenticationServiceClient + transport auth.PublicAuthenticationServiceClient + tokenProvider ClientTokenProvider } func (s Auth) PublicAuthenticationService() PublicAuthenticationService { - return PublicAuthenticationService{transport: s.transport.PublicAuthenticationServiceClient()} + return PublicAuthenticationService{ + transport: s.transport.PublicAuthenticationServiceClient(), + tokenProvider: s.tokenProvider, + } } func (x *PublicAuthenticationService) ListAuthenticationMethods(ctx context.Context) (*auth.ListAuthenticationMethodsResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.ListAuthenticationMethods(ctx, &emptypb.Empty{}) } type AuthenticationService struct { - transport auth.AuthenticationServiceClient + transport auth.AuthenticationServiceClient + tokenProvider ClientTokenProvider } func (s Auth) AuthenticationService() AuthenticationService { - return AuthenticationService{transport: s.transport.AuthenticationServiceClient()} + return AuthenticationService{ + transport: s.transport.AuthenticationServiceClient(), + tokenProvider: s.tokenProvider, + } } func (x *AuthenticationService) GetAuthenticationSelf(ctx context.Context) (*auth.Authentication, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.GetAuthenticationSelf(ctx, &emptypb.Empty{}) } func (x *AuthenticationService) GetAuthentication(ctx context.Context, v *auth.GetAuthenticationRequest) (*auth.Authentication, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.GetAuthentication(ctx, v) } func (x *AuthenticationService) ListAuthentications(ctx context.Context, v *auth.ListAuthenticationsRequest) (*auth.ListAuthenticationsResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.ListAuthentications(ctx, v) } func (x *AuthenticationService) DeleteAuthentication(ctx context.Context, v *auth.DeleteAuthenticationRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.DeleteAuthentication(ctx, v) return err } func (x *AuthenticationService) ExpireAuthenticationSelf(ctx context.Context, v *auth.ExpireAuthenticationSelfRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.ExpireAuthenticationSelf(ctx, v) return err } type AuthenticationMethodTokenService struct { - transport auth.AuthenticationMethodTokenServiceClient + transport auth.AuthenticationMethodTokenServiceClient + tokenProvider ClientTokenProvider } func (s Auth) AuthenticationMethodTokenService() AuthenticationMethodTokenService { - return AuthenticationMethodTokenService{transport: s.transport.AuthenticationMethodTokenServiceClient()} + return AuthenticationMethodTokenService{ + transport: s.transport.AuthenticationMethodTokenServiceClient(), + tokenProvider: s.tokenProvider, + } } func (x *AuthenticationMethodTokenService) CreateToken(ctx context.Context, v *auth.CreateTokenRequest) (*auth.CreateTokenResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.CreateToken(ctx, v) } type AuthenticationMethodOIDCService struct { - transport auth.AuthenticationMethodOIDCServiceClient + transport auth.AuthenticationMethodOIDCServiceClient + tokenProvider ClientTokenProvider } func (s Auth) AuthenticationMethodOIDCService() AuthenticationMethodOIDCService { - return AuthenticationMethodOIDCService{transport: s.transport.AuthenticationMethodOIDCServiceClient()} + return AuthenticationMethodOIDCService{ + transport: s.transport.AuthenticationMethodOIDCServiceClient(), + tokenProvider: s.tokenProvider, + } } func (x *AuthenticationMethodOIDCService) AuthorizeURL(ctx context.Context, v *auth.AuthorizeURLRequest) (*auth.AuthorizeURLResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.AuthorizeURL(ctx, v) } func (x *AuthenticationMethodOIDCService) Callback(ctx context.Context, v *auth.CallbackRequest) (*auth.CallbackResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.Callback(ctx, v) } type AuthenticationMethodKubernetesService struct { - transport auth.AuthenticationMethodKubernetesServiceClient + transport auth.AuthenticationMethodKubernetesServiceClient + tokenProvider ClientTokenProvider } func (s Auth) AuthenticationMethodKubernetesService() AuthenticationMethodKubernetesService { - return AuthenticationMethodKubernetesService{transport: s.transport.AuthenticationMethodKubernetesServiceClient()} + return AuthenticationMethodKubernetesService{ + transport: s.transport.AuthenticationMethodKubernetesServiceClient(), + tokenProvider: s.tokenProvider, + } } func (x *AuthenticationMethodKubernetesService) VerifyServiceAccount(ctx context.Context, v *auth.VerifyServiceAccountRequest) (*auth.VerifyServiceAccountResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.VerifyServiceAccount(ctx, v) } diff --git a/sdk/go/flipt.sdk.gen.go b/sdk/go/flipt.sdk.gen.go index 3f0e36e227..967ab548f4 100644 --- a/sdk/go/flipt.sdk.gen.go +++ b/sdk/go/flipt.sdk.gen.go @@ -4,124 +4,370 @@ package sdk import ( context "context" + flipt "go.flipt.io/flipt/rpc/flipt" + metadata "google.golang.org/grpc/metadata" ) type Flipt struct { - transport flipt.FliptClient + transport flipt.FliptClient + tokenProvider ClientTokenProvider } func (x *Flipt) Evaluate(ctx context.Context, v *flipt.EvaluationRequest) (*flipt.EvaluationResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.Evaluate(ctx, v) } func (x *Flipt) BatchEvaluate(ctx context.Context, v *flipt.BatchEvaluationRequest) (*flipt.BatchEvaluationResponse, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.BatchEvaluate(ctx, v) } func (x *Flipt) GetFlag(ctx context.Context, v *flipt.GetFlagRequest) (*flipt.Flag, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.GetFlag(ctx, v) } func (x *Flipt) ListFlags(ctx context.Context, v *flipt.ListFlagRequest) (*flipt.FlagList, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.ListFlags(ctx, v) } func (x *Flipt) CreateFlag(ctx context.Context, v *flipt.CreateFlagRequest) (*flipt.Flag, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.CreateFlag(ctx, v) } func (x *Flipt) UpdateFlag(ctx context.Context, v *flipt.UpdateFlagRequest) (*flipt.Flag, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.UpdateFlag(ctx, v) } func (x *Flipt) DeleteFlag(ctx context.Context, v *flipt.DeleteFlagRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.DeleteFlag(ctx, v) return err } func (x *Flipt) CreateVariant(ctx context.Context, v *flipt.CreateVariantRequest) (*flipt.Variant, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.CreateVariant(ctx, v) } func (x *Flipt) UpdateVariant(ctx context.Context, v *flipt.UpdateVariantRequest) (*flipt.Variant, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.UpdateVariant(ctx, v) } func (x *Flipt) DeleteVariant(ctx context.Context, v *flipt.DeleteVariantRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.DeleteVariant(ctx, v) return err } func (x *Flipt) GetRule(ctx context.Context, v *flipt.GetRuleRequest) (*flipt.Rule, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.GetRule(ctx, v) } func (x *Flipt) ListRules(ctx context.Context, v *flipt.ListRuleRequest) (*flipt.RuleList, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.ListRules(ctx, v) } func (x *Flipt) CreateRule(ctx context.Context, v *flipt.CreateRuleRequest) (*flipt.Rule, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.CreateRule(ctx, v) } func (x *Flipt) UpdateRule(ctx context.Context, v *flipt.UpdateRuleRequest) (*flipt.Rule, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.UpdateRule(ctx, v) } func (x *Flipt) OrderRules(ctx context.Context, v *flipt.OrderRulesRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.OrderRules(ctx, v) return err } func (x *Flipt) DeleteRule(ctx context.Context, v *flipt.DeleteRuleRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.DeleteRule(ctx, v) return err } func (x *Flipt) CreateDistribution(ctx context.Context, v *flipt.CreateDistributionRequest) (*flipt.Distribution, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.CreateDistribution(ctx, v) } func (x *Flipt) UpdateDistribution(ctx context.Context, v *flipt.UpdateDistributionRequest) (*flipt.Distribution, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.UpdateDistribution(ctx, v) } func (x *Flipt) DeleteDistribution(ctx context.Context, v *flipt.DeleteDistributionRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.DeleteDistribution(ctx, v) return err } func (x *Flipt) GetSegment(ctx context.Context, v *flipt.GetSegmentRequest) (*flipt.Segment, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.GetSegment(ctx, v) } func (x *Flipt) ListSegments(ctx context.Context, v *flipt.ListSegmentRequest) (*flipt.SegmentList, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.ListSegments(ctx, v) } func (x *Flipt) CreateSegment(ctx context.Context, v *flipt.CreateSegmentRequest) (*flipt.Segment, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.CreateSegment(ctx, v) } func (x *Flipt) UpdateSegment(ctx context.Context, v *flipt.UpdateSegmentRequest) (*flipt.Segment, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.UpdateSegment(ctx, v) } func (x *Flipt) DeleteSegment(ctx context.Context, v *flipt.DeleteSegmentRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.DeleteSegment(ctx, v) return err } func (x *Flipt) CreateConstraint(ctx context.Context, v *flipt.CreateConstraintRequest) (*flipt.Constraint, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.CreateConstraint(ctx, v) } func (x *Flipt) UpdateConstraint(ctx context.Context, v *flipt.UpdateConstraintRequest) (*flipt.Constraint, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.UpdateConstraint(ctx, v) } func (x *Flipt) DeleteConstraint(ctx context.Context, v *flipt.DeleteConstraintRequest) error { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + _, err := x.transport.DeleteConstraint(ctx, v) return err } diff --git a/sdk/go/meta.sdk.gen.go b/sdk/go/meta.sdk.gen.go index 4287262970..37f0fa8442 100644 --- a/sdk/go/meta.sdk.gen.go +++ b/sdk/go/meta.sdk.gen.go @@ -6,17 +6,37 @@ import ( context "context" meta "go.flipt.io/flipt/rpc/flipt/meta" httpbody "google.golang.org/genproto/googleapis/api/httpbody" + metadata "google.golang.org/grpc/metadata" emptypb "google.golang.org/protobuf/types/known/emptypb" ) type Meta struct { - transport meta.MetadataServiceClient + transport meta.MetadataServiceClient + tokenProvider ClientTokenProvider } func (x *Meta) GetConfiguration(ctx context.Context) (*httpbody.HttpBody, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.GetConfiguration(ctx, &emptypb.Empty{}) } func (x *Meta) GetInfo(ctx context.Context) (*httpbody.HttpBody, error) { + if x.tokenProvider != nil { + token, err := x.tokenProvider.ClientToken() + if err != nil { + return nil, err + } + + ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token) + } + return x.transport.GetInfo(ctx, &emptypb.Empty{}) } diff --git a/sdk/go/sdk.gen.go b/sdk/go/sdk.gen.go index 0656e6a7cf..c21ec25e31 100644 --- a/sdk/go/sdk.gen.go +++ b/sdk/go/sdk.gen.go @@ -61,13 +61,22 @@ func New(t Transport, opts ...Option) SDK { } func (s SDK) Auth() *Auth { - return &Auth{transport: s.transport.AuthClient()} + return &Auth{ + transport: s.transport.AuthClient(), + tokenProvider: s.tokenProvider, + } } func (s SDK) Flipt() *Flipt { - return &Flipt{transport: s.transport.FliptClient()} + return &Flipt{ + transport: s.transport.FliptClient(), + tokenProvider: s.tokenProvider, + } } func (s SDK) Meta() *Meta { - return &Meta{transport: s.transport.MetaClient()} + return &Meta{ + transport: s.transport.MetaClient(), + tokenProvider: s.tokenProvider, + } }