Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 82 additions & 8 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,60 @@ let
};
}
);
validationService = {
package = "test-package";
entryPoint = "bin/service";
args = [ ];
environment = { };
secrets = [ ];
user = "nobody";
ports = [ ];
restart = "always";
healthCheck = null;
targets = [ "laptop" ];
class = "systemd";
};
# Keep each registry valid except for the exact issue #6383 shape.
newlineNameRegistry = pkgs.writeText "deploy-targets-newline-name.json" ''
{"svc\n": ${builtins.toJSON validationService}}
'';
duplicateSecretsRegistry = pkgs.writeText "deploy-targets-duplicate-secrets.json" (
builtins.toJSON {
svc = validationService // {
secrets = [
"secret.name"
"secret.name"
];
};
}
);
duplicateTargetsRegistry = pkgs.writeText "deploy-targets-duplicate-targets.json" (
builtins.toJSON {
svc = validationService // {
targets = [
"laptop"
"laptop"
];
};
}
);
hostlessHealthCheckRegistry = pkgs.writeText "deploy-targets-hostless-health-check.json" (
builtins.toJSON {
svc = validationService // {
healthCheck = "http://";
};
}
);
duplicatePortsRegistry = pkgs.writeText "deploy-targets-duplicate-ports.json" (
builtins.toJSON {
svc = validationService // {
ports = [
8080
8080
];
};
}
);
# Duplicate keys cannot be expressed as a Nix attrset, so these
# registries are raw JSON text (issue #6384): load_registry must
# reject them naming the duplicated key.
Expand Down Expand Up @@ -1244,15 +1298,30 @@ let
mkdir -p $out
cp list.txt $out/list.txt
'';
deploy-dupkeys-property = pkgs.runCommandLocal "check-deploy-dupkeys-property"
{
nativeBuildInputs = [ pkgs.python3 ];
}
''
python3 ${../pkgs/deploy/test_dupkeys_property.py} ${../pkgs/deploy/deploy.py}
mkdir -p $out
'';
deploy-dupkeys-property =
pkgs.runCommandLocal "check-deploy-dupkeys-property"
{
nativeBuildInputs = [ pkgs.python3 ];
}
''
python3 ${../pkgs/deploy/test_dupkeys_property.py} ${../pkgs/deploy/deploy.py}
mkdir -p $out
'';
deploy-list-malformed = pkgs.runCommandLocal "check-deploy-list-malformed" { } ''
expect_invalid() {
registry="$1"
expected="$2"
if ${deployPkgs.deploy}/bin/deploy list --registry "$registry" > out.txt 2> err.txt; then
echo "deploy list accepted a malformed registry: $registry" >&2
exit 1
fi
if ! grep -Fq "$expected" err.txt; then
echo "deploy list did not report '$expected' for $registry" >&2
cat err.txt >&2
exit 1
fi
test ! -s out.txt
}
if ${deployPkgs.deploy}/bin/deploy list --registry ${badRegistry} > out.txt 2> err.txt; then
echo "deploy list accepted a malformed registry" >&2
exit 1
Expand Down Expand Up @@ -1284,6 +1353,11 @@ let
exit 1
fi
test ! -s unhash-out.txt
expect_invalid ${newlineNameRegistry} "name must match"
expect_invalid ${duplicateSecretsRegistry} "secrets must not repeat a secret name"
expect_invalid ${duplicateTargetsRegistry} "targets must not repeat a target"
expect_invalid ${hostlessHealthCheckRegistry} "healthCheck must be an http(s) URL with a host"
expect_invalid ${duplicatePortsRegistry} "ports must not repeat a port"
mkdir -p $out
cp err.txt $out/errors.txt
'';
Expand Down
35 changes: 29 additions & 6 deletions pkgs/deploy/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import re
import sys
from urllib.parse import urlsplit

CLASSES = {"systemd", "launchd", "cloudflare", "sandbox", "container"}
RESTART_POLICIES = {
Expand Down Expand Up @@ -61,7 +62,7 @@ def validate(registry):
return ["registry: no service definitions"]
for name, defn in sorted(registry.items()):
where = f"service {name!r}"
if not NAME_RE.match(name):
if not NAME_RE.fullmatch(name):
errors.append(f"{where}: name must match {NAME_RE.pattern}")
if not isinstance(defn, dict):
errors.append(f"{where}: definition must be an object")
Expand Down Expand Up @@ -108,6 +109,11 @@ def validate(registry):
errors.append(
f"{where}: secrets must be a list of secret names (names only, never values)"
)
elif (
"secrets" in defn
and len(set(defn["secrets"])) != len(defn["secrets"])
):
errors.append(f"{where}: secrets must not repeat a secret name")
if "user" in defn and not (isinstance(defn["user"], str) and defn["user"]):
errors.append(f"{where}: user must be a non-empty string")
if "ports" in defn:
Expand All @@ -130,11 +136,23 @@ def validate(registry):
)
if "healthCheck" in defn and defn["healthCheck"] is not None:
hc = defn["healthCheck"]
if not (
isinstance(hc, str)
and (hc.startswith("http://") or hc.startswith("https://"))
):
errors.append(f"{where}: healthCheck must be an http(s) URL or null")
try:
parsed = urlsplit(hc) if isinstance(hc, str) else None
hostname = parsed.hostname if parsed is not None else None
if parsed is not None:
# Accessing .port also rejects malformed/out-of-range ports.
parsed.port
valid_url = (
parsed is not None
and parsed.scheme in {"http", "https"}
and bool(hostname)
)
except ValueError:
valid_url = False
if not valid_url:
errors.append(
f"{where}: healthCheck must be an http(s) URL with a host or null"
)
if "targets" in defn and not (
isinstance(defn["targets"], list)
and defn["targets"]
Expand All @@ -143,6 +161,11 @@ def validate(registry):
errors.append(
f"{where}: targets must be a non-empty list of host/class names"
)
elif (
"targets" in defn
and len(set(defn["targets"])) != len(defn["targets"])
):
errors.append(f"{where}: targets must not repeat a target")
if "class" in defn and not (
isinstance(defn["class"], str) and defn["class"] in CLASSES
):
Expand Down