com.amazonaws.auth.EnvironmentVariableCredentialsProvider Java Examples

The following examples show how to use com.amazonaws.auth.EnvironmentVariableCredentialsProvider. 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: S3KeyGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() throws Exception {

  if (multiPart && fileSize < OM_MULTIPART_MIN_SIZE) {
    throw new IllegalArgumentException(
        "Size of multipart upload parts should be at least 5MB (5242880)");
  }
  init();

  AmazonS3ClientBuilder amazonS3ClientBuilder =
      AmazonS3ClientBuilder.standard()
          .withCredentials(new EnvironmentVariableCredentialsProvider());

  if (endpoint.length() > 0) {
    amazonS3ClientBuilder
        .withPathStyleAccessEnabled(true)
        .withEndpointConfiguration(
            new EndpointConfiguration(endpoint, "us-east-1"));

  } else {
    amazonS3ClientBuilder.withRegion(Regions.DEFAULT_REGION);
  }

  s3 = amazonS3ClientBuilder.build();

  content = RandomStringUtils.randomAscii(fileSize);

  timer = getMetrics().timer("key-create");

  System.setProperty(DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");
  runTests(this::createKey);

  return null;
}
 
Example #2
Source File: AWSLambdaConfiguration.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 * @param clientConfiguration clientConfiguration
 * @param environment environment
 */
public AWSLambdaConfiguration(AWSClientConfiguration clientConfiguration, Environment environment) {
    this.clientConfiguration = clientConfiguration;

    this.builder.setCredentials(new AWSCredentialsProviderChain(
        new EnvironmentAWSCredentialsProvider(environment),
        new EnvironmentVariableCredentialsProvider(),
        new SystemPropertiesCredentialsProvider(),
        new ProfileCredentialsProvider(),
        new EC2ContainerCredentialsProviderWrapper()
    ));
}
 
Example #3
Source File: Main.java    From apigateway-generic-java-sdk with Apache License 2.0 5 votes vote down vote up
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
    String apiIntputStream = new Scanner(inputStream).useDelimiter("\\A").next();
    JsonNode rootNode = (new ObjectMapper(new JsonFactory())).readTree(apiIntputStream);
    String myApiId = rootNode.path("requestContext").path("apiId").asText();
    if (myApiId.isEmpty()) { myApiId = "TODO"; } // Not called from API Gateway

    final GenericApiGatewayClient client = new GenericApiGatewayClientBuilder()
            .withClientConfiguration(new ClientConfiguration())
            .withCredentials(new EnvironmentVariableCredentialsProvider())
            .withEndpoint("https://" + myApiId + ".execute-api.us-west-2.amazonaws.com") // your API ID
            .withRegion(Region.getRegion(Regions.fromName("us-west-2")))
            .build();

    GenericApiGatewayResponse apiResponse;
    ProxyResponse resp;
    try {
        apiResponse = client.execute(  // throws exception for non-2xx response
                new GenericApiGatewayRequestBuilder()
                        .withHttpMethod(HttpMethodName.GET)
                        .withResourcePath("/Prod/hello").build());

        System.out.println("Response: " + apiResponse.getBody());
        System.out.println("Status: " + apiResponse.getHttpResponse().getStatusCode());

        resp = new ProxyResponse("200", apiResponse.getBody());

    } catch (GenericApiGatewayException e) {
        System.out.println("Client threw exception " + e);
        resp = new ProxyResponse("400", e.getMessage());
    }

    String responseString = new ObjectMapper(new JsonFactory()).writeValueAsString(resp);
    OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
    writer.write(responseString);
    writer.close();
}
 
Example #4
Source File: AuthenticationInfoAWSCredentialsProviderChain.java    From lambadaframework with MIT License 5 votes vote down vote up
AuthenticationInfoAWSCredentialsProviderChain(AuthenticationInfo authenticationInfo) {
    super(
            new InstanceProfileCredentialsProvider(),
            new ProfileCredentialsProvider(),
            new EnvironmentVariableCredentialsProvider(),
            new SystemPropertiesCredentialsProvider(),
            new InstanceProfileCredentialsProvider());
}
 
Example #5
Source File: DiscoverInstances.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE = "\n" +
        "To run this example, supply the Namespacename , ServiceName of aws cloud map!\n" +
        "\n" +
        "Ex: DiscoverInstances <namespace-name> <service-name> \n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String namespace_name = args[0];
    String service_name = args[1];

    AWSCredentials credentials =null;
    try {
        credentials= new EnvironmentVariableCredentialsProvider().getCredentials();
    }catch (Exception e) {
        throw new AmazonClientException("Cannot Load Credentials");
    }


    System.out.format("Instances in AWS cloud map %s:\n", namespace_name);

    AWSServiceDiscovery client = AWSServiceDiscoveryClientBuilder
            .standard()
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withRegion(System.getenv("AWS_REGION"))
            .build();

    DiscoverInstancesRequest request = new DiscoverInstancesRequest();
    request.setNamespaceName(namespace_name);
    request.setServiceName(service_name);

    DiscoverInstancesResult result=client.discoverInstances(request);

    System.out.println(result.toString());

    }
 
Example #6
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 #7
Source File: DeployTask.java    From gradle-beanstalk-plugin with MIT License 5 votes vote down vote up
@TaskAction
protected void deploy() {
    String versionLabel = getVersionLabel();

    AWSCredentialsProviderChain credentialsProvider = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider(), new ProfileCredentialsProvider(beanstalk.getProfile()), new EC2ContainerCredentialsProviderWrapper());

    BeanstalkDeployer deployer = new BeanstalkDeployer(beanstalk.getS3Endpoint(), beanstalk.getBeanstalkEndpoint(), credentialsProvider);

    File warFile = getProject().files(war).getSingleFile();
    deployer.deploy(warFile, deployment.getApplication(), deployment.getEnvironment(), deployment.getTemplate(), versionLabel);
}
 
Example #8
Source File: KinesisPubsubClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private static AmazonKinesis createClientWithCredentials(Properties props) throws AmazonClientException {
	AWSCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
	return AmazonKinesisClientBuilder.standard()
		.withCredentials(credentialsProvider)
		.withEndpointConfiguration(
			new AwsClientBuilder.EndpointConfiguration(
				props.getProperty(ConsumerConfigConstants.AWS_ENDPOINT), "us-east-1"))
		.build();
}
 
Example #9
Source File: AWSClusterSecurityManager.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
DefaultAWSCredentialsProviderChain(Config config) {
  super(new EnvironmentVariableCredentialsProvider(),
          new SystemPropertiesCredentialsProvider(),
          new ConfigurationCredentialsProvider(config),
          new ProfileCredentialsProvider(),
          new InstanceProfileCredentialsProvider());
}
 
Example #10
Source File: AWSUtil.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * If the provider is ASSUME_ROLE, then the credentials for assuming this role are determined
 * recursively.
 *
 * @param configProps the configuration properties
 * @param configPrefix the prefix of the config properties for this credentials provider,
 *                     e.g. aws.credentials.provider for the base credentials provider,
 *                     aws.credentials.provider.role.provider for the credentials provider
 *                     for assuming a role, and so on.
 */
private static AWSCredentialsProvider getCredentialsProvider(final Properties configProps, final String configPrefix) {
	CredentialProvider credentialProviderType;
	if (!configProps.containsKey(configPrefix)) {
		if (configProps.containsKey(AWSConfigConstants.accessKeyId(configPrefix))
			&& configProps.containsKey(AWSConfigConstants.secretKey(configPrefix))) {
			// if the credential provider type is not specified, but the Access Key ID and Secret Key are given, it will default to BASIC
			credentialProviderType = CredentialProvider.BASIC;
		} else {
			// if the credential provider type is not specified, it will default to AUTO
			credentialProviderType = CredentialProvider.AUTO;
		}
	} else {
		credentialProviderType = CredentialProvider.valueOf(configProps.getProperty(configPrefix));
	}

	switch (credentialProviderType) {
		case ENV_VAR:
			return new EnvironmentVariableCredentialsProvider();

		case SYS_PROP:
			return new SystemPropertiesCredentialsProvider();

		case PROFILE:
			String profileName = configProps.getProperty(
					AWSConfigConstants.profileName(configPrefix), null);
			String profileConfigPath = configProps.getProperty(
					AWSConfigConstants.profilePath(configPrefix), null);
			return (profileConfigPath == null)
				? new ProfileCredentialsProvider(profileName)
				: new ProfileCredentialsProvider(profileConfigPath, profileName);

		case BASIC:
			return new AWSCredentialsProvider() {
				@Override
				public AWSCredentials getCredentials() {
					return new BasicAWSCredentials(
						configProps.getProperty(AWSConfigConstants.accessKeyId(configPrefix)),
						configProps.getProperty(AWSConfigConstants.secretKey(configPrefix)));
				}

				@Override
				public void refresh() {
					// do nothing
				}
			};

		case ASSUME_ROLE:
			final AWSSecurityTokenService baseCredentials = AWSSecurityTokenServiceClientBuilder.standard()
					.withCredentials(getCredentialsProvider(configProps, AWSConfigConstants.roleCredentialsProvider(configPrefix)))
					.withRegion(configProps.getProperty(AWSConfigConstants.AWS_REGION))
					.build();
			return new STSAssumeRoleSessionCredentialsProvider.Builder(
					configProps.getProperty(AWSConfigConstants.roleArn(configPrefix)),
					configProps.getProperty(AWSConfigConstants.roleSessionName(configPrefix)))
					.withExternalId(configProps.getProperty(AWSConfigConstants.externalId(configPrefix)))
					.withStsClient(baseCredentials)
					.build();

		default:
		case AUTO:
			return new DefaultAWSCredentialsProviderChain();
	}
}
 
Example #11
Source File: AWSUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * If the provider is ASSUME_ROLE, then the credentials for assuming this role are determined
 * recursively.
 *
 * @param configProps the configuration properties
 * @param configPrefix the prefix of the config properties for this credentials provider,
 *                     e.g. aws.credentials.provider for the base credentials provider,
 *                     aws.credentials.provider.role.provider for the credentials provider
 *                     for assuming a role, and so on.
 */
private static AWSCredentialsProvider getCredentialsProvider(final Properties configProps, final String configPrefix) {
	CredentialProvider credentialProviderType;
	if (!configProps.containsKey(configPrefix)) {
		if (configProps.containsKey(AWSConfigConstants.accessKeyId(configPrefix))
			&& configProps.containsKey(AWSConfigConstants.secretKey(configPrefix))) {
			// if the credential provider type is not specified, but the Access Key ID and Secret Key are given, it will default to BASIC
			credentialProviderType = CredentialProvider.BASIC;
		} else {
			// if the credential provider type is not specified, it will default to AUTO
			credentialProviderType = CredentialProvider.AUTO;
		}
	} else {
		credentialProviderType = CredentialProvider.valueOf(configProps.getProperty(configPrefix));
	}

	switch (credentialProviderType) {
		case ENV_VAR:
			return new EnvironmentVariableCredentialsProvider();

		case SYS_PROP:
			return new SystemPropertiesCredentialsProvider();

		case PROFILE:
			String profileName = configProps.getProperty(
					AWSConfigConstants.profileName(configPrefix), null);
			String profileConfigPath = configProps.getProperty(
					AWSConfigConstants.profilePath(configPrefix), null);
			return (profileConfigPath == null)
				? new ProfileCredentialsProvider(profileName)
				: new ProfileCredentialsProvider(profileConfigPath, profileName);

		case BASIC:
			return new AWSCredentialsProvider() {
				@Override
				public AWSCredentials getCredentials() {
					return new BasicAWSCredentials(
						configProps.getProperty(AWSConfigConstants.accessKeyId(configPrefix)),
						configProps.getProperty(AWSConfigConstants.secretKey(configPrefix)));
				}

				@Override
				public void refresh() {
					// do nothing
				}
			};

		case ASSUME_ROLE:
			final AWSSecurityTokenService baseCredentials = AWSSecurityTokenServiceClientBuilder.standard()
					.withCredentials(getCredentialsProvider(configProps, AWSConfigConstants.roleCredentialsProvider(configPrefix)))
					.withRegion(configProps.getProperty(AWSConfigConstants.AWS_REGION))
					.build();
			return new STSAssumeRoleSessionCredentialsProvider.Builder(
					configProps.getProperty(AWSConfigConstants.roleArn(configPrefix)),
					configProps.getProperty(AWSConfigConstants.roleSessionName(configPrefix)))
					.withExternalId(configProps.getProperty(AWSConfigConstants.externalId(configPrefix)))
					.withStsClient(baseCredentials)
					.build();

		default:
		case AUTO:
			return new DefaultAWSCredentialsProviderChain();
	}
}
 
Example #12
Source File: AWSEnviromentCredentialsConfigurator.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
public AWSEnviromentCredentialsConfigurator() {
    super(new EnvironmentVariableCredentialsProvider());
}
 
Example #13
Source File: CustomCredentialsProviderChain.java    From strongbox with Apache License 2.0 4 votes vote down vote up
public CustomCredentialsProviderChain(ClientConfiguration clientConfiguration, ProfileIdentifier profile, Supplier<MFAToken> mfaTokenSupplier) {
    super(new EnvironmentVariableCredentialsProvider(),
            new SystemPropertiesCredentialsProvider(),
            new ProfileCredentialProvider(clientConfiguration, profile, mfaTokenSupplier),
            new EC2ContainerCredentialsProviderWrapper());
}
 
Example #14
Source File: CustomCredentialsProviderChain.java    From aws-big-data-blog with Apache License 2.0 4 votes vote down vote up
public CustomCredentialsProviderChain() {
    super(new EnvironmentVariableCredentialsProvider(),
            new SystemPropertiesCredentialsProvider(),
            new ClasspathPropertiesFileCredentialsProvider(),
            new InstanceProfileCredentialsProvider());
}
 
Example #15
Source File: AWSUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * If the provider is ASSUME_ROLE, then the credentials for assuming this role are determined
 * recursively.
 *
 * @param configProps the configuration properties
 * @param configPrefix the prefix of the config properties for this credentials provider,
 *                     e.g. aws.credentials.provider for the base credentials provider,
 *                     aws.credentials.provider.role.provider for the credentials provider
 *                     for assuming a role, and so on.
 */
private static AWSCredentialsProvider getCredentialsProvider(final Properties configProps, final String configPrefix) {
	CredentialProvider credentialProviderType;
	if (!configProps.containsKey(configPrefix)) {
		if (configProps.containsKey(AWSConfigConstants.accessKeyId(configPrefix))
			&& configProps.containsKey(AWSConfigConstants.secretKey(configPrefix))) {
			// if the credential provider type is not specified, but the Access Key ID and Secret Key are given, it will default to BASIC
			credentialProviderType = CredentialProvider.BASIC;
		} else {
			// if the credential provider type is not specified, it will default to AUTO
			credentialProviderType = CredentialProvider.AUTO;
		}
	} else {
		credentialProviderType = CredentialProvider.valueOf(configProps.getProperty(configPrefix));
	}

	switch (credentialProviderType) {
		case ENV_VAR:
			return new EnvironmentVariableCredentialsProvider();

		case SYS_PROP:
			return new SystemPropertiesCredentialsProvider();

		case PROFILE:
			String profileName = configProps.getProperty(
					AWSConfigConstants.profileName(configPrefix), null);
			String profileConfigPath = configProps.getProperty(
					AWSConfigConstants.profilePath(configPrefix), null);
			return (profileConfigPath == null)
				? new ProfileCredentialsProvider(profileName)
				: new ProfileCredentialsProvider(profileConfigPath, profileName);

		case BASIC:
			return new AWSCredentialsProvider() {
				@Override
				public AWSCredentials getCredentials() {
					return new BasicAWSCredentials(
						configProps.getProperty(AWSConfigConstants.accessKeyId(configPrefix)),
						configProps.getProperty(AWSConfigConstants.secretKey(configPrefix)));
				}

				@Override
				public void refresh() {
					// do nothing
				}
			};

		case ASSUME_ROLE:
			final AWSSecurityTokenService baseCredentials = AWSSecurityTokenServiceClientBuilder.standard()
					.withCredentials(getCredentialsProvider(configProps, AWSConfigConstants.roleCredentialsProvider(configPrefix)))
					.withRegion(configProps.getProperty(AWSConfigConstants.AWS_REGION))
					.build();
			return new STSAssumeRoleSessionCredentialsProvider.Builder(
					configProps.getProperty(AWSConfigConstants.roleArn(configPrefix)),
					configProps.getProperty(AWSConfigConstants.roleSessionName(configPrefix)))
					.withExternalId(configProps.getProperty(AWSConfigConstants.externalId(configPrefix)))
					.withStsClient(baseCredentials)
					.build();

		case WEB_IDENTITY_TOKEN:
			return WebIdentityTokenCredentialsProvider.builder()
					.roleArn(configProps.getProperty(AWSConfigConstants.roleArn(configPrefix), null))
					.roleSessionName(configProps.getProperty(AWSConfigConstants.roleSessionName(configPrefix), null))
					.webIdentityTokenFile(configProps.getProperty(AWSConfigConstants.webIdentityTokenFile(configPrefix), null))
					.build();

		default:
		case AUTO:
			return new DefaultAWSCredentialsProviderChain();
	}
}
 
Example #16
Source File: CustomCredentialsProviderChain.java    From kinesis-log4j-appender with Apache License 2.0 4 votes vote down vote up
public CustomCredentialsProviderChain() {
  super(new ClasspathPropertiesFileCredentialsProvider(), new InstanceProfileCredentialsProvider(),
      new SystemPropertiesCredentialsProvider(), new EnvironmentVariableCredentialsProvider());
}