Search in sources :

Example 16 with AssumeRoleRequest

use of com.amazonaws.services.securitytoken.model.AssumeRoleRequest in project cloud-pipeline by epam.

the class S3TemporaryCredentials method generate.

@Override
public AbstractTemporaryCredentials generate(List<DataStorageAction> actions) {
    String policy = createPolicyWithPermissions(actions);
    AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest().withDurationSeconds(getDuration()).withPolicy(policy).withRoleSessionName(sessionName).withRoleArn(getRole());
    AWSSecurityTokenServiceClientBuilder builder = AWSSecurityTokenServiceClientBuilder.standard();
    builder.setRegion(getAwsRegionId());
    builder.setCredentials(DefaultAWSCredentialsProviderChain.getInstance());
    AssumeRoleResult assumeRoleResult = builder.build().assumeRole(assumeRoleRequest);
    Credentials resultingCredentials = assumeRoleResult.getCredentials();
    setAccessKey(resultingCredentials.getSecretAccessKey());
    setKeyId(resultingCredentials.getAccessKeyId());
    setToken(resultingCredentials.getSessionToken());
    setExpirationTime(expirationTimeWithUTC(resultingCredentials.getExpiration()));
    setRegion(getAwsRegionId());
    return this;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) AWSSecurityTokenServiceClientBuilder(com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AbstractTemporaryCredentials(com.epam.pipeline.entity.datastorage.AbstractTemporaryCredentials) Credentials(com.amazonaws.services.securitytoken.model.Credentials)

Example 17 with AssumeRoleRequest

use of com.amazonaws.services.securitytoken.model.AssumeRoleRequest in project singleton by vmware.

the class S3Client method getRoleCredentials.

private synchronized Credentials getRoleCredentials() {
    AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(config.getAccessKey(), config.getSecretkey()));
    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(awsCredentialsProvider).withRegion(config.getS3Region()).build();
    AssumeRoleRequest arreq = new AssumeRoleRequest();
    arreq.setDurationSeconds(DURATIONSEC);
    arreq.setRoleArn(config.getRoleArn());
    arreq.setRoleSessionName("SingletonRoleSession");
    AssumeRoleResult sessionTokenResult = stsClient.assumeRole(arreq);
    long time = System.currentTimeMillis();
    Credentials result = sessionTokenResult.getCredentials();
    reducedTime = (result.getExpiration().getTime() - (DURATIONSEC * 1000) - time) + TIME_ERR_RANGE;
    return result;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Credentials(com.amazonaws.services.securitytoken.model.Credentials)

Example 18 with AssumeRoleRequest

use of com.amazonaws.services.securitytoken.model.AssumeRoleRequest in project athenz by AthenZ.

the class ZTSClient method getAssumeRoleRequest.

AssumeRoleRequest getAssumeRoleRequest(String account, String roleName) {
    // assume the target role to get the credentials for the client
    // aws format is arn:aws:iam::<account-id>:role/<role-name>
    final String arn = "arn:aws:iam::" + account + ":role/" + roleName;
    AssumeRoleRequest req = new AssumeRoleRequest();
    req.setRoleArn(arn);
    req.setRoleSessionName(roleName);
    return req;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) DERIA5String(org.bouncycastle.asn1.DERIA5String)

Example 19 with AssumeRoleRequest

use of com.amazonaws.services.securitytoken.model.AssumeRoleRequest in project cvs-auto-svc by dvsa.

the class AwsUtil method isCertificateCreated.

public static boolean isCertificateCreated(String testNumber, String vin, int iterations) {
    Regions clientRegion = Regions.EU_WEST_1;
    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build();
    String uuid = String.valueOf(UUID.randomUUID());
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(System.getProperty("AWS_ROLE")).withDurationSeconds(3600).withRoleSessionName(uuid);
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken());
    String bucketName = loader.getS3Bucket();
    String fileName = testNumber + "_" + vin + ".pdf";
    String key = loader.getBranchName() + "/" + fileName;
    AmazonS3 s3Client = new AmazonS3Client(temporaryCredentials);
    System.out.println("Waiting on file " + key + " to be created... on bucket: " + bucketName);
    System.out.println("time started checking " + DateTime.now().withZone(DateTimeZone.UTC));
    for (int i = 0; i < iterations; i++) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (s3Client.doesObjectExist(bucketName, key)) {
            System.out.println("time stopped checking " + DateTime.now().withZone(DateTimeZone.UTC));
            System.out.println("file found in the s3 bucket... after " + i + " iterations");
            return true;
        }
    }
    System.out.println("time stopped checking " + DateTime.now().withZone(DateTimeZone.UTC));
    System.out.println("file " + key + " was not created in 60 iterations or less...");
    return false;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Example 20 with AssumeRoleRequest

use of com.amazonaws.services.securitytoken.model.AssumeRoleRequest in project cvs-auto-svc by dvsa.

the class AwsUtil method getNextSystemNumberInSequence.

public static String getNextSystemNumberInSequence() {
    Regions clientRegion = Regions.EU_WEST_1;
    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build();
    String uuid = String.valueOf(UUID.randomUUID());
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(System.getProperty("AWS_ROLE")).withDurationSeconds(3600).withRoleSessionName(uuid);
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken());
    AmazonDynamoDBClient client = new AmazonDynamoDBClient(temporaryCredentials);
    client.setRegion(Region.getRegion(clientRegion));
    DynamoDB dynamoDB = new DynamoDB(client);
    String tableName = "cvs-" + loader.getBranchName() + "-test-number";
    Table table = dynamoDB.getTable(tableName);
    ItemCollection<ScanOutcome> items = // FilterExpression
    table.scan(// FilterExpression
    "attribute_exists(systemNumber)", // ProjectionExpression
    "systemNumber", // ExpressionAttributeNames - not used in this example
    null, // ExpressionAttributeValues - not used in this example
    null);
    System.out.println("Scan of " + tableName + " for items with systemNumber not null");
    Iterator<Item> iterator = items.iterator();
    String lastSystemNumberUsed = null;
    while (iterator.hasNext()) {
        lastSystemNumberUsed = GenericData.getValueFromJsonPath(iterator.next().toJSONPretty(), "$.systemNumber");
    }
    if (lastSystemNumberUsed != null) {
        int nextSystemNumberInSequence = Integer.parseInt(lastSystemNumberUsed) + 1;
        return Integer.toString(nextSystemNumberInSequence);
    } else {
        throw new AutomationException("No value found for last used systemNumber");
    }
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AutomationException(exceptions.AutomationException) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Aggregations

AssumeRoleRequest (com.amazonaws.services.securitytoken.model.AssumeRoleRequest)53 AssumeRoleResult (com.amazonaws.services.securitytoken.model.AssumeRoleResult)41 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)30 AWSSecurityTokenService (com.amazonaws.services.securitytoken.AWSSecurityTokenService)28 Regions (com.amazonaws.regions.Regions)13 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)11 Credentials (com.amazonaws.services.securitytoken.model.Credentials)11 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)10 AmazonDynamoDBClient (com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)10 AWSCredentials (com.amazonaws.auth.AWSCredentials)9 AWSSecurityTokenServiceClientBuilder (com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder)8 AutomationException (exceptions.AutomationException)7 ClientConfiguration (com.amazonaws.ClientConfiguration)6 AWSSecurityTokenServiceClient (com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient)6 AmazonServiceException (com.amazonaws.AmazonServiceException)4 Test (org.testng.annotations.Test)4 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)3 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)3 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)2 AnonymousAWSCredentials (com.amazonaws.auth.AnonymousAWSCredentials)2