|
2 | 2 |
|
3 | 3 | set -e |
4 | 4 |
|
| 5 | +USAGE=" |
| 6 | +NB: This script has been designed to be run from the repos root level directroy, as that is where it will be executed from in the pre-validation stage of the catalog onboarding pipeline. |
| 7 | +
|
| 8 | +The script will generate SSH keys and then append the public key to the catalogValidationValues.json file which is generated by the onboarding catalog pipeline before this script runs. |
| 9 | +
|
| 10 | +Usage (from repos root directory): ./tests/scripts/pre-validation-generate-ssh-key.sh <ssh_key_variable_name> <directory_of_json> |
| 11 | +
|
| 12 | +where: |
| 13 | +<ssh_key_variable_name> = The name of the terraform variable which takes the SSH public key as input |
| 14 | +<directory_of_json> = The directory where the catalogValidationValues.json file exists (will be the same directroy as the terraform code being onboarded) |
| 15 | +" |
| 16 | + |
5 | 17 | if [ $# -eq 0 ]; then |
6 | | - echo "No arguments supplied - expected the terraform input variable name for ssh public key" |
| 18 | + echo "No arguments supplied. See usage:" |
| 19 | + echo "${USAGE}" |
7 | 20 | exit 1 |
8 | 21 | fi |
9 | 22 |
|
10 | | -var_name=$1 |
11 | | - |
12 | | -# Paths relative to base directory of script |
13 | | -BASE_DIR=$(dirname "$0") |
14 | | -TERRAFORM_SOURCE_DIR="../resources" |
15 | | -JSON_FILE="../../../catalogValidationValues.json" |
| 23 | +VAR_NAME=$1 |
| 24 | +DIR=$2 |
16 | 25 |
|
17 | | -( |
18 | | - # Execute script from base directory |
19 | | - cd "${BASE_DIR}" |
20 | | - echo "Generating SSH public key to be used for validation .." |
| 26 | +# Generate SSH keys and place in temp directory |
| 27 | +temp_dir=$(mktemp -d) |
| 28 | +ssh-keygen -f "${temp_dir}/id_rsa" -t rsa -N '' <<<y |
21 | 29 |
|
22 | | - cd ${TERRAFORM_SOURCE_DIR} |
23 | | - terraform init || exit 1 |
24 | | - terraform apply -auto-approve || exit 1 |
| 30 | +# Extract public key value and delete temp directory |
| 31 | +ssh_public_key=$(cat "${temp_dir}/id_rsa.pub") |
| 32 | +rm -rf "${temp_dir}" |
25 | 33 |
|
26 | | - ssh_public_key=$(terraform output -state=terraform.tfstate -raw ssh_public_key) |
27 | | - echo "Appending SSH public key to $(basename ${JSON_FILE}).." |
28 | | - jq -r --arg var_name "${var_name}" --arg ssh_public_key "${ssh_public_key}" '. + {($var_name): $ssh_public_key}' "${JSON_FILE}" > tmpfile && mv tmpfile "${JSON_FILE}" || exit 1 |
| 34 | +# Append the SSH public key value to the JSON file which will be used during catalog onboarding validation |
| 35 | +json_file="${DIR}/catalogValidationValues.json" # This gets created by pipeline code based on the catalogValidationValues.json.template |
| 36 | +echo "Appending SSH public key to ${json_file}.." |
| 37 | +jq -r --arg var_name "${VAR_NAME}" --arg ssh_public_key "${ssh_public_key}" '. + {($var_name): $ssh_public_key}' "${json_file}" > tmpfile && mv tmpfile "${json_file}" || exit 1 |
29 | 38 |
|
30 | | - echo "Pre-validation complete successfully" |
31 | | -) |
| 39 | +echo "Pre-validation complete successfully" |
0 commit comments