Skip to content

Commit fc7056f

Browse files
committed
address review comments
1 parent b0e4b6e commit fc7056f

3 files changed

Lines changed: 30 additions & 31 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: 24 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,17 @@ public B setQuotaProjectId(String quotaProjectId) {
313316
return self();
314317
}
315318

319+
/**
320+
* Sets the configuration determining whether self-signed JWT with scopes
321+
* are used for service account credentials.
322+
*
323+
* @return the builder
324+
*/
325+
public B setUseJwtAccessWithScope(boolean useJwtAccessWithScope) {
326+
this.useJwtAccessWithScope = useJwtAccessWithScope;
327+
return self();
328+
}
329+
316330
/**
317331
* Sets the {@link ApiTracerFactory}. It will be used to create an {@link ApiTracer} that is
318332
* annotated throughout the lifecycle of an RPC operation.
@@ -365,6 +379,7 @@ protected ServiceOptions(
365379
builder.quotaProjectId != null
366380
? builder.quotaProjectId
367381
: getValueFromCredentialsFile(getCredentialsPath(), "quota_project_id");
382+
useJwtAccessWithScope = builder.useJwtAccessWithScope;
368383
apiTracerFactory = builder.apiTracerFactory;
369384
}
370385

@@ -650,17 +665,13 @@ public Credentials getScopedCredentials() {
650665
&& ((GoogleCredentials) credentials).createScopedRequired()) {
651666
credentialsToReturn = ((GoogleCredentials) credentials).createScoped(getScopes());
652667
}
653-
if (useSelfSignedJwt() && credentialsToReturn instanceof ServiceAccountCredentials) {
668+
if (getUseJwtAccessWithScope() && credentialsToReturn instanceof ServiceAccountCredentials) {
654669
credentialsToReturn =
655670
((ServiceAccountCredentials) credentialsToReturn).createWithUseJwtAccessWithScope(true);
656671
}
657672
return credentialsToReturn;
658673
}
659674

660-
protected boolean useSelfSignedJwt() {
661-
return true;
662-
}
663-
664675
/** Returns configuration parameters for request retries. */
665676
public RetrySettings getRetrySettings() {
666677
return retrySettings;
@@ -831,6 +842,14 @@ public String getQuotaProjectId() {
831842
return quotaProjectId;
832843
}
833844

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

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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());
@@ -657,14 +638,16 @@ void testGetScopedCredentials_enablesSelfSignedJwtForServiceAccount() {
657638
.build();
658639
com.google.auth.Credentials scoped = options.getScopedCredentials();
659640
assertThat(scoped).isInstanceOf(ServiceAccountCredentials.class);
641+
assertThat(((ServiceAccountCredentials) scoped).getUseJwtAccessWithScope()).isTrue();
660642
}
661643

662644
@Test
663645
void testGetScopedCredentials_optsOutSelfSignedJwt() {
664646
TestServiceOptions options =
665-
new NonSsjwtServiceOptions.Builder()
647+
new TestServiceOptions.Builder()
666648
.setProjectId("project-id")
667649
.setCredentials(credentials)
650+
.setUseJwtAccessWithScope(false)
668651
.build();
669652
com.google.auth.Credentials scoped = options.getScopedCredentials();
670653
assertThat(scoped).isInstanceOf(ServiceAccountCredentials.class);

0 commit comments

Comments
 (0)