Skip to content

HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.

gateway: a key can be renamed, as the console's Rename has asked since the keys page shipped (#559)

github-actions[bot]52f13f1d68d6
diff --git a/gateway/app.py b/gateway/app.py
index 103c50a2..edd8b702 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -12254,6 +12254,28 @@ async def list_keys(org: str, request: Request) -> dict:
return {"keys": keys}
+class KeyPatch(BaseModel):
+ """The parts of a key a person may edit. Only the name: the secret never changes in place
+ (rotate = a new key, then revoke the old one), and the scope is fixed at creation."""
+ name: str | None = None
+
+
[email protected]("/v1/orgs/{org}/keys/{kid}")
+async def rename_key(org: str, kid: str, body: KeyPatch, request: Request) -> dict:
+ """A key's new name. The console's Rename has sent this since the keys page shipped, and the
+ route was the hosted service's alone, so every rename on a self-hosted box answered 405."""
+ p = await _owned_org(request, org)
+ kv = await _vertex_get(kid)
+ if not kv or str(kv.get("org") or "") != org or kv.get("kind") != "harness_api_key" \
+ or not _scope_keeps(p, kv.get("workspace")):
+ raise HTTPException(404, "key not found")
+ name = (body.name or "").strip()[:120]
+ if not name:
+ raise HTTPException(422, "name is required")
+ await _vg_upsert("HarnessApiKey", kid, {"name": name}, raise_on_fail=True)
+ return {"id": kid, "name": name}
+
+
@app.delete("/v1/orgs/{org}/keys/{kid}")
async def revoke_key(org: str, kid: str, request: Request) -> dict:
p = await _owned_org(request, org)
diff --git a/gateway/tests/test_key_rename.py b/gateway/tests/test_key_rename.py
new file mode 100644
index 00000000..dfb3b0d1
--- /dev/null
+++ b/gateway/tests/test_key_rename.py
@@ -0,0 +1,69 @@
+"""A key can be renamed (PATCH /v1/orgs/{org}/keys/{kid}), and only its own organization's.
+
+The console's Rename sends this; the route was the hosted service's alone, so on a self-hosted box
+every rename answered 405. Driven through the real routes on the local store.
+"""
+import os
+import sys
+import tempfile
+from pathlib import Path
+
+import pytest
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+os.environ.setdefault("HR_BACKING", "local")
+os.environ.setdefault("HR_DATA_DIR", tempfile.mkdtemp(prefix="hr-keyrename-"))
+import app as gw # noqa: E402
+from fastapi.testclient import TestClient # noqa: E402
+
+
+def _as(org: str, member: str = "owner@local") -> dict:
+ return {"x-harness-internal": gw.INTERNAL_KEY, "x-harness-org": org, "x-harness-member": member}
+
+
+def client(monkeypatch):
+ monkeypatch.setattr(gw, "INTERNAL_KEY", gw.INTERNAL_KEY or "test-internal-key")
+ with TestClient(gw.app) as c:
+ yield c
+
+
+def _mint(client, org: str, name: str) -> str:
+ r = client.post(f"/v1/orgs/{org}/keys", headers=_as(org), json={"name": name})
+ assert r.status_code == 200, r.text
+ return r.json()["id"]
+
+
+def _names(client, org: str) -> dict:
+ r = client.get(f"/v1/orgs/{org}/keys", headers=_as(org))
+ assert r.status_code == 200, r.text
+ return {k["id"]: k["name"] for k in r.json()["keys"]}
+
+
+def test_a_key_is_renamed_and_the_list_says_so(client):
+ kid = _mint(client, "org-rn", "first name")
+ r = client.patch(f"/v1/orgs/org-rn/keys/{kid}", headers=_as("org-rn"), json={"name": " ci deploy "})
+ assert r.status_code == 200, r.text
+ assert r.json() == {"id": kid, "name": "ci deploy"}
+ assert _names(client, "org-rn")[kid] == "ci deploy"
+
+
+def test_an_empty_name_is_refused_and_the_old_one_kept(client):
+ kid = _mint(client, "org-rn2", "keep me")
+ r = client.patch(f"/v1/orgs/org-rn2/keys/{kid}", headers=_as("org-rn2"), json={"name": " "})
+ assert r.status_code == 422
+ assert _names(client, "org-rn2")[kid] == "keep me"
+
+
+def test_another_organizations_key_is_not_there(client):
+ kid = _mint(client, "org-a1", "theirs")
+ r = client.patch(f"/v1/orgs/org-b1/keys/{kid}", headers=_as("org-b1"), json={"name": "mine now"})
+ assert r.status_code == 404
+ r = client.patch(f"/v1/orgs/org-a1/keys/{kid}", headers=_as("org-b1"), json={"name": "mine now"})
+ assert r.status_code == 403
+ assert _names(client, "org-a1")[kid] == "theirs"
+
+
+def test_an_id_that_is_not_a_key_is_not_renamed(client):
+ r = client.patch("/v1/orgs/org-rn3/keys/sess_not_a_key", headers=_as("org-rn3"), json={"name": "x"})
+ assert r.status_code == 404