-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We will use the regional endpoint with STS whenever possible. (#45)
* We will use the regional endpoint with STS whenever possible. The global endpoint is used in case the region cannot be found. #44 * STSClient is now an injectable. This is necessary to consult the ApplicationConfiguration object to create the AWS STS client. * Added some more comments. * Incorporated the PR comments. - Renamed constant to use regional STS endpoint - Added some comments about the constant * Default to global endpoint if we can't find the AWS region.
- Loading branch information
1 parent
246d139
commit e4a9c4d
Showing
9 changed files
with
124 additions
and
39 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
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
12 changes: 12 additions & 0 deletions
12
emr-user-role-mapper-application/src/main/java/com/amazon/aws/emr/credentials/STSClient.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,12 @@ | ||
package com.amazon.aws.emr.credentials; | ||
|
||
import com.amazonaws.services.securitytoken.model.AssumeRoleRequest; | ||
import com.amazonaws.services.securitytoken.model.AssumeRoleResult; | ||
|
||
/** | ||
* Interface representing the AWS STS client. | ||
*/ | ||
public interface STSClient { | ||
|
||
AssumeRoleResult assumeRole(AssumeRoleRequest assumeRoleRequest); | ||
} |
64 changes: 64 additions & 0 deletions
64
...r-role-mapper-application/src/main/java/com/amazon/aws/emr/credentials/STSClientImpl.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,64 @@ | ||
package com.amazon.aws.emr.credentials; | ||
|
||
import com.amazon.aws.emr.ApplicationConfiguration; | ||
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; | ||
import com.amazonaws.regions.Region; | ||
import com.amazonaws.regions.Regions; | ||
import com.amazonaws.services.securitytoken.AWSSecurityTokenService; | ||
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; | ||
import com.amazonaws.services.securitytoken.model.AssumeRoleRequest; | ||
import com.amazonaws.services.securitytoken.model.AssumeRoleResult; | ||
import javax.annotation.PostConstruct; | ||
import javax.inject.Inject; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.glassfish.hk2.api.Immediate; | ||
|
||
/** | ||
* Creates a custom AWS STS client based on {@link ApplicationConfiguration} | ||
*/ | ||
@Slf4j | ||
@Immediate | ||
public class STSClientImpl implements STSClient { | ||
// If using regional configurations we will use us-west-2 as default | ||
// This is primarily used in integration tests | ||
private String regionString = "us-west-2"; | ||
|
||
@Inject | ||
ApplicationConfiguration applicationConfiguration; | ||
|
||
AWSSecurityTokenService stsClient; | ||
|
||
@PostConstruct | ||
void init() { | ||
if (applicationConfiguration.isRegionalStsEnabled()) { | ||
Region region = null; | ||
try { | ||
region = Regions.getCurrentRegion(); | ||
regionString = region.getName(); | ||
String endpoint = String.format("https://sts.%s.amazonaws.com", regionString); | ||
log.info("Running the application with regional STS endpoint " + endpoint); | ||
stsClient = AWSSecurityTokenServiceClientBuilder | ||
.standard() | ||
.withEndpointConfiguration(new EndpointConfiguration(endpoint, regionString)) | ||
.build(); | ||
} catch (Exception e) { | ||
log.error("Cannot determine the AWS region. Defaulting to global endpoint."); | ||
createGlobalEndpointClient(); | ||
} | ||
} else { | ||
createGlobalEndpointClient(); | ||
} | ||
} | ||
|
||
private void createGlobalEndpointClient() { | ||
log.info("Running the application with global STS endpoint."); | ||
stsClient = AWSSecurityTokenServiceClientBuilder | ||
.standard() | ||
.build(); | ||
} | ||
|
||
@Override | ||
public AssumeRoleResult assumeRole(AssumeRoleRequest assumeRoleRequest) { | ||
return stsClient.assumeRole(assumeRoleRequest); | ||
} | ||
} |
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
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