-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #16620: Add a command to generate user accounts after installation
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|