Search in sources :

Example 51 with AssumeRoleRequest

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

the class AwsUtil method insertVehicle.

public static void insertVehicle(String jsonBody) {
    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() + "-technical-records";
    Table table = dynamoDB.getTable(tableName);
    String sysNo = GenericData.getValueFromJsonPath(jsonBody, "$.systemNumber");
    try {
        Item item = Item.fromJSON(jsonBody);
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table.putItem(item);
        System.out.println("PutItem succeeded:\n" + item.toJSONPretty());
    } catch (Exception e) {
        System.err.println("Unable to add item with systemNumber: " + sysNo);
        System.err.println(e);
    }
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService) AutomationException(exceptions.AutomationException)

Example 52 with AssumeRoleRequest

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

the class AwsUtil method insertJsonInTable.

public static void insertJsonInTable(String json, String tableName, String primaryKey) {
    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);
    Table table = dynamoDB.getTable("cvs-" + loader.getBranchName() + "-" + tableName);
    String valueForPrimaryKey = GenericData.getValueFromJsonPath(json, "$." + primaryKey);
    try {
        Item item = Item.fromJSON(json);
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table.putItem(item);
        System.out.println("PutItem succeeded:\n" + item.toJSONPretty());
    } catch (Exception e) {
        System.err.println("Unable to add item with " + primaryKey + ": " + valueForPrimaryKey);
        e.printStackTrace();
    }
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService) AutomationException(exceptions.AutomationException)

Example 53 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)

Aggregations

AssumeRoleRequest (com.amazonaws.services.securitytoken.model.AssumeRoleRequest)53 AssumeRoleResult (com.amazonaws.services.securitytoken.model.AssumeRoleResult)42 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 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)11 Credentials (com.amazonaws.services.securitytoken.model.Credentials)11 AWSCredentials (com.amazonaws.auth.AWSCredentials)10 AmazonDynamoDBClient (com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)10 AWSSecurityTokenServiceClientBuilder (com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder)8 AWSSecurityTokenServiceClient (com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient)7 AutomationException (exceptions.AutomationException)7 ClientConfiguration (com.amazonaws.ClientConfiguration)6 AmazonServiceException (com.amazonaws.AmazonServiceException)4 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)4 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)3 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)2 AnonymousAWSCredentials (com.amazonaws.auth.AnonymousAWSCredentials)2 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)2