Search in sources :

Example 56 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project athenz by yahoo.

the class AwsPrivateKeyStoreTest method testGetPrivateKeyAlgorithm.

private void testGetPrivateKeyAlgorithm(final String service) throws IOException {
    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";
    System.setProperty("athenz.aws.s3.region", "us-east-1");
    System.setProperty("athenz.aws." + service + ".bucket_name", bucketName);
    System.setProperty("athenz.aws." + service + ".key_name", keyName);
    System.setProperty("athenz.aws." + service + ".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);
    File privKeyFile = new File("src/test/resources/unit_test_zts_private.pem");
    final String privKey = new String(Files.readAllBytes(privKeyFile.toPath()), StandardCharsets.UTF_8);
    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);
    ServerPrivateKey serverPrivateKey = awsPrivateKeyStore.getPrivateKey(service, "testServerHostName", "us-east-1", "rsa");
    assertNotNull(serverPrivateKey);
    assertNotNull(serverPrivateKey.getKey());
    assertEquals(serverPrivateKey.getAlgorithm().toString(), "RS256");
    assertEquals(serverPrivateKey.getId(), "1");
    System.clearProperty("athenz.aws.s3.region");
    System.clearProperty("athenz.aws." + service + ".bucket_name");
    System.clearProperty("athenz.aws." + service + ".key_name");
    System.clearProperty("athenz.aws." + service + ".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) File(java.io.File) ServerPrivateKey(com.yahoo.athenz.auth.ServerPrivateKey) AWSKMS(com.amazonaws.services.kms.AWSKMS)

Example 57 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project athenz by yahoo.

the class S3ChangeLogStoreTest method testGetSignedDomainInternal.

@Test
public void testGetSignedDomainInternal() throws IOException {
    MockS3ChangeLogStore store = new MockS3ChangeLogStore();
    InputStream is = new FileInputStream("src/test/resources/iaas.json");
    MockS3ObjectInputStream s3Is = new MockS3ObjectInputStream(is, null);
    S3Object object = mock(S3Object.class);
    when(object.getObjectContent()).thenReturn(s3Is);
    when(store.awsS3Client.getObject("s3-unit-test-bucket-name", "iaas")).thenReturn(object);
    SignedDomain signedDomain = store.getSignedDomain(store.awsS3Client, "iaas");
    assertNotNull(signedDomain);
    DomainData domainData = signedDomain.getDomain();
    assertNotNull(domainData);
    assertEquals(domainData.getName(), "iaas");
    is.close();
}
Also used : S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) SignedDomain(com.yahoo.athenz.zms.SignedDomain) DomainData(com.yahoo.athenz.zms.DomainData) S3Object(com.amazonaws.services.s3.model.S3Object) Test(org.testng.annotations.Test)

Example 58 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project athenz by yahoo.

the class S3ChangeLogStoreTest method testGetUpdatedJWSDomainsWithChange1.

@Test
public void testGetUpdatedJWSDomainsWithChange1() throws IOException {
    MockS3ChangeLogStore store = new MockS3ChangeLogStore();
    ArrayList<S3ObjectSummary> objectList = new ArrayList<>();
    S3ObjectSummary objectSummary = new S3ObjectSummary();
    objectSummary.setKey("iaas");
    objectSummary.setLastModified(new Date(100));
    objectList.add(objectSummary);
    objectSummary = new S3ObjectSummary();
    objectSummary.setKey("iaas.athenz");
    objectSummary.setLastModified(new Date(200));
    objectList.add(objectSummary);
    // we'll also include an invalid domain that should be skipped
    objectSummary = new S3ObjectSummary();
    objectSummary.setKey("unknown");
    objectSummary.setLastModified(new Date(200));
    objectList.add(objectSummary);
    ObjectListing objectListing = mock(ObjectListing.class);
    when(objectListing.getObjectSummaries()).thenReturn(objectList);
    when(objectListing.isTruncated()).thenReturn(false);
    when(store.awsS3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(objectListing);
    InputStream is = new FileInputStream("src/test/resources/iaas.jws");
    MockS3ObjectInputStream s3Is = new MockS3ObjectInputStream(is, null);
    S3Object object = mock(S3Object.class);
    when(object.getObjectContent()).thenReturn(s3Is);
    when(store.awsS3Client.getObject("s3-unit-test-bucket-name", "iaas")).thenReturn(object);
    when(store.awsS3Client.getObject("s3-unit-test-bucket-name", "iaas.athenz")).thenReturn(object);
    // set the last modification time to return one of the domains
    store.lastModTime = (new Date(150)).getTime();
    StringBuilder lastModTimeBuffer = new StringBuilder(512);
    List<JWSDomain> jwsDomains = store.getUpdatedJWSDomains(lastModTimeBuffer);
    assertTrue(lastModTimeBuffer.length() > 0);
    assertEquals(jwsDomains.size(), 1);
    is.close();
}
Also used : S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) JWSDomain(com.yahoo.athenz.zms.JWSDomain) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) S3Object(com.amazonaws.services.s3.model.S3Object) Test(org.testng.annotations.Test)

Example 59 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project athenz by yahoo.

the class S3ChangeLogStoreTest method testGetSignedDomainException.

@Test
public void testGetSignedDomainException() throws IOException {
    MockS3ChangeLogStore store = new MockS3ChangeLogStore();
    InputStream is = new FileInputStream("src/test/resources/iaas.json");
    MockS3ObjectInputStream s3Is = new MockS3ObjectInputStream(is, null);
    S3Object object = mock(S3Object.class);
    when(object.getObjectContent()).thenReturn(s3Is);
    // first call we return null, second call we return success
    when(store.awsS3Client.getObject("s3-unit-test-bucket-name", "iaas")).thenThrow(new AmazonServiceException("test")).thenReturn(object);
    SignedDomain signedDomain = store.getLocalSignedDomain("iaas");
    assertNotNull(signedDomain);
    DomainData domainData = signedDomain.getDomain();
    assertNotNull(domainData);
    assertEquals(domainData.getName(), "iaas");
    is.close();
}
Also used : S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) SignedDomain(com.yahoo.athenz.zms.SignedDomain) DomainData(com.yahoo.athenz.zms.DomainData) S3Object(com.amazonaws.services.s3.model.S3Object) Test(org.testng.annotations.Test)

Example 60 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project athenz by yahoo.

the class S3ChangeLogStoreTest method testGetAllDomainsException.

public void testGetAllDomainsException(boolean jwsSupport) throws IOException {
    MockS3ChangeLogStore store = new MockS3ChangeLogStore(1);
    store.setJWSDomainSupport(jwsSupport);
    InputStream is1 = new FileInputStream("src/test/resources/iaas.json");
    MockS3ObjectInputStream s3Is1 = new MockS3ObjectInputStream(is1, null);
    InputStream is2 = new FileInputStream("src/test/resources/iaas.json");
    MockS3ObjectInputStream s3Is2 = new MockS3ObjectInputStream(is2, null);
    S3Object object = mock(S3Object.class);
    when(object.getObjectContent()).thenReturn(s3Is1).thenReturn(s3Is2);
    when(store.awsS3Client.getObject("s3-unit-test-bucket-name", "iaas")).thenReturn(object);
    ObjectListing mockObjectListing = mock(ObjectListing.class);
    when(store.awsS3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(mockObjectListing);
    List<S3ObjectSummary> tempList = new ArrayList<>();
    S3ObjectSummary s3ObjectSummary = mock(S3ObjectSummary.class);
    when(s3ObjectSummary.getKey()).thenReturn("iaas");
    tempList.add(s3ObjectSummary);
    when(mockObjectListing.getObjectSummaries()).thenReturn(tempList);
    List<String> temp = new LinkedList<>();
    temp.add("iaas");
    try {
        when(store.executorService.awaitTermination(defaultTimeoutSeconds, TimeUnit.SECONDS)).thenThrow(new InterruptedException());
        assertFalse(store.getAllDomains(temp));
        assertTrue(store.getLocalDomainList().size() > 0);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    is1.close();
    is2.close();
}
Also used : S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) S3Object(com.amazonaws.services.s3.model.S3Object)

Aggregations

S3Object (com.amazonaws.services.s3.model.S3Object)110 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)34 InputStream (java.io.InputStream)28 IOException (java.io.IOException)24 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)23 ByteArrayInputStream (java.io.ByteArrayInputStream)21 AmazonServiceException (com.amazonaws.AmazonServiceException)20 AmazonS3 (com.amazonaws.services.s3.AmazonS3)20 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)18 Test (org.junit.Test)18 Test (org.testng.annotations.Test)18 S3Object (software.amazon.awssdk.services.s3.model.S3Object)17 File (java.io.File)14 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)13 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)13 FileInputStream (java.io.FileInputStream)13 Date (java.util.Date)11 SignedDomain (com.yahoo.athenz.zms.SignedDomain)10 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)9 AmazonClientException (com.amazonaws.AmazonClientException)8