|
| 1 | +#!/usr/bin/env bash |
| 2 | +export DEV_DIR=/workspace/dev |
| 3 | +export CONTAINER_CONFIG=/workspace/.devcontainer/internal_dev |
| 4 | +git config --global --add safe.directory /workspace |
| 5 | + |
| 6 | +get_installation_id_and_key() { |
| 7 | + pushd ./dev >/dev/null || exit |
| 8 | + echo "Please enter your installation id and key from https://bitwarden.com/host:" |
| 9 | + read -r -p "Installation id: " INSTALLATION_ID |
| 10 | + read -r -p "Installation key: " INSTALLATION_KEY |
| 11 | + jq ".globalSettings.installation.id = \"$INSTALLATION_ID\" | |
| 12 | + .globalSettings.installation.key = \"$INSTALLATION_KEY\"" \ |
| 13 | + secrets.json.example >secrets.json # create/overwrite secrets.json |
| 14 | + popd >/dev/null || exit |
| 15 | +} |
| 16 | + |
| 17 | +remove_comments() { |
| 18 | + # jq will not parse files with comments |
| 19 | + file="$1" |
| 20 | + |
| 21 | + if [[ -f "$file" ]]; then |
| 22 | + sed -e '/^\/\//d' -e 's@[[:blank:]]\{1,\}//.*@@' "$file" >"$file.tmp" |
| 23 | + mv "$file.tmp" "$file" |
| 24 | + fi |
| 25 | +} |
| 26 | + |
| 27 | +configure_other_vars() { |
| 28 | + pushd ./dev >/dev/null || exit |
| 29 | + cp secrets.json .secrets.json.tmp |
| 30 | + # set DB_PASSWORD equal to .services.mssql.environment.MSSQL_SA_PASSWORD, accounting for quotes |
| 31 | + DB_PASSWORD="$(grep -oP 'MSSQL_SA_PASSWORD=["'"'"']?\K[^"'"'"'\s]+' $DEV_DIR/.env)" |
| 32 | + CERT_OUTPUT="$(./create_certificates_linux.sh)" |
| 33 | + #shellcheck disable=SC2086 |
| 34 | + IDENTITY_SERVER_FINGERPRINT="$(echo $CERT_OUTPUT | awk -F 'Identity Server Dev: ' '{match($2, /[[:alnum:]]+/); print substr($2, RSTART, RLENGTH)}')" |
| 35 | + #shellcheck disable=SC2086 |
| 36 | + DATA_PROTECTION_FINGERPRINT="$(echo $CERT_OUTPUT | awk -F 'Data Protection Dev: ' '{match($2, /[[:alnum:]]+/); print substr($2, RSTART, RLENGTH)}')" |
| 37 | + SQL_CONNECTION_STRING="Server=localhost;Database=vault_dev;User Id=SA;Password=$DB_PASSWORD;Encrypt=True;TrustServerCertificate=True" |
| 38 | + echo "Identity Server Dev: $IDENTITY_SERVER_FINGERPRINT" |
| 39 | + echo "Data Protection Dev: $DATA_PROTECTION_FINGERPRINT" |
| 40 | + jq \ |
| 41 | + ".globalSettings.sqlServer.connectionString = \"$SQL_CONNECTION_STRING\" | |
| 42 | + .globalSettings.postgreSql.connectionString = \"Host=localhost;Username=postgres;Password=$DB_PASSWORD;Database=vault_dev;Include Error Detail=true\" | |
| 43 | + .globalSettings.mySql.connectionString = \"server=localhost;uid=root;pwd=$DB_PASSWORD;database=vault_dev\" | |
| 44 | + .globalSettings.identityServer.certificateThumbprint = \"$IDENTITY_SERVER_FINGERPRINT\" | |
| 45 | + .globalSettings.dataProtection.certificateThumbprint = \"$DATA_PROTECTION_FINGERPRINT\"" \ |
| 46 | + .secrets.json.tmp >secrets.json |
| 47 | + rm .secrets.json.tmp |
| 48 | + popd >/dev/null || exit |
| 49 | +} |
| 50 | + |
| 51 | +one_time_setup() { |
| 52 | + read -r -p \ |
| 53 | + "Would you like to configure your secrets and certificates for the first time? |
| 54 | +WARNING: This will overwrite any existing secrets.json and certificate files. |
| 55 | +Proceed? [y/N] " response |
| 56 | + if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then |
| 57 | + echo "Running one-time setup script..." |
| 58 | + sleep 1 |
| 59 | + read -r -p \ |
| 60 | + "Place the secrets.json and dev.pfx files from our shared Collection in the ./dev directory. |
| 61 | +Press <Enter> to continue." |
| 62 | + remove_comments ./dev/secrets.json |
| 63 | + configure_other_vars |
| 64 | + echo "Installing Az module. This will take ~a minute..." |
| 65 | + pwsh -Command "Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force" |
| 66 | + pwsh ./dev/setup_azurite.ps1 |
| 67 | + |
| 68 | + dotnet tool install dotnet-certificate-tool -g >/dev/null |
| 69 | + |
| 70 | + read -r -s -p "Paste the \"Licensing Certificate - Dev\" password: " CERT_PASSWORD |
| 71 | + echo |
| 72 | + pushd ./dev >/dev/null || exit |
| 73 | + certificate-tool add --file ./dev.pfx --password "$CERT_PASSWORD" |
| 74 | + echo "Injecting dotnet secrets..." |
| 75 | + pwsh ./setup_secrets.ps1 || true |
| 76 | + popd >/dev/null || exit |
| 77 | + |
| 78 | + echo "Running migrations..." |
| 79 | + sleep 5 # wait for DB container to start |
| 80 | + dotnet run --project ./util/MsSqlMigratorUtility "$SQL_CONNECTION_STRING" |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +# main |
| 85 | +one_time_setup |
0 commit comments