Example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

List of usage examples for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

Introduction

In this page you can find the example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials.

Prototype

public BasicAWSCredentials(String accessKey, String secretKey) 

Source Link

Document

Constructs a new BasicAWSCredentials object, with the specified AWS access key and AWS secret key.

Usage

From source file:com.tvarit.plugin.InfrastructureMojo.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().debug("Starting " + this.getClass().getSimpleName() + " execution ");
    getLog().warn(/*from w  w  w  .j a va2 s  . com*/
            "This goal has been deprecated and may be removed without notice. Please use the goal 'make-infrastructure' instead.");
    final BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonCloudFormationClient amazonCloudFormationClient = new AmazonCloudFormationClient(awsCredentials);
    final com.amazonaws.services.cloudformation.model.Parameter domainNameParameter = new com.amazonaws.services.cloudformation.model.Parameter()
            .withParameterKey("domainName").withParameterValue(this.domainName);
    final com.amazonaws.services.cloudformation.model.Parameter projectNameParameter = new com.amazonaws.services.cloudformation.model.Parameter()
            .withParameterKey("projectName").withParameterValue(this.projectName);
    final com.amazonaws.services.cloudformation.model.Parameter bucketNameParameter = new com.amazonaws.services.cloudformation.model.Parameter()
            .withParameterKey("bucketName").withParameterValue("tvarit-" + this.bucketName);
    final CreateStackRequest createStackRequest = new CreateStackRequest()
            .withCapabilities(Capability.CAPABILITY_IAM).withStackName(projectName + "-infra")
            .withParameters(domainNameParameter, projectNameParameter, bucketNameParameter);
    if (templateUrl == null) {
        final String template = new TemplateReader().readTemplate("/cfn-templates/vpc-infra.template");
        createStackRequest.withTemplateBody(template);
    } else {
        createStackRequest.withTemplateURL(templateUrl);
    }
    new StackMaker().makeStack(createStackRequest, amazonCloudFormationClient, getLog());

    getLog().info("Finished completing stack");

}

From source file:com.tvarit.plugin.NewInfrastructureMojo.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().debug("Starting " + this.getClass().getSimpleName() + " execution ");
    final BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonCloudFormationClient amazonCloudFormationClient = new AmazonCloudFormationClient(awsCredentials);
    final com.amazonaws.services.cloudformation.model.Parameter domainNameParameter = new com.amazonaws.services.cloudformation.model.Parameter()
            .withParameterKey("domainName").withParameterValue(this.domainName);
    final com.amazonaws.services.cloudformation.model.Parameter projectNameParameter = new com.amazonaws.services.cloudformation.model.Parameter()
            .withParameterKey("projectName").withParameterValue(this.projectName);
    final com.amazonaws.services.cloudformation.model.Parameter bucketNameParameter = new com.amazonaws.services.cloudformation.model.Parameter()
            .withParameterKey("bucketName").withParameterValue(this.bucketName);
    final String template;
    final MavenProject project = (MavenProject) this.getPluginContext().getOrDefault("project", null);

    if (templateUrl == null) {
        try {//from w w w .j  a  va 2 s . c om
            templateUrl = new TemplateUrlMaker().makeUrl(project, "vpc-infra.template").toString();
        } catch (MalformedURLException e) {
            throw new MojoExecutionException(
                    "Could not create default url for templates. Please open an issue on github.", e);
        }
    }
    final CreateStackRequest createStackRequest = new CreateStackRequest()
            .withCapabilities(Capability.CAPABILITY_IAM).withStackName(projectName + "-infra")
            .withParameters(domainNameParameter, projectNameParameter, bucketNameParameter)
            .withTemplateURL(templateUrl);
    new StackMaker().makeStack(createStackRequest, amazonCloudFormationClient, getLog());
    getLog().info("Finished completing stack");

}

From source file:com.tvarit.plugin.TvaritTomcatDeployerMojo.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

    final MavenProject project = (MavenProject) this.getPluginContext().getOrDefault("project", null);
    if (templateUrl == null)
        try {//  w  w w .ja va 2s .c  om
            templateUrl = new TemplateUrlMaker().makeUrl(project, "newinstance.template").toString();
        } catch (MalformedURLException e) {
            throw new MojoExecutionException(
                    "Could not create default url for templates. Please open an issue on github.", e);
        }

    final BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonS3Client s3Client = new AmazonS3Client(awsCredentials);
    final File warFile = project.getArtifact().getFile();
    final String key = "deployables/" + project.getGroupId() + "/" + project.getArtifactId() + "/"
            + project.getVersion() + "/" + warFile.getName();
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, warFile);
    final ObjectMetadata metadata = new ObjectMetadata();
    final HashMap<String, String> userMetadata = new HashMap<>();
    userMetadata.put("project_name", projectName);
    userMetadata.put("stack_template_url", templateUrl);
    userMetadata.put("private_key_name", sshKeyName);
    metadata.setUserMetadata(userMetadata);
    putObjectRequest.withMetadata(metadata);
    final PutObjectResult putObjectResult = s3Client.putObject(putObjectRequest);

    /*
            AmazonCloudFormationClient amazonCloudFormationClient = new AmazonCloudFormationClient(awsCredentials);
            final com.amazonaws.services.cloudformation.model.Parameter projectNameParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("projectName").withParameterValue(this.projectName);
            final com.amazonaws.services.cloudformation.model.Parameter publicSubnetsParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("publicSubnets").withParameterValue(commaSeparatedSubnetIds);
            final com.amazonaws.services.cloudformation.model.Parameter tvaritRoleParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("tvaritRole").withParameterValue(tvaritRole);
            final com.amazonaws.services.cloudformation.model.Parameter tvaritInstanceProfileParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("tvaritInstanceProfile").withParameterValue(this.tvaritInstanceProfile);
            final com.amazonaws.services.cloudformation.model.Parameter tvaritBucketNameParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("bucketName").withParameterValue(this.bucketName);
            final com.amazonaws.services.cloudformation.model.Parameter instanceSecurityGroupIdParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("sgId").withParameterValue(this.instanceSecurityGroupId);
            final com.amazonaws.services.cloudformation.model.Parameter sshKeyNameParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("keyName").withParameterValue(this.sshKeyName);
            final String warFileUrl = s3Client.getUrl(bucketName, key).toString();
            final com.amazonaws.services.cloudformation.model.Parameter warFileUrlParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("warFileUrl").withParameterValue(warFileUrl);
            final CreateStackRequest createStackRequest = new CreateStackRequest();
            if (templateUrl == null) {
    try {
        templateUrl = new TemplateUrlMaker().makeUrl(project, "newinstance.template").toString();
    } catch (MalformedURLException e) {
        throw new MojoExecutionException("Could not create default url for templates. Please open an issue on github.", e);
    }
            }
            createStackRequest.
        withStackName(projectName + "-instance-" + project.getVersion().replace(".", "-")).
        withParameters(
                projectNameParameter,
                publicSubnetsParameter,
                tvaritInstanceProfileParameter,
                tvaritRoleParameter,
                tvaritBucketNameParameter,
                instanceSecurityGroupIdParameter,
                warFileUrlParameter,
                sshKeyNameParameter
        ).
        withDisableRollback(true).
        withTemplateURL(templateUrl);
            createStackRequest.withDisableRollback(true);
            final Stack stack = new StackMaker().makeStack(createStackRequest, amazonCloudFormationClient, getLog());
            AmazonAutoScalingClient amazonAutoScalingClient = new AmazonAutoScalingClient(awsCredentials);
            final AttachInstancesRequest attachInstancesRequest = new AttachInstancesRequest();
            attachInstancesRequest.withInstanceIds(stack.getOutputs().get(0).getOutputValue(), stack.getOutputs().get(1).getOutputValue()).withAutoScalingGroupName(autoScalingGroupName);
            amazonAutoScalingClient.attachInstances(attachInstancesRequest);
    */

}

From source file:com.twitter.heron.uploader.s3.S3Uploader.java

License:Open Source License

@Override
public void initialize(Config config) {
    bucket = S3Context.bucket(config);
    String accessKey = S3Context.accessKey(config);
    String accessSecret = S3Context.secretKey(config);

    if (bucket == null || bucket.isEmpty()) {
        throw new RuntimeException("Missing heron.uploader.s3.bucket config value");
    }//  w ww .  ja v a2s. c  o  m

    if (accessKey == null || accessKey.isEmpty()) {
        throw new RuntimeException("Missing heron.uploader.s3.access_key config value");
    }

    if (accessSecret == null || accessSecret.isEmpty()) {
        throw new RuntimeException("Missing heron.uploader.s3.secret_key config value");
    }

    AWSCredentials credentials = new BasicAWSCredentials(accessKey, accessSecret);
    s3Client = new AmazonS3Client(credentials);

    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());
}

From source file:com.twitter.services.SQS.java

License:Open Source License

private static void createSQS() {
    AWSCredentials credentials = null;/*from  w  w w .j a v  a2  s.c  o m*/
    try {
        credentials = new BasicAWSCredentials("", "");
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);
}

From source file:com.upplication.s3fs.S3FileSystemProvider.java

License:Open Source License

protected S3FileSystem createFileSystem0(URI uri, Object accessKey, Object secretKey, Object sessionToken) {
    AmazonS3Client client;// w w w  . j a  v  a 2 s. co  m
    ClientConfiguration config = createClientConfig(props);

    if (accessKey == null && secretKey == null) {
        client = new AmazonS3Client(new com.amazonaws.services.s3.AmazonS3Client(config));
    } else {

        AWSCredentials credentials = (sessionToken == null
                ? new BasicAWSCredentials(accessKey.toString(), secretKey.toString())
                : new BasicSessionCredentials(accessKey.toString(), secretKey.toString(),
                        sessionToken.toString()));
        client = new AmazonS3Client(new com.amazonaws.services.s3.AmazonS3Client(credentials, config));
    }

    // note: path style access is going to be deprecated
    // https://aws.amazon.com/blogs/aws/amazon-s3-path-deprecation-plan-the-rest-of-the-story/
    boolean usePathStyle = "true".equals(props.getProperty("s_3_path_style_access"))
            || "true".equals(props.getProperty("s3_path_style_access"));
    if (usePathStyle) {
        S3ClientOptions options = S3ClientOptions.builder().setPathStyleAccess(usePathStyle).build();
        client.client.setS3ClientOptions(options);
    }

    if (uri.getHost() != null) {
        client.setEndpoint(uri.getHost());
    } else if (props.getProperty("endpoint") != null) {
        client.setEndpoint(props.getProperty("endpoint"));
    } else if (props.getProperty("region") != null) {
        client.setRegion(props.getProperty("region"));
    }

    S3FileSystem result = new S3FileSystem(this, client, uri.getHost());
    return result;
}

From source file:com.urbancode.terraform.credentials.aws.CredentialsAWS.java

License:Apache License

private AWSCredentials createBasicAWSCredentials() throws NullPointerException {
    AWSCredentials result;/*w w w . java 2 s.  c om*/

    if (accessKey == null || "".equals(accessKey)) {
        log.error("access key for Credentials " + getName() + " is empty!");
        throw new NullPointerException("Empty Access Key");
    }

    if (secretKey == null || "".equals(secretKey)) {
        log.error("secret key for Credentials " + getName() + " is empty!");
        throw new NullPointerException("Empty Secret Key");
    }

    result = new BasicAWSCredentials(accessKey, secretKey);
    return result;
}

From source file:com.vanilla.transmilenio.servicio.AmazonServicio.java

public AmazonServicio() {
    AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
    s3client = new AmazonS3Client(credentials);
}

From source file:com.vmware.photon.controller.model.adapters.awsadapter.AWSEndpointAdapterService.java

License:Open Source License

/**
 * Method gets the aws accountId from the specified credentials.
 *
 * @param privateKeyId/*  w  ww.  j a  va 2 s . c  o m*/
 * @param privateKey
 * @return account ID
 */
private String getAccountId(String privateKeyId, String privateKey) {
    AWSCredentials awsCredentials = new BasicAWSCredentials(privateKeyId, privateKey);
    AmazonIdentityManagementClient iamClient = new AmazonIdentityManagementClient(awsCredentials);
    String userId = null;
    try {
        if ((iamClient.getUser() != null) && (iamClient.getUser().getUser() != null)
                && (iamClient.getUser().getUser().getArn() != null)) {

            String arn = iamClient.getUser().getUser().getArn();
            /*
             * arn:aws:service:region:account:resource -> so limiting the split to 6 words and
             * extracting the accountId which is 5th one in list. If the user is not authorized
             * to perform iam:GetUser on that resource,still error mesage will have accountId
             */
            userId = arn.split(":", 6)[4];
        }
    } catch (AmazonServiceException ex) {
        if (ex.getErrorCode().compareTo("AccessDenied") == 0) {
            String msg = ex.getMessage();
            userId = msg.split(":", 7)[5];
        }
    }
    return userId;
}

From source file:com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.java

License:Open Source License

public static AmazonEC2AsyncClient getAsyncClient(AuthCredentialsServiceState credentials, String region,
        ExecutorService executorService) {
    AmazonEC2AsyncClient ec2AsyncClient = new AmazonEC2AsyncClient(
            new BasicAWSCredentials(credentials.privateKeyId, credentials.privateKey), executorService);

    ec2AsyncClient.setRegion(Region.getRegion(Regions.fromName(region)));

    return ec2AsyncClient;

}