Skip to content
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

Merged

Conversation

balamurugana
Copy link
Member

No description provided.

@balamurugana balamurugana force-pushed the add-iam-aws-credential-provider branch 3 times, most recently from 9cc7580 to 75aea94 Compare August 21, 2020 11:21
@balamurugana balamurugana force-pushed the add-iam-aws-credential-provider branch from 75aea94 to 42e5866 Compare September 3, 2020 10:38
}

HttpUrl url = this.customEndpoint;
if (getProperty("AWS_WEB_IDENTITY_TOKEN_FILE") != null) {
Copy link
Contributor

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

Copy link
Member Author

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).

Comment on lines 114 to 175
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;
Copy link
Contributor

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

Copy link
Member Author

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).

Comment on lines 136 to 160
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();
}
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to getIamRoleNamedUrl()

Comment on lines 162 to 178
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);
}
Copy link
Contributor

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

Copy link
Member Author

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");
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using IllegalStateException() now.

Copy link
Contributor

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..

@balamurugana balamurugana force-pushed the add-iam-aws-credential-provider branch from 1c5c1b2 to 2f3cd92 Compare September 6, 2020 16:10
@balamurugana balamurugana force-pushed the add-iam-aws-credential-provider branch from 2f3cd92 to 3454c1d Compare September 6, 2020 16:21
Copy link
Contributor

@anjalshireesh anjalshireesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@sinhaashish sinhaashish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants