Search in sources :

Example 46 with AssumeRoleRequest

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

the class TranslateConnection method getRoleAssumedComprehendClient.

/**
 * Creates and returns a new instance of the {@link AmazonTranslate} client using rule assumption.
 *
 * @param connectionInformation The connection information
 * @return AmazonComprehend client
 * @throws Exception thrown if client could not be instantiated
 */
private static final AmazonTranslate getRoleAssumedComprehendClient(final CloudConnectionInformation connectionInformation) throws Exception {
    final AWSSecurityTokenServiceClientBuilder builder = AWSSecurityTokenServiceClientBuilder.standard().withRegion(connectionInformation.getHost());
    if (!connectionInformation.useKeyChain()) {
        final AWSCredentials credentials = getCredentials(connectionInformation);
        builder.withCredentials(new AWSStaticCredentialsProvider(credentials));
    }
    final AWSSecurityTokenService stsClient = builder.build();
    final AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest().withRoleArn(buildARN(connectionInformation)).withDurationSeconds(3600).withRoleSessionName("KNIME_Translate_Connection");
    final AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRoleRequest);
    final BasicSessionCredentials tempCredentials = new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken());
    return AmazonTranslateClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(tempCredentials)).withRegion(connectionInformation.getHost()).build();
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) AWSSecurityTokenServiceClientBuilder(com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Example 47 with AssumeRoleRequest

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

the class AwsUtil method checkLogsFor.

public static boolean checkLogsFor(String log, String keyValuePair) {
    Regions clientRegion = Regions.EU_WEST_1;
    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build();
    System.out.println(System.getProperty("AWS_ROLE"));
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(System.getProperty("AWS_ROLE")).withDurationSeconds(3600).withRoleSessionName(UUID.randomUUID().toString());
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken());
    AWSLogs logsClient = new AWSLogsClient(temporaryCredentials).withRegion(clientRegion);
    String logGroup = log + "-" + loader.getBranchName();
    for (int times = 0; times < 15; times++) {
        System.out.println("... " + times + " ...");
        DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest().withLogGroupName(logGroup).withOrderBy("LastEventTime").withDescending(true).withLimit(1);
        DescribeLogStreamsResult describeLogStreamsResult = logsClient.describeLogStreams(describeLogStreamsRequest);
        LogStream logStream = describeLogStreamsResult.getLogStreams().get(0);
        GetLogEventsRequest getLogEventsRequest = new GetLogEventsRequest().withLogGroupName(logGroup).withLogStreamName(logStream.getLogStreamName());
        GetLogEventsResult result = logsClient.getLogEvents(getLogEventsRequest);
        for (OutputLogEvent event : result.getEvents()) {
            System.out.println("*****************************");
            System.out.println("# event: " + event.getMessage());
            System.out.println("Looking for: " + keyValuePair);
            if (event.getMessage().contains(keyValuePair)) {
                System.out.println("!!!!!!!!!!!!!!!###### FOUND !!! ######!!!!!!!!!!!!!!!");
                System.out.println("$$$$$$$$$$$   " + logStream.getLogStreamName() + "   $$$$$$$$$$$");
                return true;
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return false;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSLogs(com.amazonaws.services.logs.AWSLogs) AWSLogsClient(com.amazonaws.services.logs.AWSLogsClient) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Example 48 with AssumeRoleRequest

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

the class AwsUtil method checkDispatcherLogsForData.

public static boolean checkDispatcherLogsForData(String... keyValuePairs) {
    Regions clientRegion = Regions.EU_WEST_1;
    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build();
    System.out.println(System.getProperty("AWS_ROLE"));
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(System.getProperty("AWS_ROLE")).withDurationSeconds(3600).withRoleSessionName(UUID.randomUUID().toString());
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken());
    AWSLogs logsClient = new AWSLogsClient(temporaryCredentials).withRegion(clientRegion);
    String logGroup = "/aws/lambda/edh-dispatcher-" + loader.getBranchName();
    logStreamLoop: for (int times = 0; times < 50; times++) {
        System.out.println("... " + times + " ...");
        DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest().withLogGroupName(logGroup).withOrderBy("LastEventTime").withDescending(true).withLimit(20);
        DescribeLogStreamsResult describeLogStreamsResult = logsClient.describeLogStreams(describeLogStreamsRequest);
        LogStream logStream = describeLogStreamsResult.getLogStreams().get(0);
        GetLogEventsRequest getLogEventsRequest = new GetLogEventsRequest().withLogGroupName(logGroup).withLogStreamName(logStream.getLogStreamName());
        GetLogEventsResult result = logsClient.getLogEvents(getLogEventsRequest);
        eventLoop: for (OutputLogEvent event : result.getEvents()) {
            System.out.println("\n----------------------------------------------------------------------");
            for (String keyValuePair : keyValuePairs) {
                System.out.println("searching inside event for: " + keyValuePair);
                if (!event.getMessage().contains(keyValuePair)) {
                    System.out.println("########################## " + keyValuePair + " NOT FOUND in event: \n" + event.getMessage() + "\n ##########################");
                    continue eventLoop;
                }
            }
            System.out.println("!!!!!!!!!!!!!!!###### FOUND !!! ######!!!!!!!!!!!!!!!");
            System.out.println("$$$$$$$$$$$   " + logStream.getLogStreamName() + "   $$$$$$$$$$$");
            return true;
        }
        try {
            System.out.println("########################## " + keyValuePairs + " NOT FOUND in log stream: \n" + logStream.getLogStreamName() + "\n ##########################");
            System.out.println("waiting 2 seconds");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return false;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSLogs(com.amazonaws.services.logs.AWSLogs) AWSLogsClient(com.amazonaws.services.logs.AWSLogsClient) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Example 49 with AssumeRoleRequest

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

the class AwsUtil method deleteTestResultId.

public static void deleteTestResultId(String testResultId) {
    System.out.println("deleting the test-result: " + testResultId);
    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-results";
    Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
    expressionAttributeValues.put(":result_id", new AttributeValue().withS(testResultId));
    ScanRequest scanRequest = new ScanRequest().withTableName(tableName).withFilterExpression("testResultId = :result_id").withProjectionExpression("vin, testResultId").withExpressionAttributeValues(expressionAttributeValues);
    ScanResult result = client.scan(scanRequest);
    System.out.println("result.toString: " + result.toString());
    System.out.println("result.getCount: " + result.getCount());
    System.out.println("result.getItems:" + result.getItems());
    Table table = dynamoDB.getTable(tableName);
    for (Map<String, AttributeValue> item : result.getItems()) {
        System.out.println("item.size: " + item.size());
        System.out.println("item.values: " + item.values());
        System.out.println("item.get(vin).getS(): " + item.get("vin").getS());
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("vin", item.get("vin").getS(), "testResultId", testResultId);
        DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec);
    }
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Example 50 with AssumeRoleRequest

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

the class AwsUtil method deleteActivityById.

public static void deleteActivityById(String id) {
    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() + "-activities";
    Table table = dynamoDB.getTable(tableName);
    try {
        System.out.println("deleting item with id: " + id + " ....");
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("id", id);
        DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec);
        System.out.println("Printing item that was deleted...");
    } catch (Exception e) {
        System.err.println("Error deleting item in " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) 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)

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