fix(pipeline): corregir OIDC token handling y backend region validation
- Escribe BITBUCKET_STEP_OIDC_TOKEN a archivo temporal para evitar OSError: File name too long en el decoder JWT - Python lee token via stdin en lugar de pasar JWT como filename - Agrega skip_region_validation = true en backend.dev.hcl y backend.prod.hcl para compatibilidad con mx-central-1
This commit is contained in:
+18
-10
@@ -1,8 +1,8 @@
|
||||
#!/bin/bash
|
||||
# ===============================================================================================================
|
||||
# aws-oidc-setup.sh - Configura credenciales AWS via OIDC para Bitbucket Pipelines
|
||||
# Descripción:
|
||||
# Obtiene credenciales temporales de AWS explícitamente mediante
|
||||
# Descripcion:
|
||||
# Obtiene credenciales temporales de AWS explicitamente mediante
|
||||
# assume-role-with-web-identity y exporta las variables necesarias
|
||||
# para que Terraform (incluyendo su backend S3) y AWS CLI funcionen.
|
||||
#
|
||||
@@ -28,13 +28,18 @@ fi
|
||||
ENV="${1:-dev}"
|
||||
|
||||
if [[ -z "${BITBUCKET_STEP_OIDC_TOKEN:-}" ]]; then
|
||||
echo "ERROR: BITBUCKET_STEP_OIDC_TOKEN no está definido."
|
||||
echo "Asegúrate de agregar 'oidc: true' al step en bitbucket-pipelines.yml"
|
||||
echo "ERROR: BITBUCKET_STEP_OIDC_TOKEN no esta definido."
|
||||
echo "Asegurate de agregar 'oidc: true' al step en bitbucket-pipelines.yml"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
export AWS_WEB_IDENTITY_TOKEN_FILE="$(pwd)/web-identity-token"
|
||||
printf '%s' "${BITBUCKET_STEP_OIDC_TOKEN}" > "${AWS_WEB_IDENTITY_TOKEN_FILE}"
|
||||
# Escribir el token a un archivo temporal seguro para evitar que el decoder
|
||||
# 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
|
||||
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 "=== Decoding OIDC Token ==="
|
||||
# Leer el token desde stdin para nunca pasar el contenido como argumento o filename.
|
||||
python3 -c "
|
||||
import json, base64, sys
|
||||
t = open('${AWS_WEB_IDENTITY_TOKEN_FILE}').read().strip()
|
||||
t = sys.stdin.read().strip()
|
||||
payload = t.split('.')[1]
|
||||
padding = 4 - len(payload) % 4
|
||||
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 "==========================="
|
||||
|
||||
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_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}"
|
||||
|
||||
echo "Credenciales obtenidas exitosamente."
|
||||
|
||||
@@ -3,3 +3,4 @@ key = "proyectosacc/terraform.tfstate"
|
||||
region = "mx-central-1"
|
||||
encrypt = true
|
||||
dynamodb_table = "terraform-locks"
|
||||
skip_region_validation = true
|
||||
|
||||
@@ -3,3 +3,4 @@ key = "proyectosacc/terraform.tfstate"
|
||||
region = "mx-central-1"
|
||||
encrypt = true
|
||||
dynamodb_table = "terraform-locks-proyectosacc-prod"
|
||||
skip_region_validation = true
|
||||
|
||||
Reference in New Issue
Block a user