-
Notifications
You must be signed in to change notification settings - Fork 53
/
create_passwords.sh
executable file
·30 lines (26 loc) · 1.16 KB
/
create_passwords.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
# This script will create a random password per user and store it in the keyvault under a secret named <user>-password
AZHOP_CONFIG=config.yml
ANSIBLE_VARIABLES=playbooks/group_vars/all.yml
if [ ! -e $AZHOP_CONFIG ]; then
echo "$AZHOP_CONFIG doesn't exist, exiting"
exit 1
fi
key_vault=$(yq eval '.key_vault' $ANSIBLE_VARIABLES)
if [ "$key_vault" == "" ]; then
echo "Keyvault retrieved from $ANSIBLE_VARIABLES is empty"
exit 1
fi
users=$(yq eval '.users[].name' $AZHOP_CONFIG)
for user in $users; do
# Because secret names are restricted to '^[0-9a-zA-Z-]+$' we need to remove all other characters
secret_name=$(echo $user-password | tr -dc 'a-zA-Z0-9-')
current_password=$(az keyvault secret list --vault-name $key_vault --query "[?name=='$secret_name'].name" -o tsv)
if [ "$current_password" == "" ]; then
password=$(openssl rand -base64 20)
az keyvault secret set --value $password --name $secret_name --vault-name $key_vault -o table > /dev/null
echo "Generating a password for $user and storing it as secret $secret_name in keyvault $key_vault"
else
echo "User $user has already a password stored in keyvault $key_vault"
fi
done