This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
executable file
·129 lines (108 loc) · 3.14 KB
/
entrypoint.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
#!/usr/bin/env bash
set -euo pipefail
# Ensure these are set before continuing
TASKDHOME=$TASKDHOME
TASKDDATA=$TASKDDATA
TASKDGIT=$TASKDGIT
TASKDPKI=$TASKDPKI
# Config
TASKD_LOG=${TASKD_LOG:="/dev/stdout"}
TASKD_PID_FILE=${TASKD_PID_FILE:="$TASKDHOME/taskd.pid"}
TASKD_SERVER=${TASKD_SERVER:="0.0.0.0:53589"}
# Override the pki variables or fallback to default values
pki_vars_override() {
TASKD_SEC_PARAM=${TASKD_SEC_PARAM:="high"} \
TASKD_EXPIRATION_DAYS=${TASKD_EXPIRATION_DAYS:="365"} \
TASKD_ORGANIZATION=${TASKD_ORGANIZATION:='"Göteborg Bit Factory"'} \
TASKD_CN=${TASKD_CN:="localhost"} \
TASKD_COUNTRY=${TASKD_COUNTRY:="SE"} \
TASKD_STATE=${TASKD_STATE:='"Västra Götaland"'} \
TASKD_LOCALITY=${TASKD_LOCALITY:='"Göteborg"'} \
envsubst < $TASKDPKI/vars.template > $TASKDPKI/vars
}
create_certificates() {
pki_vars_override && (cd "$TASKDPKI" && ./generate >/dev/null 2>&1)
}
generate_user_cert() {
(cd "$TASKDPKI" && ./generate.client "$1" >/dev/null 2>&1)
}
taskd_init() {
taskd init
create_certificates
# Configure all generated certificates
for cert in $TASKDPKI/*.pem; do
cert=$(basename "$cert")
config="${cert%.pem}"
# In case of the api.* certificate, the config key needs to be client.*
if [[ $cert =~ ^api ]]; then
config="${config/#api/client}"
fi;
taskd config --force "$config" "$TASKDPKI/$cert"
done;
taskd config --force log "$TASKD_LOG"
taskd config --force pid.file "$TASKD_PID_FILE"
taskd config --force server "$TASKD_SERVER"
# Display diagnostics
taskd diagnostics
}
taskd_start() {
if [ -n "${DEBUG:-}" ]; then
taskd server --debug --debug.tls=2 --data "${TASKDDATA}"
else
taskd server --data "${TASKDDATA}"
fi
}
taskd_add_org() {
taskd add org "$1"
}
taskd_add_user() {
key=$(taskd add user "$1" "$2" | awk -F ': ' '/New user key/{ print $2 }')
filename=$(head /dev/urandom | tr -dc [:alnum:] | head -c12)
# Generate client certificates for user
generate_user_cert "$filename"
printf "%-20s\t%-20s\t%-40s\t%-12s\n" "ORG" "USERNAME" "KEY" "USER-CERT"
printf "%-20s\t%-20s\t%-40s\t%-12s\n" "$1" "$2" "$key" "$filename"
}
main() {
if [[ ! -e "$TASKDDATA/config" ]]; then
taskd_init
fi
start=1
while [[ (($# > 0)) ]]
do
case "$1" in
add-user)
taskd_add_user "$2" "$3"
shift 3
start=0
;;
add-org)
taskd_add_org "$2"
shift 2
start=0
;;
user-cert)
cat "$TASKDPKI/$2.cert.pem"
shift 2
start=0
;;
user-key)
cat "$TASKDPKI/$2.key.pem"
shift 2
start=0
;;
ca-cert)
cat "$TASKDPKI/ca.cert.pem"
shift
start=0
;;
*)
shift
;;
esac
done
if [[ (($start > 0)) ]]; then
taskd_start
fi
}
main "$@"