Java Code Examples for software.amazon.awssdk.auth.credentials.StaticCredentialsProvider#create()

The following examples show how to use software.amazon.awssdk.auth.credentials.StaticCredentialsProvider#create() . 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: S3Manager.java    From joyqueue with Apache License 2.0 9 votes vote down vote up
private String getS3Url(String objectKey) {
    AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey));
    S3Presigner preSigner = S3Presigner.builder()
            .credentialsProvider(credentialsProvider)
            .endpointOverride(URI.create(endpoint))
            .region(clientRegion).build();
    GetObjectRequest getObjectRequest = GetObjectRequest.builder()
            .bucket(bucketName)
            .key(objectKey)
            .build();
    GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
            .getObjectRequest(getObjectRequest).signatureDuration(Duration.ofDays(7)).build();
    PresignedGetObjectRequest presignedGetObjectRequest = preSigner.presignGetObject(getObjectPresignRequest);
    String url = presignedGetObjectRequest.url().toString();
    preSigner.close();
    return url;
}
 
Example 2
Source File: ChecksumResetsOnRetryTest.java    From aws-sdk-java-v2 with Apache License 2.0 7 votes vote down vote up
@Before
public void setup() {
    StaticCredentialsProvider credentials = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
    s3Client = S3Client.builder()
                       .credentialsProvider(credentials)
                       .region(Region.US_WEST_2)
                       .endpointOverride(URI.create("http://localhost:" + mockServer.port()))
                       .build();

    s3AsyncClient = S3AsyncClient.builder()
                                 .credentialsProvider(credentials)
                                 .region(Region.US_WEST_2)
                                 .endpointOverride(URI.create("http://localhost:" + mockServer.port()))
                                 .build();

    body = "foo".getBytes(StandardCharsets.UTF_8);
    String checksumAsHexString = "acbd18db4cc2f85cedef654fccc4a4d8";
    bodyEtag = "\"" + checksumAsHexString + "\"";
    bodyWithTrailingChecksum = ArrayUtils.addAll(body, BinaryUtils.fromHex(checksumAsHexString));
}
 
Example 3
Source File: S3FileSystem.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
protected AwsCredentialsProvider getAsync2Provider(Configuration config) {
  switch(config.get(Constants.AWS_CREDENTIALS_PROVIDER)) {
    case ACCESS_KEY_PROVIDER:
      return StaticCredentialsProvider.create(AwsBasicCredentials.create(
        config.get(Constants.ACCESS_KEY), config.get(Constants.SECRET_KEY)));
    case EC2_METADATA_PROVIDER:
      return InstanceProfileCredentialsProvider.create();
    case NONE_PROVIDER:
      return AnonymousCredentialsProvider.create();
    case ASSUME_ROLE_PROVIDER:
      return new STSCredentialProviderV2(config);
    default:
      throw new IllegalStateException(config.get(Constants.AWS_CREDENTIALS_PROVIDER));
  }
}
 
Example 4
Source File: AmazonWebServicesClientProxy.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
public AmazonWebServicesClientProxy(final boolean inHandshakeMode,
                                    final LoggerProxy loggerProxy,
                                    final Credentials credentials,
                                    final Supplier<Long> remainingTimeToExecute,
                                    final DelayFactory override) {
    this.inHandshakeMode = inHandshakeMode;
    this.loggerProxy = loggerProxy;
    this.remainingTimeInMillis = remainingTimeToExecute;

    BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(credentials.getAccessKeyId(),
                                                                                  credentials.getSecretAccessKey(),
                                                                                  credentials.getSessionToken());
    this.v1CredentialsProvider = new AWSStaticCredentialsProvider(basicSessionCredentials);

    AwsSessionCredentials awsSessionCredentials = AwsSessionCredentials.create(credentials.getAccessKeyId(),
        credentials.getSecretAccessKey(), credentials.getSessionToken());
    this.v2CredentialsProvider = StaticCredentialsProvider.create(awsSessionCredentials);
    this.override = Objects.requireNonNull(override);
}
 
Example 5
Source File: STSCredentialProviderV2.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public STSCredentialProviderV2(Configuration conf) {
  AwsCredentialsProvider awsCredentialsProvider = null;

  if (S3StoragePlugin.ACCESS_KEY_PROVIDER.equals(conf.get(Constants.ASSUMED_ROLE_CREDENTIALS_PROVIDER))) {
    awsCredentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(
      conf.get(Constants.ACCESS_KEY), conf.get(Constants.SECRET_KEY)));
  } else if (S3StoragePlugin.EC2_METADATA_PROVIDER.equals(conf.get(Constants.ASSUMED_ROLE_CREDENTIALS_PROVIDER))) {
    awsCredentialsProvider = InstanceProfileCredentialsProvider.create();
  }

  final StsClientBuilder builder = StsClient.builder()
    .credentialsProvider(awsCredentialsProvider)
    .region(S3FileSystem.getAWSRegionFromConfigurationOrDefault(conf))
    .httpClientBuilder(initConnectionSettings(conf));
  S3FileSystem.getStsEndpoint(conf).ifPresent(e -> {
    try {
      builder.endpointOverride(new URI(e));
    } catch (URISyntaxException use) {
      throw UserException.sourceInBadState(use).buildSilently();
    }
  });

  initUserAgent(builder, conf);

  final AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder()
    .roleArn(conf.get(Constants.ASSUMED_ROLE_ARN))
    .roleSessionName(UUID.randomUUID().toString())
    .build();

  this.stsAssumeRoleCredentialsProvider = StsAssumeRoleCredentialsProvider.builder()
    .refreshRequest(assumeRoleRequest)
    .stsClient(builder.build())
    .build();
}
 
Example 6
Source File: ProfileCredentialsUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Load a basic set of credentials that have been configured in this profile.
 */
private AwsCredentialsProvider basicProfileCredentialsProvider() {
    requireProperties(ProfileProperty.AWS_ACCESS_KEY_ID,
                      ProfileProperty.AWS_SECRET_ACCESS_KEY);
    AwsCredentials credentials = AwsBasicCredentials.create(properties.get(ProfileProperty.AWS_ACCESS_KEY_ID),
                                                                 properties.get(ProfileProperty.AWS_SECRET_ACCESS_KEY));
    return StaticCredentialsProvider.create(credentials);
}
 
Example 7
Source File: InspectorErrorUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    StaticCredentialsProvider credsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
    inspector = InspectorClient.builder()
                               .credentialsProvider(credsProvider)
                               .region(Region.US_EAST_1)
                               .endpointOverride(URI.create("http://localhost:" + wireMock.port()))
                               .build();
}
 
Example 8
Source File: SqsResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    DockerClientFactory.instance().client();
    String queueUrl;
    try {
        services = new LocalStackContainer("0.11.1").withServices(Service.SQS);
        services.start();
        StaticCredentialsProvider staticCredentials = StaticCredentialsProvider
            .create(AwsBasicCredentials.create("accesskey", "secretKey"));

        client = SqsClient.builder()
            .endpointOverride(new URI(endpoint()))
            .credentialsProvider(staticCredentials)
            .httpClientBuilder(UrlConnectionHttpClient.builder())
            .region(Region.US_EAST_1).build();

        queueUrl = client.createQueue(q -> q.queueName(QUEUE_NAME)).queueUrl();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Could not start localstack server", e);
    }

    Map<String, String> properties = new HashMap<>();
    properties.put("quarkus.sqs.endpoint-override", endpoint());
    properties.put("quarkus.sqs.aws.region", "us-east-1");
    properties.put("quarkus.sqs.aws.credentials.type", "static");
    properties.put("quarkus.sqs.aws.credentials.static-provider.access-key-id", "accessKey");
    properties.put("quarkus.sqs.aws.credentials.static-provider.secret-access-key", "secretKey");
    properties.put("queue.url", queueUrl);

    return properties;
}
 
Example 9
Source File: SesResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    DockerClientFactory.instance().client();
    try {
        services = new LocalStackContainer("0.11.1").withServices(Service.SES);
        services.start();
        StaticCredentialsProvider staticCredentials = StaticCredentialsProvider
            .create(AwsBasicCredentials.create("accesskey", "secretKey"));

        client = SesClient.builder()
            .endpointOverride(new URI(endpoint()))
            .credentialsProvider(staticCredentials)
            .httpClientBuilder(UrlConnectionHttpClient.builder())
            .region(Region.US_EAST_1).build();

        client.verifyEmailIdentity(req -> req.emailAddress(FROM_EMAIL));
        client.verifyEmailIdentity(req -> req.emailAddress(TO_EMAIL));

    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Could not start localstack server", e);
    }

    Map<String, String> properties = new HashMap<>();
    properties.put("quarkus.ses.endpoint-override", endpoint());
    properties.put("quarkus.ses.aws.region", "us-east-1");
    properties.put("quarkus.ses.aws.credentials.type", "static");
    properties.put("quarkus.ses.aws.credentials.static-provider.access-key-id", "accessKey");
    properties.put("quarkus.ses.aws.credentials.static-provider.secret-access-key", "secretKey");

    return properties;
}
 
Example 10
Source File: SnsResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    DockerClientFactory.instance().client();
    String topicArn;
    try {
        services = new LocalStackContainer("0.11.1").withServices(Service.SNS);
        services.start();
        StaticCredentialsProvider staticCredentials = StaticCredentialsProvider
            .create(AwsBasicCredentials.create("accesskey", "secretKey"));

        client = SnsClient.builder()
            .endpointOverride(new URI(endpoint()))
            .credentialsProvider(staticCredentials)
            .httpClientBuilder(UrlConnectionHttpClient.builder())
            .region(Region.US_EAST_1).build();

        topicArn = client.createTopic(t -> t.name(TOPIC_NAME)).topicArn();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Could not start ocalstack server", e);
    }

    Map<String, String> properties = new HashMap<>();
    properties.put("quarkus.sns.endpoint-override", endpoint());
    properties.put("quarkus.sns.aws.region", "us-east-1");
    properties.put("quarkus.sns.aws.credentials.type", "static");
    properties.put("quarkus.sns.aws.credentials.static-provider.access-key-id", "accessKey");
    properties.put("quarkus.sns.aws.credentials.static-provider.secret-access-key", "secretKey");
    properties.put("topic.arn", topicArn);

    return properties;
}
 
Example 11
Source File: KmsResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    DockerClientFactory.instance().client();
    String masterKeyId;
    try {
        services = new KmsContainer();
        services.start();
        StaticCredentialsProvider staticCredentials = StaticCredentialsProvider
            .create(AwsBasicCredentials.create("accesskey", "secretKey"));

        client = KmsClient.builder()
            .endpointOverride(new URI(endpoint()))
            .credentialsProvider(staticCredentials)
            .httpClientBuilder(UrlConnectionHttpClient.builder())
            .region(Region.US_EAST_1).build();

        masterKeyId = client.createKey().keyMetadata().keyId();
        client.generateDataKey(r -> r.keyId(masterKeyId).keySpec(DataKeySpec.AES_256));

    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Could not start localstack server", e);
    }

    Map<String, String> properties = new HashMap<>();
    properties.put("quarkus.kms.endpoint-override", endpoint());
    properties.put("quarkus.kms.aws.region", "us-east-1");
    properties.put("quarkus.kms.aws.credentials.type", "static");
    properties.put("quarkus.kms.aws.credentials.static-provider.access-key-id", "accessKey");
    properties.put("quarkus.kms.aws.credentials.static-provider.secret-access-key", "secretKey");
    properties.put("key.arn", masterKeyId);

    return properties;
}
 
Example 12
Source File: S3Manager.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public String upload(String path) {
    try {
        AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey));
        S3Configuration configuration = S3Configuration.builder()
                .chunkedEncodingEnabled(true)
                .pathStyleAccessEnabled(true)
                .build();
        S3Client client = S3Client.builder()
                .region(clientRegion)
                .credentialsProvider(credentialsProvider)
                .endpointOverride(URI.create(endpoint))
                .serviceConfiguration(configuration)
                .build();
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .contentType("application/octet-stream")
                .bucket(bucketName)
                .key(path)
                .build();
        RequestBody requestBody = RequestBody.fromFile(Paths.get(path));
        client.putObject(putObjectRequest, requestBody);
        logger.info("S3: upload file success");
        String url = getS3Url(path);
        logger.info("url:{}", url);
        return url;
    } catch (Exception e) {
        logger.error("S3: failed to upload file :{}, error: {}", path, e.getMessage());
    }
    return null;
}
 
Example 13
Source File: AmazonWebServicesProvider.java    From cloudformation-cli-java-plugin with Apache License 2.0 4 votes vote down vote up
protected AwsCredentialsProvider getCredentialsProvider() {
    return StaticCredentialsProvider.create(this.credentialsProvider.get());
}
 
Example 14
Source File: DefaultPollyPresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Before
public void methodSetup() {
    credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
}
 
Example 15
Source File: S3UtilitiesTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private static AwsCredentialsProvider dummyCreds() {
    return StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
}
 
Example 16
Source File: EndpointTraitTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private StaticCredentialsProvider mockCredentials() {
    return StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
}
 
Example 17
Source File: AwsIntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * @return AwsCredentialsProvider to use during tests. Setup by base fixture
 */
protected static AwsCredentialsProvider getCredentialsProvider() {
    return StaticCredentialsProvider.create(CREDENTIALS);
}
 
Example 18
Source File: ClientReflector.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * @return Dummy credentials to create client with.
 */
private StaticCredentialsProvider getMockCredentials() {
    return StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
}
 
Example 19
Source File: KinesisTestConfiguration.java    From synapse with Apache License 2.0 4 votes vote down vote up
@Bean
public AwsCredentialsProvider awsCredentialsProvider() {
    LOG.info("Configuring StaticCredentialsProvider for local tests");
    return StaticCredentialsProvider.create(AwsBasicCredentials.create("foobar", "foobar"));
}
 
Example 20
Source File: KinesisTestConfiguration.java    From synapse with Apache License 2.0 4 votes vote down vote up
@Bean
public AwsCredentialsProvider awsCredentialsProvider() {
    LOG.info("Configuring StaticCredentialsProvider for local tests");
    return StaticCredentialsProvider.create(AwsBasicCredentials.create("foobar", "foobar"));
}