Repository navigation
feat(auth): support universeDomain in JwtCredentials - #15176
Conversation
There was a problem hiding this comment.
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.
| @Override | ||
| public String getUniverseDomain() throws IOException { | ||
| return this.universeDomain; | ||
| } |
There was a problem hiding this comment.
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.
| @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
- 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.
There was a problem hiding this comment.
These serialization compatibility issues seem legit to me, wdyt?
| 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); | ||
| } |
There was a problem hiding this comment.
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
- 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.
ebe2b15 to
c704387
Compare
|
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. |
| @Override | ||
| public String getUniverseDomain() throws IOException { | ||
| return this.universeDomain; | ||
| } |
There was a problem hiding this comment.
These serialization compatibility issues seem legit to me, wdyt?
Adds
universeDomainsupport toJwtCredentials:universeDomainfield (defaulting null or blank values togoogleapis.com), overridesgetUniverseDomain(), addsBuilder.setUniverseDomain()andBuilder.getUniverseDomain(), preservesuniverseDomaininjwtWithClaims(), and includes it inequals()andhashCode().JwtCredentialsTestto cover builder and resolved universe domains,jwtWithClaims()propagation, serialization across default and custom universe domains, and field-levelequals()/hashCode()coverage.