-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support aws-s3 EC2 instance metadata authentication (#215)
- Loading branch information
1 parent
2f42744
commit 3e1a5b2
Showing
9 changed files
with
188 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
.vscode/ | ||
build/ | ||
bin/ | ||
.tmp/ | ||
data/ | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/epam/aidial/core/storage/CredentialProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.epam.aidial.core.storage; | ||
|
||
import org.jclouds.domain.Credentials; | ||
|
||
public interface CredentialProvider { | ||
|
||
Credentials getCredentials(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/epam/aidial/core/storage/DefaultCredentialProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.epam.aidial.core.storage; | ||
|
||
import org.jclouds.domain.Credentials; | ||
|
||
import java.util.Objects; | ||
|
||
public class DefaultCredentialProvider implements CredentialProvider { | ||
|
||
private final Credentials credentials; | ||
|
||
public DefaultCredentialProvider(String identity, String credential) { | ||
this.credentials = new Credentials(Objects.requireNonNull(identity), Objects.requireNonNull(credential)); | ||
} | ||
|
||
@Override | ||
public Credentials getCredentials() { | ||
return credentials; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/main/java/com/epam/aidial/core/storage/Ec2InstanceMetadataCredentialProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.epam.aidial.core.storage; | ||
|
||
import com.epam.aidial.core.util.ProxyUtil; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jclouds.aws.domain.SessionCredentials; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
|
||
/** | ||
* Implementation of EC2 Instance Metadata credentials provider by following | ||
* see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-metadata-v2-how-it-works.html">AWS spec</a> | ||
*/ | ||
@Slf4j | ||
public class Ec2InstanceMetadataCredentialProvider implements CredentialProvider { | ||
|
||
private static final String EC2_INSTANCE_METADATA_BASE_URL = "http://169.254.169.254/latest/"; | ||
private static final String EC2_INSTANCE_METADATA_CREDENTIALS_URL = EC2_INSTANCE_METADATA_BASE_URL + "meta-data/iam/security-credentials/"; | ||
private static final String EC2_TOKEN_TTL_HEADER_NAME = "X-aws-ec2-metadata-token-ttl-seconds"; | ||
private static final String EC2_METADATA_TOKEN_HEADER_NAME = "X-aws-ec2-metadata-token"; | ||
private static final Duration DEFAULT_REQUEST_TIMEOUT = Duration.of(10, ChronoUnit.SECONDS); | ||
|
||
private final HttpClient httpClient; | ||
|
||
private SessionCredentials credentials; | ||
|
||
public Ec2InstanceMetadataCredentialProvider(HttpClient httpClient) { | ||
this.httpClient = httpClient; | ||
} | ||
|
||
public Ec2InstanceMetadataCredentialProvider() { | ||
this(HttpClient.newHttpClient()); | ||
} | ||
|
||
@Override | ||
public synchronized SessionCredentials getCredentials() { | ||
try { | ||
// if token present and not expired | ||
if (credentials != null && credentials.getExpiration().isPresent() && Date.from(Instant.now()).after(credentials.getExpiration().get())) { | ||
return credentials; | ||
} | ||
String token = getToken(); | ||
String roleName = getRoleName(token); | ||
AwsCredentials awsCredentials = getAwsCredentials(token, roleName); | ||
|
||
credentials = SessionCredentials.builder() | ||
.accessKeyId(awsCredentials.getAccessKeyId()) | ||
.expiration(Date.from(Instant.parse(awsCredentials.getExpiration()))) | ||
.secretAccessKey(awsCredentials.getSecretAccessKey()) | ||
.sessionToken(awsCredentials.getToken()).build(); | ||
|
||
return credentials; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private String getToken() throws IOException, InterruptedException { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(EC2_INSTANCE_METADATA_BASE_URL + "api/token")) | ||
.setHeader(EC2_TOKEN_TTL_HEADER_NAME, "21600") | ||
.timeout(DEFAULT_REQUEST_TIMEOUT) | ||
.PUT(HttpRequest.BodyPublishers.noBody()) | ||
.build(); | ||
|
||
return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body(); | ||
} | ||
|
||
private String getRoleName(String token) throws IOException, InterruptedException { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(EC2_INSTANCE_METADATA_CREDENTIALS_URL)) | ||
.setHeader(EC2_METADATA_TOKEN_HEADER_NAME, token) | ||
.timeout(DEFAULT_REQUEST_TIMEOUT) | ||
.GET() | ||
.build(); | ||
|
||
return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body(); | ||
} | ||
|
||
private AwsCredentials getAwsCredentials(String token, String roleName) throws IOException, InterruptedException { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(EC2_INSTANCE_METADATA_CREDENTIALS_URL + roleName)) | ||
.setHeader(EC2_METADATA_TOKEN_HEADER_NAME, token) | ||
.timeout(DEFAULT_REQUEST_TIMEOUT) | ||
.GET() | ||
.build(); | ||
|
||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
return ProxyUtil.convertToObject(response.body(), AwsCredentials.class); | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class) | ||
static class AwsCredentials { | ||
String code; | ||
String lastUpdated; | ||
String type; | ||
String accessKeyId; | ||
String secretAccessKey; | ||
String token; | ||
String expiration; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/epam/aidial/core/storage/StorageProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.epam.aidial.core.storage; | ||
|
||
public enum StorageProvider { | ||
S3, AWS_S3, FILESYSTEM, GOOGLE_CLOUD_STORAGE, AZURE_BLOB; | ||
|
||
public static StorageProvider from(String storageProviderName) { | ||
return switch (storageProviderName) { | ||
case "s3" -> S3; | ||
case "aws-s3" -> AWS_S3; | ||
case "azureblob" -> AZURE_BLOB; | ||
case "google-cloud-storage" -> GOOGLE_CLOUD_STORAGE; | ||
case "filesystem" -> FILESYSTEM; | ||
default -> throw new IllegalArgumentException("Unknown storage provider"); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters