Skip to content

feat(auth): support universeDomain in JwtCredentials - #15176

Merged
lqiu96 merged 1 commit into
googleapis:mainfrom
lqiu96:feat/auth-jwt-credentials-universe-domain
Nov 24, 2026
Merged

lqiu96 merged 1 commit into
googleapis:mainfrom
lqiu96:feat/auth-jwt-credentials-universe-domain

Conversation

@lqiu96

@lqiu96 lqiu96 commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Adds universeDomain support to JwtCredentials:

  • Adds universeDomain field (defaulting null or blank values to googleapis.com), overrides getUniverseDomain(), adds Builder.setUniverseDomain() and Builder.getUniverseDomain(), preserves universeDomain in jwtWithClaims(), and includes it in equals() and hashCode().
  • Updates JwtCredentialsTest to cover builder and resolved universe domains, jwtWithClaims() propagation, serialization across default and custom universe domains, and field-level equals()/hashCode() coverage.

@lqiu96
lqiu96 requested review from a team as code owners November 23, 2026 23:13

@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 adds support for a universeDomain field in JwtCredentials and its builder, defaulting to Credentials.GOOGLE_DEFAULT_UNIVERSE if not specified, and includes corresponding unit tests. The review feedback identifies backward compatibility issues when deserializing older instances of JwtCredentials where universeDomain would be null. The reviewer suggests falling back to the default universe domain in getUniverseDomain(), and applying the same fallback logic in equals and hashCode to maintain consistency.

Comment on lines +172 to +175
@Override
public String getUniverseDomain() throws IOException {
return this.universeDomain;
}

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

Since JwtCredentials implements Serializable, adding the new universeDomain field introduces a backward compatibility issue. When deserializing an older serialized instance of JwtCredentials (which does not contain the universeDomain field), Java serialization will initialize universeDomain to null. To prevent getUniverseDomain() from returning null in these cases, we should return Credentials.GOOGLE_DEFAULT_UNIVERSE if this.universeDomain is null.

Suggested change
@Override
public String getUniverseDomain() throws IOException {
return this.universeDomain;
}
@Override
public String getUniverseDomain() throws IOException {
return this.universeDomain == null ? Credentials.GOOGLE_DEFAULT_UNIVERSE : this.universeDomain;
}
References
  1. When adding a new field to a serializable class that may be null when deserializing older versions, provide a getter with a default fallback value.

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.

These serialization compatibility issues seem legit to me, wdyt?

Comment on lines 209 to 224
return Objects.equals(this.privateKey, other.privateKey)
&& Objects.equals(this.privateKeyId, other.privateKeyId)
&& Objects.equals(this.jwtClaims, other.jwtClaims)
&& Objects.equals(this.lifeSpanSeconds, other.lifeSpanSeconds);
&& Objects.equals(this.lifeSpanSeconds, other.lifeSpanSeconds)
&& Objects.equals(this.universeDomain, other.universeDomain);
}

@Override
public int hashCode() {
return Objects.hash(this.privateKey, this.privateKeyId, this.jwtClaims, this.lifeSpanSeconds);
return Objects.hash(
this.privateKey,
this.privateKeyId,
this.jwtClaims,
this.lifeSpanSeconds,
this.universeDomain);
}

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

To ensure backward compatibility with older serialized instances where universeDomain is deserialized as null, we should treat null as equivalent to Credentials.GOOGLE_DEFAULT_UNIVERSE in both equals and hashCode comparisons.

    String thisUniverse = this.universeDomain == null ? Credentials.GOOGLE_DEFAULT_UNIVERSE : this.universeDomain;
    String otherUniverse = other.universeDomain == null ? Credentials.GOOGLE_DEFAULT_UNIVERSE : other.universeDomain;
    return Objects.equals(this.privateKey, other.privateKey)
        && Objects.equals(this.privateKeyId, other.privateKeyId)
        && Objects.equals(this.jwtClaims, other.jwtClaims)
        && Objects.equals(this.lifeSpanSeconds, other.lifeSpanSeconds)
        && Objects.equals(thisUniverse, otherUniverse);
  }

  @Override
  public int hashCode() {
    String effectiveUniverse = this.universeDomain == null ? Credentials.GOOGLE_DEFAULT_UNIVERSE : this.universeDomain;
    return Objects.hash(
        this.privateKey,
        this.privateKeyId,
        this.jwtClaims,
        this.lifeSpanSeconds,
        effectiveUniverse);
  }
References
  1. When adding a new field to a serializable class that may be null when deserializing older versions, and providing a getter with a default fallback value, ensure that equals() and hashCode() use the getter method (or apply the same fallback logic) rather than accessing the field directly, to maintain consistency between deserialized and newly constructed instances.

@lqiu96
lqiu96 force-pushed the feat/auth-jwt-credentials-universe-domain branch from ebe2b15 to c704387 Compare November 23, 2026 23:50
@whowes

whowes commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Is it on your radar to propagate the universe domain in the service account related JwtProvider implementations too? see e.g. ServiceAccountCredentials.jwtWithClaims and ServiceAccountJwtAccessCredentials.jwtWithClaims. Seems fine for a separate PR IMO, but this came up in an AI-assisted review and I didn't see PRs for them yet.

Comment on lines +172 to +175
@Override
public String getUniverseDomain() throws IOException {
return this.universeDomain;
}

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.

These serialization compatibility issues seem legit to me, wdyt?

@lqiu96
lqiu96 merged commit ec8c96a into googleapis:main Nov 24, 2026
302 of 307 checks passed
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