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

IamAwsProvider: fix custom endpoint having non-default port #1523

Merged
merged 5 commits into from
Mar 2, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions api/src/main/java/io/minio/credentials/IamAwsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
* for Amazon EC2</a>.
*/
public class IamAwsProvider extends EnvironmentProvider {

private static final int DEFAULT_PORT_HTTP = 80;
private static final int DEFAULT_PORT_HTTPS = 443;
private static final String SCHEME_HTTP = "http";
private static final String SCHEME_HTTPS = "https";
balamurugana marked this conversation as resolved.
Show resolved Hide resolved
// Custom endpoint to fetch IAM role credentials.
private final HttpUrl customEndpoint;
private final OkHttpClient httpClient;
Expand Down Expand Up @@ -136,12 +141,9 @@ private String fetchImdsToken() {
if (url == null) {
url = HttpUrl.parse("http://169.254.169.254/latest/api/token");
} else {
url =
new HttpUrl.Builder()
.scheme(url.scheme())
.host(url.host())
.addPathSegments("latest/api/token")
.build();
HttpUrl.Builder urlBuilder = new HttpUrl.Builder().scheme(url.scheme()).host(url.host());
setPortIfRequired(urlBuilder, url);
url = urlBuilder.addPathSegments("latest/api/token").build();
balamurugana marked this conversation as resolved.
Show resolved Hide resolved
}
String token = "";
Request request =
Expand All @@ -158,6 +160,21 @@ private String fetchImdsToken() {
return token;
}

private void setPortIfRequired(HttpUrl.Builder builder, HttpUrl sourceUrl) {
if (shouldAddPortToUrl(sourceUrl)) {
builder.port(sourceUrl.port());
}
}

private boolean shouldAddPortToUrl(HttpUrl sourceUrl) {
String scheme = sourceUrl.scheme();
int port = sourceUrl.port();
boolean isDefaultPortForProtocolUsed =
(SCHEME_HTTP.equals(scheme) && port == DEFAULT_PORT_HTTP)
|| (SCHEME_HTTPS.equals(scheme) && port == DEFAULT_PORT_HTTPS);
return !isDefaultPortForProtocolUsed;
}

balamurugana marked this conversation as resolved.
Show resolved Hide resolved
private String getIamRoleName(HttpUrl url, String token) {
String[] roleNames = null;
Request.Builder builder = new Request.Builder().url(url).method("GET", null);
Expand All @@ -184,12 +201,9 @@ private HttpUrl getIamRoleNamedUrl(String token) {
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();
HttpUrl.Builder urlBuilder = new HttpUrl.Builder().scheme(url.scheme()).host(url.host());
setPortIfRequired(urlBuilder, url);
url = urlBuilder.addPathSegments("latest/meta-data/iam/security-credentials/").build();
balamurugana marked this conversation as resolved.
Show resolved Hide resolved
}

String roleName = getIamRoleName(url, token);
Expand Down
Loading