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