Skip to content

feat: enable self-signed JWTs by default in ServiceOptions#13338

Merged
whowes merged 5 commits into
mainfrom
whowes/ssjwt
Jun 12, 2026
Merged

feat: enable self-signed JWTs by default in ServiceOptions#13338
whowes merged 5 commits into
mainfrom
whowes/ssjwt

Conversation

@whowes

@whowes whowes commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

This should improve efficiency/reliability by avoiding OAuth token exchange - see https://google.aip.dev/auth/4111.

BigQueryOptions (b/523372553) and StorageOptions (b/523372960) are disabled by default for now.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for self-signed JWTs in the base ServiceOptions class, defaulting to true, and overrides it to false in BigQueryOptions. The reviewer raised two important points: first, defaulting to true in the base class could break other inheriting services that do not support self-signed JWTs, so defaulting to false and opting in is recommended. Second, to avoid unnecessary object creation, the code should check if JWT access is already enabled on the credentials before calling createWithUseJwtAccessWithScope.

Comment on lines +660 to +662
protected boolean useSelfSignedJwt() {
return true;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Enabling self-signed JWTs by default in the base ServiceOptions class will affect all inheriting Google Cloud services (e.g., Storage, Pub/Sub, Datastore, Spanner, etc.). If any of these services do not support self-signed JWTs, they will break unless they explicitly override this method to return false.

A safer and more defensive approach is to default to false in ServiceOptions and let individual services that support and benefit from self-signed JWTs opt-in by overriding this method to return true. Alternatively, if this is intended to be opt-out, please ensure that all inheriting services in the repository have been verified and updated if they do not support self-signed JWTs.

Comment on lines +653 to +656
if (useSelfSignedJwt() && credentialsToReturn instanceof ServiceAccountCredentials) {
credentialsToReturn =
((ServiceAccountCredentials) credentialsToReturn).createWithUseJwtAccessWithScope(true);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid unnecessary object creation and copying, we can check if isUseJwtAccessWithScope() is already true before calling createWithUseJwtAccessWithScope(true).

    if (useSelfSignedJwt() && credentialsToReturn instanceof ServiceAccountCredentials) {
      ServiceAccountCredentials sac = (ServiceAccountCredentials) credentialsToReturn;
      if (!sac.isUseJwtAccessWithScope()) {
        credentialsToReturn = sac.createWithUseJwtAccessWithScope(true);
      }
    }
References
  1. Reuse pre-configured objects from options classes directly instead of creating new ones and copying settings.

@whowes whowes force-pushed the whowes/ssjwt branch 5 times, most recently from 9bfa1f9 to 40cb42c Compare June 10, 2026 18:02
@whowes

whowes commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a configuration option useJwtAccessWithScope in ServiceOptions to control whether self-signed JWTs with scopes are used for service account credentials. This option defaults to true in ServiceOptions but is explicitly disabled by default in BigQueryOptions and StorageOptions. The review feedback correctly points out that the current implementation only propagates this setting to the credentials when it is true. To ensure that a false configuration is strictly honored and to prevent potential issues if credential defaults change, the reviewer suggests always propagating the value of getUseJwtAccessWithScope() to the credentials.

Comment on lines +669 to +672
if (getUseJwtAccessWithScope() && credentialsToReturn instanceof ServiceAccountCredentials) {
credentialsToReturn =
((ServiceAccountCredentials) credentialsToReturn).createWithUseJwtAccessWithScope(true);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Currently, if getUseJwtAccessWithScope() is false, the code does not modify the credentials. However, if the passed-in ServiceAccountCredentials already has useJwtAccessWithScope set to true (either explicitly or if the default in google-auth-library changes to true in the future), it will not be overridden to false as configured in ServiceOptions (e.g., for BigQueryOptions or StorageOptions).

To ensure that the configuration in ServiceOptions is strictly honored and to prevent unexpected behavior if defaults change, we should always propagate the value of getUseJwtAccessWithScope() to the credentials. Since ServiceAccountCredentials.createWithUseJwtAccessWithScope(boolean) returns this if the value is already the same, there is no performance overhead or unnecessary object creation.

Suggested change
if (getUseJwtAccessWithScope() && credentialsToReturn instanceof ServiceAccountCredentials) {
credentialsToReturn =
((ServiceAccountCredentials) credentialsToReturn).createWithUseJwtAccessWithScope(true);
}
if (credentialsToReturn instanceof ServiceAccountCredentials) {
credentialsToReturn =
((ServiceAccountCredentials) credentialsToReturn).createWithUseJwtAccessWithScope(getUseJwtAccessWithScope());
}

@whowes whowes marked this pull request as ready for review June 11, 2026 15:38
@whowes whowes requested review from a team as code owners June 11, 2026 15:38
@sonarqubecloud

Copy link
Copy Markdown

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed for 'gapic-generator-java-root'

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud


private Builder() {}
private Builder() {
setUseJwtAccessWithScope(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For BQ and Storage, are there any internal tracking tickets that we can link for these exceptions (e.g. default false)? IIRC, these are more so temporary and not permanent as we want this for default. I think would be helpful for future context

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, I added tracking tickets to the PR description.

@whowes whowes merged commit 110d2b7 into main Jun 12, 2026
276 of 280 checks passed
@whowes whowes deleted the whowes/ssjwt branch June 12, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants