forked from mtusi2026/mtusi2026lab7
ops: add PKI and HTTPS ingress
This commit is contained in:
@@ -58,6 +58,13 @@ service:
|
||||
telemetry:
|
||||
logs:
|
||||
level: info
|
||||
metrics:
|
||||
readers:
|
||||
- pull:
|
||||
exporter:
|
||||
prometheus:
|
||||
host: 0.0.0.0
|
||||
port: 8888
|
||||
pipelines:
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
|
||||
Executable
+107
@@ -0,0 +1,107 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
umask 077
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
repo_dir=$(CDPATH= cd -- "$script_dir/../.." && pwd)
|
||||
security_dir="$repo_dir/.runtime/security"
|
||||
staging_dir="$security_dir/.bootstrap.$$"
|
||||
|
||||
cleanup() {
|
||||
rm -rf "$staging_dir"
|
||||
}
|
||||
|
||||
trap cleanup EXIT HUP INT TERM
|
||||
|
||||
if [ "${1:-}" = "--force" ]; then
|
||||
force=1
|
||||
elif [ "$#" -eq 0 ]; then
|
||||
force=0
|
||||
else
|
||||
echo "usage: $0 [--force]" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if [ -d "$security_dir/public-ca" ] || [ -d "$security_dir/internal-ca" ]; then
|
||||
if [ "$force" -ne 1 ]; then
|
||||
echo "security material already exists; use --force to replace it" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
mkdir -p "$security_dir" "$staging_dir"
|
||||
chmod 0700 "$security_dir" "$staging_dir"
|
||||
|
||||
create_ca() {
|
||||
name=$1
|
||||
common_name=$2
|
||||
directory="$staging_dir/$name"
|
||||
|
||||
mkdir -p "$directory"
|
||||
openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$directory/ca.key"
|
||||
openssl req -new -x509 -sha256 -days 3650 \
|
||||
-key "$directory/ca.key" \
|
||||
-out "$directory/ca.crt" \
|
||||
-subj "/CN=$common_name" \
|
||||
-config "$script_dir/templates/ca.cnf" \
|
||||
-extensions v3_ca
|
||||
}
|
||||
|
||||
create_leaf() {
|
||||
name=$1
|
||||
common_name=$2
|
||||
issuer=$3
|
||||
template=$4
|
||||
directory="$staging_dir/$name"
|
||||
issuer_directory="$staging_dir/$issuer"
|
||||
|
||||
mkdir -p "$directory"
|
||||
openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$directory/tls.key"
|
||||
openssl req -new -sha256 \
|
||||
-key "$directory/tls.key" \
|
||||
-out "$directory/tls.csr" \
|
||||
-subj "/CN=$common_name"
|
||||
openssl x509 -req -sha256 -days 30 \
|
||||
-in "$directory/tls.csr" \
|
||||
-CA "$issuer_directory/ca.crt" \
|
||||
-CAkey "$issuer_directory/ca.key" \
|
||||
-CAcreateserial \
|
||||
-out "$directory/tls.crt" \
|
||||
-extfile "$script_dir/templates/$template"
|
||||
rm "$directory/tls.csr"
|
||||
}
|
||||
|
||||
create_ca public-ca "MTUSI Lab 7 Public CA"
|
||||
create_ca internal-ca "MTUSI Lab 7 Internal CA"
|
||||
create_ca foreign-ca "MTUSI Lab 7 Foreign CA"
|
||||
|
||||
create_leaf ingress localhost public-ca ingress.cnf
|
||||
create_leaf api api internal-ca api.cnf
|
||||
create_leaf load-balancer load-balancer internal-ca load-balancer.cnf
|
||||
create_leaf prometheus prometheus internal-ca prometheus.cnf
|
||||
create_leaf forbidden-client worker internal-ca forbidden-client.cnf
|
||||
create_leaf foreign-client load-balancer foreign-ca load-balancer.cnf
|
||||
|
||||
openssl verify -purpose sslserver -verify_hostname localhost \
|
||||
-CAfile "$staging_dir/public-ca/ca.crt" "$staging_dir/ingress/tls.crt" >/dev/null
|
||||
openssl verify -purpose sslserver -verify_hostname api \
|
||||
-CAfile "$staging_dir/internal-ca/ca.crt" "$staging_dir/api/tls.crt" >/dev/null
|
||||
openssl verify -purpose sslclient \
|
||||
-CAfile "$staging_dir/internal-ca/ca.crt" \
|
||||
"$staging_dir/load-balancer/tls.crt" \
|
||||
"$staging_dir/prometheus/tls.crt" \
|
||||
"$staging_dir/forbidden-client/tls.crt" >/dev/null
|
||||
openssl verify -purpose sslclient \
|
||||
-CAfile "$staging_dir/foreign-ca/ca.crt" "$staging_dir/foreign-client/tls.crt" >/dev/null
|
||||
|
||||
find "$staging_dir" -type d -exec chmod 0700 {} \;
|
||||
find "$staging_dir" -type f -exec chmod 0600 {} \;
|
||||
|
||||
for directory in public-ca internal-ca foreign-ca ingress api load-balancer prometheus forbidden-client foreign-client; do
|
||||
rm -rf "$security_dir/$directory"
|
||||
mv "$staging_dir/$directory" "$security_dir/$directory"
|
||||
done
|
||||
|
||||
echo "local PKI created in .runtime/security"
|
||||
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
umask 077
|
||||
|
||||
rm -rf /target/api/internal-ca /target/api/api
|
||||
mkdir -p /target/api/internal-ca /target/api/api
|
||||
cp /source/internal-ca/ca.crt /target/api/internal-ca/ca.crt
|
||||
cp /source/api/tls.crt /target/api/api/tls.crt
|
||||
cp /source/api/tls.key /target/api/api/tls.key
|
||||
chown -R 10001:10001 /target/api
|
||||
find /target/api -type d -exec chmod 0700 {} \;
|
||||
find /target/api -type f -exec chmod 0600 {} \;
|
||||
|
||||
rm -rf /target/nginx/ingress /target/nginx/public-ca /target/nginx/internal-ca /target/nginx/load-balancer
|
||||
mkdir -p /target/nginx/ingress /target/nginx/public-ca /target/nginx/internal-ca /target/nginx/load-balancer
|
||||
cp /source/ingress/tls.crt /target/nginx/ingress/tls.crt
|
||||
cp /source/ingress/tls.key /target/nginx/ingress/tls.key
|
||||
cp /source/public-ca/ca.crt /target/nginx/public-ca/ca.crt
|
||||
cp /source/internal-ca/ca.crt /target/nginx/internal-ca/ca.crt
|
||||
cp /source/load-balancer/tls.crt /target/nginx/load-balancer/tls.crt
|
||||
cp /source/load-balancer/tls.key /target/nginx/load-balancer/tls.key
|
||||
chown -R 0:0 /target/nginx
|
||||
find /target/nginx -type d -exec chmod 0700 {} \;
|
||||
find /target/nginx -type f -exec chmod 0600 {} \;
|
||||
|
||||
rm -rf /target/prometheus/internal-ca /target/prometheus/prometheus
|
||||
mkdir -p /target/prometheus/internal-ca /target/prometheus/prometheus
|
||||
cp /source/internal-ca/ca.crt /target/prometheus/internal-ca/ca.crt
|
||||
cp /source/prometheus/tls.crt /target/prometheus/prometheus/tls.crt
|
||||
cp /source/prometheus/tls.key /target/prometheus/prometheus/tls.key
|
||||
chown -R 65534:65534 /target/prometheus
|
||||
find /target/prometheus -type d -exec chmod 0700 {} \;
|
||||
find /target/prometheus -type f -exec chmod 0600 {} \;
|
||||
|
||||
rm -rf /target/client/public-ca
|
||||
mkdir -p /target/client/public-ca
|
||||
cp /source/public-ca/ca.crt /target/client/public-ca/ca.crt
|
||||
chown -R 10001:10001 /target/client
|
||||
find /target/client -type d -exec chmod 0700 {} \;
|
||||
find /target/client -type f -exec chmod 0600 {} \;
|
||||
@@ -0,0 +1,10 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
DNS.1 = api
|
||||
URI.1 = spiffe://mtusi/lab7/api
|
||||
@@ -0,0 +1,12 @@
|
||||
[req]
|
||||
distinguished_name = distinguished_name
|
||||
prompt = no
|
||||
|
||||
[distinguished_name]
|
||||
CN = local-ca
|
||||
|
||||
[v3_ca]
|
||||
basicConstraints = critical,CA:TRUE,pathlen:0
|
||||
keyUsage = critical,keyCertSign,cRLSign
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid:always
|
||||
@@ -0,0 +1,9 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
URI.1 = spiffe://mtusi/lab7/worker
|
||||
@@ -0,0 +1,11 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
DNS.1 = localhost
|
||||
DNS.2 = nginx
|
||||
IP.1 = 127.0.0.1
|
||||
@@ -0,0 +1,9 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
URI.1 = spiffe://mtusi/lab7/load-balancer
|
||||
@@ -0,0 +1,4 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/prometheus
|
||||
Reference in New Issue
Block a user