|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +source ../utils.sh |
| 5 | + |
| 6 | +# This script does the following in sequence: |
| 7 | +# - Runs a Helm install of a Conjur OSS server |
| 8 | +# - Runs a Helm test that deploys a test container that runs a |
| 9 | +# Bash Automated Test System (a.k.a. "Bats") test script that |
| 10 | +# confirms that the Conjur server's status page is active. |
| 11 | +# |
| 12 | +# Optional Environment Variables: |
| 13 | +# CONJUR_NAMESPACE: Namespace to use for Conjur deployment. The |
| 14 | +# namespace is created if it doesn't exist. |
| 15 | +# HELM_INSTALL_TIMEOUT: Helm install timeout in seconds. |
| 16 | +# Defaults to `180`. |
| 17 | +# HELM_TEST_LOGGING: Set to true to enable Helm test log collection. |
| 18 | +# Defaults to false. |
| 19 | +# HELM_VERSION: Helm client version to use for the test. |
| 20 | +# Defaults to '3.1.3'. |
| 21 | +# KUBECTL_VERSION: Kubectl client version to use for the test. |
| 22 | +# Defaults to '1.16.9'. |
| 23 | +# SKIP_GCLOUD_LOGIN: If set to 'true', then skip Gcloud authentication. |
| 24 | +# This is useful for local testing whereby you've |
| 25 | +# already authenticated with GCP and/or have 'kubectl' |
| 26 | +# access to a cluster. Defaults to 'false'. |
| 27 | + |
| 28 | +test_id="$(random_string)" |
| 29 | + |
| 30 | +export CONJUR_NAMESPACE="${CONJUR_NAMESPACE:-conjur-oss-test-$test_id}" |
| 31 | +export HELM_INSTALL_TIMEOUT="${HELM_INSTALL_TIMEOUT:-180}" |
| 32 | +export HELM_TEST_LOGGING="${HELM_TEST_LOGGING:-true}" |
| 33 | +export HELM_VERSION="${HELM_VERSION:-3.1.3}" |
| 34 | +export KUBECTL_VERSION="${KUBECTL_VERSION:-1.16.9}" |
| 35 | +export RELEASE_NAME="$CONJUR_NAMESPACE" |
| 36 | +export SKIP_GCLOUD_LOGIN="${SKIP_GCLOUD_LOGIN:-false}" |
| 37 | + |
| 38 | +announce "Building gcloud/kubectl/helm client image..." |
| 39 | +# Build the gcloud/kubectl/helm client container image |
| 40 | +tools_image_name="conjur-oss-helm-kubectl" |
| 41 | +docker build -t "${tools_image_name}" \ |
| 42 | + --quiet \ |
| 43 | + --build-arg HELM_VERSION="$HELM_VERSION" \ |
| 44 | + --build-arg KUBECTL_VERSION="$KUBECTL_VERSION" \ |
| 45 | + -f Dockerfile \ |
| 46 | + . |
| 47 | + |
| 48 | +tmp_dir="$(pwd)/.tmp" |
| 49 | +tmp_bin_dir="${tmp_dir}/bin" |
| 50 | +mkdir -p "${tmp_bin_dir}" \ |
| 51 | + "${tmp_dir}/.kube" \ |
| 52 | + "${tmp_dir}/.config" |
| 53 | +export PATH="${tmp_bin_dir}:${PATH}" |
| 54 | + |
| 55 | +# Create a local alias for running 'gcloud' in client container |
| 56 | +cat > "${tmp_bin_dir}/gcloud" <<EOF |
| 57 | + docker run --rm \ |
| 58 | + --ipc="host" \ |
| 59 | + -v "${tmp_dir}/.kube:/root/.kube" \ |
| 60 | + -v "${tmp_dir}/.config:/root/.config" \ |
| 61 | + --entrypoint /usr/bin/gcloud \ |
| 62 | + "${tools_image_name}" \ |
| 63 | + "\$@" |
| 64 | +EOF |
| 65 | +chmod +x "${tmp_bin_dir}/gcloud" |
| 66 | + |
| 67 | +# Create a local alias for running 'helm' in client container |
| 68 | +cat > "${tmp_bin_dir}/helm" <<EOF |
| 69 | + docker run --rm \ |
| 70 | + -v "${tmp_dir}/.kube:/root/.kube" \ |
| 71 | + -v "${tmp_dir}/.config:/root/.config" \ |
| 72 | + -v "$(cd ../conjur-oss; pwd):/src/conjur-oss:ro" \ |
| 73 | + --entrypoint /usr/local/bin/helm \ |
| 74 | + "${tools_image_name}" \ |
| 75 | + "\$@" |
| 76 | +EOF |
| 77 | +chmod +x "${tmp_bin_dir}/helm" |
| 78 | + |
| 79 | +# Create a local alias for running 'kubectl' in client container |
| 80 | +cat > "${tmp_bin_dir}/kubectl" <<EOF |
| 81 | + docker run --rm \ |
| 82 | + -v "${tmp_dir}/.kube:/root/.kube" \ |
| 83 | + -v "${tmp_dir}/.config:/root/.config" \ |
| 84 | + --entrypoint /usr/local/bin/kubectl \ |
| 85 | + "${tools_image_name}" \ |
| 86 | + "\$@" |
| 87 | +EOF |
| 88 | +chmod +x "${tmp_bin_dir}/kubectl" |
| 89 | + |
| 90 | +if [ "$SKIP_GCLOUD_LOGIN" = true ]; then |
| 91 | + cp "$HOME/.kube/config" "$tmp_dir/.kube/config" |
| 92 | + cp -r "$HOME/.config/gcloud" "$tmp_dir/.config/gcloud" |
| 93 | +else |
| 94 | + announce "Logging in to GCP..." |
| 95 | + # It is assumed that the environment variables below are set by summon. |
| 96 | + docker run --rm \ |
| 97 | + -e GCLOUD_CLUSTER_NAME \ |
| 98 | + -e GCLOUD_PROJECT_NAME \ |
| 99 | + -e GCLOUD_SERVICE_KEY="/tmp${GCLOUD_SERVICE_KEY}" \ |
| 100 | + -e GCLOUD_ZONE \ |
| 101 | + -e DOCKER_REGISTRY_URL \ |
| 102 | + -e DOCKER_REGISTRY_PATH \ |
| 103 | + -v "${tmp_dir}/.kube:/root/.kube" \ |
| 104 | + -v "${tmp_dir}/.config:/root/.config" \ |
| 105 | + -v "${GCLOUD_SERVICE_KEY}:/tmp${GCLOUD_SERVICE_KEY}" \ |
| 106 | + -v "$(pwd):/src:ro" \ |
| 107 | + "${tools_image_name}" \ |
| 108 | + bash -c "./platform_login.sh" |
| 109 | +fi |
| 110 | + |
| 111 | +# Fix permissions on files created within Docker |
| 112 | +docker run --rm \ |
| 113 | + -v "${tmp_dir}/.kube:/root/.kube" \ |
| 114 | + -v "${tmp_dir}/.config:/root/.config" \ |
| 115 | + "${tools_image_name}" \ |
| 116 | + bash -c "chown ${UID} -R /root/.kube/config /root/.config/*" |
| 117 | + |
| 118 | +function delete_namespace { |
| 119 | + announce "Deleting namespace $CONJUR_NAMESPACE" |
| 120 | + kubectl delete namespace --ignore-not-found=true "$CONJUR_NAMESPACE" |
| 121 | +} |
| 122 | + |
| 123 | +if ! is_helm_v2; then |
| 124 | + # Helm v3 requires units for timeout values |
| 125 | + HELM_INSTALL_TIMEOUT+="s" |
| 126 | +else |
| 127 | + helm init --upgrade |
| 128 | +fi |
| 129 | + |
| 130 | +announce "Deploying and testing Conjur OSS" |
| 131 | +cd .. |
| 132 | +trap delete_namespace EXIT |
| 133 | +if ! ./test.sh; then |
| 134 | + announce " FAILED" |
| 135 | + exit 1 |
| 136 | +fi |
| 137 | +announce " SUCCESS" |
0 commit comments