Debian Codegraph @

internal/buildscripts/gen-certs.sh

raw
1 #!/usr/bin/env bash
2 #
3 # Copyright The OpenTelemetry Authors
4 # SPDX-License-Identifier: Apache-2.0
5
6 # This script is used to create the CA, server and client's certificates and keys required by unit tests.
7 # These certificates use the Subject Alternative Name extension rather than the Common Name, which will be unsupported from Go 1.15.
8
9 usage() {
10 echo "Usage: $0 [-d]"
11 echo
12 echo "-d Dry-run mode. No project files will not be modified. Default: 'false'"
13 echo "-m Domain name to use in the certificate. Default: 'localhost'"
14 echo "-o Output directory where certificates will be written to. Default: '.'; the current directory"
15 echo "-s A suffix for the generated certificate. Default: \"\"; an empty string"
16 exit 1
17 }
18
19 dry_run=false
20 domain="localhost"
21 output_dir="."
22 suffix=""
23
24 while getopts "dm:o:s:" o; do
25 case "${o}" in
26 d)
27 dry_run=true
28 ;;
29 m)
30 domain=$OPTARG
31 ;;
32 o)
33 output_dir=$OPTARG
34 ;;
35 s)
36 suffix=$OPTARG
37 ;;
38 *)
39 usage
40 ;;
41 esac
42 done
43 shift $((OPTIND-1))
44
45 set -ex
46
47 # Create temp dir for generated files.
48 tmp_dir=$(mktemp -d -t certificatesXXX)
49 clean_up() {
50 ARG=$?
51 if [ $dry_run = true ]; then
52 echo "Dry-run complete. Generated files can be found in $tmp_dir"
53 else
54 rm -rf "$tmp_dir"
55 fi
56 exit $ARG
57 }
58 trap clean_up EXIT
59
60 gen_ssl_conf() {
61 domain_name=$1
62 output_file=$2
63
64 cat << EOF > "$output_file"
65 [ req ]
66 prompt = no
67 default_bits = 2048
68 distinguished_name = req_distinguished_name
69 req_extensions = req_ext
70
71 [ req_distinguished_name ]
72 countryName = AU
73 stateOrProvinceName = Australia
74 localityName = Sydney
75 organizationName = MyOrgName
76 commonName = MyCommonName
77
78 [ req_ext ]
79 subjectAltName = @alt_names
80
81 [alt_names]
82 DNS.1 = $domain_name
83 EOF
84 }
85
86 # Generate config files.
87 gen_ssl_conf "$domain" "$tmp_dir/ssl.conf"
88
89 # Create CA (accept defaults from prompts).
90 openssl genrsa -out "$tmp_dir/ca${suffix}.key" 2048
91 openssl req -new -key "$tmp_dir/ca${suffix}.key" -x509 -days 3650 -out "$tmp_dir/ca${suffix}.crt" -config "$tmp_dir/ssl.conf"
92
93 # Create client and server keys.
94 openssl genrsa -out "$tmp_dir/server${suffix}.key" 2048
95 openssl genrsa -out "$tmp_dir/client${suffix}.key" 2048
96
97 # Create certificate sign request using the above created keys.
98 openssl req -new -nodes -key "$tmp_dir/server${suffix}.key" -out "$tmp_dir/server${suffix}.csr" -config "$tmp_dir/ssl.conf"
99 openssl req -new -nodes -key "$tmp_dir/client${suffix}.key" -out "$tmp_dir/client${suffix}.csr" -config "$tmp_dir/ssl.conf"
100
101 # Creating the client and server certificates.
102 openssl x509 -req \
103 -sha256 \
104 -days 3650 \
105 -in "$tmp_dir/server${suffix}.csr" \
106 -out "$tmp_dir/server${suffix}.crt" \
107 -extensions req_ext \
108 -CA "$tmp_dir/ca${suffix}.crt" \
109 -CAkey "$tmp_dir/ca${suffix}.key" \
110 -CAcreateserial \
111 -extfile "$tmp_dir/ssl.conf"
112 openssl x509 -req \
113 -sha256 \
114 -days 3650 \
115 -in "$tmp_dir/client${suffix}.csr" \
116 -out "$tmp_dir/client${suffix}.crt" \
117 -extensions req_ext \
118 -CA "$tmp_dir/ca${suffix}.crt" \
119 -CAkey "$tmp_dir/ca${suffix}.key" \
120 -CAcreateserial \
121 -extfile "$tmp_dir/ssl.conf"
122
123 # Copy files if not in dry-run mode.
124 if [ $dry_run = false ]; then
125 cp "$tmp_dir/ca${suffix}.crt" \
126 "$tmp_dir/client${suffix}.crt" \
127 "$tmp_dir/client${suffix}.key" \
128 "$tmp_dir/server${suffix}.crt" \
129 "$tmp_dir/server${suffix}.key" \
130 "$output_dir"
131 fi