| 1 | #!/bin/sh |
| 2 | |
| 3 | # Get the S3 URL containing all of the MQTT5 testing environment variables passed in to the bash script |
| 4 | testing_env_bucket=$1 |
| 5 | region=$2 |
| 6 | |
| 7 | # Make sure we have something: |
| 8 | if [ "${testing_env_bucket}" != "" ] && [ "${region}" != "" ]; then |
| 9 | echo "S3 bucket for environment variables found and region" |
| 10 | else |
| 11 | echo "Could not get S3 bucket for environment variables and/or region." |
| 12 | echo "You need to run this script and pass the S3 URL of the file containing" |
| 13 | echo "all of the environment variables to set, as well as the secrets for certificates and private keys" |
| 14 | echo "" |
| 15 | echo "Example: mqtt5_test_setup.sh s3://<bucket>/<file> <region>" |
| 16 | echo "" |
| 17 | echo "When finished, run 'cleanup' to remove the files downloaded:" |
| 18 | echo "" |
| 19 | echo "Example: mqtt5_test_setup.sh s3://<bucket>/<file> cleanup" |
| 20 | echo "" |
| 21 | return 1 |
| 22 | fi |
| 23 | |
| 24 | # Is this just a request to clean up? |
| 25 | # NOTE: This blindly assumes there is a environment_files.txt file |
| 26 | if [ "${region}" != "cleanup" ]; then |
| 27 | sleep 0.1 # we have to do something to do an else... |
| 28 | else |
| 29 | echo "Undoing environment variables" |
| 30 | unset $(grep -v '^#' ${PWD}/environment_files.txt | xargs | cut -d "=" -f 1) |
| 31 | unset AWS_TEST_MQTT5_CERTIFICATE_FILE |
| 32 | unset AWS_TEST_MQTT5_KEY_FILE |
| 33 | unset AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH |
| 34 | unset AWS_TEST_MQTT5_IOT_KEY_PATH |
| 35 | |
| 36 | echo "Cleaning up resources..." |
| 37 | rm "${PWD}/environment_files.txt" |
| 38 | rm "${PWD}/crt_certificate.pem" |
| 39 | rm "${PWD}/crt_privatekey.pem" |
| 40 | rm "${PWD}/iot_certificate.pem" |
| 41 | rm "${PWD}/iot_privatekey.pem" |
| 42 | |
| 43 | echo "Success!" |
| 44 | return 0 |
| 45 | fi |
| 46 | |
| 47 | # Get the file from S3 |
| 48 | aws s3 cp ${testing_env_bucket} ${PWD}/environment_files.txt |
| 49 | testing_env_file=$( cat environment_files.txt ) |
| 50 | # Make sure we have data of some form |
| 51 | if [ "${testing_env_file}" != "" ]; then |
| 52 | echo "Environment variables secret found" |
| 53 | else |
| 54 | echo "Could not get environment variables from secrets!" |
| 55 | return 1 |
| 56 | fi |
| 57 | |
| 58 | # Make all the variables in mqtt5_environment_variables.txt exported |
| 59 | # so we can run MQTT5 tests |
| 60 | export $(grep -v '^#' environment_files.txt | xargs) |
| 61 | |
| 62 | # CRT/non-builder certificate and key processing |
| 63 | # Get the certificate and key secrets (dumps straight to a file) |
| 64 | crt_cert_file=$(aws secretsmanager get-secret-value --secret-id "${AWS_TEST_MQTT5_CERTIFICATE_FILE_SECRET}" --query "SecretString" --region ${region} | cut -f2 -d":" | cut -f2 -d\") && echo -e "$crt_cert_file" > ${PWD}/crt_certificate.pem |
| 65 | crt_key_file=$(aws secretsmanager get-secret-value --secret-id "${AWS_TEST_MQTT5_KEY_FILE_SECRET}" --query "SecretString" --region ${region} | cut -f2 -d":" | cut -f2 -d\") && echo -e "$crt_key_file" > ${PWD}/crt_privatekey.pem |
| 66 | # Does the certificate file have data? If not, then abort! |
| 67 | if [ "${crt_cert_file}" != "" ]; then |
| 68 | echo "CRT Certificate secret found" |
| 69 | else |
| 70 | echo "Could not get CRT certificate from secrets!" |
| 71 | |
| 72 | # Clean up... |
| 73 | unset $(grep -v '^#' environment_files.txt | xargs | cut -d "=" -f 1) |
| 74 | rm "${PWD}/environment_files.txt" |
| 75 | rm "${PWD}/crt_certificate.pem" |
| 76 | rm "${PWD}/crt_privatekey.pem" |
| 77 | |
| 78 | return 1 |
| 79 | fi |
| 80 | # Does the private key file have data? If not, then abort! |
| 81 | if [ "${crt_key_file}" != "" ]; then |
| 82 | echo "CRT Private key secret found" |
| 83 | else |
| 84 | echo "Could not get CRT private key from secrets!" |
| 85 | |
| 86 | # Clean up... |
| 87 | unset $(grep -v '^#' environment_files.txt | xargs | cut -d "=" -f 1) |
| 88 | rm "${PWD}/environment_files.txt" |
| 89 | rm "${PWD}/crt_certificate.pem" |
| 90 | rm "${PWD}/crt_privatekey.pem" |
| 91 | |
| 92 | return 1 |
| 93 | fi |
| 94 | # Set the certificate and key paths (absolute paths for best compatibility) |
| 95 | export AWS_TEST_MQTT5_CERTIFICATE_FILE="${PWD}/crt_certificate.pem" |
| 96 | export AWS_TEST_MQTT5_KEY_FILE="${PWD}/crt_privatekey.pem" |
| 97 | |
| 98 | |
| 99 | # IoT/Builder certificate and key processing |
| 100 | # Get the certificate and key secrets (dumps straight to a file) |
| 101 | iot_cert_file=$(aws secretsmanager get-secret-value --secret-id "${AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH_SECRET}" --query "SecretString" --region ${region} | cut -f2 -d":" | cut -f2 -d\") && echo -e "$iot_cert_file" > ./iot_certificate.pem |
| 102 | iot_key_file=$(aws secretsmanager get-secret-value --secret-id "${AWS_TEST_MQTT5_IOT_KEY_PATH_SECRET}" --query "SecretString" --region ${region} | cut -f2 -d":" | cut -f2 -d\") && echo -e "$iot_key_file" > ./iot_privatekey.pem |
| 103 | # Does the certificate file have data? If not, then abort! |
| 104 | if [ "${iot_cert_file}" != "" ]; then |
| 105 | echo "IoT Certificate secret found" |
| 106 | else |
| 107 | echo "Could not get IoT certificate from secrets!" |
| 108 | |
| 109 | # Clean up... |
| 110 | unset $(grep -v '^#' environment_files.txt | xargs | cut -d "=" -f 1) |
| 111 | unset AWS_TEST_MQTT5_CERTIFICATE_FILE |
| 112 | unset AWS_TEST_MQTT5_KEY_FILE |
| 113 | rm "${PWD}/environment_files.txt" |
| 114 | rm "${PWD}/crt_certificate.pem" |
| 115 | rm "${PWD}/crt_privatekey.pem" |
| 116 | rm "${PWD}/iot_certificate.pem" |
| 117 | rm "${PWD}/iot_privatekey.pem" |
| 118 | |
| 119 | return 1 |
| 120 | fi |
| 121 | # Does the private key file have data? If not, then abort! |
| 122 | if [ "${iot_key_file}" != "" ]; then |
| 123 | echo "IoT Private key secret found" |
| 124 | else |
| 125 | echo "Could not get IoT private key from secrets!" |
| 126 | |
| 127 | # Clean up... |
| 128 | unset $(grep -v '^#' environment_files.txt | xargs | cut -d "=" -f 1) |
| 129 | unset AWS_TEST_MQTT5_CERTIFICATE_FILE |
| 130 | unset AWS_TEST_MQTT5_KEY_FILE |
| 131 | rm "${PWD}/environment_files.txt" |
| 132 | rm "${PWD}/crt_certificate.pem" |
| 133 | rm "${PWD}/crt_privatekey.pem" |
| 134 | rm "${PWD}/iot_certificate.pem" |
| 135 | rm "${PWD}/iot_privatekey.pem" |
| 136 | |
| 137 | return 1 |
| 138 | fi |
| 139 | # Set IoT certificate and key paths |
| 140 | export AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH="${PWD}/iot_certificate.pem" |
| 141 | export AWS_TEST_MQTT5_IOT_KEY_PATH="${PWD}/iot_privatekey.pem" |
| 142 | |
| 143 | # Everything is set |
| 144 | echo "Success: Environment variables set!" |
| 145 | return 0 |