Skip to content

Commit 9bfa1f9

Browse files
committed
address review comments
1 parent b0e4b6e commit 9bfa1f9

3 files changed

Lines changed: 34 additions & 32 deletions

File tree

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public static class Builder extends ServiceOptions.Builder<BigQuery, BigQueryOpt
7777
private Tracer openTelemetryTracer;
7878
private ResultRetryAlgorithm<?> resultRetryAlgorithm;
7979

80-
private Builder() {}
80+
private Builder() {
81+
setUseJwtAccessWithScope(false);
82+
}
8183

8284
private Builder(BigQueryOptions options) {
8385
super(options);
@@ -213,11 +215,6 @@ public static HttpTransportOptions getDefaultHttpTransportOptions() {
213215
return HttpTransportOptions.newBuilder().setReadTimeout(DEFAULT_READ_API_TIME_OUT).build();
214216
}
215217

216-
@Override
217-
protected boolean useSelfSignedJwt() {
218-
return false;
219-
}
220-
221218
@Override
222219
protected Set<String> getScopes() {
223220
return SCOPES;

sdk-platform-java/java-core/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public abstract class ServiceOptions<
106106
private final TransportOptions transportOptions;
107107
private final HeaderProvider headerProvider;
108108
private final String quotaProjectId;
109+
private final boolean useJwtAccessWithScope;
109110

110111
private transient ServiceRpcFactory<OptionsT> serviceRpcFactory;
111112
private transient ServiceFactory<ServiceT, OptionsT> serviceFactory;
@@ -140,6 +141,7 @@ public abstract static class Builder<
140141
private HeaderProvider headerProvider;
141142
private String clientLibToken = ServiceOptions.getGoogApiClientLibName();
142143
private String quotaProjectId;
144+
private boolean useJwtAccessWithScope = true;
143145

144146
private ApiTracerFactory apiTracerFactory;
145147

@@ -159,6 +161,7 @@ protected Builder(ServiceOptions<ServiceT, OptionsT> options) {
159161
transportOptions = options.transportOptions;
160162
clientLibToken = options.clientLibToken;
161163
quotaProjectId = options.quotaProjectId;
164+
useJwtAccessWithScope = options.useJwtAccessWithScope;
162165
apiTracerFactory = options.apiTracerFactory;
163166
}
164167

@@ -313,6 +316,18 @@ public B setQuotaProjectId(String quotaProjectId) {
313316
return self();
314317
}
315318

319+
/**
320+
* Sets the configuration determining whether self-signed JWT with scopes are used for service
321+
* account credentials.
322+
*
323+
* @param useJwtAccessWithScope whether to use self-signed JWT with scopes
324+
* @return the builder
325+
*/
326+
public B setUseJwtAccessWithScope(final boolean useJwtAccessWithScope) {
327+
this.useJwtAccessWithScope = useJwtAccessWithScope;
328+
return self();
329+
}
330+
316331
/**
317332
* Sets the {@link ApiTracerFactory}. It will be used to create an {@link ApiTracer} that is
318333
* annotated throughout the lifecycle of an RPC operation.
@@ -365,6 +380,7 @@ protected ServiceOptions(
365380
builder.quotaProjectId != null
366381
? builder.quotaProjectId
367382
: getValueFromCredentialsFile(getCredentialsPath(), "quota_project_id");
383+
useJwtAccessWithScope = builder.useJwtAccessWithScope;
368384
apiTracerFactory = builder.apiTracerFactory;
369385
}
370386

@@ -650,17 +666,13 @@ public Credentials getScopedCredentials() {
650666
&& ((GoogleCredentials) credentials).createScopedRequired()) {
651667
credentialsToReturn = ((GoogleCredentials) credentials).createScoped(getScopes());
652668
}
653-
if (useSelfSignedJwt() && credentialsToReturn instanceof ServiceAccountCredentials) {
669+
if (getUseJwtAccessWithScope() && credentialsToReturn instanceof ServiceAccountCredentials) {
654670
credentialsToReturn =
655671
((ServiceAccountCredentials) credentialsToReturn).createWithUseJwtAccessWithScope(true);
656672
}
657673
return credentialsToReturn;
658674
}
659675

660-
protected boolean useSelfSignedJwt() {
661-
return true;
662-
}
663-
664676
/** Returns configuration parameters for request retries. */
665677
public RetrySettings getRetrySettings() {
666678
return retrySettings;
@@ -831,6 +843,15 @@ public String getQuotaProjectId() {
831843
return quotaProjectId;
832844
}
833845

846+
/**
847+
* Returns true when self-signed JWT with scopes are used for service account credentials.
848+
*
849+
* @return true when self-signed JWT with scopes are used
850+
*/
851+
public boolean getUseJwtAccessWithScope() {
852+
return useJwtAccessWithScope;
853+
}
854+
834855
/**
835856
* Returns the resolved host for the Service to connect to Google Cloud
836857
*

sdk-platform-java/java-core/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ protected TestServiceOptions build() {
285285
}
286286
}
287287

288-
protected TestServiceOptions(Builder builder) {
288+
private TestServiceOptions(Builder builder) {
289289
super(
290290
TestServiceFactory.class,
291291
TestServiceRpcFactory.class,
@@ -337,25 +337,6 @@ public int hashCode() {
337337
}
338338
}
339339

340-
private static class NonSsjwtServiceOptions extends TestServiceOptions {
341-
private static class Builder extends TestServiceOptions.Builder {
342-
@Override
343-
protected NonSsjwtServiceOptions build() {
344-
return new NonSsjwtServiceOptions(this);
345-
}
346-
}
347-
348-
private NonSsjwtServiceOptions(Builder builder) {
349-
super(builder);
350-
}
351-
352-
@Override
353-
protected boolean useSelfSignedJwt() {
354-
return false;
355-
}
356-
}
357-
358-
359340
@Test
360341
public void testBuilder() {
361342
assertSame(credentials, OPTIONS.getCredentials());
@@ -646,6 +627,7 @@ void testIsValidUniverseDomain_userUniverseDomainConfig_nonGDUCredentials() thro
646627
.setUniverseDomain("random.com")
647628
.setCredentials(credentialsNotInGDU)
648629
.build();
630+
assertThat(options.hasValidUniverseDomain()).isTrue();
649631
}
650632

651633
@Test
@@ -657,14 +639,16 @@ void testGetScopedCredentials_enablesSelfSignedJwtForServiceAccount() {
657639
.build();
658640
com.google.auth.Credentials scoped = options.getScopedCredentials();
659641
assertThat(scoped).isInstanceOf(ServiceAccountCredentials.class);
642+
assertThat(((ServiceAccountCredentials) scoped).getUseJwtAccessWithScope()).isTrue();
660643
}
661644

662645
@Test
663646
void testGetScopedCredentials_optsOutSelfSignedJwt() {
664647
TestServiceOptions options =
665-
new NonSsjwtServiceOptions.Builder()
648+
new TestServiceOptions.Builder()
666649
.setProjectId("project-id")
667650
.setCredentials(credentials)
651+
.setUseJwtAccessWithScope(false)
668652
.build();
669653
com.google.auth.Credentials scoped = options.getScopedCredentials();
670654
assertThat(scoped).isInstanceOf(ServiceAccountCredentials.class);

0 commit comments

Comments
 (0)