Search in sources :

Example 41 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project gora by apache.

the class DynamoDBUtils method waitForTableToBecomeAvailable.

/**
 * Waits up to 6 minutes to confirm if a table has been created or not
 *
 * @param awsClient
 * @param tableName
 */
public static void waitForTableToBecomeAvailable(AmazonDynamoDB awsClient, String tableName) {
    LOG.debug("Waiting for {} to become available", tableName);
    long startTime = System.currentTimeMillis();
    long endTime = startTime + WAIT_TIME;
    while (System.currentTimeMillis() < endTime) {
        try {
            Thread.sleep(SLEEP_TIME);
        } catch (Exception e) {
        }
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
            TableDescription tableDescription = awsClient.describeTable(request).getTable();
            String tableStatus = tableDescription.getTableStatus();
            LOG.debug("{} - current state: {}", tableName, tableStatus);
            if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                return;
        } catch (AmazonServiceException ase) {
            if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
                throw ase;
        }
    }
    throw new RuntimeException("Table " + tableName + " never became active");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 42 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project gora by apache.

the class DynamoDBUtils method executeCreateTableRequest.

/**
 * Executes a create table request using the DynamoDB client and waits the
 * default time until it's been created.
 *
 * @param awsClient
 * @param keySchema
 * @param tableName
 * @param proThrou
 */
public static void executeCreateTableRequest(AmazonDynamoDB awsClient, String tableName, ArrayList<KeySchemaElement> keySchema, Map<String, String> attrs, ProvisionedThroughput proThrou) {
    CreateTableRequest createTableRequest = buildCreateTableRequest(tableName, keySchema, proThrou, attrs);
    // use the client to perform the request
    try {
        awsClient.createTable(createTableRequest).getTableDescription();
        // wait for table to become active
        waitForTableToBecomeAvailable(awsClient, tableName);
    } catch (ResourceInUseException ex) {
        LOG.warn("Table '{}' already exists.", tableName);
    } finally {
        LOG.info("Table '{}' is available.", tableName);
    }
}
Also used : ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest)

Example 43 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project athenz by yahoo.

the class DynamoDBClientFetcherImpl method getAuthenticatedDynamoDBClient.

private DynamoDBClientAndCredentials getAuthenticatedDynamoDBClient(DynamoDBClientSettings dynamoDBClientSettings, ZTSClientNotificationSender ztsClientNotificationSender) {
    AWSCredentialsProviderImpl credentialsProvider = getCredentials(dynamoDBClientSettings, ztsClientNotificationSender);
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withCredentials(credentialsProvider).withRegion(dynamoDBClientSettings.getRegion()).build();
    return new DynamoDBClientAndCredentials(client, credentialsProvider);
}
Also used : AWSCredentialsProviderImpl(com.yahoo.athenz.zts.AWSCredentialsProviderImpl) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 44 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project athenz by yahoo.

the class DynamoDBStatusChecker method check.

@Override
public void check() throws StatusCheckException {
    DynamoDBClientAndCredentials clientAndCreds = null;
    try {
        // Get DynamoDB client and temp credentials (if required)
        DynamoDBClientFetcher dynamoDBClientFetcher = getDynamoDBClientFetcher();
        clientAndCreds = dynamoDBClientFetcher.getDynamoDBClient(null, keyStore);
        AmazonDynamoDB amazonDynamoDB = clientAndCreds.getAmazonDynamoDB();
        // Get list of tables and verify our table appears
        boolean tableFound = amazonDynamoDB.listTables().getTableNames().stream().anyMatch(fetchedTableName -> fetchedTableName.equals(tableName));
        if (!tableFound) {
            throw new StatusCheckException(HttpStatus.SC_OK, "Table named " + tableName + " wasn't found in DynamoDB");
        }
    } catch (StatusCheckException ex) {
        throw ex;
    } catch (Throwable ex) {
        throw new StatusCheckException(ex);
    } finally {
        // Close resources
        if (clientAndCreds != null) {
            try {
                if (clientAndCreds.getAmazonDynamoDB() != null) {
                    clientAndCreds.getAmazonDynamoDB().shutdown();
                }
                if (clientAndCreds.getAwsCredentialsProvider() != null) {
                    clientAndCreds.getAwsCredentialsProvider().close();
                }
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : StatusCheckException(com.yahoo.athenz.common.server.status.StatusCheckException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException)

Example 45 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project athenz by yahoo.

the class DynamoDBClientFetcherImplTest method testGetAuthenticatedClient.

@Test
public void testGetAuthenticatedClient() {
    // public
    String certPath = Resources.getResource("gdpr.aws.core.cert.pem").getPath();
    // private
    String keyPath = Resources.getResource("unit_test_gdpr.aws.core.key.pem").getPath();
    System.setProperty(ZTS_PROP_DYNAMODB_KEY_PATH, keyPath);
    System.setProperty(ZTS_PROP_DYNAMODB_CERT_PATH, certPath);
    System.setProperty(ZTS_PROP_DYNAMODB_DOMAIN, "test.domain");
    System.setProperty(ZTS_PROP_DYNAMODB_REGION, "test.region");
    System.setProperty(ZTS_PROP_DYNAMODB_ROLE, "test.role");
    System.setProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE, "test.truststore");
    System.setProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE_PASSWORD, "test.truststore.password");
    System.setProperty(ZTS_PROP_DYNAMODB_ZTS_URL, "https://dev.zts.athenzcompany.com:4443/zts/v1");
    DynamoDBClientFetcherImpl dynamoDBClientFetcher = new DynamoDBClientFetcherImpl();
    PrivateKeyStore keyStore = Mockito.mock(PrivateKeyStore.class);
    when(keyStore.getApplicationSecret(Mockito.eq(""), Mockito.eq("test.truststore.password"))).thenReturn("mockPassword");
    ZTSClientNotificationSender ztsClientNotificationSender = Mockito.mock(ZTSClientNotificationSender.class);
    AmazonDynamoDB dynamoDBClient = dynamoDBClientFetcher.getDynamoDBClient(ztsClientNotificationSender, keyStore).getAmazonDynamoDB();
    assertNotNull(dynamoDBClient);
    // Also try with min and max expiry set
    System.setProperty(ZTS_PROP_DYNAMODB_MIN_EXPIRY_TIME, "10");
    System.setProperty(ZTS_PROP_DYNAMODB_MAX_EXPIRY_TIME, "100");
    dynamoDBClient = dynamoDBClientFetcher.getDynamoDBClient(ztsClientNotificationSender, keyStore).getAmazonDynamoDB();
    assertNotNull(dynamoDBClient);
    System.clearProperty(ZTS_PROP_DYNAMODB_KEY_PATH);
    System.clearProperty(ZTS_PROP_DYNAMODB_CERT_PATH);
    System.clearProperty(ZTS_PROP_DYNAMODB_DOMAIN);
    System.clearProperty(ZTS_PROP_DYNAMODB_REGION);
    System.clearProperty(ZTS_PROP_DYNAMODB_ROLE);
    System.clearProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE);
    System.clearProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE_PASSWORD);
    System.clearProperty(ZTS_PROP_DYNAMODB_ZTS_URL);
}
Also used : PrivateKeyStore(com.yahoo.athenz.auth.PrivateKeyStore) ZTSClientNotificationSender(com.yahoo.athenz.zts.ZTSClientNotificationSender) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) Test(org.testng.annotations.Test)

Aggregations

AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)70 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)16 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)14 Test (org.junit.Test)13 Table (com.amazonaws.services.dynamodbv2.document.Table)12 IOException (java.io.IOException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)11 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)11 HashMap (java.util.HashMap)11 AmazonClientException (com.amazonaws.AmazonClientException)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)9 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)8 ToString (lombok.ToString)7 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)6 ArrayList (java.util.ArrayList)6 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)5 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)5 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)5