Merge developer into master: OIDC fixes and backend region validation
This commit is contained in:
@@ -11,12 +11,11 @@ image: atlassian/default-image:5
|
|||||||
|
|
||||||
options:
|
options:
|
||||||
max-time: 120
|
max-time: 120
|
||||||
|
|
||||||
definitions:
|
|
||||||
oidc:
|
oidc:
|
||||||
audiences:
|
audiences:
|
||||||
- sts.amazonaws.com
|
- sts.amazonaws.com
|
||||||
|
|
||||||
|
definitions:
|
||||||
steps:
|
steps:
|
||||||
- step: ¬ify-start
|
- step: ¬ify-start
|
||||||
name: Notify Start
|
name: Notify Start
|
||||||
|
|||||||
+18
-10
@@ -1,8 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# ===============================================================================================================
|
# ===============================================================================================================
|
||||||
# aws-oidc-setup.sh - Configura credenciales AWS via OIDC para Bitbucket Pipelines
|
# aws-oidc-setup.sh - Configura credenciales AWS via OIDC para Bitbucket Pipelines
|
||||||
# Descripción:
|
# Descripcion:
|
||||||
# Obtiene credenciales temporales de AWS explícitamente mediante
|
# Obtiene credenciales temporales de AWS explicitamente mediante
|
||||||
# assume-role-with-web-identity y exporta las variables necesarias
|
# assume-role-with-web-identity y exporta las variables necesarias
|
||||||
# para que Terraform (incluyendo su backend S3) y AWS CLI funcionen.
|
# para que Terraform (incluyendo su backend S3) y AWS CLI funcionen.
|
||||||
#
|
#
|
||||||
@@ -28,13 +28,18 @@ fi
|
|||||||
ENV="${1:-dev}"
|
ENV="${1:-dev}"
|
||||||
|
|
||||||
if [[ -z "${BITBUCKET_STEP_OIDC_TOKEN:-}" ]]; then
|
if [[ -z "${BITBUCKET_STEP_OIDC_TOKEN:-}" ]]; then
|
||||||
echo "ERROR: BITBUCKET_STEP_OIDC_TOKEN no está definido."
|
echo "ERROR: BITBUCKET_STEP_OIDC_TOKEN no esta definido."
|
||||||
echo "Asegúrate de agregar 'oidc: true' al step en bitbucket-pipelines.yml"
|
echo "Asegurate de agregar 'oidc: true' al step en bitbucket-pipelines.yml"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export AWS_WEB_IDENTITY_TOKEN_FILE="$(pwd)/web-identity-token"
|
# Escribir el token a un archivo temporal seguro para evitar que el decoder
|
||||||
printf '%s' "${BITBUCKET_STEP_OIDC_TOKEN}" > "${AWS_WEB_IDENTITY_TOKEN_FILE}"
|
# de Python trate el JWT crudo como un nombre de archivo (OSError 36).
|
||||||
|
TOKEN_FILE="$(mktemp)"
|
||||||
|
trap 'rm -f "${TOKEN_FILE}"' EXIT
|
||||||
|
|
||||||
|
printf '%s' "${BITBUCKET_STEP_OIDC_TOKEN}" > "${TOKEN_FILE}"
|
||||||
|
export AWS_WEB_IDENTITY_TOKEN_FILE="${TOKEN_FILE}"
|
||||||
|
|
||||||
if [[ "$ENV" == "prod" ]]; then
|
if [[ "$ENV" == "prod" ]]; then
|
||||||
export AWS_ROLE_ARN="arn:aws:iam::523761210517:role/BitbucketProyectosaccCICDRoleProd"
|
export AWS_ROLE_ARN="arn:aws:iam::523761210517:role/BitbucketProyectosaccCICDRoleProd"
|
||||||
@@ -54,14 +59,17 @@ echo "Token file : $AWS_WEB_IDENTITY_TOKEN_FILE"
|
|||||||
echo "Obteniendo credenciales temporales via STS..."
|
echo "Obteniendo credenciales temporales via STS..."
|
||||||
|
|
||||||
echo "=== Decoding OIDC Token ==="
|
echo "=== Decoding OIDC Token ==="
|
||||||
|
# Leer el token desde stdin para nunca pasar el contenido como argumento o filename.
|
||||||
python3 -c "
|
python3 -c "
|
||||||
import json, base64, sys
|
import json, base64, sys
|
||||||
t = open('${AWS_WEB_IDENTITY_TOKEN_FILE}').read().strip()
|
t = sys.stdin.read().strip()
|
||||||
payload = t.split('.')[1]
|
payload = t.split('.')[1]
|
||||||
padding = 4 - len(payload) % 4
|
padding = 4 - len(payload) % 4
|
||||||
if padding != 4: payload += '=' * padding
|
if padding != 4: payload += '=' * padding
|
||||||
print(json.dumps(json.loads(base64.b64decode(payload)), indent=2))
|
decoded = json.loads(base64.b64decode(payload))
|
||||||
"
|
print(json.dumps(decoded, indent=2))
|
||||||
|
print('aud claim :', decoded.get('aud', 'N/A'))
|
||||||
|
" < "${TOKEN_FILE}"
|
||||||
echo "==========================="
|
echo "==========================="
|
||||||
|
|
||||||
CREDS=$(aws sts assume-role-with-web-identity \
|
CREDS=$(aws sts assume-role-with-web-identity \
|
||||||
@@ -75,7 +83,7 @@ export AWS_ACCESS_KEY_ID=$(echo "$CREDS" | python3 -c "import sys,json; print(js
|
|||||||
export AWS_SECRET_ACCESS_KEY=$(echo "$CREDS" | python3 -c "import sys,json; print(json.load(sys.stdin)['Credentials']['SecretAccessKey'])")
|
export AWS_SECRET_ACCESS_KEY=$(echo "$CREDS" | python3 -c "import sys,json; print(json.load(sys.stdin)['Credentials']['SecretAccessKey'])")
|
||||||
export AWS_SESSION_TOKEN=$(echo "$CREDS" | python3 -c "import sys,json; print(json.load(sys.stdin)['Credentials']['SessionToken'])")
|
export AWS_SESSION_TOKEN=$(echo "$CREDS" | python3 -c "import sys,json; print(json.load(sys.stdin)['Credentials']['SessionToken'])")
|
||||||
|
|
||||||
# Terraform S3 backend requiere estas variables explícitas
|
# Terraform S3 backend requiere estas variables explicitas
|
||||||
export AWS_REGION="${AWS_DEFAULT_REGION}"
|
export AWS_REGION="${AWS_DEFAULT_REGION}"
|
||||||
|
|
||||||
echo "Credenciales obtenidas exitosamente."
|
echo "Credenciales obtenidas exitosamente."
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ bucket = "ccsoft-terraform-state"
|
|||||||
key = "proyectosacc/terraform.tfstate"
|
key = "proyectosacc/terraform.tfstate"
|
||||||
region = "mx-central-1"
|
region = "mx-central-1"
|
||||||
encrypt = true
|
encrypt = true
|
||||||
dynamodb_table = "terraform-locks"
|
dynamodb_table = "terraform-locks"
|
||||||
|
skip_region_validation = true
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ bucket = "ccsoft-proyectosacc-terraform-state-prod"
|
|||||||
key = "proyectosacc/terraform.tfstate"
|
key = "proyectosacc/terraform.tfstate"
|
||||||
region = "mx-central-1"
|
region = "mx-central-1"
|
||||||
encrypt = true
|
encrypt = true
|
||||||
dynamodb_table = "terraform-locks-proyectosacc-prod"
|
dynamodb_table = "terraform-locks-proyectosacc-prod"
|
||||||
|
skip_region_validation = true
|
||||||
|
|||||||
Reference in New Issue
Block a user