com.amazonaws.auth.PropertiesFileCredentialsProvider Java Examples

The following examples show how to use com.amazonaws.auth.PropertiesFileCredentialsProvider. 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: AWSCredentialsProviderControllerServiceTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileCredentialsProvider() throws Throwable {
    final TestRunner runner = TestRunners.newTestRunner(FetchS3Object.class);
    final AWSCredentialsProviderControllerService serviceImpl = new AWSCredentialsProviderControllerService();
    runner.addControllerService("awsCredentialsProvider", serviceImpl);
    runner.setProperty(serviceImpl, CredentialPropertyDescriptors.CREDENTIALS_FILE,
            "src/test/resources/mock-aws-credentials.properties");
    runner.enableControllerService(serviceImpl);

    runner.assertValid(serviceImpl);
    final AWSCredentialsProviderService service = (AWSCredentialsProviderService) runner.getProcessContext()
            .getControllerServiceLookup().getControllerService("awsCredentialsProvider");
    Assert.assertNotNull(service);
    final AWSCredentialsProvider credentialsProvider = service.getCredentialsProvider();
    Assert.assertNotNull(credentialsProvider);
    assertEquals("credentials provider should be equal", PropertiesFileCredentialsProvider.class,
            credentialsProvider.getClass());
}
 
Example #2
Source File: AWSCredentialsProviderControllerServiceTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileCredentialsProvider() throws Throwable {
    final TestRunner runner = TestRunners.newTestRunner(FetchS3Object.class);
    final AWSCredentialsProviderControllerService serviceImpl = new AWSCredentialsProviderControllerService();
    runner.addControllerService("awsCredentialsProvider", serviceImpl);
    runner.setProperty(serviceImpl, AbstractAWSProcessor.CREDENTIALS_FILE,
            "src/test/resources/mock-aws-credentials.properties");
    runner.enableControllerService(serviceImpl);

    runner.assertValid(serviceImpl);
    final AWSCredentialsProviderService service = (AWSCredentialsProviderService) runner.getProcessContext()
            .getControllerServiceLookup().getControllerService("awsCredentialsProvider");
    Assert.assertNotNull(service);
    final AWSCredentialsProvider credentialsProvider = service.getCredentialsProvider();
    Assert.assertNotNull(credentialsProvider);
    assertEquals("credentials provider should be equal", PropertiesFileCredentialsProvider.class,
            credentialsProvider.getClass());
}
 
Example #3
Source File: AwsModuleTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testPropertiesFileCredentialsProviderSerializationDeserialization() throws Exception {
  String credentialsFilePath = "/path/to/file";

  PropertiesFileCredentialsProvider credentialsProvider =
      new PropertiesFileCredentialsProvider(credentialsFilePath);

  String serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  AWSCredentialsProvider deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AWSCredentialsProvider.class);

  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  Field field = PropertiesFileCredentialsProvider.class.getDeclaredField("credentialsFilePath");
  field.setAccessible(true);
  String deserializedCredentialsFilePath = (String) field.get(deserializedCredentialsProvider);
  assertEquals(credentialsFilePath, deserializedCredentialsFilePath);
}
 
Example #4
Source File: AWSProvider.java    From testgrid with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<String> getInstanceName(String region, String instanceId) {
    final AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.standard()
            .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString()))
            .withRegion(region)
            .build();

    List<String> instanceIds = new ArrayList<>();
    instanceIds.add(instanceId);

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.setInstanceIds(instanceIds);

    DescribeInstancesResult result = amazonEC2.describeInstances(request);
    Optional<String> tagOptional = result.getReservations()
            .stream()
            .flatMap(reservation -> reservation.getInstances().stream())
            .flatMap(instance -> instance.getTags().stream())
            .filter(tag -> NAME_ENTRY.equals(tag.getKey()))
            .findFirst()
            .map(Tag::getValue);
    return tagOptional;
}
 
Example #5
Source File: AWSProvider.java    From testgrid with Apache License 2.0 5 votes vote down vote up
/**
 *  This method will retrieve the instance username that is used to log into the instance via ssh.
 *  E.g Ubuntu instance has username ubuntu
 *  CentOS instance has username centos
 *  The username must be present in the ami as a TAG with key USERNAME.
 *
 * @param region The aws region where instance is located
 * @param instanceId ID value for the instance
 * @return The username extracted from the TAG
 */
public Optional<String> getInstanceUserName(String region, String instanceId) {

    final AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.standard()
            .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString()))
            .withRegion(region)
            .build();

    List<String> instanceIds = new ArrayList<>();
    instanceIds.add(instanceId);
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.setInstanceIds(instanceIds);
    DescribeInstancesResult result = amazonEC2.describeInstances(request);
    //Get instance id from the results
    Optional<String> imageId = result.getReservations().stream()
            .flatMap(reservation -> reservation.getInstances().stream())
            .findFirst()
            .map(Instance::getImageId);

    if (imageId.isPresent()) {
        List<String> imageIds = new ArrayList<>();
        imageIds.add(imageId.get());
        //get ami from the instance ID
        DescribeImagesRequest imageReq = new DescribeImagesRequest();
        imageReq.setImageIds(imageIds);
        //Get the Tag containing the username from the ami
        DescribeImagesResult describeImagesResult = amazonEC2.describeImages(imageReq);
        Optional<String> userName = describeImagesResult.getImages().stream()
                .flatMap(image -> image.getTags().stream())
                .filter(tag -> USERNAME_ENTRY.equals(tag.getKey()))
                .findFirst()
                .map(Tag::getValue);
        return userName;
    }
    return Optional.empty();
}
 
Example #6
Source File: TestCredentialsProviderFactory.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCredentials() throws Throwable {
    final TestRunner runner = TestRunners.newTestRunner(MockAWSProcessor.class);
    runner.setProperty(CredentialPropertyDescriptors.CREDENTIALS_FILE, "src/test/resources/mock-aws-credentials.properties");
    runner.assertValid();

    Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties();
    final CredentialsProviderFactory factory = new CredentialsProviderFactory();
    final AWSCredentialsProvider credentialsProvider = factory.getCredentialsProvider(properties);
    Assert.assertNotNull(credentialsProvider);
    assertEquals("credentials provider should be equal", PropertiesFileCredentialsProvider.class,
            credentialsProvider.getClass());
}
 
Example #7
Source File: S3StorageFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
private static AWSCredentialsProvider buildCredentialsProvider(Config config)
{
    if (config.has("credentials.file")) {
        return new PropertiesFileCredentialsProvider(
                config.get("credentials.file", String.class));
    }
    else if (config.has("credentials.access-key-id")) {
        final BasicAWSCredentials creds = new BasicAWSCredentials(
            config.get("credentials.access-key-id", String.class),
            config.get("credentials.secret-access-key", String.class));
        return new AWSCredentialsProvider()
        {
            @Override
            public AWSCredentials getCredentials()
            {
                return creds;
            }

            @Override
            public void refresh()
            { }
        };
    }
    else {
        return new DefaultAWSCredentialsProviderChain();
    }
}
 
Example #8
Source File: AwsModule.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public AWSCredentialsProvider deserializeWithType(
    JsonParser jsonParser, DeserializationContext context, TypeDeserializer typeDeserializer)
    throws IOException {
  Map<String, String> asMap =
      jsonParser.readValueAs(new TypeReference<Map<String, String>>() {});

  String typeNameKey = typeDeserializer.getPropertyName();
  String typeName = asMap.get(typeNameKey);
  if (typeName == null) {
    throw new IOException(
        String.format("AWS credentials provider type name key '%s' not found", typeNameKey));
  }

  if (typeName.equals(AWSStaticCredentialsProvider.class.getSimpleName())) {
    return new AWSStaticCredentialsProvider(
        new BasicAWSCredentials(asMap.get(AWS_ACCESS_KEY_ID), asMap.get(AWS_SECRET_KEY)));
  } else if (typeName.equals(PropertiesFileCredentialsProvider.class.getSimpleName())) {
    return new PropertiesFileCredentialsProvider(asMap.get(CREDENTIALS_FILE_PATH));
  } else if (typeName.equals(
      ClasspathPropertiesFileCredentialsProvider.class.getSimpleName())) {
    return new ClasspathPropertiesFileCredentialsProvider(asMap.get(CREDENTIALS_FILE_PATH));
  } else if (typeName.equals(DefaultAWSCredentialsProviderChain.class.getSimpleName())) {
    return new DefaultAWSCredentialsProviderChain();
  } else if (typeName.equals(EnvironmentVariableCredentialsProvider.class.getSimpleName())) {
    return new EnvironmentVariableCredentialsProvider();
  } else if (typeName.equals(SystemPropertiesCredentialsProvider.class.getSimpleName())) {
    return new SystemPropertiesCredentialsProvider();
  } else if (typeName.equals(ProfileCredentialsProvider.class.getSimpleName())) {
    return new ProfileCredentialsProvider();
  } else if (typeName.equals(EC2ContainerCredentialsProviderWrapper.class.getSimpleName())) {
    return new EC2ContainerCredentialsProviderWrapper();
  } else {
    throw new IOException(
        String.format("AWS credential provider type '%s' is not supported", typeName));
  }
}
 
Example #9
Source File: AWSArtifactReader.java    From testgrid with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance of {@link AWSArtifactReader} for the given region and bucket name.
 * <p>
 * Please do note that accessKey and secretKey values should be set in a properties file
 * in order to authenticate to the AWS.
 *
 * @param region region where the S3 bucket is located
 * @param bucket name of the bucket
 * @throws ArtifactReaderException thrown if the given parameters are null or empty
 */
public AWSArtifactReader(String region, String bucket) throws ArtifactReaderException, IOException {
    if (StringUtil.isStringNullOrEmpty(region)) {
        throw new ArtifactReaderException("AWS S3 bucket region is null or empty");
    }
    if (StringUtil.isStringNullOrEmpty(bucket)) {
        throw new ArtifactReaderException("AWS S3 bucket name is null or empty");
    }
    Path configFilePath = TestGridUtil.getConfigFilePath();
    amazonS3 = AmazonS3ClientBuilder.standard()
            .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString()))
            .withRegion(region)
            .build();
    bucketName = bucket;
}
 
Example #10
Source File: AWSProvider.java    From testgrid with Apache License 2.0 5 votes vote down vote up
/**
 * This method releases the provisioned AWS infrastructure.
 *
 * @param script            the script config
 * @param inputs
 * @return true or false to indicate the result of destroy operation.
 * @throws TestGridInfrastructureException when AWS error occurs in deletion process.
 * @throws InterruptedException            when there is an interruption while waiting for the result.
 */
private boolean doRelease(Script script, Properties inputs, TestPlan testPlan)
        throws TestGridInfrastructureException, InterruptedException, TestGridDAOException {
    Path configFilePath;
    String stackName = script.getName();
    String region = inputs.getProperty(AWS_REGION_PARAMETER);
    configFilePath = TestGridUtil.getConfigFilePath();
    AmazonCloudFormation stackdestroy = AmazonCloudFormationClientBuilder.standard()
            .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString()))
            .withRegion(region)
            .build();
    DeleteStackRequest deleteStackRequest = new DeleteStackRequest();
    deleteStackRequest.setStackName(stackName);
    stackdestroy.deleteStack(deleteStackRequest);
    logger.info(StringUtil.concatStrings("Stack : ", stackName, " is handed over for deletion!"));

    //Notify AWSResourceManager about stack destruction to release acquired resources
    AWSResourceManager awsResourceManager = new AWSResourceManager();
    awsResourceManager.notifyStackDeletion(testPlan, script, region);

    boolean waitForStackDeletion = Boolean
            .parseBoolean(getProperty(ConfigurationProperties.WAIT_FOR_STACK_DELETION));
    if (waitForStackDeletion) {
        logger.info(StringUtil.concatStrings("Waiting for stack : ", stackName, " to delete.."));
        Waiter<DescribeStacksRequest> describeStacksRequestWaiter = new
                AmazonCloudFormationWaiters(stackdestroy).stackDeleteComplete();
        try {
            describeStacksRequestWaiter.run(new WaiterParameters<>(new DescribeStacksRequest()
                    .withStackName(stackName)));
        } catch (WaiterUnrecoverableException e) {

            throw new TestGridInfrastructureException("Error occurred while waiting for Stack :"
                                                      + stackName + " deletion !");
        }
    }
    return true;
}
 
Example #11
Source File: AWSProvider.java    From testgrid with Apache License 2.0 5 votes vote down vote up
private static AmazonEC2 getAmazonEC2(String region) {
    Path configFilePath = TestGridUtil.getConfigFilePath();
    return AmazonEC2ClientBuilder.standard()
            .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString()))
            .withRegion(region)
            .build();
}
 
Example #12
Source File: AMIMapper.java    From testgrid with Apache License 2.0 5 votes vote down vote up
public AMIMapper(String allocatedRegion) throws TestGridInfrastructureException {
    Path configFilePath = Paths.get(TestGridUtil.getTestGridHomePath(),
            TestGridConstants.TESTGRID_CONFIG_FILE);
    if (!Files.exists(configFilePath)) {
        throw new TestGridInfrastructureException(
                TestGridConstants.TESTGRID_CONFIG_FILE + " file not found." +
                        " Unable to obtain AWS credentials. Check if the file exists in " +
                        configFilePath.toFile().toString());
    }
    this.region = allocatedRegion;
    amazonEC2 = AmazonEC2ClientBuilder.standard()
            .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString()))
            .withRegion(region)
            .build();
}
 
Example #13
Source File: TestCredentialsProviderFactory.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCredentials() throws Throwable {
    final TestRunner runner = TestRunners.newTestRunner(MockAWSProcessor.class);
    runner.setProperty(CredentialPropertyDescriptors.CREDENTIALS_FILE, "src/test/resources/mock-aws-credentials.properties");
    runner.assertValid();

    Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties();
    final CredentialsProviderFactory factory = new CredentialsProviderFactory();
    final AWSCredentialsProvider credentialsProvider = factory.getCredentialsProvider(properties);
    Assert.assertNotNull(credentialsProvider);
    assertEquals("credentials provider should be equal", PropertiesFileCredentialsProvider.class,
            credentialsProvider.getClass());
}
 
Example #14
Source File: FileCredentialsStrategy.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public AWSCredentialsProvider getCredentialsProvider(Map<PropertyDescriptor, String> properties) {
    String credsFile = properties.get(CredentialPropertyDescriptors.CREDENTIALS_FILE);
    return new PropertiesFileCredentialsProvider(credsFile);
}
 
Example #15
Source File: SQSTestBase.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
public SQSTestBase()
{
  PropertiesFileCredentialsProvider file = new PropertiesFileCredentialsProvider(getTestCredsFilePath());
  testCreds = (PropertiesCredentials)file.getCredentials();
  sqs = new AmazonSQSClient(testCreds);
}
 
Example #16
Source File: SQSStringInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
protected void starting(Description description)
{
  final String methodName = description.getMethodName();
  final String className = description.getClassName();

  testBase = new SQSTestBase();
  if (testBase.validateTestCreds() == false) {
    return;
  }
  testBase.generateCurrentQueueName(methodName);
  try {
    testBase.beforTest();
  } catch (AssumptionViolatedException ave) {
    throw ave;
  } catch (Exception e) {
    throw new RuntimeException(e);
  }

  baseDir = "target/" + className + "/" + methodName;

  Attribute.AttributeMap attributeMap = new Attribute.AttributeMap.DefaultAttributeMap();
  attributeMap.put(Context.OperatorContext.SPIN_MILLIS, 500);
  attributeMap.put(Context.DAGContext.APPLICATION_PATH, baseDir);

  context = mockOperatorContext(1, attributeMap);
  operator = new JMSStringInputOperator();
  operator.setConnectionFactoryBuilder(new JMSBase.ConnectionFactoryBuilder()
  {

    @Override
    public ConnectionFactory buildConnectionFactory()
    {
      // Create the connection factory using the environment variable credential provider.
      // Connections this factory creates can talk to the queues in us-east-1 region.
      SQSConnectionFactory connectionFactory =
          SQSConnectionFactory.builder()
          .withRegion(Region.getRegion(Regions.US_EAST_1))
          .withAWSCredentialsProvider(new PropertiesFileCredentialsProvider(testBase.getDevCredsFilePath()))
          .build();
      return connectionFactory;
    }

    @Override
    public String toString()
    {
      return className + "/" + methodName + "/ConnectionFactoryBuilder";
    }

  });
  operator.setSubject(testBase.getCurrentQueueName());
  // for SQS ack mode should be "AUTO_ACKNOWLEDGE" and transacted = false
  operator.setAckMode("AUTO_ACKNOWLEDGE");
  operator.setTransacted(false);

  sink = new CollectorTestSink<>();
  operator.output.setSink(sink);
  operator.setup(context);
  operator.activate(context);
}
 
Example #17
Source File: FileCredentialsStrategy.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public AWSCredentialsProvider getCredentialsProvider(Map<PropertyDescriptor, String> properties) {
    String credsFile = properties.get(CredentialPropertyDescriptors.CREDENTIALS_FILE);
    return new PropertiesFileCredentialsProvider(credsFile);
}
 
Example #18
Source File: TestReportEngineTest.java    From testgrid with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "testPlanInputsNum")
public void generateEmailReport(String testNum) throws Exception {
    logger.info("---- Running " + testNum);

    List<TestPlan> testPlans = getTestPlansFor(testNum);
    when(testPlanUOW.getLatestTestPlans(Matchers.any(Product.class)))
            .thenReturn(testPlans);
    when(testPlanUOW.getTestPlanById("abc")).thenReturn(Optional.of(testPlans.get(0)));
    when(testPlanUOW.getTestPlanById(Matchers.anyString())).thenAnswer(
            inv -> {
                final String testPlanId = inv.getArgumentAt(0, String.class);
                if (testPlanId.equals("abc")) {
                    return Optional.of(testPlans.get(0));
                }
                return Optional.of(testPlans.get(Integer.valueOf(testPlanId)));
            });
    when(testPlanUOW.getCurrentStatus(product)).thenReturn(TestPlanStatus.FAIL);
    when(testPlanUOW.getTestExecutionSummary(any())).thenReturn(testPlans.stream().map(tp -> tp.getStatus()
            .toString()).collect(Collectors.toList()));

    final TestReportEngine testReportEngine = new TestReportEngine(testPlanUOW,
            new EmailReportProcessor(testPlanUOW, infrastructureParameterUOW), new GraphDataProvider(testPlanUOW));

    PowerMockito.mockStatic(ConfigurationContext.class);
    when(ConfigurationContext.getProperty(ConfigurationProperties.AWS_S3_BUCKET_NAME))
            .thenReturn("testrun-artifacts");
    when(ConfigurationContext.getProperty(ConfigurationProperties.AWS_S3_ARTIFACTS_DIR)).thenReturn("artifacts");

    AmazonS3 s3ClientMock = Mockito.mock(AmazonS3.class);
    AmazonS3ClientBuilder amazonS3ClientBuilderMock = PowerMockito.mock(AmazonS3ClientBuilder.class);
    S3Object s3ObjectMock = Mockito.mock(S3Object.class);
    S3ObjectInputStream inputStream = Mockito.mock(S3ObjectInputStream.class);

    PowerMockito.mockStatic(AmazonS3ClientBuilder.class);
    PowerMockito.when(AmazonS3ClientBuilder.standard()).thenReturn(amazonS3ClientBuilderMock);
    PowerMockito.when(amazonS3ClientBuilderMock.withRegion(anyString())).thenReturn(amazonS3ClientBuilderMock);
    PowerMockito.when(amazonS3ClientBuilderMock
            .withCredentials(any(PropertiesFileCredentialsProvider.class))).thenReturn(amazonS3ClientBuilderMock);
    PowerMockito.when(amazonS3ClientBuilderMock.build()).thenReturn(s3ClientMock);

    when(s3ClientMock.getObject(anyString(), anyString())).thenReturn(s3ObjectMock);
    when(s3ObjectMock.getObjectContent()).thenReturn(inputStream);

    if (testNum.equals("02")) {
        //We can only handle maximum of one data provider for chart generation code.
        //TODO: this was done because one JVM can only call JAVAFX Platform.exit() once.
        // See @ChartGenerator#stopApplication

        Optional<Path> path = testReportEngine.generateSummarizedEmailReport(product, productDir.toString());
        Assert.assertTrue(path.isPresent(), "Email report generation has failed. File path is empty.");
        logger.info("v2 email report file: " + path.get());

    }

}