|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2020 Google LLC. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# This file is called in the early stage of `trampoline_v2.sh` to |
| 17 | +# populate secrets needed for the CI builds. |
| 18 | + |
| 19 | +set -eo pipefail |
| 20 | + |
| 21 | +function now { date +"%Y-%m-%d %H:%M:%S" | tr -d '\n' ;} |
| 22 | +function msg { println "$*" >&2 ;} |
| 23 | +function println { printf '%s\n' "$(now) $*" ;} |
| 24 | + |
| 25 | +# Populates requested secrets set in SECRET_MANAGER_KEYS |
| 26 | + |
| 27 | +# In Kokoro CI builds, we use the service account attached to the |
| 28 | +# Kokoro VM. This means we need to setup auth on other CI systems. |
| 29 | +# For local run, we just use the gcloud command for retrieving the |
| 30 | +# secrets. |
| 31 | + |
| 32 | +if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then |
| 33 | + GCLOUD_COMMANDS=( |
| 34 | + "docker" |
| 35 | + "run" |
| 36 | + "--entrypoint=gcloud" |
| 37 | + "--volume=${KOKORO_GFILE_DIR}:${KOKORO_GFILE_DIR}" |
| 38 | + "gcr.io/google.com/cloudsdktool/cloud-sdk" |
| 39 | + ) |
| 40 | + if [[ "${TRAMPOLINE_CI:-}" == "kokoro" ]]; then |
| 41 | + SECRET_LOCATION="${KOKORO_GFILE_DIR}/secret_manager" |
| 42 | + else |
| 43 | + echo "Authentication for this CI system is not implemented yet." |
| 44 | + exit 2 |
| 45 | + # TODO: Determine appropriate SECRET_LOCATION and the GCLOUD_COMMANDS. |
| 46 | + fi |
| 47 | +else |
| 48 | + # For local run, use /dev/shm or temporary directory for |
| 49 | + # KOKORO_GFILE_DIR. |
| 50 | + if [[ -d "/dev/shm" ]]; then |
| 51 | + export KOKORO_GFILE_DIR=/dev/shm |
| 52 | + else |
| 53 | + export KOKORO_GFILE_DIR=$(mktemp -d -t ci-XXXXXXXX) |
| 54 | + fi |
| 55 | + SECRET_LOCATION="${KOKORO_GFILE_DIR}/secret_manager" |
| 56 | + GCLOUD_COMMANDS=("gcloud") |
| 57 | +fi |
| 58 | + |
| 59 | +msg "Creating folder on disk for secrets: ${SECRET_LOCATION}" |
| 60 | +mkdir -p ${SECRET_LOCATION} |
| 61 | + |
| 62 | +for key in $(echo ${SECRET_MANAGER_KEYS} | sed "s/,/ /g") |
| 63 | +do |
| 64 | + msg "Retrieving secret ${key}" |
| 65 | + "${GCLOUD_COMMANDS[@]}" \ |
| 66 | + secrets versions access latest \ |
| 67 | + --project cloud-devrel-kokoro-resources \ |
| 68 | + --secret $key > \ |
| 69 | + "$SECRET_LOCATION/$key" |
| 70 | + if [[ $? == 0 ]]; then |
| 71 | + msg "Secret written to ${SECRET_LOCATION}/${key}" |
| 72 | + else |
| 73 | + msg "Error retrieving secret ${key}" |
| 74 | + exit 2 |
| 75 | + fi |
| 76 | +done |
0 commit comments