Skip to content

Commit a1c9fc0

Browse files
committed
chore: Update to use the file type enum
1 parent d261753 commit a1c9fc0

21 files changed

Lines changed: 77 additions & 64 deletions

oauth2_http/java/com/google/auth/oauth2/AppEngineCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void init() throws IOException {
125125
this.signForApp = serviceClass.getMethod(SIGN_FOR_APP_METHOD, byte[].class);
126126
Class<?> signingResultClass = forName(SIGNING_RESULT_CLASS);
127127
this.getSignature = signingResultClass.getMethod(GET_SIGNATURE_METHOD);
128-
this.type = "App Engine Credentials";
128+
this.name = GoogleCredentialsInfo.APP_ENGINE_CREDENTIALS.getCredentialName();
129129
} catch (ClassNotFoundException
130130
| NoSuchMethodException
131131
| IllegalAccessException

oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static CloudShellCredentials create(int authPort) {
7070
private CloudShellCredentials(Builder builder) {
7171
super(builder);
7272
this.authPort = builder.getAuthPort();
73+
this.name = GoogleCredentialsInfo.CLOUD_SHELL_CREDENTIALS.getCredentialName();
7374
}
7475

7576
protected int getAuthPort() {

oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private ComputeEngineCredentials(ComputeEngineCredentials.Builder builder) {
220220
}
221221
this.transport = builder.getGoogleAuthTransport();
222222
this.bindingEnforcement = builder.getBindingEnforcement();
223-
this.type = "Compute Engine Credentials";
223+
this.name = GoogleCredentialsInfo.COMPUTE_ENGINE_CREDENTIALS.getCredentialName();
224224
}
225225

226226
@Override

oauth2_http/java/com/google/auth/oauth2/ExternalAccountAuthorizedUserCredentials.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ public class ExternalAccountAuthorizedUserCredentials extends GoogleCredentials
8181

8282
private static final long serialVersionUID = -2181779590486283287L;
8383

84-
static final String EXTERNAL_ACCOUNT_AUTHORIZED_USER_FILE_TYPE =
85-
"external_account_authorized_user";
86-
8784
private final String transportFactoryClassName;
8885
private final String audience;
8986
private final String tokenUrl;
@@ -117,7 +114,8 @@ private ExternalAccountAuthorizedUserCredentials(Builder builder) {
117114
this.clientId = builder.clientId;
118115
this.clientSecret = builder.clientSecret;
119116

120-
this.type = "External Account Authorized User Credentials";
117+
this.name =
118+
GoogleCredentialsInfo.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS.getCredentialName();
121119

122120
Preconditions.checkState(
123121
getAccessToken() != null || canRefresh(),

oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public abstract class ExternalAccountCredentials extends GoogleCredentials {
7171
private static final String CLOUD_PLATFORM_SCOPE =
7272
"https://www.googleapis.com/auth/cloud-platform";
7373

74-
static final String EXTERNAL_ACCOUNT_FILE_TYPE = "external_account";
7574
static final String EXECUTABLE_SOURCE_KEY = "executable";
7675

7776
static final String DEFAULT_TOKEN_URL = "https://sts.{UNIVERSE_DOMAIN}/v1/token";
@@ -214,7 +213,7 @@ protected ExternalAccountCredentials(
214213
}
215214

216215
this.metricsHandler = new ExternalAccountMetricsHandler(this);
217-
this.type = "External Account Credentials";
216+
this.name = GoogleCredentialsInfo.EXTERNAL_ACCOUNT_CREDENTIALS.getCredentialName();
218217
}
219218

220219
/**
@@ -273,7 +272,7 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)
273272
? new ExternalAccountMetricsHandler(this)
274273
: builder.metricsHandler;
275274

276-
this.type = "External Account Credentials";
275+
this.name = GoogleCredentialsInfo.EXTERNAL_ACCOUNT_CREDENTIALS.getCredentialName();
277276
}
278277

279278
ImpersonatedCredentials buildImpersonatedCredentials() {

oauth2_http/java/com/google/auth/oauth2/GdchCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class GdchCredentials extends GoogleCredentials {
9696
this.caCertPath = builder.caCertPath;
9797
this.apiAudience = builder.apiAudience;
9898
this.lifetime = builder.lifetime;
99-
this.type = "GDCH Credentials";
99+
this.name = GoogleCredentialsInfo.GDCH_CREDENTIALS.getCredentialName();
100100
}
101101

102102
/**

oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,22 @@ public class GoogleCredentials extends OAuth2Credentials implements QuotaProject
6262

6363
static final String QUOTA_PROJECT_ID_HEADER_KEY = "x-goog-user-project";
6464

65-
enum GoogleCredentialsType {
65+
enum GoogleCredentialsInfo {
6666
USER_CREDENTIALS("User Credentials", "authorized_user"),
6767
SERVICE_ACCOUNT_CREDENTIALS("Service Account Credentials", "service_account"),
6868
GDCH_CREDENTIALS("GDCH Credentials", "gdch_service_account"),
6969
EXTERNAL_ACCOUNT_CREDENTIALS("External Account Credentials", "external_account"),
70-
EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS("External Account Authorized User Credentials", "external_account_authorized_user"),
71-
IMPERSONATED_CREDENTIALS("Impersonated Credentials","impersonated_service_account"),
70+
EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS(
71+
"External Account Authorized User Credentials", "external_account_authorized_user"),
72+
IMPERSONATED_CREDENTIALS("Impersonated Credentials", "impersonated_service_account"),
7273
APP_ENGINE_CREDENTIALS("App Engine Credentials", null),
7374
CLOUD_SHELL_CREDENTIALS("Cloud Shell Credentials", null),
7475
COMPUTE_ENGINE_CREDENTIALS("Compute Engine Credentials", null);
7576

7677
private final String credentialName;
7778
private final String fileType;
7879

79-
GoogleCredentialsType(String credentialName, String fileType) {
80+
GoogleCredentialsInfo(String credentialName, String fileType) {
8081
this.credentialName = credentialName;
8182
this.fileType = fileType;
8283
}
@@ -90,12 +91,11 @@ public String getFileType() {
9091
}
9192
}
9293

93-
9494
/* The following package-private fields provide additional info for errors message */
9595
// Source of the credential (e.g. env var value or well know file location)
9696
String source;
9797
// User-friendly name of actual Credential class
98-
String type;
98+
String name;
9999
// Identity of the credential (not all credentials will have this)
100100
String principal;
101101

@@ -242,35 +242,37 @@ public static GoogleCredentials fromStream(
242242
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
243243
}
244244

245-
if (GoogleCredentialsType.USER_CREDENTIALS.getFileType().equals(fileType)) {
245+
if (GoogleCredentialsInfo.USER_CREDENTIALS.getFileType().equals(fileType)) {
246246
return UserCredentials.fromJson(fileContents, transportFactory);
247247
}
248-
if (GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
248+
if (GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
249249
return ServiceAccountCredentials.fromJson(fileContents, transportFactory);
250250
}
251-
if (GoogleCredentialsType.GDCH_CREDENTIALS.getFileType().equals(fileType)) {
251+
if (GoogleCredentialsInfo.GDCH_CREDENTIALS.getFileType().equals(fileType)) {
252252
return GdchCredentials.fromJson(fileContents);
253253
}
254-
if (GoogleCredentialsType.EXTERNAL_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
254+
if (GoogleCredentialsInfo.EXTERNAL_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
255255
return ExternalAccountCredentials.fromJson(fileContents, transportFactory);
256256
}
257-
if (GoogleCredentialsType.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS.getFileType().equals(fileType)) {
257+
if (GoogleCredentialsInfo.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS
258+
.getFileType()
259+
.equals(fileType)) {
258260
return ExternalAccountAuthorizedUserCredentials.fromJson(fileContents, transportFactory);
259261
}
260-
if (GoogleCredentialsType.IMPERSONATED_CREDENTIALS.getFileType().equals(fileType)) {
262+
if (GoogleCredentialsInfo.IMPERSONATED_CREDENTIALS.getFileType().equals(fileType)) {
261263
return ImpersonatedCredentials.fromJson(fileContents, transportFactory);
262264
}
263265
throw new IOException(
264266
String.format(
265267
"Error reading credentials from stream, 'type' value '%s' not recognized."
266268
+ " Valid values are '%s', '%s', '%s', '%s', '%s', '%s'.",
267269
fileType,
268-
GoogleCredentialsType.USER_CREDENTIALS.getFileType(),
269-
GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType(),
270-
GoogleCredentialsType.GDCH_CREDENTIALS.getFileType(),
271-
GoogleCredentialsType.EXTERNAL_ACCOUNT_CREDENTIALS.getFileType(),
272-
GoogleCredentialsType.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS.getFileType(),
273-
GoogleCredentialsType.IMPERSONATED_CREDENTIALS.getFileType()));
270+
GoogleCredentialsInfo.USER_CREDENTIALS.getFileType(),
271+
GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getFileType(),
272+
GoogleCredentialsInfo.GDCH_CREDENTIALS.getFileType(),
273+
GoogleCredentialsInfo.EXTERNAL_ACCOUNT_CREDENTIALS.getFileType(),
274+
GoogleCredentialsInfo.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS.getFileType(),
275+
GoogleCredentialsInfo.IMPERSONATED_CREDENTIALS.getFileType()));
274276
}
275277

276278
/**
@@ -543,11 +545,14 @@ GoogleCredentials withSource(String source) {
543545

544546
/**
545547
* Provides additional information regarding credential initialization source
548+
*
546549
* <ul>
547-
* <li> credential source - Initialized via the GOOGLE_APPLICATION_CREDENTIALS env var or well known file type
548-
* <li> credential type - The type of credential created
549-
* <li> principal - Identity used for the credential
550+
* <li>credential source - Initialized via the GOOGLE_APPLICATION_CREDENTIALS env var or well
551+
* known file type
552+
* <li>credential name - The user-friendly name of the credential created
553+
* <li>principal - Identity used for the credential
550554
* </ul>
555+
*
551556
* These fields are populated on a best-effort basis and may be populated missing
552557
*
553558
* @return Map of information regarding how the Credential was initialized
@@ -557,8 +562,8 @@ public Map<String, String> getCredentialInfo() {
557562
if (!Strings.isNullOrEmpty(source)) {
558563
infoMap.put("Credential Source", source);
559564
}
560-
if (!Strings.isNullOrEmpty(type)) {
561-
infoMap.put("Credential Type", type);
565+
if (!Strings.isNullOrEmpty(name)) {
566+
infoMap.put("Credential Name", name);
562567
}
563568
if (!Strings.isNullOrEmpty(principal)) {
564569
infoMap.put("Principal", principal);

oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@
9595
public class ImpersonatedCredentials extends GoogleCredentials
9696
implements ServiceAccountSigner, IdTokenProvider {
9797

98-
static final String IMPERSONATED_CREDENTIALS_FILE_TYPE = "impersonated_service_account";
99-
10098
private static final long serialVersionUID = -2133257318957488431L;
10199
private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ssX";
102100
private static final int TWELVE_HOURS_IN_SECONDS = 43200;
@@ -399,9 +397,11 @@ static ImpersonatedCredentials fromJson(
399397
}
400398

401399
GoogleCredentials sourceCredentials;
402-
if (GoogleCredentialsType.USER_CREDENTIALS.getFileType().equals(sourceCredentialsType)) {
400+
if (GoogleCredentialsInfo.USER_CREDENTIALS.getFileType().equals(sourceCredentialsType)) {
403401
sourceCredentials = UserCredentials.fromJson(sourceCredentialsJson, transportFactory);
404-
} else if (GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(sourceCredentialsType)) {
402+
} else if (GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS
403+
.getFileType()
404+
.equals(sourceCredentialsType)) {
405405
sourceCredentials =
406406
ServiceAccountCredentials.fromJson(sourceCredentialsJson, transportFactory);
407407
} else {
@@ -480,7 +480,7 @@ private ImpersonatedCredentials(Builder builder) throws IOException {
480480
throw new IllegalStateException("lifetime must be less than or equal to 43200");
481481
}
482482

483-
this.type = "Impersonated Credentials";
483+
this.name = GoogleCredentialsInfo.IMPERSONATED_CREDENTIALS.getCredentialName();
484484
this.principal = builder.targetPrincipal;
485485

486486
// Do not expect explicit universe domain, throw exception if the explicit universe domain

oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public class ServiceAccountCredentials extends GoogleCredentials
150150
this.lifetime = builder.lifetime;
151151
this.useJwtAccessWithScope = builder.useJwtAccessWithScope;
152152
this.defaultRetriesEnabled = builder.defaultRetriesEnabled;
153-
this.type = "Service Account Credentials";
153+
this.name = GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getCredentialName();
154154
this.principal = builder.clientEmail;
155155
}
156156

oauth2_http/java/com/google/auth/oauth2/ServiceAccountJwtAccessCredentials.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,17 @@ public static ServiceAccountJwtAccessCredentials fromStream(
296296
if (fileType == null) {
297297
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
298298
}
299-
if (GoogleCredentials.GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
299+
if (GoogleCredentials.GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS
300+
.getFileType()
301+
.equals(fileType)) {
300302
return fromJson(fileContents, defaultAudience);
301303
}
302304
throw new IOException(
303305
String.format(
304306
"Error reading credentials from stream, 'type' value '%s' not recognized."
305307
+ " Expecting '%s'.",
306-
fileType, GoogleCredentials.GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType()));
308+
fileType,
309+
GoogleCredentials.GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getFileType()));
307310
}
308311

309312
private LoadingCache<JwtClaims, JwtCredentials> createCache() {

0 commit comments

Comments
 (0)