-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·235 lines (197 loc) · 4.8 KB
/
install.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
set -u
# First check if the OS is Linux.
if [[ "$(uname)" = "Linux" ]]; then
ON_LINUX=1
fi
if [[ -z "${ON_LINUX-}" ]]; then
STAT="stat -f"
CHOWN="/usr/sbin/chown"
CHGRP="/usr/bin/chgrp"
GROUP="admin"
TOUCH="/usr/bin/touch"
else
STAT="stat --printf"
CHOWN="/bin/chown"
CHGRP="/bin/chgrp"
GROUP="$(id -gn)"
TOUCH="/bin/touch"
fi
JSON_CONFIG=
ACTION="install"
PDP_ADDR=
DEBUG=""
CONFIG="pdp_config.json"
LOCAL_CONFIG="/etc/docker/$CONFIG"
DOCKERD_CONFIG="/etc/docker/daemon.json"
PLUGIN_VERSION="v0.4"
# TODO: support osx
usage()
{
echo "usage: install.sh [-d] [-c] [-p] [-u] [-h]"
}
while [ $# -ge 1 ] && [ "$1" != "" ]; do
case $1 in
-p | --pdp-addr ) shift
PDP_ADDR=$1
;;
-c | --config ) shift
CONFIG=$1
;;
-u | --uninstall ) ACTION="uninstall"
;;
-d | --debug ) DEBUG="-debug"
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
docker_plugin_install() {
pdp_config
execute "docker" "plugin" "install" "buildsecurity/pdp-docker-authz:${PLUGIN_VERSION}" "pdp-args=-config-file ${CONFIG} ${DEBUG}"
docker_plugin_config
docker_config_restart
}
pdp_config() {
if [[ ! -f "$LOCAL_CONFIG" ]]; then
write_file $LOCAL_CONFIG "{}"
fi
if [[ ! -z "$PDP_ADDR" ]]; then
JSON_CONFIG=$(jq ".\"pdp_addr\" = \"$PDP_ADDR\"" $LOCAL_CONFIG)
write_file $LOCAL_CONFIG "$JSON_CONFIG"
fi
}
docker_plugin_config() {
if [[ ! -f "$DOCKERD_CONFIG" ]]; then
write_file $DOCKERD_CONFIG "{}"
fi
JSON_CONFIG=$(jq '."authorization-plugins" += ["buildsecurity/pdp-docker-authz:'${PLUGIN_VERSION}'"]' $DOCKERD_CONFIG)
if [ $? -ne 0 ]; then
abort "jq failure"
fi
write_file $DOCKERD_CONFIG "$JSON_CONFIG"
}
docker_plugin_uninstall() {
docker_plugin_remove_config
docker_config_restart
execute "docker" "plugin" "rm" "-f" "buildsecurity/pdp-docker-authz:${PLUGIN_VERSION}"
}
docker_plugin_remove_config() {
JSON_CONFIG=$(jq 'del(."authorization-plugins"[] | select(. | startswith("buildsecurity/pdp-docker-authz")))' $DOCKERD_CONFIG)
if [ $? -ne 0 ]; then
abort "jq failure"
fi
write_file $DOCKERD_CONFIG "$JSON_CONFIG"
}
docker_config_restart() {
if [[ -z "${ON_LINUX-}" ]]; then
abort "Please restart your docker daemon"
else
execute_sudo "kill" "-HUP" "$(pidof dockerd)"
fi
}
have_sudo_access() {
local -a args
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A")
fi
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
if [[ -n "${args[*]-}" ]]; then
/usr/bin/sudo "${args[@]}" -l mkdir &>/dev/null
else
/usr/bin/sudo -l mkdir &>/dev/null
fi
HAVE_SUDO_ACCESS="$?"
fi
if [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then
abort "Need sudo access (e.g. the user $USER to be an Administrator)!"
fi
return "$HAVE_SUDO_ACCESS"
}
write_file() {
DATA=$2
# TODO: @Q is supproted from bash 4 - on osx the bash version is too old
execute_sudo "bash" "-c" "echo ${DATA@Q} | tee $1"
}
# string formatters
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
TTY_UNDERLINE="$(tty_escape "4;39")"
TTY_BLUE="$(tty_mkbold 34)"
TTY_RED="$(tty_mkbold 31)"
TTY_BOLD="$(tty_mkbold 39)"
TTY_RESET="$(tty_escape 0)"
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
ohai() {
printf "${TTY_BLUE}==>${TTY_BOLD} %s${TTY_RESET}\n" "$(shell_join "$@")"
}
warn() {
printf "${TTY_RED}Warning${TTY_RESET}: %s\n" "$(chomp "$1")"
}
abort() {
printf "%s\n" "$1"
exit 1
}
execute() {
if ! "$@"; then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
execute_sudo() {
local -a args=("$@")
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A" "${args[@]}")
fi
if have_sudo_access; then
ohai "/usr/bin/sudo" "${args[@]}"
execute "/usr/bin/sudo" "${args[@]}"
else
ohai "${args[@]}"
execute "${args[@]}"
fi
}
getc() {
local save_state
save_state=$(/bin/stty -g)
/bin/stty raw -echo
IFS= read -r -n 1 -d '' "$@"
/bin/stty "$save_state"
}
wait_for_user() {
local c
echo
echo "Press RETURN to continue or any other key to abort"
getc c
# we test for \r and \n because some stuff does \r instead
if ! [[ "$c" == $'\r' || "$c" == $'\n' ]]; then
exit 1
fi
}
have_sudo_access
if ! [ -x "$(command -v jq)" ]; then
abort "Please install jq first"
fi
if [ $ACTION == "install" ]; then
docker_plugin_install
elif [ $ACTION == "uninstall" ]; then
docker_plugin_uninstall
fi