Search in sources :

Example 1 with StsException

use of software.amazon.awssdk.services.sts.model.StsException in project aws-doc-sdk-examples by awsdocs.

the class IAMScenario method assumeGivenRole.

// Invoke an Amazon S3 operation using the Assumed Role.
public static void assumeGivenRole(String roleArn, String roleSessionName, String bucketName) {
    StsClient stsClient = StsClient.builder().region(Region.US_EAST_1).build();
    try {
        AssumeRoleRequest roleRequest = AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName).build();
        AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
        Credentials myCreds = roleResponse.credentials();
        String key = myCreds.accessKeyId();
        String secKey = myCreds.secretAccessKey();
        String secToken = myCreds.sessionToken();
        // List all objects in an Amazon S3 bucket using the temp creds.
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(key, secKey, secToken))).region(region).build();
        System.out.println("Created a S3Client using temp credentials.");
        System.out.println("Listing objects in " + bucketName);
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            System.out.println("The name of the key is " + myValue.key());
            System.out.println("The owner is " + myValue.owner());
        }
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : StsClient(software.amazon.awssdk.services.sts.StsClient) AssumeRoleRequest(software.amazon.awssdk.services.sts.model.AssumeRoleRequest) ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) StsException(software.amazon.awssdk.services.sts.model.StsException) AssumeRoleResponse(software.amazon.awssdk.services.sts.model.AssumeRoleResponse) Region(software.amazon.awssdk.regions.Region) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) Credentials(software.amazon.awssdk.services.sts.model.Credentials)

Example 2 with StsException

use of software.amazon.awssdk.services.sts.model.StsException in project aws-doc-sdk-examples by awsdocs.

the class AssumeRole method assumeGivenRole.

// snippet-start:[sts.java2.assume_role.main]
public static void assumeGivenRole(StsClient stsClient, String roleArn, String roleSessionName) {
    try {
        AssumeRoleRequest roleRequest = AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName).build();
        AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
        Credentials myCreds = roleResponse.credentials();
        // Display the time when the temp creds expire
        Instant exTime = myCreds.expiration();
        String tokenInfo = myCreds.sessionToken();
        // Convert the Instant to readable date
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.US).withZone(ZoneId.systemDefault());
        formatter.format(exTime);
        System.out.println("The token " + tokenInfo + "  expires on " + exTime);
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : AssumeRoleRequest(software.amazon.awssdk.services.sts.model.AssumeRoleRequest) StsException(software.amazon.awssdk.services.sts.model.StsException) AssumeRoleResponse(software.amazon.awssdk.services.sts.model.AssumeRoleResponse) Instant(java.time.Instant) DateTimeFormatter(java.time.format.DateTimeFormatter) Credentials(software.amazon.awssdk.services.sts.model.Credentials)

Example 3 with StsException

use of software.amazon.awssdk.services.sts.model.StsException in project aws-doc-sdk-examples by awsdocs.

the class GetAccessKeyInfo method getKeyInfo.

// snippet-start:[sts.java2.get_access_key.main]
public static void getKeyInfo(StsClient stsClient, String accessKeyId) {
    try {
        GetAccessKeyInfoRequest accessRequest = GetAccessKeyInfoRequest.builder().accessKeyId(accessKeyId).build();
        GetAccessKeyInfoResponse accessResponse = stsClient.getAccessKeyInfo(accessRequest);
        System.out.println("The account associated with the access key is " + accessResponse.account());
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : GetAccessKeyInfoResponse(software.amazon.awssdk.services.sts.model.GetAccessKeyInfoResponse) StsException(software.amazon.awssdk.services.sts.model.StsException) GetAccessKeyInfoRequest(software.amazon.awssdk.services.sts.model.GetAccessKeyInfoRequest)

Example 4 with StsException

use of software.amazon.awssdk.services.sts.model.StsException in project aws-doc-sdk-examples by awsdocs.

the class GetSessionToken method getToken.

// snippet-start:[sts.java2.get_session_token.main]
public static void getToken(StsClient stsClient) {
    try {
        GetSessionTokenRequest tokenRequest = GetSessionTokenRequest.builder().durationSeconds(1500).build();
        GetSessionTokenResponse tokenResponse = stsClient.getSessionToken(tokenRequest);
        System.out.println("The token value is " + tokenResponse.credentials().sessionToken());
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : StsException(software.amazon.awssdk.services.sts.model.StsException) GetSessionTokenResponse(software.amazon.awssdk.services.sts.model.GetSessionTokenResponse) GetSessionTokenRequest(software.amazon.awssdk.services.sts.model.GetSessionTokenRequest)

Example 5 with StsException

use of software.amazon.awssdk.services.sts.model.StsException in project aws-doc-sdk-examples by awsdocs.

the class GetCallerIdentity method getCallerId.

// snippet-start:[sts.java2.get_call_id.main]
public static void getCallerId(StsClient stsClient) {
    try {
        GetCallerIdentityResponse response = stsClient.getCallerIdentity();
        System.out.println("The user id is" + response.userId());
        System.out.println("The ARN value is" + response.arn());
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : GetCallerIdentityResponse(software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse) StsException(software.amazon.awssdk.services.sts.model.StsException)

Aggregations

StsException (software.amazon.awssdk.services.sts.model.StsException)5 AssumeRoleRequest (software.amazon.awssdk.services.sts.model.AssumeRoleRequest)2 AssumeRoleResponse (software.amazon.awssdk.services.sts.model.AssumeRoleResponse)2 Credentials (software.amazon.awssdk.services.sts.model.Credentials)2 Instant (java.time.Instant)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 AwsSessionCredentials (software.amazon.awssdk.auth.credentials.AwsSessionCredentials)1 Region (software.amazon.awssdk.regions.Region)1 S3Client (software.amazon.awssdk.services.s3.S3Client)1 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)1 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)1 S3Object (software.amazon.awssdk.services.s3.model.S3Object)1 StsClient (software.amazon.awssdk.services.sts.StsClient)1 GetAccessKeyInfoRequest (software.amazon.awssdk.services.sts.model.GetAccessKeyInfoRequest)1 GetAccessKeyInfoResponse (software.amazon.awssdk.services.sts.model.GetAccessKeyInfoResponse)1 GetCallerIdentityResponse (software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse)1 GetSessionTokenRequest (software.amazon.awssdk.services.sts.model.GetSessionTokenRequest)1 GetSessionTokenResponse (software.amazon.awssdk.services.sts.model.GetSessionTokenResponse)1