Search in sources :

Example 56 with Region

use of com.amazonaws.services.s3.model.Region in project jackrabbit by apache.

the class S3Backend method init.

public void init(CachingDataStore store, String homeDir, Properties prop) throws DataStoreException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        startTime = new Date();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("init");
        setDataStore(store);
        s3ReqDecorator = new S3RequestDecorator(prop);
        s3service = Utils.openService(prop);
        if (bucket == null || "".equals(bucket.trim())) {
            bucket = prop.getProperty(S3Constants.S3_BUCKET);
        }
        String region = prop.getProperty(S3Constants.S3_REGION);
        Region s3Region = null;
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region ec2Region = Regions.getCurrentRegion();
            if (ec2Region != null) {
                s3Region = Region.fromValue(ec2Region.getName());
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION + "] not configured and cannot be derived from environment");
            }
        } else {
            if (Utils.DEFAULT_AWS_BUCKET_REGION.equals(region)) {
                s3Region = Region.US_Standard;
            } else if (Region.EU_Ireland.toString().equals(region)) {
                s3Region = Region.EU_Ireland;
            } else {
                s3Region = Region.fromValue(region);
            }
        }
        if (!s3service.doesBucketExist(bucket)) {
            s3service.createBucket(bucket, s3Region);
            LOG.info("Created bucket [{}] in [{}] ", bucket, region);
        } else {
            LOG.info("Using bucket [{}] in [{}] ", bucket, region);
        }
        int writeThreads = 10;
        String writeThreadsStr = prop.getProperty(S3Constants.S3_WRITE_THREADS);
        if (writeThreadsStr != null) {
            writeThreads = Integer.parseInt(writeThreadsStr);
        }
        LOG.info("Using thread pool of [{}] threads in S3 transfer manager.", writeThreads);
        tmx = new TransferManager(s3service, (ThreadPoolExecutor) Executors.newFixedThreadPool(writeThreads, new NamedThreadFactory("s3-transfer-manager-worker")));
        int asyncWritePoolSize = 10;
        String maxConnsStr = prop.getProperty(S3Constants.S3_MAX_CONNS);
        if (maxConnsStr != null) {
            asyncWritePoolSize = Integer.parseInt(maxConnsStr) - writeThreads;
        }
        setAsyncWritePoolSize(asyncWritePoolSize);
        String renameKeyProp = prop.getProperty(S3Constants.S3_RENAME_KEYS);
        boolean renameKeyBool = (renameKeyProp == null || "".equals(renameKeyProp)) ? false : Boolean.parseBoolean(renameKeyProp);
        LOG.info("Rename keys [{}]", renameKeyBool);
        if (renameKeyBool) {
            renameKeys();
        }
        LOG.debug("S3 Backend initialized in [{}] ms", +(System.currentTimeMillis() - startTime.getTime()));
    } catch (Exception e) {
        LOG.debug("  error ", e);
        throw new DataStoreException("Could not initialize S3 from " + prop, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) NamedThreadFactory(org.apache.jackrabbit.core.data.util.NamedThreadFactory) AmazonClientException(com.amazonaws.AmazonClientException) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException) S3RequestDecorator(org.apache.jackrabbit.aws.ext.S3RequestDecorator) Region(com.amazonaws.services.s3.model.Region) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 57 with Region

use of com.amazonaws.services.s3.model.Region in project athenz by yahoo.

the class AwsPrivateKeyStoreTest method testGetPrivateKeyAlgorithmInvalidKey.

@Test
public void testGetPrivateKeyAlgorithmInvalidKey() {
    final String bucketName = "my_bucket";
    final String keyName = "my_key";
    final String algKeyName = "my_key.rsa";
    final String keyId = "my_key_id";
    final String algKeyId = "my_key_id.rsa";
    final String expectedKeyId = "1";
    final String privKey = "invalid-key";
    System.setProperty("athenz.aws.s3.region", "us-east-1");
    System.setProperty("athenz.aws.zts.bucket_name", bucketName);
    System.setProperty("athenz.aws.zts.key_name", keyName);
    System.setProperty("athenz.aws.zts.key_id_name", keyId);
    AmazonS3 s3 = mock(AmazonS3.class);
    AWSKMS kms = mock(AWSKMS.class);
    S3Object s3ObjectKey = mock(S3Object.class);
    Mockito.when(s3.getObject(bucketName, algKeyName)).thenReturn(s3ObjectKey);
    InputStream isKey = new ByteArrayInputStream(privKey.getBytes());
    S3ObjectInputStream s3ObjectKeyInputStream = new S3ObjectInputStream(isKey, null);
    Mockito.when(s3ObjectKey.getObjectContent()).thenReturn(s3ObjectKeyInputStream);
    S3Object s3ObjectKeyId = mock(S3Object.class);
    Mockito.when(s3.getObject(bucketName, algKeyId)).thenReturn(s3ObjectKeyId);
    InputStream isKeyId = new ByteArrayInputStream(expectedKeyId.getBytes());
    S3ObjectInputStream s3ObjectKeyIdInputStream = new S3ObjectInputStream(isKeyId, null);
    Mockito.when(s3ObjectKeyId.getObjectContent()).thenReturn(s3ObjectKeyIdInputStream);
    AwsPrivateKeyStore awsPrivateKeyStore = new AwsPrivateKeyStore(s3, kms);
    assertNull(awsPrivateKeyStore.getPrivateKey("zts", "testServerHostName", "us-east-1", "rsa"));
    System.clearProperty("athenz.aws.s3.region");
    System.clearProperty("athenz.aws.zts.bucket_name");
    System.clearProperty("athenz.aws.zts.key_name");
    System.clearProperty("athenz.aws.zts.key_id_name");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) InputStream(java.io.InputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) AWSKMS(com.amazonaws.services.kms.AWSKMS) Test(org.testng.annotations.Test)

Example 58 with Region

use of com.amazonaws.services.s3.model.Region in project athenz by yahoo.

the class AwsPrivateKeyStoreTest method testGetApplicationSecret.

@Test
public void testGetApplicationSecret() {
    System.setProperty("athenz.aws.s3.region", "us-east-1");
    System.setProperty(ATHENZ_AWS_KMS_REGION, "us-east-1");
    String bucketName = "my_bucket";
    String keyName = "my_key";
    String expected = "my_value";
    AmazonS3 s3 = mock(AmazonS3.class);
    AWSKMS kms = mock(AWSKMS.class);
    S3Object s3Object = mock(S3Object.class);
    Mockito.when(s3.getObject(bucketName, keyName)).thenReturn(s3Object);
    InputStream is = new ByteArrayInputStream(expected.getBytes());
    S3ObjectInputStream s3ObjectInputStream = new S3ObjectInputStream(is, null);
    Mockito.when(s3Object.getObjectContent()).thenReturn(s3ObjectInputStream);
    ByteBuffer buffer = ByteBuffer.wrap(expected.getBytes());
    DecryptResult decryptResult = mock(DecryptResult.class);
    Mockito.when(kms.decrypt(Mockito.any(DecryptRequest.class))).thenReturn(decryptResult);
    Mockito.when(decryptResult.getPlaintext()).thenReturn(buffer);
    System.setProperty("athenz.aws.store_kms_decrypt", "true");
    AwsPrivateKeyStore awsPrivateKeyStore = new AwsPrivateKeyStore();
    AwsPrivateKeyStore spyAWS = Mockito.spy(awsPrivateKeyStore);
    doReturn(s3).when(spyAWS).getS3();
    doReturn(kms).when(spyAWS).getKMS();
    String actual = spyAWS.getApplicationSecret(bucketName, keyName);
    assertEquals(actual, expected);
    System.clearProperty("athenz.aws.s3.region");
    System.clearProperty(ATHENZ_AWS_KMS_REGION);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) DecryptResult(com.amazonaws.services.kms.model.DecryptResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) InputStream(java.io.InputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) ByteBuffer(java.nio.ByteBuffer) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest) AWSKMS(com.amazonaws.services.kms.AWSKMS) Test(org.testng.annotations.Test)

Example 59 with Region

use of com.amazonaws.services.s3.model.Region in project athenz by yahoo.

the class AwsPrivateKeyStoreTest method testAwsPrivateKeyStore.

@Test
public void testAwsPrivateKeyStore() {
    System.setProperty("athenz.aws.s3.region", "us-east-1");
    System.setProperty(ATHENZ_AWS_KMS_REGION, "us-east-1");
    String bucketName = "my_bucket";
    String keyName = "my_key";
    String expected = "my_value";
    System.setProperty(ATHENZ_PROP_ZTS_BUCKET_NAME, bucketName);
    System.setProperty("athenz.aws.zts.key_name", keyName);
    AmazonS3 s3 = mock(AmazonS3.class);
    AWSKMS kms = mock(AWSKMS.class);
    S3Object s3Object = mock(S3Object.class);
    Mockito.when(s3.getObject(bucketName, keyName)).thenReturn(s3Object);
    InputStream is = new ByteArrayInputStream(expected.getBytes());
    S3ObjectInputStream s3ObjectInputStream = new S3ObjectInputStream(is, null);
    Mockito.when(s3Object.getObjectContent()).thenReturn(s3ObjectInputStream);
    ByteBuffer buffer = ByteBuffer.wrap(expected.getBytes());
    DecryptResult decryptResult = mock(DecryptResult.class);
    Mockito.when(kms.decrypt(Mockito.any(DecryptRequest.class))).thenReturn(decryptResult);
    Mockito.when(decryptResult.getPlaintext()).thenReturn(buffer);
    AwsPrivateKeyStore awsPrivateKeyStore = new AwsPrivateKeyStore(s3, kms);
    String actual = awsPrivateKeyStore.getApplicationSecret(bucketName, keyName);
    StringBuilder privateKeyId = new StringBuilder(keyName);
    awsPrivateKeyStore.getPrivateKey("zts", "testServerHostName", privateKeyId);
    assertEquals(actual, expected);
    Mockito.when(s3Object.getObjectContent()).thenAnswer(invocation -> {
        throw new IOException("test IOException");
    });
    awsPrivateKeyStore.getPrivateKey("zts", "testServerHostName", privateKeyId);
    System.clearProperty("athenz.aws.s3.region");
    System.clearProperty(ATHENZ_AWS_KMS_REGION);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) DecryptResult(com.amazonaws.services.kms.model.DecryptResult) ByteArrayInputStream(java.io.ByteArrayInputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) InputStream(java.io.InputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) AWSKMS(com.amazonaws.services.kms.AWSKMS) ByteArrayInputStream(java.io.ByteArrayInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest) Test(org.testng.annotations.Test)

Example 60 with Region

use of com.amazonaws.services.s3.model.Region in project gradle-s3-build-cache by myniva.

the class AwsS3BuildCacheServiceFactory method createBuildCacheService.

@Override
public BuildCacheService createBuildCacheService(AwsS3BuildCache config, Describer describer) {
    logger.debug("Start creating S3 build cache service");
    describer.type("AWS S3").config("Region", config.getRegion()).config("Bucket", config.getBucket()).config("Reduced Redundancy", String.valueOf(config.isReducedRedundancy()));
    if (config.getPath() != null) {
        describer.config("Path", config.getPath());
    }
    if (config.getEndpoint() != null) {
        describer.config("Endpoint", config.getEndpoint());
    }
    verifyConfig(config);
    AmazonS3 s3 = createS3Client(config);
    return new AwsS3BuildCacheService(s3, config.getBucket(), config.getPath(), config.isReducedRedundancy());
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3)

Aggregations

AmazonS3 (com.amazonaws.services.s3.AmazonS3)18 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)17 IOException (java.io.IOException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)11 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)11 Test (org.junit.Test)10 AmazonClientException (com.amazonaws.AmazonClientException)9 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)9 Regions (com.amazonaws.regions.Regions)9 HashMap (java.util.HashMap)9 Date (java.util.Date)8 Map (java.util.Map)8 ClientConfiguration (com.amazonaws.ClientConfiguration)7 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)7 S3Object (com.amazonaws.services.s3.model.S3Object)7 AWSKMS (com.amazonaws.services.kms.AWSKMS)6 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 InputStream (java.io.InputStream)6