Search in sources :

Example 31 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project heron by twitter.

the class S3Uploader method initialize.

@Override
public void initialize(Config config) {
    bucket = S3Context.bucket(config);
    String accessKey = S3Context.accessKey(config);
    String accessSecret = S3Context.secretKey(config);
    String awsProfile = S3Context.awsProfile(config);
    String proxy = S3Context.proxyUri(config);
    String endpoint = S3Context.uri(config);
    String customRegion = S3Context.region(config);
    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
    if (Strings.isNullOrEmpty(bucket)) {
        throw new RuntimeException("Missing heron.uploader.s3.bucket config value");
    }
    // by not specifying a CredentialsProvider.
    if (!Strings.isNullOrEmpty(accessKey) || !Strings.isNullOrEmpty(accessSecret)) {
        if (!Strings.isNullOrEmpty(awsProfile)) {
            throw new RuntimeException("Please provide access_key/secret_key " + "or aws_profile, not both.");
        }
        if (Strings.isNullOrEmpty(accessKey)) {
            throw new RuntimeException("Missing heron.uploader.s3.access_key config value");
        }
        if (Strings.isNullOrEmpty(accessSecret)) {
            throw new RuntimeException("Missing heron.uploader.s3.secret_key config value");
        }
        builder.setCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, accessSecret)));
    } else if (!Strings.isNullOrEmpty(awsProfile)) {
        builder.setCredentials(new ProfileCredentialsProvider(awsProfile));
    }
    if (!Strings.isNullOrEmpty(proxy)) {
        URI proxyUri;
        try {
            proxyUri = new URI(proxy);
        } catch (URISyntaxException e) {
            throw new RuntimeException("Invalid heron.uploader.s3.proxy_uri config value: " + proxy, e);
        }
        ClientConfiguration clientCfg = new ClientConfiguration();
        clientCfg.withProtocol(Protocol.HTTPS).withProxyHost(proxyUri.getHost()).withProxyPort(proxyUri.getPort());
        if (!Strings.isNullOrEmpty(proxyUri.getUserInfo())) {
            String[] info = proxyUri.getUserInfo().split(":", 2);
            clientCfg.setProxyUsername(info[0]);
            if (info.length > 1) {
                clientCfg.setProxyPassword(info[1]);
            }
        }
        builder.setClientConfiguration(clientCfg);
    }
    s3Client = builder.withRegion(customRegion).withPathStyleAccessEnabled(true).withChunkedEncodingDisabled(true).withPayloadSigningEnabled(true).build();
    if (!Strings.isNullOrEmpty(endpoint)) {
        s3Client.setEndpoint(endpoint);
    }
    final String topologyName = Context.topologyName(config);
    final String topologyPackageLocation = Context.topologyPackageFile(config);
    pathPrefix = S3Context.pathPrefix(config);
    packageFileHandler = new File(topologyPackageLocation);
    // The path the packaged topology will be uploaded to
    remoteFilePath = generateS3Path(pathPrefix, topologyName, packageFileHandler.getName());
    // Generate the location of the backup file incase we need to revert the deploy
    previousVersionFilePath = generateS3Path(pathPrefix, topologyName, "previous_" + packageFileHandler.getName());
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 32 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project cas by apereo.

the class ChainingAWSCredentialsProvider method getInstance.

/**
 * Gets instance.
 *
 * @param credentialAccessKey      the credential access key
 * @param credentialSecretKey      the credential secret key
 * @param credentialPropertiesFile the credential properties file
 * @param profilePath              the profile path
 * @param profileName              the profile name
 * @return the instance
 */
public static AWSCredentialsProvider getInstance(final String credentialAccessKey, final String credentialSecretKey, final Resource credentialPropertiesFile, final String profilePath, final String profileName) {
    LOGGER.debug("Attempting to locate AWS credentials...");
    final List<AWSCredentialsProvider> chain = new ArrayList<>();
    chain.add(new InstanceProfileCredentialsProvider(false));
    if (credentialPropertiesFile != null) {
        try {
            final File f = credentialPropertiesFile.getFile();
            chain.add(new PropertiesFileCredentialsProvider(f.getCanonicalPath()));
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    if (StringUtils.isNotBlank(profilePath) && StringUtils.isNotBlank(profileName)) {
        chain.add(new ProfileCredentialsProvider(profilePath, profileName));
    }
    chain.add(new SystemPropertiesCredentialsProvider());
    chain.add(new EnvironmentVariableCredentialsProvider());
    chain.add(new ClasspathPropertiesFileCredentialsProvider("awscredentials.properties"));
    if (StringUtils.isNotBlank(credentialAccessKey) && StringUtils.isNotBlank(credentialSecretKey)) {
        final BasicAWSCredentials credentials = new BasicAWSCredentials(credentialAccessKey, credentialSecretKey);
        chain.add(new AWSStaticCredentialsProvider(credentials));
    }
    LOGGER.debug("AWS chained credential providers are configured as [{}]", chain);
    return new ChainingAWSCredentialsProvider(chain);
}
Also used : InstanceProfileCredentialsProvider(com.amazonaws.auth.InstanceProfileCredentialsProvider) EnvironmentVariableCredentialsProvider(com.amazonaws.auth.EnvironmentVariableCredentialsProvider) ArrayList(java.util.ArrayList) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClasspathPropertiesFileCredentialsProvider(com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider) PropertiesFileCredentialsProvider(com.amazonaws.auth.PropertiesFileCredentialsProvider) SystemPropertiesCredentialsProvider(com.amazonaws.auth.SystemPropertiesCredentialsProvider) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) InstanceProfileCredentialsProvider(com.amazonaws.auth.InstanceProfileCredentialsProvider) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) File(java.io.File) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) ClasspathPropertiesFileCredentialsProvider(com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider)

Example 33 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-xray-sdk-java by aws.

the class TracingHandlerTest method testS3PutObjectSubsegmentContainsBucketName.

@Test
public void testS3PutObjectSubsegmentContainsBucketName() {
    // Setup test
    AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    mockHttpClient(s3, null);
    final String BUCKET = "test-bucket", KEY = "test-key";
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    s3.putObject(BUCKET, KEY, "This is a test from java");
    Assert.assertEquals(1, segment.getSubsegments().size());
    Assert.assertEquals("PutObject", segment.getSubsegments().get(0).getAws().get("operation"));
    System.out.println(segment.getSubsegments().get(0).getAws());
    Assert.assertEquals(BUCKET, segment.getSubsegments().get(0).getAws().get("bucket_name"));
    Assert.assertEquals(KEY, segment.getSubsegments().get(0).getAws().get("key"));
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 34 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-xray-sdk-java by aws.

the class TracingHandlerTest method testLambdaInvokeSubsegmentContainsFunctionName.

@Test
public void testLambdaInvokeSubsegmentContainsFunctionName() {
    // Setup test
    AWSLambda lambda = AWSLambdaClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    // Lambda returns "null" on successful fn. with no return value
    mockHttpClient(lambda, "null");
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    InvokeRequest request = new InvokeRequest();
    request.setFunctionName("testFunctionName");
    InvokeResult r = lambda.invoke(request);
    System.out.println(r.getStatusCode());
    System.out.println(r);
    Assert.assertEquals(1, segment.getSubsegments().size());
    Assert.assertEquals("Invoke", segment.getSubsegments().get(0).getAws().get("operation"));
    System.out.println(segment.getSubsegments().get(0).getAws());
    Assert.assertEquals("testFunctionName", segment.getSubsegments().get(0).getAws().get("function_name"));
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) InvokeRequest(com.amazonaws.services.lambda.model.InvokeRequest) InvokeResult(com.amazonaws.services.lambda.model.InvokeResult) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 35 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project TOSCAna by StuPro-TOSCAna.

the class CapabilityMapper method mapOsCapabilityToImageId.

/**
 *     This method requests the AWS server for ImageIds with filters which are filled based on
 *     the values of the OsCapability. The image with the latest creation date is picked and its imageId returned.
 *
 *     @param osCapability The OsCapability to map.
 *     @return A String that contains a valid ImageId that can be added to the properties of an ec2.
 */
public String mapOsCapabilityToImageId(OsCapability osCapability) throws SdkClientException, ParseException, IllegalArgumentException {
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(awsRegion).build();
    // need to set these
    DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withFilters(new Filter("virtualization-type").withValues("hvm"), new Filter("root-device-type").withValues("ebs")).withOwners(// this is the ownerId of amazon itself
    "099720109477");
    if (osCapability.getType().isPresent() && osCapability.getType().get().equals(OsCapability.Type.WINDOWS)) {
        describeImagesRequest.withFilters(new Filter("platform").withValues("windows"));
    }
    if (osCapability.getDistribution().isPresent()) {
        if (osCapability.getDistribution().get().equals(OsCapability.Distribution.UBUNTU)) {
            // */ubuntu/images/* gets better results than plain *ubuntu*
            describeImagesRequest.withFilters(new Filter("name").withValues("*ubuntu/images/*"));
        } else {
            // just search for the string
            describeImagesRequest.withFilters(new Filter("name").withValues("*" + osCapability.getDistribution().toString() + "*"));
        }
    }
    if (osCapability.getVersion().isPresent()) {
        describeImagesRequest.withFilters(new Filter("name").withValues("*" + osCapability.getVersion().get() + "*"));
    }
    if (osCapability.getArchitecture().isPresent()) {
        if (osCapability.getArchitecture().get().equals(OsCapability.Architecture.x86_64)) {
            describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_64));
        } else if (osCapability.getArchitecture().get().equals(OsCapability.Architecture.x86_32)) {
            describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_32));
        } else {
            throw new UnsupportedOperationException("This architecture is not supported " + osCapability.getArchitecture());
        }
    } else {
        // defaulting to 64 bit architecture
        describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_64));
    }
    try {
        DescribeImagesResult describeImagesResult = ec2.describeImages(describeImagesRequest);
        String imageId = processResult(describeImagesResult);
        logger.debug("ImageId is: '{}'", imageId);
        return imageId;
    } catch (SdkClientException se) {
        logger.error("Cannot connect to AWS to request image Ids");
        throw se;
    } catch (ParseException pe) {
        logger.error("Error parsing date format of image creation dates");
        throw pe;
    } catch (IllegalArgumentException ie) {
        logger.error("With the filters created from the OsCapability there are no valid images received");
        throw ie;
    }
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) SdkClientException(com.amazonaws.SdkClientException) Filter(com.amazonaws.services.ec2.model.Filter) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) ParseException(java.text.ParseException)

Aggregations

AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)63 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)44 AWSCredentials (com.amazonaws.auth.AWSCredentials)15 Test (org.junit.Test)15 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)14 ClientConfiguration (com.amazonaws.ClientConfiguration)13 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)11 AmazonS3 (com.amazonaws.services.s3.AmazonS3)9 AmazonClientException (com.amazonaws.AmazonClientException)8 Regions (com.amazonaws.regions.Regions)8 SdkClientException (com.amazonaws.SdkClientException)7 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)7 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)6 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)6 File (java.io.File)6 AmazonServiceException (com.amazonaws.AmazonServiceException)5 EndpointConfiguration (com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration)5 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)3 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)3 AWSSecurityTokenService (com.amazonaws.services.securitytoken.AWSSecurityTokenService)3