com.amazonaws.util.Base64 Java Examples

The following examples show how to use com.amazonaws.util.Base64. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestPutLambda.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutLambdaSimple() {
    runner.setProperty(PutLambda.AWS_LAMBDA_FUNCTION_NAME, "test-function");
    runner.enqueue("TestContent");

    InvokeResult invokeResult = new InvokeResult();
    invokeResult.setStatusCode(200);
    invokeResult.setLogResult(Base64.encodeAsString("test-log-result".getBytes()));
    invokeResult.setPayload(ByteBuffer.wrap("test-payload".getBytes()));
    Mockito.when(mockLambdaClient.invoke(Mockito.any(InvokeRequest.class))).thenReturn(invokeResult);

    runner.assertValid();
    runner.run(1);

    ArgumentCaptor<InvokeRequest> captureRequest = ArgumentCaptor.forClass(InvokeRequest.class);
    Mockito.verify(mockLambdaClient, Mockito.times(1)).invoke(captureRequest.capture());
    InvokeRequest request = captureRequest.getValue();
    assertEquals("test-function", request.getFunctionName());

    runner.assertAllFlowFilesTransferred(PutLambda.REL_SUCCESS, 1);
    final List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(PutLambda.REL_SUCCESS);
    final MockFlowFile ff0 = flowFiles.get(0);
    ff0.assertAttributeEquals(PutLambda.AWS_LAMBDA_RESULT_STATUS_CODE, "200");
    ff0.assertAttributeEquals(PutLambda.AWS_LAMBDA_RESULT_LOG, "test-log-result");
    ff0.assertAttributeEquals(PutLambda.AWS_LAMBDA_RESULT_PAYLOAD, "test-payload");
}
 
Example #2
Source File: AuthenticationHelper.java    From alexa-web-information-service-api-samples with MIT License 6 votes vote down vote up
/**
 * Calculate the secret hash to be sent along with the authentication request.
 *
 * @param userPoolClientId     : The client id of the app.
 * @param userPoolClientSecret : The secret for the userpool client id.
 * @param userName             : The username of the user trying to authenticate.
 * @return Calculated secret hash.
 */
private String calculateSecretHash(String userPoolClientId, String userPoolClientSecret, String userName) {
    final String HMAC_SHA256_ALGORITHM = "HmacSHA256";

    SecretKeySpec signingKey = new SecretKeySpec(
            userPoolClientSecret.getBytes(StandardCharsets.UTF_8),
            HMAC_SHA256_ALGORITHM);
    try {
        Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
        mac.init(signingKey);
        mac.update(userName.getBytes(StandardCharsets.UTF_8));
        byte[] rawHmac = mac.doFinal(userPoolClientId.getBytes(StandardCharsets.UTF_8));
        return java.util.Base64.getEncoder().encodeToString(rawHmac);
    } catch (Exception e) {
        throw new RuntimeException("Error while calculating ");
    }
}
 
Example #3
Source File: GetNewSecret.java    From strongbox with Apache License 2.0 6 votes vote down vote up
private SecretValue getSecretValue(ToggleGroup valueSource, String value, String generated, File file) {
    Toggle current = valueSource.getSelectedToggle();

    String secretString;
    if (current.getUserData().equals("value")) {
        secretString = value;
    } else if (current.getUserData().equals("generated")) {
        Integer numBytesToGenerate = Integer.valueOf(generated);
        // TODO: store as plain bytes?
        byte[] random = Singleton.randomGenerator.generateRandom(numBytesToGenerate);
        secretString = Base64.encodeAsString(random);
    } else {
        String path = null;
        try {
            path = file.getCanonicalPath();
            return SecretValueConverter.inferEncoding(Files.readAllBytes(Paths.get(path)), SecretType.OPAQUE);
        } catch (IOException e) {
            throw new RuntimeException("Failed to read secret from file");
        }
    }

    return new SecretValue(secretString, SecretType.OPAQUE);
}
 
Example #4
Source File: Passwords.java    From bender with Apache License 2.0 6 votes vote down vote up
public static String decrypt(String str, Region region) throws UnsupportedEncodingException {
  if (isJUnitTest()) {
    return str;
  }

  AWSKMS kms = AWSKMSClientBuilder.standard().withRegion(region.getName()).build();

  /*
   * The KMS ciphertext is base64 encoded and must be decoded before the request is made
   */
  String cipherString = str;
  byte[] cipherBytes = Base64.decode(cipherString);

  /*
   * Create decode request and decode
   */
  ByteBuffer cipherBuffer = ByteBuffer.wrap(cipherBytes);
  DecryptRequest req = new DecryptRequest().withCiphertextBlob(cipherBuffer);
  DecryptResult resp = kms.decrypt(req);

  /*
   * Convert the response plaintext bytes to a string
   */
  return new String(resp.getPlaintext().array(), Charset.forName("UTF-8"));
}
 
Example #5
Source File: SnowflakeAzureClient.java    From snowflake-jdbc with Apache License 2.0 6 votes vote down vote up
/**
 * Adds encryption metadata to the StorageObjectMetadata object
 */
@Override
public void addEncryptionMetadata(StorageObjectMetadata meta,
                                  MatDesc matDesc,
                                  byte[] ivData,
                                  byte[] encKeK,
                                  long contentLength)
{
  meta.addUserMetadata(getMatdescKey(),
                       matDesc.toString());
  meta.addUserMetadata(AZ_ENCRYPTIONDATAPROP, buildEncryptionMetadataJSON(
      Base64.encodeAsString(ivData),
      Base64.encodeAsString(encKeK))
  );
  meta.setContentLength(contentLength);
}
 
Example #6
Source File: StorageObjectSummary.java    From snowflake-jdbc with Apache License 2.0 6 votes vote down vote up
private static String convertBase64ToHex(String base64String)
{
  try
  {
    byte[] bytes = Base64.decode(base64String);

    final StringBuilder builder = new StringBuilder();
    for (byte b : bytes)
    {
      builder.append(String.format("%02x", b));
    }
    return builder.toString();
    // return empty string if input is not a valid Base64 string
  }
  catch (Exception e)
  {
    return "";
  }
}
 
Example #7
Source File: SnowflakeGCSClient.java    From snowflake-jdbc with Apache License 2.0 6 votes vote down vote up
/**
 * Adds encryption metadata to the StorageObjectMetadata object
 */
@Override
public void addEncryptionMetadata(StorageObjectMetadata meta,
                                  MatDesc matDesc,
                                  byte[] ivData,
                                  byte[] encKeK,
                                  long contentLength)
{
  meta.addUserMetadata(getMatdescKey(),
                       matDesc.toString());
  meta.addUserMetadata(GCS_ENCRYPTIONDATAPROP, buildEncryptionMetadataJSON(
      Base64.encodeAsString(ivData),
      Base64.encodeAsString(encKeK))
  );
  meta.setContentLength(contentLength);
}
 
Example #8
Source File: SnowflakeS3Client.java    From snowflake-jdbc with Apache License 2.0 6 votes vote down vote up
/**
 * Adds encryption metadata to the StorageObjectMetadata object
 */
@Override
public void addEncryptionMetadata(StorageObjectMetadata meta,
                                  MatDesc matDesc,
                                  byte[] ivData,
                                  byte[] encKeK,
                                  long contentLength)
{
  meta.addUserMetadata(getMatdescKey(),
                       matDesc.toString());
  meta.addUserMetadata(AMZ_KEY,
                       Base64.encodeAsString(encKeK));
  meta.addUserMetadata(AMZ_IV,
                       Base64.encodeAsString(ivData));
  meta.setContentLength(contentLength);
}
 
Example #9
Source File: S3WritableByteChannel.java    From beam with Apache License 2.0 6 votes vote down vote up
private void flush() throws IOException {
  uploadBuffer.flip();
  ByteArrayInputStream inputStream = new ByteArrayInputStream(uploadBuffer.array());

  UploadPartRequest request =
      new UploadPartRequest()
          .withBucketName(path.getBucket())
          .withKey(path.getKey())
          .withUploadId(uploadId)
          .withPartNumber(partNumber++)
          .withPartSize(uploadBuffer.remaining())
          .withMD5Digest(Base64.encodeAsString(md5.digest()))
          .withInputStream(inputStream);
  request.setSSECustomerKey(options.getSSECustomerKey());

  UploadPartResult result;
  try {
    result = amazonS3.uploadPart(request);
  } catch (AmazonClientException e) {
    throw new IOException(e);
  }
  uploadBuffer.clear();
  md5.reset();
  eTags.add(result.getPartETag());
}
 
Example #10
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithTaupageAmi() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("blub: fdsa".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();


    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #11
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithVersionSimilarToNumber() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: 6478e18".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();

    assertThat(result.get().getApplicationId()).isEqualTo("fdsa");
    assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18");

    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #12
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithVersionSimilarToNumber1() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: '6478e18'".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();

    assertThat(result.get().getApplicationId()).isEqualTo("fdsa");
    assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18");

    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #13
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithTaupageAmiButInvalidYaml() throws Exception {
    // a yaml list is not a valid taupage format. Map is required.
    final String yamlData = "- a\n- b\n- c\n";

    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString(yamlData.getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isEmpty();


    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #14
Source File: TransformerHolisticTests.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
private void dumpTables() {
    for (String table : client.listTables().getTableNames()) {
        ScanResult scanResult;
        Map<String, AttributeValue> lastKey = null;
        do {
            scanResult = client.scan(new ScanRequest().withTableName(table).withExclusiveStartKey(lastKey));
            lastKey = scanResult.getLastEvaluatedKey();
            for (Map<String, AttributeValue> map : scanResult.getItems()) {
                for (Map.Entry<String, AttributeValue> item : map.entrySet()) {
                    System.out.print("item.put(\"");
                    System.out.print(item.getKey());
                    System.out.print("\", b642Av(\"");
                    System.out.print(Base64.encodeAsString(AttributeValueMarshaller.marshall(item.getValue()).array()));
                    System.out.println("\"));");
                }
                System.out.print("ddb.putItem(new PutItemRequest(\"");
                System.out.print(table);
                System.out.println("\", item));");
                System.out.println("item.clear();");
                System.out.println();
            }
        } while (lastKey != null);

    }
}
 
Example #15
Source File: CompressUtil.java    From s3-bucket-loader with Apache License 2.0 6 votes vote down vote up
public static char[] decompressAndB64DecodeUTF8Bytes(byte[] b64EncodedCompressedBytes) throws Exception {

		byte[] input = Base64.decode(b64EncodedCompressedBytes);
		
		// Compressor with highest level of compression
	    Inflater inflater = new Inflater();
	    
	    // Give the compressor the data to compress
	    inflater.setInput(input);
	    
	    ByteArrayOutputStream stream = new ByteArrayOutputStream();
	    byte[] buf = new byte[32];
	    while (!inflater.finished()) {
	        int count = inflater.inflate(buf);
	        stream.write(buf, 0, count);
	    }
	    return new String(stream.toByteArray(),"UTF-8").toCharArray();
	}
 
Example #16
Source File: FetchTaupageYamlImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testBrokenYaml() throws Exception{
    // a yaml list is not a valid taupage format. Map is required.
    final String yamlData = "- a\n- b\n- c\n";

    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString(yamlData.getBytes()))));

    final FetchTaupageYaml fetchTaupageYaml = new FetchTaupageYamlImpl(clientProviderMock);

    final Optional<TaupageYaml> result = fetchTaupageYaml.getTaupageYaml(INSTANCE_ID, ACCOUNT, REGION);

    assertThat(result).isEmpty();

    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #17
Source File: TestPasswordAuthentication.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void test()
        throws IOException
{
    String json = new ObjectMapper().writeValueAsString(ImmutableMap.<String, Object>builder()
            .put("value", 42L)
            .build());

    client.getLowLevelClient()
            .performRequest(
                    "POST",
                    "/test/_doc?refresh",
                    ImmutableMap.of(),
                    new NStringEntity(json, ContentType.APPLICATION_JSON),
                    new BasicHeader("Authorization", format("Basic %s", Base64.encodeAsString(format("%s:%s", USER, PASSWORD).getBytes(StandardCharsets.UTF_8)))));

    assertThat(assertions.query("SELECT * FROM test"))
            .matches("VALUES BIGINT '42'");
}
 
Example #18
Source File: SQSMessageConsumerPrefetchFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test ConvertToJMSMessage with an object message
 */
@Test
public void testConvertToJMSMessageObjectTypeAttribute() throws JMSException, IOException {

    /*
     * Set up consumer prefetch and mocks
     */

    com.amazonaws.services.sqs.model.Message message = createValidFifoMessage(1, "G");
    // Return message attributes with message type 'OBJECT'
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.OBJECT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    message.getMessageAttributes().put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    // Encode an object to byte array
    Integer integer = new Integer("10");
    ByteArrayOutputStream array = new ByteArrayOutputStream(10);
    ObjectOutputStream oStream = new ObjectOutputStream(array);
    oStream.writeObject(integer);
    oStream.close();
    
    message.setBody(Base64.encodeAsString(array.toByteArray()));

    /*
     * Convert the SQS message to JMS Message
     */
    javax.jms.Message jmsMessage = consumerPrefetch.convertToJMSMessage(message);

    /*
     * Verify results
     */
    assertTrue(jmsMessage instanceof SQSObjectMessage);
    assertEquals(integer, ((SQSObjectMessage) jmsMessage).getObject());
    assertEquals(message.getAttributes().get(SQSMessagingClientConstants.MESSAGE_DEDUPLICATION_ID), jmsMessage.getStringProperty(SQSMessagingClientConstants.JMS_SQS_DEDUPLICATION_ID));
    assertEquals(message.getAttributes().get(SQSMessagingClientConstants.SEQUENCE_NUMBER), jmsMessage.getStringProperty(SQSMessagingClientConstants.JMS_SQS_SEQUENCE_NUMBER));
    assertEquals(message.getAttributes().get(SQSMessagingClientConstants.MESSAGE_GROUP_ID), jmsMessage.getStringProperty(SQSMessagingClientConstants.JMSX_GROUP_ID));
}
 
Example #19
Source File: DynamoDBUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
public static byte[] decodeSortableBase64(final byte[] original) {
  final byte[] bytes = new byte[original.length];
  for (int i = 0; i < bytes.length; i++) {
    bytes[i] = sortableToDefault[original[i]];
  }
  return Base64.decode(bytes);
}
 
Example #20
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSByteMessage
 */
@Test
public void testSendInternalSQSByteMessageFromReceivedMessage() throws JMSException, IOException {
    
    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.BYTE_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
    String messageBody = Base64.encodeAsString(byteArray);
    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                    .withMessageAttributes(mapMessageAttributes)
                    .withAttributes(mapAttributes)
                    .withBody(messageBody);

    SQSObjectMessage msg = spy(new SQSObjectMessage(acknowledger, QUEUE_URL, message));

    Map<String, MessageAttributeValue> messageAttributes = createMessageAttribute("object");

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, Arrays.asList(messageBody),
            messageAttributes)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
}
 
Example #21
Source File: SQSMessageConsumerPrefetchFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test ConvertToJMSMessage with byte message type
 */
@Test
public void testConvertToJMSMessageByteTypeAttribute() throws JMSException, IOException {

    /*
     * Set up consumer prefetch and mocks
     */

    com.amazonaws.services.sqs.model.Message message = createValidFifoMessage(1, "G");
    // Return message attributes with message type 'BYTE'
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.BYTE_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    message.getMessageAttributes().put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
    message.setBody(Base64.encodeAsString(byteArray));

    /*
     * Convert the SQS message to JMS Message
     */
    javax.jms.Message jmsMessage = consumerPrefetch.convertToJMSMessage(message);

    /*
     * Verify results
     */
    assertTrue(jmsMessage instanceof SQSBytesMessage);
    for (byte b : byteArray) {
        assertEquals(b, ((SQSBytesMessage)jmsMessage).readByte());
    }
    assertEquals(message.getAttributes().get(SQSMessagingClientConstants.MESSAGE_DEDUPLICATION_ID), jmsMessage.getStringProperty(SQSMessagingClientConstants.JMS_SQS_DEDUPLICATION_ID));
    assertEquals(message.getAttributes().get(SQSMessagingClientConstants.SEQUENCE_NUMBER), jmsMessage.getStringProperty(SQSMessagingClientConstants.JMS_SQS_SEQUENCE_NUMBER));
    assertEquals(message.getAttributes().get(SQSMessagingClientConstants.MESSAGE_GROUP_ID), jmsMessage.getStringProperty(SQSMessagingClientConstants.JMSX_GROUP_ID));
}
 
Example #22
Source File: DirectKmsMaterialProviderTest.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleWithKmsEc3() throws GeneralSecurityException {
    DirectKmsMaterialProvider prov = new DirectKmsMaterialProvider(kms, keyId);

    Map<String, AttributeValue> attrVals = new HashMap<>();
    attrVals.put("hk",
            new AttributeValue().withB(ByteBuffer.wrap("Foo".getBytes(StandardCharsets.UTF_8))));
    attrVals.put("rk",
            new AttributeValue().withB(ByteBuffer.wrap("Bar".getBytes(StandardCharsets.UTF_8))));

    ctx = new EncryptionContext.Builder().withHashKeyName("hk").withRangeKeyName("rk")
            .withTableName("KmsTableName").withAttributeValues(attrVals).build();
    EncryptionMaterials eMat = prov.getEncryptionMaterials(ctx);
    SecretKey encryptionKey = eMat.getEncryptionKey();
    assertNotNull(encryptionKey);
    Key signingKey = eMat.getSigningKey();
    assertNotNull(signingKey);
    assertNotNull(signingKey);
    Map<String, String> kmsCtx = kms.getSingleEc();
    assertEquals(Base64.encodeAsString("Foo".getBytes(StandardCharsets.UTF_8)),
            kmsCtx.get("hk"));
    assertEquals(Base64.encodeAsString("Bar".getBytes(StandardCharsets.UTF_8)),
            kmsCtx.get("rk"));
    assertEquals("KmsTableName", kmsCtx.get("*aws-kms-table*"));

    EncryptionContext dCtx = new EncryptionContext.Builder(ctx(eMat)).withHashKeyName("hk")
            .withRangeKeyName("rk").withTableName("KmsTableName").withAttributeValues(attrVals)
            .build();

    DecryptionMaterials dMat = prov.getDecryptionMaterials(dCtx);
    assertEquals(encryptionKey, dMat.getDecryptionKey());
    assertEquals(signingKey, dMat.getVerificationKey());
}
 
Example #23
Source File: KeyStoreMaterialsProviderTest.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {

    KeyGenerator macGen = KeyGenerator.getInstance("HmacSHA256");
    macGen.init(256, Utils.getRng());
    macKey = macGen.generateKey();

    KeyGenerator aesGen = KeyGenerator.getInstance("AES");
    aesGen.init(128, Utils.getRng());
    encryptionKey = aesGen.generateKey();

    keyStore = KeyStore.getInstance("jceks");
    keyStore.load(null, password.toCharArray());

    KeyFactory kf = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec rsaSpec = new PKCS8EncodedKeySpec(Base64.decode(keyPem));
    privateKey = kf.generatePrivate(rsaSpec);
    CertificateFactory cf = CertificateFactory.getInstance("X509");
    certificate = cf.generateCertificate(new ByteArrayInputStream(Base64.decode(certPem)));


    keyStore.setEntry("enc", new SecretKeyEntry(encryptionKey), passwordProtection);
    keyStore.setEntry("sig", new SecretKeyEntry(macKey), passwordProtection);
    keyStore.setEntry("enc-a", new PrivateKeyEntry(privateKey, new Certificate[]{certificate}), passwordProtection);
    keyStore.setEntry("sig-a", new PrivateKeyEntry(privateKey, new Certificate[]{certificate}), passwordProtection);
    keyStore.setCertificateEntry("trustedCert", certificate);
}
 
Example #24
Source File: SQSSpanProcessor.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
private void process(final List<Message> messages) {
  if (messages.size() == 0) return;

  final List<DeleteMessageBatchRequestEntry> toDelete = new ArrayList<>();
  int count = 0;
  for (Message message : messages) {
    final String deleteId = String.valueOf(count++);
    try {
      String stringBody = message.getBody();
      if (stringBody.isEmpty() || stringBody.equals("[]")) continue;
      // allow plain-text json, but permit base64 encoded thrift or json
      byte[] serialized =
          stringBody.charAt(0) == '[' ? stringBody.getBytes(UTF_8) : Base64.decode(stringBody);
      metrics.incrementMessages();
      metrics.incrementBytes(serialized.length);
      collector.acceptSpans(
          serialized,
          new Callback<Void>() {
            @Override
            public void onSuccess(Void value) {
              toDelete.add(
                  new DeleteMessageBatchRequestEntry(deleteId, message.getReceiptHandle()));
            }

            @Override
            public void onError(Throwable t) {
              logger.log(Level.WARNING, "collector accept failed", t);
              // for cases that are not recoverable just discard the message,
              // otherwise ignore so processing can be retried.
              if (t instanceof IllegalArgumentException) {
                toDelete.add(
                    new DeleteMessageBatchRequestEntry(deleteId, message.getReceiptHandle()));
              }
            }
          });
    } catch (RuntimeException | Error e) {
      logger.log(Level.WARNING, "message decoding failed", e);
      toDelete.add(new DeleteMessageBatchRequestEntry(deleteId, message.getReceiptHandle()));
    }
  }

  if (!toDelete.isEmpty()) {
    delete(toDelete);
  }
}
 
Example #25
Source File: LambdaInvokeService.java    From aws-lambda-jenkins-plugin with MIT License 5 votes vote down vote up
/**
 * Synchronously or asynchronously invokes an AWS Lambda function.
 * If synchronously invoked, the AWS Lambda log is collected and the response payload is returned
 * @param invokeConfig AWS Lambda invocation configuration
 * @return response payload
 */
public String invokeLambdaFunction(InvokeConfig invokeConfig) throws LambdaInvokeException {
    InvokeRequest invokeRequest = new InvokeRequest()
            .withFunctionName(invokeConfig.getFunctionName())
            .withPayload(invokeConfig.getPayload());

    if(invokeConfig.isSynchronous()){
        invokeRequest
                .withInvocationType(InvocationType.RequestResponse)
                .withLogType(LogType.Tail);
    } else {
        invokeRequest
                .withInvocationType(InvocationType.Event);
    }
    logger.log("Lambda invoke request:%n%s%nPayload:%n%s%n", invokeRequest.toString(), invokeConfig.getPayload());

    InvokeResult invokeResult = client.invoke(invokeRequest);
    String payload = "";
    if(invokeResult.getPayload() != null){
        payload = new String(invokeResult.getPayload().array(), Charset.forName("UTF-8"));
    }
    logger.log("Lambda invoke response:%n%s%nPayload:%n%s%n", invokeResult.toString(), payload);

    if(invokeResult.getLogResult() != null){
        logger.log("Log:%n%s%n", new String(Base64.decode(invokeResult.getLogResult()), Charset.forName("UTF-8")));
    }

    if(StringUtils.isNotEmpty(invokeResult.getFunctionError())){
        throw new LambdaInvokeException("Function returned error of type: " + invokeResult.getFunctionError());
    }

    return payload;
}
 
Example #26
Source File: HttpIT.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicAuth()
        throws Exception
{
    String uri = "http://localhost:" + httpMockWebServer.getPort() + "/test";
    runWorkflow(folder, "acceptance/http/http.dig", ImmutableMap.of("test_uri", uri), ImmutableMap.of(
            "secrets.http.user", "test-user",
            "secrets.http.password", "test-pass"));
    assertThat(httpMockWebServer.getRequestCount(), is(1));
    RecordedRequest request = httpMockWebServer.takeRequest();

    assertThat(request.getHeader(AUTHORIZATION.asString()), is("Basic " + Base64.encodeAsString("test-user:test-pass".getBytes(UTF_8))));
}
 
Example #27
Source File: TestSpecFactory.java    From justtestlah with Apache License 2.0 5 votes vote down vote up
public String createTestSpec() throws IOException {
  Scanner scanner =
      new Scanner(
          AWSTestRunner.class
              .getClassLoader()
              .getResourceAsStream("aws-devicefarm-testspec-template.yml"));
  String testSpec = scanner.useDelimiter("\\A").next();
  scanner.close();
  StringWriter justTestLahProperties = new StringWriter();
  Properties props = new Properties();
  props.putAll(properties.getProperties());

  // these settings will be overridden by the test spec execution
  props.remove("android.appPath");
  props.remove("ios.appPath");
  props.remove("cloudprovider");
  props.remove("testusers.file");
  props.store(justTestLahProperties, "justtestlah properties");

  // encode the `justtestlah.properties` into the testSpec file
  testSpec =
      testSpec.replace(
          "__JUSTTESTLAH_PROPERTIES_BASE64__",
          Base64.encodeAsString(
              justTestLahProperties.toString().replaceAll("(?m)^#.*", "").getBytes()));

  LOG.info("Test spec file: \n{}", testSpec);
  String path =
      System.getProperty("java.io.tmpdir") + File.separator + "aws-devicefarm-testspec.yml";
  Files.write(Paths.get(path), testSpec.getBytes());
  return path;
}
 
Example #28
Source File: RepositoryS3.java    From github-bucket with ISC License 5 votes vote down vote up
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException {
    byte[] content;
    byte[] newHash;
    LOG.debug("Start processing file: {}", path);
    try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) {
        // Get content
        content = IOUtils.toByteArray(is);
        // Get hash
        newHash = is.getMessageDigest().digest();
    }
    if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) {
        LOG.info("Uploading file: {}", path);
        ObjectMetadata bucketMetadata = new ObjectMetadata();
        bucketMetadata.setContentMD5(Base64.encodeAsString(newHash));
        bucketMetadata.setContentLength(content.length);
        // Give Tika a few hints for the content detection
        Metadata tikaMetadata = new Metadata();
        tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
        // Fire!
        try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) {
            bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
            s3.putObject(bucket.getName(), path, bis, bucketMetadata);
            return true;
        }
    }
    LOG.info("Skipping file (same checksum): {}", path);
    return false;
}
 
Example #29
Source File: TaupageYamlProviderImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
private Optional<TaupageYaml> getTaupageYaml(@Nonnull final EC2InstanceContext context) {

        if (context.isTaupageAmi().orElse(false)) {

            final String instanceId = context.getInstanceId();

            try {
                return Optional.of(context.getClient(AmazonEC2Client.class))
                        .map(client -> client.describeInstanceAttribute(new DescribeInstanceAttributeRequest()
                                .withInstanceId(instanceId)
                                .withAttribute(USER_DATA)))
                        .map(DescribeInstanceAttributeResult::getInstanceAttribute)
                        .map(InstanceAttribute::getUserData)
                        .map(Base64::decode)
                        .map(String::new)
                        .map(TaupageYamlUtil::parseTaupageYaml);

            } catch (final AmazonClientException e) {
                log.warn("Could not get Taupage YAML for instance: " + instanceId, e);
                return empty();
            } catch (YAMLException | IllegalArgumentException s)   {
                log.warn("Taupage YAML is not valid for instance: " + instanceId, s);
                return empty();
            }

        } else {
            return empty();
        }

    }
 
Example #30
Source File: AttributeValueMarshallerTest.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testVersioningCompatibility() {
    AttributeValue newObject = buildComplexAttributeValue();
    byte[] oldBytes = Base64.decode(COMPLEX_ATTRIBUTE_MARSHALLED);
    byte[] newBytes = marshall(newObject).array();
    AssertJUnit.assertArrayEquals(oldBytes, newBytes);

    AttributeValue oldObject = unmarshall(ByteBuffer.wrap(oldBytes));
    assertEquals(oldObject, newObject);
}