Fornisci credenziali temporanee nel codice - AWS SDK for Java 2.x

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Fornisci credenziali temporanee nel codice

Se la catena di credenziali predefinita o un provider o una catena di fornitori specifici o personalizzati non funzionano per l'applicazione, puoi fornire credenziali temporanee direttamente nel codice. Queste possono essere credenziali di ruolo IAM come descritto sopra o credenziali temporanee recuperate da (). AWS Security Token Service AWS STS Se hai recuperato credenziali temporanee utilizzando AWS STS, forniscile a un AWS servizio client come mostrato nel seguente esempio di codice.

  1. Assumi un ruolo chiamando. StsClient.assumeRole()

  2. Crea un StaticCredentialsProvideroggetto e forniscilo con l'AwsSessionCredentialsoggetto.

  3. Configura il service client builder con StaticCredentialsProvider e crea il client.

L'esempio seguente crea un client di servizio Amazon S3 utilizzando credenziali temporanee restituite da AWS STS per un ruolo assunto da IAM.

// The AWS IAM Identity Center identity (user) who executes this method does not have permission to list buckets. // The identity is configured in the [default] profile. public static void assumeRole(String roleArn, String roleSessionName) { // The IAM role represented by the 'roleArn' parameter can be assumed by identities in two different accounts // and the role permits the user to only list buckets. // The SDK's default credentials provider chain will find the single sign-on settings in the [default] profile. // The identity configured with the [default] profile needs permission to call AssumeRole on the STS service. try { Credentials tempRoleCredentials; try (StsClient stsClient = StsClient.create()) { AssumeRoleRequest roleRequest = AssumeRoleRequest.builder() .roleArn(roleArn) .roleSessionName(roleSessionName) .build(); AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest); tempRoleCredentials = roleResponse.credentials(); } // Use the following temporary credential items for the S3 client. String key = tempRoleCredentials.accessKeyId(); String secKey = tempRoleCredentials.secretAccessKey(); String secToken = tempRoleCredentials.sessionToken(); // List all buckets in the account associated with the assumed role // by using the temporary credentials retrieved by invoking stsClient.assumeRole(). StaticCredentialsProvider staticCredentialsProvider = StaticCredentialsProvider.create( AwsSessionCredentials.create(key, secKey, secToken)); try (S3Client s3 = S3Client.builder() .credentialsProvider(staticCredentialsProvider) .build()) { List<Bucket> buckets = s3.listBuckets().buckets(); for (Bucket bucket : buckets) { System.out.println("bucket name: " + bucket.name()); } } } catch (StsException | S3Exception e) { logger.error(e.getMessage()); System.exit(1); } }

Il seguente set di autorizzazioni definito in AWS IAM Identity Center consente all'identità (utente) di eseguire le due operazioni seguenti

  1. Il GetObject funzionamento di Amazon Simple Storage Service.

  2. Il AssumeRole funzionamento di AWS Security Token Service.

Senza assumere il ruolo, il s3.listBuckets() metodo illustrato nell'esempio fallirebbe.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "sts:AssumeRole" ], "Resource": [ "*" ] } ] }

Politica sulle autorizzazioni relative ai ruoli assunti

La seguente politica di autorizzazioni è associata al ruolo assunto nell'esempio precedente. Questa politica di autorizzazioni consente di elencare tutti i bucket nello stesso account del ruolo.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets" ], "Resource": [ "*" ] } ] }

Politica di fiducia per i ruoli assunti

La seguente politica di fiducia è associata al ruolo assunto nell'esempio precedente. La politica consente alle identità (utenti) di assumere il ruolo in due account.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": [ "arn:aws:iam::111122223333:root", "arn:aws:iam::555555555555:root" ] }, "Action": "sts:AssumeRole", "Condition": {} } ] }