#!/usr/bin/env bash

# This script installs the unsecure Vagrant ssh keys. This allows users to
# log into the VMs using these keys instead of a password.

TOP_DIR=$(cd $(dirname "$0")/.. && pwd)
source "$TOP_DIR/config/paths"
source "$LIB_DIR/functions.guest"

indicate_current_auto

exec_logfile

function install_vagrant_public_key {
    local VAGRANT_KEY_NAME="vagrant.pub"
    local KEY_URL=https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/$VAGRANT_KEY_NAME
    local VAGRANT_KEY_DIR=$LIB_DIR/vagrant-ssh-keys

    if [ ! -f "$VAGRANT_KEY_DIR/$VAGRANT_KEY_NAME" ]; then
        wget --output-document "$VAGRANT_KEY_DIR/$VAGRANT_KEY_NAME" "$KEY_URL"
        if [ $? -ne 0 ]; then
            echo >&2 "Error when downloading $KEY_URL"
            return 1
        fi
    fi

    mkdir -p "$HOME/.ssh"
    chmod 700 "$HOME/.ssh"
    cat "$VAGRANT_KEY_DIR/$VAGRANT_KEY_NAME" >> "$HOME/.ssh/authorized_keys"
    chmod 400 "$HOME/.ssh/authorized_keys"
}

if grep -qs "vagrant insecure public key" "$HOME/.ssh/authorized_keys"; then
    echo "Vagrant insecure public key already installed"
else
    install_vagrant_public_key
fi

Allow osbash to enable Vagrant ssh keys
Allow osbash to enable Vagrant ssh keys

This script can be used to enable access to osbash VMs using the same
ssh key that is used to for logging into Vagrant VMs.

If the script is run from a directory shared with the host, the
downloaded key will be cached.

Partial-Bug: 1312764
Implements: blueprint openstack-training-labs
Change-Id: Ia541e01cabd69f794ea79d8a318f490f518d28ef
