Comment puis-je transmettre des informations d'identification temporaires pour AssumeRole à l'environnement d'exécution Docker avec AWS CodeBuild ?
Dernière mise à jour : 24/09/2020
Je souhaite transmettre des informations d'identification temporaires pour AssumeRole à l'environnement d'exécution Docker avec AWS CodeBuild.
Brève description
CodeBuild utilise le rôle de service CodeBuild comme informations d'identification AWS par défaut dans le conteneur de version et l'environnement d'exécution Docker.
Exportez les informations d'identification AssumeRole en tant que variables d'environnement. Ensuite, passez ces variables dans l'environnement d'exécution Docker en utilisant le paramètre docker build —build-arg . Pour plus d'informations, consultez docker build sur le site Web Docker Docs.
Solution
1. Créez un nouveau rôle pour l'environnement d'exécution Docker (par ex., Secretassumerole). Utilisez ensuite le nouveau rôle pour obtenir la valeur AWSExampleSecret du secret à partir d'AWS Secrets Manager. Par exemple :
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetResourcePolicy",
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret",
"secretsmanager:ListSecretVersionIds"
],
"Resource": [
"arn:aws:secretsmanager:ap-northeast-1:$account_id:secret:tutorials/AWSExampleSecret-EHWYme"
]
}
]
}
Remarque : vous pouvez accorder n'importe quelle autorisation d'opération pendant l'exécution de Docker.
2. Ajoutez des autorisations sts : assumeRole à votre rôle de service CodeBuild pour autoriser les opérations AssumeRole. Par exemple :
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::$account_id:role:role/Secretassumerole"
}
]
}
3. Utilisez une spécification de version pour exporter les informations d'identification AssumeRole dans une variable d'environnement. Ensuite, utilisez la commande “docker build” (version de docker) pour transmettre les informations d'identification à votre environnement d'exécution Docker. Par exemple :
version: 0.2
phases:
install:
runtime-versions:
nodejs: 8
commands:
- ASSUME_ROLE_ARN="arn:aws:iam::$account_id:role/Secretassumerole"
- TEMP_ROLE=`aws sts assume-role --role-arn $ASSUME_ROLE_ARN --role-session-name test`
- export TEMP_ROLE
- echo $TEMP_ROLE
- export AWS_ACCESS_KEY_ID=$(echo "${TEMP_ROLE}" | jq -r '.Credentials.AccessKeyId')
- export AWS_SECRET_ACCESS_KEY=$(echo "${TEMP_ROLE}" | jq -r '.Credentials.SecretAccessKey')
- export AWS_SESSION_TOKEN=$(echo "${TEMP_ROLE}" | jq -r '.Credentials.SessionToken')
- echo $AWS_ACCESS_KEY_ID
- echo $AWS_SECRET_ACCESS_KEY
- echo $AWS_SESSION_TOKEN
pre_build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build --build-arg AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID --build-arg AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY --build-arg AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN
Remarque : Vous pouvez utiliser --duration-seconds pour définir la durée maximale de la session pour AssumeRole. L'option --duration-seconds accepte une valeur comprise entre 1 heure et 12 heures. Si vous définissez une valeur supérieure à 12 heures, l'opération échoue.
4. Dans votre Dockerfile, obtenez les informations d'identification AssumeRole lorsque vous créez une image. Par exemple :
FROM amazonlinux:latest
RUN yum -y install aws-cli
ARG AWS_DEFAULT_REGION
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
ARG AWS_SESSION_TOKEN
RUN echo $AWS_DEFAULT_REGION
RUN echo $AWS_ACCESS_KEY_ID
RUN echo $AWS_SECRET_ACCESS_KEY
RUN echo $AWS_SESSION_TOKEN
RUN aws sts get-caller-identity
RUN aws secretsmanager get-secret-value --secret-id tutorials/AWSExampleSecret
Remarque : vous pouvez supprimer l'instruction echo de la buildspec et Dockerfile qui imprime les informations d'identification temporaires.
Le résultat est similaire à celui-ci :
Step 8/11 : RUN echo $AWS_ACCESS_KEY_ID
---> Running in 7349ee896c1a
ASIAWNOF2TBWYN3DC7RX
Removing intermediate container 7349ee896c1a
---> 32a8170f9697
Step 9/11 : RUN echo $AWS_SECRET_ACCESS_KEY
---> Running in 9f16f1252d93
KJq+JNqmnNq1JirNUBkxc+kRVavgZwhpFFIJjxD6
Removing intermediate container 9f16f1252d93
---> 91fe8de3d301
Step 10/11 : RUN echo $AWS_SESSION_TOKEN
---> Running in 12ddfe17d5de
FQoGZXIvYXdzEJP//////////wEaDPTjooaOAaU8NDj5oyKkAjVwT4uQHTZJdCtfOZxa6wTZVOy0Zkw+laN1RRVZhvhdPOWhU8VgK2d7ZgTlqaXn4NSrdWlnub6g5JobP4o509t3VLdMUR5ZJJcpnSlJAY5YM7vlPndroGcV+Y689ztVzQ1uVxdtpjQK1qh87fw6m0dHt7Q8Y8TferRNVvCM4kOroxPzovTbO6IkLDcBp8PhOVgtVtxEpON6nZrN990zzUmhXjT0vrtpDtAi339hhs7fzDOrnllQHSAmSerT0NhMOYVqBH1HJOq3FYnG+TUbHENpSq3kwTiPL2uoTw7/Ufrrgz4i3ENHm3rIWlbD8VuleDl5yhooKifmKDPjQAHs5HbVjD9lnxQFrCIuyvZdmsqzgoIjPt6z5H8lzugLHAAmbgiOwDoo+Oba7QU=
Removing intermediate container 12ddfe17d5de
---> 0cc838f3c865
Step 11/11 : RUN aws sts get-caller-identity
---> Running in 98b7c2f07621
{
"Account": “xxxxxxxxx”,
"UserId": "AROAWNOF2TBWS3TGMQRV3:test",
"Arn": "arn:aws:sts::$account_id:assumed-role/Secretassumerole/test"
}
Removing intermediate container 6d525393d667
---> 2da2f38adc77
Step 12/12 : RUN aws secretsmanager get-secret-value --secret-id tutorials/AWSExampleSecret --region ap-northeast-1
---> Running in c8ac00304416
{
"Name": "tutorials/AWSExampleSecret",
"VersionId": "3f37aa26-691e-4b4b-ad80-861680464cb9",
"SecretString": "{\"username\":\"myserviceusername\",\"password\":\"MyVerySecureP@ssw0rd!\"}",
"VersionStages": [
"AWSCURRENT"
],
"CreatedDate": 1558616482.926,
"ARN": "arn:aws:secretsmanager:ap-northeast-1:$account_id:secret:tutorials/M-EHWYme"
}
Removing intermediate container c8ac00304416
---> c3be32e39b0e
Successfully built c3be32e39b0e
Cet article vous a-t-il été utile ?
Besoin d'aide pour une question technique ou de facturation ?