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
10 changes: 6 additions & 4 deletions .github/workflows/close-issues.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Close issues

# Closes the issues named with `Closes #<ISSUE>` in a PR's description when the
# PR merges to staging. GitHub only acts on that keyword for PRs to the default
# branch.
# Closes the issues named with a closing keyword such as `Closes #<ISSUE>` in a
# PR's description or its merged commit message when the PR merges to staging.
# GitHub only acts on these keywords for PRs to the default branch.
on:
push:
branches: [staging]
Expand All @@ -26,7 +26,9 @@ jobs:
const pr = prs.find((p) => p.merged_at && p.base.ref === "staging");
if (!pr) return;

for (const [, issue_number] of (pr.body ?? "").matchAll(/\bcloses:? #(\d+)\b/gi)) {
const text = `${pr.body ?? ""}\n${context.payload.head_commit?.message ?? ""}`;
const pattern = /\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?):? #(\d+)\b/gi;
for (const [, issue_number] of text.matchAll(pattern)) {
const { data: issue } = await github.rest.issues.get({ owner, repo, issue_number });
if (issue.pull_request || issue.state === "closed") continue;

Expand Down
1 change: 1 addition & 0 deletions docs/configuration/offline-publishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Each payload is tagged with the protocol it arrived on:

## Rules

- Listing anything other than the protocols above fails `manage.sh check`, and the services refuse to start.
- An offline payload from a protocol not in `OFFLINE_PUBLISH_ALLOWED_PROTOCOLS` is discarded. An empty list allows every protocol.
- An offline payload over `https` must carry `OFFLINE_PUBLISH_SHARED_SECRET` in the request's `tag` field, or it's discarded.
- Leaving `OFFLINE_PUBLISH_ALLOWED_PROTOCOLS` empty, or listing `https`, without the secret fails `manage.sh check`, and the services refuse to start.
Expand Down
2 changes: 1 addition & 1 deletion manage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cmd_status() {
# the subshell instead of stopping the script.
add_unit() {
local name="$1" full="$1" svc
[[ "$name" == *.service ]] || full="relaysms-publisher-${name}.service"
[[ "$name" == *.service ]] || full="$(unit_name_for "relaysms-publisher-${name}.service")"
for svc in "${SERVICE_UNITS[@]}"; do
if [ "$svc" = "$full" ]; then
units+=("$full")
Expand Down
11 changes: 10 additions & 1 deletion publisher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ROOT = Path(__file__).resolve().parents[1]
DATABASE_DIALECTS = ("sqlite", "mysql", "postgres")
CELERY_BROKERS = ("sqlite", "redis", "rabbitmq")
OFFLINE_PUBLISH_PROTOCOLS = ("https", "smtp")
GITHUB_ORG_PATTERN = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$")

# Fill unset variables from .env. Values already in the environment win.
Expand Down Expand Up @@ -345,7 +346,15 @@ class OfflinePublishConfig(Section):

@classmethod
def load(cls, read: _Reader) -> Self:
allowed = read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS")
names = read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS")
allowed = list(dict.fromkeys(name.lower() for name in names))
for protocol in allowed:
if protocol not in OFFLINE_PUBLISH_PROTOCOLS:
read.fail(
"OFFLINE_PUBLISH_ALLOWED_PROTOCOLS",
f"expected one of {', '.join(OFFLINE_PUBLISH_PROTOCOLS)}, "
f"got {protocol!r}",
)
https_allowed = not allowed or "https" in allowed
return cls(
allowed_protocols=allowed,
Expand Down
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ httpx==0.28.1
import-linter==2.15
pre-commit==4.7.0
pyright==1.1.414
pytest==9.1.1
pytest==9.2.0
pytest-cov==7.1.0
ruff==0.17.2
ruff==0.17.3
16 changes: 8 additions & 8 deletions requirements-observability.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# Includes requirements.txt, since both pull in OTel packages that must
# resolve to the same versions.
-r requirements.txt
opentelemetry-distro==0.66b1
opentelemetry-exporter-otlp-proto-grpc==1.45.1
opentelemetry-instrumentation-celery==0.66b1
opentelemetry-instrumentation-fastapi==0.66b1
opentelemetry-instrumentation-grpc==0.66b1
opentelemetry-instrumentation-logging==0.66b1
opentelemetry-instrumentation-sqlalchemy==0.66b1
opentelemetry-sdk==1.45.1
opentelemetry-distro==0.66b2
opentelemetry-exporter-otlp-proto-grpc==1.45.2
opentelemetry-instrumentation-celery==0.66b2
opentelemetry-instrumentation-fastapi==0.66b2
opentelemetry-instrumentation-grpc==0.66b2
opentelemetry-instrumentation-logging==0.66b2
opentelemetry-instrumentation-sqlalchemy==0.66b2
opentelemetry-sdk==1.45.2
14 changes: 7 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
alembic==1.20.1
argon2-cffi==25.1.0
authres==1.2.0
cachetools==7.2.5
cachetools==7.2.8
celery[sqlalchemy,redis]==5.6.3
cryptography==50.0.2
dkimpy==1.1.8
fastapi==0.162.0
GitPython==3.2.1
grpc-interceptor==0.15.4
grpcio==1.85.0
grpcio-health-checking==1.85.0
grpcio-testing==1.85.0
grpcio-tools==1.85.0
grpcio==1.85.1
grpcio-health-checking==1.85.1
grpcio-testing==1.85.1
grpcio-tools==1.85.1
imap-tools==1.16.0
msgspec==0.22.0
opentelemetry-api==1.45.1
phonenumbers==9.0.41
opentelemetry-api==1.45.2
phonenumbers==9.0.42
psycopg2-binary==2.9.13
pymysql==1.2.3
python-dotenv==1.2.4
Expand Down
77 changes: 77 additions & 0 deletions tests/scripts/test_manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# SPDX-License-Identifier: GPL-3.0-only

import os
import shutil
import subprocess

import pytest

from tests.scripts import REPO


@pytest.fixture
def logs(install, tmp_path):
"""Run `manage.sh logs` in the install directory, with journalctl faked."""
shutil.copy(REPO / "manage.sh", install)
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
journalctl = bin_dir / "journalctl"
journalctl.write_text('#!/usr/bin/env bash\nprintf "%s\\n" "$@"\n')
journalctl.chmod(0o755)

def run(*args: str, instance: str = "") -> subprocess.CompletedProcess[str]:
if instance:
(install / ".instance-name").write_text(instance)
return subprocess.run(
["bash", "manage.sh", "logs", "--no-follow", *args],
cwd=install,
env={
**os.environ,
"NO_COLOR": "1",
"PATH": f"{bin_dir}:{os.environ['PATH']}",
},
capture_output=True,
text=True,
check=False,
)

return run


def journal_units(result: subprocess.CompletedProcess[str]) -> list[str]:
assert result.returncode == 0, result.stderr
args = result.stdout.splitlines()
return [args[i + 1] for i, arg in enumerate(args) if arg == "-u"]


class TestLogsUnit:
def test_short_name_selects_the_unit(self, logs):
assert journal_units(logs("--unit", "rest")) == [
"relaysms-publisher-rest.service"
]

def test_short_name_selects_the_named_instance_unit(self, logs):
result = logs("--unit", "rest", "-u", "worker", "--unit=beat", instance="acme")

assert journal_units(result) == [
"relaysms-publisher-acme-rest.service",
"relaysms-publisher-acme-worker.service",
"relaysms-publisher-acme-beat.service",
]

def test_full_name_selects_the_named_instance_unit(self, logs):
result = logs("--unit=relaysms-publisher-acme-grpc.service", instance="acme")

assert journal_units(result) == ["relaysms-publisher-acme-grpc.service"]

def test_unknown_name_on_a_named_instance_fails(self, logs):
result = logs("--unit", "nope", instance="acme")

assert result.returncode != 0
assert "Unknown service unit: nope" in result.stderr

def test_unit_of_another_instance_fails(self, logs):
result = logs("--unit", "relaysms-publisher-rest.service", instance="acme")

assert result.returncode != 0
assert "Unknown service unit: relaysms-publisher-rest.service" in result.stderr
29 changes: 28 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,41 @@ def test_offline_shared_secret_must_be_a_key():
assert offline.shared_secret == KEY


@pytest.mark.parametrize("protocols", ["https", "smtp,https", ""])
@pytest.mark.parametrize("protocols", ["https", "HTTPS", "smtp,https", ""])
def test_offline_https_requires_shared_secret(protocols):
allowed = {"OFFLINE_PUBLISH_ALLOWED_PROTOCOLS": protocols}

assert "OFFLINE_PUBLISH_SHARED_SECRET" in errors(**allowed)
assert not errors(**allowed, OFFLINE_PUBLISH_SHARED_SECRET=KEY)


@pytest.mark.parametrize(
("protocols", "expected"),
[
("smtp", ["smtp"]),
("SMTP", ["smtp"]),
("https,smtp", ["https", "smtp"]),
("smtp,smtp", ["smtp"]),
("smtp,SMTP", ["smtp"]),
("", []),
],
)
def test_offline_allowed_protocols_are_normalized(protocols, expected):
offline = load(
OfflinePublishConfig,
OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols,
OFFLINE_PUBLISH_SHARED_SECRET=KEY,
)
assert offline.allowed_protocols == expected


@pytest.mark.parametrize("protocols", ["smpt", "SMTP,ftp", "http"])
def test_offline_allowed_protocols_reject_unknown_entries(protocols):
problems = errors(OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols)
assert "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS" in problems
assert "OFFLINE_PUBLISH_SHARED_SECRET" not in problems


def test_template_env_paths_match_defaults():
# scripts/lib.sh falls back to template.env for paths unset in .env.
template = dotenv_values(config.ROOT / "template.env")
Expand Down
Loading