Search in sources :

Example 61 with AmazonS3

use of com.amazonaws.services.s3.AmazonS3 in project bayou by capergroup.

the class S3LoggerBase method putToS3.

/**
 * Performs an S3 Put Object operation storing the UTF-8 bytes of logMsg under the given key
 * using construction provided AWS credentials.
 *
 * @param objectKey the S3 object key. may not be null or whitespace only.
 * @param logMsg the message to store
 * @throws IllegalArgumentException if objectKey is whitespace only.
 */
void putToS3(String objectKey, String logMsg) {
    if (objectKey == null)
        throw new NullPointerException("objectKey");
    if (objectKey.trim().length() == 0)
        throw new IllegalArgumentException("objectKey may not be only whitespace.");
    /*
         * Make the client used to send the log msg to S3.
         */
    AmazonS3 client;
    {
        Regions regions = Regions.US_EAST_1;
        if (_credentials == null) {
            client = // get creds from environment
            AmazonS3ClientBuilder.standard().withRegion(regions).build();
        } else {
            client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(_credentials)).withRegion(regions).build();
        }
    }
    /*
         * Store the log msg in S3.
         */
    byte[] logMsgBytes = logMsg.getBytes(StandardCharsets.UTF_8);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(logMsgBytes.length);
    client.putObject(_bucketName, objectKey, new ByteArrayInputStream(logMsgBytes), metadata);
    _logger.debug("exiting");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) Regions(com.amazonaws.regions.Regions) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 62 with AmazonS3

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

the class S3ChangeLogStore method getUpdatedSignedDomains.

@Override
public SignedDomains getUpdatedSignedDomains(StringBuilder lastModTimeBuffer) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("getUpdatedSignedDomains: Retrieving updating signed domains from S3...");
    }
    // We need save the timestamp at the beginning just in case we end up getting
    // paged results and while processing the last page, S3 gets pushed
    // updated domains from the earlier pages
    lastModTimeBuffer.append(System.currentTimeMillis());
    // AWS S3 API does not provide support for listing objects filtered
    // based on its last modification timestamp so we need to get
    // the full list and filter ourselves
    // instead of using our fetched s3 client, we're going to
    // obtain a new one to get the changes
    AmazonS3 s3 = getS3Client();
    ArrayList<String> domains = new ArrayList<>();
    listObjects(s3, domains, lastModTime);
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("getUpdatedSignedDomains: {} updated domains", domains.size());
    }
    ArrayList<SignedDomain> signedDomainList = new ArrayList<>();
    SignedDomain signedDomain = null;
    for (String domain : domains) {
        signedDomain = getSignedDomain(s3, domain);
        if (signedDomain != null) {
            signedDomainList.add(signedDomain);
        }
    }
    SignedDomains signedDomains = new SignedDomains();
    signedDomains.setDomains(signedDomainList);
    return signedDomains;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ArrayList(java.util.ArrayList) SignedDomain(com.yahoo.athenz.zms.SignedDomain) SignedDomains(com.yahoo.athenz.zms.SignedDomains)

Example 63 with AmazonS3

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

the class AwsPrivateKeyStoreTest method testAwsPrivateKeyStore.

@Test
public void testAwsPrivateKeyStore() throws Exception {
    String bucketName = "my_bucket";
    String keyName = "my_key";
    String expected = "my_value";
    AmazonS3 s3 = Mockito.mock(AmazonS3.class);
    AWSKMS kms = Mockito.mock(AWSKMS.class);
    S3Object s3Object = Mockito.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);
    String result = expected;
    ByteBuffer buffer = ByteBuffer.wrap(result.getBytes());
    DecryptResult decryptResult = Mockito.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);
    Assert.assertEquals(actual, expected);
}
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)

Aggregations

AmazonS3 (com.amazonaws.services.s3.AmazonS3)55 AmazonServiceException (com.amazonaws.AmazonServiceException)15 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)10 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)10 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)9 Bucket (com.amazonaws.services.s3.model.Bucket)6 ArrayList (java.util.ArrayList)6 ClientConfiguration (com.amazonaws.ClientConfiguration)5 S3Object (com.amazonaws.services.s3.model.S3Object)5 Test (org.junit.Test)5 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)4 AccessControlList (com.amazonaws.services.s3.model.AccessControlList)4 Configuration (org.apache.hadoop.conf.Configuration)4 AWSCredentials (com.amazonaws.auth.AWSCredentials)3 S3ClientOptions (com.amazonaws.services.s3.S3ClientOptions)3 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)2 BucketWebsiteConfiguration (com.amazonaws.services.s3.model.BucketWebsiteConfiguration)2