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
30 changes: 5 additions & 25 deletions src/kiro_crew/dashboard/handlers_crew_turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@
#: test rather than copied by eye.
CONTROL_SECRET_HEADER = "X-SMC-Control-Secret"

#: The shape a per-crew control secret's name has, as the lane writes it: the
#: path the lane's own launch spec builds (``MicroVmLaunchSpec.control_secret_name``)
#: from the crew's launch tag, so the hub derives the name itself instead of
#: being told a path by the caller. Used only to DERIVE an id when the lane
#: recorded none; the recorded reference is always preferred, because it is what
#: the launch actually minted.
_SECRET_PATH = "{prefix}/{crew}/CONTROL_SECRET"


#: The one lane this route serves.
#:
Expand Down Expand Up @@ -103,27 +95,15 @@ def control_secret_id(inst: Any) -> str:
Fargate lane's own label (``Kiro Crew Cloud (<tag>)``) is not a valid secret
name at all.

Derives the name from the operator's configured prefix -- not the hardcoded
default, so a moved prefix is honoured -- only when the record was already
matched and simply carries no reference, where the tag used is the RECORD's
own. A row that owns no record gets ``""``, which the caller reports as a
crew it cannot authenticate to rather than guessing a name for it.
The recorded reference is the only source: ``MicroVmLaunchEngine.provision``
writes it into the record it creates. A row that owns no record, or a record
that carries no reference, gets ``""``, which the caller reports as a crew it
cannot authenticate to rather than guessing a name for it.
"""
try:
from kiro_crew.cloud.config import CloudConfig
except Exception: # noqa: BLE001 - no lane, no reference
return ""
record = _record_for(inst)
if record is None:
return ""
if record.control_secret_ref:
return record.control_secret_ref
# No reference stored, but the row IS this record's: the tag is the record's
# own, not a label the caller supplied, so deriving a path from it names this
# crew's secret and no other.
config = CloudConfig.load().microvm_config()
prefix = config.secret_path_prefix if config else "kirocrew/crew"
return _SECRET_PATH.format(prefix=prefix, crew=record.tag)
return record.control_secret_ref


def _record_for(inst: Any) -> Any:
Expand Down
55 changes: 8 additions & 47 deletions test/test_crew_turn_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,6 @@ def test_the_far_ends_own_turn_url_wins_over_a_composed_one():
assert mod._turn_target(status, "microvm") == "http://127.0.0.1:19999/v1/chat/completions"


def test_the_secret_path_is_the_one_the_deploy_wrote():
"""Derived from the crew's launch tag, never supplied by the caller.

A caller-supplied secret path would let any request name a secret for the
gateway to read with the owner's credentials and send to an endpoint of the
caller's choosing -- which is a read-any-secret primitive wearing a chat
route's clothes.
"""
assert (
mod._SECRET_PATH.format(prefix="kirocrew/crew", crew="l2crew")
== "kirocrew/crew/l2crew/CONTROL_SECRET"
)
from kiro_crew.cloud.microvm.engine import MicroVmLaunchSpec

spec = MicroVmLaunchSpec(
image_identifier="arn:aws:lambda:us-east-1:1:microvm-image:x",
image_version="1.0",
kms_key_id="",
)
# The lane's launcher and this route must name the SAME secret, or the hub
# authenticates with a value the guest never read.
assert spec.control_secret_name("l2crew") == mod._SECRET_PATH.format(
prefix="kirocrew/crew", crew="l2crew"
)


def test_the_control_header_matches_the_containers_own_constant():
"""One definition, asserted rather than copied by eye.

Expand Down Expand Up @@ -332,8 +306,7 @@ async def test_a_turn_against_a_really_registered_row_is_not_refused_for_its_sec
class TestTheSecretIsNamedByTheLaneNotTheDisplayName:
"""A crew's display name is a label, and labels are not secret names.

``Kiro Crew Cloud (<tag>)`` is not a valid Secrets Manager name, and a hardcoded
path would ignore a moved ``microvm.secret_path_prefix``.
``Kiro Crew Cloud (<tag>)`` is not a valid Secrets Manager name.
"""

def test_a_recorded_reference_is_preferred(self, tmp_path, monkeypatch):
Expand Down Expand Up @@ -394,13 +367,6 @@ class Inst:

assert control_secret_id(Inst()) == ""

def test_the_path_shape_takes_the_configured_prefix(self):
"""A hardcoded prefix ignores an operator who moved theirs."""
from kiro_crew.dashboard.handlers_crew_turn import _SECRET_PATH

assert "{prefix}" in _SECRET_PATH
assert _SECRET_PATH.startswith("{prefix}")


class TestTheCrewIsAddressedByItsOwnNameNotTheLaunchTag:
"""``model`` names the crew, and the launch tag is not that name.
Expand Down Expand Up @@ -914,8 +880,7 @@ def _must_not_run(*a, **k):


class TestControlSecretIdLanePrecedence:
"""``control_secret_id`` prefers this crew's recorded reference, then a derive
FROM THAT RECORD'S OWN TAG -- never from a label the row supplied.
"""``control_secret_id`` answers with this crew's recorded reference or nothing.

A row with no record resolves to nothing: ``instances.json`` is not write-protected,
so deriving from the row's label would let a relabelled row name any crew's secret.
Expand Down Expand Up @@ -950,11 +915,9 @@ def test_a_record_for_another_instance_is_not_borrowed(self, tmp_path, monkeypat
assert "someone-else" not in got, "another instance's reference was borrowed"
assert got == "", "a secret path was derived from the row's own label"

def test_the_derive_still_runs_for_a_matched_record_without_a_reference(
self, tmp_path, monkeypatch
):
"""The surviving half of the precedence: identity matched, no reference
stored, so the path is derived -- from the RECORD's tag."""
def test_a_matched_record_without_a_reference_resolves_nothing(self, tmp_path, monkeypatch):
"""Identity matched, no reference stored: no name is guessed for the
secret, so the route answers ``crew_secret_unavailable``."""
from types import SimpleNamespace

from kiro_crew.cloud.microvm.record import CrewRecord, CrewStore
Expand All @@ -964,17 +927,15 @@ def test_the_derive_still_runs_for_a_matched_record_without_a_reference(
monkeypatch.setattr("kiro_crew.cloud.microvm.record.CrewStore", lambda *a, **k: store)

inst = SimpleNamespace(
name="Kiro Crew Cloud (anything-else)",
name="Kiro Crew Cloud (l2crew)",
provisioner_id="microvm",
id="anything-else",
id="l2crew",
connection_method="ssm",
ssm_target=_REAL_NODE,
aws_profile="",
aws_region="",
)
got = mod.control_secret_id(inst)
assert got.endswith("/l2crew/CONTROL_SECRET"), "the record's own tag was not used"
assert "anything-else" not in got, "the row's label reached the secret name"
assert mod.control_secret_id(inst) == ""


class _ExplodingStream(_CapturingStream):
Expand Down
Loading