Skip to content

Commit bd2713b

Browse files
mario-vimalaeitzmanlsiracrenovate-botlqiu96
authored
Fix: Make derived classes of CredentialSource public (#1236)
* fix: Make derived classes of Credential Source public * fix: Lint * move nested public class outside * make CredentialSource public * Rename CredentialSource to ExternalAccountCredentialSource * fix format * Move ExternalAccountCredentialSource back to ExternalAccountCredential * Fix comment * optimize imports * chore(deps): update dependency com.google.auto.service:auto-service-annotations to v1.1.1 (#1253) Co-authored-by: Lawrence Qiu <lqiu96@gmail.com> --------- Co-authored-by: aeitzman <12433791+aeitzman@users.noreply.github.com> Co-authored-by: Leo <39062083+lsirac@users.noreply.github.com> Co-authored-by: Mend Renovate <bot@renovateapp.com> Co-authored-by: Lawrence Qiu <lqiu96@gmail.com>
1 parent a34a86c commit bd2713b

11 files changed

+370
-279
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package com.google.auth.oauth2;
33+
34+
import java.util.Map;
35+
import java.util.regex.Matcher;
36+
import java.util.regex.Pattern;
37+
38+
/** The AWS credential source. Stores data required to retrieve the AWS credential. */
39+
public class AwsCredentialSource extends ExternalAccountCredentials.CredentialSource {
40+
41+
static final String IMDSV2_SESSION_TOKEN_URL_FIELD_NAME = "imdsv2_session_token_url";
42+
static final long serialVersionUID = -4180558200808134436L;
43+
44+
final String regionUrl;
45+
final String url;
46+
final String regionalCredentialVerificationUrl;
47+
final String imdsv2SessionTokenUrl;
48+
49+
/**
50+
* The source of the AWS credential. The credential source map must contain the
51+
* `regional_cred_verification_url` field.
52+
*
53+
* <p>The `regional_cred_verification_url` is the regional GetCallerIdentity action URL, used to
54+
* determine the account ID and its roles.
55+
*
56+
* <p>The `environment_id` is the environment identifier, in the format “aws${version}”. This
57+
* indicates whether breaking changes were introduced to the underlying AWS implementation.
58+
*
59+
* <p>The `region_url` identifies the targeted region. Optional.
60+
*
61+
* <p>The `url` locates the metadata server used to retrieve the AWS credentials. Optional.
62+
*/
63+
public AwsCredentialSource(Map<String, Object> credentialSourceMap) {
64+
super(credentialSourceMap);
65+
if (!credentialSourceMap.containsKey("regional_cred_verification_url")) {
66+
throw new IllegalArgumentException(
67+
"A regional_cred_verification_url representing the"
68+
+ " GetCallerIdentity action URL must be specified.");
69+
}
70+
71+
String environmentId = (String) credentialSourceMap.get("environment_id");
72+
73+
// Environment version is prefixed by "aws". e.g. "aws1".
74+
Matcher matcher = Pattern.compile("(aws)([\\d]+)").matcher(environmentId);
75+
if (!matcher.matches()) {
76+
throw new IllegalArgumentException("Invalid AWS environment ID.");
77+
}
78+
79+
int environmentVersion = Integer.parseInt(matcher.group(2));
80+
if (environmentVersion != 1) {
81+
throw new IllegalArgumentException(
82+
String.format(
83+
"AWS version %s is not supported in the current build.", environmentVersion));
84+
}
85+
86+
this.regionUrl = (String) credentialSourceMap.get("region_url");
87+
this.url = (String) credentialSourceMap.get("url");
88+
this.regionalCredentialVerificationUrl =
89+
(String) credentialSourceMap.get("regional_cred_verification_url");
90+
91+
if (credentialSourceMap.containsKey(IMDSV2_SESSION_TOKEN_URL_FIELD_NAME)) {
92+
this.imdsv2SessionTokenUrl =
93+
(String) credentialSourceMap.get(IMDSV2_SESSION_TOKEN_URL_FIELD_NAME);
94+
} else {
95+
this.imdsv2SessionTokenUrl = null;
96+
}
97+
}
98+
}

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

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
import java.util.HashMap;
5151
import java.util.List;
5252
import java.util.Map;
53-
import java.util.regex.Matcher;
54-
import java.util.regex.Pattern;
5553
import javax.annotation.Nullable;
5654

5755
/**
@@ -73,71 +71,6 @@ public class AwsCredentials extends ExternalAccountCredentials {
7371
static final String AWS_IMDSV2_SESSION_TOKEN_TTL = "300";
7472
private static final long serialVersionUID = -3670131891574618105L;
7573

76-
/**
77-
* The AWS credential source. Stores data required to retrieve the AWS credential from the AWS
78-
* metadata server.
79-
*/
80-
static class AwsCredentialSource extends CredentialSource {
81-
82-
private static final String IMDSV2_SESSION_TOKEN_URL_FIELD_NAME = "imdsv2_session_token_url";
83-
private static final long serialVersionUID = -4180558200808134436L;
84-
85-
private final String regionUrl;
86-
private final String url;
87-
private final String regionalCredentialVerificationUrl;
88-
private final String imdsv2SessionTokenUrl;
89-
90-
/**
91-
* The source of the AWS credential. The credential source map must contain the
92-
* `regional_cred_verification_url` field.
93-
*
94-
* <p>The `regional_cred_verification_url` is the regional GetCallerIdentity action URL, used to
95-
* determine the account ID and its roles.
96-
*
97-
* <p>The `environment_id` is the environment identifier, in the format “aws${version}”. This
98-
* indicates whether breaking changes were introduced to the underlying AWS implementation.
99-
*
100-
* <p>The `region_url` identifies the targeted region. Optional.
101-
*
102-
* <p>The `url` locates the metadata server used to retrieve the AWS credentials. Optional.
103-
*/
104-
AwsCredentialSource(Map<String, Object> credentialSourceMap) {
105-
super(credentialSourceMap);
106-
if (!credentialSourceMap.containsKey("regional_cred_verification_url")) {
107-
throw new IllegalArgumentException(
108-
"A regional_cred_verification_url representing the"
109-
+ " GetCallerIdentity action URL must be specified.");
110-
}
111-
112-
String environmentId = (String) credentialSourceMap.get("environment_id");
113-
114-
// Environment version is prefixed by "aws". e.g. "aws1".
115-
Matcher matcher = Pattern.compile("(aws)([\\d]+)").matcher(environmentId);
116-
if (!matcher.matches()) {
117-
throw new IllegalArgumentException("Invalid AWS environment ID.");
118-
}
119-
120-
int environmentVersion = Integer.parseInt(matcher.group(2));
121-
if (environmentVersion != 1) {
122-
throw new IllegalArgumentException(
123-
String.format(
124-
"AWS version %s is not supported in the current build.", environmentVersion));
125-
}
126-
127-
this.regionUrl = (String) credentialSourceMap.get("region_url");
128-
this.url = (String) credentialSourceMap.get("url");
129-
this.regionalCredentialVerificationUrl =
130-
(String) credentialSourceMap.get("regional_cred_verification_url");
131-
132-
if (credentialSourceMap.containsKey(IMDSV2_SESSION_TOKEN_URL_FIELD_NAME)) {
133-
this.imdsv2SessionTokenUrl =
134-
(String) credentialSourceMap.get(IMDSV2_SESSION_TOKEN_URL_FIELD_NAME);
135-
} else {
136-
this.imdsv2SessionTokenUrl = null;
137-
}
138-
}
139-
}
140-
14174
private final AwsCredentialSource awsCredentialSource;
14275

14376
/** Internal constructor. See {@link AwsCredentials.Builder}. */

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
import com.google.api.client.json.JsonObjectParser;
3939
import com.google.auth.RequestMetadataCallback;
4040
import com.google.auth.http.HttpTransportFactory;
41-
import com.google.auth.oauth2.AwsCredentials.AwsCredentialSource;
42-
import com.google.auth.oauth2.IdentityPoolCredentials.IdentityPoolCredentialSource;
43-
import com.google.auth.oauth2.PluggableAuthCredentials.PluggableAuthCredentialSource;
4441
import com.google.common.base.MoreObjects;
4542
import java.io.IOException;
4643
import java.io.InputStream;
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package com.google.auth.oauth2;
33+
34+
import java.util.HashMap;
35+
import java.util.Locale;
36+
import java.util.Map;
37+
import javax.annotation.Nullable;
38+
39+
/**
40+
* The IdentityPool credential source. Dictates the retrieval method of the external credential,
41+
* which can either be through a metadata server or a local file.
42+
*/
43+
public class IdentityPoolCredentialSource extends ExternalAccountCredentials.CredentialSource {
44+
45+
private static final long serialVersionUID = -745855247050085694L;
46+
IdentityPoolCredentialSourceType credentialSourceType;
47+
CredentialFormatType credentialFormatType;
48+
String credentialLocation;
49+
@Nullable String subjectTokenFieldName;
50+
@Nullable Map<String, String> headers;
51+
52+
/**
53+
* The source of the 3P credential.
54+
*
55+
* <p>If this is a file based 3P credential, the credentials file can be retrieved using the
56+
* `file` key.
57+
*
58+
* <p>If this is URL-based 3p credential, the metadata server URL can be retrieved using the `url`
59+
* key.
60+
*
61+
* <p>The third party credential can be provided in different formats, such as text or JSON. The
62+
* format can be specified using the `format` header, which returns a map with keys `type` and
63+
* `subject_token_field_name`. If the `type` is json, the `subject_token_field_name` must be
64+
* provided. If no format is provided, we expect the token to be in the raw text format.
65+
*
66+
* <p>Optional headers can be present, and should be keyed by `headers`.
67+
*/
68+
public IdentityPoolCredentialSource(Map<String, Object> credentialSourceMap) {
69+
super(credentialSourceMap);
70+
71+
if (credentialSourceMap.containsKey("file") && credentialSourceMap.containsKey("url")) {
72+
throw new IllegalArgumentException(
73+
"Only one credential source type can be set, either file or url.");
74+
}
75+
76+
if (credentialSourceMap.containsKey("file")) {
77+
credentialLocation = (String) credentialSourceMap.get("file");
78+
credentialSourceType = IdentityPoolCredentialSourceType.FILE;
79+
} else if (credentialSourceMap.containsKey("url")) {
80+
credentialLocation = (String) credentialSourceMap.get("url");
81+
credentialSourceType = IdentityPoolCredentialSourceType.URL;
82+
} else {
83+
throw new IllegalArgumentException(
84+
"Missing credential source file location or URL. At least one must be specified.");
85+
}
86+
87+
Map<String, String> headersMap = (Map<String, String>) credentialSourceMap.get("headers");
88+
if (headersMap != null && !headersMap.isEmpty()) {
89+
headers = new HashMap<>();
90+
headers.putAll(headersMap);
91+
}
92+
93+
// If the format is not provided, we expect the token to be in the raw text format.
94+
credentialFormatType = CredentialFormatType.TEXT;
95+
96+
Map<String, String> formatMap = (Map<String, String>) credentialSourceMap.get("format");
97+
if (formatMap != null && formatMap.containsKey("type")) {
98+
String type = formatMap.get("type");
99+
100+
if (type != null && "json".equals(type.toLowerCase(Locale.US))) {
101+
// For JSON, the subject_token field name must be provided.
102+
if (!formatMap.containsKey("subject_token_field_name")) {
103+
throw new IllegalArgumentException(
104+
"When specifying a JSON credential type, the subject_token_field_name must be set.");
105+
}
106+
credentialFormatType = CredentialFormatType.JSON;
107+
subjectTokenFieldName = formatMap.get("subject_token_field_name");
108+
} else if (type != null && "text".equals(type.toLowerCase(Locale.US))) {
109+
credentialFormatType = CredentialFormatType.TEXT;
110+
} else {
111+
throw new IllegalArgumentException(
112+
String.format("Invalid credential source format type: %s.", type));
113+
}
114+
}
115+
}
116+
117+
boolean hasHeaders() {
118+
return headers != null && !headers.isEmpty();
119+
}
120+
121+
enum IdentityPoolCredentialSourceType {
122+
FILE,
123+
URL
124+
}
125+
126+
enum CredentialFormatType {
127+
TEXT,
128+
JSON
129+
}
130+
}

0 commit comments

Comments
 (0)