diff --git a/scripts/set-up-authoritative-keyserver.sh b/scripts/set-up-authoritative-keyserver.sh new file mode 100755 index 000000000..0dba91652 --- /dev/null +++ b/scripts/set-up-authoritative-keyserver.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +PRJ_ROOT="$(git rev-parse --show-toplevel)" + +user_credentials_file="$PRJ_ROOT/keyserver/secrets/user_credentials.json" + +set_up_or_abort() { + read -r -p "Do you want to set up a new authoritative keyserver? (y/N) " user_input + + if [[ $user_input != "Y" && $user_input != "y" ]]; then + exit 1 + fi + + if ! (mysql -u "$USER" -Bse "USE comm;" 2>/dev/null); then + echo "Database comm doesn't exist. Please re-enter 'nix develop'" + exit 1 + fi + + num_of_tables=$(mysql -u "$USER" -Bse "USE comm; SHOW TABLES;" | wc -l); + + if [[ "$num_of_tables" -gt 0 ]]; then + # Create backup db and move all tables from comm + timestamp=$(date +%s) + db_version_name="comm_backup$timestamp" + echo "backup db name: $db_version_name" + + mysql -u "$USER" -Bse "CREATE DATABASE $db_version_name;"\ + -Bse "GRANT ALL ON $db_version_name"'.*'" TO comm@localhost;" + + for table in $(mysql -u "$USER" -Bse "USE comm; SHOW TABLES FROM comm;"); do + mysql -u "$USER" -Bse "USE comm; RENAME TABLE comm.$table TO $db_version_name.$table;"; + done; + fi + + node "$PRJ_ROOT"/scripts/set-user-credentials.js "$PRJ_ROOT" +} + +if grep -q '"usingIdentityCredentials":.*true' "$user_credentials_file"; then + if ! (mysql -u "$USER" -Bse "USE comm; SELECT * FROM metadata" 2>/dev/null | grep "db_version">/dev/null); then + echo -e "'usingIdentityCredentials' is set to true, but the database is not set up.\n" \ + "This was likely caused by the keyserver failing to login with the provided credentials,"\ + "or the keyserver never being run" + set_up_or_abort + fi +else + echo "'usingIdentityCredentials' is missing or set to false in user_credentials.json." + set_up_or_abort +fi diff --git a/scripts/set-user-credentials.js b/scripts/set-user-credentials.js new file mode 100644 index 000000000..839e6c2ff --- /dev/null +++ b/scripts/set-user-credentials.js @@ -0,0 +1,52 @@ +/* eslint-disable flowtype/require-valid-file-annotation */ + +const basePath = process.argv[2]; + +const fs = require('fs'); +const readline = require('readline'); + +// Create an interface for reading input +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const data = {}; + +rl.question('username: ', value1 => { + data.username = value1; + + rl.question('password: ', value2 => { + data.password = value2; + + writeFiles(data); + + // Close the readline interface + rl.close(); + }); +}); + +function writeFiles(credentials) { + try { + const userCredentials = { + username: credentials.username, + password: credentials.password, + usingIdentityCredentials: true, + }; + const userCredentialsJSON = JSON.stringify(userCredentials, null, 2); + const keyserverSecrets = `${basePath}/keyserver/secrets`; + if (!fs.existsSync(keyserverSecrets)) { + fs.mkdirSync(keyserverSecrets); + } + fs.writeFileSync( + `${keyserverSecrets}/user_credentials.json`, + userCredentialsJSON, + ); + } catch (e) { + console.error( + 'Failure creating configuration files: ' + + 'admin data could not be correctly written', + ); + throw e; + } +}