-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
commands
executable file
·268 lines (228 loc) · 7.42 KB
/
commands
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
APP=$2
SHARED_KEY_TO_ADD=$2
SHARED_KEY_TO_ADD_APP=$3
HOSTNAME_TO_ADD=$2
HOSTNAME_TO_ADD_APP=$3
HOSTNAME_TO_REMOVE=$2
HOSTNAME_TO_REMOVE_APP=$3
APP_SPECIFIC_HOSTKEYS_FOLDER="$DOKKU_ROOT/.hostkeys/$APP/.ssh"
APP_SPECIFIC_HOSTKEYS_FILE="$APP_SPECIFIC_HOSTKEYS_FOLDER/known_hosts"
SHARED_HOSTKEYS_FOLDER="$DOKKU_ROOT/.hostkeys/shared/.ssh"
SHARED_HOSTKEYS_FILE="$SHARED_HOSTKEYS_FOLDER/known_hosts"
check_exists() {
if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then
echo "App must exist before managing hostkeys for it"
exit 1
fi
}
check_app() {
if [[ -z "$APP" ]]; then
echo "You must specify an app name"
exit 1
fi
}
check_install() {
if [[ ! -d "$SHARED_HOSTKEYS_FOLDER" ]]; then
echo "No shared keys folder available. Did you run 'dokku plugins-install'? Exiting."
exit 1
fi
if [[ ! -f "$SHARED_HOSTKEYS_FILE" ]]; then
echo "No shared keys file available. Did you run 'dokku plugins-install'? Exiting."
exit 1
fi
}
check_install_app() {
if [[ ! -d "$APP_SPECIFIC_HOSTKEYS_FOLDER" ]]; then
echo "No app specific keys folder available. creating.."
mkdir -p "$APP_SPECIFIC_HOSTKEYS_FOLDER"
chmod 700 "$APP_SPECIFIC_HOSTKEYS_FOLDER"
chown -R dokku:dokku "$DOKKU_ROOT/.hostkeys/$APP"
fi
if [[ ! -f "$APP_SPECIFIC_HOSTKEYS_FILE" ]]; then
echo "No app specific keys file available. creating.."
touch "$APP_SPECIFIC_HOSTKEYS_FILE"
chmod 644 "$APP_SPECIFIC_HOSTKEYS_FILE"
chown -R dokku:dokku "$DOKKU_ROOT/.hostkeys/$APP"
fi
}
print_explanation() {
cat<<EOF
The hostkeys plugin manages the known_hosts file for your apps.
You need those known_hosts files, when you want to open a SSH connection to a foreign host,
such as when compiling the app and pulling in dependencies.
After adding hostkeys to your Dokku host/app, they will automatically be baked in on the
next time you compile your app.
There are 2 types of keys:
1.) Shared Keys
Shared keys are valid for all your apps on the Dokku host. You may probably want to add
some popular hosts in there such as BitBucket, GitHub or even your private VCS that is reachable.
2.) App-Level Hostkeys
App-Level Hostkeys may be needed for external dependencies which you licensed, or that reside
on a different host
Auto-Adding Hostkeys
--------------------
There is another possibility to add host keys, if you do not want to enter a hostkey manually:
You can autoadd hosts. You provide the hostname and your Dokku host will resolve it for you.
(THIS IS NOT A GOOD PRACTICE!)
You should only do this if you are 100% sure your DNS is not compromised.
$ dokku hostkeys:shared:autoadd github.com
This command would automatically discover the hostkeys for github.com, add it to the shared
known_hosts file and add it to your apps slug on recompile.
$ dokku hostkeys:shared:autoadd mycoolapp github.com
This command would automatically discover the hostkeys for github.com, add it to your known_hosts
file for the mycoolapp app and will be compiled inside the slug on recompile.
You may as well want to have a look at the dokku-deployment-keys plugin on GitHub:
http://github.com/cedricziel/dokku-deployment-keys
Projects are kept separate because they each do one different thing.
Available Commands:
-------------------
EOF
}
print_help() {
cat<<EOF
hostkeys, Print an explanation (Useful to get the concept)
hostkeys:shared:show, Show shared hostkeys
hostkeys:shared:add, Add a shared hostkey
hostkeys:shared:delete, Deletes all shared hostkeys
hostkeys:shared:delete <hostname>, Deletes the shared hostkeys for the given host
hostkeys:shared:autoadd <hostname>, Automatically add hostkeys for a given host to the shared hostkeys
hostkeys:app:show <app>, Show all hostkeys for a given app
hostkeys:app:add <app>, Add a app-specific hostkey
hostkeys:app:delete <app>, Deletes all app-specific hostkeys
hostkeys:app:autoadd <app> <hostname>, Automatically add hostkeys for a given host to the shared hostkeys
EOF
return
}
print_shared_keys() {
check_install
cat<<EOF
The following SHARED hostkeys have been registered:
---------------------------------------------------
EOF
if [[ ! -s "$SHARED_HOSTKEYS_FILE" ]]; then
echo "No keys registered. Use dokku 'hostkeys:shared:add <key>' to add one."
else
cat "$SHARED_HOSTKEYS_FILE"
fi
}
add_shared_key() {
check_install
if [[ ! -n "$2" ]]; then
echo $SHARED_KEY_TO_ADD >> "$SHARED_HOSTKEYS_FILE"
echo "Added $SHARED_KEY_TO_ADD to the list of shared hostkeys"
else
echo "Empty argument. Try again."
fi
}
delete_shared_keys() {
check_install
if [[ ! -n "$2" ]]; then
ssh-keygen -f "$SHARED_HOSTKEYS_FILE" -R "$HOSTNAME_TO_REMOVE"
rm "$SHARED_HOSTKEYS_FOLDER/known_hosts.old"
echo "Deleted hostkey for $HOSTNAME_TO_REMOVE as well as the backup."
else
> $SHARED_HOSTKEYS_FILE
echo "Emptied the shared hostkey file. All apps will loose the shared keys on next push. Make sure you add the required ones"
fi
}
autoadd_shared_key() {
check_install
if [[ -n "$HOSTNAME_TO_ADD" ]]; then
ssh-keyscan -H "$HOSTNAME_TO_ADD" >> "$SHARED_HOSTKEYS_FILE"
echo "Added keys for $HOSTNAME_TO_ADD"
else
echo "Please enter the name of the host as argument"
fi
}
add_app_key() {
check_app
check_exists
check_install_app
if [[ ! -n "$3" ]]; then
echo $SHARED_KEY_TO_ADD_APP >> "$APP_SPECIFIC_HOSTKEYS_FILE"
echo "Added $SHARED_KEY_TO_ADD_APP to the list of shared hostkeys"
else
echo "Empty argument. Try again."
fi
}
autoadd_app_key(){
check_app
check_exists
check_install_app
if [[ -n "$HOSTNAME_TO_ADD_APP" ]]; then
ssh-keyscan -H "$HOSTNAME_TO_ADD_APP" >> "$APP_SPECIFIC_HOSTKEYS_FILE"
echo "Added keys for $HOSTNAME_TO_ADD_APP"
else
echo "Please enter the name of the host as argument"
fi
}
delete_app_keys() {
check_app
check_exists
check_install_app
if [[ -n "$3" ]]; then
ssh-keygen -f "$APP_SPECIFIC_HOSTKEYS_FILE" -R "$HOSTNAME_TO_REMOVE_APP"
rm "$APP_SPECIFIC_HOSTKEYS_FOLDER/known_hosts.old"
echo "Deleted hostkey for $HOSTNAME_TO_REMOVE as well as the backup."
else
> $APP_SPECIFIC_HOSTKEYS_FILE
echo "Emptied the app specific hostkey file. Your app looses the specific keys on the next push. Make sure you add the required ones"
fi
}
print_keys_for_app() {
check_app
check_exists
check_install_app
print_shared_keys
cat<<EOF
The following APP SPECIFIC hostkeys have been registered:
---------------------------------------------------------
EOF
if [[ ! -s "$APP_SPECIFIC_HOSTKEYS_FILE" ]]; then
echo "No keys registered. Use dokku 'hostkeys:app:add <key>' to add one."
else
cat "$APP_SPECIFIC_HOSTKEYS_FILE"
fi
}
case "$1" in
hostkeys)
print_explanation
print_help
;;
hostkeys:shared:show)
print_shared_keys
;;
hostkeys:shared:add)
add_shared_key "$@"
;;
hostkeys:shared:delete)
delete_shared_keys "$@"
;;
hostkeys:shared:autoadd)
autoadd_shared_key
;;
hostkeys:app:show)
print_keys_for_app
;;
hostkeys:app:add)
add_app_key "$@"
;;
hostkeys:app:delete)
delete_app_keys "$@"
;;
hostkeys:app:autoadd)
autoadd_app_key
;;
help | hostkeys:help)
if [[ -n $DOKKU_API_VERSION ]]; then
print_help
else
cat && print_help
fi
;;
*)
exit $DOKKU_NOT_IMPLEMENTED_EXIT
;;
esac