Skip to content

Commit 44a6f91

Browse files
CydeWeysneozwu
authored andcommitted
Don't throw NPE on GoogleCredential.Builder when scopes not set
The list of account scopes in the GoogleCredential.Builder starts off uninitialized (i.e. null), meaning that calling the .build() method throws an NPE unless setServiceAccountScopes() is called. There are no other fields in the builder that are required to be called, and this doesn't seem to be required intentionally, so make the Builder resilient to it being null and have it correctly interpret that as an empty list of scopes. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=133587273
1 parent 9b18d3b commit 44a6f91

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/GoogleCredential.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ protected GoogleCredential(Builder builder) {
325325
} else {
326326
serviceAccountId = Preconditions.checkNotNull(builder.serviceAccountId);
327327
serviceAccountProjectId = builder.serviceAccountProjectId;
328-
serviceAccountScopes = Collections.unmodifiableCollection(builder.serviceAccountScopes);
328+
serviceAccountScopes =
329+
(builder.serviceAccountScopes == null)
330+
? Collections.<String>emptyList()
331+
: Collections.unmodifiableCollection(builder.serviceAccountScopes);
329332
serviceAccountPrivateKey = builder.serviceAccountPrivateKey;
330333
serviceAccountPrivateKeyId = builder.serviceAccountPrivateKeyId;
331334
serviceAccountUser = builder.serviceAccountUser;

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/GoogleCredentialTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ public void testCreateScoped() throws Exception {
133133
scopedCredential.getServiceAccountPrivateKey());
134134
}
135135

136+
public void testCreateScopesNotSet() throws Exception {
137+
final String serviceAccountEmail =
138+
"36680232662-vrd7ji19q3ne0ah2csanun6bnr@developer.gserviceaccount.com";
139+
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
140+
MockTokenServerTransport transport = new MockTokenServerTransport();
141+
transport.addServiceAccount(serviceAccountEmail, accessToken);
142+
143+
GoogleCredential credential = new GoogleCredential.Builder()
144+
.setServiceAccountId(serviceAccountEmail)
145+
.setServiceAccountPrivateKey(SecurityTestUtils.newRsaPrivateKey())
146+
.setTransport(transport)
147+
.setJsonFactory(JSON_FACTORY)
148+
.build();
149+
// Note that setServiceAccountScopes() is not called, so it is uninitialized (i.e. null) on the
150+
// builder.
151+
assertTrue(credential.getServiceAccountScopes().isEmpty());
152+
}
153+
136154
public void testGetApplicationDefaultNullTransportThrows() throws IOException {
137155
try {
138156
GoogleCredential.getApplicationDefault(null, JSON_FACTORY);

0 commit comments

Comments
 (0)