software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider Java Examples

The following examples show how to use software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider. 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: KinesisVerticle.java    From reactive-refarch-cloudformation with Apache License 2.0 6 votes vote down vote up
private KinesisAsyncClient createClient() {

        ClientAsyncConfiguration clientConfiguration = ClientAsyncConfiguration.builder().build();

        // Reading credentials from ENV-variables
        AwsCredentialsProvider awsCredentialsProvider = DefaultCredentialsProvider.builder().build();

        // Configuring Kinesis-client with configuration
        String tmp = System.getenv("REGION");

        Region myRegion;
        if (tmp == null || tmp.trim().length() == 0) {
            myRegion = Region.US_EAST_1;
            LOGGER.info("Using default region");
        } else {
            myRegion = Region.of(tmp);
        }

        LOGGER.info("Deploying in Region " + myRegion.toString());

        return KinesisAsyncClient.builder()
                .asyncConfiguration(clientConfiguration)
                .credentialsProvider(awsCredentialsProvider)
                .region(myRegion)
                .build();
    }
 
Example #2
Source File: StreamScaler.java    From amazon-kinesis-scaling-utils with Apache License 2.0 6 votes vote down vote up
public StreamScaler(Region region) throws Exception {
	pctFormat.setMaximumFractionDigits(1);
	
	// use the default provider chain plus support for classpath
	// properties files
	KinesisClientBuilder builder = KinesisClient.builder()
			.credentialsProvider(DefaultCredentialsProvider.builder().build()).region(region);

	String kinesisEndpoint = System.getProperty("kinesisEndpoint");
	if (kinesisEndpoint != null) {
		builder.endpointOverride(new URI(kinesisEndpoint));
	}

	this.kinesisClient = builder.build();

	if (kinesisClient.serviceName() == null) {
		throw new Exception("Unable to reach Kinesis Service");
	}
}
 
Example #3
Source File: CredentialsAndRegionFactory.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * @param environment The {@link Environment}
 * @return An {@link AwsCredentialsProviderChain} that attempts to read the values from the Micronaut environment
 * first, then delegates to {@link DefaultCredentialsProvider}.
 */
@Bean(preDestroy = "close")
@Singleton
public AwsCredentialsProviderChain awsCredentialsProvider(Environment environment) {
    return AwsCredentialsProviderChain.of(
            EnvironmentAwsCredentialsProvider.create(environment),
            DefaultCredentialsProvider.create()
    );
}
 
Example #4
Source File: S3ClientConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public AwsCredentialsProvider awsCredentialsProvider(S3ClientConfigurarionProperties s3props) {

    if (StringUtils.isBlank(s3props.getAccessKeyId())) {
        // Return default provider
        return DefaultCredentialsProvider.create();
    } 
    else {
        // Return custom credentials provider
        return () -> {
            AwsCredentials creds = AwsBasicCredentials.create(s3props.getAccessKeyId(), s3props.getSecretAccessKey());
            return creds;
        };
    }
}
 
Example #5
Source File: MultiLangDaemonConfigurationTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
public MultiLangDaemonConfiguration baseConfiguration() {
    MultiLangDaemonConfiguration configuration = new MultiLangDaemonConfiguration(utilsBean, convertUtilsBean);
    configuration.setApplicationName("Test");
    configuration.setStreamName("Test");
    configuration.getKinesisCredentialsProvider().set("class", DefaultCredentialsProvider.class.getName());

    return configuration;
}
 
Example #6
Source File: DynamoDBLeaseCoordinatorIntegrationTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws ProvisionedThroughputException, DependencyException, InvalidStateException {
    final boolean useConsistentReads = true;
    if (leaseRefresher == null) {
        DynamoDbAsyncClient dynamoDBClient = DynamoDbAsyncClient.builder()
                .credentialsProvider(DefaultCredentialsProvider.create()).build();
        leaseRefresher = new DynamoDBLeaseRefresher(TABLE_NAME, dynamoDBClient, new DynamoDBLeaseSerializer(),
                useConsistentReads, TableCreatorCallback.NOOP_TABLE_CREATOR_CALLBACK);
    }
    leaseRefresher.createLeaseTableIfNotExists(10L, 10L);

    int retryLeft = ATTEMPTS;

    while (!leaseRefresher.leaseTableExists()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Sleep called.
        }
        retryLeft--;
        if (retryLeft == 0) {
            if (!leaseRefresher.leaseTableExists()) {
                fail("Failed to create table");
            }
        }
    }

    leaseRefresher.deleteAll();
    coordinator = new DynamoDBLeaseCoordinator(leaseRefresher, WORKER_ID, LEASE_DURATION_MILLIS,
            EPSILON_MILLIS, MAX_LEASES_FOR_WORKER, MAX_LEASES_TO_STEAL_AT_ONE_TIME, MAX_LEASE_RENEWER_THREAD_COUNT,
            INITIAL_LEASE_TABLE_READ_CAPACITY, INITIAL_LEASE_TABLE_WRITE_CAPACITY, metricsFactory);
    dynamoDBCheckpointer = new DynamoDBCheckpointer(coordinator, leaseRefresher);
    dynamoDBCheckpointer.operation(OPERATION);

    coordinator.start();
}
 
Example #7
Source File: StreamMonitor.java    From amazon-kinesis-scaling-utils with Apache License 2.0 5 votes vote down vote up
public StreamMonitor(AutoscalingConfiguration config) throws Exception {
	this.config = config;
	Region setRegion = Region.of(this.config.getRegion());

	// setup credential refresh
	this.credentials = DefaultCredentialsProvider.builder().asyncCredentialUpdateEnabled(true).build();

	// create scaler class and clients
	this.cloudWatchClient = CloudWatchClient.builder().credentialsProvider(this.credentials).region(setRegion).build();
	this.kinesisClient = KinesisClient.builder().credentialsProvider(this.credentials).region(setRegion).build();
	this.snsClient = SnsClient.builder().credentialsProvider(this.credentials).region(setRegion).build();
	
	this.scaler = new StreamScaler(this.kinesisClient);
}
 
Example #8
Source File: AwsModuleTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testAwsCredentialsProviderSerializationDeserialization() throws Exception {
  AwsCredentialsProvider credentialsProvider = DefaultCredentialsProvider.create();
  String serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  AwsCredentialsProvider deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, DefaultCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = EnvironmentVariableCredentialsProvider.create();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = SystemPropertyCredentialsProvider.create();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = ProfileCredentialsProvider.create();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = ContainerCredentialsProvider.builder().build();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());
}
 
Example #9
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(StaticCredentialsProvider.class.getSimpleName())) {
    return StaticCredentialsProvider.create(
        AwsBasicCredentials.create(asMap.get(ACCESS_KEY_ID), asMap.get(SECRET_ACCESS_KEY)));
  } else if (typeName.equals(DefaultCredentialsProvider.class.getSimpleName())) {
    return DefaultCredentialsProvider.create();
  } else if (typeName.equals(EnvironmentVariableCredentialsProvider.class.getSimpleName())) {
    return EnvironmentVariableCredentialsProvider.create();
  } else if (typeName.equals(SystemPropertyCredentialsProvider.class.getSimpleName())) {
    return SystemPropertyCredentialsProvider.create();
  } else if (typeName.equals(ProfileCredentialsProvider.class.getSimpleName())) {
    return ProfileCredentialsProvider.create();
  } else if (typeName.equals(ContainerCredentialsProvider.class.getSimpleName())) {
    return ContainerCredentialsProvider.builder().build();
  } else {
    throw new IOException(
        String.format("AWS credential provider type '%s' is not supported", typeName));
  }
}
 
Example #10
Source File: S3BundlePersistenceProvider.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private AwsCredentialsProvider getCredentialsProvider(final ProviderConfigurationContext configurationContext) {
    final String credentialsProviderValue = configurationContext.getProperties().get(CREDENTIALS_PROVIDER_PROP);
    if (StringUtils.isBlank(credentialsProviderValue)) {
        throw new ProviderCreationException("The property '" + CREDENTIALS_PROVIDER_PROP + "' must be provided");
    }

    CredentialProvider credentialProvider;
    try {
        credentialProvider = CredentialProvider.valueOf(credentialsProviderValue);
    } catch (Exception e) {
        throw new ProviderCreationException("The property '" + CREDENTIALS_PROVIDER_PROP + "' must be one of ["
                + CredentialProvider.STATIC + ", " + CredentialProvider.DEFAULT_CHAIN + " ]");
    }

    if (CredentialProvider.STATIC == credentialProvider) {
        final String accesKeyValue = configurationContext.getProperties().get(ACCESS_KEY_PROP);
        final String secretAccessKey = configurationContext.getProperties().get(SECRET_ACCESS_KEY_PROP);

        if (StringUtils.isBlank(accesKeyValue) || StringUtils.isBlank(secretAccessKey)) {
            throw new ProviderCreationException("The properties '" + ACCESS_KEY_PROP + "' and '" + SECRET_ACCESS_KEY_PROP
                    + "' must be provided when using " + CredentialProvider.STATIC + " credentials provider");
        }

        LOGGER.debug("Creating StaticCredentialsProvider");
        final AwsCredentials awsCredentials = AwsBasicCredentials.create(accesKeyValue, secretAccessKey);
        return StaticCredentialsProvider.create(awsCredentials);

    } else {
        LOGGER.debug("Creating DefaultCredentialsProvider");
        return DefaultCredentialsProvider.create();
    }
}
 
Example #11
Source File: AwsDefaultClientBuilder.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the credentials that should be used based on the customer's configuration.
 */
private AwsCredentialsProvider resolveCredentials(SdkClientConfiguration config) {
    return config.option(AwsClientOption.CREDENTIALS_PROVIDER) != null
           ? config.option(AwsClientOption.CREDENTIALS_PROVIDER)
           : DefaultCredentialsProvider.builder()
                                       .profileFile(config.option(SdkClientOption.PROFILE_FILE))
                                       .profileName(config.option(SdkClientOption.PROFILE_NAME))
                                       .build();
}
 
Example #12
Source File: SyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec create() {
    return MethodSpec.methodBuilder("create")
                     .returns(className)
                     .addModifiers(Modifier.STATIC, Modifier.PUBLIC)
                     .addJavadoc(
                             "Create a {@link $T} with the region loaded from the {@link $T} and credentials loaded from the "
                             + "{@link $T}.", className, DefaultAwsRegionProviderChain.class,
                             DefaultCredentialsProvider.class)
                     .addStatement("return builder().build()")
                     .build();
}
 
Example #13
Source File: AsyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec create() {
    return MethodSpec.methodBuilder("create")
                     .returns(className)
                     .addModifiers(Modifier.STATIC, Modifier.PUBLIC)
                     .addJavadoc("Create a {@link $T} with the region loaded from the {@link $T} and credentials loaded "
                                 + "from the {@link $T}.",
                                 className,
                                 DefaultAwsRegionProviderChain.class,
                                 DefaultCredentialsProvider.class)
                     .addStatement("return builder().build()")
                     .build();
}
 
Example #14
Source File: RedisUpdate.java    From reactive-refarch-cloudformation with Apache License 2.0 5 votes vote down vote up
private static KinesisAsyncClient createClient() {

        ClientAsyncConfiguration clientConfiguration = ClientAsyncConfiguration.builder().build();

        // Reading credentials from ENV-variables
        AwsCredentialsProvider awsCredentialsProvider = DefaultCredentialsProvider.builder().build();

        // Configuring Kinesis-client with configuration
        String tmp = System.getenv("REGION");

        Region myRegion = null;
        if (tmp == null || tmp.trim().length() == 0) {
            myRegion = Region.US_EAST_1;
            System.out.println("Using default region");
        } else {
            myRegion = Region.of(tmp);
        }

        System.out.println("Deploying in Region " + myRegion.toString());

        KinesisAsyncClient kinesisClient = KinesisAsyncClient.builder()
                .asyncConfiguration(clientConfiguration)
                .credentialsProvider(awsCredentialsProvider)
                .region(myRegion)
                .build();

        return kinesisClient;
    }
 
Example #15
Source File: TranscribeStreamingDemoApp.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
private static AwsCredentialsProvider getCredentials() {
    return DefaultCredentialsProvider.create();
}
 
Example #16
Source File: AwsOptions.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public AwsCredentialsProvider create(PipelineOptions options) {
  return DefaultCredentialsProvider.create();
}
 
Example #17
Source File: AwsDefaultProviderChainPlugin.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public software.amazon.awssdk.auth.credentials.AwsCredentialsProvider getV2CredentialsProvider() {
    return DefaultCredentialsProvider.create();
}
 
Example #18
Source File: TranscribeStreamingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private static AwsCredentialsProvider getCredentials() {
    return DefaultCredentialsProvider.create();
}
 
Example #19
Source File: S3RandomAccessFile.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private S3RandomAccessFile(String url) throws IOException {
  super(url, s3BufferSize, s3MaxReadCacheSize);

  // Region is tricky. Since we are using AWS SDK to manage connections to all object stores, we might have users
  // who use netCDF-Java and never touch AWS. If that's they case, they likely have not setup a basic credentials or
  // configuration file, and thus lack a default region. What we will do here is check to see if there is one set.
  // If, by the time we make the client, profileRegion isn't set, we will default to the AWS_GLOBAL region, which is
  // like a no-op region when it comes to S3. This will allow requests to non-AWS-S3 object stores to work, because
  // a region must be set, even if it's useless.
  Optional<Region> profileRegion = ProfileFile.defaultProfileFile().profile("default")
      .map(p -> p.properties().get(ProfileProperty.REGION)).map(Region::of);

  try {
    uri = new CdmS3Uri(url);
  } catch (URISyntaxException urie) {
    // If we are given a string that is not a valid CdmS3Uri
    // throw an IOException
    throw new IOException(urie.getCause());
  }

  Builder httpConfig = ApacheHttpClient.builder().maxConnections(maxConnections)
      .connectionTimeout(Duration.ofMillis(connectionTimeout)).socketTimeout(Duration.ofMillis(socketTimeout));

  S3ClientBuilder s3ClientBuilder = S3Client.builder().httpClientBuilder(httpConfig);

  // if we are accessing an S3 compatible service, we need to override the server endpoint
  uri.getEndpoint().ifPresent(s3ClientBuilder::endpointOverride);

  // build up a chain of credentials providers
  AwsCredentialsProviderChain.Builder cdmCredentialsProviderChainBuilder = AwsCredentialsProviderChain.builder();

  // if uri has a profile name, we need setup a credentials provider to look for potential credentials, and see if a
  // region has been set
  if (uri.getProfile().isPresent()) {
    // get the profile name
    String profileName = uri.getProfile().get();

    ProfileCredentialsProvider namedProfileCredentials =
        ProfileCredentialsProvider.builder().profileName(profileName).build();

    // add it to the chain that it is the first thing checked for credentials
    cdmCredentialsProviderChainBuilder.addCredentialsProvider(namedProfileCredentials);

    // Read the region associated with the profile, if set
    // Note: the java sdk does not do this by default
    Optional<Region> namedProfileRegion = ProfileFile.defaultProfileFile().profile(profileName)
        .map(p -> p.properties().get(ProfileProperty.REGION)).map(Region::of);
    // if the named profile has a region, update profileRegion to use it.
    if (namedProfileRegion.isPresent()) {
      profileRegion = namedProfileRegion;
    }
  }

  // Add the Default Credentials Provider Chain:
  // https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/credentials.html
  cdmCredentialsProviderChainBuilder.addCredentialsProvider(DefaultCredentialsProvider.create());

  // Add the AnonymousCredentialsProvider last
  cdmCredentialsProviderChainBuilder.addCredentialsProvider(AnonymousCredentialsProvider.create());

  // build the credentials provider that we'll use
  AwsCredentialsProviderChain cdmCredentialsProviderChain = cdmCredentialsProviderChainBuilder.build();

  // Add the credentials provider to the client builder
  s3ClientBuilder.credentialsProvider(cdmCredentialsProviderChain);

  // Set the region for the client builder (default to AWS_GLOBAL)
  s3ClientBuilder.region(profileRegion.orElse(Region.AWS_GLOBAL));

  // Build the client
  client = s3ClientBuilder.build();

  // request HEAD for the object
  HeadObjectRequest headdObjectRequest =
      HeadObjectRequest.builder().bucket(uri.getBucket()).key(uri.getKey()).build();

  objectHeadResponse = client.headObject(headdObjectRequest);
}