Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Address review: sign-release reads the keys it trusts, and service-token fixes

Petr Bauche0a4d540a438
diff --git a/console/packages/agent-controller/scripts/sign-release.mjs b/console/packages/agent-controller/scripts/sign-release.mjs
index 76ba97701..28f646a14 100644
--- a/console/packages/agent-controller/scripts/sign-release.mjs
+++ b/console/packages/agent-controller/scripts/sign-release.mjs
@@ -1,7 +1,6 @@
import { createHash, createPrivateKey, createPublicKey, sign, verify } from 'node:crypto';
import { readFileSync, writeFileSync } from 'node:fs';
-import { dirname, join } from 'node:path';
-import { fileURLToPath } from 'node:url';
+import { TRUSTED_RELEASE_KEYS } from '../src/release-keys.ts';
/**
* Signs a packaged controller release:
@@ -34,13 +33,7 @@ if (privateKey.asymmetricKeyType !== 'ed25519')
throw new Error('CONTROLLER_RELEASE_SIGNING_KEY must be an Ed25519 private key (PKCS#8 PEM).');
const spki = createPublicKey(privateKey).export({ type: 'spki', format: 'der' }).toString('base64');
-const root = join(dirname(fileURLToPath(import.meta.url)), '..');
-const trusted = [
- ...readFileSync(join(root, 'src', 'release-keys.ts'), 'utf8').matchAll(
- /\{\s*id:\s*'([^']+)',\s*publicKey:\s*'([^']+)'\s*\}/g
- ),
-].map((match) => ({ id: match[1], publicKey: match[2] }));
-const key = trusted.find((candidate) => candidate.publicKey === spki);
+const key = TRUSTED_RELEASE_KEYS.find((candidate) => candidate.publicKey === spki);
if (!key)
throw new Error(
`The signing key's public key (${spki}) is not in src/release-keys.ts, so no controller would install this release. Add it there (and to install.sh) first.`
diff --git a/console/packages/agent-controller/src/release-keys.test.ts b/console/packages/agent-controller/src/release-keys.test.ts
index 0c70fca7e..6ea5b5a8a 100644
--- a/console/packages/agent-controller/src/release-keys.test.ts
+++ b/console/packages/agent-controller/src/release-keys.test.ts
@@ -135,6 +135,20 @@ describe('scripts/sign-release.mjs', () => {
}
}
+ it('reads the same trusted keys under plain node as the controller is built with', () => {
+ const read = execFileSync(
+ process.execPath,
+ [
+ '--input-type=module',
+ '-e',
+ `const { TRUSTED_RELEASE_KEYS } = await import(${JSON.stringify(join(dirname(signScript), '..', 'src', 'release-keys.ts'))}); process.stdout.write(JSON.stringify(TRUSTED_RELEASE_KEYS));`,
+ ],
+ { encoding: 'utf8' }
+ );
+ expect(TRUSTED_RELEASE_KEYS.length).toBeGreaterThan(0);
+ expect(JSON.parse(read)).toEqual(TRUSTED_RELEASE_KEYS);
+ });
+
it('fails loud with no signing key', () => {
expect(sign({})).toMatch(/CONTROLLER_RELEASE_SIGNING_KEY is not set/);
});
diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.py
index 9bd68475c..a10da8f8b 100644
--- a/core/switch_core/connections/broker.py
+++ b/core/switch_core/connections/broker.py
@@ -744,6 +744,21 @@ class ServiceBroker:
"so it was not handed out.",
retryable=False,
)
+ if (
+ policy.kind == "pass_through"
+ and issued.expires_at > datetime.now(UTC) + max_lifetime + TOKEN_LEEWAY
+ ):
+ # The owner's token outlives the cap only until enough of it has
+ # run down, so asking again later succeeds. Nothing was handed out,
+ # so there is nothing to discard or record.
+ raise ServiceError(
+ 503,
+ INTERNAL,
+ f"{definition.name}'s current token lasts longer than "
+ f"{_lifetime_words(policy.max_lifetime)}, so it is not handed "
+ "out until it has less than that left.",
+ retryable=True,
+ )
try:
now = datetime.now(UTC)
if not (
@@ -1098,6 +1113,10 @@ class ServiceBroker:
if grant.service in switched_off:
continue
entry = self._catalog.get(grant.service)
+ # Nor is one whose credential never expires: every token request
+ # for it is refused, so the session gets none of its tools.
+ if entry is not None and not _issued_to_agents(entry):
+ continue
skill = None if entry is None else entry.skill_files.get("SKILL.md")
mcp = None if entry is None else entry.definition.mcp
cli = None if entry is None else entry.definition.cli
@@ -1856,7 +1875,13 @@ class ServiceBroker:
)
if removed:
withdrawn = await self.revoke_withdrawn(
- session, (ServiceTokenIssuance.owner_id == user_id,)
+ session,
+ (
+ ServiceTokenIssuance.owner_id == user_id,
+ ServiceTokenIssuance.service.in_(
+ [service for service, _ in removed]
+ ),
+ ),
)
if withdrawn is not None:
warnings.append(withdrawn)
diff --git a/core/switch_core/connections/catalog/google-workspace/connection.yaml b/core/switch_core/connections/catalog/google-workspace/connection.yaml
index 9b860b895..480b9623c 100644
--- a/core/switch_core/connections/catalog/google-workspace/connection.yaml
+++ b/core/switch_core/connections/catalog/google-workspace/connection.yaml
@@ -29,7 +29,8 @@ auth:
account_id: sub
label: email
# Google's access tokens lived 3,599 s when measured, so one handed out never
-# has more than an hour left; a longer one is refused rather than handed out.
+# has more than an hour left. A longer one is held back (retryable) until it
+# has run down to an hour.
token: { kind: pass_through, max_lifetime: 3600 }
cli:
name: google-workspace
diff --git a/core/switch_core/db/stores/service_connection_store.py b/core/switch_core/db/stores/service_connection_store.py
index a3982fae3..9e01d5f0b 100644
--- a/core/switch_core/db/stores/service_connection_store.py
+++ b/core/switch_core/db/stores/service_connection_store.py
@@ -10,7 +10,7 @@ from __future__ import annotations
from datetime import datetime
from typing import Any
-from sqlalchemy import delete, exists, func, select, text, update
+from sqlalchemy import and_, delete, exists, func, or_, select, text, update
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.exc import DBAPIError
from sqlalchemy.ext.asyncio import AsyncSession
@@ -496,16 +496,22 @@ class ServiceConnectionStore:
)
async def holds_tokens(self, session: AsyncSession) -> bool:
- """Whether the bound tenant holds any token's ciphertext at all.
-
- Every step of a revocation pass acts only on these rows, so a tenant
- with none has nothing to clear, queue or revoke.
+ """Whether the bound tenant holds a token a revocation pass acts on:
+ one whose ciphertext is kept, or a live one passed through that is not
+ yet marked withdrawn. A tenant with neither has nothing to clear,
+ queue or revoke.
"""
held = await session.scalar(
select(ServiceTokenIssuance.id)
.where(
ServiceTokenIssuance.tenant_id == require_tenant_id(),
- ServiceTokenIssuance.encrypted_token.is_not(None),
+ or_(
+ ServiceTokenIssuance.encrypted_token.is_not(None),
+ and_(
+ ServiceTokenIssuance.revoke_requested.is_(False),
+ ServiceTokenIssuance.expires_at > func.now(),
+ ),
+ ),
)
.limit(1)
)
diff --git a/core/tests/switch_core/connections/test_broker.py b/core/tests/switch_core/connections/test_broker.py
index 13addbe3b..b8afd10ff 100644
--- a/core/tests/switch_core/connections/test_broker.py
+++ b/core/tests/switch_core/connections/test_broker.py
@@ -688,7 +688,10 @@ class TestRevocation:
await _issue(broker, session_factory, world.agent.id)
async with session_factory() as session:
await session.execute(
- ServiceTokenIssuance.__table__.update().values(encrypted_token=None)
+ ServiceTokenIssuance.__table__.update().values(
+ encrypted_token=None,
+ expires_at=datetime.now(UTC) - timedelta(minutes=5),
+ )
)
await session.commit()
@@ -1078,20 +1081,22 @@ class TestPassThrough:
await _issue(broker, session_factory, world.agent.id)
assert vendor.refreshes == 0
- async def test_refuses_a_token_past_the_catalogs_lifetime_and_revokes_nothing(
+ async def test_holds_back_a_token_past_the_catalogs_lifetime_and_revokes_nothing(
self, session_factory, tmp_path
) -> None:
+ """The owner's token runs down to the cap, so the refusal is retryable."""
pt = await _pass_through(session_factory, tmp_path / "c", expires_in=2 * 3600)
refused = await _refused(
pt.broker, session_factory, pt.world.agent.id, service="example"
)
assert (refused.status_code, refused.code, refused.retryable) == (
- 500,
+ 503,
"internal",
- False,
+ True,
)
- assert "does not expire within an hour" in refused.message
+ assert "lasts longer than an hour" in refused.message
assert pt.vendor.revoked == []
+ assert await _issuances(session_factory) == []
async def test_refuses_the_owners_token_handed_out_with_a_shorter_expiry(
self, session_factory, tmp_path, monkeypatch
@@ -1266,6 +1271,44 @@ class TestPassThrough:
assert marked == {live_id: True, expired_id: False}
assert warning is not None and warning.startswith("Example access already")
+ async def test_the_sweep_marks_a_pass_through_token_whose_owner_left(
+ self, session_factory, tmp_path
+ ) -> None:
+ """A workspace holding only pass-through tokens is still swept."""
+ pt = await _pass_through(session_factory, tmp_path / "c", expires_in=50 * 60)
+ await _issue(pt.broker, session_factory, pt.world.agent.id, service="example")
+ async with session_factory() as session:
+ await session.execute(
+ delete(TenantMember).where(TenantMember.user_id == pt.world.owner.id)
+ )
+ await session.commit()
+ await maintain_once(session_factory, pt.broker, prune=False)
+ async with session_factory() as session:
+ [record] = await session.scalars(select(ServiceTokenIssuance))
+ assert (record.revoke_requested, record.encrypted_token) == (True, None)
+ assert pt.vendor.revoked == []
+
+ async def test_disconnecting_another_service_names_only_what_it_withdrew(
+ self, session_factory, tmp_path
+ ) -> None:
+ pt = await _pass_through(session_factory, tmp_path / "c", expires_in=50 * 60)
+ await _issue(pt.broker, session_factory, pt.world.agent.id, service="example")
+ async with session_factory() as session:
+ await pt.broker.queue_revocation(
+ session, ServiceTokenIssuance.agent_id == pt.world.agent.id
+ )
+ await session.commit()
+ # A service this server has no adapter for: its sign-in cannot be
+ # revoked, so the secret is never read.
+ warning = await pt.broker.finish_disconnect(
+ session,
+ pt.world.owner.id,
+ [("unset", None)], # type: ignore[list-item]
+ )
+ assert warning is not None
+ assert "unset is not set up on this server" in warning
+ assert "Example access" not in warning
+
def _workspace_refusal(name: str) -> str:
return (
diff --git a/core/tests/switch_core/gateway/test_service_keys.py b/core/tests/switch_core/gateway/test_service_keys.py
index b7a74a432..bf53f7ec4 100644
--- a/core/tests/switch_core/gateway/test_service_keys.py
+++ b/core/tests/switch_core/gateway/test_service_keys.py
@@ -195,7 +195,7 @@ class TestConnect:
class TestUse:
- async def test_a_grant_made_before_is_refused_at_issue_and_kept(
+ async def test_a_grant_made_before_is_left_out_and_refused_at_issue(
self, service
) -> None:
harness, _ = service
@@ -210,9 +210,9 @@ class TestUse:
f"/agents/{agent_id}/service-tokens/datadog", headers=bearer(key)
)
- # Listed, so the session asks and is told why, rather than losing it
- # without a word.
- assert [grant["service"] for grant in grants.json()["grants"]] == ["datadog"]
+ # Left out of what a session starts with, as a switched-off service is:
+ # it would be refused every token anyway.
+ assert grants.json()["grants"] == []
assert token.status_code == 403, token.text
assert token.json()["error"] == {
"code": "forbidden",