gencerts.sh 3.4 KB

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