Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8040dff
add shellcheck action for pull requests
mhitza Feb 25, 2021
4b6630f
switch key generation to ed25519 instead of RSA
mhitza Mar 5, 2021
ac6997d
use dev as default env, allow env var to set default env, update scri…
Mar 6, 2021
1aa2b6b
dumping host var secrets - has brought in some duplication - step in …
Mar 6, 2021
f67e5fb
ignore source calls in shellcheck
mhitza Mar 8, 2021
b689546
test shellcheck external-sources flag
mhitza Mar 8, 2021
f9b186a
drop id_rsa suffix when generating SSH Key pairs
mhitza Mar 8, 2021
ffb9b37
use different shellcheck action/run single testsuite for PR&master br…
mhitza Mar 8, 2021
4ab3fdf
fix workflow syntax issue
mhitza Mar 8, 2021
9245bf5
add shellcheck severity level info
mhitza Mar 8, 2021
96211bd
use shellcheck already installed within runner
mhitza Mar 8, 2021
9b11fb5
tyring to make shellcheck happy
Mar 8, 2021
e9200bc
some shell check fixes
Mar 8, 2021
d9323df
don't pass directly to shellcheck file that are sourced
mhitza Mar 8, 2021
806ba33
Merge pull request #7 from LongTermSupport/shellcheck-checks-on-pr
LTSCommerce Mar 8, 2021
314bdad
Merge pull request #8 from LongTermSupport/switch-to-ed25519
LTSCommerce Mar 8, 2021
bbeb335
Merge pull request #9 from LongTermSupport/DefaultEnv
LTSCommerce Mar 8, 2021
323c3e2
dumping host var secrets - has brought in some duplication - step in …
Mar 6, 2021
145fc27
tyring to make shellcheck happy
Mar 8, 2021
e78e968
some shell check fixes
Mar 8, 2021
7f714a1
Merge branch 'HostVarSecrets' of github.com:LongTermSupport/ansible-r…
Mar 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---
name: CI
on:
- push
- pull_request
pull_request:
push:
branches:
- master

jobs:
test:
Expand All @@ -17,6 +19,21 @@ jobs:
- name: Run Molecule tests
uses: robertdebock/molecule-action@2.1.0

shellcheck:
name: Shellcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# XXX: we ignore the bash files that are included during execution from the list
# files passed to shellcheck. See https://unix.stackexchange.com/questions/164025/exclude-one-pattern-from-glob-match
# This still allows shellcheck to analyze them, but we don't pass them to shellcheck
# as individual shell scripts; to avoid it from complaining of undefined variables
- name: shellcheck
run: |
cd shellscripts
GLOBIGNORE="_*.inc.bash"
shellcheck -x *

release:
name: Release
needs:
Expand All @@ -28,3 +45,4 @@ jobs:
uses: robertdebock/galaxy-action@1.0.3
with:
galaxy_api_key: ${{ secrets.ansible_galaxy_api_key }}

37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,33 @@ It is suggested that whilst these are limitations, it is not necessarily a bad s

For each script, execute without arguments to get usage instructions

### Default and Specified Environment

For all actions, an environment name is required. This will default to `dev`.

You can override this on a per script call basis by passing the env name for the `specifiedEnv` parameter, for example:

```bash
bash shellscripts/vault/generateVaultSecret.bash prod
```

Alternatively, you can `export vaultScriptsDefaultEnv='prod'` to define a default environment for your current session, for example
```bash
export vaultScriptsDefaultEnv=prod

bash shellscripts/vault/generateVaultSecret.bash

bash shellscripts/vault/createVaultedPassword.bash vault_pass_user_foo ./environment/$vaultScriptsDefaultEnv/group_vars/all/vault_user_passwords.yml

bash shellscripts/vault/createVaultedSshKeyPair.bash vault_default ops@domain.com ./environment/$vaultScriptsDefaultEnv/group_vars/all/vault_ssh_keys.yml
```

### Generate Vault Secret

This script will generate a secret file for you with a good long chunk of random text

```bash
bash shellscripts/vault/generateVaultSecret.bash dev
bash shellscripts/vault/generateVaultSecret.bash
```
eg `vault-pass-dev.secret`
```
Expand All @@ -81,14 +102,14 @@ For example, in the `dev` environment, generate a password and vault it, then wr

```bash

bash shellscripts/vault/createVaultedPassword.bash dev vault_pass_user_foo ./environment/dev/group_vars/all/vault_user_passwords.yml
bash shellscripts/vault/createVaultedPassword.bash vault_pass_user_foo ./environment/dev/group_vars/all/vault_user_passwords.yml

```

As above, but instead of writing to file, just write to stdout so that you can copy paste it manually where ever you want
```bash

bash shellscripts/vault/createVaultedPassword.bash dev vault_pass_user_foo
bash shellscripts/vault/createVaultedPassword.bash vault_pass_user_foo

```

Expand All @@ -99,7 +120,7 @@ If you need to encrypt a password that is predefined or has specific requirement
For example, if we need a shorter password than the standard one:

```bash
bash shellscripts/vault/createVaultedString.bash prod vault_pass_user_foo "$(bash shellscripts/vault/generatePassword.bash 20)"
bash shellscripts/vault/createVaultedString.bash vault_pass_user_foo "$(bash shellscripts/vault/generatePassword.bash 20)"
```

### Create Vaulted SSH Key Pair
Expand All @@ -111,10 +132,10 @@ For example
```bash

# echo to stdout
bash shellscripts/vault/createVaultedSshKeyPair.bash dev vault_default ops@domain.com
bash shellscripts/vault/createVaultedSshKeyPair.bash vault_default ops@domain.com

# write directly to file
bash shellscripts/vault/createVaultedSshKeyPair.bash dev vault_default ops@domain.com ./environment/dev/group_vars/all/vault_ssh_keys.yml
bash shellscripts/vault/createVaultedSshKeyPair.bash vault_default ops@domain.com ./environment/dev/group_vars/all/vault_ssh_keys.yml

```

Expand Down Expand Up @@ -145,13 +166,13 @@ For this, you can use this script
For example, to view all secrets in the dev environment

```bash
bash shellscripts/vault/dumpSecrets.bash dev
bash shellscripts/vault/dumpSecrets.bash
```

Or you can dump a single secret:

```bash
bash shellscripts/vault/dumpSecrets.bash dev vault_root_pass
bash shellscripts/vault/dumpSecrets.bash vault_root_pass
```

Example output:
Expand Down
8 changes: 8 additions & 0 deletions shellscripts/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Look for 'source'd files relative to the checked script,
source-path=SCRIPTDIR

# bash only
shell=bash

# Turn on warnings for unassigned uppercase variables
enable=check-unassigned-uppercase
6 changes: 3 additions & 3 deletions shellscripts/_top.inc.bash
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ $(hostname &>/dev/null || echo 'no hostname set') $0 $@
"
fi

# assumes scriptDir is shellscripts/vault
#readonly projectDir="$(dirname "$( dirname "$scriptDir")")"
readonly projectDir="$(findAnsibleCfgDir)"

if [[ ! -f $projectDir/ansible.cfg ]]; then
Expand All @@ -169,4 +167,6 @@ if [[ ! -f $projectDir/ansible.cfg ]]; then

"
exit 1
fi
fi

readonly defaultEnv="${vaultScriptsDefaultEnv:-dev}"
2 changes: 0 additions & 2 deletions shellscripts/_vault.functions.inc.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


function assertValidEnv(){
local _env="$1"
if [[ "${environmentArray[*]}" == *${_env}* ]]; then
Expand Down
4 changes: 4 additions & 0 deletions shellscripts/_vault.inc.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#Bring in functions

# shellcheck source=./_top.inc.bash

# shellcheck source=./_vault.functions.inc.bash
source ./_vault.functions.inc.bash

# Basic sanity checks
Expand Down
16 changes: 7 additions & 9 deletions shellscripts/createPasswordsFromTemplate.bash
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#!/usr/bin/env bash
readonly scriptDir="$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)"
cd "$scriptDir"
# Set up bash
source ./_top.inc.bash
readonly scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
cd "$scriptDir" || exit 1

# Usage
if (( $# < 2 ))
if (( $# < 1 ))
then
echo "

This script will allow you to create a vaulted string that is the contents of the specified file, then optionally add it to the file you specify

Usage ./$(basename $0) [specifiedEnv] [pathToFileToParseVarsFrom] (outputToFile)
Usage ./$(basename $0) [pathToFileToParseVarsFrom] (optional: outputToFile) (optional: specifiedEnv - defaults to $defaultEnv)

e.g

Expand All @@ -22,9 +20,9 @@ e.g
fi

# Set variables
readonly specifiedEnv="$1"
readonly pathToFileToParseVarsFrom="$(getFilePath $2)"
readonly outputToFile="$(getFilePathOrEmptyString "${3:-}")"
readonly pathToFileToParseVarsFrom="$(getFilePath $1)"
readonly outputToFile="$(getFilePathOrEmptyString "${2:-}")"
readonly specifiedEnv="${3:-$defaultEnv}"

# Source vault top
source ./_vault.inc.bash
Expand Down
15 changes: 7 additions & 8 deletions shellscripts/createVaultedDataFromFile.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
readonly scriptDir="$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)"
cd "$scriptDir"
readonly scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
cd "$scriptDir" || exit 1
# Set up bash
source ./_top.inc.bash

Expand All @@ -11,8 +11,7 @@ then

This script will allow you to create a vaulted string that is the contents of the specified file, then optionally add it to the file you specify

Usage ./$(basename $0) [specifiedEnv] [varname] [path to file] (output to file, defaults to
dev/null)
Usage ./$(basename $0) [varname] [path to file] (optional: output to file, defaults to dev/null) (optional: specifiedEnv - defaults to $defaultEnv)

e.g

Expand All @@ -25,10 +24,10 @@ Please note, the varname must be prefixed with 'vault_'
fi

# Set variables
readonly specifiedEnv="$1"
readonly varname="$2"
readonly pathToFileToEncrypt="$3"
readonly outputToFile="$(getFilePathOrEmptyString "${4:-}")"
readonly varname="$1"
readonly pathToFileToEncrypt="$2"
readonly outputToFile="$(getFilePathOrEmptyString "${3:-}")"
readonly specifiedEnv="${4:-$defaultEnv}"

# Source vault top
source ./_vault.inc.bash
Expand Down
15 changes: 8 additions & 7 deletions shellscripts/createVaultedPassword.bash
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/usr/bin/env bash
readonly scriptDir="$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)"
cd "$scriptDir"
readonly scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
cd "$scriptDir" || exit 1
# Set up bash
source ./_top.inc.bash

# Usage
if (($# < 2 || $# > 3)); then
if (($# < 1 || $# > 3)); then
echo "
Usage:

This script will generate a random password and encrypt it, then optionally add it to the file you specify

Please note, the varname must be prefixed with 'vault_'

$(basename $0) [specifiedEnv] [varname] (optional: outputToFile)
$(basename $0) [varname] (optional: outputToFile) (optional: specifiedEnv - defaults to $defaultEnv)



Expand All @@ -22,9 +22,10 @@ if (($# < 2 || $# > 3)); then
fi

# Set variables
readonly specifiedEnv="$1"
readonly varname="$2"
readonly outputToFile="$(getProjectFilePathCreateIfNotExists "${3:-}")"
readonly varname="$1"
readonly outputToFile="$(getProjectFilePathCreateIfNotExists "${2:-}")"
readonly specifiedEnv="${3:-$defaultEnv}"

readonly password='=+'"$(scriptDir/generatePassword.bash)"

# Source vault top
Expand Down
38 changes: 19 additions & 19 deletions shellscripts/createVaultedSshKeyPair.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
readonly scriptDir="$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)"
cd "$scriptDir"
readonly scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
cd "$scriptDir" || exit 1
# Set up bash
source ./_top.inc.bash

Expand All @@ -11,7 +11,7 @@ USAGE:

This script will generate a random password and then an SSH key pair protected by that password, then optionally add it to the file you specify. Finally it can either leave the generated key files in place or otherwise will delete them

Usage ./$(basename $0) [specifiedEnv] [varname_prefix] [email] (optional: outputToFile) (optional: keepKeys)
Usage ./$(basename $0) [varname_prefix] [email] (optional: outputToFile) (optional: specifiedEnv - defaults to $defaultEnv) (optional: keepKeys)

Please note, the varname_prefix must start with 'vault_'

Expand All @@ -21,24 +21,24 @@ e.g

To generate a private and public key with variables

github_id_rsa
github_id_rsa_pub
github
github_pub

"
}

# Usage
if (( $# < 3 ))
if (( $# < 2 ))
then
usage
exit 1
fi

# Set variables
readonly specifiedEnv="$1"
readonly varname_prefix="$2"
readonly email="$3"
readonly outputToFile="$(getProjectFilePathCreateIfNotExists "${4:-}")"
readonly varname_prefix="$1"
readonly email="$2"
readonly outputToFile="$(getProjectFilePathCreateIfNotExists "${3:-}")"
readonly specifiedEnv="${4:-$defaultEnv}"
readonly keepKeys="${5:-no}"

# Source vault top
Expand All @@ -64,7 +64,7 @@ esac
readonly password='=+'"$(openssl rand -base64 32)"

#Write out encrypted Password
readonly varname="${varname_prefix}_id_rsa_passphrase"
readonly varname="${varname_prefix}_passphrase"
encrypted="$(echo -n "$password" | ansible-vault encrypt_string \
--vault-id="$specifiedEnv@$vaultSecretsPath" \
--stdin-name $varname)"
Expand All @@ -74,20 +74,20 @@ writeEncrypted "$encrypted" "$varname" "$outputToFile"
workDir=/tmp/_keys
rm -rf $workDir
mkdir $workDir
ssh-keygen -t rsa -b 4096 -C "$email" -N "$password" -f $workDir/${varname_prefix}_id_rsa
ssh-keygen -t ed25519 -C "$email" -N "$password" -f $workDir/${varname_prefix}

# Write Variables
encryptedPrivKey="$(cat "$workDir/${varname_prefix}_id_rsa" | ansible-vault encrypt_string \
encryptedPrivKey="$(cat "$workDir/${varname_prefix}" | ansible-vault encrypt_string \
--vault-id="$specifiedEnv@$vaultSecretsPath" \
--stdin-name "${varname_prefix}_id_rsa")"
--stdin-name "${varname_prefix}")"

writeEncrypted "$encryptedPrivKey" "${varname_prefix}_id_rsa" "$outputToFile"
writeEncrypted "$encryptedPrivKey" "${varname_prefix}" "$outputToFile"

encryptedPubKey="$(cat "$workDir/${varname_prefix}_id_rsa.pub" | ansible-vault encrypt_string \
encryptedPubKey="$(cat "$workDir/${varname_prefix}.pub" | ansible-vault encrypt_string \
--vault-id="$specifiedEnv@$vaultSecretsPath" \
--stdin-name "${varname_prefix}_id_rsa_pub")"
--stdin-name "${varname_prefix}_pub")"

writeEncrypted "$encryptedPubKey" "${varname_prefix}_id_rsa_pub" "$outputToFile"
writeEncrypted "$encryptedPubKey" "${varname_prefix}_pub" "$outputToFile"

# Clean up
if [[ "yes" == "$keepKeys" ]]; then
Expand All @@ -96,4 +96,4 @@ if [[ "yes" == "$keepKeys" ]]; then
printf "\n\n\n"
exit 0
fi
rm -rf $workDir
rm -rf $workDir
Loading