-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
internal-functions
executable file
·59 lines (47 loc) · 1.7 KB
/
internal-functions
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
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
fn-http-auth-enabled() {
declare desc="check if an app has http-auth enabled"
declare APP="$1"
local APP_ROOT="$DOKKU_ROOT/$APP"
local enabled=false
if [[ -f "$APP_ROOT/nginx.conf.d/http-auth.conf" ]]; then
enabled=true
fi
echo "$enabled"
}
fn-http-auth-add-user() {
declare APP="$1" AUTH_USERNAME="$2" AUTH_PASSWORD="$3"
local APP_ROOT="$DOKKU_ROOT/$APP"
touch "$APP_ROOT/htpasswd"
sed -i "/^${AUTH_USERNAME}:/d" "$APP_ROOT/htpasswd"
HASHED_PASSWORD=$(mkpasswd -m sha-512 "$AUTH_PASSWORD")
echo "$AUTH_USERNAME:$HASHED_PASSWORD" >>"$APP_ROOT/htpasswd"
}
fn-http-auth-list-users() {
declare APP="$1"
local APP_ROOT="$DOKKU_ROOT/$APP"
touch "$APP_ROOT/htpasswd"
while IFS="" read -r line || [[ -n "$line" ]]; do
printf '%s\n' "$line" | cut -d':' -f1
done <"$APP_ROOT/htpasswd"
}
fn-http-auth-remove-user() {
declare APP="$1" AUTH_USERNAME="$2"
local APP_ROOT="$DOKKU_ROOT/$APP"
touch "$APP_ROOT/htpasswd"
sed -i "/^${AUTH_USERNAME}:/d" "$APP_ROOT/htpasswd"
}
fn-http-auth-template-config() {
declare APP="$1"
local APP_ROOT="$DOKKU_ROOT/$APP"
local DOKKU_TEMPLATE="$PLUGIN_AVAILABLE_PATH/http-auth/templates/http-auth.conf"
local HAS_ALLOWED_USERS="$(test -s $APP_ROOT/htpasswd && echo "true")"
local ALLOWED_IPS
ALLOWED_IPS="$(fn-plugin-property-list-get "http-auth" "$APP" "allowed-ips" | xargs)"
mkdir -p "$APP_ROOT/nginx.conf.d"
sigil -f "$DOKKU_TEMPLATE" APP_ROOT="$APP_ROOT" ALLOWED_IPS="$ALLOWED_IPS" HAS_ALLOWED_USERS="$HAS_ALLOWED_USERS" | cat -s >"$APP_ROOT/nginx.conf.d/http-auth.conf"
touch "$APP_ROOT/htpasswd"
}