diff --git a/cli/contributing/network/diagnose/config.py b/cli/contributing/network/diagnose/config.py index 23848c6e68..a9575f6d68 100644 --- a/cli/contributing/network/diagnose/config.py +++ b/cli/contributing/network/diagnose/config.py @@ -2,6 +2,8 @@ from __future__ import annotations +from utils.paths import read_group_paths + DEFAULT_HOSTS: tuple[str, ...] = ( "github.com", "objects.githubusercontent.com", @@ -58,12 +60,7 @@ PROXY_ENV_KEYS: tuple[str, ...] = ( "CA_TRUST_NAME", ) -CA_BUNDLE_CANDIDATES: tuple[str, ...] = ( - "/etc/ssl/certs/ca-certificates.crt", - "/etc/pki/tls/certs/ca-bundle.crt", - "/etc/ssl/ca-bundle.pem", - "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", -) +CA_BUNDLE_CANDIDATES: tuple[str, ...] = tuple(read_group_paths("CA_BUNDLE_CANDIDATES")) PMTU_PROBE_SIZES: tuple[int, ...] = (1472, 1452, 1400, 1300, 1200, 1024, 576, 256) diff --git a/group_vars/all/05_paths.yml b/group_vars/all/05_paths.yml index cae7f96bc1..270384ce19 100644 --- a/group_vars/all/05_paths.yml +++ b/group_vars/all/05_paths.yml @@ -30,3 +30,10 @@ DIR_RESCUE_DIAGNOSTICS: "/tmp/infinito-rescue-diagnostics" FILE_DNS53_SAMPLER_LOG: "/var/log/infinito/dns53-sampler.log" FILE_ENVIRONMENT: "/etc/environment" # File to safe environment variables in + +CA_BUNDLE_CANDIDATES: # Where the distributions keep the extracted CA bundle, most likely first + - "/etc/ssl/certs/ca-certificates.crt" + - "/etc/pki/tls/certs/ca-bundle.crt" + - "/etc/ssl/ca-bundle.pem" + - "/etc/pki/tls/cacert.pem" + - "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" diff --git a/roles/dev-nix/defaults/main.yml b/roles/dev-nix/defaults/main.yml index c2dc7d7728..0eca2a808f 100644 --- a/roles/dev-nix/defaults/main.yml +++ b/roles/dev-nix/defaults/main.yml @@ -38,8 +38,4 @@ dev_nix_experimental_features: - nix-command - flakes -DEV_NIX_SSL_CERT_FILE_CANDIDATES: - - /etc/ssl/certs/ca-certificates.crt - - /etc/pki/tls/certs/ca-bundle.crt - - /etc/ssl/ca-bundle.pem - - /etc/pki/tls/cacert.pem +DEV_NIX_SSL_CERT_FILE_CANDIDATES: "{{ CA_BUNDLE_CANDIDATES }}" diff --git a/roles/dev-nix/tasks/00_core.yml b/roles/dev-nix/tasks/00_core.yml index b9d4805f72..bcd81755ae 100644 --- a/roles/dev-nix/tasks/00_core.yml +++ b/roles/dev-nix/tasks/00_core.yml @@ -26,7 +26,7 @@ | map(attribute="item") | list | first - ) | default("/etc/ssl/certs/ca-certificates.crt", true) + ) | default(CA_BUNDLE_CANDIDATES | first, true) }} - name: 📦 Install Nix via package manager on Arch-based systems diff --git a/scripts/meta/env/load.sh b/scripts/meta/env/load.sh index c7e0182d67..b1dff51f4c 100755 --- a/scripts/meta/env/load.sh +++ b/scripts/meta/env/load.sh @@ -86,6 +86,17 @@ for _infinito_env_key in "${!_infinito_env_preserved[@]}"; do done unset _infinito_env_key _infinito_env_preserved +if [[ -z "${REQUESTS_CA_BUNDLE:-}" ]]; then + for _infinito_env_ca in ${INFINITO_CA_BUNDLE_CANDIDATES}; do + if [[ -r "${_infinito_env_ca}" ]]; then + export REQUESTS_CA_BUNDLE="${_infinito_env_ca}" + export SSL_CERT_FILE="${SSL_CERT_FILE:-${_infinito_env_ca}}" + break + fi + done + unset _infinito_env_ca +fi + export INFINITO_ENV_LOADED="1" _infinito_env_mtime="$(stat -c %Y "${_infinito_env_dotenv}" 2>/dev/null || echo 0)" export INFINITO_ENV_LOADED_MTIME="${_infinito_env_mtime}" diff --git a/utils/env/handlers/__init__.py b/utils/env/handlers/__init__.py index 1dc8f7ae1f..4ddc07b473 100644 --- a/utils/env/handlers/__init__.py +++ b/utils/env/handlers/__init__.py @@ -15,6 +15,7 @@ from . import ( passthrough, ) from .infinito import ( + ca_bundle_candidates, ca_cert_host, container, distros, @@ -70,6 +71,7 @@ ORDERED_HANDLERS = [ running_on_github, tor_socks_port, is_wsl2, + ca_bundle_candidates, ca_cert_host, outer_network_mtu, inventory, diff --git a/utils/env/handlers/infinito/ca_bundle_candidates.py b/utils/env/handlers/infinito/ca_bundle_candidates.py new file mode 100644 index 0000000000..bea170ba9d --- /dev/null +++ b/utils/env/handlers/infinito/ca_bundle_candidates.py @@ -0,0 +1,21 @@ +"""INFINITO_CA_BUNDLE_CANDIDATES: where the distributions keep the extracted CA +bundle, read from the group_vars paths SPOT (group_vars/all/05_paths.yml +CA_BUNDLE_CANDIDATES).""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from utils.paths import read_group_paths + +if TYPE_CHECKING: + from utils.env.builder import BuildContext, EnvBuilder + +KEY = "INFINITO_CA_BUNDLE_CANDIDATES" +COMMENT = "Extracted CA bundle per distribution layout, most likely first; consumers take the first readable one (SPOT: group_vars/all/05_paths.yml CA_BUNDLE_CANDIDATES)." + + +def apply(eb: EnvBuilder, ctx: BuildContext) -> None: + eb.setdefault( + KEY, " ".join(read_group_paths("CA_BUNDLE_CANDIDATES")), comment=COMMENT + ) diff --git a/utils/paths.py b/utils/paths.py index bf1c816c14..ef67eb1ea1 100644 --- a/utils/paths.py +++ b/utils/paths.py @@ -27,6 +27,7 @@ _GROUP_PATHS_FILE = str(PROJECT_ROOT / "group_vars" / "all" / "05_paths.yml") _ENTRY_RE = re.compile( r'^(?P[A-Za-z_][A-Za-z0-9_]*):\s*"?(?P[^"#]*)"?\s*(?:#.*)?$' ) +_ITEM_RE = re.compile(r'^\s+-\s*"?(?P[^"#]*)"?\s*(?:#.*)?$') def read_group_path(key: str) -> str: @@ -56,6 +57,35 @@ def read_group_path(key: str) -> str: raise KeyError(f"{key} not defined in {_GROUP_PATHS_FILE}") +def read_group_paths(key: str) -> list[str]: + """Plain-string list value from the group_vars paths SPOT. + + Args: + key: top-level variable name whose value is a YAML block sequence. + + Returns: + The literal string entries, in file order. + + Raises: + KeyError: the key is not defined in the SPOT or carries no entry. + """ + entries: list[str] = [] + collecting = False + for line in read_text(_GROUP_PATHS_FILE).splitlines(): + if line.startswith(f"{key}:"): + collecting = True + continue + if not collecting: + continue + item = _ITEM_RE.match(line) + if item is None: + break + entries.append(item.group("value").strip()) + if not entries: + raise KeyError(f"{key} not defined as a list in {_GROUP_PATHS_FILE}") + return entries + + def _dir_var_lib() -> str: env = os.environ.get("INFINITO_DIR_VAR_LIB") if env: