generated from ddev/ddev-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotect
executable file
·75 lines (69 loc) · 2.16 KB
/
protect
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
#!/usr/bin/env bash
#ddev-generated
## Description: Enable or disable basic auth on a nixsal hosted dev project
## Usage: protect [off|on|reset]
## Example: "ddev protect on"
# Helpers ====================================
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[1;33m'
NC='\033[0m'
echo-red () { echo -e "${red}$1${NC}"; }
echo-green () { echo -e "${green}$1${NC}"; }
echo-yellow () { echo -e "${yellow}$1${NC}"; }
# End Helpers ====================================
# Vars
PROTECT_CONFIG=${DDEV_APPROOT}/.ddev/nginx/protect.conf
PROTECT_FILE=${DDEV_APPROOT}/.ddev/.passwd
PROTECT_USER=$(whoami)
if [ ! $ANNERTECH_LOCAL_DEV ]; then
echo-yellow "========================================="
echo-yellow "= No changes have been made because ="
echo-yellow "= this command requires a nixOS server. ="
echo-yellow "========================================="
exit 0
fi
function check_protect_file() {
## Check if there's a passwd file
if [ ! -f $PROTECT_FILE ]; then
## No file. Let's create one
echo "In order to use this script we need to generate a password file."
if read -t 10 -n 1 -sp "Press enter to create one or wait 10 seconds to exit...\n" CREATE; then
nix-shell -p apacheHttpd --run "htpasswd -c $PROTECT_FILE $PROTECT_USER"
else
echo-yellow "\n\n No file created"
exit 0
fi
fi
}
# Command logic
case $@ in
off)
check_protect_file
# Comment-out the protect.conf lines starting with 'auth'
echo-yellow "Disabling basic auth protection"
sed -i '/auth/s/^/#/g' $PROTECT_CONFIG
echo-green "Restarting ddev..."
ddev restart
;;
on)
check_protect_file
# Uncomment lines starting with 'auth'
echo-yellow "Enabling basic auth protection"
sed -i '/auth/s/^#//g' $PROTECT_CONFIG
echo-green "Restarting ddev..."
ddev restart
;;
reset)
echo-yellow "Reseting basic auth password"
if [ -f $PROTECT_FILE ]; then
nix-shell -p apacheHttpd --run "htpasswd $PROTECT_FILE $PROTECT_USER"
else
check_protect_file
fi
echo-green "Restarting ddev..."
ddev restart
;;
*)
echo "Please enter an action when running this command: e.g. on, off or reset"
esac