2015-08-14 00:07:43 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
const (
|
2015-08-18 01:22:03 +00:00
|
|
|
// This is a constant representing a script to install and uninstall public
|
|
|
|
// key in remote hosts.
|
|
|
|
DefaultPublicKeyInstallScript = `
|
2015-08-14 00:07:43 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2015-08-24 17:50:46 +00:00
|
|
|
# This is a default script which installs or uninstalls an RSA public key to/from
|
2016-11-08 14:32:23 +00:00
|
|
|
# authorized_keys file in a typical linux machine.
|
|
|
|
#
|
2015-08-24 18:18:37 +00:00
|
|
|
# If the platform differs or if the binaries used in this script are not available
|
2016-07-01 18:31:37 +00:00
|
|
|
# in target machine, use the 'install_script' parameter with 'roles/' endpoint to
|
2015-08-24 18:18:37 +00:00
|
|
|
# register a custom script (applicable for Dynamic type only).
|
2015-08-14 00:07:43 +00:00
|
|
|
#
|
|
|
|
# Vault server runs this script on the target machine with the following params:
|
|
|
|
#
|
2015-08-24 18:07:07 +00:00
|
|
|
# $1:INSTALL_OPTION: "install" or "uninstall"
|
2015-08-14 00:07:43 +00:00
|
|
|
#
|
2015-08-24 18:18:37 +00:00
|
|
|
# $2:PUBLIC_KEY_FILE: File name containing public key to be installed. Vault server
|
|
|
|
# uses UUID as name to avoid collisions with public keys generated for other requests.
|
2015-08-14 00:07:43 +00:00
|
|
|
#
|
2015-08-24 18:07:07 +00:00
|
|
|
# $3:AUTH_KEYS_FILE: Absolute path of the authorized_keys file.
|
2015-08-18 01:22:03 +00:00
|
|
|
# Currently, vault uses /home/<username>/.ssh/authorized_keys as the path.
|
2015-08-14 00:07:43 +00:00
|
|
|
#
|
2015-08-24 18:18:37 +00:00
|
|
|
# [Note: This script will be run by Vault using the registered admin username.
|
|
|
|
# Notice that some commands below are run as 'sudo'. For graceful execution of
|
|
|
|
# this script there should not be any password prompts. So, disable password
|
|
|
|
# prompt for the admin username registered with Vault.
|
2015-08-24 17:50:46 +00:00
|
|
|
|
|
|
|
set -e
|
2015-08-14 00:07:43 +00:00
|
|
|
|
2015-08-24 18:18:37 +00:00
|
|
|
# Storing arguments into variables, to increase readability of the script.
|
2015-08-20 23:12:17 +00:00
|
|
|
INSTALL_OPTION=$1
|
|
|
|
PUBLIC_KEY_FILE=$2
|
|
|
|
AUTH_KEYS_FILE=$3
|
|
|
|
|
|
|
|
# Delete the public key file and the temporary file
|
|
|
|
function cleanup
|
|
|
|
{
|
2015-08-24 17:50:46 +00:00
|
|
|
rm -f "$PUBLIC_KEY_FILE" temp_$PUBLIC_KEY_FILE
|
2015-08-20 23:12:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 18:18:37 +00:00
|
|
|
# 'cleanup' will be called if the script ends or if any command fails.
|
2015-08-24 17:50:46 +00:00
|
|
|
trap cleanup EXIT
|
|
|
|
|
2015-08-24 18:18:37 +00:00
|
|
|
# Return if the option is anything other than 'install' or 'uninstall'.
|
2015-08-27 12:45:34 +00:00
|
|
|
if [ "$INSTALL_OPTION" != "install" ] && [ "$INSTALL_OPTION" != "uninstall" ]; then
|
2015-08-14 00:07:43 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-04-23 22:04:22 +00:00
|
|
|
# use locking to avoid parallel script execution
|
|
|
|
(
|
|
|
|
flock --timeout 10 200
|
|
|
|
# Create the .ssh directory and authorized_keys file if it does not exist
|
|
|
|
SSH_DIR=$(dirname $AUTH_KEYS_FILE)
|
|
|
|
sudo mkdir -p "$SSH_DIR"
|
|
|
|
sudo touch "$AUTH_KEYS_FILE"
|
|
|
|
# Remove the key from authorized_keys file if it is already present.
|
|
|
|
# This step is common for both install and uninstall. Note that grep's
|
|
|
|
# return code is ignored, thus if grep fails all keys will be removed
|
|
|
|
# rather than none and it fails secure
|
|
|
|
sudo grep -vFf "$PUBLIC_KEY_FILE" "$AUTH_KEYS_FILE" > temp_$PUBLIC_KEY_FILE || true
|
|
|
|
cat temp_$PUBLIC_KEY_FILE | sudo tee "$AUTH_KEYS_FILE"
|
|
|
|
# Append the new public key to authorized_keys file
|
|
|
|
if [ "$INSTALL_OPTION" == "install" ]; then
|
|
|
|
cat "$PUBLIC_KEY_FILE" | sudo tee --append "$AUTH_KEYS_FILE"
|
|
|
|
fi
|
|
|
|
) 200> ${AUTH_KEYS_FILE}.lock
|
2015-08-14 00:07:43 +00:00
|
|
|
`
|
|
|
|
)
|