diff --git a/.gitignore b/.gitignore index de5742c..81f4748 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ __pycache__/ dist/ secrets.yml .env -root-ca-home/ \ No newline at end of file +root-ca-home/ +ndnd-root-ca-home/ \ No newline at end of file diff --git a/README.md b/README.md index 977bff2..35bbf10 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,10 @@ docker compose exec -e HOME=/testbed/root-ca-home master ndnsec list -c Each node runs the pinned `ndnd-repo` container with identity `/ndnd/repo` (e.g. `/ndn/edu/ucla/ndnd/repo`). Master generates a dedicated key, obtains a certificate from the local site CA, and installs the public chain selected by `globals.ndnd_repo_anchors`. The repo can write only its keychain and storage; it has no access to CA private keys or master renewal scripts. +## NDNd Distance Vector + +Nodes with `ndnd_enabled: true` run the NDNd forwarder and a separate `ndnd dv` router. The router connects only to the forwarder's `/run/ndnd/ndnd.sock` and exchanges DV traffic over the forwarder's UDP listener on port 6364; it does not use NFD's socket or NLSR. Router identities are root-signed as `/32=DV`, and each router has permanent UDP neighbor links to the other NDNd-enabled nodes. + ## NDNd Merkle Only UCLA runs the `ndnd-merkle` container, selected by `services.ndnd-merkle.hosts: [UCLA]` in `config.yml`, with identity `/ndn/edu/ucla/ndnd/merkle`. Master automatically provisions and renews its dedicated site-issued certificate using the same testbed anchors as the repo. Its trust schema permits testbed-certified users to append under their own identities and only the log key to sign responses. History persists in `dist/ndnd-merkle/storage`. @@ -144,3 +148,70 @@ sudo systemctl enable unattended-upgrades sudo systemctl start unattended-upgrades sudo unattended-upgrades --debug ``` + +## NDNd status + +The NDNd forwarder, DV router, status server, CA services, and `/ndnd` Merkle log +use the digest-pinned DV2 staging image configured in Compose. The `/ndnd` +repository retains its separately pinned DV2 image. +Upgrade the forwarder, DV router, and status server together; the DV2 BIFT +management dataset uses TLV encoding. Both repository configurations explicitly +supply their management trust schema. + +The NFD-connected `ndnd-repo` retains the October 22 image because DV2's local +prefix advertising uses the DV management API instead of NFD RIB registration. +The NFD-connected Merkle log also retains the October 22 image. + +On hosts with `ndnd_enabled: true`, Caddy exposes: + +- `/ndnd-status/`: HTML status page. +- `/ndnd-status/status.xml`: XML status report. +- `/ndnd-status/status.json`: JSON status report. + +For example: `https://suns.cs.ucla.edu/ndnd-status/status.json`. + +The renderer compiles `templates/ndnd-dv/dv-schema.lvs.j2` for both DV and the +status server. It permits only inventoried NDNd router certificates, issued +directly by the `/ndnd` root, to sign their routing and status datasets. DV +explicitly loads this schema; the status server receives the same schema and +the public root certificate in PEM format through a separate read-only mount. +It has no access to router private keys. Changes to either status trust file +cause the existing renderer to restart the status server. +The same compiled schema is published at `/file-server/ndnd/dv-schema.tlv`, +matching the schema URL in the NDNd CA profile. + +The status server validates `//32=DV/32=STATUS` before including the +routing dataset. It does not fall back to an unrestricted schema. The local +DV summary remains available if the signed dataset cannot be validated; check +the status server logs and the report's routing state in that case. + + +## NDNd network bridge + +NDNd-enabled sites bridge the two forwarders locally over UDP. NFD sends `/ndnd` +Interests to NDNd through a capture route, excluding inherited root routes, and +advertises `/ndnd` through NLSR. NDNd installs a `/ndn` PET +next hop toward NFD and advertises it through DV. Producers still register on +the network matching their top-level prefix; the bridge does not redistribute +producer registrations or change application trust policies. + +Each enabled host needs a unique, stable `ndnd_bier_index`. The forwarder loads +that index at startup, and `framework/ndnd-network-init.py` registers the same +router-to-index inventory on every node and rebuilds the BIFT. Both cron scripts +run this initializer after service updates. It binds announcements to permanent +UDP faces, restores entries after restarts, and reloads NDNd-connected producers +when their forwarder's startup time changes. The NFD-connected repository and +Merkle service are outside that restart group. + +NDNd uses the NLSR neighbor connections between enabled sites. DV2 currently +uses a fixed cost of 1 per link; it does not use NLSR's configured link costs. + +To reconcile a running node manually from its checkout: + +```bash +docker compose exec -T -e PWD="$PWD" master python3 framework/ndnd-network-init.py +``` + +The bridge has static routes. An abrupt peer failure does not automatically +withdraw its gateway advertisement; alternative gateway advertisements alone do +not guarantee immediate failover. Gateway health tracking is not implemented here. diff --git a/anchors/ndnd-go-root.base64 b/anchors/ndnd-go-root.base64 new file mode 100644 index 0000000..2e12a12 --- /dev/null +++ b/anchors/ndnd-go-root.base64 @@ -0,0 +1 @@ +BugHIwgEbmRuZAgDS0VZCAiErVznAwK56QgCTkE2CAAAAaFYpS5aFAkYAQIZBAA27oAVLDAqMAUGAytlcAMhAOi1L42QxRzGB/yMvduMM19dM3ukSmyubURXLZG9RvY1FkYbAQUcFwcVCARuZG5kCANLRVkICIStXOcDArnp/QD9Jv0A/g8yMDI2MTAyMFQwOTI5NDL9AP8PMjAyNzEwMjBUMDkyOTQyF0Aj+qOlJSdFN9wGXmnobfYLUmjqNikwnydWB6qUAmp1EGZN5d3q8kcQ5By/6B44uzeVau5TksFdwRZKoB7HtxwE \ No newline at end of file diff --git a/config.yml b/config.yml index 4fc53d5..1f567f9 100644 --- a/config.yml +++ b/config.yml @@ -6,6 +6,42 @@ host_vars_path: host_vars # This must match exactly with the docker-compose.yml file services: + ndnd: + enabled_by: ndnd_enabled + profile: ndnd + template_path: templates/ndnd + render_path: dist/ndnd + + ndnd-dv: + enabled_by: ndnd_enabled + profile: ndnd + template_path: templates/ndnd-dv + render_path: dist/ndnd-dv-independent + + ndnd-http-status-server: + enabled_by: ndnd_enabled + profile: ndnd + template_path: templates/ndnd-http-status-server + render_path: dist/ndnd-http-status-server + + ndnd-go-ca: + enabled_by: ndnd_enabled + profile: ndnd-go-ca + template_path: templates/ndnd-site-ca + render_path: dist/ndnd-site-ca-config + + ndnd-go-root-ca: + hosts: [UCLA] + profile: ndnd-go-root-ca + template_path: templates/ndnd-go-ca + render_path: dist/ndnd-go-root-config + + ndnd-root-http-ca: + hosts: [UCLA] + profile: ndnd-go-root-ca + template_path: templates/ndnd-root-http-ca + render_path: dist/ndnd-go-root-config + caddy: template_path: templates/caddy render_path: dist/caddy @@ -39,12 +75,25 @@ services: template_path: templates/ndnd-repo render_path: dist/ndnd-repo + ndnd-network-repo: + enabled_by: ndnd_enabled + profile: ndnd + template_path: templates/ndnd-network-repo + render_path: dist/ndnd-network-repo + ndnd-merkle: hosts: [UCLA] profile: merkle-log template_path: templates/ndnd-merkle render_path: dist/ndnd-merkle + ndnd-network-merkle: + hosts: [UCLA] + enabled_by: ndnd_enabled + profile: ndnd-merkle-log + template_path: templates/ndnd-network-merkle + render_path: dist/ndnd-network-merkle + ndncert: template_path: templates/ndncert render_path: dist/ndncert @@ -74,6 +123,21 @@ globals: # Explicitly select the active root; retain additional roots here during rollover. ndnd_repo_anchors: - anchors/ndn-testbed-root.ndncert.2204.base64 + ndnd_udp_port: 6364 + ndnd_dv_network: /ndnd + ndnd_ca_host: UCLA + ndnd_ca_image: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + ndnd_ca_info: "NDNd Testbed Go CA" + ndnd_ca_max_validity_period: "7776000" + ndnd_ca_max_suffix_length: "5" + ndnd_ca_supported_challenges: [email, dns] + ndnd_site_ca_supported_challenges: [email] + ndnd_root_certificate_url: https://suns.cs.ucla.edu/file-server/ndnd/ndnd-root.ndncert + ndnd_root_certificate_sha256: c020b0ddf2c82dcbdbb8339bc63d8a63d8fece4be41d3f69097cd3185dd47889 + ndnd_root_http_ca_url: https://suns.cs.ucla.edu/ndnd-ca + ndnd_root_anchor_path: anchors/ndnd-go-root.base64 + ndnd_trust_schema_url: https://suns.cs.ucla.edu/file-server/ndnd/dv-schema.tlv + ndnd_root_http_ca_internal_url: http://ndnd-root-http-ca:8777 # Declare variables available for each host # You can use lint.py to check variables, their types and @@ -97,8 +161,12 @@ variables: memsize: number neighbors: dict position: list + ndnd_ca_prefix: "?string" hr_angle: number hr_radius: number ethernet_device: string is_root_ca: "?boolean" is_status_page: "?boolean" + ndnd_enabled: "?boolean" + ndnd_router_prefix: "?string" + ndnd_bier_index: "?number" diff --git a/docker-compose.yml b/docker-compose.yml index 590cc87..d801542 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -43,6 +43,140 @@ services: logging: driver: local + ndnd: + image: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + pull_policy: always + profiles: [ndnd] + init: true + volumes: + - /run/ndnd:/run/ndnd + - ${PWD}/dist/ndnd:/config:ro + environment: + NDN_CLIENT_TRANSPORT: unix:///run/ndnd/ndnd.sock + command: [fw, run, /config/yanfd.yml] + ports: + - 6364:6364/udp + restart: unless-stopped + healthcheck: + test: [CMD, /ndnd, fw, status] + interval: 1m + start_period: 30s + depends_on: + master: { condition: service_healthy } + logging: + driver: local + + ndnd-dv: + image: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + pull_policy: always + profiles: [ndnd] + init: true + volumes: + - /run/ndnd:/run/ndnd:ro + - ${PWD}/dist/ndnd-dv-independent/dv.yml:/config/dv.yml:ro + - ${PWD}/dist/ndnd-dv-independent/dv-schema.tlv:/config/dv-schema.tlv:ro + - ${PWD}/dist/ndnd-dv-independent/keys:/config/keys + environment: + NDN_CLIENT_TRANSPORT: unix:///run/ndnd/ndnd.sock + command: [dv, run, /config/dv.yml] + network_mode: none + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + restart: unless-stopped + depends_on: + ndnd: { condition: service_healthy } + logging: + driver: local + + ndnd-http-status-server: + image: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + pull_policy: always + profiles: [ndnd] + init: true + volumes: + - /run/ndnd:/run/ndnd:ro + - ${PWD}/dist/ndnd-http-status-server:/config:ro + environment: + NDN_CLIENT_TRANSPORT: unix:///run/ndnd/ndnd.sock + command: [fw, status-server, --trust-schema, /config/dv-schema.tlv, --trust-anchor, /config/root.cert.pem] + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + restart: unless-stopped + depends_on: + ndnd: { condition: service_healthy } + logging: + driver: local + + ndnd-go-ca: + image: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + pull_policy: always + profiles: [ndnd-go-ca] + init: true + volumes: + - /run/ndnd:/run/ndnd:ro + - ${PWD}/dist/ndnd-site-ca-independent:/ca:ro + - ${PWD}/dist/ndnd-site-ca-config:/config:ro + environment: + NDN_CLIENT_TRANSPORT: unix:///run/ndnd/ndnd.sock + NDND_SMTP_PASSWORD: ${VAR_SMTP_PASSWORD:-} + command: [sec, certca, /config/ca.json, /ca/ca.cert.pem, /ca/ca.key.pem] + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + restart: unless-stopped + depends_on: + ndnd: { condition: service_healthy } + logging: + driver: local + + ndnd-go-root-ca: + image: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + pull_policy: always + profiles: [ndnd-go-root-ca] + init: true + volumes: + - /run/ndnd:/run/ndnd:ro + - ${PWD}/ndnd-root-ca-home/ndnd:/ca:ro + - ${PWD}/dist/ndnd-go-root-config:/config:ro + environment: + NDN_CLIENT_TRANSPORT: unix:///run/ndnd/ndnd.sock + NDND_SMTP_PASSWORD: ${VAR_SMTP_PASSWORD:-} + command: [sec, certca, /config/ca.json, /ca/ca.cert.pem, /ca/ca.key.pem] + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + restart: unless-stopped + depends_on: + ndnd: { condition: service_healthy } + logging: + driver: local + + ndnd-root-http-ca: + image: ghcr.io/named-data/testbed-master:20261012 + profiles: [ndnd-go-root-ca] + init: true + tty: true + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - ${PWD}/framework/http-ca-server.py:/app/http-ca-server.py:ro + - ${PWD}/dist/ndnd-go-root-config:/config:ro + - ${PWD}/dist/ndnd-go-root-ca/cert-store:/cert-store + environment: + NDND_ROOT_CA_SECRET: ${VAR_NDND_ROOT_CA_SECRET:?VAR_NDND_ROOT_CA_SECRET must be set} + NDND_CA_IMAGE: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + NDND_CA_HOST_DIR: ${PWD}/ndnd-root-ca-home/ndnd + entrypoint: [python3, /app/http-ca-server.py] + command: [/config/http-ca-server.yml] + restart: unless-stopped + healthcheck: + test: [CMD-SHELL, "python3 -c \"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8777/health', timeout=2).read()\""] + interval: 1m + start_period: 5s + logging: + driver: local + nfd: image: ghcr.io/named-data/nfd:20250420 init: true @@ -136,7 +270,7 @@ services: driver: local ndn-python-repo: - image: ghcr.io/ucla-irl/ndn-python-repo:20240508 + image: ghcr.io/ucla-irl/ndn-python-repo:20261023@sha256:781c3db4aad30a91a0672da787b316599ee08957b63961219f09d7c4408cf763 init: true volumes: - /run/nfd:/run/nfd:ro @@ -154,7 +288,8 @@ services: driver: local ndnd-repo: - image: ghcr.io/named-data/ndnd:20261012 + image: ghcr.io/named-data/ndnd:20261022@sha256:1816103c4fec8ea04253ed3d5f9e5c53c6657067a2cbc4d3c1fbb6be7ed28c3a + pull_policy: always init: true volumes: - /run/nfd:/run/nfd:ro @@ -175,8 +310,41 @@ services: logging: driver: local + ndnd-network-repo: + image: ghcr.io/named-data/ndnd:dv2-staging@sha256:cd362167f0d34804ba79cc33ba654e35d666f39c9a729debafe0e93b40348a9c + profiles: [ndnd] + init: true + volumes: + - /run/ndnd:/run/ndnd:ro + - ${PWD}/dist/ndnd-network-repo/keys:/config/keys + - ${PWD}/dist/ndnd-network-repo/storage:/config/storage + - type: bind + source: ${PWD}/dist/ndnd-network-repo/repo.yml + target: /config/repo.yml + read_only: true + bind: + create_host_path: false + - type: bind + source: ${PWD}/dist/ndnd-network-repo/management-schema.tlv + target: /config/management-schema.tlv + read_only: true + bind: + create_host_path: false + environment: + NDN_CLIENT_TRANSPORT: unix:///run/ndnd/ndnd.sock + command: [repo, run, /config/repo.yml] + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + restart: unless-stopped + depends_on: + ndnd: { condition: service_healthy } + ndnd-dv: { condition: service_started } + logging: + driver: local + ndnd-merkle: - image: ghcr.io/named-data/ndnd:20261012 + image: ghcr.io/named-data/ndnd:20261022@sha256:1816103c4fec8ea04253ed3d5f9e5c53c6657067a2cbc4d3c1fbb6be7ed28c3a pull_policy: always profiles: [merkle-log] # selected by config.yml through maintenance init: true @@ -203,6 +371,44 @@ services: logging: driver: local + ndnd-network-merkle: + image: ghcr.io/named-data/ndnd:dv2-staging@sha256:aba4d409d0f3ed2a7b134bd5d0ade0e0c9c37ad55e69254951c306f961b36d5b + pull_policy: always + profiles: [ndnd-merkle-log] + init: true + volumes: + - /run/ndnd:/run/ndnd:ro + - ${PWD}/dist/ndnd-network-merkle/keys:/config/keys + - ${PWD}/dist/ndnd-network-merkle/storage:/config/storage + - type: bind + source: ${PWD}/dist/ndnd-network-merkle/merkle.yml + target: /config/merkle.yml + read_only: true + bind: + create_host_path: false + - type: bind + source: ${PWD}/dist/ndnd-network-merkle/management-schema.tlv + target: /config/management-schema.tlv + read_only: true + bind: + create_host_path: false + environment: + NDN_CLIENT_TRANSPORT: unix:///run/ndnd/ndnd.sock + command: [merkle, run, /config/merkle.yml] + network_mode: none + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + mem_limit: 2g + cpus: 1 + pids_limit: 128 + restart: unless-stopped + depends_on: + ndnd: { condition: service_healthy } + ndnd-dv: { condition: service_started } + logging: + driver: local + ndncert: image: ghcr.io/ucla-irl/ndncert-ca:20250923 init: true diff --git a/framework/http-ca-server.py b/framework/http-ca-server.py index f2bff58..832c49c 100755 --- a/framework/http-ca-server.py +++ b/framework/http-ca-server.py @@ -7,6 +7,9 @@ import subprocess import ndn import ipaddress +import os +from datetime import datetime, timedelta +from urllib.parse import parse_qs from dataclasses import dataclass from http.server import BaseHTTPRequestHandler, HTTPServer @@ -17,10 +20,33 @@ class Config: secret: str hosts: dict[str, dict] port: int + signer_backend: str = 'ndnsec' + ndnd_ca_image: str = '' + ndnd_ca_host_dir: str = '' config: Config = None + +def is_ndnd_csr_name(prefix: str, name: list[bytes]) -> bool: + prefix_name = ndn.encoding.Name.normalize(prefix) + if len(name) != len(prefix_name) + 4 or name[:len(prefix_name)] != prefix_name: + return False + component = ndn.encoding.Component + return (name[len(prefix_name)] == component.from_str('KEY') + and name[len(prefix_name) + 2] == component.from_str('self') + and component.get_type(name[-1]) == component.TYPE_VERSION) + + class MyHTTPRequestHandler(BaseHTTPRequestHandler): + def do_GET(self): + if self.path == '/health': + self.send_response(200) + self.send_header('Content-Type', 'text/plain; charset=utf-8') + self.end_headers() + self.wfile.write(b'ok\n') + return + self.send_error(404) + def which_host_ip(self, address: str) -> dict|None: for _, host in config.hosts.items(): for subnet in host['subnets']: @@ -30,7 +56,13 @@ def which_host_ip(self, address: str) -> dict|None: def sign(self, path: str, query: str, data: bytes) -> None: # Check if the secret is correct from query parameter - if query != f'secret={config.secret}': + try: + params = parse_qs(query, strict_parsing=True) + except ValueError: + self.send_error(400, "Invalid query parameters") + return + authorization = self.headers.get('Authorization') + if authorization != f'Bearer {config.secret}' and params.get('secret') != [config.secret]: self.send_error(401, "Invalid secret") return @@ -44,13 +76,69 @@ def sign(self, path: str, query: str, data: bytes) -> None: # Validate prefix of the CSR pfx = host['prefix'] name = ndn.encoding.ndn_format_0_3.parse_data(base64.b64decode(data))[0] - if not ndn.encoding.name.Name.is_prefix(pfx, name): + if config.signer_backend == 'ndnd-go': + valid_name = is_ndnd_csr_name(pfx, name) + ca_prefix = host.get('ca_prefix') + if ca_prefix: + valid_name |= is_ndnd_csr_name(ca_prefix, name) + else: + valid_name = ndn.encoding.name.Name.is_prefix(pfx, name) + if not valid_name: str_name = ndn.encoding.name.Name.to_str(name) self.send_error(400, f"Invalid name {str_name}, expected prefix {pfx}") return - # Sign a certificate for the name - proc = subprocess.run(['ndnsec', 'cert-gen', '-s', config.global_prefix, '-'], input=data, capture_output=True) + not_before = params.get('not_before', []) + not_after = params.get('not_after', []) + if bool(not_before) != bool(not_after) or len(not_before) > 1 or len(not_after) > 1: + self.send_error(400, "Both validity bounds must be provided once") + return + + if not_before: + try: + start = datetime.strptime(not_before[0], '%Y%m%d%H%M%S') + end = datetime.strptime(not_after[0], '%Y%m%d%H%M%S') + except ValueError: + self.send_error(400, "Invalid certificate validity bounds") + return + if end <= start or end - start > timedelta(days=90): + self.send_error(400, "Certificate validity must be positive and no longer than 90 days") + return + + if config.signer_backend == 'ndnd-go': + if not config.ndnd_ca_image or not config.ndnd_ca_host_dir: + self.send_error(500, "NDNd Go signer is not configured") + return + try: + csr_wire = base64.b64decode(b''.join(data.split()), validate=True) + except ValueError: + self.send_error(400, "Invalid base64 CSR") + return + csr_pem = (b'-----BEGIN NDN CERT-----\n' + base64.encodebytes(csr_wire) + + b'-----END NDN CERT-----\n') + command = ['docker', 'run', '--rm', '-i', '-v', + f'{config.ndnd_ca_host_dir}:/ca:ro', config.ndnd_ca_image, + 'sec', 'sign-cert', '/ca/ca.key.pem'] + if not_before: + command.extend(['--start', not_before[0], '--end', not_after[0]]) + proc = subprocess.run(command, input=csr_pem, capture_output=True, timeout=30) + if proc.returncode == 0: + decoded = subprocess.run( + ['docker', 'run', '--rm', '-i', config.ndnd_ca_image, 'sec', 'pem-decode'], + input=proc.stdout, capture_output=True, timeout=30) + if decoded.returncode != 0: + proc = decoded + else: + proc.stdout = base64.encodebytes(decoded.stdout) + elif config.signer_backend == 'ndnsec': + command = ['ndnsec', 'cert-gen', '-s', config.global_prefix] + if not_before: + command.extend(['-S', not_before[0], '-E', not_after[0]]) + command.append('-') + proc = subprocess.run(command, input=data, capture_output=True, timeout=30) + else: + self.send_error(500, "Unsupported signer backend") + return if proc.returncode != 0: self.send_error(500, f"Failed to sign certificate: {proc.stderr}") @@ -66,8 +154,13 @@ def sign(self, path: str, query: str, data: bytes) -> None: self.end_headers() self.wfile.write(certificate) - # Store the certificate if path provided - if path := host.get('cert_file'): + # This cache is consumed as the host's site certificate, not as a + # general log of every descendant certificate signed for that host. + identity = ndn.encoding.name.Name.to_str(cert_name[:-4]) if len(cert_name) >= 4 else '' + ca_prefix = host.get('ca_prefix') + path = host.get('ca_cert_file') if ca_prefix and identity == ca_prefix else host.get('cert_file') + expected_identity = ca_prefix if ca_prefix and identity == ca_prefix else pfx + if path and identity == expected_identity: with open(path, 'wb') as f: f.write(certificate) @@ -99,10 +192,22 @@ def address_string(self) -> str: return self.client_address[0] + def log_request(self, code='-', size='-') -> None: + # Legacy callers put the root CA secret in the query string. + requestline = self.requestline.split('?', 1)[0] + self.log_message('"%s" %s %s', requestline, str(code), str(size)) + if __name__ == '__main__': # Read YAML config with open(sys.argv[1], 'r') as f: - config = Config(**yaml.safe_load(f)) + config_data = yaml.safe_load(f) + if os.environ.get('NDND_ROOT_CA_SECRET'): + config_data['secret'] = os.environ['NDND_ROOT_CA_SECRET'] + config_data['ndnd_ca_image'] = os.environ.get('NDND_CA_IMAGE', config_data.get('ndnd_ca_image', '')) + config_data['ndnd_ca_host_dir'] = os.environ.get('NDND_CA_HOST_DIR', config_data.get('ndnd_ca_host_dir', '')) + if not config_data.get('secret'): + raise RuntimeError('HTTP CA secret must be configured') + config = Config(**config_data) # Start server server_address = ('', config.port) diff --git a/framework/internal/conf.py b/framework/internal/conf.py index 88017c6..b51451f 100644 --- a/framework/internal/conf.py +++ b/framework/internal/conf.py @@ -3,6 +3,7 @@ from dataclasses import dataclass from os import getenv +from pathlib import Path from typing import Any from .utils import read_dotenv @@ -34,7 +35,16 @@ def get() -> Config: def service_enabled(service: dict, node: str) -> bool: - return 'hosts' not in service or node in service['hosts'] + if 'hosts' in service and node not in service['hosts']: + return False + enabled_by = service.get('enabled_by') + if enabled_by is None: + return True + if not node: + return False + with open(Path(get().host_vars_path) / node) as stream: + host_vars = yaml.safe_load(stream) or {} + return bool(host_vars.get(enabled_by, False)) def compose_profiles(node: str = None) -> str: diff --git a/framework/internal/ndnd_network.py b/framework/internal/ndnd_network.py new file mode 100644 index 0000000..83d47a2 --- /dev/null +++ b/framework/internal/ndnd_network.py @@ -0,0 +1,138 @@ +"""Initialize BIER and the local NFD–NDNd gateway after service startup.""" + +import json +import os +from pathlib import Path +import re +import subprocess +import time + +import yaml + +from . import conf + + +def router_indices(hosts): + routers = {} + for name, host in hosts.items(): + if not host.get('ndnd_enabled', False): + continue + index = host.get('ndnd_bier_index') + if type(index) is not int or index < 0: + raise ValueError(f'{name}: ndnd_bier_index must be a non-negative integer') + prefix = host['ndnd_router_prefix'] + if index in routers or prefix in routers.values(): + raise ValueError(f'{name}: duplicate NDNd router prefix or BIER index') + routers[index] = prefix + return routers + + +def run(args, check=True, timeout=30): + result = subprocess.run(args, capture_output=True, text=True, timeout=timeout) + if check and result.returncode: + raise RuntimeError(f'{args}: {result.stderr.strip()}') + return result + + +def wait_ready(command): + for _ in range(20): + result = run(command, check=False) + if result.returncode == 0: + return + time.sleep(1) + raise RuntimeError(f'Service not ready: {command}: {result.stderr.strip()}') + + +def face_id(output, uri): + for match in re.finditer(r'^faceid=(\d+) remote=(\S+)', output, re.MULTILINE): + if match[2] == uri: + return match[1] + return None + + +def configure(containers, routers, network, port, previous): + nfd, fw = containers['nfd'], containers['ndnd'] + networks = nfd['NetworkSettings']['Networks'].keys() & fw['NetworkSettings']['Networks'].keys() + if len(networks) != 1: + raise RuntimeError('NFD and NDNd must share exactly one Docker network') + docker_network = next(iter(networks)) + to_nfd = 'udp4://' + nfd['NetworkSettings']['Networks'][docker_network]['IPAddress'] + ':6363' + to_ndnd = 'udp4://' + fw['NetworkSettings']['Networks'][docker_network]['IPAddress'] + ':' + str(port) + nfd_cmd = ['docker', 'exec', nfd['Id'], 'nfdc'] + ndnd_cmd = ['docker', 'exec', fw['Id'], '/ndnd'] + changed = previous.get('forwarder_started') != fw['State']['StartedAt'] + + wait_ready(ndnd_cmd + ['fw', 'status']) + wait_ready(nfd_cmd + ['status']) + for index, prefix in sorted(routers.items()): + run(ndnd_cmd + ['fw', 'bift-register', 'prefix=' + prefix, 'index=' + str(index)]) + run(ndnd_cmd + ['fw', 'bift-rebuild']) + if changed or not containers['ndnd-dv']['State']['Running']: + run(['docker', 'restart', containers['ndnd-dv']['Id']]) + wait_ready(ndnd_cmd + ['dv', 'status']) + + # Remove our previous bridge entries if Docker assigned a new peer address. + if previous.get('to_nfd') not in (None, to_nfd) and not changed: + old_face = face_id(run(ndnd_cmd + ['fw', 'face-list']).stdout, previous['to_nfd']) + if old_face: + run(ndnd_cmd + ['dv', 'prefix-withdraw', 'prefix=/ndn', 'face=' + old_face]) + run(ndnd_cmd + ['fw', 'pet-remove-nexthop', 'prefix=/ndn', 'face=' + old_face]) + if previous.get('to_ndnd') not in (None, to_ndnd): + # A missing old face means NFD already removed its routes. + if face_id(run(nfd_cmd + ['face', 'list']).stdout, previous['to_ndnd']): + run(nfd_cmd + ['route', 'remove', network, previous['to_ndnd']]) + + # nfdc updates existing on-demand faces too; NDNd's create-only CLI returns 409. + # Mount only the socket directory; nfdc needs its own writable client keychain. + socket_dir = next(m['Source'] for m in fw['Mounts'] if m['Destination'] == '/run/ndnd') + run(['docker', 'run', '--rm', '--network', 'none', + '--mount', 'type=bind,src=' + socket_dir + ',dst=/run/ndnd,readonly', + '-e', 'NDN_CLIENT_TRANSPORT=unix:///run/ndnd/ndnd.sock', + '--entrypoint', '/usr/bin/nfdc', nfd['Image'], + 'face', 'create', to_nfd, 'persistency', 'permanent']) + run(nfd_cmd + ['face', 'create', to_ndnd, 'persistency', 'permanent']) + # Exclude inherited application/default routes from the foreign namespace. + run(nfd_cmd + ['route', 'add', network, to_ndnd, 'capture']) + bridge_face = face_id(run(ndnd_cmd + ['fw', 'face-list']).stdout, to_nfd) + if bridge_face is None: + raise RuntimeError('NDNd bridge face was not created') + run(ndnd_cmd + ['fw', 'pet-add-nexthop', 'prefix=/ndn', 'face=' + bridge_face]) + # Bind the announcement to the UDP face, not this short-lived CLI connection. + run(ndnd_cmd + ['dv', 'prefix-announce', 'prefix=/ndn', 'face=' + bridge_face]) + run(ndnd_cmd + ['fw', 'bift-rebuild']) + + if changed: + # These services keep registrations on the forwarder's Unix socket. + dependents = ['ndnd-http-status-server', 'ndnd-go-ca', 'ndnd-go-root-ca', + 'ndnd-network-repo', 'ndnd-network-merkle'] + restart = [containers[s]['Id'] for s in dependents + if s in containers and containers[s]['State']['Running']] + if restart: + run(['docker', 'restart', *restart], timeout=120) + return {'forwarder_started': fw['State']['StartedAt'], 'to_nfd': to_nfd, 'to_ndnd': to_ndnd} + + +def reconcile(node): + config = conf.get() + hosts = {path.name: yaml.safe_load(path.read_text()) + for path in Path(config.host_vars_path).iterdir() if path.is_file()} + if not hosts[node].get('ndnd_enabled', False): + return + routers = router_indices(hosts) + os.environ['COMPOSE_PROFILES'] = conf.compose_profiles(node) + ids = run(['docker', 'compose', 'ps', '-aq']).stdout.split() + containers = {c['Config']['Labels']['com.docker.compose.service']: c + for c in json.loads(run(['docker', 'inspect', *ids]).stdout)} if ids else {} + if 'ndnd-dv' not in containers: + raise RuntimeError('ndnd-dv must be created before NDNd network initialization') + for service in ['nfd', 'ndnd']: + if service not in containers or not containers[service]['State']['Running']: + raise RuntimeError(f'{service} must be running before NDNd network initialization') + state_file = Path('dist/.ndnd-network-state.json') + previous = json.loads(state_file.read_text()) if state_file.exists() else {} + state = configure(containers, routers, config.globals['ndnd_dv_network'], + config.globals['ndnd_udp_port'], previous) + temporary = state_file.with_suffix('.tmp') + temporary.write_text(json.dumps(state)) + temporary.replace(state_file) + print('NDNd BIER and symmetric NFD gateway initialized') diff --git a/framework/ndnd-network-init.py b/framework/ndnd-network-init.py new file mode 100644 index 0000000..d64ddb6 --- /dev/null +++ b/framework/ndnd-network-init.py @@ -0,0 +1,18 @@ +"""Reconcile the local NDNd network after Compose or certificate updates.""" + +import fcntl +import os +from pathlib import Path + +from internal.ndnd_network import reconcile + + +if __name__ == '__main__': + Path('dist').mkdir(exist_ok=True) + with open('dist/.ndnd-network.lock', 'w') as lock: + try: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + except BlockingIOError: + print('NDNd network initialization already running') + else: + reconcile(os.environ['MANAGED_HOST']) diff --git a/framework/ndnd_ca_bootstrap.py b/framework/ndnd_ca_bootstrap.py new file mode 100644 index 0000000..0263a65 --- /dev/null +++ b/framework/ndnd_ca_bootstrap.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 +"""Create and publish the persistent Go NDNd root CA identity.""" + +import base64 +import hashlib +import json +import os +import subprocess +import tempfile +from pathlib import Path + +import yaml +from Cryptodome.PublicKey import ECC, RSA +from ndn.app_support.security_v2 import parse_certificate +from ndn.encoding import Component, ContentType, Name, SignatureType, parse_data +from ndn.security.validator import verify_ecdsa, verify_ed25519, verify_rsa + + +def run_ndnd(image: str, ca_home_host: Path, args: list[str], input_data: bytes | None = None, + mount_ca: bool = False) -> bytes: + command = ["docker", "run", "--rm"] + if input_data is not None: + command.append("-i") + if mount_ca: + command.extend(["-v", f"{ca_home_host}:/ca:ro"]) + command.extend([image, *args]) + result = subprocess.run(command, input=input_data, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if result.returncode != 0: + raise RuntimeError(result.stderr.decode(errors="replace").strip() or "NDNd CLI failed") + return result.stdout + + +def verify_self_signed_root(wire: bytes) -> bool: + cert = parse_certificate(wire) + if cert.meta_info.content_type != ContentType.KEY: + return False + locator = cert.signature_info.key_locator + if locator is None or Name.to_bytes(locator.name) not in ( + Name.to_bytes(cert.name[:-2]), Name.to_bytes(cert.name)): + return False + _, _, _, pointers = parse_data(wire) + if cert.signature_info.signature_type == SignatureType.SHA256_WITH_ECDSA: + return verify_ecdsa(ECC.import_key(bytes(cert.content)), pointers) + if cert.signature_info.signature_type == SignatureType.SHA256_WITH_RSA: + return verify_rsa(RSA.import_key(bytes(cert.content)), pointers) + if cert.signature_info.signature_type == SignatureType.ED25519: + return verify_ed25519(ECC.import_key(bytes(cert.content)), pointers) + return False + + +def atomic_write(path: Path, data: bytes, mode: int) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + descriptor, temporary = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.") + try: + os.fchmod(descriptor, mode) + with os.fdopen(descriptor, "wb") as stream: + stream.write(data) + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, path) + finally: + if os.path.exists(temporary): + os.unlink(temporary) + + +def main() -> None: + project_dir = Path(__file__).resolve().parent.parent + with (project_dir / "config.yml").open() as stream: + config = yaml.safe_load(stream) + values = config["globals"] + if os.environ.get("MANAGED_HOST") != values["ndnd_ca_host"]: + return + + host_root = Path(os.environ.get("ROOT_DIR", str(project_dir))) + ca_home = project_dir / "ndnd-root-ca-home" / "ndnd" + ca_home_host = host_root / "ndnd-root-ca-home" / "ndnd" + public_dir = project_dir / "dist/file-server/ndnd" + anchor_path = project_dir / values["ndnd_root_anchor_path"] + ca_home.mkdir(mode=0o700, parents=True, exist_ok=True) + ca_home.chmod(0o700) + key_path = ca_home / "ca.key.pem" + cert_path = ca_home / "ca.cert.pem" + image = values["ndnd_ca_image"] + ca_prefix = Name.normalize(values["ndnd_dv_network"]) + + if not key_path.exists() and not cert_path.exists(): + atomic_write(key_path, run_ndnd(image, ca_home_host, ["sec", "keygen", "/ndnd", "ed25519"]), 0o600) + elif not key_path.exists(): + raise RuntimeError("NDNd CA certificate exists without its private key; refusing to rotate the root") + + if not cert_path.exists(): + cert = run_ndnd(image, ca_home_host, ["sec", "sign-cert", "/ca/ca.key.pem"], + key_path.read_bytes(), mount_ca=True) + atomic_write(cert_path, cert, 0o644) + + cert_wire = run_ndnd(image, ca_home_host, ["sec", "pem-decode"], cert_path.read_bytes()) + pinned_root = base64.b64decode("".join(anchor_path.read_text().split()), validate=True) + if cert_wire != pinned_root: + raise RuntimeError("NDNd CA certificate does not match the committed /ndnd trust anchor") + cert = parse_certificate(cert_wire) + root_identity = cert.name[:-4] + if Name.to_bytes(root_identity) != Name.to_bytes(ca_prefix): + raise RuntimeError(f"NDNd CA cert identity must be {values['ndnd_dv_network']}, got {Name.to_str(root_identity)}") + if not verify_self_signed_root(cert_wire): + raise RuntimeError("NDNd CA certificate is not validly self-signed") + + cert_name = Name.to_str(cert.name) + cert_digest = hashlib.sha256(cert_wire).hexdigest() + if cert_digest != values["ndnd_root_certificate_sha256"]: + raise RuntimeError("NDNd CA certificate does not match the pinned testbed fingerprint") + previous_cert = public_dir / "ndnd-root-previous.ndncert" + published_cert = public_dir / "ndnd-root.ndncert" + if published_cert.exists() and published_cert.read_bytes() != cert_wire: + atomic_write(previous_cert, published_cert.read_bytes(), 0o644) + atomic_write(published_cert, cert_wire, 0o644) + profile = { + "schema_version": 1, + "ca_prefix": values["ndnd_dv_network"], + "ca_info": values["ndnd_ca_info"], + "max_validity_period_seconds": int(values["ndnd_ca_max_validity_period"]), + "probe_parameters": ["email"], + "name_assignment": {"email": "/nonexist"}, + "max_suffix_length": int(values["ndnd_ca_max_suffix_length"]), + "supported_challenges": values["ndnd_ca_supported_challenges"], + "router_identity_suffix": "/32=DV", + "root_certificate_url": values["ndnd_root_certificate_url"], + "root_certificate_name": cert_name, + "root_certificate_sha256": cert_digest, + "bootstrap_csr_signing_endpoint": values["ndnd_root_http_ca_url"] + "/sign", + "trust_schema_url": values["ndnd_trust_schema_url"], + } + atomic_write(public_dir / "ndnd-ca-profile.json", (json.dumps(profile, indent=2) + "\n").encode(), 0o644) + print(f"Prepared Go CA identity {cert_name}") + + +if __name__ == "__main__": + main() diff --git a/framework/test_ndnd_network.py b/framework/test_ndnd_network.py new file mode 100644 index 0000000..b5582b4 --- /dev/null +++ b/framework/test_ndnd_network.py @@ -0,0 +1,112 @@ +import subprocess +import unittest +from unittest.mock import patch + +import jinja2 +import yaml + +from internal import ndnd_network +from internal import conf + + +class NetworkInitTest(unittest.TestCase): + def setUp(self): + self.commands = [] + self.containers = {} + for service, ip in [('nfd', '172.20.0.2'), ('ndnd', '172.20.0.3'), + ('ndnd-dv', ''), ('ndnd-network-repo', ''), + ('ndnd-go-ca', ''), ('ndnd-repo', ''), ('nlsr', '')]: + self.containers[service] = { + 'Id': service, 'Image': 'nfd-image', + 'Mounts': [{'Source': '/run/ndnd', 'Destination': '/run/ndnd'}, + {'Source': '/read-only/config', 'Destination': '/config'}], + 'State': {'Running': True, 'StartedAt': 'start-1'}, + 'NetworkSettings': {'Networks': {'default': {'IPAddress': ip}}}, + } + self.previous = {'forwarder_started': 'start-1', + 'to_nfd': 'udp4://172.20.0.2:6363', + 'to_ndnd': 'udp4://172.20.0.3:6364'} + + def run_command(self, args, check=True, **kwargs): + self.commands.append(args) + output = '' + if args[-2:] == ['fw', 'face-list']: + output = 'faceid=42 remote=udp4://172.20.0.2:6363\nfaceid=43 remote=udp4://172.20.0.9:6363\n' + elif args[-2:] == ['face', 'list']: + output = 'faceid=70 remote=udp4://172.20.0.8:6364\n' + return subprocess.CompletedProcess(args, 0, output, '') + + def configure(self, previous=None): + with patch.object(ndnd_network, 'run', side_effect=self.run_command): + return ndnd_network.configure(self.containers, {0: '/ndnd/a', 1: '/ndnd/b'}, + '/ndnd', 6364, self.previous if previous is None else previous) + + def test_announcement_uses_persistent_udp_face(self): + state = self.configure() + self.assertIn(['docker', 'exec', 'ndnd', '/ndnd', 'dv', 'prefix-announce', + 'prefix=/ndn', 'face=42'], self.commands) + self.assertEqual(state, self.previous) + self.assertIn(['docker', 'exec', 'nfd', 'nfdc', 'route', 'add', + '/ndnd', self.previous['to_ndnd'], 'capture'], self.commands) + self.assertTrue(any(c[:3] == ['docker', 'run', '--rm'] and + 'type=bind,src=/run/ndnd,dst=/run/ndnd,readonly' in c and + '--volumes-from' not in c and c[-4:] == ['create', self.previous['to_nfd'], + 'persistency', 'permanent'] + for c in self.commands)) + + def test_repeated_initialization_does_not_restart_services(self): + self.configure() + self.assertFalse(any(command[1] == 'restart' for command in self.commands)) + + def test_forwarder_restart_restores_only_its_dependents(self): + self.configure({}) + restarted = [command[2:] for command in self.commands if command[1] == 'restart'] + self.assertEqual(restarted, [['ndnd-dv'], ['ndnd-go-ca', 'ndnd-network-repo']]) + self.assertEqual(sum('bift-register' in command for command in self.commands), 2) + + def test_peer_address_change_removes_old_bridge_routes(self): + previous = dict(self.previous, to_nfd='udp4://172.20.0.9:6363', + to_ndnd='udp4://172.20.0.8:6364') + self.configure(previous) + self.assertIn(['docker', 'exec', 'ndnd', '/ndnd', 'dv', 'prefix-withdraw', + 'prefix=/ndn', 'face=43'], self.commands) + self.assertIn(['docker', 'exec', 'nfd', 'nfdc', 'route', 'remove', + '/ndnd', previous['to_ndnd']], self.commands) + + def test_stopped_dv_restarts_without_restarting_producers(self): + self.containers['ndnd-dv']['State']['Running'] = False + self.configure() + self.assertEqual([c[2:] for c in self.commands if c[1] == 'restart'], [['ndnd-dv']]) + + def test_duplicate_indices_rejected(self): + hosts = {'A': {'ndnd_enabled': True, 'ndnd_bier_index': 0, 'ndnd_router_prefix': '/ndnd/a'}, + 'B': {'ndnd_enabled': True, 'ndnd_bier_index': 0, 'ndnd_router_prefix': '/ndnd/b'}} + with self.assertRaises(ValueError): + ndnd_network.router_indices(hosts) + + def test_invalid_index_rejected(self): + for index in [None, -1, True, 1.5]: + with self.subTest(index=index), self.assertRaises(ValueError): + ndnd_network.router_indices({'A': {'ndnd_enabled': True, 'ndnd_bier_index': index}}) + + def test_enabled_hosts_have_stable_unique_indices_and_bridge_advertisements(self): + from pathlib import Path + config = conf.get() + hosts = {p.name: yaml.safe_load(p.read_text()) + for p in Path(config.host_vars_path).iterdir() if p.is_file()} + indices = ndnd_network.router_indices(hosts) + self.assertEqual(len(indices), 6) + env = jinja2.Environment(loader=jinja2.FileSystemLoader('.'), undefined=jinja2.StrictUndefined) + for name, host in hosts.items(): + context = dict(host, **config.globals) + context['hostvars'] = {n: dict(h, **config.globals) for n, h in hosts.items()} + rendered = env.get_template('templates/nlsr/nlsr.conf.j2').render(**context) + advertising = rendered.split('advertising\n{', 1)[1].split('}', 1)[0] + self.assertEqual('/ndnd 0' in advertising, bool(host.get('ndnd_enabled'))) + if host.get('ndnd_enabled'): + fw = yaml.safe_load(env.get_template('templates/ndnd/yanfd.yml.j2').render(**context)) + self.assertEqual(indices[fw['fw']['bier_index']], host['ndnd_router_prefix']) + + +if __name__ == '__main__': + unittest.main() diff --git a/framework/test_ndnd_status_trust.py b/framework/test_ndnd_status_trust.py new file mode 100644 index 0000000..b898634 --- /dev/null +++ b/framework/test_ndnd_status_trust.py @@ -0,0 +1,86 @@ +import base64 +import hashlib +from pathlib import Path +import unittest + +import jinja2 +import yaml +from ndn.app_support.light_versec import Checker, compile_lvs + +from internal import conf +from internal.render import JINJA_FUNCTIONS + + +class TestNdndStatusTrust(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.config = conf.get() + cls.hostvars = {} + for path in Path(cls.config.host_vars_path).iterdir(): + if path.is_file(): + cls.hostvars[path.name] = { + **yaml.safe_load(path.read_text()), + **cls.config.globals, + 'inventory_hostname': path.name, + } + cls.environment = jinja2.Environment( + loader=jinja2.FileSystemLoader(cls.config.environment_path), + undefined=jinja2.StrictUndefined, + trim_blocks=True, + lstrip_blocks=True, + ) + + def render(self, template, host): + return self.environment.get_template(template).render( + **self.hostvars[host], hostvars=self.hostvars, **JINJA_FUNCTIONS) + + def test_router_and_status_authorization(self): + root = self.config.globals['ndnd_dv_network'] + root_cert = root + '/KEY/root/self/v=1' + for host, values in self.hostvars.items(): + if not values.get('ndnd_enabled'): + continue + with self.subTest(host=host): + routing = self.render('templates/ndnd-dv/dv-schema.lvs.j2', host) + status = self.render('templates/ndnd-http-status-server/dv-schema.lvs.j2', host) + model = compile_lvs(routing) + self.assertEqual(0x00011000, model.version) + self.assertEqual(bytes(model.encode()), bytes(compile_lvs(status).encode())) + published = self.render('templates/file-server/ndnd/dv-schema.lvs.j2', host) + self.assertEqual(bytes(model.encode()), bytes(compile_lvs(published).encode())) + checker = Checker(model, {}) + router = values['ndnd_router_prefix'] + cert = router + '/32=DV/KEY/router/root/v=1' + self.assertTrue(checker.check(cert, root_cert)) + for name in [router + '/32=DV/32=STATUS', + '/localhop' + router + '/32=DV/32=ADV/v=1', + root + '/32=DV/32=PSD' + router + '/t=1/seq=1']: + self.assertTrue(checker.check(name, cert), name) + self.assertFalse(checker.check(name, root_cert), name) + self.assertFalse(checker.check(name, router + '/g/user/KEY/key/site/v=1'), name) + for peer, peer_values in self.hostvars.items(): + if peer != host and peer_values.get('ndnd_enabled'): + peer_cert = peer_values['ndnd_router_prefix'] + '/32=DV/KEY/router/root/v=1' + self.assertFalse(checker.check(name, peer_cert), name) + outsider = root + '/unlisted/32=DV' + self.assertFalse(checker.check(outsider + '/KEY/router/root/v=1', root_cert)) + self.assertFalse(checker.check(outsider + '/32=STATUS', cert)) + + dv = yaml.safe_load(self.render('templates/ndnd-dv/dv.yml.j2', host))['dv'] + self.assertEqual('/config/dv-schema.tlv', dv['trust_schema']) + fw = yaml.safe_load(self.render('templates/ndnd/yanfd.yml.j2', host)) + self.assertEqual(router, fw['fw']['router_name']) + + def test_public_status_anchor_matches_pin(self): + for host, values in self.hostvars.items(): + if values.get('ndnd_enabled'): + with self.subTest(host=host): + pem = self.render('templates/ndnd-http-status-server/root.cert.pem.j2', host) + wire = base64.b64decode(''.join(pem.splitlines()[1:-1]), validate=True) + pinned = base64.b64decode(''.join(Path(values['ndnd_root_anchor_path']).read_text().split()), validate=True) + self.assertEqual(pinned, wire) + self.assertEqual(values['ndnd_root_certificate_sha256'], hashlib.sha256(wire).hexdigest()) + + +if __name__ == '__main__': + unittest.main() diff --git a/host_vars/ARIZONA b/host_vars/ARIZONA index 3cdf5eb..138ddc0 100644 --- a/host_vars/ARIZONA +++ b/host_vars/ARIZONA @@ -1,7 +1,7 @@ site: /edu/arizona site_name: The University of Arizona -operator_name: Teng Liang -operator_email: philoliang2011@gmail.com +operator_name: Beichuan Zhang +operator_email: bzhang@cs.arizona.edu ansible_host: hobo.cs.arizona.edu host_ip: 128.196.203.36 local_ip: 128.196.203.36 @@ -32,6 +32,10 @@ neighbors: position: - 32.2289614 - -110.9483226 +ndnd_ca_prefix: /ndnd/arizona hr_angle: 2.97033 hr_radius: 16.2305 ethernet_device: eth0 +ndnd_enabled: true +ndnd_router_prefix: /ndnd/arizona +ndnd_bier_index: 2 diff --git a/host_vars/AVEIRO b/host_vars/AVEIRO index 8309148..3bfca8c 100644 --- a/host_vars/AVEIRO +++ b/host_vars/AVEIRO @@ -1,7 +1,7 @@ site: /pt/it/av site_name: University of Aveiro, Portugal -operator_name: Jose Quevedo -operator_email: quevedo@av.it.pt +operator_name: Rui Miguel Morais Silva +operator_email: ruimiguelsilva@av.it.pt ansible_host: selficn.av.it.pt host_ip: 193.136.92.155 local_ip: 10.0.12.127 diff --git a/host_vars/BERN b/host_vars/BERN index efc68ae..7b48801 100644 --- a/host_vars/BERN +++ b/host_vars/BERN @@ -1,7 +1,7 @@ site: /ch/unibe site_name: University of Bern, Switzerland -operator_name: Jonnahtan Saltarin -operator_email: saltarin@inf.unibe.ch +operator_name: hexu xing +operator_email: hexu.xing@unibe.ch ansible_host: os-float159.cnds.unibe.ch host_ip: 130.92.70.159 local_ip: 192.168.111.10 diff --git a/host_vars/DELFT b/host_vars/DELFT index fbf18ee..02a52cf 100644 --- a/host_vars/DELFT +++ b/host_vars/DELFT @@ -1,7 +1,7 @@ site: /nl/delft site_name: Delft University of Technology, Netherlands -operator_name: Chhagan Lal -operator_email: c.lal@tudelft.nl +operator_name: Ruud de Jong +operator_email: R.deJong@tudelft.nl ansible_host: ndn-testbed.ewi.tudelft.nl host_ip: 131.180.178.63 local_ip: 131.180.178.63 diff --git a/host_vars/FRANKFURT b/host_vars/FRANKFURT index 6d830cc..b1a7055 100644 --- a/host_vars/FRANKFURT +++ b/host_vars/FRANKFURT @@ -1,7 +1,7 @@ site: /pcl/frankfurt site_name: PCL Frankfurt -operator_name: Teng Liang -operator_email: redact@pcl.ac.cn +operator_name: Jian Yang +operator_email: yangj02@pcl.ac.cn ansible_host: frankfurt.testbed.named-data.net host_ip: 49.51.163.56 local_ip: 49.51.163.56 @@ -37,3 +37,7 @@ position: hr_angle: 0 hr_radius: 0 ethernet_device: eth0 +ndnd_ca_prefix: /ndnd/frankfurt +ndnd_enabled: true +ndnd_router_prefix: /ndnd/frankfurt +ndnd_bier_index: 5 diff --git a/host_vars/HK b/host_vars/HK index 9978445..4e86092 100644 --- a/host_vars/HK +++ b/host_vars/HK @@ -1,7 +1,7 @@ site: /pcl/hk site_name: Hong Kong operator_name: Jian Yang -operator_email: yangj@stu.hit.edu.cn +operator_email: yangj02@pcl.ac.cn ansible_host: hk.testbed.named-data.net host_ip: 47.238.59.170 local_ip: 47.238.59.170 @@ -27,3 +27,7 @@ position: hr_angle: 0 hr_radius: 0 ethernet_device: eth0 +ndnd_ca_prefix: /ndnd/hk +ndnd_enabled: true +ndnd_router_prefix: /ndnd/hk +ndnd_bier_index: 3 diff --git a/host_vars/MEMPHIS b/host_vars/MEMPHIS index 1df85c0..6b53709 100644 --- a/host_vars/MEMPHIS +++ b/host_vars/MEMPHIS @@ -1,7 +1,7 @@ site: /edu/memphis site_name: The University of Memphis -operator_name: Saurab Dulal -operator_email: sdulal@memphis.edu +operator_name: Mark Roberts +operator_email: mgrbrts1@memphis.edu ansible_host: titan.cs.memphis.edu host_ip: 141.225.11.173 local_ip: 141.225.11.173 @@ -27,6 +27,10 @@ neighbors: position: - 35.1186099 - -89.93700109999999 +ndnd_ca_prefix: /ndnd/memphis hr_angle: 2.9836 hr_radius: 15.2647 ethernet_device: eth1 +ndnd_enabled: true +ndnd_router_prefix: /ndnd/memphis +ndnd_bier_index: 1 diff --git a/host_vars/NEU b/host_vars/NEU index 9a69868..93d0803 100644 --- a/host_vars/NEU +++ b/host_vars/NEU @@ -1,7 +1,7 @@ site: /edu/neu site_name: Northeastern University -operator_name: Ran Liu -operator_email: liuranapply@gmail.com +operator_name: Edmund Yeh (PI) +operator_email: eyeh@ece.neu.edu ansible_host: neu.testbed.named-data.net host_ip: 155.33.178.42 local_ip: 155.33.178.42 diff --git a/host_vars/QUB b/host_vars/QUB index 63085b5..8f9c366 100644 --- a/host_vars/QUB +++ b/host_vars/QUB @@ -1,7 +1,7 @@ site: /uk/ac/qub site_name: Queen's University Belfast, UK -operator_name: Paul McAllister -operator_email: P.McAllister@qub.ac.uk +operator_name: James Grant +operator_email: J.Grant@qub.ac.uk ansible_host: ndn.qub.ac.uk host_ip: 51.148.47.242 local_ip: 10.60.60.2 diff --git a/host_vars/SINGAPORE b/host_vars/SINGAPORE index 53d4598..dc48247 100644 --- a/host_vars/SINGAPORE +++ b/host_vars/SINGAPORE @@ -1,7 +1,7 @@ site: /pcl/singapore site_name: PCL Singapore -operator_name: Teng Liang -operator_email: redact@pcl.ac.cn +operator_name: Jian Yang +operator_email: yangj02@pcl.ac.cn ansible_host: singapore.testbed.named-data.net host_ip: 43.134.56.106 local_ip: 43.134.56.106 @@ -35,3 +35,7 @@ position: hr_angle: 0 hr_radius: 0 ethernet_device: eth0 +ndnd_ca_prefix: /ndnd/singapore +ndnd_enabled: true +ndnd_router_prefix: /ndnd/singapore +ndnd_bier_index: 4 diff --git a/host_vars/UCLA b/host_vars/UCLA index ae7d657..05e4d79 100644 --- a/host_vars/UCLA +++ b/host_vars/UCLA @@ -1,7 +1,7 @@ site: /edu/ucla site_name: University of California, Los Angeles -operator_name: Tianyuan Yu -operator_email: royu29@g.ucla.edu +operator_name: Adam Thieme +operator_email: adam@cs.ucla.edu ansible_host: suns.cs.ucla.edu host_ip: 131.179.196.48 local_ip: 131.179.196.48 @@ -40,7 +40,11 @@ neighbors: position: - 34.0728225 - -118.4387398 +ndnd_ca_prefix: /ndnd/ucla hr_angle: 2.99911 hr_radius: 16.5866 ethernet_device: eth0 is_root_ca: true +ndnd_enabled: true +ndnd_router_prefix: /ndnd/ucla +ndnd_bier_index: 0 diff --git a/host_vars/UFBA b/host_vars/UFBA index 276688d..e5325bb 100644 --- a/host_vars/UFBA +++ b/host_vars/UFBA @@ -1,7 +1,7 @@ site: /br/ufba site_name: Federal University of Bahia, Brazil -operator_name: 'Thiago Bomfim ' -operator_email: thiagobomfim@pop-ba.rnp.br +operator_name: Prof. Dr. LEOBINO N SAMPAIO +operator_email: leobino@ufba.br ansible_host: ufba.testbed.named-data.net host_ip: 200.128.51.61 local_ip: 200.128.51.61 diff --git a/host_vars/WASEDA b/host_vars/WASEDA index 3392bde..5f402f1 100644 --- a/host_vars/WASEDA +++ b/host_vars/WASEDA @@ -1,6 +1,6 @@ site: /jp/waseda site_name: Waseda University, Tokyo, Japan -operator_name: Xiabo Li +operator_name: Hidenori Nakazato operator_email: lxb385@fuji.waseda.jp ansible_host: ndn-tb.nz.comm.waseda.ac.jp host_ip: 133.9.67.98 diff --git a/host_vars/WU b/host_vars/WU index 20d2d60..244a542 100644 --- a/host_vars/WU +++ b/host_vars/WU @@ -1,7 +1,7 @@ site: /edu/wustl site_name: Washington University in St. Louis -operator_name: John DeHart -operator_email: jdd@wustl.edu +operator_name: Patrick Crowley +operator_email: pcrowley@wustl.edu ansible_host: wu.testbed.named-data.net host_ip: 128.252.185.35 local_ip: 128.252.185.35 diff --git a/scripts/cert-renew.sh b/scripts/cert-renew.sh index dc2f14d..7474abf 100644 --- a/scripts/cert-renew.sh +++ b/scripts/cert-renew.sh @@ -2,6 +2,9 @@ source "$(pwd)/scripts/utils.sh" +COMPOSE_PROFILES=$(python3 framework/service-profiles.py) || exit $? +export COMPOSE_PROFILES + NDNCERT_CERTFILE="$(pwd)/dist/ndncert/site.ndncert" FORCE="" @@ -20,6 +23,46 @@ bash "$(pwd)/dist/nlsr/renew.sh" "${FORCE}" || exit $? bash "$(pwd)/dist/ndn-python-repo/renew.sh" "${FORCE}" || exit $? bash "$(pwd)/dist/ndnd-repo/renew.sh" ${FORCE:+"${FORCE}"} || exit $? +DV_ENABLED=$(python3 framework/service-profiles.py --service ndnd-dv) || exit $? +if [[ "${DV_ENABLED}" == 1 ]]; then + NDND_SIGNER_ENABLED=$(python3 framework/service-profiles.py --service ndnd-root-http-ca) || exit $? + if [[ "${NDND_SIGNER_ENABLED}" == 1 ]]; then + PWD="${ROOT_DIR:-$PWD}" docker compose up -d --no-deps --wait --wait-timeout 30 ndnd-root-http-ca || exit $? + fi + SITE_CA_ENABLED=$(python3 framework/service-profiles.py --service ndnd-go-ca) || exit $? + if [[ "${SITE_CA_ENABLED}" == 1 ]]; then + SITE_CA_CONFIG_DIR=dist/ndnd-site-ca-config + SITE_CA_DIR=dist/ndnd-site-ca-independent + bash "${PWD}/${SITE_CA_CONFIG_DIR}/renew.sh" ${FORCE:+"${FORCE}"} || exit $? + if [[ -f "${SITE_CA_DIR}/.security-changed" ]]; then + RUNNING_CA=$(PWD="${ROOT_DIR:-$PWD}" docker compose ps --status running -q ndnd-go-ca) || exit $? + if [[ -n "${RUNNING_CA}" ]]; then + PWD="${ROOT_DIR:-$PWD}" docker compose restart ndnd-go-ca || exit $? + fi + rm -f "${SITE_CA_DIR}/.security-changed" + fi + fi + DV_DIR=dist/ndnd-dv-independent + bash "${DV_DIR}/renew.sh" ${FORCE:+"${FORCE}"} || exit $? + if [[ -f "${DV_DIR}/.security-changed" ]]; then + RUNNING_DV=$(PWD="${ROOT_DIR:-$PWD}" docker compose ps --status running -q ndnd-dv) || exit $? + if [[ -n "${RUNNING_DV}" ]]; then + PWD="${ROOT_DIR:-$PWD}" docker compose restart ndnd-dv || exit $? + fi + rm -f "${DV_DIR}/.security-changed" + fi + NETWORK_REPO_DIR=dist/ndnd-network-repo + bash "${NETWORK_REPO_DIR}/renew.sh" ${FORCE:+"${FORCE}"} || exit $? + mkdir -p "${NETWORK_REPO_DIR}/storage" + if [[ -f "${NETWORK_REPO_DIR}/.security-changed" ]]; then + RUNNING_NETWORK_REPO=$(PWD="${ROOT_DIR:-$PWD}" docker compose ps --status running -q ndnd-network-repo) || exit $? + if [[ -n "${RUNNING_NETWORK_REPO}" ]]; then + PWD="${ROOT_DIR:-$PWD}" docker compose restart ndnd-network-repo || exit $? + fi + rm -f "${NETWORK_REPO_DIR}/.security-changed" + fi +fi + # Restart containers if force if [[ -n "${FORCE}" ]]; then PWD="${ROOT_DIR:-$PWD}" docker compose restart nlsr ndncert serve-certs ndn-python-repo || exit $? @@ -37,26 +80,28 @@ if [[ -f dist/ndnd-repo/.security-changed ]]; then fi # Optional services run after the core renewals and retain their own retry state. -MERKLE_ENABLED=$(python3 framework/service-profiles.py --service ndnd-merkle) || exit $? -if [[ "${MERKLE_ENABLED}" == 1 ]]; then - MERKLE_DIR=dist/ndnd-merkle - if [[ -n "${FORCE}" ]]; then - touch "${MERKLE_DIR}/.renew-pending" - fi - MERKLE_FORCE="" - if [[ -f "${MERKLE_DIR}/.renew-pending" ]]; then - MERKLE_FORCE="--force" - fi - if bash "${MERKLE_DIR}/renew.sh" ${MERKLE_FORCE:+"${MERKLE_FORCE}"}; then - rm -f "${MERKLE_DIR}/.renew-pending" - if [[ -f "${MERKLE_DIR}/.security-changed" ]]; then - RUNNING_LOG=$(PWD="${ROOT_DIR:-$PWD}" docker compose ps --status running -q ndnd-merkle) || exit $? - if [[ -n "${RUNNING_LOG}" ]]; then - PWD="${ROOT_DIR:-$PWD}" docker compose restart ndnd-merkle || exit $? +for MERKLE_SERVICE in ndnd-merkle ndnd-network-merkle; do + MERKLE_ENABLED=$(python3 framework/service-profiles.py --service "${MERKLE_SERVICE}") || exit $? + if [[ "${MERKLE_ENABLED}" == 1 ]]; then + MERKLE_DIR="dist/${MERKLE_SERVICE}" + if [[ -n "${FORCE}" ]]; then + touch "${MERKLE_DIR}/.renew-pending" + fi + MERKLE_FORCE="" + if [[ -f "${MERKLE_DIR}/.renew-pending" ]]; then + MERKLE_FORCE="--force" + fi + if bash "${MERKLE_DIR}/renew.sh" ${MERKLE_FORCE:+"${MERKLE_FORCE}"}; then + rm -f "${MERKLE_DIR}/.renew-pending" + if [[ -f "${MERKLE_DIR}/.security-changed" ]]; then + RUNNING_LOG=$(PWD="${ROOT_DIR:-$PWD}" docker compose ps --status running -q "${MERKLE_SERVICE}") || exit $? + if [[ -n "${RUNNING_LOG}" ]]; then + PWD="${ROOT_DIR:-$PWD}" docker compose restart "${MERKLE_SERVICE}" || exit $? + fi + rm -f "${MERKLE_DIR}/.security-changed" fi - rm -f "${MERKLE_DIR}/.security-changed" + else + echo "${MERKLE_SERVICE} credential renewal failed; core services renewed, log will retry next poll" >&2 fi - else - echo "NDNd Merkle credential renewal failed; core services renewed, log will retry next poll" >&2 fi -fi +done diff --git a/scripts/cron-host.sh b/scripts/cron-host.sh index a09bd3f..74ae02a 100755 --- a/scripts/cron-host.sh +++ b/scripts/cron-host.sh @@ -29,6 +29,9 @@ COMPOSE_PROFILES=$(docker compose exec -T master python3 framework/service-profi export COMPOSE_PROFILES # Ensure services are running -docker compose up -d --remove-orphans +docker compose up -d --remove-orphans || exit $? + +# Services must be running before their management commands can install routes. +docker compose exec -T -e PWD="$PWD" master python3 framework/ndnd-network-init.py || exit $? echo -e "Finished cron-host at $(date)" >&2 diff --git a/scripts/cron-master.sh b/scripts/cron-master.sh index 5baef03..dbc4d63 100755 --- a/scripts/cron-master.sh +++ b/scripts/cron-master.sh @@ -23,10 +23,16 @@ fi rm -f .git/index.lock git pull +# Bootstrap and verify the dedicated NDNd CA root before rendering DV trust. +python3 framework/ndnd_ca_bootstrap.py || exit $? + # Render new service templates before certificate provisioning on an existing node. PWD="${ROOT_DIR}" python3 framework/main.py || exit $? # Renew certificates if needed bash $(pwd)/scripts/cert-renew.sh || exit $? +# Restore runtime forwarding entries after rendered services or credentials changed. +PWD="${ROOT_DIR:-$PWD}" python3 framework/ndnd-network-init.py || exit $? + echo -e "Finished cron-master at $(date)" >&2 diff --git a/scripts/master.sh b/scripts/master.sh index 815e3df..4b7f8da 100755 --- a/scripts/master.sh +++ b/scripts/master.sh @@ -19,6 +19,9 @@ if [[ -z "$DEBUG" ]]; then git config --global --add safe.directory /testbed git pull + # Bootstrap and verify the dedicated NDNd CA root before rendering DV trust. + python3 framework/ndnd_ca_bootstrap.py + # Bootstrap all configuration files python3 framework/main.py --dry diff --git a/scripts/ndnd-infra-security.py b/scripts/ndnd-infra-security.py index 6b77abf..8f7a641 100644 --- a/scripts/ndnd-infra-security.py +++ b/scripts/ndnd-infra-security.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Provision NDNd's directory keychain using the existing ndn-cxx site CA.""" +"""Provision NDNd service keychains using the testbed certificate authorities.""" import argparse import base64 @@ -12,13 +12,15 @@ import subprocess import tempfile import time +from urllib.parse import urlencode +from urllib.request import Request, urlopen from Cryptodome.PublicKey import ECC, RSA import yaml from ndn.app_support.security_v2 import new_cert, parse_certificate from ndn.encoding import Component, ContentType, MetaInfo, Name, SignatureType, make_data, parse_data from ndn.security import Sha256WithEcdsaSigner -from ndn.security.validator import verify_ecdsa, verify_rsa +from ndn.security.validator import verify_ecdsa, verify_ed25519, verify_rsa def decode_certificate(path): @@ -61,6 +63,18 @@ def import_packet(directory, wire, kind): return write_atomic(path, pem(wire, kind)) +def sign_with_root_ca(csr, root_ca_url, secret, before, after): + query = urlencode({ + 'not_before': before.strftime('%Y%m%d%H%M%S'), + 'not_after': after.strftime('%Y%m%d%H%M%S'), + }) + url = f'{root_ca_url.rstrip("/")}/sign?{query}' + request = Request(url, data=base64.encodebytes(csr), method='POST', + headers={'Authorization': f'Bearer {secret}'}) + with urlopen(request, timeout=30) as response: + return base64.b64decode(b''.join(response.read().split()), validate=True) + + def validity(wire): period = parse_certificate(wire).signature_info.validity_period return tuple(datetime.strptime(bytes(value).decode(), '%Y%m%dT%H%M%S').replace(tzinfo=timezone.utc) @@ -82,6 +96,8 @@ def verify_certificate(wire, issuer): return verify_ecdsa(ECC.import_key(bytes(parent.content)), pointers) if signature_type == SignatureType.SHA256_WITH_RSA: return verify_rsa(RSA.import_key(bytes(parent.content)), pointers) + if signature_type == SignatureType.ED25519: + return verify_ed25519(ECC.import_key(bytes(parent.content)), pointers) return False @@ -112,41 +128,90 @@ def get_key(directory, identity): return name, key, wire -def provision(directory, site_home, site_prefix, anchors_path, force=False, renew_before_days=30, service='repo'): +def provision(directory, site_home, site_prefix, anchors_path, force=False, renew_before_days=30, + service='repo', root_ca_url='', root_cert_path=None, identity=None): # Serialize overlapping cron/manual renewal runs without rotating the key. + directory.mkdir(mode=0o700, parents=True, exist_ok=True) + directory.chmod(0o700) with (directory / '.security.lock').open('a') as lock: fcntl.flock(lock, fcntl.LOCK_EX) - return provision_locked(directory, site_home, site_prefix, anchors_path, force, renew_before_days, service) + return provision_locked(directory, site_home, site_prefix, anchors_path, force, renew_before_days, + service, root_ca_url, root_cert_path, identity) -def provision_locked(directory, site_home, site_prefix, anchors_path, force, renew_before_days, service): - if service not in ('repo', 'merkle'): +def provision_locked(directory, site_home, site_prefix, anchors_path, force, renew_before_days, + service, root_ca_url, root_cert_path, identity): + if service not in ('repo', 'merkle', 'dv', 'ca'): raise ValueError('Unsupported NDNd service') - if service == 'merkle' and site_prefix != '/ndn/edu/ucla': + if service == 'merkle' and site_prefix not in ('/ndn/edu/ucla', '/ndnd/ucla'): raise ValueError('Merkle log provisioning is restricted to UCLA') - with (directory / f'{service}.yml').open() as stream: - config = yaml.safe_load(stream)[service] - identity = Name.normalize(config['name']) - if Name.to_bytes(identity) != Name.to_bytes(Name.normalize(site_prefix + '/ndnd/' + service)): - raise ValueError(f'Identity must be /ndnd/{service}') + if service == 'ca': + config = {} + else: + with (directory / f'{service}.yml').open() as stream: + config = yaml.safe_load(stream)[service] + if service == 'dv': + identity = Name.normalize(config['router'] + '/32=DV') + expected_identity = Name.normalize(site_prefix + '/32=DV') + if not root_ca_url or not os.environ.get('NDND_ROOT_CA_SECRET'): + raise ValueError('DV credential provisioning requires the NDNd root CA URL and NDND_ROOT_CA_SECRET') + if root_cert_path is None: + raise ValueError('DV credential provisioning requires the NDNd root certificate path') + elif service == 'ca': + if not identity: + raise ValueError('Site CA provisioning requires an explicit identity') + identity = Name.normalize(identity) + expected_identity = Name.normalize(site_prefix) + if not root_ca_url or not os.environ.get('NDND_ROOT_CA_SECRET'): + raise ValueError('Site CA provisioning requires the NDNd root CA URL and NDND_ROOT_CA_SECRET') + if root_cert_path is None: + raise ValueError('Site CA provisioning requires the NDNd root certificate path') + else: + expected_identity = Name.normalize(identity or (site_prefix + '/ndnd/' + service)) + identity = Name.normalize(config['name']) + if not Name.is_prefix(Name.normalize(site_prefix), expected_identity): + raise ValueError('Service identity must be beneath the site prefix') + if Name.to_bytes(identity) != Name.to_bytes(expected_identity): + raise ValueError(f'Invalid {service} identity {Name.to_str(identity)}') now = datetime.now(timezone.utc) - site_wire = decode_certificate(site_home / 'site.ndncert') - site = parse_certificate(site_wire) - if Name.to_bytes(site.name[:-4]) != Name.to_bytes(Name.normalize(site_prefix)): - raise ValueError('Site certificate has the wrong identity') - available = {} - for path in anchors_path.glob('*.base64'): - wire = decode_certificate(path) - available[Name.to_str(parse_certificate(wire).name)] = wire - anchor_wires = [available[name] for name in config['trust_anchors']] - if not anchor_wires: - raise ValueError('At least one pinned trust anchor is required') - issuer = next((wire for wire in anchor_wires if verify_certificate(site_wire, wire)), None) - if issuer is None: - raise ValueError('Site certificate does not verify against a pinned testbed root') - site_before, site_after = validity(site_wire) - root_before, root_after = validity(issuer) + if service in ('dv', 'ca'): + issuer = decode_certificate(root_cert_path) + root = parse_certificate(issuer) + root_identity = root.name[:-4] + expected_root = Name.normalize('/ndnd') + if Name.to_bytes(root_identity) != Name.to_bytes(expected_root): + raise ValueError(f'NDNd CA certificate identity must be /ndnd, got {Name.to_str(root_identity)}') + if service == 'dv' and Name.to_str(root.name) not in config.get('trust_anchors', []): + raise ValueError('DV configuration does not trust the configured NDNd root certificate') + if not verify_certificate(issuer, issuer): + raise ValueError('NDNd root certificate is not self-signed by its own key') + anchor_wires = [issuer] + signing_issuer = issuer + root_before, root_after = validity(issuer) + site_before = root_before + site_after = root_after + else: + site_cert_path = site_home / 'site.ndncert' + if not site_cert_path.exists(): + site_cert_path = site_home / 'ca.ndncert' + site_wire = decode_certificate(site_cert_path) + site = parse_certificate(site_wire) + if Name.to_bytes(site.name[:-4]) != Name.to_bytes(Name.normalize(site_prefix)): + raise ValueError('Site certificate has the wrong identity') + available = {} + for path in anchors_path.glob('*.base64'): + wire = decode_certificate(path) + available[Name.to_str(parse_certificate(wire).name)] = wire + anchor_wires = [available[name] for name in config['trust_anchors']] + if not anchor_wires: + raise ValueError('At least one pinned trust anchor is required') + issuer = next((wire for wire in anchor_wires if verify_certificate(site_wire, wire)), None) + if issuer is None: + raise ValueError('Site certificate does not verify against a pinned testbed root') + signing_issuer = site_wire + site_before, site_after = validity(site_wire) + root_before, root_after = validity(issuer) # ndn-cxx rounds NotBefore up to a whole second when issuing a certificate. # Wait for that boundary rather than accepting a certificate before it is valid. starts = max(site_before, root_before) @@ -170,49 +235,74 @@ def provision_locked(directory, site_home, site_prefix, anchors_path, force, ren renew |= (not before <= now < after - margin or Name.to_bytes(cert.name[:-2]) != Name.to_bytes(key_name) or bytes(cert.content) != key.public_key().export_key(format='DER') - or not verify_certificate(cert_wire, site_wire) + or not verify_certificate(cert_wire, signing_issuer) or before < max(site_before, root_before) or after > min(site_after, root_after)) if renew: before = max(now - timedelta(minutes=5), site_before, root_before).replace(microsecond=0) after = min(now + timedelta(days=90), site_after, root_after).replace(microsecond=0) + if service in ('dv', 'ca'): + after = min(after, before + timedelta(days=90)) if after <= now + margin: raise ValueError('Renew the site/root certificate before issuing a service certificate') signer = Sha256WithEcdsaSigner(key_name, key.export_key(format='DER', use_pkcs8=False)) _, csr = new_cert(key_name, Component.from_str('self'), key.public_key().export_key(format='DER'), signer, before, after) - command = ['ndnsec', 'cert-gen', '-s', site_prefix, - '-S', before.strftime('%Y%m%d%H%M%S'), '-E', after.strftime('%Y%m%d%H%M%S'), '-'] - result = subprocess.run(command, input=base64.encodebytes(csr), stdout=subprocess.PIPE, - env={**os.environ, 'HOME': str(site_home)}, check=True, timeout=30) - cert_wire = base64.b64decode(b''.join(result.stdout.split()), validate=True) + if service in ('dv', 'ca'): + cert_wire = sign_with_root_ca(csr, root_ca_url, os.environ['NDND_ROOT_CA_SECRET'], before, after) + elif site_cert_path.name == 'ca.ndncert': + site_key_wire = decode_pem((site_home / 'ca.key.pem').read_text(), 'KEY') + _, _, secret, _ = parse_data(site_key_wire) + site_key = ECC.import_key(bytes(secret)) + if site_key.public_key().export_key(format='DER') != bytes(site.content): + raise ValueError('Site CA private key does not match its certificate') + site_signer = Sha256WithEcdsaSigner(site.name[:-2], site_key.export_key(format='DER', use_pkcs8=False)) + _, cert_wire = new_cert(key_name, Component.from_str('site'), + key.public_key().export_key(format='DER'), site_signer, before, after) + else: + command = ['ndnsec', 'cert-gen', '-s', site_prefix, + '-S', before.strftime('%Y%m%d%H%M%S'), '-E', after.strftime('%Y%m%d%H%M%S'), '-'] + result = subprocess.run(command, input=base64.encodebytes(csr), stdout=subprocess.PIPE, + env={**os.environ, 'HOME': str(site_home)}, check=True, timeout=30) + cert_wire = base64.b64decode(b''.join(result.stdout.split()), validate=True) cert = parse_certificate(cert_wire) actual_before, actual_after = validity(cert_wire) if (Name.to_bytes(cert.name[:-2]) != Name.to_bytes(key_name) or bytes(cert.content) != key.public_key().export_key(format='DER') - or not verify_certificate(cert_wire, site_wire) + or not verify_certificate(cert_wire, signing_issuer) or not before <= actual_before <= now < actual_after <= after): raise ValueError('Site CA returned an invalid service certificate') # Prepare every credential before marking it for a service reload. changed = import_packet(keys, key_wire, 'KEY') - for wire in [*anchor_wires, site_wire, cert_wire]: + certificates = [*anchor_wires, cert_wire] + if service not in ('dv', 'ca'): + certificates.insert(len(anchor_wires), site_wire) + for wire in certificates: changed |= import_packet(keys, wire, 'CERT') changed |= write_atomic(cert_path, base64.encodebytes(cert_wire)) + if service == 'ca': + changed |= write_atomic(directory / 'ca.cert.pem', pem(cert_wire, 'CERT')) + changed |= write_atomic(directory / 'ca.key.pem', pem(key_wire, 'KEY')) if changed: write_atomic(directory / '.security-changed', b'\n') - print(f"NDNd {service} {config['name']}: {'credentials updated' if changed else 'credentials current'}") + print(f"NDNd {service} {Name.to_str(identity)}: {'credentials updated' if changed else 'credentials current'}") return changed if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--directory', type=Path, required=True) - parser.add_argument('--site-home', type=Path, required=True) + parser.add_argument('--site-home', type=Path) parser.add_argument('--site-prefix', required=True) parser.add_argument('--anchors', type=Path, required=True) - parser.add_argument('--service', choices=('repo', 'merkle'), default='repo') + parser.add_argument('--service', choices=('repo', 'merkle', 'dv', 'ca'), default='repo') + parser.add_argument('--identity') + parser.add_argument('--root-ca-url', default='') + parser.add_argument('--root-cert', type=Path) parser.add_argument('--force', action='store_true') args = parser.parse_args() - provision(args.directory, args.site_home, args.site_prefix, args.anchors, args.force, service=args.service) + provision(args.directory, args.site_home, args.site_prefix, args.anchors, args.force, + service=args.service, root_ca_url=args.root_ca_url, root_cert_path=args.root_cert, + identity=args.identity) diff --git a/templates/caddy/Caddyfile.j2 b/templates/caddy/Caddyfile.j2 index bb6973f..aef754f 100644 --- a/templates/caddy/Caddyfile.j2 +++ b/templates/caddy/Caddyfile.j2 @@ -1,10 +1,28 @@ {{ ansible_host }} { + {% if ndnd_enabled | default(false) %} + redir /ndnd-status /ndnd-status/ 308 + handle_path /ndnd-status/* { + reverse_proxy http://ndnd-http-status-server:6380 + } + + @ndndws path /ndnd /ndnd/* + reverse_proxy @ndndws http://ndnd:9696 + {% endif %} + @nfdstatus path / /nfd-status.xsl /xslt-polyfill.min.js /*.css reverse_proxy @nfdstatus http://nfd-http-status-server:6380 @ws path /ws /ws/* reverse_proxy @ws http://nfd:9696 + {% if inventory_hostname == ndnd_ca_host %} + handle_path /ndnd-ca/* { + reverse_proxy http://ndnd-root-http-ca:8777 { + header_up X-Forwarded-For {remote_host} + } + } + {% endif %} + root * /srv file_server diff --git a/templates/file-server/ndnd/dv-schema.lvs.j2 b/templates/file-server/ndnd/dv-schema.lvs.j2 new file mode 100644 index 0000000..25418dd --- /dev/null +++ b/templates/file-server/ndnd/dv-schema.lvs.j2 @@ -0,0 +1 @@ +{% include "templates/ndnd-dv/dv-schema.lvs.j2" %} diff --git a/templates/ndnd-dv/dv-schema.lvs.j2 b/templates/ndnd-dv/dv-schema.lvs.j2 new file mode 100644 index 0000000..0fc1e63 --- /dev/null +++ b/templates/ndnd-dv/dv-schema.lvs.j2 @@ -0,0 +1,17 @@ +// All NDNd routers use the shared /ndnd DV network. The testbed root +// directly issues each site router's /32=DV certificate. +#KEY: "KEY"/_/_/_ +#network: {{ ndn_lvs_name(ndnd_dv_network) }} +#network_cert: {{ ndn_lvs_name(ndnd_dv_network) }}/#KEY +#prefix_state: #network/"32=DV"/"32=PSD" + +{% for host, values in hostvars.items() %} +{% if values.get('ndnd_enabled', false) %} +{% set router = ndn_lvs_name(values.ndnd_router_prefix) %} +#router_{{ loop.index }}: {{ router }} +#router_cert_{{ loop.index }}: #router_{{ loop.index }}/"32=DV"/#KEY <= #network_cert +#advertisement_data_{{ loop.index }}: /"localhop"/#router_{{ loop.index }}/"32=DV"/"32=ADV"/_ <= #router_cert_{{ loop.index }} +#status_data_{{ loop.index }}: #router_{{ loop.index }}/"32=DV"/"32=STATUS" <= #router_cert_{{ loop.index }} +#prefix_state_data_{{ loop.index }}: #prefix_state/#router_{{ loop.index }}/_/_ <= #router_cert_{{ loop.index }} +{% endif %} +{% endfor %} diff --git a/templates/ndnd-dv/dv.yml.j2 b/templates/ndnd-dv/dv.yml.j2 new file mode 100644 index 0000000..7a25c92 --- /dev/null +++ b/templates/ndnd-dv/dv.yml.j2 @@ -0,0 +1,19 @@ +dv: + network: {{ ndnd_dv_network | tojson }} + router: {{ ndnd_router_prefix | tojson }} + keychain: dir:///config/keys + trust_schema: /config/dv-schema.tlv + trust_anchors: + - {{ ndn_cert_name(read_file(ndnd_root_anchor_path)) | tojson }} + neighbors: + {% set peers = namespace(count=0) %} + {% for host in neighbors %} + {% set values = hostvars[host] %} + {% if host != inventory_hostname and values.get('ndnd_enabled', false) %} + - uri: {{ ("udp4://" + values.host_ip + ":" + str(ndnd_udp_port)) | tojson }} + {% set peers.count = peers.count + 1 %} + {% endif %} + {% endfor %} + {% if peers.count == 0 %} + [] + {% endif %} diff --git a/templates/ndnd-dv/renew.sh.j2 b/templates/ndnd-dv/renew.sh.j2 new file mode 100644 index 0000000..932ea16 --- /dev/null +++ b/templates/ndnd-dv/renew.sh.j2 @@ -0,0 +1,28 @@ +#!/bin/bash + +set -eu + +REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TESTBED_DIR="$(cd "${REPO_DIR}/../.." && pwd)" +source "${TESTBED_DIR}/.env" +NDND_ROOT_CA_SECRET="${VAR_NDND_ROOT_CA_SECRET:?Set VAR_NDND_ROOT_CA_SECRET in .env}" +export NDND_ROOT_CA_SECRET +{% if inventory_hostname == ndnd_ca_host %} +NDND_ROOT_HTTP_CA_URL={{ ndnd_root_http_ca_internal_url | tojson }} +{% else %} +NDND_ROOT_HTTP_CA_URL={{ ndnd_root_http_ca_url | tojson }} +{% endif %} + +if ! curl -fsS --max-time 5 "${NDND_ROOT_HTTP_CA_URL}/health" >/dev/null; then + echo "NDNd root signer is not ready; deferring DV renewal to the next cron run" >&2 + exit 0 +fi + +python3 "${TESTBED_DIR}/scripts/ndnd-infra-security.py" \ + --directory "${REPO_DIR}" \ + --site-home "${TESTBED_DIR}/dist/ndnd-dv-independent" \ + --site-prefix {{ ndnd_router_prefix | tojson }} \ + --anchors "${TESTBED_DIR}/anchors" \ + --service dv \ + --root-ca-url "${NDND_ROOT_HTTP_CA_URL}" \ + --root-cert "${TESTBED_DIR}/{{ ndnd_root_anchor_path }}" "$@" \ No newline at end of file diff --git a/templates/ndnd-go-ca/ca.json.j2 b/templates/ndnd-go-ca/ca.json.j2 new file mode 100644 index 0000000..186b2b1 --- /dev/null +++ b/templates/ndnd-go-ca/ca.json.j2 @@ -0,0 +1,40 @@ +{ + "ca-prefix": {{ ndnd_dv_network | tojson }}, + "ca-info": {{ ndnd_ca_info | tojson }}, + "max-validity-period": {{ ndnd_ca_max_validity_period | tojson }}, + "max-suffix-length": {{ ndnd_ca_max_suffix_length | tojson }}, + "probe-parameters": [{ "probe-parameter-key": "email" }], + "name-assignment": { "email": "/nonexist" }, + "redirect-to": [ + {% set redirects = namespace(comma=false) %} + {% for host, values in hostvars.items() %} + {% if values.get('ndnd_enabled', false) %} + {% set cert_path = "./dist/ndnd-go-root-ca/cert-store/site-ca." + host + ".ndncert" %} + {% if path_exists(cert_path) %} + {% set cert_oneline = oneline(read_file(cert_path)) %} + {% for email_domain in values.site_email_domain %} + {% if redirects.comma %},{% endif %} + { + "ca-prefix": {{ values.ndnd_ca_prefix | tojson }}, + "certificate": {{ cert_oneline | tojson }}, + "policy-type": "email", + "policy-param": {{ email_domain | tojson }} + } + {% set redirects.comma = true %} + {% endfor %} + {% endif %} + {% endif %} + {% endfor %} + ], + "supported-challenges": [ + {% for challenge in ndnd_ca_supported_challenges %}{ "challenge": {{ challenge | tojson }} }{% if not loop.last %},{% endif %}{% endfor %} + ], + "email": { + "smtp-server": {{ SMTP_SERVER | tojson }}, + "smtp-port": 587, + "smtp-username": {{ SMTP_USER | tojson }}, + "smtp-password-env": "NDND_SMTP_PASSWORD", + "from": "NDN Certificate Robot ", + "subject": "Your NDNCERT verification code" + } +} diff --git a/templates/ndnd-http-status-server/dv-schema.lvs.j2 b/templates/ndnd-http-status-server/dv-schema.lvs.j2 new file mode 100644 index 0000000..25418dd --- /dev/null +++ b/templates/ndnd-http-status-server/dv-schema.lvs.j2 @@ -0,0 +1 @@ +{% include "templates/ndnd-dv/dv-schema.lvs.j2" %} diff --git a/templates/ndnd-http-status-server/root.cert.pem.j2 b/templates/ndnd-http-status-server/root.cert.pem.j2 new file mode 100644 index 0000000..960f641 --- /dev/null +++ b/templates/ndnd-http-status-server/root.cert.pem.j2 @@ -0,0 +1,3 @@ +-----BEGIN NDN CERT----- +{{ oneline(read_file(ndnd_root_anchor_path)) }} +-----END NDN CERT----- diff --git a/templates/ndnd-network-merkle/management-schema.lvs.j2 b/templates/ndnd-network-merkle/management-schema.lvs.j2 new file mode 100644 index 0000000..ac172f5 --- /dev/null +++ b/templates/ndnd-network-merkle/management-schema.lvs.j2 @@ -0,0 +1,28 @@ +// Root -> user, or root -> site -> user/log; only the local log signs responses. +#key: "KEY"/_/_/_ +#root: {{ ndn_lvs_name(ndnd_dv_network) }}/#key +{% for depth in range(1, 7) %} +#root_user_{{ depth }}: {{ ndn_lvs_name(ndnd_dv_network) }}{% for n in range(depth) %}/rootuser{{ n }}{% endfor %} + +#root_user_key_{{ depth }}: #root_user_{{ depth }}/#key <= #root +#root_append_{{ depth }}: {{ ndn_lvs_name(ndnd_router_prefix + '/merkle') }}/"32=append"/#root_user_{{ depth }}/_ <= #root_user_key_{{ depth }} +{% endfor %} +{% for host, values in hostvars.items() %} +{% if values.get('ndnd_enabled', false) %} +{% set site_id = loop.index %} +#site_{{ site_id }}: {{ ndn_lvs_name(values.ndnd_ca_prefix) }}/#key <= #root +{% for depth in range(1, 6) %} +{% set id = site_id ~ '_' ~ depth %} +#user_{{ id }}: {{ ndn_lvs_name(values.ndnd_ca_prefix) }}{% for n in range(depth) %}/user{{ n }}{% endfor %} + +#user_key_{{ id }}: #user_{{ id }}/#key <= #site_{{ site_id }} +#append_{{ id }}: {{ ndn_lvs_name(ndnd_router_prefix + '/merkle') }}/"32=append"/#user_{{ id }}/_ <= #user_key_{{ id }} +{% endfor %} +{% if host == inventory_hostname %} +#log_key: {{ ndn_lvs_name(ndnd_router_prefix + '/merkle') }}/#key <= #site_{{ site_id }} +{% endif %} +{% endif %} +{% endfor %} +// Object validation removes terminal version and segment components. +#append_response: {{ ndn_lvs_name(ndnd_router_prefix + '/merkle') }}/"32=append"/_ <= #log_key +#check_response: {{ ndn_lvs_name(ndnd_router_prefix + '/merkle') }}/"32=check"/_ <= #log_key diff --git a/templates/ndnd-network-merkle/merkle.yml.j2 b/templates/ndnd-network-merkle/merkle.yml.j2 new file mode 100644 index 0000000..1fbea6a --- /dev/null +++ b/templates/ndnd-network-merkle/merkle.yml.j2 @@ -0,0 +1,8 @@ +merkle: + name: {{ (ndnd_router_prefix + '/merkle') | tojson }} + storage_dir: /config/storage + max_entries: 1000000 + keychain: dir:///config/keys + trust_schema: /config/management-schema.tlv + trust_anchors: + - {{ ndn_cert_name(read_file(ndnd_root_anchor_path)) | tojson }} diff --git a/templates/ndnd-network-merkle/renew.sh.j2 b/templates/ndnd-network-merkle/renew.sh.j2 new file mode 100644 index 0000000..f4a5e29 --- /dev/null +++ b/templates/ndnd-network-merkle/renew.sh.j2 @@ -0,0 +1,14 @@ +#!/bin/bash + +set -eu + +SERVICE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TESTBED_DIR="$(cd "${SERVICE_DIR}/../.." && pwd)" + +python3 "${TESTBED_DIR}/scripts/ndnd-infra-security.py" \ + --service merkle \ + --directory "${SERVICE_DIR}" \ + --site-home "${TESTBED_DIR}/dist/ndnd-site-ca-independent" \ + --site-prefix {{ ndnd_ca_prefix | tojson }} \ + --identity {{ (ndnd_router_prefix + '/merkle') | tojson }} \ + --anchors "${TESTBED_DIR}/anchors" "$@" diff --git a/templates/ndnd-network-repo/management-schema.lvs.j2 b/templates/ndnd-network-repo/management-schema.lvs.j2 new file mode 100644 index 0000000..2b56274 --- /dev/null +++ b/templates/ndnd-network-repo/management-schema.lvs.j2 @@ -0,0 +1,33 @@ +#key: "KEY"/_/_/_ +#root: {{ ndn_lvs_name(ndnd_dv_network) }}/#key +#flat_user: {{ ndn_lvs_name(ndnd_dv_network) }}/user +#flat_user_key: #flat_user/#key <= #root +#flat_command: #flat_user/"32=repo" <= #flat_user_key +#flat_security: #flat_user/"32=repo-security" <= #flat_user_key +{% for depth in range(2, 7) %} +// Root-issued NDNCERT identities may have multiple components (e.g. com/gmail/user). +#root_user_{{ depth }}: {{ ndn_lvs_name(ndnd_dv_network) }}{% for n in range(depth) %}/rootuser{{ n }}{% endfor %} + +#root_user_key_{{ depth }}: #root_user_{{ depth }}/#key <= #root +#root_command_{{ depth }}: #root_user_{{ depth }}/"32=repo" <= #root_user_key_{{ depth }} +#root_security_{{ depth }}: #root_user_{{ depth }}/"32=repo-security" <= #root_user_key_{{ depth }} +{% endfor %} +{% for host, values in hostvars.items() %} +{% if values.get('ndnd_enabled', false) %} +{% set site_id = loop.index %} +#site_{{ site_id }}: {{ ndn_lvs_name(values.ndnd_ca_prefix) }}/#key <= #root +#flat_user_key: #flat_user/#key <= #site_{{ site_id }} +{% for depth in range(1, 6) %} +{% set id = site_id ~ '_' ~ depth %} +#user_{{ id }}: {{ ndn_lvs_name(values.ndnd_ca_prefix) }}{% for n in range(depth) %}/user{{ n }}{% endfor %} + +#user_key_{{ id }}: #user_{{ id }}/#key <= #site_{{ site_id }} +#command_{{ id }}: #user_{{ id }}/"32=repo" <= #user_key_{{ id }} +#security_{{ id }}: #user_{{ id }}/"32=repo-security" <= #user_key_{{ id }} +{% endfor %} +{% if host == inventory_hostname %} +#repo_key: {{ ndn_lvs_name(ndnd_router_prefix + '/repo') }}/#key <= #site_{{ site_id }} +{% endif %} +{% endif %} +{% endfor %} +#response: {{ ndn_lvs_name(ndnd_router_prefix + '/repo') }}/"32=cmd"/_ <= #repo_key diff --git a/templates/ndnd-network-repo/renew.sh.j2 b/templates/ndnd-network-repo/renew.sh.j2 new file mode 100644 index 0000000..02b4ea1 --- /dev/null +++ b/templates/ndnd-network-repo/renew.sh.j2 @@ -0,0 +1,13 @@ +#!/bin/bash +set -eu + +REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TESTBED_DIR="$(cd "${REPO_DIR}/../.." && pwd)" + +python3 "${TESTBED_DIR}/scripts/ndnd-infra-security.py" \ + --directory "${REPO_DIR}" \ + --site-home "${TESTBED_DIR}/dist/ndnd-site-ca-independent" \ + --site-prefix {{ ndnd_ca_prefix | tojson }} \ + --identity {{ (ndnd_router_prefix + '/repo') | tojson }} \ + --anchors "${TESTBED_DIR}/anchors" \ + --service repo "$@" diff --git a/templates/ndnd-network-repo/repo.yml.j2 b/templates/ndnd-network-repo/repo.yml.j2 new file mode 100644 index 0000000..282c3d6 --- /dev/null +++ b/templates/ndnd-network-repo/repo.yml.j2 @@ -0,0 +1,8 @@ +repo: + name: {{ (ndnd_router_prefix + '/repo') | tojson }} + storage_dir: /config/storage + keychain: dir:///config/keys + trust_schema: /config/management-schema.tlv + trust_anchors: + - {{ ndn_cert_name(read_file(ndnd_root_anchor_path)) | tojson }} + ignore_validity: false diff --git a/templates/ndnd-root-http-ca/http-ca-server.yml.j2 b/templates/ndnd-root-http-ca/http-ca-server.yml.j2 new file mode 100644 index 0000000..9abfab0 --- /dev/null +++ b/templates/ndnd-root-http-ca/http-ca-server.yml.j2 @@ -0,0 +1,23 @@ +global_prefix: {{ ndnd_dv_network | tojson }} +port: 8777 +secret: "" +signer_backend: ndnd-go +ndnd_ca_image: {{ ndnd_ca_image | tojson }} +ndnd_ca_host_dir: {{ ("./ndnd-root-ca-home/ndnd") | tojson }} + +hosts: + {% for host, values in hostvars.items() %} + {% if values.get('ndnd_enabled', false) %} + {{ host }}: + subnets: + - {{ values.host_ip }}/32 + {% if host == ndnd_ca_host %} + - 172.16.0.0/12 + - 192.168.0.0/16 + {% endif %} + prefix: {{ (values.ndnd_router_prefix + '/32=DV') | tojson }} + ca_prefix: {{ values.ndnd_ca_prefix | tojson }} + ca_cert_file: /cert-store/site-ca.{{ host }}.ndncert + cert_file: /cert-store/router-dv.{{ host }}.ndncert + {% endif %} + {% endfor %} \ No newline at end of file diff --git a/templates/ndnd-site-ca/ca.json.j2 b/templates/ndnd-site-ca/ca.json.j2 new file mode 100644 index 0000000..b1037d6 --- /dev/null +++ b/templates/ndnd-site-ca/ca.json.j2 @@ -0,0 +1,19 @@ +{ + "ca-prefix": {{ ndnd_ca_prefix | tojson }}, + "ca-info": {{ ("NDN Testbed NDNCERT Site CA for " + inventory_hostname) | tojson }}, + "max-validity-period": {{ ndnd_ca_max_validity_period | tojson }}, + "max-suffix-length": {{ ndnd_ca_max_suffix_length | tojson }}, + "probe-parameters": [{ "probe-parameter-key": "email" }], + "name-assignment": { "email": {{ site | tojson }} }, + "supported-challenges": [ + {% for challenge in ndnd_site_ca_supported_challenges %}{ "challenge": {{ challenge | tojson }} }{% if not loop.last %},{% endif %}{% endfor %} + ], + "email": { + "smtp-server": {{ SMTP_SERVER | tojson }}, + "smtp-port": 587, + "smtp-username": {{ SMTP_USER | tojson }}, + "smtp-password-env": "NDND_SMTP_PASSWORD", + "from": "NDN Certificate Robot ", + "subject": "Your NDNCERT verification code" + } +} \ No newline at end of file diff --git a/templates/ndnd-site-ca/renew.sh.j2 b/templates/ndnd-site-ca/renew.sh.j2 new file mode 100644 index 0000000..9389bea --- /dev/null +++ b/templates/ndnd-site-ca/renew.sh.j2 @@ -0,0 +1,28 @@ +#!/bin/bash + +set -eu + +REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TESTBED_DIR="$(cd "${REPO_DIR}/../.." && pwd)" +source "${TESTBED_DIR}/.env" +NDND_ROOT_CA_SECRET="${VAR_NDND_ROOT_CA_SECRET:?Set VAR_NDND_ROOT_CA_SECRET in .env}" +export NDND_ROOT_CA_SECRET +{% if inventory_hostname == ndnd_ca_host %} +NDND_ROOT_HTTP_CA_URL={{ ndnd_root_http_ca_internal_url | tojson }} +{% else %} +NDND_ROOT_HTTP_CA_URL={{ ndnd_root_http_ca_url | tojson }} +{% endif %} + +if ! curl -fsS --max-time 5 "${NDND_ROOT_HTTP_CA_URL}/health" >/dev/null; then + echo "NDNd root signer is not ready; deferring site CA renewal to the next cron run" >&2 + exit 0 +fi + +python3 "${TESTBED_DIR}/scripts/ndnd-infra-security.py" \ + --directory "${TESTBED_DIR}/dist/ndnd-site-ca-independent" \ + --site-prefix {{ ndnd_ca_prefix | tojson }} \ + --identity {{ ndnd_ca_prefix | tojson }} \ + --anchors "${TESTBED_DIR}/anchors" \ + --service ca \ + --root-ca-url "${NDND_ROOT_HTTP_CA_URL}" \ + --root-cert "${TESTBED_DIR}/{{ ndnd_root_anchor_path }}" "$@" \ No newline at end of file diff --git a/templates/ndnd/yanfd.yml.j2 b/templates/ndnd/yanfd.yml.j2 new file mode 100644 index 0000000..cb1a929 --- /dev/null +++ b/templates/ndnd/yanfd.yml.j2 @@ -0,0 +1,24 @@ +fw: + bier_index: {{ ndnd_bier_index }} + router_name: {{ ndnd_router_prefix | tojson }} + +mgmt: + allow_localhop: true + +faces: + udp: + enabled_unicast: true + enabled_multicast: false + port_unicast: {{ ndnd_udp_port }} + tcp: + enabled: false + unix: + enabled: true + socket_path: /run/ndnd/ndnd.sock + websocket: + enabled: true + bind: "" + port: 9696 + tls_enabled: false + http3: + enabled: false diff --git a/templates/nfd/nfd-init.sh.j2 b/templates/nfd/nfd-init.sh.j2 index 66fcd23..4393036 100755 --- a/templates/nfd/nfd-init.sh.j2 +++ b/templates/nfd/nfd-init.sh.j2 @@ -17,7 +17,7 @@ nfdc strategy set / /localhost/nfd/strategy/best-route {% endif %} # Set multicast strategy -nfdc strategy set /ndn/multicast /localhost/nfd/strategy/multicast +nfdc strategy set /ndn/multicast /localhost/nfd/strategy/multicast/v=5/retx-suppression-max~1000 nfdc strategy set /ndn/broadcast /localhost/nfd/strategy/multicast nfdc strategy set /localhost /localhost/nfd/strategy/multicast diff --git a/templates/nlsr/nlsr.conf.j2 b/templates/nlsr/nlsr.conf.j2 index 4ca01c0..a8d0cb2 100644 --- a/templates/nlsr/nlsr.conf.j2 +++ b/templates/nlsr/nlsr.conf.j2 @@ -53,6 +53,9 @@ fib advertising { + {% if ndnd_enabled | default(false) and ndnd_dv_network not in advertised_prefixes %} + {{ ndnd_dv_network }} 0 + {% endif %} {% for prefix in advertised_prefixes %} {{ prefix }} 0 {% endfor %}