Example usage for com.amazonaws.auth BasicSessionCredentials getAWSSecretKey

List of usage examples for com.amazonaws.auth BasicSessionCredentials getAWSSecretKey

Introduction

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

Prototype

public String getAWSSecretKey() 

Source Link

Usage

From source file:com.okta.tools.awscli.java

License:Open Source License

private static String setAWSCredentials(AssumeRoleWithSAMLResult assumeResult, String credentialsProfileName)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {
    BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials(
            assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(),
            assumeResult.getCredentials().getSessionToken());

    String awsAccessKey = temporaryCredentials.getAWSAccessKeyId();
    String awsSecretKey = temporaryCredentials.getAWSSecretKey();
    String awsSessionToken = temporaryCredentials.getSessionToken();

    //File file = new File(System.getProperty("user.home") + "/.aws/credentials");
    //file.getParentFile().mkdirs();
    //try {/*from   w ww  .  ja v  a  2 s.c  o m*/

    if (credentialsProfileName.startsWith("arn:aws:sts::")) {
        credentialsProfileName = credentialsProfileName.substring(13);
    }
    if (credentialsProfileName.contains(":assumed-role")) {
        credentialsProfileName = credentialsProfileName.replaceAll(":assumed-role", "");
    }

    Object[] args = { new String(credentialsProfileName) };
    //writer.println("[aws-okta]");
    MessageFormat fmt = new MessageFormat("[{0}]");
    String profileNameLine = fmt.format(args);

    ProfilesConfigFile profilesConfigFile = null;
    try {
        profilesConfigFile = new ProfilesConfigFile();
    } catch (AmazonClientException ace) {
        PopulateCredentialsFile(profileNameLine, awsAccessKey, awsSecretKey, awsSessionToken);
    }

    try {
        if (profilesConfigFile != null && profilesConfigFile.getCredentials(credentialsProfileName) != null) {

            //if we end up here, it means we were  able to find a matching profile
            PopulateCredentialsFile(profileNameLine, awsAccessKey, awsSecretKey, awsSessionToken);
        }
    } catch (IllegalArgumentException iae) {

        //if we end up here, it means we were not able to find a matching profile so we need to append one
        FileWriter fileWriter = new FileWriter(System.getProperty("user.home") + "/.aws/credentials", true); //TODO: need to be updated to work with Windows
        PrintWriter writer = new PrintWriter(fileWriter); // new PrintWriter(file, "UTF-8");
        WriteNewProfile(writer, profileNameLine, awsAccessKey, awsSecretKey, awsSessionToken);
        fileWriter.close();
    }

    return credentialsProfileName;
}

From source file:io.hekate.cluster.seed.jclouds.aws.AwsCredentialsSupplier.java

License:Apache License

@Override
public Credentials get() {
    String identity = getIdentity() != null ? getIdentity().trim() : null;
    String credential = getCredential() != null ? getCredential().trim() : null;

    if (identity == null || identity.isEmpty() || credential == null || credential.isEmpty()) {
        DefaultAWSCredentialsProviderChain chain = new DefaultAWSCredentialsProviderChain();

        AWSCredentials cred = chain.getCredentials();

        if (cred instanceof BasicSessionCredentials) {
            BasicSessionCredentials sesCred = (BasicSessionCredentials) cred;

            return new SessionCredentials.Builder().identity(sesCred.getAWSAccessKeyId())
                    .credential(sesCred.getAWSSecretKey()).sessionToken(sesCred.getSessionToken()).build();
        } else {// w  w w. j  av a  2s.  c  o m
            return new Credentials.Builder<>().identity(cred.getAWSAccessKeyId())
                    .credential(cred.getAWSSecretKey()).build();
        }
    }

    return super.get();
}