-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitlab-protector.sh
executable file
·639 lines (549 loc) · 18.8 KB
/
gitlab-protector.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/usr/bin/env bash
set -eu
################################################################################
## CONFIG
################################################################################
## NOTE:
## In case you need to change any of the defaults, please create a file
## /etc/default/gitlab-protector and use the same variable names in order
## to override the default values.
## Path to the hashed GitLab repositories.
DIR_GITLAB_HASHED_REPOS="/var/opt/gitlab/git-data/repositories/@hashed/"
################################################################################
## FUNCTIONS
################################################################################
## Print script usage.
function print_usage() {
cat <<_EOF_
USAGE: $(basename "$0") [COMMAND] [OPTIONS...]
COMMAND
config, c [ARGS] Starts interactive configuration menu.
fix, f Fixes all dangling symlinks in repositories that use GitLab Protector.
status, s Displays an overview of the configuration status per repository.
uninstall, u Starts interactive uninstall menu for repositories.
OPTIONS
-h, --help This help screen.
ARGS for 'config'
groups, g Starts interactive configuration menu for groups.
repository, repo, r Starts interactive configuration menu for repositories.
_EOF_
}
## Finds all gitlab repos and stores them in REPO_* global variables.
function load_gitlab_repos() {
local IFS=$'\n'
local repo_paths=($(
cd "$DIR_GITLAB_HASHED_REPOS"
find . -type f -name config \
| grep -v '.wiki.git/' \
| grep -v '\+[0-9]+\+deleted.git/' \
| xargs grep 'fullpath = ' \
| sed -E 's|^./||;s|^(.+\.git)/config:\s+fullpath = (.+$)|\2\|\|\|\1|' \
| grep -vE 'gitlab-instance-administrators-.+' \
| sort -k1
))
REPO_NAME=()
REPO_PATH=()
REPO_SYMLINK_INSTALLED=()
REPO_SYMLINK_DANGLING=()
local repo_raw
for repo_raw in ${repo_paths[@]}
do
local repo_name="${repo_raw%|||*}"
local repo_path="${repo_raw#*|||}"
REPO_NAME+=("$repo_name")
REPO_PATH+=("$repo_path")
local file_symlink_src="$SCRIPTPATH/$PATH_GITLAB_PROTECTOR_SCRIPT"
local file_symlink_dst="$DIR_GITLAB_HASHED_REPOS/$repo_path/custom_hooks/$PATH_GITLAB_PROTECTOR_SCRIPT"
## Does the symlink exist?
if [[ -L "$file_symlink_dst" ]]
then
REPO_SYMLINK_INSTALLED+=(1)
## Does the symlink point to the correct location?
if [[ "$file_symlink_src" == "$(readlink -f "$file_symlink_dst")" ]]
then
REPO_SYMLINK_DANGLING+=(0)
else
REPO_SYMLINK_DANGLING+=(1)
fi
else
REPO_SYMLINK_INSTALLED+=(0)
REPO_SYMLINK_DANGLING+=(0)
fi
done
REPO_NUM=${#REPO_PATH[@]}
}
## Finds all user configs and stores them in USER_CONFIG_* global variables.
function load_user_configs() {
USER_CONFIG_PATH=()
USER_CONFIG_RULES=()
local IFS=$'\n'
local user_config_paths=($(
cd "$DIR_USER_CONFIG" || exit 1
find . -type f -name "repo.*.conf" \
| sed -E 's|^./||' \
| sort -k1
))
local user_config_path
for user_config_path in ${user_config_paths[@]}
do
USER_CONFIG_PATH+=("$user_config_path")
local rules_num=$(
cat "$DIR_USER_CONFIG/$user_config_path" \
| grep -vE '^\s*#' \
| sed '/^\s*$/d' \
| wc -l
)
USER_CONFIG_RULES+=("$rules_num")
done
USER_CONFIG_NUM=${#USER_CONFIG_PATH[@]}
}
## Checks which user configs do have an existing repo and stores info in USER_CONFIG_HAS_REPO global variable.
function load_user_config_has_repo() {
USER_CONFIG_HAS_REPO=()
local i
for (( i=0; i<USER_CONFIG_NUM; i++ ))
do
local user_config_path="${USER_CONFIG_PATH[$i]}"
local user_config_hash="$(dash2slash "${user_config_path}")"
user_config_hash="${user_config_hash#repo.}"
user_config_hash="${user_config_hash%.conf}"
local has_repo=0
for (( j=0; j<REPO_NUM; j++ ))
do
local repo_path="${REPO_PATH[$j]}"
local repo_hash="${repo_path%.git}"
[[ "$repo_hash" == "$user_config_hash" ]] || continue
if [[ -d "$DIR_GITLAB_HASHED_REPOS/$repo_path" ]]
then
has_repo=1
break
fi
done
USER_CONFIG_HAS_REPO+=( $has_repo )
done
}
## Checks which repos do have an existing user config and stores info in REPO_HAS_USER_CONFIG global variable.
function load_repo_has_user_config() {
REPO_HAS_USER_CONFIG=()
local i
for (( i=0; i<REPO_NUM; i++ ))
do
local repo_path="${REPO_PATH[$i]}"
local repo_hash="${repo_path%.git}"
local has_user_config=0
for (( j=0; j<USER_CONFIG_NUM; j++ ))
do
local user_config_path="${USER_CONFIG_PATH[$j]}"
local user_config_hash="$(dash2slash "${user_config_path}")"
user_config_hash="${user_config_hash#repo.}"
user_config_hash="${user_config_hash%.conf}"
[[ "$repo_hash" == "$user_config_hash" ]] || continue
if [[ -f "$DIR_USER_CONFIG/$user_config_path" ]]
then
has_user_config=1
break
fi
done
REPO_HAS_USER_CONFIG+=( $has_user_config )
done
}
## Searches for an existing user config by a repo hash.
##
## @param repo_hash
## @returns index for USER_CONFIG_* global variables
function get_user_config_index_by_repo_hash() {
local repo_hash="$1"
local i
for (( i=0; i<USER_CONFIG_NUM; i++ ))
do
local user_config_path="${USER_CONFIG_PATH[$i]}"
local user_config_hash="$(dash2slash "${user_config_path}")"
user_config_hash="${user_config_hash#repo.}"
user_config_hash="${user_config_hash%.conf}"
if [[ "$repo_hash" == "$user_config_hash" ]]
then
echo $i
return
fi
done
echo -1
}
## Converts all slashes to dashes in input string.
##
## @returns Converted string
function slash2dash() {
echo "${1//\//-}"
}
## Converts all dashes to slashes in input string.
##
## @returns Converted string
function dash2slash() {
echo "${1//-/\/}"
}
## Runs the interactive configuration (main) menu.
## By passing arguments to this function a specific menu can be opened directly.
##
## @param args for 'config' command
function do_config() {
if (( $# > 0 ))
then
case "$1" in
groups|group|g)
do_config_groups
;;
repository|repo|r)
do_config_repo
;;
*)
echo -e "\033[0;31;1mERROR:\033[0m Invalid choice." >&2
;;
esac
return
fi
while :
do
echo
echo " g: groups"
echo " r: repository"
echo
echo -n "What do you want to configure? "
read
case "$REPLY" in
'')
return
;;
g)
do_config_groups
;;
r)
do_config_repo
;;
*)
echo -e "\033[0;31;1mERROR:\033[0m Invalid choice." >&2
;;
esac
done
}
## Runs the interactive configuration menu for the global 'groups' configuration file.
## Before the configuration file is opened in the editor it will be checked whether
## a configuration file for 'groups' does already exist. If it does not exist a copy
## from the groups config template will be created first.
function do_config_groups() {
file_config="$FILE_GROUPS"
if [[ ! -e "$file_config" ]]
then
echo "Creating new configuration file from template ..."
cp "$PATH_TEMPLATE_GROUPS_CONF" "$file_config"
fi
${EDITOR:-vi} "$file_config"
exit 0
}
## Runs the interactive configuration menu for selecting a repository to edit.
## Before a configuration file is opened in the editor it will be checked whether
## a configuration file for the selected repo does already exist. If it does not exist
## a copy from the repo config template will be created first.
function do_config_repo() {
if (( REPO_NUM == 0 ))
then
echo -e "\033[0;31;1mERROR:\033[0m No GitLab repositories found. Create at least one first and try again." >&2
return
fi
while :
do
echo
local i
for (( i=0; i<REPO_NUM; i++ ))
do
local repo_name="${REPO_NAME[$i]}"
local repo_path="${REPO_PATH[$i]}"
printf " %3d: %s\n" \
"$i" \
"$repo_name"
done
echo
echo -n "Which repository do you want to configure? "
read
local input="${REPLY// /}"
[[ -n "${input}" ]] || break
if ! [[ "$input" =~ ^[0-9]+ ]] || (( input >= REPO_NUM ))
then
echo -e "\033[0;31;1mERROR:\033[0m Invalid choice." >&2
continue
fi
local i="$input"
local repo_name="${REPO_NAME[$i]}"
local repo_path="${REPO_PATH[$i]}"
local repo_hash="${repo_path%.git}"
local user_config_path="repo.$(slash2dash "${repo_hash}").conf"
file_config="$DIR_USER_CONFIG/$user_config_path"
if [[ ! -e "$file_config" ]]
then
echo "Creating new configuration file from template ..."
cp "$PATH_TEMPLATE_REPO_CONF" "$file_config"
fi
${EDITOR:-vi} "$file_config"
install_or_update_config_by_repo_hash "$repo_hash"
exit 0
done
}
## Displays a status overview about each GitLab repo found and how many rules
## have been configured if any.
function do_status() {
echo -e " GitLab Hashed Repos : \033[0;33;1m$DIR_GITLAB_HASHED_REPOS\033[0m"
echo -e " GitLab Protector user config: \033[0;33;1m$DIR_USER_CONFIG\033[0m"
echo
printf " \033[0;37;1m%-74s\033[0m | \033[0;37;1m%-9s\033[0m | \033[0;37;1m%-5s\033[0m | \033[0;37;1m%s\033[0m\n" \
"GITLAB HASHED REPOSITORY DIRECTORY" \
"INSTALLED" \
"RULES" \
"REPOSITORY NAME"
echo " ---------------------------------------------------------------------------+-----------+-------+--------------------------------------"
local i
for (( i=0; i<REPO_NUM; i++ ))
do
local repo_name="${REPO_NAME[$i]}"
local repo_path="${REPO_PATH[$i]}"
local repo_hash="${repo_path%.git}"
local has_user_config="${REPO_HAS_USER_CONFIG[$i]}"
local user_config_rules
if (( has_user_config == 1 ))
then
local index=$(get_user_config_index_by_repo_hash "$repo_hash")
if [[ -n "$index" ]] && (( index >= 0 ))
then
user_config_rules="${USER_CONFIG_RULES[$index]}"
else
user_config_rules="?"
fi
else
user_config_rules=""
fi
local installed
local color_installed
if (( ${REPO_SYMLINK_INSTALLED[$i]} == 1 ))
then
if (( ${REPO_SYMLINK_DANGLING[$i]} == 1 ))
then
installed="dangling"
color_installed=$'\033[0;33;5m'
else
installed="yes"
color_installed=$'\033[0;32;1m'
fi
else
installed="no"
color_installed=''
fi
printf " %74s | ${color_installed}%9s\033[0m | %5s | %s\n" \
"${repo_hash}.git" \
"$installed" \
"$user_config_rules" \
"$repo_name"
done
echo
for (( i=0; i<USER_CONFIG_NUM; i++ ))
do
local user_config_path="${USER_CONFIG_PATH[$i]}"
local has_repo=${USER_CONFIG_HAS_REPO[$i]}
if (( has_repo == 0 ))
then
(
echo -e "\033[0;33;1mWARNING:\033[0m Repository is missing for configuration \`\033[0;33;1m$user_config_path\033[0m'."
echo "The corresponding repository has probably been deleted. Delete this configuration file if you no longer need it."
echo
) >&2
fi
done
if [[ ! -r "$FILE_GROUPS" ]]
then
(
echo -e "\033[0;33;1mWARNING:\033[0m No group configuration file found at \`\033[0;33;1m$FILE_GROUPS\033[0m'."
echo -e "Use command '\033[0;36;1m$(basename "$0") config groups\033[0m' to resolve this issue."
echo
) >&2
fi
}
## Fixes all dangling symlinks by replacing them with the new/correct location.
function do_fix_dangling_symlinks() {
local num_fixed=0
local i
for (( i=0; i<REPO_NUM; i++ ))
do
local repo_name="${REPO_NAME[$i]}"
local repo_path="${REPO_PATH[$i]}"
local repo_hash="${repo_path%.git}"
if (( ${REPO_SYMLINK_INSTALLED[$i]} == 1 ))
then
if (( ${REPO_SYMLINK_DANGLING[$i]} == 1 ))
then
echo -e "\033[0;32;1m*\033[0m Fixing dangling symlink for repo \`\033[0;33;1m$repo_name\033[0m' ..."
install_or_update_config_by_repo_hash "$repo_hash" none
num_fixed=$(( num_fixed+1 ))
fi
fi
done
if (( num_fixed == 0 ))
then
echo "No dangling symlinks found. All good!"
fi
}
## Runs the interactive configuration menu for uninstalling GitLab Protector for a repository.
## The uninstallation process is simply removing the symlink. It will NOT delete your user config.
function do_uninstall_repo() {
if (( REPO_NUM == 0 ))
then
echo -e "\033[0;31;1mERROR:\033[0m No GitLab repositories found. Create at least one first and try again." >&2
return
fi
while :
do
echo
local uninstallable_repo_num=0
local i
for (( i=0; i<REPO_NUM; i++ ))
do
local repo_name="${REPO_NAME[$i]}"
local repo_path="${REPO_PATH[$i]}"
(( ${REPO_SYMLINK_INSTALLED[$i]} == 1 )) || continue
uninstallable_repo_num=$(( uninstallable_repo_num + 1 ))
printf " %3d: %s\n" \
"$i" \
"$repo_name"
done
if (( uninstallable_repo_num == 0 ))
then
echo -e "\033[0;31;1mERROR:\033[0m No GitLab repositories with GitLab Protector installed were found." >&2
return
fi
echo
echo -n "For which repository do you want to uninstall GitLab Protector? "
read
local input="${REPLY// /}"
[[ -n "${input}" ]] || break
if ! [[ "$input" =~ ^[0-9]+ ]] || (( input >= REPO_NUM ))
then
echo -e "\033[0;31;1mERROR:\033[0m Invalid choice." >&2
continue
fi
if (( ${REPO_SYMLINK_INSTALLED[$input]} != 1 ))
then
echo -e "\033[0;31;1mERROR:\033[0m Invalid choice. GitLab Protector is not installed for this repository." >&2
continue
fi
local i="$input"
local repo_name="${REPO_NAME[$i]}"
local repo_path="${REPO_PATH[$i]}"
local repo_hash="${repo_path%.git}"
uninstall_symlink_by_repo_hash "$repo_hash"
exit 0
done
}
## Convenience method to load and prepare all required data at once.
function load_all() {
load_gitlab_repos
load_user_configs
load_user_config_has_repo
load_repo_has_user_config
}
## Installs or updates the symlink of a repo by the given repo hash.
##
## @param repo_hash
## @param verbosity {none|info}, default=info
function install_or_update_config_by_repo_hash() {
local repo_hash="$1"
local verbosity="${2:-info}"
local repo_path="$DIR_GITLAB_HASHED_REPOS/${repo_hash}.git"
local file_symlink_src="$SCRIPTPATH/$PATH_GITLAB_PROTECTOR_SCRIPT"
local file_symlink_dst="$repo_path/custom_hooks/$PATH_GITLAB_PROTECTOR_SCRIPT"
local dir_symlink_dst="$(dirname "$file_symlink_dst")"
[[ -d "$dir_symlink_dst" ]] || mkdir -p "$dir_symlink_dst"
if [[ ! -e "$file_symlink_dst" ]]
then
[[ "$verbosity" == "info" ]] && echo -e "Installing GitLab Protector in: \033[0;33;1m$file_symlink_dst\033[0m"
ln -sf "$file_symlink_src" "$file_symlink_dst"
fi
}
function check_env_exit_on_fail() {
if [[ ! -d "$DIR_GITLAB_HASHED_REPOS" ]]
then
echo -e "\033[0;31;1mERROR:\033[0m GitLab hashed storage directory not found: \`\033[0;33;1m$DIR_GITLAB_HASHED_REPOS\033[0m'" >&2
exit 1
fi
}
## Uninstalls the symlink of a repo by the given repo hash.
##
## @param repo_hash
## @param verbosity {none|info}, default=info
function uninstall_symlink_by_repo_hash() {
local repo_hash="$1"
local verbosity="${2:-info}"
local repo_path="$DIR_GITLAB_HASHED_REPOS/${repo_hash}.git"
local file_symlink_dst="$repo_path/custom_hooks/$PATH_GITLAB_PROTECTOR_SCRIPT"
if [[ -L "$file_symlink_dst" ]]
then
[[ "$verbosity" == "info" ]] && echo -e "Uninstalling GitLab Protector ..."
rm "$file_symlink_dst"
else
echo -e "\033[0;31;1mERROR:\033[0m Could not uninstall GitLab Protector for selected repository. File \`$file_symlink_dst' is not a symlink or does not exist." >&2
fi
}
################################################################################
## MAIN
################################################################################
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
DIR_USER_CONFIG="$SCRIPTPATH/user-config"
FILE_GROUPS="$DIR_USER_CONFIG/groups.global.conf"
PATH_GITLAB_PROTECTOR_SCRIPT="pre-receive.d/net.twistedbytes.gitlab-protector.py"
PATH_TEMPLATE_REPO_CONF="$DIR_USER_CONFIG/repo.conf.TEMPLATE"
PATH_TEMPLATE_GROUPS_CONF="$DIR_USER_CONFIG/groups.global.conf.TEMPLATE"
REPO_NAME=()
REPO_PATH=()
REPO_HAS_USER_CONFIG=()
REPO_SYMLINK_INSTALLED=()
REPO_SYMLINK_DANGLING=()
REPO_NUM=0
USER_CONFIG_PATH=()
USER_CONFIG_HAS_REPO=()
USER_CONFIG_RULES=()
USER_CONFIG_NUM=0
## Optionally load custom settings configured for this system
[[ -r "/etc/default/gitlab-protector" ]] && . "/etc/default/gitlab-protector"
## Create user config directory if missing
[[ -d "$DIR_USER_CONFIG" ]] || mkdir "$DIR_USER_CONFIG" || exit 1
if (( $# == 0 ))
then
print_usage >&2
exit 1
fi
COMMAND="$1"
shift
case "$COMMAND" in
-h|--help|help)
print_usage >&2
exit 1
;;
config|c)
check_env_exit_on_fail
load_all
do_config $@
;;
fix|f)
check_env_exit_on_fail
load_all
do_fix_dangling_symlinks
;;
status|s)
check_env_exit_on_fail
load_all
do_status
;;
uninstall|u)
check_env_exit_on_fail
load_all
do_uninstall_repo
;;
*)
echo "Unknown command \`$COMMAND'. Try '$(basename "$0") --help' for more information." >&2
exit 1
;;
esac