-
Notifications
You must be signed in to change notification settings - Fork 487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add IAM AWS credential provider #1038
add IAM AWS credential provider #1038
Conversation
9cc7580
to
75aea94
Compare
75aea94
to
42e5866
Compare
} | ||
|
||
HttpUrl url = this.customEndpoint; | ||
if (getProperty("AWS_WEB_IDENTITY_TOKEN_FILE") != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To making this call twice with same argument, we can save the output of thisgetProperty
in a variable tokenFile
and pass it as argument to getToken
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to fetchCredentials(String tokenFile)
.
if (url == null) { | ||
String region = getProperty("AWS_REGION"); | ||
url = | ||
HttpUrl.parse( | ||
(region == null) | ||
? "https://sts.amazonaws.com" | ||
: "https://sts." + region + ".amazonaws.com"); | ||
} | ||
|
||
Provider provider = | ||
new WebIdentityProvider( | ||
() -> getToken(), | ||
url.toString(), | ||
null, | ||
null, | ||
getProperty("AWS_ROLE_ARN"), | ||
getProperty("AWS_ROLE_SESSION_NAME"), | ||
httpClient); | ||
credentials = provider.fetch(); | ||
return credentials; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be extracted out into a method fetchUsingAwsTokenFile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to fetchCredentials(String tokenFile)
.
if (getProperty("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI") != null) { | ||
if (url == null) { | ||
url = | ||
new HttpUrl.Builder() | ||
.scheme("http") | ||
.host("169.254.170.2") | ||
.addPathSegments(getProperty("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI")) | ||
.build(); | ||
} | ||
} else if (getProperty("AWS_CONTAINER_CREDENTIALS_FULL_URI") != null) { | ||
if (url == null) { | ||
url = HttpUrl.parse(getProperty("AWS_CONTAINER_CREDENTIALS_FULL_URI")); | ||
} | ||
checkLoopbackHost(url); | ||
} else { | ||
if (url == null) { | ||
url = HttpUrl.parse("http://169.254.169.254/latest/meta-data/iam/security-credentials/"); | ||
} else { | ||
url = | ||
new HttpUrl.Builder() | ||
.scheme(url.scheme()) | ||
.host(url.host()) | ||
.addPathSegments("latest/meta-data/iam/security-credentials/") | ||
.build(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be extracted into a method getAwsCredentialsUrl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to getIamRoleNamedUrl()
String[] roleNames = null; | ||
try (Response response = | ||
httpClient | ||
.newCall(new Request.Builder().url(url).method("GET", null).build()) | ||
.execute()) { | ||
if (!response.isSuccessful()) { | ||
throw new IllegalStateException(url + " failed with HTTP status code " + response.code()); | ||
} | ||
|
||
roleNames = response.body().string().split("\\R"); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Unable to parse response", e); | ||
} | ||
|
||
if (roleNames.length == 0) { | ||
throw new IllegalStateException("No IAM roles attached to EC2 service " + url); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be extracted into a method getAwsRoleName
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to getIamRoleName()
try { | ||
for (InetAddress addr : InetAddress.getAllByName(url.host())) { | ||
if (!addr.isLoopbackAddress()) { | ||
throw new IllegalArgumentException(url.host() + " is not loopback only host"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if IllegalArgumentException
is the right choice for all the exceptions in this class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using IllegalStateException()
now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I wrote this comment at the wrong place with the wrong exception name! I meant to say that I'm not sure if IllegalStateException
makes sense for most of the exceptions in the class..
1c5c1b2
to
2f3cd92
Compare
2f3cd92
to
3454c1d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
No description provided.