Skip to content

Commit

Permalink
Fixes #16620: Add a command to generate user accounts after installation
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Apr 10, 2020
1 parent bbfbef2 commit 3ed4cb5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions share/commands/server-create-user
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
# @description create an admin user account
# @man This commands allows inserting a new user account. It is particularly useful
# @man to create the first admin account on the server. It requires that the
# @man authentication hash is bcrypt (default from fresh 6.1).
# @man +
# @man *Options*:
# @man +
# @man *-u*: specify the user name ("admin" by default)

. "${BASEDIR}/../lib/common.sh"

USERFILE="/opt/rudder/etc/rudder-users.xml"
USER="admin"

while getopts "u:" opt; do
case $opt in
u)
USER="${OPTARG}"
;;
esac
done

# check hash
if ! grep -qE "^[[:space:]]*<authentication[[:space:]]+hash=\"bcrypt\"[[:space:]]*>" "${USERFILE}"
then
echo "This command can only create users with the 'bcrypt' hash method"
exit 1
fi

# check if user is already there
if grep -qE "name[[:space:]]*=[[:space:]]*\"${USER}\"" "${USERFILE}"
then
echo "User '${USER}' already exists, aborting."
exit 1
fi

# bcrypt (12 cost)
hash=$(htpasswd -nBC 12 "" | tr -d ':\n')

details="<user name=\"${USER}\" password=\"${hash}\" role=\"administrator\" />"
sed -i "/^[[:space:]]*<\/authentication>/i ${details}" "${USERFILE}"

echo "User '${USER}' added to Rudder server"

0 comments on commit 3ed4cb5

Please sign in to comment.