-
Notifications
You must be signed in to change notification settings - Fork 193
SNOW-1902245: Add AWS Workload Identity Federation #2135
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dc3cb66
SNOW-1902245: Add workflow identity federation
sfc-gh-dheyman 885456e
Conflicts resolved
sfc-gh-dheyman 7a13b16
Add sts dependency to public pom.xml
sfc-gh-dheyman 04731e9
CR suggestions applied
sfc-gh-dheyman 3640a53
Remove gson dependency
sfc-gh-dheyman 56f28e5
reformat
sfc-gh-dheyman 332be8e
CR suggestions
sfc-gh-dheyman 5f0d399
Add comments
sfc-gh-dheyman d6d8e46
CR suggestions
sfc-gh-dheyman 44663b8
Fix checkstyle
sfc-gh-dheyman 3d9eec4
Merge branch 'master' into SNOW-1902245-wif
sfc-gh-dheyman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,5 +18,6 @@ public enum ClientAuthnParameter { | |
| SESSION_PARAMETERS, | ||
| PROOF_KEY, | ||
| TOKEN, | ||
| OAUTH_TYPE | ||
| OAUTH_TYPE, | ||
| PROVIDER | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
src/main/java/net/snowflake/client/core/auth/wif/AWSAttestationService.java
This file contains hidden or 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,53 @@ | ||
| package net.snowflake.client.core.auth.wif; | ||
|
|
||
| import com.amazonaws.SignableRequest; | ||
| import com.amazonaws.auth.AWS4Signer; | ||
| import com.amazonaws.auth.AWSCredentials; | ||
| import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; | ||
| import com.amazonaws.regions.InstanceMetadataRegionProvider; | ||
| import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; | ||
| import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest; | ||
| import com.amazonaws.services.securitytoken.model.GetCallerIdentityResult; | ||
| import java.util.Optional; | ||
| import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
| import net.snowflake.client.jdbc.EnvironmentVariables; | ||
| import net.snowflake.client.jdbc.SnowflakeUtil; | ||
|
|
||
| @SnowflakeJdbcInternalApi | ||
| public class AWSAttestationService { | ||
|
|
||
| private static final String SECURE_TOKEN_SERVICE_NAME = "sts"; | ||
| private static boolean regionInitialized = false; | ||
| private static String region; | ||
|
|
||
| private final AWS4Signer aws4Signer; | ||
|
|
||
| public AWSAttestationService() { | ||
| aws4Signer = new AWS4Signer(); | ||
| aws4Signer.setServiceName(SECURE_TOKEN_SERVICE_NAME); | ||
| } | ||
|
|
||
| AWSCredentials getAWSCredentials() { | ||
| return DefaultAWSCredentialsProviderChain.getInstance().getCredentials(); | ||
| } | ||
|
|
||
| String getAWSRegion() { | ||
| if (!regionInitialized) { | ||
| String envRegion = SnowflakeUtil.systemGetEnv(EnvironmentVariables.AWS_REGION.getName()); | ||
| region = envRegion != null ? envRegion : new InstanceMetadataRegionProvider().getRegion(); | ||
|
sfc-gh-pfus marked this conversation as resolved.
|
||
| regionInitialized = true; | ||
|
sfc-gh-pmotacki marked this conversation as resolved.
|
||
| } | ||
| return region; | ||
| } | ||
|
|
||
| String getArn() { | ||
| GetCallerIdentityResult callerIdentity = | ||
| AWSSecurityTokenServiceClientBuilder.defaultClient() | ||
| .getCallerIdentity(new GetCallerIdentityRequest()); | ||
| return Optional.ofNullable(callerIdentity).map(GetCallerIdentityResult::getArn).orElse(null); | ||
|
sfc-gh-pmotacki marked this conversation as resolved.
|
||
| } | ||
|
|
||
| void signRequestWithSigV4(SignableRequest<Void> signableRequest, AWSCredentials awsCredentials) { | ||
| aws4Signer.sign(signableRequest, awsCredentials); | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
src/main/java/net/snowflake/client/core/auth/wif/AwsIdentityAttestationCreator.java
This file contains hidden or 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,81 @@ | ||
| package net.snowflake.client.core.auth.wif; | ||
|
|
||
| import com.amazonaws.DefaultRequest; | ||
| import com.amazonaws.Request; | ||
| import com.amazonaws.auth.AWSCredentials; | ||
| import com.amazonaws.http.HttpMethodName; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
| import java.util.Collections; | ||
| import net.minidev.json.JSONObject; | ||
| import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
| import net.snowflake.client.log.SFLogger; | ||
| import net.snowflake.client.log.SFLoggerFactory; | ||
|
|
||
| @SnowflakeJdbcInternalApi | ||
| public class AwsIdentityAttestationCreator implements WorkloadIdentityAttestationCreator { | ||
|
|
||
| private static final SFLogger logger = | ||
| SFLoggerFactory.getLogger(AwsIdentityAttestationCreator.class); | ||
|
|
||
| private static final String SNOWFLAKE_AUDIENCE_HEADER_NAME = "X-Snowflake-Audience"; | ||
| private static final String SNOWFLAKE_AUDIENCE = "snowflakecomputing.com"; | ||
|
|
||
| private final AWSAttestationService attestationService; | ||
|
sfc-gh-pmotacki marked this conversation as resolved.
|
||
|
|
||
| public AwsIdentityAttestationCreator(AWSAttestationService attestationService) { | ||
| this.attestationService = attestationService; | ||
| } | ||
|
|
||
| @Override | ||
| public WorkloadIdentityAttestation createAttestation() { | ||
| logger.debug("Creating AWS identity attestation..."); | ||
| AWSCredentials awsCredentials = attestationService.getAWSCredentials(); | ||
| if (awsCredentials == null) { | ||
| logger.debug("No AWS credentials were found."); | ||
| return null; | ||
| } | ||
| String region = attestationService.getAWSRegion(); | ||
| if (region == null) { | ||
| logger.debug("No AWS region was found."); | ||
| return null; | ||
| } | ||
| String arn = attestationService.getArn(); | ||
| if (arn == null) { | ||
| logger.debug("No Caller Identity was found."); | ||
| return null; | ||
| } | ||
|
|
||
| String stsHostname = String.format("sts.%s.amazonaws.com", region); | ||
| Request<Void> request = createStsRequest(stsHostname); | ||
| attestationService.signRequestWithSigV4(request, awsCredentials); | ||
|
|
||
| String credential = createBase64EncodedRequestCredential(request); | ||
| return new WorkloadIdentityAttestation( | ||
| WorkloadIdentityProviderType.AWS, credential, Collections.singletonMap("arn", arn)); | ||
| } | ||
|
|
||
| private Request<Void> createStsRequest(String hostname) { | ||
| Request<Void> request = new DefaultRequest<>("sts"); | ||
| request.setHttpMethod(HttpMethodName.POST); | ||
| request.setEndpoint( | ||
|
sfc-gh-pmotacki marked this conversation as resolved.
|
||
| URI.create( | ||
| String.format("https://%s/?Action=GetCallerIdentity&Version=2011-06-15", hostname))); | ||
| request.addHeader("Host", hostname); | ||
| request.addHeader(SNOWFLAKE_AUDIENCE_HEADER_NAME, SNOWFLAKE_AUDIENCE); | ||
| return request; | ||
| } | ||
|
|
||
| private String createBase64EncodedRequestCredential(Request<Void> request) { | ||
| JSONObject assertionJson = new JSONObject(); | ||
| JSONObject headers = new JSONObject(); | ||
| headers.putAll(request.getHeaders()); | ||
| assertionJson.put("url", request.getEndpoint().toString()); | ||
| assertionJson.put("method", request.getHttpMethod().toString()); | ||
| assertionJson.put("headers", headers); | ||
|
|
||
| String assertionJsonString = assertionJson.toString(); | ||
| return Base64.getEncoder().encodeToString(assertionJsonString.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/main/java/net/snowflake/client/core/auth/wif/AzureIdentityAttestationCreator.java
This file contains hidden or 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 net.snowflake.client.core.auth.wif; | ||
|
|
||
| import net.snowflake.client.core.SFException; | ||
| import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
| import net.snowflake.client.jdbc.ErrorCode; | ||
| import net.snowflake.client.log.SFLogger; | ||
| import net.snowflake.client.log.SFLoggerFactory; | ||
|
|
||
| @SnowflakeJdbcInternalApi | ||
| public class AzureIdentityAttestationCreator implements WorkloadIdentityAttestationCreator { | ||
|
|
||
| private static final SFLogger logger = | ||
| SFLoggerFactory.getLogger(AzureIdentityAttestationCreator.class); | ||
|
|
||
| @Override | ||
| public WorkloadIdentityAttestation createAttestation() throws SFException { | ||
| throw new SFException(ErrorCode.FEATURE_UNSUPPORTED, "Azure Workload Identity not supported"); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/net/snowflake/client/core/auth/wif/GcpIdentityAttestationCreator.java
This file contains hidden or 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 net.snowflake.client.core.auth.wif; | ||
|
|
||
| import net.snowflake.client.core.SFException; | ||
| import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
| import net.snowflake.client.jdbc.ErrorCode; | ||
| import net.snowflake.client.log.SFLogger; | ||
| import net.snowflake.client.log.SFLoggerFactory; | ||
|
|
||
| @SnowflakeJdbcInternalApi | ||
| public class GcpIdentityAttestationCreator implements WorkloadIdentityAttestationCreator { | ||
|
|
||
| private static final SFLogger logger = | ||
| SFLoggerFactory.getLogger(GcpIdentityAttestationCreator.class); | ||
|
|
||
| @Override | ||
| public WorkloadIdentityAttestation createAttestation() throws SFException { | ||
| throw new SFException(ErrorCode.FEATURE_UNSUPPORTED, "GCP Workload Identity not supported"); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/net/snowflake/client/core/auth/wif/OidcIdentityAttestationCreator.java
This file contains hidden or 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 net.snowflake.client.core.auth.wif; | ||
|
|
||
| import net.snowflake.client.core.SFException; | ||
| import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
| import net.snowflake.client.jdbc.ErrorCode; | ||
| import net.snowflake.client.log.SFLogger; | ||
| import net.snowflake.client.log.SFLoggerFactory; | ||
|
|
||
| @SnowflakeJdbcInternalApi | ||
| public class OidcIdentityAttestationCreator implements WorkloadIdentityAttestationCreator { | ||
|
|
||
| private static final SFLogger logger = | ||
| SFLoggerFactory.getLogger(OidcIdentityAttestationCreator.class); | ||
|
|
||
| @Override | ||
| public WorkloadIdentityAttestation createAttestation() throws SFException { | ||
| throw new SFException(ErrorCode.FEATURE_UNSUPPORTED, "OIDC Workload Identity not supported"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.