-
Notifications
You must be signed in to change notification settings - Fork 9
/
gencerts.sh
executable file
·85 lines (71 loc) · 2.36 KB
/
gencerts.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
#!/bin/sh
CA_ORG="/O=IoT Management/emailAddress=admin@example.com"
CA_DN="/CN=mqtt${CA_ORG}"
MQTT_DN="/CN=mqtt$CA_ORG"
TWIN_DN="/CN=mqtt$CA_ORG"
# Certificate authority
openssl req -newkey rsa:2048 -x509 -nodes -sha512 -days 3650 -extensions v3_ca -keyout ca.key -out ca.crt -subj "${CA_DN}"
# MQTT broker certificate
openssl genrsa -out mqtt.key 2048
openssl req -new -sha512 -out mqtt.csr -key mqtt.key -subj "${MQTT_DN}"
cat > "./mqtt.cnf" << EOF
[v3_req]
basicConstraints = critical,CA:FALSE
nsCertType = server
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
EOF
openssl x509 -req -sha512 -in mqtt.csr -CA ca.crt -CAkey ca.key -CAcreateserial -CAserial ca.srl -out mqtt.crt -days 365 -extfile "./mqtt.cnf" -extensions v3_req
# Device twin certificate
openssl genrsa -out devicetwin.key 2048
openssl req -new -sha512 -out devicetwin.csr -key devicetwin.key -subj "${TWIN_DN}"
cat > "./devicetwin.cnf" << EOF
[v3_req]
basicConstraints = critical,CA:FALSE
nsCertType = client
extendedKeyUsage = clientAuth
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = digitalSignature, keyEncipherment, keyAgreement
EOF
openssl x509 -req -sha512 -in devicetwin.csr -CA ca.crt -CAkey ca.key -CAcreateserial -CAserial ca.srl -out devicetwin.crt -days 365 -extfile "./devicetwin.cnf" -extensions v3_req
# Generate the kubernetes secrets
cat > "./devicetwin.yaml" << EOF
apiVersion: v1
kind: Secret
metadata:
name: devicetwin-certs
data:
# base64 encoded X509 certificate files
ca.crt: `cat ca.crt | base64 -w0`
server.crt: `cat devicetwin.crt | base64 -w0`
server.key: `cat devicetwin.key | base64 -w0`
---
EOF
cat > "./mqtt.yaml" << EOF
apiVersion: v1
kind: Secret
metadata:
name: mqtt-certs
data:
# base64 encoded X509 certificate files
ca.crt: `cat ca.crt | base64 -w0`
server.crt: `cat mqtt.crt | base64 -w0`
server.key: `cat mqtt.key | base64 -w0`
---
EOF
cat > "./identity.yaml" << EOF
apiVersion: v1
kind: Secret
metadata:
name: identity-certs
data:
# base64 encoded X509 certificate files
ca.crt: `cat ca.crt | base64 -w0`
ca.key: `cat ca.key | base64 -w0`
---
EOF
# Clean up
rm *.cnf *.csr
echo "Use the *.yaml files for deploying the X509 certificates as kubernetes secrets"