feat: add local pki, https ingress and api mtls
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
"""Generate local stand identities and credentials, once per secrets volume."""
|
||||
|
||||
import os
|
||||
import secrets
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path("/state")
|
||||
ROLES = {"api": 10, "worker": 5, "relay": 3, "reconciler": 2}
|
||||
|
||||
|
||||
def openssl(*args):
|
||||
subprocess.run(["openssl", *args], check=True, stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.PIPE)
|
||||
|
||||
|
||||
def write(path, value):
|
||||
path.write_text(value, encoding="utf-8")
|
||||
|
||||
|
||||
def own(directory, uid):
|
||||
for path in directory.iterdir():
|
||||
path.chmod(0o400)
|
||||
os.chown(path, uid, -1)
|
||||
os.chown(directory, uid, -1)
|
||||
|
||||
|
||||
def ca(directory, name):
|
||||
key, crt = directory / f"{name}.key", directory / f"{name}.crt"
|
||||
openssl("req", "-new", "-newkey", "rsa:3072", "-nodes", "-x509", "-sha256",
|
||||
"-days", "365", "-subj", f"/CN={name}", "-addext",
|
||||
"basicConstraints=critical,CA:TRUE", "-addext", "keyUsage=critical,keyCertSign,cRLSign",
|
||||
"-keyout", key, "-out", crt)
|
||||
return key, crt
|
||||
|
||||
|
||||
def leaf(directory, name, issuer, purpose="server", extra_san=""):
|
||||
key, crt = directory / f"{name}.key", directory / f"{name}.crt"
|
||||
csr, ext = directory / "request.csr", directory / "extensions.cnf"
|
||||
openssl("req", "-new", "-newkey", "rsa:2048", "-nodes", "-subj", f"/CN={name}",
|
||||
"-keyout", key, "-out", csr)
|
||||
write(ext, "basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\n"
|
||||
"subjectKeyIdentifier=hash\nauthorityKeyIdentifier=keyid\n"
|
||||
f"extendedKeyUsage={purpose}Auth\nsubjectAltName=DNS:{name}{extra_san}\n")
|
||||
openssl("x509", "-req", "-in", csr, "-out", crt, "-CA", issuer[1], "-CAkey",
|
||||
issuer[0], "-set_serial", f"0x{secrets.token_hex(16)}", "-sha256",
|
||||
"-days", "90", "-extfile", ext)
|
||||
csr.unlink()
|
||||
ext.unlink()
|
||||
return key, crt
|
||||
|
||||
|
||||
def copy_ca(directory, issuer):
|
||||
shutil.copyfile(issuer[1], directory / "ca.crt")
|
||||
|
||||
|
||||
def bundle(directory, name, pair):
|
||||
write(directory / name, pair[1].read_text() + pair[0].read_text())
|
||||
|
||||
|
||||
def main():
|
||||
if (ROOT / "initialized").is_file():
|
||||
print("Local identities already initialized")
|
||||
return
|
||||
os.umask(0o077)
|
||||
names = ("operator", "lb", "checks", *ROLES)
|
||||
directories = {name: ROOT / name for name in names}
|
||||
for directory in directories.values():
|
||||
directory.mkdir(exist_ok=True)
|
||||
operator = directories["operator"]
|
||||
server_ca = ca(operator, "lab7-servers")
|
||||
client_ca = ca(operator, "lab7-clients")
|
||||
for directory in directories.values():
|
||||
copy_ca(directory, server_ca)
|
||||
|
||||
api = directories["api"]
|
||||
lb = directories["lb"]
|
||||
checks = directories["checks"]
|
||||
leaf(api, "api", server_ca)
|
||||
shutil.copyfile(client_ca[1], api / "clients.crt")
|
||||
lb_client = leaf(lb, "lb.lab7.internal", client_ca, "client")
|
||||
bundle(lb, "client.pem", lb_client)
|
||||
for path in lb_client:
|
||||
shutil.copyfile(path, checks / path.name)
|
||||
bundle(lb, "ingress.pem", leaf(lb, "localhost", server_ca, extra_san=",IP:127.0.0.1"))
|
||||
leaf(checks, "unrelated.lab7.internal", client_ca, "client")
|
||||
foreign_ca = ca(operator, "lab7-untrusted")
|
||||
foreign_dir = checks / "foreign"
|
||||
foreign_dir.mkdir(exist_ok=True)
|
||||
foreign = leaf(foreign_dir, "lb.lab7.internal", foreign_ca, "client")
|
||||
for path in foreign:
|
||||
path.rename(checks / f"foreign-{path.name}")
|
||||
foreign_dir.rmdir()
|
||||
shutil.copyfile(foreign_ca[1], checks / "foreign-ca.crt")
|
||||
|
||||
shutil.copyfile(api / "api.crt", checks / "api.crt")
|
||||
owners = {"lb": 1000, **dict.fromkeys(ROLES, 1000)}
|
||||
for name, uid in owners.items():
|
||||
own(directories[name], uid)
|
||||
(ROOT / "initialized").touch()
|
||||
print("Generated local TLS identities and stand credentials")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,11 +1,13 @@
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 2000
|
||||
ssl-default-bind-options ssl-min-ver TLSv1.2
|
||||
ssl-default-server-options ssl-min-ver TLSv1.2
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
log-format "%ci:%cp %ft %b/%s %ST %Tt"
|
||||
option dontlognull
|
||||
timeout connect 1s
|
||||
timeout client 30s
|
||||
@@ -31,13 +33,19 @@ resolvers docker
|
||||
accepted_payload_size 8192
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
bind :8443 ssl crt /run/tls/ingress.pem
|
||||
default_backend api
|
||||
|
||||
frontend local-health
|
||||
bind 127.0.0.1:8404
|
||||
no log
|
||||
http-request return status 200 if { nbsrv(api) gt 0 }
|
||||
http-request return status 503
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
http-request set-timeout server 60s if { method POST } { path_reg ^/api/v1/chats/[^/]+/attachments$ }
|
||||
option httpchk GET /health/core
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none
|
||||
server-template api 1-4 api:8080
|
||||
server-template api 1-4 api:8080 ssl ca-file /run/tls/ca.crt verify required verifyhost api sni str(api) check-sni api crt /run/tls/client.pem
|
||||
|
||||
Reference in New Issue
Block a user