|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -x |
| 3 | + |
| 4 | +# Following environment variables must be set |
| 5 | +# - IMAGE: uid2-operator image |
| 6 | +# - OUTPUT_DIR: output directory to store the artifacts |
| 7 | +# - MANIFEST_DIR: output directory to store the manifest for the enclave Id |
| 8 | +# - VERSION_NUMBER: the version number of the build |
| 9 | + |
| 10 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 11 | +INPUT_DIR=${SCRIPT_DIR} |
| 12 | + |
| 13 | +if [[ -z ${IMAGE} ]]; then |
| 14 | + echo "IMAGE cannot be empty" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | +IMAGE_VERSION=$(echo $IMAGE | awk -F':' '{print $2}') |
| 18 | +if [[ -z ${IMAGE_VERSION} ]]; then |
| 19 | + echo "Failed to extract image version from ${IMAGE}" |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +if [[ -z ${OUTPUT_DIR} ]]; then |
| 24 | + echo "OUTPUT_DIR cannot be empty" |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +mkdir -p ${OUTPUT_DIR} |
| 29 | +if [[ $? -ne 0 ]]; then |
| 30 | + echo "Failed to create ${OUTPUT_DIR}" |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +mkdir -p ${MANIFEST_DIR} |
| 35 | +if [[ $? -ne 0 ]]; then |
| 36 | + echo "Failed to create ${MANIFEST_DIR}" |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Input files |
| 41 | +INPUT_FILES=( |
| 42 | + operator.yaml |
| 43 | +) |
| 44 | + |
| 45 | +# Copy input files to output dir |
| 46 | +for f in ${INPUT_FILES[@]}; do |
| 47 | + cp ${INPUT_DIR}/${f} ${OUTPUT_DIR}/${f} |
| 48 | + if [[ $? -ne 0 ]]; then |
| 49 | + echo "Failed to copy ${INPUT_DIR}/${f} to ${OUTPUT_DIR}" |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +done |
| 53 | + |
| 54 | +az version |
| 55 | +# Install confcom extension, az is originally available in GitHub workflow environment |
| 56 | +az extension add --name confcom |
| 57 | +if [[ $? -ne 0 ]]; then |
| 58 | + echo "Failed to install Azure confcom extension" |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +# Required by az confcom |
| 63 | +sudo usermod -aG docker ${USER} |
| 64 | +if [[ $? -ne 0 ]]; then |
| 65 | + echo "Failed to add current user to docker group" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +# Generate operator template |
| 70 | +sed -i "s#IMAGE_PLACEHOLDER#${IMAGE}#g" ${OUTPUT_DIR}/operator.yaml |
| 71 | +# && \ |
| 72 | +# sed -i "s#IMAGE_VERSION_PLACEHOLDER#${IMAGE_VERSION}#g" ${OUTPUT_DIR}/operator.yaml |
| 73 | +if [[ $? -ne 0 ]]; then |
| 74 | + echo "Failed to pre-process operator template file" |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +# Export the policy, update it to turn off allow_environment_variable_dropping, and then insert it into the template |
| 79 | +# note that the EnclaveId is generated by generate.py on the raw policy, not the base64 version |
| 80 | +POLICY_DIGEST_FILE=azure-aks-operator-digest-$VERSION_NUMBER.txt |
| 81 | +az confcom acipolicygen --virtual-node-yaml ${OUTPUT_DIR}/operator.yaml --print-policy > ${INPUT_DIR}/policy.base64 |
| 82 | +if [[ $? -ne 0 ]]; then |
| 83 | + echo "Failed to generate ACI policy" |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +base64 -di < ${INPUT_DIR}/policy.base64 > ${INPUT_DIR}/generated.rego |
| 88 | +sed -i "s#allow_environment_variable_dropping := true#allow_environment_variable_dropping := false#g" ${INPUT_DIR}/generated.rego |
| 89 | +sed -i 's#{"pattern":"DEPLOYMENT_ENVIRONMENT=DEPLOYMENT_ENVIRONMENT_PLACEHOLDER","required":false,"strategy":"string"}#{"pattern":"DEPLOYMENT_ENVIRONMENT=.+","required":false,"strategy":"re2"}#g' generated.rego |
| 90 | +sed -i 's#{"pattern":"VAULT_NAME=VAULT_NAME_PLACEHOLDER","required":false,"strategy":"string"}#{"pattern":"VAULT_NAME=.+","required":false,"strategy":"re2"}#g' generated.rego |
| 91 | +sed -i 's#{"pattern":"OPERATOR_KEY_SECRET_NAME=OPERATOR_KEY_SECRET_NAME_PLACEHOLDER","required":false,"strategy":"string"}#{"pattern":"OPERATOR_KEY_SECRET_NAME=.+","required":false,"strategy":"re2"}#g' generated.rego |
| 92 | +base64 -w0 < ${INPUT_DIR}/generated.rego > ${INPUT_DIR}/generated.rego.base64 |
| 93 | +python3 ${SCRIPT_DIR}/generate.py ${INPUT_DIR}/generated.rego > ${MANIFEST_DIR}/${POLICY_DIGEST_FILE} |
| 94 | + |
| 95 | +sed -i "s#CCE_POLICY_PLACEHOLDER#$(cat ${INPUT_DIR}/generated.rego.base64)#g" ${OUTPUT_DIR}/operator.yaml |
0 commit comments