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

feat(sdk/go): thread client token on RPC calls when provided #1389

Merged
merged 6 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
32 changes: 28 additions & 4 deletions internal/cmd/protoc-gen-go-flipt-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
}

Expand All @@ -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("}")
}

Expand All @@ -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")
}
Expand All @@ -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:]
}

Expand Down
134 changes: 123 additions & 11 deletions sdk/go/auth.sdk.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading