Repository: googleapis/gapic-generator-go
Pull request #1523 (2024-05-21 22:19:52)

feat(gengapic): conditionally enable cloud.google.com/go/auth
* Initial allowlist: `cloudkms.googleapis.com`, `secretmanager.googleapis.com`


Patch:
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 9f7099d..55f9927 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -90,7 +90,7 @@ jobs:
     runs-on: ubuntu-latest
     steps:
     - uses: actions/checkout@v4
-    - uses: bazel-contrib/setup-bazel@0.8.3
+    - uses: bazel-contrib/setup-bazel@0.8.4
       with:
         # Avoid downloading Bazel every time.
         bazelisk-cache: true
diff --git a/.github/workflows/deps.yaml b/.github/workflows/deps.yaml
index d618650..f8caa1b 100644
--- a/.github/workflows/deps.yaml
+++ b/.github/workflows/deps.yaml
@@ -16,7 +16,7 @@ jobs:
     runs-on: ubuntu-latest
     steps:
     - uses: actions/checkout@v4
-    - uses: bazel-contrib/setup-bazel@0.8.3
+    - uses: bazel-contrib/setup-bazel@0.8.4
       with:
         # Avoid downloading Bazel every time.
         bazelisk-cache: true
diff --git a/internal/gengapic/generator.go b/internal/gengapic/generator.go
index ec0fca9..e5fbfe7 100644
--- a/internal/gengapic/generator.go
+++ b/internal/gengapic/generator.go
@@ -36,6 +36,12 @@ import (
 	"google.golang.org/protobuf/types/pluginpb"
 )
 
+var enableNewAuthLibrary = map[string]bool{
+	"cloudkms.googleapis.com":      true,
+	"secretmanager.googleapis.com": true,
+	"showcase.googleapis.com":      true,
+}
+
 type generator struct {
 	pt printer.P
 
diff --git a/internal/gengapic/gengrpc.go b/internal/gengapic/gengrpc.go
index 6ec8e93..a2ac505 100644
--- a/internal/gengapic/gengrpc.go
+++ b/internal/gengapic/gengrpc.go
@@ -185,6 +185,9 @@ func (g *generator) grpcClientOptions(serv *descriptorpb.ServiceDescriptorProto,
 	p("    internaloption.WithDefaultAudience(%q),", generateDefaultAudience(host))
 	p("    internaloption.WithDefaultScopes(DefaultAuthScopes()...),")
 	p("    internaloption.EnableJwtWithScope(),")
+	if _, ok := enableNewAuthLibrary[g.serviceConfig.GetName()]; ok {
+		p("internaloption.EnableNewAuthLibrary(),")
+	}
 	p("    option.WithGRPCDialOption(grpc.WithDefaultCallOptions(")
 	p("      grpc.MaxCallRecvMsgSize(math.MaxInt32))),")
 	p("  }")
diff --git a/internal/gengapic/genrest.go b/internal/gengapic/genrest.go
index 23131d3..e95b17d 100644
--- a/internal/gengapic/genrest.go
+++ b/internal/gengapic/genrest.go
@@ -137,6 +137,9 @@ func (g *generator) restClientOptions(serv *descriptorpb.ServiceDescriptorProto,
 	p("    internaloption.WithDefaultUniverseDomain(%q),", googleDefaultUniverse)
 	p("    internaloption.WithDefaultAudience(%q),", generateDefaultAudience(host))
 	p("    internaloption.WithDefaultScopes(DefaultAuthScopes()...),")
+	if _, ok := enableNewAuthLibrary[g.serviceConfig.GetName()]; ok {
+		p("internaloption.EnableNewAuthLibrary(),")
+	}
 	p("  }")
 	p("}")
 }


Test patch:
diff --git a/internal/gengapic/client_init_test.go b/internal/gengapic/client_init_test.go
index f2d6194..a93155f 100644
--- a/internal/gengapic/client_init_test.go
+++ b/internal/gengapic/client_init_test.go
@@ -123,6 +123,7 @@ func TestClientOpt(t *testing.T) {
 			"google.iam.v1.IAMPolicy":         iamPolicyMethods(),
 		},
 		serviceConfig: &serviceconfig.Service{
+			Name: "showcase.googleapis.com",
 			Apis: []*apipb.Api{
 				{Name: "foo.bar.Baz"},
 				{Name: "google.iam.v1.IAMPolicy"},
diff --git a/internal/gengapic/testdata/empty_opt.want b/internal/gengapic/testdata/empty_opt.want
index a5603fe..e35ef00 100644
--- a/internal/gengapic/testdata/empty_opt.want
+++ b/internal/gengapic/testdata/empty_opt.want
@@ -24,6 +24,7 @@ func defaultGRPCClientOptions() []option.ClientOption {
 		internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
 		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
 		internaloption.EnableJwtWithScope(),
+		internaloption.EnableNewAuthLibrary(),
 		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
 		grpc.MaxCallRecvMsgSize(math.MaxInt32))),
 	}
diff --git a/internal/gengapic/testdata/foo_opt.want b/internal/gengapic/testdata/foo_opt.want
index 54e4393..06a92d8 100644
--- a/internal/gengapic/testdata/foo_opt.want
+++ b/internal/gengapic/testdata/foo_opt.want
@@ -24,6 +24,7 @@ func defaultFooGRPCClientOptions() []option.ClientOption {
 		internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
 		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
 		internaloption.EnableJwtWithScope(),
+		internaloption.EnableNewAuthLibrary(),
 		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
 		grpc.MaxCallRecvMsgSize(math.MaxInt32))),
 	}
diff --git a/internal/gengapic/testdata/host_port_opt.want b/internal/gengapic/testdata/host_port_opt.want
index 879720a..f1ccc19 100644
--- a/internal/gengapic/testdata/host_port_opt.want
+++ b/internal/gengapic/testdata/host_port_opt.want
@@ -22,6 +22,7 @@ func defaultBarGRPCClientOptions() []option.ClientOption {
 		internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
 		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
 		internaloption.EnableJwtWithScope(),
+		internaloption.EnableNewAuthLibrary(),
 		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
 		grpc.MaxCallRecvMsgSize(math.MaxInt32))),
 	}
diff --git a/internal/gengapic/testdata/iam_override_opt.want b/internal/gengapic/testdata/iam_override_opt.want
index f7aede3..7448f77 100644
--- a/internal/gengapic/testdata/iam_override_opt.want
+++ b/internal/gengapic/testdata/iam_override_opt.want
@@ -21,6 +21,7 @@ func defaultBazGRPCClientOptions() []option.ClientOption {
 		internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
 		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
 		internaloption.EnableJwtWithScope(),
+		internaloption.EnableNewAuthLibrary(),
 		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
 		grpc.MaxCallRecvMsgSize(math.MaxInt32))),
 	}