|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Unless explicitly stated otherwise all files in this repository are licensed |
| 4 | +# under the Apache License Version 2.0. |
| 5 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 6 | +# Copyright 2019 Datadog, Inc. |
| 7 | + |
| 8 | +set -e |
| 9 | + |
| 10 | +LAYER_DIR=".layers" |
| 11 | +LAYER_FILES=("datadog_lambda_node10.15.zip" "datadog_lambda_node12.13.zip") |
| 12 | +SIGNING_PROFILE_NAME="DatadogLambdaSigningProfile" |
| 13 | + |
| 14 | +# Check account parameter |
| 15 | +VALID_ACCOUNTS=("sandbox" "prod") |
| 16 | +if [ -z "$1" ]; then |
| 17 | + echo "ERROR: You must pass an account parameter to sign the layers" |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | +if [[ ! "${VALID_ACCOUNTS[@]}" =~ $1 ]]; then |
| 21 | + echo "ERROR: The account parameter was invalid. Please choose sandbox or prod." |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | +if [ "$1" = "sandbox" ]; then |
| 25 | + REGION="sa-east-1" |
| 26 | + S3_BUCKET_NAME="dd-lambda-signing-bucket-sandbox" |
| 27 | +fi |
| 28 | +if [ "$1" = "prod" ]; then |
| 29 | + REGION="us-east-1" |
| 30 | + S3_BUCKET_NAME="dd-lambda-signing-bucket" |
| 31 | +fi |
| 32 | + |
| 33 | +for LAYER_FILE in "${LAYER_FILES[@]}" |
| 34 | +do |
| 35 | + echo |
| 36 | + echo "${LAYER_FILE}" |
| 37 | + echo "-------------------------" |
| 38 | + |
| 39 | + LAYER_LOCAL_PATH="${LAYER_DIR}/${LAYER_FILE}" |
| 40 | + |
| 41 | + # Upload the layer to S3 for signing |
| 42 | + echo "Uploading layer to S3 for signing..." |
| 43 | + UUID=$(uuidgen) |
| 44 | + S3_UNSIGNED_ZIP_KEY="${UUID}.zip" |
| 45 | + S3_UNSIGNED_ZIP_URI="s3://${S3_BUCKET_NAME}/${S3_UNSIGNED_ZIP_KEY}" |
| 46 | + aws s3 cp $LAYER_LOCAL_PATH $S3_UNSIGNED_ZIP_URI |
| 47 | + |
| 48 | + # Start a signing job |
| 49 | + echo "Starting the signing job..." |
| 50 | + SIGNING_JOB_ID=$(aws signer start-signing-job \ |
| 51 | + --source "s3={bucketName=${S3_BUCKET_NAME},key=${S3_UNSIGNED_ZIP_KEY},version=null}" \ |
| 52 | + --destination "s3={bucketName=${S3_BUCKET_NAME}}" \ |
| 53 | + --profile-name $SIGNING_PROFILE_NAME \ |
| 54 | + --region $REGION \ |
| 55 | + | jq -r '.jobId'\ |
| 56 | + ) |
| 57 | + |
| 58 | + # Wait for the signing job to complete |
| 59 | + echo "Waiting for the signing job to complete..." |
| 60 | + SECONDS_WAITED_SO_FAR=0 |
| 61 | + while : |
| 62 | + do |
| 63 | + sleep 3 |
| 64 | + SECONDS_WAITED_SO_FAR=$((SECONDS_WAITED_SO_FAR + 3)) |
| 65 | + |
| 66 | + SIGNING_JOB_DESCRIPTION=$(aws signer describe-signing-job \ |
| 67 | + --job-id $SIGNING_JOB_ID \ |
| 68 | + --region $REGION\ |
| 69 | + ) |
| 70 | + SIGNING_JOB_STATUS=$(echo $SIGNING_JOB_DESCRIPTION | jq -r '.status') |
| 71 | + SIGNING_JOB_STATUS_REASON=$(echo $SIGNING_JOB_DESCRIPTION | jq -r '.statusReason') |
| 72 | + |
| 73 | + if [ $SIGNING_JOB_STATUS = "Succeeded" ]; then |
| 74 | + echo "Signing job succeeded!" |
| 75 | + break |
| 76 | + fi |
| 77 | + |
| 78 | + if [ $SIGNING_JOB_STATUS = "Failed" ]; then |
| 79 | + echo "ERROR: Signing job failed" |
| 80 | + echo $SIGNING_JOB_STATUS_REASON |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + |
| 84 | + if [ $SECONDS_WAITED_SO_FAR -ge 60 ]; then |
| 85 | + echo "ERROR: Timed out waiting for the signing job to complete" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + |
| 89 | + echo "Signing job still in progress..." |
| 90 | + done |
| 91 | + |
| 92 | + # Download the signed ZIP, overwriting the original ZIP |
| 93 | + echo "Replacing the local layer with the signed layer from S3..." |
| 94 | + S3_SIGNED_ZIP_KEY="${SIGNING_JOB_ID}.zip" |
| 95 | + S3_SIGNED_ZIP_URI="s3://${S3_BUCKET_NAME}/${S3_SIGNED_ZIP_KEY}" |
| 96 | + aws s3 cp $S3_SIGNED_ZIP_URI $LAYER_LOCAL_PATH |
| 97 | + |
| 98 | + # Delete the signed and unsigned ZIPs in S3 |
| 99 | + echo "Cleaning up the S3 bucket..." |
| 100 | + aws s3api delete-object --bucket $S3_BUCKET_NAME --key $S3_UNSIGNED_ZIP_KEY |
| 101 | + aws s3api delete-object --bucket $S3_BUCKET_NAME --key $S3_SIGNED_ZIP_KEY |
| 102 | +done |
| 103 | + |
| 104 | +echo |
| 105 | +echo "Successfully signed all layers!" |
0 commit comments