Skip to content

Commit 990e734

Browse files
authored
client: Add cloud-platform scope to GoogleCredentials (spotify#1159)
* client: Add cloud-platform scope to GoogleCredentials When the `GoogleCredentials` to use are `ServiceAccountCredentials`, we must define an access scope before calling `refresh()`. Though it is quite broad, the `https://www.googleapis.com/auth/cloud-platform` scope is necessary to make API requests against Google Cloud IAM and many other Google Cloud Platform APIs, so we must use it here. * client: Address review comments
1 parent 040edfd commit 990e734

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

helios-client/src/main/java/com/spotify/helios/client/GoogleCredentialsAccessTokenProvider.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121

2222
package com.spotify.helios.client;
2323

24+
import static java.util.Collections.singletonList;
25+
2426
import com.google.auth.oauth2.AccessToken;
2527
import com.google.auth.oauth2.GoogleCredentials;
2628
import java.io.File;
2729
import java.io.FileInputStream;
2830
import java.io.IOException;
31+
import java.util.List;
2932
import org.slf4j.Logger;
3033
import org.slf4j.LoggerFactory;
3134

32-
public class GoogleCredentialsAccessTokenProvider {
35+
class GoogleCredentialsAccessTokenProvider {
3336
private static final Logger log =
3437
LoggerFactory.getLogger(GoogleCredentialsAccessTokenProvider.class);
3538

@@ -38,7 +41,7 @@ public class GoogleCredentialsAccessTokenProvider {
3841
* <ol>
3942
* <li>First check to see if the environment variable HELIOS_GOOGLE_CREDENTIALS is set
4043
* and points to a readable file</li>
41-
* <li>Otherwise check if Google Application Default Credentials can be loaded</li>
44+
* <li>Otherwise check if Google Application Default Credentials (ADC) can be loaded</li>
4245
* </ol>
4346
*
4447
* <p>Note that we use a special environment variable of our own in addition to any environment
@@ -47,7 +50,7 @@ public class GoogleCredentialsAccessTokenProvider {
4750
*
4851
* @return Return an AccessToken or null
4952
*/
50-
public static AccessToken getAccessToken() throws IOException {
53+
static AccessToken getAccessToken(List<String> scopes) throws IOException {
5154
GoogleCredentials credentials = null;
5255

5356
// first check whether the environment variable is set
@@ -71,7 +74,17 @@ public static AccessToken getAccessToken() throws IOException {
7174
return null;
7275
}
7376

77+
if (!scopes.isEmpty()) {
78+
credentials.createScoped(scopes);
79+
}
7480
credentials.refresh();
81+
7582
return credentials.getAccessToken();
7683
}
84+
85+
static AccessToken getAccessToken() throws IOException {
86+
// Google Service Account Credentials require an access scope before calling `refresh()`;
87+
// see https://cloud.google.com/compute/docs/access/service-accounts#accesscopesiam.
88+
return getAccessToken(singletonList("https://www.googleapis.com/auth/cloud-platform"));
89+
}
7790
}

0 commit comments

Comments
 (0)