forked from fydrah/loginapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genconf.sh
executable file
·334 lines (311 loc) · 10.6 KB
/
genconf.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
#!/bin/bash
# /!\ For testing purpose only
CURR_DIR=$(dirname $0)
NODE_IP=$(docker inspect loginapp-control-plane -f '{{ .NetworkSettings.Networks.kind.IPAddress }}')
mkdir -p ${CURR_DIR}/generated/ssl ${CURR_DIR}/kubernetes/generated ${CURR_DIR}/helm/generated
for cert in dex loginapp
do
echo "Generating CSR for ${cert}"
cat << EOF > ${CURR_DIR}/generated/ssl/req-${cert}.cnf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
[req_distinguished_name]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment, dataEncipherment, keyAgreement, keyCertSign, cRLSign
extendedKeyUsage = clientAuth, serverAuth, emailProtection, codeSigning
subjectAltName = @alt_names
[alt_names]
DNS.1 = ${cert}.${NODE_IP}.nip.io
DNS.2 = ${cert}.127.0.0.1.nip.io
EOF
[ -e ${CURR_DIR}/generated/ssl/key-${cert}.pem ] || openssl genrsa -out ${CURR_DIR}/generated/ssl/key-${cert}.pem 2048 >/dev/null
[ -e ${CURR_DIR}/generated/ssl/csr-${cert}.pem ] || openssl req -new -key ${CURR_DIR}/generated/ssl/key-${cert}.pem \
-out ${CURR_DIR}/generated/ssl/csr-${cert}.pem -subj "/CN=system:node:loginapp;/O=system:nodes" -config ${CURR_DIR}/generated/ssl/req-${cert}.cnf >/dev/null
kubectl get csr ${cert} >/dev/null || cat <<EOF | kubectl create -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: ${cert}
spec:
signerName: kubernetes.io/kubelet-serving
request: $(base64 -i ${CURR_DIR}/generated/ssl/csr-${cert}.pem -w0)
usages:
- key encipherment
- digital signature
- server auth
EOF
echo "OK"
echo "Approving CSR for ${cert}"
sleep 0.5
[ "$(kubectl get csr ${cert} -o jsonpath='{.status.conditions[0].type}')" == "Approved" ] || kubectl certificate approve ${cert}
echo "OK"
echo "Waiting for certificate for ${cert}"
while [ -z "$(kubectl get csr ${cert} -o jsonpath='{.status.certificate}')" ]
do
sleep 0.5
echo -n "."
done
echo "OK"
echo "Creating TLS secret for ${cert}"
kubectl get csr ${cert} -o jsonpath='{.status.certificate}' | base64 -d > ${CURR_DIR}/generated/ssl/${cert}.crt
cat <<EOF > ${CURR_DIR}/kubernetes/generated/${cert}-certs.yaml
---
apiVersion: v1
kind: Secret
metadata:
name: ${cert}-certs
namespace: kube-system
type: kubernetes.io/tls
data:
tls.crt: $(kubectl get csr ${cert} -o jsonpath='{.status.certificate}')
tls.key: $(base64 -i ${CURR_DIR}/generated/ssl/key-${cert}.pem -w0)
EOF
done
echo "Generating dex and loginapp configurations"
### Dex
cat <<EOF > ${CURR_DIR}/helm/generated/dex-overrides.yaml
fullnameOverride: dex
https:
enabled: true
service:
type: NodePort
ports:
https:
nodePort: 32000
volumes:
- name: tls
secret:
secretName: dex-certs
volumeMounts:
- name: tls
mountPath: /etc/dex/tls
config:
issuer: https://dex.${NODE_IP}.nip.io:32000
storage:
type: kubernetes
config:
inCluster: true
web:
https: 0.0.0.0:5554
tlsCert: /etc/dex/tls/tls.crt
tlsKey: /etc/dex/tls/tls.key
oauth2:
skipApprovalScreen: true
staticClients:
- id: loginapp
redirectURIs:
- 'https://loginapp.${NODE_IP}.nip.io:32001/callback'
name: 'Loginapp Kube'
secret: ZXhhbXBsZS1hcHAtc2VjcmV0
- id: loginapp-local
redirectURIs:
- 'https://loginapp.127.0.0.1.nip.io:8443/callback'
name: 'Loginapp local'
secret: ZXhhbXBsZS1hcHAtc2VjcmV1
enablePasswordDB: true
staticPasswords:
- email: "[email protected]"
# bcrypt hash of the string "password"
hash: "$2a$10$2b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W"
username: "admin"
userID: "08a8684b-db88-4b73-90a9-3cd1661f5466"
EOF
### Loginapp
cat <<EOF > ${CURR_DIR}/kubernetes/generated/loginapp-config.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: loginapp-config
namespace: kube-system
data:
config.yaml: |
name: "Kubernetes Auth"
listen: "0.0.0.0:8443"
oidc:
client:
id: "loginapp"
redirectURL: "https://loginapp.${NODE_IP}.nip.io:32001/callback"
issuer:
rootCA: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
url: "https://dex.${NODE_IP}.nip.io:32000"
tls:
enabled: true
cert: /ssl/tls.crt
key: /ssl/tls.key
log:
level: Debug
format: json
clusters:
- name: myfakecluster
server: https://myfakecluster.org
certificate-authority: |
-----BEGIN CERTIFICATE-----
MIIDZzCCAk+gAwIB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a5zcSnnCUxsFIbmEPun6Wk1t
0qICrfqPjH98dOqfu5+IBb6EoCrTkp9t9ic9hrkdS5CYBvwZHJf+a5/rmte+xyix
pvbI7bJ+Sw8Sx2E=
-----END CERTIFICATE-----
insecure-skip-tls-verify: false
---
apiVersion: v1
kind: Secret
metadata:
name: loginapp-secret-env
namespace: kube-system
type: Opaque
data:
# original: ZXhhbXBsZS1hcHAtc2VjcmV0
LOGINAPP_OIDC_CLIENT_SECRET: WlhoaGJYQnNaUzFoY0hBdGMyVmpjbVYw
EOF
cat <<EOF > ${CURR_DIR}/generated/loginapp-config-manual.yaml
---
name: "Kubernetes Auth"
listen: "0.0.0.0:8443"
oidc:
client:
id: "loginapp-local"
secret: ZXhhbXBsZS1hcHAtc2VjcmV1
redirectURL: "https://loginapp.127.0.0.1.nip.io:8443/callback"
issuer:
rootCA: "${CURR_DIR}/generated/ssl/ca.crt"
url: "https://dex.${NODE_IP}.nip.io:32000"
tls:
enabled: true
cert: ${CURR_DIR}/generated/ssl/loginapp.crt
key: ${CURR_DIR}/generated/ssl/key-loginapp.pem
log:
level: Debug
format: json
clusters:
- name: myfakecluster
server: https://myfakecluster.org
certificate-authority: |
-----BEGIN CERTIFICATE-----
MIIDZzCCAk+gAwIBAgIRAM/Oqk7538VUzHwMZ+x7eN0wDQYJKoZIhvcNAQELBQAw
FTETMBEGA1UEAxMKa3ViZXJuZXRlczAeFw0yMTExMDUxMDUyMDdaFw0yMjExMDUx
MDUyMDdaMDcxFTATBgNVBAoTDHN5c3RlbTpub2RlczEeMBwGA1UEAwwVc3lzdGVt
Om5vZGU6bG9naW5hcHA7MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
yc4mV0leq+fJ5KsF6yZTKtWRVzwcZGeYZv3gf1R9zSdexshOtBORRaNHJmuAu0st
aHunVPlsSInBGBf4pO4OTLilu965/OQOvFvIFLzl3LNMoHOvUMofJ8lwBlcczil+
UYf+xvDNYOYN3Q8WIBs5W2yHgDdFdfb9Z/X6ezL31T0/1+VYXR5cSawIKP9zEY/z
miUWqrqRVZN0qvFYp5phMUbcIlNknEo/p0nUXfnz87P/7862BJuVaJVZmAMcg3IS
q9Xl27P4NVnIvI5WPrlNrDNv3AupEJFS0c/Gms3nr6BZWNg5tM2mRhAr1Gfm37me
fFqM0gxJn2sHjSF5y+M0jQIDAQABo4GPMIGMMA4GA1UdDwEB/wQEAwIFoDATBgNV
HSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFOw9vdeK
hC8ALh7H+2QYpXPUWUU6MDYGA1UdEQQvMC2CFWRleC4xNzIuMTcuMC4yLm5pcC5p
b4IUZGV4LjEyNy4wLjAuMS5uaXAuaW8wDQYJKoZIhvcNAQELBQADggEBAHgdgQ07
YXERpAw8wRxxxX6Ozsod/vKww6E5Q4KmU8FxTqkx7hxQpp3neC6b5+yrsbGw2h0R
8U9Kog+oNLtoN9K9AqVhbP/t6Ny8XCuEx/WXq01jBfwcrP4BCFD7oOfK5V5Ah5ey
11KzZ10tsUTlNXmqVFUr93tCy5Yf4iK8k0SJpxWPcoltzWCG1H5l/j5frlK8AbmD
Yn3zbspD+UG7wEbZWM4SFXEDC4DpEOMtRaEDFZPXa5zcSnnCUxsFIbmEPun6Wk1t
0qICrfqPjH98dOqfu5+IBb6EoCrTkp9t9ic9hrkdS5CYBvwZHJf+a5/rmte+xyix
pvbI7bJ+Sw8Sx2E=
-----END CERTIFICATE-----
insecure-skip-tls-verify: false
EOF
cat <<EOF > ${CURR_DIR}/helm/generated/overrides.yaml
replicas: 2
service:
type: NodePort
nodePort: 32001
args:
- "-v"
config:
secret: opdzojferfijcreoo
clientID: loginapp
clientSecret: ZXhhbXBsZS1hcHAtc2VjcmV0
clientRedirectURL: "https://loginapp.${NODE_IP}.nip.io:32001/callback"
issuerURL: "https://dex.${NODE_IP}.nip.io:32000"
# Don't check issuer certificates (self-signed)
issuerInsecureSkipVerify: true
refreshToken: true
tls:
# This will generate a self-signed certificate
enabled: true
altnames:
- "loginapp.172.17.0.2.nip.io"
clusters:
- name: myfakecluster
server: https://myfakecluster.org
certificate-authority: |
-----BEGIN CERTIFICATE-----
MIIC/zCCAeegAwIBAgIULkYvGJPRl50tMoVE4BNM0laRQncwDQYJKoZIhvcNAQEL
BQAwDzENMAsGA1UEAwwEbXljYTAeFw0xOTAyMTgyMjA5NTJaFw0xOTAyMjgyMjA5
NTJaMA8xDTALBgNVBAMMBG15Y2EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
-----END CERTIFICATE-----
insecure-skip-tls-verify: false
configOverwrites:
web:
# "name": "admin", "email": [email protected]
mainUsernameClaim: "name"
dex:
enabled: true
fullnameOverride: dex
replicas: 2
https:
enabled: true
service:
type: NodePort
ports:
https:
nodePort: 32000
volumes:
- name: tls
secret:
secretName: dex-certs
volumeMounts:
- name: tls
mountPath: /etc/dex/tls
config:
issuer: https://dex.${NODE_IP}.nip.io:32000
storage:
type: kubernetes
config:
inCluster: true
web:
https: 0.0.0.0:5554
tlsCert: /etc/dex/tls/tls.crt
tlsKey: /etc/dex/tls/tls.key
oauth2:
skipApprovalScreen: true
staticClients:
- id: loginapp
redirectURIs:
- 'https://loginapp.${NODE_IP}.nip.io:32001/callback'
name: 'Loginapp Kube'
secret: ZXhhbXBsZS1hcHAtc2VjcmV0
- id: loginapp-local
redirectURIs:
- 'https://loginapp.127.0.0.1.nip.io:8443/callback'
name: 'Loginapp local'
secret: ZXhhbXBsZS1hcHAtc2VjcmV1
enablePasswordDB: true
staticPasswords:
- email: "[email protected]"
# bcrypt hash of the string "password"
hash: "a0b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W"
username: "admin"
userID: "08a8684b-db88-4b73-90a9-3cd1661f5466"
EOF
### Get Kubernetes certificate authority
echo "Get Kubernetes certificate authority (${CURR_DIR}/generated/ssl/ca.crt)"
kubectl config view --minify --flatten -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' | base64 -d > ${CURR_DIR}/generated/ssl/ca.crt
kubectl -n kube-system create configmap root-ca --from-file=ca.crt=${CURR_DIR}/generated/ssl/ca.crt 2>/dev/null