blob: a042ab62af8c973b19f349107f10edff709eab99 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001#!/usr/bin/env bash
2
3# Copyright 2017 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19# gencerts.sh generates the certificates for the webhook tests.
20#
21# It is not expected to be run often (there is no go generate rule), and mainly
22# exists for documentation purposes.
23
24CN_BASE="webhook_tests"
25
26cat > server.conf << EOF
27[req]
28req_extensions = v3_req
29distinguished_name = req_distinguished_name
30[req_distinguished_name]
31[ v3_req ]
32basicConstraints = CA:FALSE
33keyUsage = nonRepudiation, digitalSignature, keyEncipherment
34extendedKeyUsage = clientAuth, serverAuth
35subjectAltName = @alt_names
36[alt_names]
37IP.1 = 127.0.0.1
38EOF
39
40cat > client.conf << EOF
41[req]
42req_extensions = v3_req
43distinguished_name = req_distinguished_name
44[req_distinguished_name]
45[ v3_req ]
46basicConstraints = CA:FALSE
47keyUsage = nonRepudiation, digitalSignature, keyEncipherment
48extendedKeyUsage = clientAuth, serverAuth
49subjectAltName = @alt_names
50[alt_names]
51IP.1 = 127.0.0.1
52EOF
53
54# Create a certificate authority
55openssl genrsa -out caKey.pem 2048
56openssl req -x509 -new -nodes -key caKey.pem -days 100000 -out caCert.pem -subj "/CN=${CN_BASE}_ca"
57
58# Create a second certificate authority
59openssl genrsa -out badCAKey.pem 2048
60openssl req -x509 -new -nodes -key badCAKey.pem -days 100000 -out badCACert.pem -subj "/CN=${CN_BASE}_ca"
61
62# Create a server certiticate
63openssl genrsa -out serverKey.pem 2048
64openssl req -new -key serverKey.pem -out server.csr -subj "/CN=${CN_BASE}_server" -config server.conf
65openssl x509 -req -in server.csr -CA caCert.pem -CAkey caKey.pem -CAcreateserial -out serverCert.pem -days 100000 -extensions v3_req -extfile server.conf
66
67# Create a client certiticate
68openssl genrsa -out clientKey.pem 2048
69openssl req -new -key clientKey.pem -out client.csr -subj "/CN=${CN_BASE}_client" -config client.conf
70openssl x509 -req -in client.csr -CA caCert.pem -CAkey caKey.pem -CAcreateserial -out clientCert.pem -days 100000 -extensions v3_req -extfile client.conf
71
72outfile=certs_test.go
73
74cat > $outfile << EOF
75/*
76Copyright 2017 The Kubernetes Authors.
77
78Licensed under the Apache License, Version 2.0 (the "License");
79you may not use this file except in compliance with the License.
80You may obtain a copy of the License at
81
82 http://www.apache.org/licenses/LICENSE-2.0
83
84Unless required by applicable law or agreed to in writing, software
85distributed under the License is distributed on an "AS IS" BASIS,
86WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
87See the License for the specific language governing permissions and
88limitations under the License.
89*/
90
91EOF
92
93echo "// This file was generated using openssl by the gencerts.sh script" >> $outfile
94echo "// and holds raw certificates for the webhook tests." >> $outfile
95echo "" >> $outfile
96echo "package webhook" >> $outfile
97for file in caKey caCert badCAKey badCACert serverKey serverCert clientKey clientCert; do
98 data=$(cat ${file}.pem)
99 echo "" >> $outfile
100 echo "var $file = []byte(\`$data\`)" >> $outfile
101done
102
103# Clean up after we're done.
104rm *.pem
105rm *.csr
106rm *.srl
107rm *.conf