forked from nmarus/docker-gitbox
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ng-auth.sh
183 lines (168 loc) · 5.01 KB
/
ng-auth.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
#nginx auth script for gitbox
#https://github.com/nmarus/docker-gitbox
#nmarus@gmail.com
set -e
USER="root"
GROUP="root"
MOD="644"
AUTH="/ng-auth"
QUIET=false
#SFLOG="/ng-auth.log"
#help text
showhelp() {
cat 1>&2 << EOF
╔═══════════════════════════════════════════════════════════════════╗
║ Usage: ║
╠═══════════════════════════════════════════════════════════════════╣
║ Validate: ng-auth -v ║
║ Reset to defaults: ng-auth -x ║
║ Add user: ng-auth -u <user-name> -p <password> ║
║ Change user password: ng-auth -u <user-name> -p <password> ║
║ Remove user: ng-auth -r <user-name> ║
╚═══════════════════════════════════════════════════════════════════╝
EOF
}
#print timestamp
timestamp() {
date +"%Y-%m-%d %T"
}
#screen/file logger
sflog() {
#if $1 is not null
if [ ! -z ${1+x} ]; then
message=$1
else
#exit function
return 1;
fi
#if $QUIET is not true
if ! $($QUIET); then
echo "${message}"
fi
#if $SFLOG is not null
if [ ! -z ${SFLOG+x} ]; then
#if $2 is regular file or does not exist
if [ -f ${SFLOG} ] || [ ! -e ${SFLOG} ]; then
echo "$(timestamp) ${message}" >> ${SFLOG}
fi
fi
}
#screen/file error logger
sferror() {
#if $1 is not null
if [ ! -z ${1+x} ]; then
message=$1
echo ""
sflog "ERROR: ${message}" >&2
echo ""
showhelp
fi
exit 1
}
#init auth
ng-auth-init() {
#if directory
if [ -d ${AUTH} ]; then
chown -R ${USER}:${GROUP} ${AUTH} &> /dev/null
#if $MOD is not null
if [ ! -z ${MOD+x} ]; then
find ${AUTH} -type f -exec chmod ${MOD} '{}' + &> /dev/null
fi
rm -f ${AUTH}/${ADMIN}.password &> /dev/null
#check if htpasswd exists
if [ ! -e ${AUTH}/htpasswd ]; then
sflog "Generating htpassed file and default account ${ADMIN}"
#generate random password
ADMINPASS=$(openssl rand -base64 8)
#store password to file
echo ${ADMINPASS} > ${AUTH}/${ADMIN}.password
#create htpasswd and set default creds
htpasswd -cb ${AUTH}/htpasswd ${ADMIN} ${ADMINPASS} &> /dev/null
sflog "The password for ${ADMIN} can be found at ${AUTH}/${ADMIN}.password"
sflog "This file will be deleted as soon as the next user is added..."
fi
else
sferror "${AUTH} not found"
fi
}
#reset auth
ng-auth-reset() {
ng-auth-init
#remove htpasswd file
rm -f ${AUTH}/htpasswd &> /dev/null
sflog "Removed ${AUTH}/htpasswd"
ng-auth-init
}
#add/edit user
ng-auth-user() {
ng-auth-init
#add user to htpasswd
htpasswd -b ${AUTH}/htpasswd ${GBUSER} ${GBPASS} &> /dev/null
sflog "The user ${GBUSER} has been set with password ${GBPASS}"
}
#remove user
ng-auth-remove() {
ng-auth-init
#remove user from htpasswd
htpasswd -D ${AUTH}/htpasswd ${REMOVE} &> /dev/null
sflog "The user ${REMOVE} has been removed"
}
#validate htpasswd
if ! $(which htpasswd &> /dev/null); then
sferror "htpasswd not found in path"
fi
#validate openssl
if ! $(which openssl &> /dev/null); then
sferror "openssl not found in path"
fi
#parse arguments
while getopts ":vxu:p:r:" opt; do
case "${opt}" in
v)
ng-auth-init
exit 0
;;
x)
ng-auth-reset
exit 0
;;
u)
if [[ ${OPTARG} =~ ^- ]]; then
sferror "Bad argument string"
fi
GBUSER=${OPTARG}
#if $GBPASS has already been parsed
if [ ! -z ${GBPASS+x} ]; then
ng-auth-user
exit 0
fi
;;
p)
if [[ ${OPTARG} =~ ^- ]]; then
sferror "Bad argument string"
fi
GBPASS=${OPTARG}
#if $GBUSER has already been parsed
if [ ! -z ${GBUSER+x} ]; then
ng-auth-user
exit 0
fi
;;
r)
if [[ ${OPTARG} =~ ^- ]]; then
sferror "Bad argument string"
fi
REMOVE=${OPTARG}
ng-auth-remove
exit 0
;;
esac
done
#check partial entry for -u -p
if [ ! -z ${USER+x} ] || [ ! -z ${DESC+x} ]; then
sferror "Both -u and -p must be specified"
fi
#default action if no cmd line args are found
showhelp
exit 0