Skip to content

Commit 385e1f2

Browse files
authored
fix(auth): refine JSpecify nullability annotations for external account credentials (#14164)
Stacked on #14162. Refines JSpecify nullability annotations across ExternalAccountCredentials and subclasses: - **Type-Use Placement & Nullability**: Annotate optional fields, getters, and builder setters/fields with `@Nullable` across: - `ExternalAccountCredentials` - `IdentityPoolCredentials` - `AwsCredentials` - `PluggableAuthCredentials` - `ExternalAccountAuthorizedUserCredentials` - **Preconditions**: Standardize on `com.google.common.base.Preconditions` over `com.google.api.client.util.Preconditions`. - **Builder Copy Constructors**: Ensure builder copy constructors properly preserve all fields.
1 parent 0992cf0 commit 385e1f2

22 files changed

Lines changed: 349 additions & 273 deletions

‎google-auth-library-java/appengine/java/com/google/auth/appengine/AppEngineCredentials.java‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public class AppEngineCredentials extends GoogleCredentials implements ServiceAc
6868

6969
private transient AppIdentityService appIdentityService;
7070

71-
private AppEngineCredentials(Collection<String> scopes, AppIdentityService appIdentityService) {
71+
private AppEngineCredentials(
72+
@Nullable Collection<String> scopes, @Nullable AppIdentityService appIdentityService) {
7273
this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes);
7374
this.appIdentityService =
7475
appIdentityService != null
@@ -96,7 +97,7 @@ public boolean createScopedRequired() {
9697
}
9798

9899
@Override
99-
public GoogleCredentials createScoped(Collection<String> scopes) {
100+
public GoogleCredentials createScoped(@Nullable Collection<String> scopes) {
100101
return new AppEngineCredentials(scopes, appIdentityService);
101102
}
102103

@@ -175,36 +176,44 @@ public Builder toBuilder() {
175176

176177
public static class Builder extends GoogleCredentials.Builder {
177178

178-
private Collection<String> scopes;
179-
private AppIdentityService appIdentityService;
179+
private @Nullable Collection<String> scopes;
180+
private @Nullable AppIdentityService appIdentityService;
180181

181182
protected Builder() {}
182183

183184
protected Builder(AppEngineCredentials credentials) {
185+
super(credentials);
184186
this.scopes = credentials.scopes;
185187
this.appIdentityService = credentials.appIdentityService;
186188
}
187189

188190
@CanIgnoreReturnValue
189-
public Builder setScopes(Collection<String> scopes) {
191+
public Builder setScopes(@Nullable Collection<String> scopes) {
190192
this.scopes = scopes;
191193
return this;
192194
}
193195

194196
@CanIgnoreReturnValue
195-
public Builder setAppIdentityService(AppIdentityService appIdentityService) {
197+
public Builder setAppIdentityService(@Nullable AppIdentityService appIdentityService) {
196198
this.appIdentityService = appIdentityService;
197199
return this;
198200
}
199201

200-
public Collection<String> getScopes() {
202+
public @Nullable Collection<String> getScopes() {
201203
return scopes;
202204
}
203205

204-
public AppIdentityService getAppIdentityService() {
206+
public @Nullable AppIdentityService getAppIdentityService() {
205207
return appIdentityService;
206208
}
207209

210+
@Override
211+
@CanIgnoreReturnValue
212+
public Builder setQuotaProjectId(@Nullable String quotaProjectId) {
213+
super.setQuotaProjectId(quotaProjectId);
214+
return this;
215+
}
216+
208217
@Override
209218
public AppEngineCredentials build() {
210219
return new AppEngineCredentials(scopes, appIdentityService);

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/AccessToken.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class AccessToken implements Serializable {
5656
* @param tokenValue String representation of the access token.
5757
* @param expirationTime Time when access token will expire.
5858
*/
59-
public AccessToken(String tokenValue, Date expirationTime) {
59+
public AccessToken(String tokenValue, @Nullable Date expirationTime) {
6060
this.tokenValue = tokenValue;
6161
this.expirationTimeMillis = (expirationTime == null) ? null : expirationTime.getTime();
6262
this.scopes = new ArrayList<>();
@@ -108,7 +108,7 @@ public String getTokenValue() {
108108
return new Date(expirationTimeMillis);
109109
}
110110

111-
Long getExpirationTimeMillis() {
111+
@Nullable Long getExpirationTimeMillis() {
112112
return expirationTimeMillis;
113113
}
114114

@@ -145,8 +145,8 @@ public boolean equals(@Nullable Object obj) {
145145
}
146146

147147
public static class Builder {
148-
private String tokenValue;
149-
private Date expirationTime;
148+
private @Nullable String tokenValue;
149+
private @Nullable Date expirationTime;
150150
private List<String> scopes = new ArrayList<>();
151151

152152
protected Builder() {}
@@ -157,34 +157,34 @@ protected Builder(AccessToken accessToken) {
157157
this.scopes = accessToken.getScopes();
158158
}
159159

160-
public String getTokenValue() {
160+
public @Nullable String getTokenValue() {
161161
return this.tokenValue;
162162
}
163163

164164
public List<String> getScopes() {
165165
return this.scopes;
166166
}
167167

168-
public Date getExpirationTime() {
168+
public @Nullable Date getExpirationTime() {
169169
return this.expirationTime;
170170
}
171171

172172
@CanIgnoreReturnValue
173-
public Builder setTokenValue(String tokenValue) {
173+
public Builder setTokenValue(@Nullable String tokenValue) {
174174
this.tokenValue = tokenValue;
175175
return this;
176176
}
177177

178178
@CanIgnoreReturnValue
179-
public Builder setScopes(String scopes) {
179+
public Builder setScopes(@Nullable String scopes) {
180180
if (scopes != null && scopes.trim().length() > 0) {
181181
this.scopes = Arrays.asList(scopes.split(" "));
182182
}
183183
return this;
184184
}
185185

186186
@CanIgnoreReturnValue
187-
public Builder setScopes(List<String> scopes) {
187+
public Builder setScopes(@Nullable List<String> scopes) {
188188
if (scopes == null) {
189189
this.scopes = new ArrayList<>();
190190
} else {
@@ -195,7 +195,7 @@ public Builder setScopes(List<String> scopes) {
195195
}
196196

197197
@CanIgnoreReturnValue
198-
public Builder setExpirationTime(Date expirationTime) {
198+
public Builder setExpirationTime(@Nullable Date expirationTime) {
199199
this.expirationTime = expirationTime;
200200
return this;
201201
}

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/AppEngineCredentials.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ class AppEngineCredentials extends GoogleCredentials implements ServiceAccountSi
8080
private transient Method getExpirationTime;
8181
private transient Method signForApp;
8282
private transient Method getSignature;
83-
private transient String account;
83+
private transient @Nullable String account;
8484

85-
AppEngineCredentials(Collection<String> scopes, Collection<String> defaultScopes)
85+
AppEngineCredentials(
86+
@Nullable Collection<String> scopes, @Nullable Collection<String> defaultScopes)
8687
throws IOException {
8788
// Use defaultScopes only when scopes don't exist.
8889
if (scopes == null || scopes.isEmpty()) {
@@ -96,7 +97,7 @@ class AppEngineCredentials extends GoogleCredentials implements ServiceAccountSi
9697
}
9798

9899
AppEngineCredentials(
99-
Collection<String> scopes,
100+
@Nullable Collection<String> scopes,
100101
@Nullable Collection<String> defaultScopes,
101102
AppEngineCredentials unscoped) {
102103
this.appIdentityService = unscoped.appIdentityService;
@@ -164,13 +165,13 @@ public boolean createScopedRequired() {
164165
}
165166

166167
@Override
167-
public GoogleCredentials createScoped(Collection<String> scopes) {
168+
public GoogleCredentials createScoped(@Nullable Collection<String> scopes) {
168169
return new AppEngineCredentials(scopes, null, this);
169170
}
170171

171172
@Override
172173
public GoogleCredentials createScoped(
173-
Collection<String> scopes, Collection<String> defaultScopes) {
174+
@Nullable Collection<String> scopes, @Nullable Collection<String> defaultScopes) {
174175
return new AppEngineCredentials(scopes, defaultScopes, this);
175176
}
176177

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public class AwsCredentials extends ExternalAccountCredentials {
6767
private final ExternalAccountSupplierContext supplierContext;
6868
// Regional credential verification url override. This needs to be its own value so we can
6969
// correctly pass it to a builder.
70-
@Nullable private final String regionalCredentialVerificationUrlOverride;
71-
@Nullable private final String regionalCredentialVerificationUrl;
70+
private final @Nullable String regionalCredentialVerificationUrlOverride;
71+
private final @Nullable String regionalCredentialVerificationUrl;
7272
private final String metricsHeaderValue;
7373

7474
/** Internal constructor. See {@link AwsCredentials.Builder}. */
@@ -83,7 +83,8 @@ public class AwsCredentials extends ExternalAccountCredentials {
8383
// Check that one and only one of supplier or credential source are provided.
8484
if (builder.awsSecurityCredentialsSupplier != null && builder.credentialSource != null) {
8585
throw new IllegalArgumentException(
86-
"AwsCredentials cannot have both an awsSecurityCredentialsSupplier and a credentialSource.");
86+
"AwsCredentials cannot have both an awsSecurityCredentialsSupplier and a"
87+
+ " credentialSource.");
8788
}
8889
if (builder.awsSecurityCredentialsSupplier == null && builder.credentialSource == null) {
8990
throw new IllegalArgumentException(
@@ -162,7 +163,7 @@ public String retrieveSubjectToken() throws IOException {
162163
/** Clones the AwsCredentials with the specified scopes. */
163164
@Override
164165
public GoogleCredentials createScoped(Collection<String> newScopes) {
165-
return new AwsCredentials((AwsCredentials.Builder) newBuilder(this).setScopes(newScopes));
166+
return newBuilder(this).setScopes(newScopes).build();
166167
}
167168

168169
@Override
@@ -194,7 +195,7 @@ private String buildSubjectToken(AwsRequestSignature signature)
194195
}
195196

196197
@VisibleForTesting
197-
String getRegionalCredentialVerificationUrl() {
198+
@Nullable String getRegionalCredentialVerificationUrl() {
198199
return this.regionalCredentialVerificationUrl;
199200
}
200201

@@ -203,8 +204,7 @@ AwsSecurityCredentialsSupplier getAwsSecurityCredentialsSupplier() {
203204
return this.awsSecurityCredentialsSupplier;
204205
}
205206

206-
@Nullable
207-
public String getRegionalCredentialVerificationUrlOverride() {
207+
public @Nullable String getRegionalCredentialVerificationUrlOverride() {
208208
return this.regionalCredentialVerificationUrlOverride;
209209
}
210210

@@ -237,9 +237,9 @@ public Builder toBuilder() {
237237

238238
public static class Builder extends ExternalAccountCredentials.Builder {
239239

240-
private AwsSecurityCredentialsSupplier awsSecurityCredentialsSupplier;
240+
private @Nullable AwsSecurityCredentialsSupplier awsSecurityCredentialsSupplier;
241241

242-
private String regionalCredentialVerificationUrlOverride;
242+
private @Nullable String regionalCredentialVerificationUrlOverride;
243243

244244
Builder() {}
245245

@@ -282,30 +282,35 @@ public Builder setRegionalCredentialVerificationUrlOverride(
282282
return this;
283283
}
284284

285+
@Override
285286
@CanIgnoreReturnValue
286287
public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) {
287288
super.setHttpTransportFactory(transportFactory);
288289
return this;
289290
}
290291

292+
@Override
291293
@CanIgnoreReturnValue
292294
public Builder setAudience(String audience) {
293295
super.setAudience(audience);
294296
return this;
295297
}
296298

299+
@Override
297300
@CanIgnoreReturnValue
298301
public Builder setSubjectTokenType(String subjectTokenType) {
299302
super.setSubjectTokenType(subjectTokenType);
300303
return this;
301304
}
302305

306+
@Override
303307
@CanIgnoreReturnValue
304308
public Builder setSubjectTokenType(SubjectTokenTypes subjectTokenType) {
305309
super.setSubjectTokenType(subjectTokenType);
306310
return this;
307311
}
308312

313+
@Override
309314
@CanIgnoreReturnValue
310315
public Builder setTokenUrl(String tokenUrl) {
311316
super.setTokenUrl(tokenUrl);
@@ -318,60 +323,71 @@ public Builder setCredentialSource(AwsCredentialSource credentialSource) {
318323
return this;
319324
}
320325

326+
@Override
321327
@CanIgnoreReturnValue
322-
public Builder setServiceAccountImpersonationUrl(String serviceAccountImpersonationUrl) {
328+
public Builder setServiceAccountImpersonationUrl(
329+
@Nullable String serviceAccountImpersonationUrl) {
323330
super.setServiceAccountImpersonationUrl(serviceAccountImpersonationUrl);
324331
return this;
325332
}
326333

334+
@Override
327335
@CanIgnoreReturnValue
328336
public Builder setTokenInfoUrl(String tokenInfoUrl) {
329337
super.setTokenInfoUrl(tokenInfoUrl);
330338
return this;
331339
}
332340

341+
@Override
333342
@CanIgnoreReturnValue
334-
public Builder setQuotaProjectId(String quotaProjectId) {
343+
public Builder setQuotaProjectId(@Nullable String quotaProjectId) {
335344
super.setQuotaProjectId(quotaProjectId);
336345
return this;
337346
}
338347

348+
@Override
339349
@CanIgnoreReturnValue
340350
public Builder setClientId(String clientId) {
341351
super.setClientId(clientId);
342352
return this;
343353
}
344354

355+
@Override
345356
@CanIgnoreReturnValue
346357
public Builder setClientSecret(String clientSecret) {
347358
super.setClientSecret(clientSecret);
348359
return this;
349360
}
350361

362+
@Override
351363
@CanIgnoreReturnValue
352364
public Builder setScopes(Collection<String> scopes) {
353365
super.setScopes(scopes);
354366
return this;
355367
}
356368

369+
@Override
357370
@CanIgnoreReturnValue
358371
public Builder setWorkforcePoolUserProject(String workforcePoolUserProject) {
359372
super.setWorkforcePoolUserProject(workforcePoolUserProject);
360373
return this;
361374
}
362375

376+
@Override
363377
@CanIgnoreReturnValue
364378
public Builder setServiceAccountImpersonationOptions(Map<String, Object> optionsMap) {
365379
super.setServiceAccountImpersonationOptions(optionsMap);
366380
return this;
367381
}
368382

383+
@Override
369384
@CanIgnoreReturnValue
370385
public Builder setUniverseDomain(String universeDomain) {
371386
super.setUniverseDomain(universeDomain);
372387
return this;
373388
}
374389

390+
@Override
375391
@CanIgnoreReturnValue
376392
Builder setEnvironmentProvider(EnvironmentProvider environmentProvider) {
377393
super.setEnvironmentProvider(environmentProvider);

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public static class Builder extends GoogleCredentials.Builder {
135135
protected Builder() {}
136136

137137
protected Builder(CloudShellCredentials credentials) {
138+
super(credentials);
138139
this.authPort = credentials.authPort;
139140
}
140141

@@ -144,9 +145,10 @@ public Builder setAuthPort(int authPort) {
144145
return this;
145146
}
146147

148+
@Override
147149
@CanIgnoreReturnValue
148-
public Builder setQuotaProjectId(String quotaProjectId) {
149-
super.quotaProjectId = quotaProjectId;
150+
public Builder setQuotaProjectId(@Nullable String quotaProjectId) {
151+
super.setQuotaProjectId(quotaProjectId);
150152
return this;
151153
}
152154

0 commit comments

Comments
 (0)