Compare commits

...
Author SHA1 Message Date
shadowdaoandClaude Opus 5.5 05ed1c37a5 fix(aws): phase 7 parked minors
Build App (Preview) / compute-version (pull_request) Successful in 9s
Secret Scan / scan (push) Successful in 21s
Secret Scan / scan (pull_request) Successful in 9s
Build App (Preview) / create-release (pull_request) Successful in 2s
Build Container / build-container (pull_request) Successful in 2m59s
Build App (Preview) / build-macos (pull_request) Successful in 6m17s
Build App (Preview) / build-windows (pull_request) Successful in 12m0s
Build App (Preview) / build-linux (pull_request) Successful in 21m5s
Build App (Preview) / prune-previews (pull_request) Successful in 4s
Build App (Preview) / test (pull_request) Successful in 24m1s
Interrupted-start notice gets its own toast topic and headline; the launch-type switch note only shows after a successful save and is dropped by later saves; README states the CreateLogStream scoping limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 05:24:56 -08:00
shadowdaoandClaude Opus 5.5 70cfa6b757 test(aws): Fargate logs under the narrowed scope
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:01:28 -08:00
shadowdaoandClaude Opus 5.5 6fa7ef5605 docs(aws): record phase 7 live run 7 (log scoping)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:44:47 -08:00
shadowdaoandClaude Opus 5.5 d85292ea00 fix(aws): add the EC2 root volume to the cost estimate; name public IPv4
The EC2 estimate adds the 50 GiB gp3 root volume at the region's gp3
GB-month price (Pricing API, cached per region like the instance price;
fixture recorded 2026-11-16). The note says whether it is included and
that a public-mode environment's IPv4 address adds $0.005/hour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:40:12 -08:00
shadowdaoandClaude Opus 5.5 1d1e356fcd fix(aws): EC2 host settings polish
HostSection says what a launch-type switch changed besides the type,
checks the saved reservations against the instance type once on load,
and is re-indented. The EC2 capacity cache keeps none/unusable for one
minute only. RuntimeSection's VPN hint on EC2 speaks of an ECS task on
the user's instance. Joining over an incomplete onboarding with no
record warns that None removes half-built EC2 capacity, and starts from
the capacity last chosen in this run when there is one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:37:07 -08:00
shadowdaoandClaude Opus 5.5 77db6028d0 fix(aws): write logs only into the user's own streams; no log rights on the instance role
The instance role (reachable from a Docker-access project's host-network
container) could PutLogEvents into any user's streams. It now has no log
rights: awslogs writes with the execution role
(ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE=true). The task and execution
roles create streams in the environment's group but write only into
log-stream:<owner_key>/* (every awslogs stream's prefix); the Fargate
fingerprint moves deliberately. The live test asserts task logs are
written. README: per-user logs, orphan clock skew, verdict wording;
boundary version comments no longer mention Session Manager.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:33:57 -08:00
shadowdaoandClaude Opus 5.5 8fe0810901 fix(aws): never let the user record vouch for EC2 names tagged as another owner's
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:30:41 -08:00
shadowdaoandClaude Opus 5.5 8c9ffd4c4c fix(aws): never attach to an exec-off EC2 task after a restart; widen the orphan margin
Reconcile stops a placed or orphaned exec-off task (REPLACED_REASON,
checked; the ARN is cleared only once the stop succeeded) and marks a
recent one still waiting Error with its ARN kept and an 'interrupted'
warning. Reattach refuses an exec-off task. ORPHAN_MARGIN is 10 minutes
(clock skew). A failed EC2 record read no longer blocks attaching to a
live task. One helper builds the task cluster list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:30:05 -08:00
shadowdaoandClaude Sonnet 5.5 719a75b46d docs(aws): phase 7
Spec: phase 7 done, rulings (E1-E11 as built, R1-R4, cold-start rerun),
data model, feature table. CLAUDE.md: EC2 rules and the docker-socket
step; sandbox is off on all AWS. aws/README.md: triple-c-user admin
setup and caveats, measured sizes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 18:19:39 -08:00
shadowdaoandClaude Opus 5.5 6e24977cf0 fix(aws): replace an orphaned exec-off EC2 task instead of refusing for ever
A found task with ECS Exec off that still waits for an instance is
another start's only while one could still be waiting for it (created
within the EC2 placement wait plus 2 minutes); an older one is a crashed
start's orphan and is replaced (stopped with its own reason, checked,
waited for). INSTANCE_RACE now mentions Stop. The rerun's race records
the winner's task, as the first launch's race does; a first task another
start replaced says so. TaskInfo gains created_unix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:16:22 -08:00
shadowdaoandClaude Opus 5.5 374fb5a35d test(aws): harden the phase 7 live test; record the update probe
The guard paginates Auto Scaling groups and treats a failed read as not
yet gone; the test asserts exactly one live task after a cold start,
the sandbox off in the task's settings and bwrap failing (E8). README:
updating a group to an unsupported template version is denied by the
launch check (live probe); run 6 after the review fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:12:51 -08:00
shadowdaoandClaude Opus 5.5 10f473f3da fix(aws): show the sandbox as off wherever an AWS project can't run it
PermissionModeControl read the raw sandbox_mode_enabled, so an AWS
project showed "Sandbox ON — bubblewrap isolation" and no Bypass
caution although the host forces it off. A shared sandboxEffective()
(false for any AWS project) now feeds both it and the Runtime toggle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:59:44 -08:00
shadowdaoandClaude Opus 5.5 c25761789e fix(aws): never run a second task for a stopped or contested cold EC2 start
The cold-start rerun now runs again only once an instance has taken the
first task and it is still meant to run; a first task that stops by
itself (a user's Stop, a failed placement) ends the start with its
reason. The second launch goes through find_or_run, so a concurrent
start's task is reported as START_RACE instead of doubled. A found task
with ECS Exec off that is still waiting for an instance belongs to a
start in progress (INSTANCE_RACE); a placed one is stopped, the stop is
checked and waited for (its own 3-minute budget) before a new run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:59:09 -08:00
shadowdaoandClaude Opus 5.5 fc3456c44e test(aws): live phase 7 EC2 checks
a_phase7_ec2_workspace_round_trips: onboarding with per-user EC2
capacity through prepare_onboarding (tagged workspace role, all four
admin files), its collision check and refusal, admin-tier scaling
probes, a cold and a warm EC2 start (Docker access, VPN, ECS Exec into
the bridge task), in-container checks (docker ps, wg/tun,
src_valid_mark, bwrap, IMDS, introspection), RunTask on another user's
cluster, stop and scale-in, removal, re-apply without EC2 with an
instance up, offboard and destroy. The guard (ec2_cleanup) force-deletes
any per-user EC2 capacity. Results in aws/README.md and the spec.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:52:36 -08:00
shadowdaoandClaude Opus 5.5 6c7c2ea4ef fix(aws): keep Claude Code's sandbox off on EC2 too (E8)
Measured live on the ECS-optimized AL2023 AMI (kernel 6.1,
max_user_namespaces 30937): bwrap --unshare-user in the ECS task fails
with "No permissions to create new namespace", as on Fargate. Docker's
default seccomp profile refuses it without CAP_SYS_ADMIN. sandbox_allowed
is false for every AWS project, and the Runtime toggle is locked off on
EC2 as on Fargate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 16:35:30 -08:00
shadowdaoandClaude Opus 5.5 43fcc520ab fix(aws): run a cold EC2 task again once its instance is up
ECS turns enableExecuteCommand off on a RunTask that has to wait for an
instance (PROVISIONING behind a capacity provider), and the task keeps it
off once placed: verified live (CloudTrail shows true in the request,
false in the answer; a RunTask with an instance up keeps it). Such a
task can never be reached, so every cold EC2 start timed out.

A cold start now lets that first task bring the instance up, stops it as
soon as it is placed, waits for it to stop and runs the task again onto
the instance; a second task without ECS Exec is stopped and reported. A
live task found with ECS Exec off is stopped and replaced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 16:34:38 -08:00
shadowdaoandClaude Opus 5.5 bf5a810e0d fix(aws): allow template-only Auto Scaling group creates
A CreateAutoScalingGroup that names only a launch template sends no
autoscaling:InstanceTypes (verified live), so the scaling file's
Null:false on it denied every per-user group. Drop it; the type is held
by the caller's own ec2:RunInstances launch check on the template, which
also refuses MixedInstancesPolicy overrides of unsupported types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:24:55 -08:00
shadowdaoandClaude Opus 5.5 9e1a63cde5 feat(aws): EC2 projects in the UI, Docker access and VPN
Host settings get the launch-type control (Fargate | EC2 with the user's
instance type, disabled with the reason when their onboarding has no EC2
capacity), switching through launchTypePatch, which now also clears the
gateway sidecar and Spot for EC2 and snaps sizes per side. EC2 projects size
CPU/memory reservations checked by Rust's fit rule (ec2_fit_problem, new
command) against the user's record (aws_ec2_capacity, new command); no
ephemeral size, Spot or architecture; notes for scale-to-zero, Docker access
as root on the instance and no sidecar. The sandbox follows the project on EC2.

The cost estimate prices an EC2 project as its instance type's AmazonEC2
on-demand Linux shared-tenancy price (recorded fixture), cached per region and
type with the Fargate cache's rules. The entrypoint's Docker-socket group step
now runs in aws mode when the socket exists; the VPN skill follows
VPN_SUPPORT_ENABLED there as on Docker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:03:57 -08:00
shadowdaoandClaude Opus 5.5 d6ef6a5fd2 feat(aws): run projects on per-user EC2 capacity
EC2 projects register an EC2/bridge task definition (no runtime platform or
ephemeral storage; task-level reservations checked against the user's
instance type with a 10% memory margin), with the instance's Docker socket
and the Docker VPN pieces (NET_ADMIN, /dev/net/tun, src_valid_mark) when
enabled, and run on the user's cluster through their capacity provider.
The start reads the user record, waits up to 10 minutes with "Starting an
instance" progress, and maps RunTask access denied to the triple-c-user
mismatch. Stop, reconcile, removal and the watch follow the task's own
cluster, so a running EC2 task outlives its record. The sandbox stays on
for EC2; the gateway sidecar is refused there for now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:46:06 -08:00
shadowdaoandClaude Opus 5.5 98e330060e fix(aws): EC2 name check by owner tag; keep unusable records; bundle warning
The pre-apply check now decides ownership by the resources' triple-c:owner
tag (record as fallback), so a failed apply of one's own capacity can be
re-applied; any cluster status but INACTIVE holds the name. An onboarding
seam (OnboardApi) makes the record read and the name lookups testable, and
prepare_onboarding settles everything before launch. With no choice given,
an unreadable or invalid record is refused instead of removing capacity.
Bundles warn when their cluster name looks taken; the README check prints
owner tags. The UI keeps the recorded type when the instance list fails and
says when an apply removes EC2 capacity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:31:30 -08:00
shadowdaoandClaude Opus 5.5 1a2c530be8 feat(aws): choose EC2 capacity when onboarding
Self-onboarding and the onboarding bundle take an EC2 capacity choice:
none, or an instance type from the app's table (grouped x86/ARM, default
m7i.large), 1-4 instances and the user's triple-c-user value. A re-apply
keeps the recorded capacity unless changed (the record gains
max_instances). Before any self-onboarding apply with EC2 the app refuses
per-user names (ACTIVE cluster or Auto Scaling group) that this owner's
record doesn't name (R3). OnboardingState carries the user's compute from
their record; the panel shows it. The bundle README's collision check now
comes before the apply step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:22:31 -08:00
shadowdaoandClaude Opus 5.5 e9c20287b7 fix(aws): EC2 name-collision step in onboarding bundles; bridge agent config
Task 3 fix round 1 (rulings R2, R3):
- Onboarding admin bundles' README gains an "EC2 capacity" step: before
  applying with ec2_instance_type, check with describe-clusters /
  describe-auto-scaling-groups that triple-c-<env>--u-<tag> is unused
  unless this owner's /triple-c/users/<env>/<owner key> record names it
  (exact names; <tag> when the bundle has none). Tested.
- User data for bridge tasks (R2): ECS_ENABLE_TASK_IAM_ROLE=true instead
  of ECS_AWSVPC_BLOCK_IMDS; IMDSv2 hop limit 1 kept, comment corrected;
  the test asserts no ECS_ENABLE_TASK_ENI / awsvpc settings.
- Reverted unrelated rustfmt churn in tofu_modules/tests.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:07:28 -08:00
shadowdaoandClaude Opus 5.5 9410c098ed feat(aws): optional per-user EC2 capacity in the onboarding module
Phase 7 Task 3 (E1-E4, E11; onboarding module version 2):
- ec2.tf: with ec2_instance_type set, the user's own cluster
  triple-c-<env>--u-<ec2_user_tag> with exactly one capacity provider
  (managed termination protection, managed scaling 100% one instance at
  a time, 60 s warm-up), an Auto Scaling group of 0..ec2_max_instances
  (desired left to ECS, protect_from_scale_in, force_delete off so the
  provider drains before deleting, fixed launch-template version, default
  tags + AmazonECSManaged), a launch template on the ECS-optimized AL2023
  AMI by architecture (IMDSv2 hop 1, 50 GiB gp3 encrypted root, the
  environment's tasks group, public IP as the environment's tasks get
  one, ECS_CLUSTER + ECS_AWSVPC_BLOCK_IMDS), the instance role
  triple-c-<env>-<owner key>-instance (bounded, tagged triple-c:cluster,
  ECS agent on its cluster only, ECR pull, logs; no SSM core) and its
  instance profile, and the user record /triple-c/users/<env>/<owner key>.
  Empty type (the default): count 0 everywhere, Fargate plan unchanged.
- Variables ec2_instance_type (the app's 30 types), ec2_max_instances
  (1-4), ec2_user_tag (as validate_ec2_user_tag, required with a type);
  onboarding_vars takes Option<&Ec2Compute> and validates the same.
- The environment record's assign_public_ip is now format-checked.
- tofu_modules.rs: ONBOARD_MODULE_VERSION 2, user_record_parameter_name,
  USER_RECORD_FIELDS, Ec2Compute, EC2_MAX_INSTANCES, ec2.tf embedded.
- Tests: EC2 calls merged into MODULE_CALLS under real types
  (all_module_calls removed); gating + version-1 resource fingerprint;
  launch template, group, capacity provider, instance role, AMI by arch,
  record fields, names, variable rules; the instance role inside the
  boundary with its cluster principal tag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:00:23 -08:00
shadowdaoandClaude Opus 5.5 9829ff0c43 feat(aws): per-user EC2 clusters keyed by the triple-c-user principal tag
Fix round 2 for the phase 7 policies (amends plan E1):
- Per-user EC2 names: triple-c-<env>--u-<user tag> for the cluster,
  capacity provider, Auto Scaling group and launch template
  (user_ec2_stem, validate_ec2_user_tag; `--` can't occur in environment
  names or user tags, so the patterns are unambiguous); the instance
  role/profile stay owner-key named (user_instance_role_name, <= 64).
- workspace-user-tasks.json: RunTask/StartTask on a triple-c-*--u-*
  cluster is denied unless it is the caller's own
  (${aws:PrincipalTag/triple-c-user}), and denied without the tag.
- Admin: capacity providers and groups narrowed to triple-c-*--u-*; the
  boundary's agent cluster tag must match triple-c-*--u-*.
- Instance-type fail-open closed: ec2:InstanceType required (StringEquals,
  volumes in their own statement); group create requires
  autoscaling:InstanceTypes present.
- The compute file is split: environment-admin-scaling.json (groups,
  instance profiles, service-linked roles) is the admin tier's fourth file.
- Evaluator learns ArnNotLike. README: triple-c-user setup, sizes,
  still-open probes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 13:44:55 -08:00
shadowdaoandClaude Opus 5.5 e6fbf4e133 fix(aws): pin, type-limit and fence per-user EC2 capacity permissions
Review fix round 1 for the phase 7 admin compute file and boundary:
- Auto Scaling groups: create needs autoscaling:LaunchTemplateVersionSpecified
  (update BoolIfExists), no target groups / load balancers, supported
  instance types; CreateOrUpdateTags only on already-tagged groups.
  ModifyLaunchTemplate dropped (the default version never moves).
- RunInstances: instance and volume need the component request tag,
  the instance a supported type (the app's ec2_instance_spec table,
  pinned by a test); the security group must be the template's own and
  component-tagged, the image Amazon's and the template's.
- Boundary: the ECS agent only on the cluster its role's
  triple-c:cluster principal tag names (must be triple-c-*); Session
  Manager / ec2messages dropped (plan E4 amended).
- Evaluator learns Bool/BoolIfExists; the simulator sends the Boolean
  keys typed Boolean.
- README: instance types, pinning, -instance suffix is not an admin
  fence, RunInstances wording, still-open probes, IMDS/Docker note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 13:17:54 -08:00
shadowdaoandClaude Opus 5.5 7393143762 feat(aws): admin and boundary permissions for per-user EC2 capacity
Phase 7 Task 2 (E4, E5, E11):
- New third admin file environment-admin-compute.json (3,705 chars):
  capacity providers (triple-c-*), component-tagged launch templates,
  triple-c-* Auto Scaling groups (request tag to create, resource tag to
  change/protect/delete), RunInstances only from a tagged launch template
  with Amazon images, tags only at create, instance profiles (triple-c-*),
  PassRole of triple-c-*-instance roles to ec2.amazonaws.com only,
  service-linked roles for Auto Scaling and ECS only, the public
  ECS-optimized AMI parameter, autoscaling/capacity-provider reads.
- Boundary v2 (2,625 chars): the ECS agent's calls on triple-c-* clusters
  and container instances, TagResource only at RegisterContainerInstance,
  DiscoverPollEndpoint and Session Manager core. BOUNDARY_MODULE_VERSION=2
  with a template_version local and default tag, pinned by a test.
- Checks for every new action; tests for tag/name scoping, launch-only-
  through-templates, PassRole to EC2 only for instance roles, the
  workspace tier changing no compute, the boundary's agent fence.
- EC2_COMPUTE_CALLS in tofu_modules/tests.rs: the onboarding module's
  expected calls, evaluated against the admin tier and in the write drift
  test until Task 3 moves them into MODULE_CALLS.
- README: third file, sizes, boundary re-apply, resource table rows,
  scoping notes and "still open" points for the live run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 13:07:37 -08:00
shadowdaoandClaude Opus 5.5 c6b46a7347 fix(aws): refuse Docker/VPN on Fargate without grandfathering; launchTypePatch
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:56:18 -08:00
shadowdaoandClaude Opus 5.5 86f253b7da feat(aws): EC2 launch type setting
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:54:24 -08:00
shadowdaoandClaude Opus 5.5 edef77feed docs(aws): phase 7 plan — ECS on EC2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:46:40 -08:00
jknapp 1659788b70 Merge pull request 'AWS host phase 6: EFS, copy-at-connect, setup scripts, lifetime cap, ports, gateway sidecar, cost estimate' (#83) from feat/aws-phase6 into main
Secret Scan / scan (push) Successful in 22s
Build Container / build-container (push) Successful in 4m51s
2026-11-16 20:36:36 +00:00
shadowdaoandClaude Opus 5.5 0a325cb7f2 fix(aws): Host settings merge saves against the latest config and cache the environment's lifetime cap
Build App (Preview) / compute-version (pull_request) Successful in 38s
Secret Scan / scan (push) Successful in 17s
Secret Scan / scan (pull_request) Successful in 11s
Build App (Preview) / create-release (pull_request) Successful in 7s
Build App (Preview) / test (pull_request) Successful in 8m14s
Build App (Preview) / build-macos (pull_request) Successful in 6m4s
Build App (Preview) / build-windows (pull_request) Successful in 10m43s
Build Container / build-container (pull_request) Successful in 19m45s
Build App (Preview) / build-linux (pull_request) Successful in 16m22s
Build App (Preview) / prune-previews (pull_request) Successful in 9s
A save right after the setup script's blur (a toggle) spread the render-time
config and reverted the script; saves now start from the stored config plus
every patch saved since. aws_environment_max_hours is cached per connection,
region and environment for 10 minutes, so re-mounting the section does not
ask AWS again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:10:37 -08:00
shadowdaoandClaude Opus 5.5 ea37f8434f fix(aws): retry the copy-at-connect secret read while the secret is pending deletion
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:46 -08:00
shadowdaoandClaude Opus 5.5 544409c554 fix(aws): no Pricing client counts as a failed fetch, so the 5-minute back-off applies
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:46 -08:00
shadowdaoandClaude Opus 5.5 be96b5f68f fix(aws): the copy-at-connect warning gets its own toast topic
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:46 -08:00
shadowdaoandClaude Opus 5.5 ff5ae9862b fix(aws): a waiting clone retry no longer holds the retry lock, so a later connect cannot strand it
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:07:14 -08:00
shadowdaoandClaude Opus 5.5 9e1e72d2a4 feat(aws): setup script editor
Build App (Preview) / compute-version (pull_request) Successful in 6s
Secret Scan / scan (push) Successful in 7s
Secret Scan / scan (pull_request) Successful in 14s
Build App (Preview) / create-release (pull_request) Successful in 6s
Build App (Preview) / build-macos (pull_request) Successful in 6m3s
Build App (Preview) / test (pull_request) Successful in 8m39s
Build App (Preview) / build-windows (pull_request) Successful in 11m38s
Build Container / build-container (pull_request) Successful in 17m24s
Build App (Preview) / build-linux (pull_request) Failing after 21m19s
Build App (Preview) / prune-previews (pull_request) Skipped
HostSection gets a monospace "Setup script" textarea (saved on blur, blank
as none) with the 32 KiB UTF-8 limit checked using the backend's message,
and notes on when it runs (as claude, bash -e, after the clone, every start,
30 minutes, output in the Overview card) and what it gets under
copy-at-connect.

Also in Host settings: the environment's advisory lifetime cap (Q8), read
by a new aws_environment_max_hours command, and the copy-at-connect limits
(no CA before the first connect, removed credentials linger in a running
task, no setup re-run after a re-clone), documented in CLAUDE.md too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:59:37 -08:00
shadowdaoandClaude Opus 5.5 5388db487e fix(aws): never run two workspace-status polls at once
A read in flight blocks the visibilitychange restart, so hiding and showing
the window mid-read no longer starts a second poll chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:59:37 -08:00
shadowdaoandClaude Opus 5.5 3bc2b41b70 test(aws): pin the 168-hour lifetime cap across Rust, TypeScript and HCL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:27 -08:00
shadowdaoandClaude Opus 5.5 f3d542ba60 fix(aws): build the pricing client inside the cost estimate's 8 s bound
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:27 -08:00
shadowdaoandClaude Opus 5.5 66a9993e69 fix(aws): retry a secret write while its deletion finishes
After a timed-out replace (secret force-deleted, not made again) the next
start retries the create/put for up to 30 s, then says to start again in a
minute; no more advice to restore the secret in the console.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:26 -08:00
shadowdaoandClaude Opus 5.5 56acab1bf2 fix(aws): register the workspace follower before spawning it
begin_follow runs in spawn_follow and the token is passed in, so a quick
Stop→Start can't leave the old follower registered last.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:26 -08:00
shadowdaoandClaude Opus 5.5 f87edbc5df fix(aws): build the gateway sidecar payload only when preparing a task
Attaching to a running task no longer needs the provider key or gateway
models; the payload is built lazily on the prepare path only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:26 -08:00
shadowdaoandClaude Opus 5.5 34f8a4161d fix(container): copy-at-connect setup waits for the connect; distinct WIP refs; atomic CA swap
- start_clone_repos waits for the first connect when a setup script exists,
  not only when there are repositories, so the script sees the CA, SSH keys
  and Git credentials.
- wip_ref appends 8 hex of sha256(raw folder) when sanitising changed the
  name, so `a b` and `a_b` never share a WIP branch.
- triple-c-apply-connect moves new CA files to a temporary name and swaps
  them in; a failed move keeps the CA installed before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:50:55 -08:00
shadowdaoandClaude Sonnet 5.5 d63f484edc docs(aws): restore spec sections lost in the phase 6 docs edit
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 10:38:25 -08:00
shadowdaoandClaude Sonnet 5.5 9f0c482222 docs(aws): phase 6
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 10:37:21 -08:00
shadowdaoandClaude Opus 5.5 3cd8542670 test(aws): live phase 6 checks
a_phase6_workspace_round_trips: one environment (max_workspace_hours 1)
and one project with EFS storage, a setup script, copy-at-connect with a
CA file, a port mapping and the gateway sidecar, two task starts, then
removal with discard. Passed live in us-west-2 (results in aws/README.md
and the spec). The phase-5 setup moves into deploy_for_workspace_user;
aws_host::ensure_started_with takes the sidecar's provider key from the
caller (the live tests have no keychain). scripts/aws-e2e-workspace.sh
runs TRIPLEC_E2E_TEST when set. The guard's task-definition retirement
listed nothing (ECS familyPrefix is a whole family name); it now filters
the full list by prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:34:46 -08:00
shadowdaoandClaude Opus 5.5 904547b2f1 feat(aws): estimated hourly cost per workspace
Q5: aws/pricing.rs reads on-demand Fargate rates (x86 and ARM vCPU-hours,
GB-hours, ephemeral storage GB-hours) from the Price List API in us-east-1
(AmazonECS; regionCode, termType=OnDemand, usagetype CONTAINS Fargate),
caches them per region for 24 h under <data_dir>/triple-c/aws-pricing/,
and estimates a config's hourly cost (ephemeral storage billed above
20 GiB; the gateway sidecar adds nothing). aws_cost_estimate(connection_id,
region, config) never errors: denied, offline or slow (8 s) is None,
shown as "Estimate unavailable"; a failed region is not re-asked for
5 minutes and a stale cache still answers.

pricing:GetProducts on * joins workspace-user-tasks.json with its check,
a policy test, the README and a live test (passed). HostSection shows the
estimate while configuring, Overview while running, with the Spot and EFS
notes. Fixture: a recorded us-west-2 GetProducts answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:14:06 -08:00
shadowdaoandClaude Opus 5.5 2857b4ae7d feat(aws): optional LLM gateway sidecar
A non-essential 'gateway' container runs the digest-pinned LiteLLM image
next to the workspace (Q7): its secrets are exactly the three
TRIPLEC_GATEWAY_* keys, the config uses os.environ/ references (no key
in it), /bin/sh writes it to a 0600 file and execs LiteLLM on
127.0.0.1:4000. The workspace's ANTHROPIC_BASE_URL/AUTH_TOKEN point at
it with a master key minted per task start; under copy-at-connect that
token and the gateway keys stay in the secret (R1). Needs the
OpenAI-compatible backend and a gateway model (validated on save, start
and in the UI). Requests with the toggle off are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:57:07 -08:00
shadowdaoandClaude Opus 5.5 45aef8da7d fix(aws): serialise and diff port-mapping forwards; refuse the gateway sidecar's port
Opens hold a per-project lock and re-read the stored mappings, so
overlapping saves converge on the last one without false 'in use' errors.
Unchanged forwards keep their live connections. The sidecar's 4000 is
refused while it runs (mappings reach workspace loopback). Stored rows
that would be refused no longer block unrelated saves; they are skipped
with a warning at connect. Port-mapping logs get their own prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:45:37 -08:00
shadowdaoandClaude Opus 5.5 08c779ad36 feat(aws): forward port mappings to AWS workspaces
Each TCP mapping of an AWS project becomes a loopback-only listener
carried into the task over socat-over-attach (the auth bridge's
PortForward, generalised to separate host and container ports; mappings
skip the bridge's first-byte/web gate). One set per project, opened after
every connect and on a save while connected, closed with the tunnel
(stop, remove, task death, exit). A failing mapping is a workspace-warning;
UDP and reserved ports are refused on save. The Config tab offers the
editor on AWS, TCP only, "Reachable only from this machine (localhost)".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:00:56 -08:00
shadowdaoandClaude Opus 5.5 262ea041c8 fix(aws): create the secret before reading it; one staging dir per connect
- Copy-at-connect tries CreateSecret first and reads the current value only
  when the secret exists, so a first start never reads a missing secret
  (which the owner-tag condition may refuse).
- triple-c-apply-connect: --prepare makes and prints a per-connect nonce
  dir; the app uploads into it and applies exactly that dir, so concurrent
  connects never mix files. CA files move to a fixed path so an unchanged
  CA is not reinstalled.
- connect.env parsing kept a trailing '=' (base64 padding) — it was dropped.
- Pending-deletion errors from GetSecretValue are reported as such; message
  and doc-comment fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:51:14 -08:00
shadowdaoandClaude Opus 5.5 820bfab25c fix(aws): copy-at-connect review fixes — lock, ~/.ssh off EFS, secret history
- triple-c-apply-connect: the background re-clone closes the apply lock's
  fd, so a later connect never waits on it; --prepare takes the lock.
- R6: under copy-at-connect Hybrid/EFS no longer link ~/.ssh to EFS; the
  copied keys vanish with the task (UI says so, and that old keys on EFS
  stay until removed).
- R7: a copy-at-connect start that finds credentials still in the secret
  force-deletes it and creates it again (retrying ~30 s while AWS finishes),
  so no AWSPREVIOUS version keeps them.
- TRIPLE_C_CUSTOM_ENV (unsalted fingerprint) left out of the secret.
- Terminals get only the credential pairs (no file reads; keychain off the
  runtime), so a bad SSH/CA file never drops the API keys.
- ConnectBundle values are Zeroizing; UI copy updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:43:12 -08:00
shadowdaoandClaude Opus 5.5 8510797c22 feat(aws): copy credentials at connect instead of storing them
Copy-at-connect (phase 6, Q2): with secrets_delivery copy_at_connect the
project secret keeps settings, repos and the agent token but drops
TRIPLEC_FILES and every credential pair (SECRET_ENV_KEYS and all custom
variables), marking TRIPLEC_CREDENTIALS=connect. After every connect (start,
attach, background re-attach) the app uploads the SSH/CA files and a
connect.env as root into /dev/shm/triple-c and runs the new
triple-c-apply-connect: CA install as root, SSH merge and git credential
helper as claude, a `connected` marker the first clone waits for, and a
background re-clone of failed repos only. Terminals add the pairs to their
own ExecSpec env. A failed push is a workspace-warning.

The entrypoint's CA, SSH, git-credential, Hybrid-home and clone functions
move to container/entrypoint-lib.sh (sourced first; defines only), so both
scripts run the same code; Docker behaviour is unchanged. HostSection gets
the Credentials choice with its warnings (and the gateway note, R1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:32:09 -08:00
shadowdaoandClaude Opus 5.5 9c888caf13 fix(aws): follow clone and setup in the background, after the start
Ruling R5: the start ends after Connecting (Running, lock released) and a
background follower (cancelled on Stop, Remove, tunnel close, app exit)
reports only a failure it saw or one on a task this start ran. A stale
Starting/Stopping AWS project loads as Error, and reconcile also checks a
Stopped one that still has a task ARN, so a crash never hides a live task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:09:51 -08:00
shadowdaoandClaude Opus 5.5 6b62a2a241 feat(aws): follow clone and setup when a workspace starts
The payload carries TRIPLEC_SETUP_SCRIPT and TRIPLEC_MAX_LIFETIME_MINUTES
(environment hours x 60 when capped); the 64 KiB error names the largest
parts. After Connecting, the start follows triple-c-workspace-status through
the agent (background exec, 2 s, progress on change, 35 min cap, quiet stop
when the tunnel goes). A failed clone/setup is a workspace-warning event and
a warning toast; Overview shows an AWS status card via aws_workspace_status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:01:10 -08:00
shadowdaoandClaude Opus 5.5 431cdc83cb fix(container): one WIP branch per run and folder, setup chain records failed, lifetime read as decimal
Two folders sharing a remote no longer force-push over each other's
triple-c/wip/<run-id>; the folder is part of the branch name (sanitised,
check-ref-format validated, hash fallback). A setup chain that dies with
the setup still running records failed so the idle stop is not blocked.
TRIPLEC_MAX_LIFETIME_MINUTES 00 means no cap and 08 means 8. The run record
is written after the agent-token block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 07:52:12 -08:00
shadowdaoandClaude Opus 5.5 618cac1a49 feat(container): setup script, lifetime cap and one WIP branch per run
TRIPLEC_SETUP_SCRIPT runs as claude after the background clone (bash -e,
stdin /dev/null, 30 min timeout) with status/log in /run/triple-c-setup;
the idle watcher counts it as busy and enforces TRIPLEC_MAX_LIFETIME_MINUTES
(best-effort WIP push, marker lifetime, exit 76 = StopKind::LifetimeCap).
WIP pushes go to one triple-c/wip/<run-id> branch per task run, skipping an
unchanged tree. New triple-c-workspace-status prints clone + setup JSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 07:44:20 -08:00
shadowdaoandClaude Opus 5.5 ad447555d9 fix(aws): clone through scratch and never delete a final path
clone_repos now clones into /workspace/.triple-c-clone/<folder> and renames
into place only on success, emptying scratch at each start; nothing at a
final path is ever deleted by the clone code. Replaces the in-repo
triplec.cloning marker, whose failed unset could get a working repository
deleted on the next start. triple-c-wip-push skips the scratch folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:49:57 -08:00
shadowdaoandClaude Opus 5.5 5477c2cea5 feat(aws): EFS storage mode
EFS storage is Hybrid plus /workspace on a second access point of the
user's own file system, rooted at /triple-c-workspaces/<owner_key>/<project>
(a sibling of the home roots) and mounted at /workspace. The workspace
access point is found by root + owner + project tags or created, waited
for before RunTask, recorded store-owned, kept when switching to Hybrid or
Ephemeral, and deleted with the home one by a discarding removal. Hybrid
and Ephemeral task definitions are byte-identical to before (pinned
fingerprints).

The entrypoint links home state for efs as for hybrid, warns when
/workspace is not mounted, and marks a clone in progress in its own
.git/config so a clone a killed task cut short is redone on the next start
while existing repositories are still skipped.

The EFS segment is enabled in HostSection, with a note when /workspace
files remain on an access point after switching away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:44:17 -08:00
shadowdaoandClaude Sonnet 5.5 cbaf6209fd feat(aws): environments can cap workspace lifetime
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 21:33:54 -08:00
shadowdaoandClaude Opus 5.5 34a6edd413 feat(aws): phase 6 project settings
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:29:29 -08:00
shadowdaoandClaude Opus 5.5 6497dd84e9 docs(aws): phase 6 plan — lifetime cap enforced inside the workspace
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:24:51 -08:00
shadowdaoandClaude Opus 5.5 fd0d51219f docs(aws): phase 6 plan
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:56:25 -08:00
jknapp 03f62b093f Merge pull request 'AWS host phase 5: Fargate workspaces' (#82) from feat/aws-fargate-workspaces into main
Secret Scan / scan (push) Successful in 5s
Build Container / build-container (push) Successful in 1m49s
2026-11-15 21:51:12 +00:00
shadowdaoandClaude Opus 5.5 b80ca0681e docs(aws): record the live re-run of the final idle-stop path
Secret Scan / scan (push) Successful in 10s
Build App (Preview) / compute-version (pull_request) Successful in 7s
Secret Scan / scan (pull_request) Successful in 17s
Build Container / build-container (pull_request) Successful in 4m3s
Build App (Preview) / create-release (pull_request) Successful in 4s
Build App (Preview) / test (pull_request) Successful in 6m19s
Build App (Preview) / build-macos (pull_request) Successful in 5m54s
Build App (Preview) / build-windows (pull_request) Successful in 10m27s
Build App (Preview) / build-linux (pull_request) Successful in 16m45s
Build App (Preview) / prune-previews (pull_request) Successful in 20s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:28:39 -08:00
shadowdaoandClaude Opus 5.5 d73a19a9e7 docs: note the stop guard order, background polls, clone skip and trap order
Secret Scan / scan (push) Successful in 18s
Build App (Preview) / compute-version (pull_request) Successful in 5s
Build App (Preview) / test (pull_request) Successful in 6m21s
Secret Scan / scan (pull_request) Successful in 8s
Build App (Preview) / create-release (pull_request) Successful in 1s
Build App (Preview) / build-macos (pull_request) Successful in 5m55s
Build App (Preview) / build-linux (pull_request) Successful in 8m29s
Build App (Preview) / build-windows (pull_request) Successful in 10m36s
Build App (Preview) / prune-previews (pull_request) Successful in 2s
Build Container / build-container (pull_request) Successful in 22m23s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:08:05 -08:00
shadowdaoandClaude Opus 5.5 c467308513 fix(aws): keep the access point id when a removal keeps the saved files
remove_aws_resources cleared access_point_id even without discard_data, though the
access point stays; the project could then move hosts and orphan it. The id is
now kept, so a later start reuses it, a later removal with "Also remove the
access point" deletes it, and moving the project is refused until then (the
message and the Host section say so).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:08:05 -08:00
shadowdaoandClaude Opus 5.5 be2825d344 fix(aws): the file viewer's change poll no longer counts as workspace activity
Its 2 s sha256sum poll now carries X-Triple-C-Background, so an open viewer does
not keep an idle AWS workspace up. Opening and saving a file stay foreground;
Docker is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:08:05 -08:00
shadowdaoandClaude Opus 5.5 33eb63d907 fix(aws): forget a stale stopped mark on a project's unattached target id
The death watch's stopped marks are keyed by target id, and an unattached AWS
project's id (http://unattached.invalid/<id>/) is reused by every later unattached
spell, but only an endpoint URL's mark was ever removed. A stale mark could make
the watch report a live, billed task as stopped. Registering a tunnel now clears
the project's placeholder mark, and reconcile forgets it before asking ECS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:08:05 -08:00
shadowdaoandClaude Opus 5.5 884a665e7d fix(aws): close terminals before the stop guard's push and bound it in the container
The guard pushed and recorded `pushed` while terminals (and Claude in them) were
still open; the TERM hook then skipped its push, so later edits were lost. Stop now
closes the project's exec sessions and auth bridge first, then pushes, records the
marker and stops the task. A failed push still aborts the stop with the status
restored; the auth bridge is re-armed and the error says the terminals were closed.

The script also runs under `/usr/bin/timeout -k 5` ending 10 s before the client's
timeout: the agent does not kill an exec its client gave up on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:07:54 -08:00
shadowdaoandClaude Opus 5.5 170ba1e94d fix(container): skip repos still cloning, re-check idleness after the idle push, trap TERM before background work
- clone_repos records `<folder>\tcloning` as each clone begins; triple-c-wip-push
  skips a folder whose last state is `cloning` while the clone is not done (dry run
  reports "cloning": true; RepoState/TS carry it, the stop guard lists it as
  "still cloning — nothing to push" and never asks because of it).
- The idle watcher re-checks quiet() after a successful push and goes back to
  watching if someone came back during it.
- The entrypoint installs its stop trap before the idle watcher and the background
  clone start, so a TERM in between still runs aws_before_stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:07:53 -08:00
shadowdaoandClaude Opus 5.5 276f5378a4 fix(aws): give the permission check room for throttled retries and say it can be slow
Secret Scan / scan (push) Successful in 5s
Build App (Preview) / compute-version (pull_request) Successful in 4s
Build App (Preview) / create-release (pull_request) Successful in 2s
Secret Scan / scan (pull_request) Successful in 6s
Build App (Preview) / build-macos (pull_request) Successful in 5m56s
Build App (Preview) / build-linux (pull_request) Successful in 8m42s
Build App (Preview) / build-windows (pull_request) Successful in 10m38s
Build App (Preview) / prune-previews (pull_request) Successful in 3s
Build App (Preview) / test (pull_request) Successful in 22m44s
Build Container / build-container (pull_request) Successful in 25m25s
The connection-wide 30 s operation limit counts retry backoff, so a
throttled SimulatePrincipalPolicy got about six of its ten adaptive
attempts. The IAM client used by the check now has a 120 s operation
limit, and Settings tells the user a check can take a few minutes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:54:34 -08:00
shadowdaoandClaude Opus 5.5 3c1c276769 docs(aws): note the idle-stop changes are not yet re-verified live
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:37:58 -08:00
shadowdaoandClaude Opus 5.5 8a3c552958 test(aws): the live idle stop holds a terminal open and polls in the background
The idle wait now keeps an attach open (silent) and runs the auth
bridge's background exec every 2 s; the task must still stop itself and
ECS must report it as an idle stop (exit 75 -> StopKind::Idle).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:36:23 -08:00
shadowdaoandClaude Opus 5.5 ce020f79a1 fix(aws): the e2e script uses only the named profile and always cleans up
It unsets every caller AWS_* variable (keeping only where the profile's
files are, now documented), sets AWS_PROFILE explicitly, attempts the ECR
delete in the EXIT trap unconditionally (RepositoryNotFound tolerated,
bounded at 120 s), and exits 130/143 on INT/TERM (M21).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:35:50 -08:00
shadowdaoandClaude Opus 5.5 24a82bd4c8 fix(aws): warn that Bedrock profile auth can't work in an AWS workspace
The task has no ~/.aws from this machine and SSO can't refresh there;
the note points at static keys or a bearer token (m10). The auth bridge
stays enabled on AWS (its poll is now a background request).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:34:34 -08:00
shadowdaoandClaude Opus 5.5 7f113f58ac fix(aws): refuse credentials in repo URLs and HEAD as a branch
- validate_repo_url rejects https URLs with userinfo (user:pass@ or
  token@), pointing at the project's Git HTTPS token setting (m4).
- validate_branch rejects HEAD (m5).
- The FIXED_ENVIRONMENT doc comment sits on FIXED_ENVIRONMENT again (m6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:34:03 -08:00
shadowdaoandClaude Opus 5.5 2772647690 fix(aws): reconcile AWS projects at startup without waiting for Docker
- New reconcile_aws_projects command (handler, capability, TS wrapper)
  that App runs on mount regardless of Docker; reconcile_project_statuses
  now skips AWS projects. Projects reconcile concurrently, 4 at a time,
  each still bounded at 15 s.
- reconcile_task falls back to find_project_task when the stored task is
  gone or stopped, before calling the project Gone (m3).
- An AWS project with no registered tunnel gets a placeholder target id
  (http://unattached.invalid/<id>/) instead of the last run's loopback
  URL, so a reused port can't match the wrong project; the death watch
  still reports it by that id until a re-attach registers (m9).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:33:22 -08:00
shadowdaoandClaude Opus 5.5 5e8e67a0a4 fix(aws): say why a task stopped, and bound every AWS call
- classify_stop is wired into the death watch: stopped_among returns
  each task's StopKind, the tunnel registry keeps it, and ContainerExited
  carries a StopNote. Spot -> 'Spot interruption'; TaskFailedToStart ->
  Error with the reason; the entrypoint's idle exit (75, read from the
  workspace container's exit code) -> 'stopped after being idle'; a user
  stop is applied silently (P8). is_quota_error, dead, moved to tests.
- The SDK config has a TimeoutConfig (30 s operation / 10 s attempt); the
  watch's per-group poll and StopTask also have tokio bounds.
- Watcher tests cover silent sessions, unsaved work with the WIP push on
  and off, a failing push and the idle exit status; docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:31:28 -08:00
shadowdaoandClaude Opus 5.5 151b7690df fix(aws): the auth bridge's poll no longer keeps a workspace up
The auth bridge reads /proc/net every 2 s; on the agent that now goes
through ContainerTarget::exec_oneshot_background, which sends
X-Triple-C-Background: 1 so the agent does not count it as activity.
Docker behaviour is unchanged. The idle-stop hint says what idle means
now (R11) and what happens to unsaved work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:31:28 -08:00
shadowdaoandClaude Opus 5.5 95b68ed068 fix(container): idle stop ignores silent sessions and never loses work
- The agent marks requests with X-Triple-C-Background: 1 (and the
  attach/tunnel sessions they open) as not activity (R11).
- triple-c-idle-watch no longer requires zero open execs/tunnels: idle is
  last_activity older than N minutes, no claude, no scheduler job, no
  running clone, and no unsaved git work. With the WIP push on, it pushes
  as claude before signalling and stays up (backing off) when the push
  fails; with it off, dirty repos keep the workspace up.
- An idle stop exits 75 so ECS reports it distinctly from a crash; the
  TERM trap no longer pushes again after the watcher's push.
- start_clone_repos writes done from an EXIT trap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:31:19 -08:00
shadowdaoandClaude Opus 5.5 3f986368aa fix(aws): removal deletes the secret at once and words the access point honestly
Removing a project's AWS resources now deletes its secret with
ForceDeleteWithoutRecovery (R10) so the next start can create it again,
and the EFS option is renamed: it removes the access point, while the
files stay on the user's own file system until offboarding (R9).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:10:52 -08:00
shadowdaoandClaude Sonnet 5.5 55ec7e33fe docs(aws): fix policy sizes and a constant name
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 11:59:31 -08:00
shadowdaoandClaude Sonnet 5.5 595897a5fd docs(aws): phase 5 Fargate workspaces
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 11:58:42 -08:00
shadowdaoandClaude Opus 5.5 084fa6efe4 test(aws): guard the live workspace test against hangs and leaks
Review round 1 of Task 9:
- the phase-5 body runs under a 45-minute timeout and each exec under
  180 s, so a hang becomes a panic the guard catches; the body future is
  boxed (moved into the timeout it overflowed the test thread's stack,
  which aborts without any guard);
- scripts/aws-e2e-workspace.sh builds and pushes the image to a throwaway
  ECR repository, runs the test and deletes the repository in an exit
  trap; the test's guard deletes it too (TRIPLEC_E2E_ECR_REPO);
- the guard survives a poisoned lock, closes the tunnel and restores
  AWS_CONFIG_FILE;
- new live probe: a bare elasticfilesystem:TagResource on the user's own
  file system is denied; README records it and the final run (R8 removal
  end state), and the admin command uses xargs -r.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:56:40 -08:00
shadowdaoandClaude Opus 5.5 1700cc7985 fix(aws): leave task definitions to admins on removal
Ruling R8. ECS authorizes ecs:DeregisterTaskDefinition against * only,
with no tag condition (verified live), so the workspace tier's
RetireOwnTaskDefinitions could never match, and allowing it on * would
let any user deregister any revision in the account. DeleteTaskDefinitions
is resource-level but deletes only INACTIVE revisions, so it goes too.

Removing an AWS project now stops the task, schedules the secret and
(with discard) deletes the access point, and leaves the task definition
revisions registered, saying so in the log: they hold no secret values,
cost nothing, and RunOwnTasks still needs the owner tag. aws/README.md
tells admins how to deregister them. The policy statement, its checks and
owner-scoped rows, the RuntimeApi methods, the fakes, the removal UI copy
and the live test's final assertion follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:34:52 -08:00
shadowdaoandClaude Opus 5.5 e901a08ab6 test(aws): live Fargate workspace lifecycle
aws::live_tests::environment::a_fargate_workspace_round_trips (ignored):
a public environment deployed by the environment admin, a role holding
only the workspace-user files onboarded to it and driving ProjectHost::Aws
through a profile connection: start (Hybrid, 1 vCPU / 2 GB, one public
repo), exec, background clone, ~/.claude persistence across stop/start,
attach without a second RunTask, the stop guard's dry run, the SIGTERM
WIP path, idle stop at 5 minutes, bubblewrap, denials on another owner's
revision and secret, and project removal. TRIPLEC_E2E_IMAGE names the
image; a private ECR image gets a test-only pull policy on the exec role.
A guard stops tasks, retires revisions, deletes access points and the
secret before the fixture destroys the modules.

aws/README.md gains the phase 5 results; the spec's To verify items are
answered. Open: DeregisterTaskDefinition is authorized against * only,
so the workspace tier cannot retire revisions (the test's last assertion
fails until that is decided).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:30:33 -08:00
shadowdaoandClaude Opus 5.5 1eed994da7 fix(aws): tag access points at create against their file system
EFS authorizes the elasticfilesystem:TagResource that a tagged
CreateAccessPoint needs against the parent file system's ARN, not the new
access point's: a workspace user's first Hybrid start was denied
("not authorized to perform: elasticfilesystem:TagResource on the
specified resource"). Verified live with a probe role: access-point/*
is denied; file-system/* is allowed, as is * with each of
elasticfilesystem:CreateAction, aws:RequestTag and aws:TagKeys.

TagOwnAccessPointsAtCreate now names file-system/* with the same
conditions (CreateAction = CreateAccessPoint, owner request tag, the
three tag keys); its check and owner-scoped test follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:46:59 -08:00
shadowdaoandClaude Opus 5.5 074cb28848 docs(aws): correct the 0.25 vCPU Fargate memory sizes in the phase 5 plan
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:31:06 -08:00
shadowdaoandClaude Opus 5.5 08b6d341f2 fix(aws): stop guard review round 2
A failed StopTask removes the stop marker it recorded, so a later stop
pushes as configured. The unchecked guard's Stop anyway sends no choice
and records nothing, so the workspace's own shutdown push still runs as
the dialog says. The idle watcher keeps a pushed/discard marker instead
of overwriting it; the remove-resources dialog sends discard only when
there may be saved files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:30:56 -08:00
shadowdaoandClaude Opus 5.5 8441a096c8 fix(aws): stop guard review round 1
The stop guard's choice now reaches the workspace: after a successful
push (or for Stop without pushing) the app records pushed/discard in
/run/triple-c/stop-reason as root, and the entrypoint's shutdown hook
then skips its own push; a marker that can't be written is reported
and the stop goes on. The push runs before the auth bridge is stopped;
a guarded stop of a workspace that already stopped succeeds. The
Fargate table allows only 512/1024/2048 MiB with 256 CPU, pinned on
both sides by a shared fixture. Removal's missing-connection refusal
carries a stable prefix. OpenAI Compatible is allowed on AWS, with a
warning for local addresses and the app's own gateway. HostSection
locks resource actions unless Stopped, the add dialog drops the
environment on a region change, and the repository editor keeps rows
in progress across a refresh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:26:53 -08:00
shadowdaoandClaude Opus 5.5 eb7c6c7b21 feat(aws): AWS project settings and the stop guard
Add Project picks Local or an AWS connection, a region and a ready,
joined environment, with repositories instead of folders. The Config
tab's Host section edits CPU/memory (valid pairs only), architecture,
storage, ephemeral size, Spot, idle stop, WIP push and repositories, and
can remove the AWS resources or move the project back to this machine.
Docker access, VPN, port mappings and the gateway backend are disabled on
AWS with the reason; the sandbox shows Off on AWS for now. Stop on an AWS
workspace with unsaved work asks first (Push to WIP branch preselected);
a failed start shows the task's last log lines; removing an AWS project
can delete its saved files, and asks again when its connection is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:15:14 -08:00
shadowdaoandClaude Opus 5.5 51042f5d9e feat(aws): stop guard, task log and AWS resource removal commands
check_unsaved_work runs triple-c-wip-push --dry-run through the agent as
claude (checked:false with the reason when the workspace can't be asked);
stop_project_container takes wip: push runs the push first and a failure
aborts the stop with its output. aws_task_log reads the current or last
task's stream in the environment record's log group. remove_aws_resources
removes a stopped project's AWS resources and clears its runtime handles,
so it can move off AWS. add_project takes the host fields; an AWS project
takes repositories, no folders, and no runtime handles from the caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:15:14 -08:00
shadowdaoandClaude Opus 5.5 b1ac78613c fix(container): aws mode review round 1
Clone repos in the background after the agent starts (progress in
/run/triple-c-clone; an unfinished clone keeps the workspace from idling),
give the task definition stopTimeout 120 for the SIGTERM WIP push, reserve
the TRIPLEC_ env prefix on both hosts, and stop root writing through
claude-controlled links (~/.claude.json written as claude only via the exact
Hybrid link; ~/.ssh merged only through the exact Hybrid link, Docker paths
unchanged). Keep the user's ssh command and add BatchMode, cover dot-folders,
refuse undecodable or over-long file names by name, and upload LFS objects
before the --no-verify push.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:59:00 -08:00
shadowdaoandClaude Opus 5.5 51f71212d2 feat(container): aws mode — secret files, repo clone, Hybrid home, WIP push, idle stop
With TRIPLEC_HOST=aws the entrypoint decodes TRIPLEC_FILES into
/tmp/.host-ssh and /tmp/.host-ca and unsets it (and TRIPLEC_REPOS) before
the agent's environment snapshot; links ~/.claude, ~/.claude.json and ~/.ssh
into /mnt/triple-c-home for Hybrid storage; clones TRIPLEC_REPOS as claude
after git credentials and ~/.ssh are set up; runs triple-c-wip-push from the
TERM trap; and starts triple-c-idle-watch, which signals the entrypoint
itself (PID 1 is docker-init under initProcessEnabled). Docker mode is
unchanged: every new step is gated on TRIPLEC_HOST=aws.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:46:03 -08:00
shadowdaoandClaude Opus 5.5 8d45a93d63 fix(aws): a start cancels a pending re-attach; keep an unstoppable task recorded
ensure_started cancels a background re-attach instead of waiting for it,
and every register bumps the project's generation, so a late re-attach can
never replace a newer tunnel. A task StopTask refused after a failed start
keeps its ARN. A re-attach that gives up clears the pending flag only if
still current; close_if_task checks and takes under one lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:30:17 -08:00
shadowdaoandClaude Opus 5.5 37e94b1ee6 fix(aws): record a task as soon as it exists; repair a raced token; guard re-attach
A start stores the task ARN when RunTask returns (or the task is found),
reconcile finds an unrecorded live task, and a failed attach leaves the
project in Error rather than hiding a running task. The winner of a start
race puts its agent token back in the secret. A background re-attach
registers only if the project still runs that task and was not stopped
meanwhile. Removing a project whose connection is gone needs explicit
confirmation. Names come from tofu_modules; files are read with a hard cap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:25:53 -08:00
shadowdaoandClaude Opus 5.5 e05f7e3cd8 feat(aws): start, attach and stop a project on its AWS environment
ProjectHost::Aws and the tunnel registry: start looks for the project's
live task before preparing (a running task is attached to with the token
read back from its secret, never rotated), waits for a new access point,
runs, waits, tunnels and checks the agent; a task the start ran is stopped
on any later failure. Stop, remove (resources; data only on request),
reconcile (background re-attach) and the AWS death watch, with exits
confirmed through the registry. Docker-only Reset, migration, staleness
and container info refuse or skip AWS projects; an AWS project with
resources can't be moved off its host. Shutdown closes every tunnel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:15:37 -08:00
shadowdaoandClaude Opus 5.5 7fd6fa6aaa feat(aws): environment record, agent token, access point wait and resource removal
RuntimeApi (GetParameter, GetRole, GetSecretValue, DescribeAccessPoints,
task definition retirement, DeleteSecret with a 7-day window,
DeleteAccessPoint) with the decisions in tested functions over it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:15:37 -08:00
shadowdaoandClaude Opus 5.5 e43ed26e20 feat(aws): run, find, stop and watch workspace tasks
aws/tasks.rs: TaskApi (+ SdkTaskApi) with RunTask, DescribeTasks (tags),
ListTasks by family, StopTask and a GetLogEvents tail. find_or_run reuses
the project's live task only when its tags name the caller and the
project, and otherwise runs the exact family:revision with the three
ownership tags (FARGATE, or the FARGATE_SPOT capacity provider for Spot),
with a client token so SDK retries cannot start a second task.
wait_ready polls with injectable timing and fails with the stop reason
and the last 50 log lines. Quota refusals become AwsError::QuotaExceeded
and are never retried. classify_stop maps stop codes per the spec.

host/watch.rs: ExitSource::Aws polls an injected project-to-task lookup
every 15 s, per connection+region+cluster, 100 tasks per call; a group
that can't be asked is skipped. has_exited's Agent arm is unchanged (R3).
container_watch::run/spawn take their ExitSource.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:56:11 -08:00
shadowdaoandClaude Opus 5.5 30d6b26a42 fix(aws): fingerprint the built task definition; harden workspace lookups
- The fingerprint hashes a canonical rendering of the built request
  (Debug-redacted environment and HashMap log options rendered
  separately, labels excluded) instead of the plan plus a template
  constant, so a builder change can't silently keep old revisions.
- Page ListTaskDefinitions so a longer family can't crowd out the
  caller's revisions.
- Project ids that aren't [A-Za-z0-9-] get a hash suffix in names, so
  ids that sanitise alike can't share a secret.
- Name a secret scheduled for deletion; only mount an available file
  system; drop the unused FileSystemRef.arn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:46:32 -08:00
shadowdaoandClaude Opus 5.5 64ff3baab2 feat(aws): per-project secret, access point and task definition
aws::workspace prepares an AWS project before its task runs: one
Secrets Manager secret holding every project-shaped value (P1; files
as one TRIPLEC_FILES key per R1), the Hybrid EFS access point, and a
task definition whose environment is only TRIPLEC_HOST and COLORTERM
(R2), with every payload key as a valueFrom secret reference.

A revision is reused only when it carries the caller's owner tag, this
project's tag and the expected fingerprint label (other users can
register into any triple-c-* family), always by its exact
family:revision ARN. Payload keys and secrets entries are sorted so
unchanged settings never register a new revision. Names (family,
secret, access point root, log stream prefix, file system token) live
in tofu_modules and use owner_key.

Adds aws-sdk-efs and aws-sdk-cloudwatchlogs (released with the pinned
aws-sdk-ecs; lock gains only their own entries).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:39:58 -08:00
shadowdaoandClaude Opus 5.5 7320aaa5d4 fix(aws): document register-into-family residual risk, correct sizes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:27:48 -08:00
shadowdaoandClaude Opus 5.5 d1883310dd feat(aws): workspace users register their own task definitions
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:26:08 -08:00
shadowdaoandClaude Sonnet 5.5 01e7b2752a refactor(docker): build container env in one host-neutral function
workspace_env() builds every project-shaped KEY=VALUE pair (backends, CA vars,
MANAGED_AUTH_KEYS blanking, custom env, instructions, Claude Code settings);
create_container keeps COLORTERM, HOST_UID/GID and the host lookups (CA
resolution, keychain token) and formats the pairs. Golden vectors recorded from
the pre-refactor block pin the order and values.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 08:22:13 -08:00
shadowdaoandClaude Opus 5.5 0fc99738bb feat(aws): project model for the AWS host
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:16:56 -08:00
shadowdaoandClaude Opus 5.5 a674f6f196 docs(aws): phase 5 plan — Fargate workspaces
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 07:47:15 -08:00
jknapp 2d094b4748 Merge pull request 'AWS host phase 4: environments and onboarding with OpenTofu' (#81) from feat/aws-environments into main
Secret Scan / scan (push) Successful in 6s
2026-11-15 15:39:32 +00:00
180 changed files with 39386 additions and 1261 deletions

No files matched your search

+210 -3
View File
@@ -284,7 +284,7 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
users have migrated.
- **`host/`** — Where a project's container runs, and the only way into it. `ContainerTarget`
(`Docker`, and `Agent` — reached through the in-container `triple-c-agent` by `agent.rs`, per
`docs/superpowers/specs/2026-11-13-triple-c-agent-protocol.md`; AWS planned —
`docs/superpowers/specs/2026-11-13-triple-c-agent-protocol.md`; AWS host, designed in
`docs/superpowers/specs/2026-11-13-aws-ecs-host-design.md`)
wraps execs, file transfer and `is_running`; `sessions.rs` holds `ExecSessionManager`.
`ssm.rs` is the AWS transport to an `Agent` target: one SSM port-forward per task, run through
@@ -292,6 +292,25 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
when it drops, so an `AgentEndpoint` stays valid. The `StartSession` token goes to the plugin
through its environment, never argv, and the plugin's environment is stripped of every
inherited `AWS_*` variable before the session's own credentials are added.
`aws_host.rs` is `ProjectHost::Aws` (an ECS task per project, Fargate or, on the user's own instance, EC2; a running task is
attached to with the token read back from its secret, never rotated; a task a failed start ran
is stopped again) and `tunnels.rs` the registry of open tunnels by project — an AWS project's
`ContainerTarget::for_project` is its tunnel's endpoint, its stored `container_id` is that
endpoint's URL, and the AWS death watch confirms exits through the registry. Shutdown closes
every tunnel (`for_project` is `None` while none is open; `for_project_waiting` covers the
background re-attach after an app restart). AWS SDK calls stay in `aws/` (`runtime.rs`,
`tasks.rs`, `workspace.rs`).
`port_forwards.rs` is an AWS project's port mappings: each TCP mapping is a **loopback-only**
auth-bridge `PortForward::bind_mapping` over socat-over-attach. Two deliberate differences from
Docker: it reaches the workspace's **loopback** (Docker publishes to eth0), so the agent port,
the browser-view ranges and — with `gateway_sidecar` on — the sidecar's 4000 are refused; and it
has **no gate** (no first-byte wait, no cross-site check, unlike the bridge). Validated on save
(rows the save adds/edits; everything when newly on AWS or the sidecar is switched); a stored row
that would be refused is skipped with a warning at connect. Opens are serialised per project and
re-read the stored mappings under that lock; unchanged forwards are kept with their connections.
Opened after every connect and on a save while connected; closed by every `tunnels::close*`
path. A failing mapping is a `workspace-warning` (`topic: "ports"`). An idle listener makes no
agent request; a connection carrying traffic is activity (keeps the workspace from idling).
`agent.rs` reuses `docker/exec.rs`'s `OneshotOutput`, host-file reader and uid probe so the two
variants cannot drift; an `Agent` target's token never appears in `Debug` or error text.
**Container-interior code must go through `ContainerTarget`, never `docker::exec`, bollard or
@@ -312,7 +331,7 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
`identity.rs` is `GetCallerIdentity` and the principal to simulate (an assumed role's ARN is
recovered with `iam:GetRole`, because SSO roles have an `/aws-reserved/…` path the session ARN
lacks); `error.rs` classifies SDK errors (SSO expired, access denied naming the action, …);
`policies.rs` embeds `aws/policies/*.json` (the tier policies — the admin tier spans several
`policies.rs` embeds `aws/policies/*.json` (the tier policies — both tiers span several
files, `policy_files` lists them — and the permissions boundary) and the table of checks the
simulator is asked; `permissions.rs` runs it
(`SimulatePrincipalPolicy`, adaptive retries — it throttles — or read-only probes when the
@@ -330,6 +349,98 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
`aws/README.md` ("Live verification"). Its `environment` tests apply the real modules as a role
holding only the admin files (`cargo build` first: the runner they start is the built app
binary), in a throwaway state bucket.
**A Fargate workspace's resources** (`workspace.rs`, `tasks.rs`, `runtime.rs`). **No project
data in a task definition (P1):** every workspace user has `ecs:Describe*` on `*`, so
`environment`, tags, labels and `overrides` are public to the account. The task definition carries
`TRIPLEC_HOST=aws`, `COLORTERM`, the three ownership tags, a fingerprint docker label (a hash,
`FINGERPRINT_LABEL`) and `valueFrom` references into the project's **one secret**
(`triple-c/<env>/<owner_key>/<project-id>`, JSON, 64 KB cap: files, repos, instructions, settings,
custom env, the agent token). The token rotates each start; a running task is attached to by reading
it back (the task role has no Secrets Manager access). **Anyone may register a revision into any
`triple-c-*` family** (IAM can't scope it), so a revision or task is reused only when it carries the
caller's own owner tag + this project's tag (+ the fingerprint), and `RunTask` always names the exact
`family:revision` ARN, never "latest". Names come only from `tofu_modules.rs`, built from `owner_key`
(`aws:userid` contains `:`). Workspace users can't deregister or delete task definitions
(`DeregisterTaskDefinition` is `*`-only in IAM): removal leaves revisions ACTIVE for an admin
(`aws/README.md`). Stop: the stop guard (`commands/stop_guard.rs`) dry-runs `triple-c-wip-push`
through the agent, closes the project's terminals and auth bridge (so nothing is written after the
push's snapshot), may push first (under an in-container `timeout`, since the agent does not kill an
exec the client gave up on), then writes `pushed` or `discard` to `/run/triple-c/stop-reason`
(root-owned; the entrypoint's TERM hook reads it, and the idle watcher writes `idle` or `lifetime`) so the hook
doesn't push twice; the marker is cleared if StopTask fails. **Claude Code's sandbox is forced off
on every AWS project, Fargate and EC2** in the generated settings (`sandbox_allowed` is false; the
Runtime toggle is locked off, `sandboxEffective` in `awsWorkspace.ts`): bubblewrap fails on Fargate
and under Docker's default seccomp profile on EC2 too ("No permissions to create new namespace",
verified live), so a `sandbox_allowed` that said yes would only break every command.
**LLM gateway sidecar** (`gateway_sidecar`, Q7): a second, `essential: false` container `gateway`
(`docker::gateway::registry_image_ref`, digest-pinned, amd64+arm64; `memoryReservation` 512; no
`environment`, no `dependsOn`) whose `secrets` are exactly `TRIPLEC_GATEWAY_{CONFIG,API_KEY,MASTER_KEY}`
— the workspace container gets every key but those. The config is `sidecar_config` (Docker's
`render_config` with `os.environ/…` key references, so it holds no key); `/bin/sh -c` writes it to a
0600 `/tmp/litellm.yaml` and `exec`s LiteLLM on **127.0.0.1:4000** only (the image's own entrypoint
can't write a file). The master key is **random per task start** (the Docker gateway's never leaves
the machine); the payload overrides `ANTHROPIC_BASE_URL`/`ANTHROPIC_AUTH_TOKEN` with the sidecar's,
and under copy-at-connect that token stays in the secret too (R1; terminals don't override it).
Log streams: `<prefix>/gateway/<task-id>` (the tail reads `workspace` only). Needs the
OpenAI-compatible backend, a gateway model and ≥ 1 vCPU / 2 GiB; off = the request is unchanged.
**Cost estimate** (`pricing.rs`, Q5): `pricing:GetProducts` (us-east-1, `AmazonECS`, filters in
`FILTERS`) → per-region x86/ARM rates cached at `<data_dir>/triple-c/aws-pricing/<region>.json` for
24 h; `aws_cost_estimate(connection_id, region, config)` never errors — denied/offline/slow (8 s) is
`None` ("Estimate unavailable"), a failed region isn't re-asked for 5 min, a stale cache still answers.
EC2 projects (E10) pass `instance_type` (the user's, from `aws_ec2_capacity`): the `AmazonEC2`
on-demand Linux/shared-tenancy hourly price (`EC2_FILTERS`), cached as `<region>--ec2-<type>.json`
with the same rules — the instance's price, not the reservations — plus the 50 GiB gp3 root volume
(`GP3_FILTERS`, `<region>--ebs-gp3.json`, best effort: without it the note says it's missing;
`EC2_ROOT_VOLUME_GIB` is pinned to `ec2.tf`). A public IPv4 address ($0.005/h) is only in the note.
**ECS on EC2 (phase 7; `aws/terraform/user-onboard/ec2.tf`, `workspace.rs` `ec2_plan`, `aws_host.rs`).**
Opt-in per onboarding (`ec2_instance_type`); a project's `launch_type` is `Fargate | Ec2`. Why the
rules are what they are:
- **Per-user cluster `triple-c-<env>--u-<tag>`**, named by the principal tag `triple-c-user`
(`user_ec2_stem` in `tofu_modules.rs`, the only place). One capacity provider per cluster,
because `PutClusterCapacityProviders` replaces the list and two modules would undo each other on
a shared cluster. Environment names and tags never contain `--`, so `triple-c-*--u-*` matches
exactly the per-user clusters. **Two Denies in `workspace-user-tasks.json`** (`RunTask`/`StartTask`
on a `--u-*` cluster other than the caller's own, and on any when the caller has **no**
`triple-c-user` principal tag) are the whole isolation between EC2 users: don't loosen them, and
keep the missing-tag Deny (a variable that resolves to nothing must not fall through). A denial
maps to `EC2_TAG_MISMATCH` ("different triple-c-user value").
- **The instance role is scoped by its own cluster tag.** `triple-c-<env>-<owner_key>-instance`
(owner key, not user tag: role names are account-global) has only the ECS agent's calls, and the
**boundary** allows them on `cluster/${aws:PrincipalTag/triple-c:cluster}` (must match `--u-*`).
A Docker-access project is root on its instance and holds that role, so the tag is what stops it
registering the instance into someone else's cluster. No SSM core and **no log rights** on the
instance (awslogs writes with the execution role: `ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE`).
The task and execution roles `PutLogEvents` only into `log-stream:<owner_key>/*` of the shared
log group, so `log_stream_prefix` must keep starting with the owner key. The boundary
is versioned (2): an account admin re-applies it once before anyone onboards with EC2.
- **Launch templates and groups are pinned to a numeric template version, and the admin policy
denies `$Latest`/`$Default`**: the admin's `iam:PassRole` and `ec2:RunInstances` checks look at the
template when it is created, and a version edited afterwards would carry any instance type or
role (a PassRole bypass). The type list (`ec2_instance_spec` in `models/project.rs`) lives in the
model table, the HCL variable and the policy (`ec2:InstanceType` in the compute file,
`autoscaling:InstanceTypes` twice in the scaling file); change all together. A template-only group
create sends no `autoscaling:InstanceTypes`, so don't add a `Null` condition on it (live-verified).
- **Bridge networking, not awsvpc (R2):** the instance is the user's alone, awsvpc tasks on EC2 get
no public IP, and bridge keeps IMDS out of reach with hop limit 1 (the agent on the host network
still reaches it). The ECS agent's introspection port (172.17.0.1:51678) stays reachable;
accepted. The gateway sidecar is refused on EC2 (no shared loopback).
- **Cold-start rerun + orphan rule.** ECS turns ECS Exec **off** for a task that has to wait for an
instance (the agent tunnel needs it), so the first RunTask only brings the instance up; once
placed, the app stops it, waits for STOPPED and runs again via `find_or_run` (so a concurrent
start never doubles a task: `START_RACE`), requiring exec on (`EXEC_OFF`). A found exec-off task
that is still waiting for an instance is another start's (`INSTANCE_RACE`) while younger than
`ec2_wait + ORPHAN_MARGIN`; older is an orphan, stopped (`REPLACED_REASON`) and replaced. Keep
both launches going through `find_or_run`.
- **Docker access = the instance's own socket**, mounted at `/var/run/docker.sock` (root on the
user's own instance, acceptable only because nobody else's task can land there); VPN =
`NET_ADMIN` + `/dev/net/tun` + `src_valid_mark`. A Fargate project can't keep either: switching
to Fargate clears both flags (`launchTypePatch`, R1).
- **Collision check before an apply** (`ec2_collision_refusal`, `prepare_onboarding`): the module
would adopt another user's cluster of the same name, so the app refuses a name owned by someone
else (resources' `triple-c:owner` tag, the user record `/triple-c/users/<env>/<owner_key>` as
fallback). Admin bundles carry the same check in `README.txt`.
- Scale to zero (min 0): the first start after ~17 min idle is cold (~3.5-4 min), warm ~20-30 s.
Re-applying onboarding without EC2 drains and deletes the group even with an instance running.
**Environments are OpenTofu modules** (`aws/terraform/{environment,user-onboard,permissions-boundary}`;
spec D2–D10). `tofu_modules.rs` embeds them and is the **only** place the shared names are built
(environment, `/triple-c/environments/<env>` record, owner key — `sha256("<region>:<aws:userid>")`,
@@ -361,6 +472,102 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
- **`Dockerfile`** — Ubuntu 24.04 base with Claude Code, Node.js 22, Python 3.12, Rust, Docker CLI, git, gh, AWS CLI v2, ripgrep, pnpm, uv, ruff pre-installed, plus the shared
libraries a browser links against (see below) and the VPN tooling the `vpn_support_enabled`
toggle grants capability for (`iproute2`, `wireguard-tools`, `iptables`)
- **`TRIPLEC_HOST=aws` mode** (`entrypoint.sh`, the `aws_*` blocks; inert without it). There are no
bind mounts: `TRIPLEC_FILES` (JSON of base64 files) is decoded into `/tmp/.host-ssh` / `/tmp/.host-ca`
so the SSH merge and CA install run unchanged, and `TRIPLEC_REPOS` is kept in an unexported shell
variable; **both are unset before the agent-token block snapshots the environment** (that snapshot is
every terminal's env and the files hold private keys). Repos clone in the background after the agent
starts (terminals are usable before they arrive). Hybrid storage mounts EFS at `/mnt/triple-c-home`
and symlinks `~/.claude`, `~/.claude.json`, `~/.ssh` into it (mounting over `/home/claude` would hide
the image's tooling); root never follows those links outside the mount (`aws_hybrid_ssh_dir`). EFS storage
is Hybrid plus a second access point (root `/triple-c-workspaces/<owner_key>/<project>`, a sibling of
the home roots) mounted straight at `/workspace`; the entrypoint links home state the same way
(`aws_storage_links_home`) and never creates or chowns `/workspace`. Because `/workspace` then
outlives the task, `clone_repos` clones into `/workspace/.triple-c-clone/<folder>` and renames
into place only on success; it never deletes anything at a final path, empties scratch at each
start, and `triple-c-wip-push` skips scratch.
Switching EFS → Hybrid/Ephemeral keeps `workspace_access_point_id` (store-owned) until "Remove AWS
resources" with discard deletes both access points. On
TERM the hook pushes unsaved work to the run's `triple-c/wip/<run-id>-<folder>` branch via a
temporary index (the user's index is untouched; needs the task definition's `stopTimeout` raised,
120 s) unless the stop marker says the app handled it.
**One WIP branch per task run and folder:** `aws_prepare_run` (right after the agent-token block)
writes `/run/triple-c/run-id` (`<UTC stamp>-<task id 8 | local>`, task id from ECS task metadata)
and `start-epoch`; `/run/triple-c` is root 0711 (the stop guard keeps that mode — at 0700 `claude`
could not read the run id). The folder is sanitised to `[A-Za-z0-9._-]` (a name that changed gets
`-<8 hex of sha256(raw name)>`, so `a b` and `a_b` never share a ref) and the ref checked with
`git check-ref-format`; later pushes force (`+`) only that run's own branch, and a tree equal to
`/run/triple-c/wip-last/<folder>` (claude-owned dir, written after a successful push) is skipped.
No usable run id (Docker) keeps a per-push `<stamp>-<folder>`, never forced.
**Setup script** (`TRIPLEC_SETUP_SCRIPT`, unset before the snapshot like `TRIPLEC_FILES`): written
to `/run/triple-c-setup/script` (root 0755 dir; root opens `log` and hands the descriptor over, so
claude writes nothing there), run as `claude`, `bash -e`, stdin `/dev/null`,
`timeout -k 10 1800`, chained after the background clone's `done` in the same background
subshell; `status` is `running` from the start until `ok` / `failed <code>` (a chain that dies
early writes `failed`, so the idle stop is never blocked). `triple-c-workspace-status` (python,
no secrets) prints `{clone:{done,repos},setup:{state,code,tail}}`. Edited in HostSection's
"Setup script" textarea (saved on blur, blank → `null`; the 32 KiB UTF-8 limit is checked in TS
with Rust's own message, `setupScriptProblem`).
**The start does not wait for them** (it ends after "Connecting…": Running, lock released);
`aws_host::spawn_follow` follows in a background task (`host/aws_host/status.rs`: background exec
every 2 s, at most 35 min, holds no project lock; ended by `tunnels::cancel_follow` on Stop/Remove
and by any tunnel close or app exit). Its only output is a `workspace-warning` event (warning
toast) for a failure it saw happen or one on a task this start ran; progress shows in Project
Home's Overview card, which polls `aws_workspace_status` every 5 s while Running and visible.
At load, a Starting/Stopping AWS project becomes Error (not Stopped — a crash mid-start may have
left a billed task), and `reconcile_aws_projects` also checks a Stopped one still holding a task ARN.
**Lifetime cap** (`TRIPLEC_MAX_LIFETIME_MINUTES`, advisory — sudo can kill it): the idle watcher
(started when idle stop *or* a non-zero cap is set) pushes best effort, records `lifetime` and
sends TERM; the hook exits 76 (`aws::tasks::LIFETIME_EXIT_CODE` → `StopKind::LifetimeCap` →
`lifetime` toast). HostSection shows a non-zero cap ("stops workspaces after N h (advisory)") from
`aws_environment_max_hours` (reads the environment record; `None` when it can't, never an error).
The idle watcher runs as root under a supervisor and signals the
entrypoint's own PID (not PID 1, which is docker-init), so idle and ECS stops share one trap.
Idle = the agent's `last_activity_unix` older than N minutes (open-but-silent attaches/tunnels
don't count; requests with `X-Triple-C-Background: 1` — the auth bridge's and the file viewer's polls — never do),
no `claude`, no scheduler job, no running clone or setup script, and no unsaved git work: with the WIP push on the
watcher pushes *before* signalling and stays up if that fails; with it off, dirty repos keep the
workspace up; activity during the watcher's push sends it back to watching. A repo the background
clone is still writing (`/run/triple-c-clone/repos` last says `cloning`, no `done`) is skipped by
`triple-c-wip-push` (dry run: `"cloning": true`). The stop trap is set before any background work
starts. An idle stop exits 75 (`aws::tasks::IDLE_EXIT_CODE`) so the death watch can tell it
from a crash (`classify_stop` → the `ContainerExited` toast).
**Copy-at-connect** (`secrets_delivery: copy_at_connect`, Q2): the secret keeps settings, repos and
the agent token but no `TRIPLEC_FILES` and no credential pairs (`SECRET_ENV_KEYS` + every custom
variable; `TRIPLEC_CREDENTIALS=connect` instead — the gateway sidecar's `TRIPLEC_GATEWAY_*` keys
stay, R1). After every connect (start, attach, background re-attach) `aws_host::connect`
uploads the files and `connect.env` (`KEY=<base64>` lines) as root into this connect's own
`/dev/shm/triple-c/connect/<nonce>/` (root 0700 tmpfs; `--prepare` prints the nonce, so two
connects at once never mix) and runs `triple-c-apply-connect <nonce>`: CA install as root, SSH merge and git
credential helper **as `claude`** (root never walks a live `~/.ssh`), `/run/triple-c/connected`,
then a background re-clone of `failed` repos only (GIT_TOKEN in that clone's env only). The
first clone waits up to 5 min for `connected` (`aws_wait_connect`) — when there are repos **or a
setup script** (the script runs after the clone and needs the CA, SSH and Git credentials). The
CA files are swapped in (`connect-ca.new` → `connect-ca`, old removed last), so a failed move
keeps the CA installed before. Terminals the app opens add
the pairs to their own `ExecSpec.env` (`aws_host::terminal_env`, pairs only — no file read);
nothing reaches the agent's snapshot, so cron/scheduled jobs and the setup script never get them.
A failed push is a `workspace-warning`. Hybrid/EFS do **not** link `~/.ssh` to EFS under
copy-at-connect (R6: keys vanish with the task). When a copy-at-connect start finds the secret
already there (CreateSecret's ResourceExists; a new project's secret is never read first) and it
`holds_credentials`, it is force-deleted and created again (R7: `PutSecretValue` would keep the
old values as AWSPREVIOUS); `TRIPLE_C_CUSTOM_ENV` is left out too. A replace whose re-create
times out (AWS still finishing the forced delete) leaves no secret; every secret write retries
while the name is pending deletion (≤ 30 s), then says to start again in a minute.
**Copy-at-connect limits** (also listed in HostSection's notes): there is no corporate CA until
the first connect (Claude Code's self-update may fail behind a TLS-inspecting proxy until then);
SSH keys, a Git token or variables removed from the project stay in the running task until it
stops (a connect only adds/overwrites them; a removed CA does go at the next connect); the setup script is not re-run
after a later connect's re-clone of a failed repo.
The re-clone closes the apply lock's fd (a later connect never waits for it); `--prepare` takes it.
The functions both scripts use live in `container/entrypoint-lib.sh`
(`/usr/local/lib/triple-c/`), sourced by the entrypoint first; it only defines things.
**The Docker-socket group step** (`# >>> docker_socket`) runs in aws mode as well as Docker mode: an
EC2 project with Docker access mounts the instance's `/var/run/docker.sock`, and the step adds
`claude` to a group with the socket's gid (`docker-host` if none exists). Fargate never has the
socket, so it is inert there.
**The `TRIPLEC_` env prefix is reserved** on both hosts (`RESERVED_ENV_PREFIXES`; `workspace.rs`
drops custom variables in it): the entrypoint's settings live there.
- **Browser runtime libraries are baked in; browser *binaries* are not.** A layer runs
`npx --yes playwright@latest install-deps chromium` as root, so Playwright names its own
dependencies and the list cannot rot against Ubuntu 24.04's `t64` renames or a new Chromium
@@ -380,7 +587,7 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
approach converges on the worst state: a 400 MB browser present with its libraries gone.
- The layer sits immediately after Node (npx is its only prerequisite) and well above the shim
`COPY`s, so editing a shim does not re-run a multi-hundred-megabyte apt install.
- **`entrypoint.sh`** — UID/GID remapping to match host user, SSH key setup (`sync_host_ssh` *merges* the host dir into `~/.ssh`; never wipe it — container-made keys live there on the home volume), git config, docker socket permissions, Claude Code settings.json injection, then stays resident **as root** waiting on a root-owned
- **`entrypoint.sh`** (+ `entrypoint-lib.sh`, the CA/SSH/git-credential/clone functions it sources) — UID/GID remapping to match host user, SSH key setup (`sync_host_ssh` *merges* the host dir into `~/.ssh`; never wipe it — container-made keys live there on the home volume), git config, docker socket permissions, Claude Code settings.json injection, then stays resident **as root** waiting on a root-owned
`triple-c-keepalive` sleep (respawned if killed; SIGTERM trap so `docker stop` is immediate).
**Never hand the container's lifetime to a process the `claude` user owns** — it used to end in
`exec su … claude -c "exec sleep infinity"`, and any `pkill sleep` / `kill -9 -1` a Claude
+104
View File
@@ -367,6 +367,56 @@ dependencies = [
"uuid",
]
[[package]]
name = "aws-sdk-autoscaling"
version = "1.144.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08cb197f98dad7b1a3d4e7fcc94fbc13a66c2ea5465ba57fdb5dd00f865d6e8"
dependencies = [
"arc-swap",
"aws-credential-types",
"aws-runtime",
"aws-smithy-async",
"aws-smithy-http",
"aws-smithy-json",
"aws-smithy-observability",
"aws-smithy-query",
"aws-smithy-runtime",
"aws-smithy-runtime-api",
"aws-smithy-schema",
"aws-smithy-types",
"aws-smithy-xml",
"aws-types",
"fastrand",
"http",
"regex-lite",
"tracing",
]
[[package]]
name = "aws-sdk-cloudwatchlogs"
version = "1.165.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "581cf82c1682f1251b910a32af5b19f9f5faed98c9117825391c1fcdfde21855"
dependencies = [
"arc-swap",
"aws-credential-types",
"aws-runtime",
"aws-smithy-async",
"aws-smithy-eventstream",
"aws-smithy-http",
"aws-smithy-json",
"aws-smithy-observability",
"aws-smithy-runtime",
"aws-smithy-runtime-api",
"aws-smithy-schema",
"aws-smithy-types",
"aws-types",
"fastrand",
"regex-lite",
"tracing",
]
[[package]]
name = "aws-sdk-ecs"
version = "1.159.0"
@@ -390,6 +440,31 @@ dependencies = [
"tracing",
]
[[package]]
name = "aws-sdk-efs"
version = "1.125.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa82ea1472278e955c2f59c534a23681f630e6f215e29263eb10e25b0b8b281a"
dependencies = [
"arc-swap",
"aws-credential-types",
"aws-runtime",
"aws-smithy-async",
"aws-smithy-http",
"aws-smithy-json",
"aws-smithy-observability",
"aws-smithy-runtime",
"aws-smithy-runtime-api",
"aws-smithy-schema",
"aws-smithy-types",
"aws-types",
"bytes",
"fastrand",
"http",
"regex-lite",
"tracing",
]
[[package]]
name = "aws-sdk-iam"
version = "1.136.0"
@@ -439,6 +514,31 @@ dependencies = [
"tracing",
]
[[package]]
name = "aws-sdk-pricing"
version = "1.125.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bb09af014cbcee1ad1753d54d70c4afd7565dfd7ca4d84d8b702292462fe80f"
dependencies = [
"arc-swap",
"aws-credential-types",
"aws-runtime",
"aws-smithy-async",
"aws-smithy-http",
"aws-smithy-json",
"aws-smithy-observability",
"aws-smithy-runtime",
"aws-smithy-runtime-api",
"aws-smithy-schema",
"aws-smithy-types",
"aws-types",
"bytes",
"fastrand",
"http",
"regex-lite",
"tracing",
]
[[package]]
name = "aws-sdk-s3"
version = "1.159.0"
@@ -7392,9 +7492,13 @@ dependencies = [
"aws-config",
"aws-credential-types",
"aws-runtime",
"aws-sdk-autoscaling",
"aws-sdk-cloudwatchlogs",
"aws-sdk-ecs",
"aws-sdk-efs",
"aws-sdk-iam",
"aws-sdk-kms",
"aws-sdk-pricing",
"aws-sdk-s3",
"aws-sdk-secretsmanager",
"aws-sdk-ssm",
+7
View File
@@ -68,6 +68,10 @@ aws-sdk-ecs = { version = "1.159.0", default-features = false, features = ["defa
aws-sdk-sts = { version = "1.126.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
aws-sdk-iam = { version = "1.136.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
aws-sdk-secretsmanager = { version = "1.128.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
aws-sdk-efs = { version = "1.125.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
aws-sdk-cloudwatchlogs = { version = "1.165.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
# aws/pricing.rs: the Fargate rates behind the hourly cost estimate (Q5).
aws-sdk-pricing = { version = "1.125.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
# Named by src/aws/connection.rs: the credentials-provider trait (its profile
# wrapper) and the profile-file set (aws-config's alias for it is deprecated).
# Both are the versions aws-config already resolves.
@@ -76,6 +80,9 @@ aws-runtime = { version = "1.10.4", default-features = false }
# The default sleep and time source for the hand-built SdkConfig (no env/profile
# loading); the version and feature already in the tree.
aws-smithy-async = { version = "1.3.1", default-features = false, features = ["rt-tokio"] }
# commands/environment_commands.rs: the pre-apply check that a user's EC2 names
# (Auto Scaling group) are not another onboarding's (phase 7, R3).
aws-sdk-autoscaling = { version = "1.144.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
aws-sdk-s3 = { version = "1.159.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
# aws/tofu.rs: `setsid` puts the detached OpenTofu runner in its own session,
+10
View File
@@ -18,8 +18,16 @@
"allow-update-project",
"allow-start-project-container",
"allow-stop-project-container",
"allow-check-unsaved-work",
"allow-aws-task-log",
"allow-aws-workspace-status",
"allow-aws-environment-max-hours",
"allow-aws-ec2-capacity",
"allow-ec2-fit-problem",
"allow-remove-aws-resources",
"allow-rebuild-project-container",
"allow-reconcile-project-statuses",
"allow-reconcile-aws-projects",
"allow-list-notes",
"allow-save-note",
"allow-delete-note",
@@ -65,6 +73,7 @@
"allow-aws-connection-sso-login",
"allow-check-aws-permissions",
"allow-aws-policy-document",
"allow-aws-cost-estimate",
"allow-detect-aws-environments",
"allow-deploy-aws-environment",
"allow-update-aws-environment",
@@ -75,6 +84,7 @@
"allow-cancel-aws-deployment",
"allow-push-aws-saved-state",
"allow-onboard-aws-user",
"allow-list-ec2-instance-types",
"allow-deploy-aws-permissions-boundary",
"allow-save-aws-admin-bundle",
"allow-detect-host-timezone",
+1 -1
View File
@@ -1 +1 @@
{"__app-acl__":{"default_permission":null,"permissions":{"allow-acquire-claude-token":{"identifier":"allow-acquire-claude-token","description":"Enables the acquire_claude_token command without any pre-configured scope.","commands":{"allow":["acquire_claude_token"],"deny":[]}},"allow-add-aws-keys-connection":{"identifier":"allow-add-aws-keys-connection","description":"Enables the add_aws_keys_connection command without any pre-configured scope.","commands":{"allow":["add_aws_keys_connection"],"deny":[]}},"allow-add-aws-profile-connection":{"identifier":"allow-add-aws-profile-connection","description":"Enables the add_aws_profile_connection command without any pre-configured scope.","commands":{"allow":["add_aws_profile_connection"],"deny":[]}},"allow-add-marketplace":{"identifier":"allow-add-marketplace","description":"Enables the add_marketplace command without any pre-configured scope.","commands":{"allow":["add_marketplace"],"deny":[]}},"allow-add-marketplace-gh-host-account":{"identifier":"allow-add-marketplace-gh-host-account","description":"Enables the add_marketplace_gh_host_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_gh_host_account"],"deny":[]}},"allow-add-marketplace-token-account":{"identifier":"allow-add-marketplace-token-account","description":"Enables the add_marketplace_token_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_token_account"],"deny":[]}},"allow-add-project":{"identifier":"allow-add-project","description":"Enables the add_project command without any pre-configured scope.","commands":{"allow":["add_project"],"deny":[]}},"allow-add-scheduled-task":{"identifier":"allow-add-scheduled-task","description":"Enables the add_scheduled_task command without any pre-configured scope.","commands":{"allow":["add_scheduled_task"],"deny":[]}},"allow-apply-marketplace-now":{"identifier":"allow-apply-marketplace-now","description":"Enables the apply_marketplace_now command without any pre-configured scope.","commands":{"allow":["apply_marketplace_now"],"deny":[]}},"allow-apply-settings-import":{"identifier":"allow-apply-settings-import","description":"Enables the apply_settings_import command without any pre-configured scope.","commands":{"allow":["apply_settings_import"],"deny":[]}},"allow-aws-connection-identity":{"identifier":"allow-aws-connection-identity","description":"Enables the aws_connection_identity command without any pre-configured scope.","commands":{"allow":["aws_connection_identity"],"deny":[]}},"allow-aws-connection-sso-login":{"identifier":"allow-aws-connection-sso-login","description":"Enables the aws_connection_sso_login command without any pre-configured scope.","commands":{"allow":["aws_connection_sso_login"],"deny":[]}},"allow-aws-policy-document":{"identifier":"allow-aws-policy-document","description":"Enables the aws_policy_document command without any pre-configured scope.","commands":{"allow":["aws_policy_document"],"deny":[]}},"allow-aws-sso-refresh":{"identifier":"allow-aws-sso-refresh","description":"Enables the aws_sso_refresh command without any pre-configured scope.","commands":{"allow":["aws_sso_refresh"],"deny":[]}},"allow-build-gateway-image":{"identifier":"allow-build-gateway-image","description":"Enables the build_gateway_image command without any pre-configured scope.","commands":{"allow":["build_gateway_image"],"deny":[]}},"allow-build-image":{"identifier":"allow-build-image","description":"Enables the build_image command without any pre-configured scope.","commands":{"allow":["build_image"],"deny":[]}},"allow-build-stt-image":{"identifier":"allow-build-stt-image","description":"Enables the build_stt_image command without any pre-configured scope.","commands":{"allow":["build_stt_image"],"deny":[]}},"allow-cancel-aws-deployment":{"identifier":"allow-cancel-aws-deployment","description":"Enables the cancel_aws_deployment command without any pre-configured scope.","commands":{"allow":["cancel_aws_deployment"],"deny":[]}},"allow-cancel-claude-token":{"identifier":"allow-cancel-claude-token","description":"Enables the cancel_claude_token command without any pre-configured scope.","commands":{"allow":["cancel_claude_token"],"deny":[]}},"allow-cancel-marketplace-gh-login":{"identifier":"allow-cancel-marketplace-gh-login","description":"Enables the cancel_marketplace_gh_login command without any pre-configured scope.","commands":{"allow":["cancel_marketplace_gh_login"],"deny":[]}},"allow-check-aws-permissions":{"identifier":"allow-check-aws-permissions","description":"Enables the check_aws_permissions command without any pre-configured scope.","commands":{"allow":["check_aws_permissions"],"deny":[]}},"allow-check-browser-view-support":{"identifier":"allow-check-browser-view-support","description":"Enables the check_browser_view_support command without any pre-configured scope.","commands":{"allow":["check_browser_view_support"],"deny":[]}},"allow-check-docker":{"identifier":"allow-Line truncated
{"__app-acl__":{"default_permission":null,"permissions":{"allow-acquire-claude-token":{"identifier":"allow-acquire-claude-token","description":"Enables the acquire_claude_token command without any pre-configured scope.","commands":{"allow":["acquire_claude_token"],"deny":[]}},"allow-add-aws-keys-connection":{"identifier":"allow-add-aws-keys-connection","description":"Enables the add_aws_keys_connection command without any pre-configured scope.","commands":{"allow":["add_aws_keys_connection"],"deny":[]}},"allow-add-aws-profile-connection":{"identifier":"allow-add-aws-profile-connection","description":"Enables the add_aws_profile_connection command without any pre-configured scope.","commands":{"allow":["add_aws_profile_connection"],"deny":[]}},"allow-add-marketplace":{"identifier":"allow-add-marketplace","description":"Enables the add_marketplace command without any pre-configured scope.","commands":{"allow":["add_marketplace"],"deny":[]}},"allow-add-marketplace-gh-host-account":{"identifier":"allow-add-marketplace-gh-host-account","description":"Enables the add_marketplace_gh_host_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_gh_host_account"],"deny":[]}},"allow-add-marketplace-token-account":{"identifier":"allow-add-marketplace-token-account","description":"Enables the add_marketplace_token_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_token_account"],"deny":[]}},"allow-add-project":{"identifier":"allow-add-project","description":"Enables the add_project command without any pre-configured scope.","commands":{"allow":["add_project"],"deny":[]}},"allow-add-scheduled-task":{"identifier":"allow-add-scheduled-task","description":"Enables the add_scheduled_task command without any pre-configured scope.","commands":{"allow":["add_scheduled_task"],"deny":[]}},"allow-apply-marketplace-now":{"identifier":"allow-apply-marketplace-now","description":"Enables the apply_marketplace_now command without any pre-configured scope.","commands":{"allow":["apply_marketplace_now"],"deny":[]}},"allow-apply-settings-import":{"identifier":"allow-apply-settings-import","description":"Enables the apply_settings_import command without any pre-configured scope.","commands":{"allow":["apply_settings_import"],"deny":[]}},"allow-aws-connection-identity":{"identifier":"allow-aws-connection-identity","description":"Enables the aws_connection_identity command without any pre-configured scope.","commands":{"allow":["aws_connection_identity"],"deny":[]}},"allow-aws-connection-sso-login":{"identifier":"allow-aws-connection-sso-login","description":"Enables the aws_connection_sso_login command without any pre-configured scope.","commands":{"allow":["aws_connection_sso_login"],"deny":[]}},"allow-aws-cost-estimate":{"identifier":"allow-aws-cost-estimate","description":"Enables the aws_cost_estimate command without any pre-configured scope.","commands":{"allow":["aws_cost_estimate"],"deny":[]}},"allow-aws-ec2-capacity":{"identifier":"allow-aws-ec2-capacity","description":"Enables the aws_ec2_capacity command without any pre-configured scope.","commands":{"allow":["aws_ec2_capacity"],"deny":[]}},"allow-aws-environment-max-hours":{"identifier":"allow-aws-environment-max-hours","description":"Enables the aws_environment_max_hours command without any pre-configured scope.","commands":{"allow":["aws_environment_max_hours"],"deny":[]}},"allow-aws-policy-document":{"identifier":"allow-aws-policy-document","description":"Enables the aws_policy_document command without any pre-configured scope.","commands":{"allow":["aws_policy_document"],"deny":[]}},"allow-aws-sso-refresh":{"identifier":"allow-aws-sso-refresh","description":"Enables the aws_sso_refresh command without any pre-configured scope.","commands":{"allow":["aws_sso_refresh"],"deny":[]}},"allow-aws-task-log":{"identifier":"allow-aws-task-log","description":"Enables the aws_task_log command without any pre-configured scope.","commands":{"allow":["aws_task_log"],"deny":[]}},"allow-aws-workspace-status":{"identifier":"allow-aws-workspace-status","description":"Enables the aws_workspace_status command without any pre-configured scope.","commands":{"allow":["aws_workspace_status"],"deny":[]}},"allow-build-gateway-image":{"identifier":"allow-build-gateway-image","description":"Enables the build_gateway_image command without any pre-configured scope.","commands":{"allow":["build_gateway_image"],"deny":[]}},"allow-build-image":{"identifier":"allow-build-image","description":"Enables the build_image command without any pre-configured scope.","commands":{"allow":["build_image"],"deny":[]}},"allow-build-stt-image":{"identifier":"allow-build-stt-image","description":"Enables the build_stt_image command without any pre-configured scope.","commands":{"allow":["build_stt_image"],"deny":[]}},"allow-cancel-aws-deployment":{"identifier":"allow-cancel-aws-deployment","description":"Enables the cancel_aws_deployment command withoutLine truncated
+1 -1
View File
@@ -1 +1 @@
{"default":{"identifier":"default","description":"Default capabilities for Triple-C. Every entry here is an IPC command a compromised webview can call directly, so the set is an enumeration of what `app/src` actually invokes — plugin and core grants verified against tauri 2.11.0's `PLUGINS` table in tauri's own `build.rs` rather than assumed from a plugin's `default` set, app-command grants (the bare `allow-*` entries) cross-checked by this crate's `build.rs` against `generate_handler!`. `core:default` in particular is NOT used: it is an alias for `core:{path,event,window,webview,app,image,resources,menu,tray}:default`, and `core:image:default` carries `allow-from-path`, whose handler (`tauri-2.11.0/src/image/plugin.rs:41` → `src/image/mod.rs:96`) is a bare `std::fs::read(path)` with no scope mechanism of any kind. Nothing imports `@tauri-apps/api/image`, so the whole plugin is dropped rather than scoped — there is nothing to scope it with. `core:menu` and `core:tray` are dropped for the same reason (no menu, no tray icon); `core:window` and `core:path` because nothing imports them; `core:resources:allow-close` because no frontend value is a `Resource`; and `core:event`'s `allow-emit`/`allow-emit-to` because the frontend only ever *listens* — every emit in this app originates in Rust. Three notes on what is deliberately kept or accepted: (1) `core:webview:allow-internal-toggle-devtools` is not called by `app/src` at all — it is called by Tauri's own injected `toggle-devtools.js`, which binds Ctrl/Cmd+Shift+I. Both that script and the command behind it are `#[cfg(any(debug_assertions, feature = \"devtools\"))]`, so this grant is a `tauri dev` convenience that does not exist in a release bundle. (2) `opener:allow-open-url` is **gone**. It could not be narrowed by host — `TerminalView`'s `WebLinksAddon` opens links Claude printed inside the container, which are arbitrary by construction, so a host allowlist would have deleted the feature rather than bounded it — and it was carried here as an accepted residual risk: a compromised webview could make the OS open an attacker-chosen http(s) URL, an outbound channel. That risk is now closed rather than recorded. Every host-browser open in the app goes through the `open_url_external` command in `url_open.rs`, which exists because the AppImage environment leaks into a cold-launched browser on Linux (triple-c#34) and which re-validates the URL in Rust — scheme allowlist, no embedded credentials, no control characters, length cap, ASCII asserted before `execvp`. On macOS and Windows that command reaches the same plugin as before, via `OpenerExt::open_url`, whose desktop implementation calls `crate::open::open` directly and is therefore not gated by this file at all (`tauri-plugin-opener-2.5.3/src/lib.rs:60`). The plugin stays a dependency for exactly that reason; what is removed is the webview's ability to reach it without passing the Rust validation. (3) `drag:allow-start-drag` is **gone**, together with the OS drag-out it existed for. It could not be scoped — `tauri-plugin-drag` takes the item paths from the caller and has no scope mechanism, so a compromised webview could call `startDrag({ item: ['~/.ssh/id_rsa'] })` against any host path the user can read — and it was carried as an accepted residual risk for one gesture. Drag-out was held back for separate hardening (see branch `hold/disk-and-dragout`) and the plugin is no longer a dependency. Getting a file *out* of a container is either \"Back up container\" on the project's Overview tab, which archives a tree through the Docker API, or the Files tab's per-row \"Save to host…\", which copies one file; getting one *in* is a drop on the Terminal or the Files tab's \"Upload…\". None of the four touches this permission. The Files tab's two are worth separating out here, because they are the only host-path commands in the app whose dialog is opened by **Rust** rather than by the webview — `pick_save_path` and `pick_files_to_upload` in `commands/file_commands.rs` drive `tauri-plugin-dialog` from the backend, so a compromised webview can ask for a picker and nothing more: it cannot name a host path as an *input* to either command. Be precise about the limit of that claim — host paths do still travel outward in error text (`Failed to create /home/j/Documents/x.txt.triple-c-part-1a2b3c4d: Permission denied`), including canonicalized ones, which disclose symlink targets. That is accepted; the app already hands the webview the project paths. What is closed is the direction that mattered — the webview naming where bytes go. That is the shape an earlier revision of this file named as the honest one if the Files tab ever regained host I/O, and it is the shape it regained it in. The `dialog:allow-open` / `dialog:allow-save` grants below are therefore *not* what those two use; they remain for the frontend pickers in Add Project, the Config tab's workspace and access sections, the CA-certificate fielLine truncated
{"default":{"identifier":"default","description":"Default capabilities for Triple-C. Every entry here is an IPC command a compromised webview can call directly, so the set is an enumeration of what `app/src` actually invokes — plugin and core grants verified against tauri 2.11.0's `PLUGINS` table in tauri's own `build.rs` rather than assumed from a plugin's `default` set, app-command grants (the bare `allow-*` entries) cross-checked by this crate's `build.rs` against `generate_handler!`. `core:default` in particular is NOT used: it is an alias for `core:{path,event,window,webview,app,image,resources,menu,tray}:default`, and `core:image:default` carries `allow-from-path`, whose handler (`tauri-2.11.0/src/image/plugin.rs:41` → `src/image/mod.rs:96`) is a bare `std::fs::read(path)` with no scope mechanism of any kind. Nothing imports `@tauri-apps/api/image`, so the whole plugin is dropped rather than scoped — there is nothing to scope it with. `core:menu` and `core:tray` are dropped for the same reason (no menu, no tray icon); `core:window` and `core:path` because nothing imports them; `core:resources:allow-close` because no frontend value is a `Resource`; and `core:event`'s `allow-emit`/`allow-emit-to` because the frontend only ever *listens* — every emit in this app originates in Rust. Three notes on what is deliberately kept or accepted: (1) `core:webview:allow-internal-toggle-devtools` is not called by `app/src` at all — it is called by Tauri's own injected `toggle-devtools.js`, which binds Ctrl/Cmd+Shift+I. Both that script and the command behind it are `#[cfg(any(debug_assertions, feature = \"devtools\"))]`, so this grant is a `tauri dev` convenience that does not exist in a release bundle. (2) `opener:allow-open-url` is **gone**. It could not be narrowed by host — `TerminalView`'s `WebLinksAddon` opens links Claude printed inside the container, which are arbitrary by construction, so a host allowlist would have deleted the feature rather than bounded it — and it was carried here as an accepted residual risk: a compromised webview could make the OS open an attacker-chosen http(s) URL, an outbound channel. That risk is now closed rather than recorded. Every host-browser open in the app goes through the `open_url_external` command in `url_open.rs`, which exists because the AppImage environment leaks into a cold-launched browser on Linux (triple-c#34) and which re-validates the URL in Rust — scheme allowlist, no embedded credentials, no control characters, length cap, ASCII asserted before `execvp`. On macOS and Windows that command reaches the same plugin as before, via `OpenerExt::open_url`, whose desktop implementation calls `crate::open::open` directly and is therefore not gated by this file at all (`tauri-plugin-opener-2.5.3/src/lib.rs:60`). The plugin stays a dependency for exactly that reason; what is removed is the webview's ability to reach it without passing the Rust validation. (3) `drag:allow-start-drag` is **gone**, together with the OS drag-out it existed for. It could not be scoped — `tauri-plugin-drag` takes the item paths from the caller and has no scope mechanism, so a compromised webview could call `startDrag({ item: ['~/.ssh/id_rsa'] })` against any host path the user can read — and it was carried as an accepted residual risk for one gesture. Drag-out was held back for separate hardening (see branch `hold/disk-and-dragout`) and the plugin is no longer a dependency. Getting a file *out* of a container is either \"Back up container\" on the project's Overview tab, which archives a tree through the Docker API, or the Files tab's per-row \"Save to host…\", which copies one file; getting one *in* is a drop on the Terminal or the Files tab's \"Upload…\". None of the four touches this permission. The Files tab's two are worth separating out here, because they are the only host-path commands in the app whose dialog is opened by **Rust** rather than by the webview — `pick_save_path` and `pick_files_to_upload` in `commands/file_commands.rs` drive `tauri-plugin-dialog` from the backend, so a compromised webview can ask for a picker and nothing more: it cannot name a host path as an *input* to either command. Be precise about the limit of that claim — host paths do still travel outward in error text (`Failed to create /home/j/Documents/x.txt.triple-c-part-1a2b3c4d: Permission denied`), including canonicalized ones, which disclose symlink targets. That is accepted; the app already hands the webview the project paths. What is closed is the direction that mattered — the webview naming where bytes go. That is the shape an earlier revision of this file named as the honest one if the Files tab ever regained host I/O, and it is the shape it regained it in. The `dialog:allow-open` / `dialog:allow-save` grants below are therefore *not* what those two use; they remain for the frontend pickers in Add Project, the Config tab's workspace and access sections, the CA-certificate fielLine truncated
@@ -416,6 +416,24 @@
"const": "allow-aws-connection-sso-login",
"markdownDescription": "Enables the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Enables the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-cost-estimate",
"markdownDescription": "Enables the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Enables the aws_ec2_capacity command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-ec2-capacity",
"markdownDescription": "Enables the aws_ec2_capacity command without any pre-configured scope."
},
{
"description": "Enables the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-environment-max-hours",
"markdownDescription": "Enables the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Enables the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -428,6 +446,18 @@
"const": "allow-aws-sso-refresh",
"markdownDescription": "Enables the aws_sso_refresh command without any pre-configured scope."
},
{
"description": "Enables the aws_task_log command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-task-log",
"markdownDescription": "Enables the aws_task_log command without any pre-configured scope."
},
{
"description": "Enables the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-workspace-status",
"markdownDescription": "Enables the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Enables the build_gateway_image command without any pre-configured scope.",
"type": "string",
@@ -506,6 +536,12 @@
"const": "allow-check-image-update",
"markdownDescription": "Enables the check_image_update command without any pre-configured scope."
},
{
"description": "Enables the check_unsaved_work command without any pre-configured scope.",
"type": "string",
"const": "allow-check-unsaved-work",
"markdownDescription": "Enables the check_unsaved_work command without any pre-configured scope."
},
{
"description": "Enables the clear_claude_token command without any pre-configured scope.",
"type": "string",
@@ -614,6 +650,12 @@
"const": "allow-download-container-file",
"markdownDescription": "Enables the download_container_file command without any pre-configured scope."
},
{
"description": "Enables the ec2_fit_problem command without any pre-configured scope.",
"type": "string",
"const": "allow-ec2-fit-problem",
"markdownDescription": "Enables the ec2_fit_problem command without any pre-configured scope."
},
{
"description": "Enables the export_settings command without any pre-configured scope.",
"type": "string",
@@ -794,6 +836,12 @@
"const": "allow-list-container-files",
"markdownDescription": "Enables the list_container_files command without any pre-configured scope."
},
{
"description": "Enables the list_ec2_instance_types command without any pre-configured scope.",
"type": "string",
"const": "allow-list-ec2-instance-types",
"markdownDescription": "Enables the list_ec2_instance_types command without any pre-configured scope."
},
{
"description": "Enables the list_marketplace_snapshots command without any pre-configured scope.",
"type": "string",
@@ -926,6 +974,12 @@
"const": "allow-rebuild-project-container",
"markdownDescription": "Enables the rebuild_project_container command without any pre-configured scope."
},
{
"description": "Enables the reconcile_aws_projects command without any pre-configured scope.",
"type": "string",
"const": "allow-reconcile-aws-projects",
"markdownDescription": "Enables the reconcile_aws_projects command without any pre-configured scope."
},
{
"description": "Enables the reconcile_project_statuses command without any pre-configured scope.",
"type": "string",
@@ -950,6 +1004,12 @@
"const": "allow-regenerate-web-terminal-token",
"markdownDescription": "Enables the regenerate_web_terminal_token command without any pre-configured scope."
},
{
"description": "Enables the remove_aws_resources command without any pre-configured scope.",
"type": "string",
"const": "allow-remove-aws-resources",
"markdownDescription": "Enables the remove_aws_resources command without any pre-configured scope."
},
{
"description": "Enables the remove_host_connection command without any pre-configured scope.",
"type": "string",
@@ -1346,6 +1406,24 @@
"const": "deny-aws-connection-sso-login",
"markdownDescription": "Denies the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Denies the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-cost-estimate",
"markdownDescription": "Denies the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Denies the aws_ec2_capacity command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-ec2-capacity",
"markdownDescription": "Denies the aws_ec2_capacity command without any pre-configured scope."
},
{
"description": "Denies the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-environment-max-hours",
"markdownDescription": "Denies the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Denies the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -1358,6 +1436,18 @@
"const": "deny-aws-sso-refresh",
"markdownDescription": "Denies the aws_sso_refresh command without any pre-configured scope."
},
{
"description": "Denies the aws_task_log command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-task-log",
"markdownDescription": "Denies the aws_task_log command without any pre-configured scope."
},
{
"description": "Denies the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-workspace-status",
"markdownDescription": "Denies the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Denies the build_gateway_image command without any pre-configured scope.",
"type": "string",
@@ -1436,6 +1526,12 @@
"const": "deny-check-image-update",
"markdownDescription": "Denies the check_image_update command without any pre-configured scope."
},
{
"description": "Denies the check_unsaved_work command without any pre-configured scope.",
"type": "string",
"const": "deny-check-unsaved-work",
"markdownDescription": "Denies the check_unsaved_work command without any pre-configured scope."
},
{
"description": "Denies the clear_claude_token command without any pre-configured scope.",
"type": "string",
@@ -1544,6 +1640,12 @@
"const": "deny-download-container-file",
"markdownDescription": "Denies the download_container_file command without any pre-configured scope."
},
{
"description": "Denies the ec2_fit_problem command without any pre-configured scope.",
"type": "string",
"const": "deny-ec2-fit-problem",
"markdownDescription": "Denies the ec2_fit_problem command without any pre-configured scope."
},
{
"description": "Denies the export_settings command without any pre-configured scope.",
"type": "string",
@@ -1724,6 +1826,12 @@
"const": "deny-list-container-files",
"markdownDescription": "Denies the list_container_files command without any pre-configured scope."
},
{
"description": "Denies the list_ec2_instance_types command without any pre-configured scope.",
"type": "string",
"const": "deny-list-ec2-instance-types",
"markdownDescription": "Denies the list_ec2_instance_types command without any pre-configured scope."
},
{
"description": "Denies the list_marketplace_snapshots command without any pre-configured scope.",
"type": "string",
@@ -1856,6 +1964,12 @@
"const": "deny-rebuild-project-container",
"markdownDescription": "Denies the rebuild_project_container command without any pre-configured scope."
},
{
"description": "Denies the reconcile_aws_projects command without any pre-configured scope.",
"type": "string",
"const": "deny-reconcile-aws-projects",
"markdownDescription": "Denies the reconcile_aws_projects command without any pre-configured scope."
},
{
"description": "Denies the reconcile_project_statuses command without any pre-configured scope.",
"type": "string",
@@ -1880,6 +1994,12 @@
"const": "deny-regenerate-web-terminal-token",
"markdownDescription": "Denies the regenerate_web_terminal_token command without any pre-configured scope."
},
{
"description": "Denies the remove_aws_resources command without any pre-configured scope.",
"type": "string",
"const": "deny-remove-aws-resources",
"markdownDescription": "Denies the remove_aws_resources command without any pre-configured scope."
},
{
"description": "Denies the remove_host_connection command without any pre-configured scope.",
"type": "string",
+120
View File
@@ -416,6 +416,24 @@
"const": "allow-aws-connection-sso-login",
"markdownDescription": "Enables the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Enables the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-cost-estimate",
"markdownDescription": "Enables the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Enables the aws_ec2_capacity command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-ec2-capacity",
"markdownDescription": "Enables the aws_ec2_capacity command without any pre-configured scope."
},
{
"description": "Enables the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-environment-max-hours",
"markdownDescription": "Enables the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Enables the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -428,6 +446,18 @@
"const": "allow-aws-sso-refresh",
"markdownDescription": "Enables the aws_sso_refresh command without any pre-configured scope."
},
{
"description": "Enables the aws_task_log command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-task-log",
"markdownDescription": "Enables the aws_task_log command without any pre-configured scope."
},
{
"description": "Enables the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-workspace-status",
"markdownDescription": "Enables the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Enables the build_gateway_image command without any pre-configured scope.",
"type": "string",
@@ -506,6 +536,12 @@
"const": "allow-check-image-update",
"markdownDescription": "Enables the check_image_update command without any pre-configured scope."
},
{
"description": "Enables the check_unsaved_work command without any pre-configured scope.",
"type": "string",
"const": "allow-check-unsaved-work",
"markdownDescription": "Enables the check_unsaved_work command without any pre-configured scope."
},
{
"description": "Enables the clear_claude_token command without any pre-configured scope.",
"type": "string",
@@ -614,6 +650,12 @@
"const": "allow-download-container-file",
"markdownDescription": "Enables the download_container_file command without any pre-configured scope."
},
{
"description": "Enables the ec2_fit_problem command without any pre-configured scope.",
"type": "string",
"const": "allow-ec2-fit-problem",
"markdownDescription": "Enables the ec2_fit_problem command without any pre-configured scope."
},
{
"description": "Enables the export_settings command without any pre-configured scope.",
"type": "string",
@@ -794,6 +836,12 @@
"const": "allow-list-container-files",
"markdownDescription": "Enables the list_container_files command without any pre-configured scope."
},
{
"description": "Enables the list_ec2_instance_types command without any pre-configured scope.",
"type": "string",
"const": "allow-list-ec2-instance-types",
"markdownDescription": "Enables the list_ec2_instance_types command without any pre-configured scope."
},
{
"description": "Enables the list_marketplace_snapshots command without any pre-configured scope.",
"type": "string",
@@ -926,6 +974,12 @@
"const": "allow-rebuild-project-container",
"markdownDescription": "Enables the rebuild_project_container command without any pre-configured scope."
},
{
"description": "Enables the reconcile_aws_projects command without any pre-configured scope.",
"type": "string",
"const": "allow-reconcile-aws-projects",
"markdownDescription": "Enables the reconcile_aws_projects command without any pre-configured scope."
},
{
"description": "Enables the reconcile_project_statuses command without any pre-configured scope.",
"type": "string",
@@ -950,6 +1004,12 @@
"const": "allow-regenerate-web-terminal-token",
"markdownDescription": "Enables the regenerate_web_terminal_token command without any pre-configured scope."
},
{
"description": "Enables the remove_aws_resources command without any pre-configured scope.",
"type": "string",
"const": "allow-remove-aws-resources",
"markdownDescription": "Enables the remove_aws_resources command without any pre-configured scope."
},
{
"description": "Enables the remove_host_connection command without any pre-configured scope.",
"type": "string",
@@ -1346,6 +1406,24 @@
"const": "deny-aws-connection-sso-login",
"markdownDescription": "Denies the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Denies the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-cost-estimate",
"markdownDescription": "Denies the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Denies the aws_ec2_capacity command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-ec2-capacity",
"markdownDescription": "Denies the aws_ec2_capacity command without any pre-configured scope."
},
{
"description": "Denies the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-environment-max-hours",
"markdownDescription": "Denies the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Denies the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -1358,6 +1436,18 @@
"const": "deny-aws-sso-refresh",
"markdownDescription": "Denies the aws_sso_refresh command without any pre-configured scope."
},
{
"description": "Denies the aws_task_log command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-task-log",
"markdownDescription": "Denies the aws_task_log command without any pre-configured scope."
},
{
"description": "Denies the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-workspace-status",
"markdownDescription": "Denies the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Denies the build_gateway_image command without any pre-configured scope.",
"type": "string",
@@ -1436,6 +1526,12 @@
"const": "deny-check-image-update",
"markdownDescription": "Denies the check_image_update command without any pre-configured scope."
},
{
"description": "Denies the check_unsaved_work command without any pre-configured scope.",
"type": "string",
"const": "deny-check-unsaved-work",
"markdownDescription": "Denies the check_unsaved_work command without any pre-configured scope."
},
{
"description": "Denies the clear_claude_token command without any pre-configured scope.",
"type": "string",
@@ -1544,6 +1640,12 @@
"const": "deny-download-container-file",
"markdownDescription": "Denies the download_container_file command without any pre-configured scope."
},
{
"description": "Denies the ec2_fit_problem command without any pre-configured scope.",
"type": "string",
"const": "deny-ec2-fit-problem",
"markdownDescription": "Denies the ec2_fit_problem command without any pre-configured scope."
},
{
"description": "Denies the export_settings command without any pre-configured scope.",
"type": "string",
@@ -1724,6 +1826,12 @@
"const": "deny-list-container-files",
"markdownDescription": "Denies the list_container_files command without any pre-configured scope."
},
{
"description": "Denies the list_ec2_instance_types command without any pre-configured scope.",
"type": "string",
"const": "deny-list-ec2-instance-types",
"markdownDescription": "Denies the list_ec2_instance_types command without any pre-configured scope."
},
{
"description": "Denies the list_marketplace_snapshots command without any pre-configured scope.",
"type": "string",
@@ -1856,6 +1964,12 @@
"const": "deny-rebuild-project-container",
"markdownDescription": "Denies the rebuild_project_container command without any pre-configured scope."
},
{
"description": "Denies the reconcile_aws_projects command without any pre-configured scope.",
"type": "string",
"const": "deny-reconcile-aws-projects",
"markdownDescription": "Denies the reconcile_aws_projects command without any pre-configured scope."
},
{
"description": "Denies the reconcile_project_statuses command without any pre-configured scope.",
"type": "string",
@@ -1880,6 +1994,12 @@
"const": "deny-regenerate-web-terminal-token",
"markdownDescription": "Denies the regenerate_web_terminal_token command without any pre-configured scope."
},
{
"description": "Denies the remove_aws_resources command without any pre-configured scope.",
"type": "string",
"const": "deny-remove-aws-resources",
"markdownDescription": "Denies the remove_aws_resources command without any pre-configured scope."
},
{
"description": "Denies the remove_host_connection command without any pre-configured scope.",
"type": "string",
+3 -2
View File
@@ -326,7 +326,8 @@ async fn poll_loop(
// bridge or stopping the container doesn't wait out an in-flight poll.
let discovery = tokio::select! {
_ = cancel.changed() => break,
res = container.exec_oneshot_limited("claude", cmd, PROC_NET_OUTPUT_LIMIT) => {
// Background: on AWS this poll must not keep an idle workspace up.
res = container.exec_oneshot_background("claude", cmd, PROC_NET_OUTPUT_LIMIT) => {
res.map(|(out, _)| out)
}
};
@@ -582,7 +583,7 @@ async fn reconcile(
);
continue;
}
match PortForward::bind(container.clone(), port, family).await {
match PortForward::bind(container.clone(), port, port, family).await {
Ok(forward) => {
if st.conflicts.remove(&port).is_some() {
log::info!("Auth bridge: host port {} became available", port);
+97 -20
View File
@@ -119,7 +119,11 @@ impl Drop for AbortOnDrop {
/// also awaiting the aborted task, which guarantees the socket is closed before
/// the caller proceeds (important when a port is rebound right after).
pub struct PortForward {
/// The host loopback port.
pub port: u16,
/// The container loopback port it reaches. The auth bridge mirrors a
/// port onto the same number; a project's port mapping may not.
pub container_port: u16,
pub family: PortFamily,
pub bridged_at: String,
/// Why `[::1]` could not be taken alongside `127.0.0.1`, if it could not.
@@ -141,15 +145,40 @@ impl Drop for PortForward {
}
impl PortForward {
/// Bind `port` on the host loopback and start proxying into `container`.
/// Bind `host_port` on the host loopback and start proxying into
/// `container_port` on the container's loopback — the auth bridge's
/// forward, gated (see [`Gate::SignInCallback`]).
///
/// The bind happens before the task is spawned, so an already-taken port is
/// reported to the caller as an error rather than disappearing into a
/// background task.
pub async fn bind(
container: ContainerTarget,
port: u16,
host_port: u16,
container_port: u16,
family: PortFamily,
) -> Result<Self, std::io::Error> {
Self::bind_with(container, host_port, container_port, family, Gate::SignInCallback).await
}
/// As [`bind`](Self::bind), for a project's port mapping on a host with
/// no Docker port publishing (AWS): every connection is carried as is
/// (see [`Gate::None`]).
pub async fn bind_mapping(
container: ContainerTarget,
host_port: u16,
container_port: u16,
family: PortFamily,
) -> Result<Self, std::io::Error> {
Self::bind_with(container, host_port, container_port, family, Gate::None).await
}
async fn bind_with(
container: ContainerTarget,
port: u16,
container_port: u16,
family: PortFamily,
gate: Gate,
) -> Result<Self, std::io::Error> {
// SECURITY BOUNDARY: the host side binds loopback ONLY — 127.0.0.1 and
// ::1, never 0.0.0.0 / ::. Everything reachable through this socket is
@@ -176,8 +205,8 @@ impl PortForward {
// bridge reports itself healthy — a silent failure with no
// thread back to this line.
log::warn!(
"Auth bridge: bound 127.0.0.1:{} but not [::1]:{} ({}) — continuing with IPv4 only; \
a client that resolves localhost to ::1 without falling back will not reach it",
"{}: bound 127.0.0.1:{} but not [::1]:{} ({}) — continuing with IPv4 only; \
a client that resolves localhost to ::1 without falling back will not reach it", gate.label(),
port,
port,
e
@@ -193,11 +222,12 @@ impl PortForward {
}
};
let target = family.socat_target(port);
let task = tokio::spawn(accept_loop(container, port, target, v4, v6));
let target = family.socat_target(container_port);
let task = tokio::spawn(accept_loop(container, port, target, v4, v6, gate));
Ok(Self {
port,
container_port,
family,
bridged_at: chrono::Utc::now().to_rfc3339(),
ipv6_warning,
@@ -214,6 +244,30 @@ impl PortForward {
}
}
/// What an accepted connection must show before it is carried in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Gate {
/// The auth bridge: a client must speak within [`FIRST_BYTE_TIMEOUT`],
/// and a cross-site sub-resource request is refused
/// ([`web_request_verdict`]).
SignInCallback,
/// A port mapping the user configured: carried as is, like Docker's own
/// port publishing (which has no gate either) — a server-speaks-first
/// protocol such as MySQL or SMTP must work, so nothing waits for the
/// client's first byte.
None,
}
impl Gate {
/// The log prefix of a forward of this kind.
fn label(self) -> &'static str {
match self {
Gate::SignInCallback => "Auth bridge",
Gate::None => "Port mapping",
}
}
}
/// Accept on both loopback listeners until aborted. Dropping this future drops
/// the listeners (freeing the port) and the `JoinSet` (aborting live tunnels).
async fn accept_loop(
@@ -222,6 +276,7 @@ async fn accept_loop(
target: String,
v4: TcpListener,
v6: Option<TcpListener>,
gate: Gate,
) {
let mut conns: JoinSet<()> = JoinSet::new();
@@ -245,25 +300,35 @@ async fn accept_loop(
// client side is whatever can dial loopback, so a queue is
// just a slower way to run out of execs.
log::warn!(
"Auth bridge: refusing connection from {} to bridged port {} — \
{} concurrent connections already open on it",
"{}: refusing connection from {} to bridged port {} — \
{} concurrent connections already open on it", gate.label(),
peer,
port,
MAX_CONNECTIONS
);
continue;
}
log::debug!("Auth bridge: connection from {} to bridged port {}", peer, port);
log::debug!("{}: connection from {} to bridged port {}", gate.label(), peer, port);
let _ = stream.set_nodelay(true);
conns.spawn(tunnel_connection(
container.clone(),
target.clone(),
stream,
port,
));
match gate {
Gate::SignInCallback => conns.spawn(tunnel_connection(
container.clone(),
target.clone(),
stream,
port,
)),
Gate::None => conns.spawn(tunnel_labelled(
gate.label(),
container.clone(),
target.clone(),
stream,
port,
Vec::new(),
)),
};
}
Err(e) => {
log::warn!("Auth bridge: accept failed on port {}: {} — stopping listener", port, e);
log::warn!("{}: accept failed on port {}: {} — stopping listener", gate.label(), port, e);
return;
}
}
@@ -520,6 +585,18 @@ pub async fn tunnel_connection_with_prelude(
stream: TcpStream,
port: u16,
prelude: Vec<u8>,
) {
tunnel_labelled("Auth bridge", container, target, stream, port, prelude).await
}
/// [`tunnel_connection_with_prelude`], logging under `label`.
async fn tunnel_labelled(
label: &'static str,
container: ContainerTarget,
target: String,
stream: TcpStream,
port: u16,
prelude: Vec<u8>,
) {
let cmd = vec!["socat".to_string(), "-".to_string(), target.clone()];
@@ -531,7 +608,7 @@ pub async fn tunnel_connection_with_prelude(
Ok(e) => e,
Err(e) => {
log::warn!(
"Auth bridge: failed to open tunnel exec for port {} ({}): {}",
"{}: failed to open tunnel exec for port {} ({}): {}", label,
port,
target,
e
@@ -579,7 +656,7 @@ pub async fn tunnel_connection_with_prelude(
Ok(chunk) => chunk,
Err(_) => {
log::debug!(
"Auth bridge: bridged port {} idle for {}s — closing the tunnel",
"{}: bridged port {} idle for {}s — closing the tunnel", label,
port,
IDLE_TIMEOUT.as_secs()
);
@@ -597,14 +674,14 @@ pub async fn tunnel_connection_with_prelude(
}
Ok(ExecChunk::StdErr(message)) => {
log::debug!(
"Auth bridge: socat stderr for port {}: {}",
"{}: socat stderr for port {}: {}", label,
port,
String::from_utf8_lossy(&message).trim()
);
}
Ok(_) => {}
Err(e) => {
log::debug!("Auth bridge: tunnel stream error on port {}: {}", port, e);
log::debug!("{}: tunnel stream error on port {}: {}", label, port, e);
break;
}
}
+35 -3
View File
@@ -5,6 +5,7 @@ use aws_config::identity::IdentityCache;
use aws_config::profile::ProfileFileCredentialsProvider;
use aws_config::provider_config::ProviderConfig;
use aws_config::retry::RetryConfig;
use aws_config::timeout::TimeoutConfig;
use aws_config::{BehaviorVersion, Region, SdkConfig};
use aws_credential_types::provider::error::CredentialsError;
use aws_credential_types::provider::{future, ProvideCredentials, SharedCredentialsProvider};
@@ -81,9 +82,9 @@ fn keys_credentials(keys: &secure::AwsKeys) -> SharedCredentialsProvider {
/// stray endpoint there would send every connection's signed requests (and
/// the permission check) somewhere other than AWS. Connections talk to the
/// real regional endpoints; a profile's own `endpoint_url` is ignored too.
/// The HTTP client, timeouts and stalled-stream protection are each service
/// client's defaults (the `default-https-client` stack every AWS crate here
/// is built with).
/// The HTTP client and stalled-stream protection are each service client's
/// defaults (the `default-https-client` stack every AWS crate here is built
/// with). Timeouts are ours: [`OPERATION_TIMEOUT`] / [`ATTEMPT_TIMEOUT`].
fn build_sdk_config(region: Region, credentials: SharedCredentialsProvider) -> SdkConfig {
let mut builder = SdkConfig::builder()
.behavior_version(BehaviorVersion::latest())
@@ -91,6 +92,7 @@ fn build_sdk_config(region: Region, credentials: SharedCredentialsProvider) -> S
.credentials_provider(credentials)
.identity_cache(IdentityCache::lazy().build())
.retry_config(RetryConfig::standard())
.timeout_config(timeouts())
.time_source(SharedTimeSource::default());
if let Some(sleep) = default_async_sleep() {
builder = builder.sleep_impl(sleep);
@@ -98,6 +100,22 @@ fn build_sdk_config(region: Region, credentials: SharedCredentialsProvider) -> S
builder.build()
}
/// Every call the app makes through the SDK is a control-plane call with a
/// small answer (ECS, Secrets Manager, EFS, IAM, SSM, CloudWatch Logs, STS,
/// and S3 reads of state objects capped at 2 KiB); OpenTofu talks to AWS
/// itself, not through these clients. Without a bound a half-open
/// connection would hang a start, a stop or the death watch for minutes.
/// The operation bound covers every retry; the attempt bound is one try.
pub const OPERATION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
pub const ATTEMPT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
fn timeouts() -> TimeoutConfig {
TimeoutConfig::builder()
.operation_timeout(OPERATION_TIMEOUT)
.operation_attempt_timeout(ATTEMPT_TIMEOUT)
.build()
}
/// Puts the profile's name into the source chain of every credentials error,
/// so a failure deep inside the SDK can be reported against the profile.
#[derive(Debug)]
@@ -297,4 +315,18 @@ sts =
assert_isolated(&cfg);
assert!(cfg.credentials_provider().is_some());
}
#[test]
fn every_sdk_call_is_bounded() {
let keys = crate::storage::secure::AwsKeys {
access_key_id: "AKIDTEST".into(),
secret_access_key: "s".into(),
session_token: None,
};
let cfg = build_sdk_config(Region::new("us-west-2"), keys_credentials(&keys));
let t = cfg.timeout_config().expect("a timeout config");
assert_eq!(t.operation_timeout(), Some(OPERATION_TIMEOUT));
assert_eq!(t.operation_attempt_timeout(), Some(ATTEMPT_TIMEOUT));
assert!(ATTEMPT_TIMEOUT < OPERATION_TIMEOUT);
}
}
+367 -14
View File
@@ -29,8 +29,9 @@ use serde_json::{json, Map, Value};
use super::error::{classify_sdk_error, AwsError};
use super::identity::CallerIdentity;
use super::tofu_modules::{
environment_parameter_name, owner_key, state_bucket_name, state_key, user_role_names,
validate_environment_name, Deployment, BOUNDARY_POLICY_NAME,
environment_parameter_name, owner_key, state_bucket_name, state_key, user_ec2_stem,
user_record_parameter_name, user_role_names, validate_ec2_user_tag, validate_environment_name,
Deployment, BOUNDARY_POLICY_NAME, EC2_MAX_INSTANCES,
};
use crate::models::host_connection::validate_supported_region;
@@ -75,6 +76,10 @@ pub struct EnvironmentInfo {
pub log_group: String,
pub network_mode: String,
pub tenancy: String,
/// Advisory lifetime cap for workspaces, in hours; 0 = none. Absent in
/// records written by module version 1.
#[serde(default)]
pub max_workspace_hours: u32,
}
fn field<'a>(obj: &'a Map<String, Value>, name: &str) -> Result<&'a Value, String> {
@@ -130,7 +135,12 @@ pub fn parse_environment_record(
.ok_or_else(|| "template_version is not a version number".to_string())?;
let cluster_arn = str_field(obj, "cluster_arn")?;
if cluster_arn != format!("arn:aws:ecs:{region}:{account}:cluster/triple-c-{name}") {
if cluster_arn
!= format!(
"arn:aws:ecs:{region}:{account}:cluster/{}",
super::tofu_modules::environment_cluster_name(&name)
)
{
return Err(
"cluster_arn is not this environment's cluster in this account and region".into(),
);
@@ -171,7 +181,7 @@ pub fn parse_environment_record(
return Err("kms_key_arn is not a KMS key in this account and region".into());
}
let log_group = str_field(obj, "log_group")?;
if log_group != format!("/triple-c/{name}") {
if log_group != super::tofu_modules::environment_log_group(&name) {
return Err("log_group is not this environment's log group".into());
}
let network_mode = str_field(obj, "network_mode")?;
@@ -182,6 +192,15 @@ pub fn parse_environment_record(
return Err("tenancy is not strict".into());
}
let max_workspace_hours = match obj.get("max_workspace_hours") {
None => 0,
Some(v) => v
.as_u64()
.and_then(|v| u32::try_from(v).ok())
.filter(|v| *v <= super::tofu_modules::MAX_WORKSPACE_HOURS)
.ok_or_else(|| "max_workspace_hours is not a number from 0 to 168".to_string())?,
};
Ok(EnvironmentInfo {
schema: RECORD_SCHEMA as u32,
name: name.clone(),
@@ -194,6 +213,7 @@ pub fn parse_environment_record(
log_group: log_group.to_string(),
network_mode: network_mode.to_string(),
tenancy: "strict".to_string(),
max_workspace_hours,
})
}
@@ -290,15 +310,97 @@ pub enum EnvironmentAction {
#[serde(tag = "state", rename_all = "snake_case")]
pub enum OnboardingState {
/// Their task role exists and, where the bucket is readable, their state
/// records a finished apply (its `onboarding_complete` output).
Joined,
/// records a finished apply (its `onboarding_complete` output). `ec2` is
/// their EC2 capacity, from their user record.
Joined { ec2: Option<UserCompute> },
NotJoined,
/// A local runner, or a recent lock.
InProgress { lock: Option<LockInfo> },
/// The last onboarding run didn't finish: a stale lock, a failed run on
/// this machine, or a state without the completion output. Join is a
/// re-apply and finishes it.
Incomplete { lock: Option<LockInfo> },
/// re-apply and finishes it; `ec2` is what their record still holds, so
/// the re-apply can keep it.
Incomplete {
lock: Option<LockInfo>,
ec2: Option<UserCompute>,
},
}
impl OnboardingState {
fn with_ec2(self, compute: Option<UserCompute>) -> Self {
match self {
OnboardingState::Joined { .. } => OnboardingState::Joined { ec2: compute },
OnboardingState::Incomplete { lock, .. } => OnboardingState::Incomplete { lock, ec2: compute },
other => other,
}
}
}
/// A user's EC2 capacity in one environment: their validated user record
/// (`tofu_modules::user_record_parameter_name`). A missing record means no
/// NEW EC2 runs there, not that nothing runs (teardown removes it first).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct UserCompute {
pub instance_type: String,
pub max_instances: u32,
/// The `triple-c-user` value the cluster is named after.
pub user_tag: String,
pub cluster_arn: String,
pub capacity_provider: String,
}
/// Validate a user record read from `user_record_parameter_name(env, …)`:
/// schema 1, a type the app's table knows, 1 to `EC2_MAX_INSTANCES`
/// instances (absent in records written before the field: 1), a valid user
/// tag, and the cluster and capacity provider of exactly that tag's per-user
/// names in `account` and `region`. The error names the field, never its
/// value.
pub fn parse_user_record(
env: &str,
value: &str,
account: &str,
region: &str,
) -> Result<UserCompute, String> {
if value.len() > MAX_UNTRUSTED_BYTES {
return Err("the record is larger than 4 KiB".into());
}
let parsed: Value =
serde_json::from_str(value).map_err(|_| "the record is not JSON".to_string())?;
let obj = parsed
.as_object()
.ok_or_else(|| "the record is not a JSON object".to_string())?;
if field(obj, "schema")?.as_u64() != Some(RECORD_SCHEMA) {
return Err("schema is not 1".into());
}
let instance_type = str_field(obj, "instance_type")?;
if crate::models::project::ec2_instance_spec(instance_type).is_none() {
return Err("instance_type is not a supported instance type".into());
}
let max_instances = match obj.get("max_instances") {
None => 1,
Some(v) => v
.as_u64()
.and_then(|v| u32::try_from(v).ok())
.filter(|v| (1..=EC2_MAX_INSTANCES).contains(v))
.ok_or_else(|| format!("max_instances is not a number from 1 to {EC2_MAX_INSTANCES}"))?,
};
let user_tag = validate_ec2_user_tag(str_field(obj, "user_tag")?)
.map_err(|_| "user_tag is not a valid triple-c-user value".to_string())?;
let stem = user_ec2_stem(env, &user_tag);
let cluster_arn = str_field(obj, "cluster_arn")?;
if cluster_arn != format!("arn:aws:ecs:{region}:{account}:cluster/{stem}") {
return Err("cluster_arn is not this user's cluster in this account and region".into());
}
if str_field(obj, "capacity_provider")? != stem {
return Err("capacity_provider is not this user's capacity provider".into());
}
Ok(UserCompute {
instance_type: instance_type.to_string(),
max_instances,
user_tag,
cluster_arn: cluster_arn.to_string(),
capacity_provider: stem,
})
}
#[derive(Serialize, Clone, Debug, PartialEq)]
@@ -546,9 +648,12 @@ fn onboarding_from_runs(
Some(OnboardingState::InProgress { lock })
} else if lock.is_some() {
actions.push(Unlock);
Some(OnboardingState::Incomplete { lock })
Some(OnboardingState::Incomplete { lock, ec2: None })
} else if run.failed || run.errored_state {
Some(OnboardingState::Incomplete { lock: None })
Some(OnboardingState::Incomplete {
lock: None,
ec2: None,
})
} else {
None
};
@@ -578,6 +683,8 @@ pub trait DetectApi: Send + Sync {
account: &str,
) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
fn role_exists(&self, name: &str) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
/// An SSM String parameter (a user record).
fn parameter(&self, name: &str) -> impl Future<Output = Result<Param, AwsError>> + Send;
/// A state object's contents, capped; `None` when it is missing, can't
/// be read or is too large.
fn state_document(
@@ -656,8 +763,13 @@ pub(crate) async fn scan_in_bucket<D: DetectApi>(
// role), which is not Joined.
None if objects.as_ref().is_some_and(|o| o.has_state(&user_state)) => {
match d.state_document(bucket, &user_state).await? {
Some(text) if onboarding_complete(&text) => OnboardingState::Joined,
_ => OnboardingState::Incomplete { lock: None },
Some(text) if onboarding_complete(&text) => {
OnboardingState::Joined { ec2: None }
}
_ => OnboardingState::Incomplete {
lock: None,
ec2: None,
},
}
}
// Without a state here (a workspace user, who can't list the
@@ -666,11 +778,20 @@ pub(crate) async fn scan_in_bucket<D: DetectApi>(
None => {
let (task_role, _) = user_role_names(&env.name, &key);
match d.role_exists(&task_role).await? {
Knowable::Yes => OnboardingState::Joined,
Knowable::Yes => OnboardingState::Joined { ec2: None },
Knowable::No | Knowable::Unknown => OnboardingState::NotJoined,
}
}
});
if let Some(state) = env.onboarding.take() {
let compute = match state {
OnboardingState::Joined { .. } | OnboardingState::Incomplete { .. } => {
user_compute(d, &env.name, &key, &who.account, region).await?
}
_ => None,
};
env.onboarding = Some(state.with_ec2(compute));
}
}
Ok(EnvironmentScan {
@@ -684,6 +805,234 @@ pub(crate) async fn scan_in_bucket<D: DetectApi>(
})
}
/// An SSM parameter read: its value, missing, or this caller can't read it.
#[derive(Clone, Debug, PartialEq)]
pub enum Param {
Value(String),
Missing,
Denied,
}
/// A user record as read: none, valid, or present but unusable (unreadable
/// or failing validation; the reason names a field, never a value).
#[derive(Clone, Debug, PartialEq)]
pub enum UserRecord {
Absent,
Valid(UserCompute),
Unusable(String),
}
impl UserRecord {
pub fn valid(&self) -> Option<&UserCompute> {
match self {
UserRecord::Valid(c) => Some(c),
_ => None,
}
}
}
/// [`UserRecord`] from a read of `user_record_parameter_name(env, …)`.
pub fn user_record_from(read: Param, env: &str, account: &str, region: &str) -> UserRecord {
match read {
Param::Missing => UserRecord::Absent,
Param::Denied => UserRecord::Unusable("it can't be read with these credentials".into()),
Param::Value(text) => match parse_user_record(env, &text, account, region) {
Ok(c) => UserRecord::Valid(c),
Err(reason) => UserRecord::Unusable(reason),
},
}
}
/// The caller's EC2 capacity in `env` from their user record; `None` when
/// there is no usable record (an unusable one is logged, never echoed).
pub(crate) async fn user_compute<D: DetectApi>(
d: &D,
env: &str,
owner_key: &str,
account: &str,
region: &str,
) -> Result<Option<UserCompute>, AwsError> {
let read = d.parameter(&user_record_parameter_name(env, owner_key)).await?;
match user_record_from(read, env, account, region) {
UserRecord::Valid(c) => Ok(Some(c)),
UserRecord::Absent => Ok(None),
UserRecord::Unusable(reason) => {
log::warn!("Ignoring the EC2 user record for environment {env}: {reason}");
Ok(None)
}
}
}
/// Who holds a per-user EC2 name (`tofu_modules::user_ec2_stem`): an ECS
/// cluster or Auto Scaling group of that name and its `triple-c:owner` tag.
#[derive(Clone, Debug, PartialEq)]
pub struct NameHolder {
/// "ECS cluster" or "Auto Scaling group".
pub kind: &'static str,
pub owner: Option<String>,
}
/// An ECS cluster or Auto Scaling group found by name: its status (clusters
/// only) and tags.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct NamedResource {
pub status: Option<String>,
pub tags: Vec<(String, String)>,
}
/// What an onboarding apply asks AWS before it starts (R3): the caller's
/// user record and whether the per-user EC2 names are taken. The cluster
/// read needs `ecs:DescribeClusters` (tags included); the group read
/// `autoscaling:DescribeAutoScalingGroups`, which only the environment-admin
/// tier has.
pub trait OnboardApi: Send + Sync {
fn parameter(&self, name: &str) -> impl Future<Output = Result<Param, AwsError>> + Send;
fn cluster(
&self,
name: &str,
) -> impl Future<Output = Result<Option<NamedResource>, AwsError>> + Send;
fn group(
&self,
name: &str,
) -> impl Future<Output = Result<Option<NamedResource>, AwsError>> + Send;
}
const OWNER_TAG: &str = "triple-c:owner";
fn owner_of(r: &NamedResource) -> Option<String> {
r.tags
.iter()
.find(|(k, _)| k == OWNER_TAG)
.map(|(_, v)| v.clone())
}
/// The cluster holding `stem`, if any. Every status but INACTIVE (a deleted
/// cluster) holds the name: PROVISIONING, DEPROVISIONING and FAILED too.
pub async fn cluster_holder<A: OnboardApi>(
api: &A,
stem: &str,
) -> Result<Option<NameHolder>, AwsError> {
Ok(api
.cluster(stem)
.await?
.filter(|c| c.status.as_deref() != Some("INACTIVE"))
.map(|c| NameHolder {
kind: "ECS cluster",
owner: owner_of(&c),
}))
}
/// Everything holding `stem`: its cluster and its Auto Scaling group.
pub async fn ec2_name_holders<A: OnboardApi>(
api: &A,
stem: &str,
) -> Result<Vec<NameHolder>, AwsError> {
let mut out: Vec<NameHolder> = cluster_holder(api, stem).await?.into_iter().collect();
if let Some(g) = api.group(stem).await? {
out.push(NameHolder {
kind: "Auto Scaling group",
owner: owner_of(&g),
});
}
Ok(out)
}
/// `ssm:GetParameter` as a [`Param`].
pub(crate) async fn get_parameter(ssm: &aws_sdk_ssm::Client, name: &str) -> Result<Param, AwsError> {
let res = ssm
.get_parameter()
.name(name)
.with_decryption(false)
.send()
.await;
match res {
Ok(out) => Ok(out
.parameter()
.and_then(|p| p.value())
.map_or(Param::Missing, |v| Param::Value(v.to_string()))),
Err(e) if e.as_service_error().is_some_and(|s| s.is_parameter_not_found()) => {
Ok(Param::Missing)
}
Err(e) => {
let (code, status) = error_parts(&e);
match lookup_outcome(code.as_deref(), status) {
Lookup::Missing => Ok(Param::Missing),
Lookup::Denied => Ok(Param::Denied),
Lookup::Error => Err(classify_sdk_error("ssm:GetParameter", &e)),
}
}
}
}
/// The real [`OnboardApi`], from a connection's `SdkConfig`.
pub struct SdkOnboardApi {
ssm: aws_sdk_ssm::Client,
ecs: aws_sdk_ecs::Client,
autoscaling: aws_sdk_autoscaling::Client,
}
impl SdkOnboardApi {
pub fn new(cfg: &SdkConfig) -> Self {
SdkOnboardApi {
ssm: aws_sdk_ssm::Client::new(cfg),
ecs: aws_sdk_ecs::Client::new(cfg),
autoscaling: aws_sdk_autoscaling::Client::new(cfg),
}
}
}
impl OnboardApi for SdkOnboardApi {
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
get_parameter(&self.ssm, name).await
}
async fn cluster(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
// A missing name is a `failures` entry, not an error.
let out = self
.ecs
.describe_clusters()
.clusters(name)
.include(aws_sdk_ecs::types::ClusterField::Tags)
.send()
.await
.map_err(|e| classify_sdk_error("ecs:DescribeClusters", &e))?;
Ok(out
.clusters()
.iter()
.find(|c| c.cluster_name() == Some(name))
.map(|c| NamedResource {
status: c.status().map(str::to_string),
tags: c
.tags()
.iter()
.filter_map(|t| Some((t.key()?.to_string(), t.value()?.to_string())))
.collect(),
}))
}
async fn group(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
let out = self
.autoscaling
.describe_auto_scaling_groups()
.auto_scaling_group_names(name)
.send()
.await
.map_err(|e| classify_sdk_error("autoscaling:DescribeAutoScalingGroups", &e))?;
Ok(out
.auto_scaling_groups()
.iter()
.find(|g| g.auto_scaling_group_name() == Some(name))
.map(|g| NamedResource {
status: None,
tags: g
.tags()
.iter()
.filter_map(|t| Some((t.key()?.to_string(), t.value()?.to_string())))
.collect(),
}))
}
}
/// An OpenTofu state that manages nothing (what `tofu destroy` leaves):
/// valid JSON whose `resources` is an empty list. Anything else, unreadable
/// contents included, counts as a deployment.
@@ -754,7 +1103,7 @@ fn error_parts<E: ProvideErrorMetadata>(
}
/// Yes / No (missing) / Unknown (denied) from a lookup's result.
fn knowable<T, E>(
pub(crate) fn knowable<T, E>(
action: &str,
res: Result<T, SdkError<E, HttpResponse>>,
) -> Result<Knowable, AwsError>
@@ -996,6 +1345,10 @@ impl DetectApi for SdkDetectApi {
knowable("iam:GetRole", res)
}
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
get_parameter(&self.ssm, name).await
}
async fn state_document(&self, bucket: &str, key: &str) -> Result<Option<String>, AwsError> {
const ACTION: &str = "s3:GetObject";
let res = self
+212 -5
View File
@@ -44,6 +44,22 @@ fn without(name: &str, field: &str) -> String {
r.to_string()
}
#[test]
fn the_lifetime_cap_is_read_when_present() {
for hours in [0, 8, 168] {
let rec = with("dev", "max_workspace_hours", json!(hours));
let info = parse_environment_record("dev", &rec, ACCOUNT, REGION).unwrap();
assert_eq!(info.max_workspace_hours, hours);
}
}
#[test]
fn a_version_1_record_reads_as_update_available() {
use crate::aws::tofu_modules::ENVIRONMENT_MODULE_VERSION;
let v1 = parsed("dev", 1);
assert!(v1.template_version < ENVIRONMENT_MODULE_VERSION);
}
#[test]
fn untrusted_environment_records_are_rejected() {
let valid = parsed("dev", 1);
@@ -52,6 +68,7 @@ fn untrusted_environment_records_are_rejected() {
assert_eq!(valid.subnet_ids.len(), 2);
assert!(valid.assign_public_ip);
assert_eq!(valid.tenancy, "strict");
assert_eq!(valid.max_workspace_hours, 0, "a v1 record has no cap");
let other_account = "999999999999";
let secret = "s3cr3t-value-never-echoed";
@@ -182,6 +199,21 @@ fn untrusted_environment_records_are_rejected() {
with("dev", "template_version", json!(secret)),
"template_version",
),
(
"cap above a week",
with("dev", "max_workspace_hours", json!(169)),
"max_workspace_hours",
),
(
"cap not a number",
with("dev", "max_workspace_hours", json!(secret)),
"max_workspace_hours",
),
(
"negative cap",
with("dev", "max_workspace_hours", json!(-1)),
"max_workspace_hours",
),
];
for (what, value, field) in cases {
let err = parse_environment_record("dev", &value, ACCOUNT, REGION)
@@ -462,6 +494,8 @@ struct FakeDetect {
roles: HashMap<String, Knowable>,
/// State objects' contents, by key.
documents: HashMap<String, String>,
/// SSM parameters (user records), by name.
parameters: HashMap<String, String>,
calls: Mutex<Vec<String>>,
}
@@ -474,6 +508,7 @@ impl FakeDetect {
boundary: Knowable::Yes,
roles: HashMap::new(),
documents: HashMap::new(),
parameters: HashMap::new(),
calls: Mutex::new(Vec::new()),
}
}
@@ -516,6 +551,10 @@ impl DetectApi for FakeDetect {
self.log(format!("role {name}"));
Ok(self.roles.get(name).cloned().unwrap_or(Knowable::No))
}
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
self.log(format!("parameter {name}"));
Ok(self.parameters.get(name).cloned().map_or(Param::Missing, Param::Value))
}
async fn state_document(&self, bucket: &str, key: &str) -> Result<Option<String>, AwsError> {
self.log(format!("document {bucket} {key}"));
Ok(self.documents.get(key).cloned())
@@ -580,8 +619,8 @@ fn onboarding_is_found_by_state_then_by_role() {
assert_eq!(scan.owner_id, USER_ID);
assert_eq!(scan.owner_key, owner_key(REGION, USER_ID));
let onboarding = |n: &str| find(&scan.environments, n).onboarding.clone();
assert_eq!(onboarding("bystate"), Some(OnboardingState::Joined));
assert_eq!(onboarding("byrole"), Some(OnboardingState::Joined));
assert_eq!(onboarding("bystate"), Some(OnboardingState::Joined { ec2: None }));
assert_eq!(onboarding("byrole"), Some(OnboardingState::Joined { ec2: None }));
assert_eq!(onboarding("denied"), Some(OnboardingState::NotJoined));
assert_eq!(onboarding("absent"), Some(OnboardingState::NotJoined));
assert!(matches!(
@@ -630,7 +669,7 @@ fn onboarding_is_found_by_state_then_by_role() {
assert_eq!(x.onboarding, Some(OnboardingState::InProgress { lock: None }));
assert_eq!(x.onboarding_actions, vec![EnvironmentAction::Follow]);
let y = find(&scan.environments, "y");
let Some(OnboardingState::Incomplete { lock: Some(lock) }) = &y.onboarding else {
let Some(OnboardingState::Incomplete { lock: Some(lock), ec2: None }) = &y.onboarding else {
panic!("{y:#?}");
};
assert!(!lock.local_process);
@@ -950,12 +989,12 @@ fn a_failed_onboarding_is_incomplete_not_joined() {
let scan = run_scan(&d, &local);
let row = |n: &str| find(&scan.environments, n).clone();
assert_eq!(row("done").onboarding, Some(OnboardingState::Joined));
assert_eq!(row("done").onboarding, Some(OnboardingState::Joined { ec2: None }));
for name in ["partial", "failedhere", "errored"] {
let r = row(name);
assert_eq!(
r.onboarding,
Some(OnboardingState::Incomplete { lock: None }),
Some(OnboardingState::Incomplete { lock: None, ec2: None }),
"{name}"
);
// Join is kept: an idempotent re-apply finishes it.
@@ -1075,3 +1114,171 @@ fn the_tls_statement_is_added_to_a_bucket_policy_never_replacing_it() {
// Something that isn't a policy is never overwritten.
assert!(state_bucket_policy_update(Some("not json"), &bucket).is_err());
}
fn user_record(env: &str, tag: &str) -> Value {
let stem = crate::aws::tofu_modules::user_ec2_stem(env, tag);
json!({
"schema": 1,
"instance_type": "m7g.large",
"cluster_arn": format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/{stem}"),
"capacity_provider": stem,
"user_tag": tag,
"max_instances": 3,
})
}
#[test]
fn a_user_record_reads_as_the_users_compute() {
let rec = user_record("dev", "alice");
let c = parse_user_record("dev", &rec.to_string(), ACCOUNT, REGION).unwrap();
assert_eq!(
c,
UserCompute {
instance_type: "m7g.large".into(),
max_instances: 3,
user_tag: "alice".into(),
cluster_arn: format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice"),
capacity_provider: "triple-c-dev--u-alice".into(),
}
);
// A record written before max_instances existed: the module's default.
let mut old = rec.clone();
old.as_object_mut().unwrap().remove("max_instances");
let c = parse_user_record("dev", &old.to_string(), ACCOUNT, REGION).unwrap();
assert_eq!(c.max_instances, 1);
}
#[test]
fn a_user_record_is_untrusted() {
let bad = |field: &str, value: Value, why: &str| {
let mut rec = user_record("dev", "alice");
rec[field] = value;
let err = parse_user_record("dev", &rec.to_string(), ACCOUNT, REGION).unwrap_err();
assert!(err.contains(why), "{field}: {err}");
assert!(!err.contains("evil"), "{err}");
};
bad("schema", json!(2), "schema");
bad("instance_type", json!("p5.48xlarge"), "instance_type");
bad("instance_type", json!(7), "instance_type");
bad("max_instances", json!(0), "max_instances");
bad("max_instances", json!(5), "max_instances");
bad("max_instances", json!("2"), "max_instances");
bad("user_tag", json!("evil--tag"), "user_tag");
// Another user's cluster, or one in another account or region.
bad(
"cluster_arn",
json!(format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-bob")),
"cluster_arn",
);
bad(
"cluster_arn",
json!(format!("arn:aws:ecs:us-east-1:{ACCOUNT}:cluster/triple-c-dev--u-alice")),
"cluster_arn",
);
bad(
"cluster_arn",
json!(format!("arn:aws:ecs:{REGION}:999999999999:cluster/triple-c-dev--u-alice")),
"cluster_arn",
);
bad("capacity_provider", json!("evil"), "capacity_provider");
// Another environment's record.
let rec = user_record("prod", "alice").to_string();
assert!(parse_user_record("dev", &rec, ACCOUNT, REGION).is_err());
for text in ["", "not json", "[]", &"x".repeat(5000)] {
assert!(parse_user_record("dev", text, ACCOUNT, REGION).is_err());
}
}
fn compute_of(s: &OnboardingState) -> Option<&UserCompute> {
match s {
OnboardingState::Joined { ec2 } | OnboardingState::Incomplete { ec2, .. } => ec2.as_ref(),
_ => None,
}
}
#[test]
fn onboarding_carries_the_users_compute_from_their_record() {
use crate::aws::tofu_modules::user_record_parameter_name;
let ok = owner_key(REGION, USER_ID);
let mut d = FakeDetect::new()
.with_record("joined", 1)
.with_record("fargate", 1)
.with_record("broken", 1)
.with_record("partial", 1)
.with_record("absent", 1);
d.roles.insert(task_role("joined"), Knowable::Yes);
d.roles.insert(task_role("fargate"), Knowable::Yes);
d.roles.insert(task_role("broken"), Knowable::Yes);
d.objects = Some(StateObjects {
states: vec![
env_key("joined"),
env_key("fargate"),
env_key("broken"),
env_key("partial"),
env_key("absent"),
user_key("partial"),
],
locks: vec![],
});
d.documents.insert(user_key("partial"), "{\"resources\":[{}]}".into());
for env in ["joined", "partial", "absent"] {
d.parameters.insert(
user_record_parameter_name(env, &ok),
user_record(env, "alice").to_string(),
);
}
d.parameters.insert(
user_record_parameter_name("broken", &ok),
user_record("other", "alice").to_string(),
);
// `joined`'s state is not readable here: found by role.
d.objects.as_mut().unwrap().states.retain(|k| *k != user_key("joined"));
let scan = run_scan(&d, &nobody_local);
let onboarding = |n: &str| find(&scan.environments, n).onboarding.clone().unwrap();
let joined = onboarding("joined");
let compute = compute_of(&joined).expect("joined has compute");
assert_eq!(compute.instance_type, "m7g.large");
assert_eq!(compute.max_instances, 3);
assert_eq!(compute.user_tag, "alice");
assert_eq!(onboarding("fargate"), OnboardingState::Joined { ec2: None });
// A record that doesn't validate is no compute.
assert_eq!(onboarding("broken"), OnboardingState::Joined { ec2: None });
// An incomplete onboarding keeps what its record still says.
let partial = onboarding("partial");
assert!(matches!(partial, OnboardingState::Incomplete { .. }), "{partial:?}");
assert_eq!(compute_of(&partial).map(|c| c.max_instances), Some(3));
// Not joined: the record isn't even read.
assert_eq!(onboarding("absent"), OnboardingState::NotJoined);
assert!(!d
.calls()
.contains(&format!("parameter {}", user_record_parameter_name("absent", &ok))));
// Serialized for the UI: the state tag plus `ec2`.
let v = serde_json::to_value(&joined).unwrap();
assert_eq!(v["state"], "joined");
assert_eq!(v["ec2"]["capacity_provider"], "triple-c-joined--u-alice");
assert_eq!(
serde_json::to_value(OnboardingState::Joined { ec2: None }).unwrap(),
json!({"state": "joined", "ec2": null})
);
}
#[test]
fn a_user_record_read_is_absent_valid_or_unusable() {
let ok = user_record("dev", "alice").to_string();
assert_eq!(user_record_from(Param::Missing, "dev", ACCOUNT, REGION), UserRecord::Absent);
assert!(matches!(
user_record_from(Param::Value(ok), "dev", ACCOUNT, REGION),
UserRecord::Valid(_)
));
// Denied or invalid: there may be capacity, so it is not "absent".
assert!(matches!(
user_record_from(Param::Denied, "dev", ACCOUNT, REGION),
UserRecord::Unusable(_)
));
assert!(matches!(
user_record_from(Param::Value("{}".into()), "dev", ACCOUNT, REGION),
UserRecord::Unusable(_)
));
}
+8 -1
View File
@@ -29,6 +29,11 @@ pub enum AwsError {
StateBucketTaken {
bucket: String,
},
/// An account limit (Fargate vCPUs, tasks, ...) refused the request.
/// Never retried; `message` says what to do.
QuotaExceeded {
message: String,
},
Other {
message: String,
},
@@ -63,7 +68,9 @@ impl std::fmt::Display for AwsError {
"The state bucket name \"{bucket}\" is already taken by another AWS account, so \
Triple-C can't keep OpenTofu state there"
),
AwsError::Other { message } => f.write_str(message),
AwsError::QuotaExceeded { message } | AwsError::Other { message } => {
f.write_str(message)
}
}
}
}
File diff suppressed because it is too large. Load diff
+4
View File
@@ -11,10 +11,14 @@ mod identity;
mod live_tests;
pub mod permissions;
pub mod policies;
pub mod pricing;
#[cfg(test)]
mod policy_eval;
pub mod runtime;
pub mod tasks;
pub mod tofu;
pub mod tofu_modules;
pub mod workspace;
pub use connection::{sdk_config, sdk_config_for_keys};
pub use error::{classify_sdk_error, AwsError};
+31 -1
View File
@@ -37,8 +37,20 @@ impl ContextEntry {
pub fn is_list(&self) -> bool {
self.values.len() > 1
}
/// Keys IAM types as Boolean (a `Bool` condition does not match a
/// String-typed simulation value).
pub fn is_boolean(&self) -> bool {
BOOLEAN_KEYS.contains(&self.key.as_str())
}
}
/// The Boolean condition keys the checks send.
const BOOLEAN_KEYS: &[&str] = &[
"autoscaling:LaunchTemplateVersionSpecified",
"ec2:IsLaunchTemplateResource",
];
/// Builds the simulation for `check`: the resource and every literal context
/// value rendered with `p`, `OwnerId` as `user_id`, and `aws:userid` =
/// `user_id` added. A key appearing more than once becomes one entry holding
@@ -135,8 +147,13 @@ impl SdkPermissionApi {
// hundred simulations) failed a third of its rows with "Rate
// exceeded" under standard retries (verified live). Adaptive retry
// backs off client-side once throttled, with room to retry.
//
// The connection's 30 s operation limit counts the retries' backoff
// too, which would cut those ten attempts to about six; the check
// gets its own, longer limit, and the UI says it can take a while.
let iam = aws_sdk_iam::config::Builder::from(cfg)
.retry_config(aws_config::retry::RetryConfig::adaptive().with_max_attempts(10))
.timeout_config(iam_check_timeouts())
.build();
SdkPermissionApi {
iam: aws_sdk_iam::Client::from_conf(iam),
@@ -147,6 +164,17 @@ impl SdkPermissionApi {
}
}
/// The whole of one simulation, retries and backoff included. Ten adaptive
/// attempts with backoff capped at 20 s fit in two minutes.
pub const IAM_CHECK_OPERATION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(120);
fn iam_check_timeouts() -> aws_config::timeout::TimeoutConfig {
aws_config::timeout::TimeoutConfig::builder()
.operation_timeout(IAM_CHECK_OPERATION_TIMEOUT)
.operation_attempt_timeout(super::connection::ATTEMPT_TIMEOUT)
.build()
}
impl PermissionApi for SdkPermissionApi {
async fn resolve_principal(&self, principal: PrincipalArn) -> Result<String, AwsError> {
identity::resolve_principal(&self.iam, principal).await
@@ -159,7 +187,9 @@ impl PermissionApi for SdkPermissionApi {
SdkEntry::builder()
.context_key_name(&e.key)
.set_context_key_values(Some(e.values.clone()))
.context_key_type(if e.is_list() {
.context_key_type(if e.is_boolean() {
ContextKeyTypeEnum::Boolean
} else if e.is_list() {
ContextKeyTypeEnum::StringList
} else {
ContextKeyTypeEnum::String
@@ -344,6 +344,29 @@ fn a_repeated_context_key_is_one_string_list_entry() {
);
assert!(req.context[0].is_list());
assert!(!req.context[1].is_list());
assert!(!req.context[1].is_boolean());
}
/// Every Boolean-typed key a check sends is one `is_boolean` knows, so the
/// simulator gets it as Boolean (a `Bool` condition never matches a String).
#[test]
fn boolean_context_keys_are_sent_as_booleans() {
let mut seen = 0;
for tier in [Tier::WorkspaceUser, Tier::EnvironmentAdmin] {
for c in checks(tier) {
for (key, value) in c.context {
if matches!(value, ContextValue::Literal("true" | "false")) {
let e = ContextEntry {
key: key.to_string(),
values: vec!["true".into()],
};
assert!(e.is_boolean(), "{key} is sent as a String");
seen += 1;
}
}
}
}
assert!(seen >= 2, "only {seen} Boolean keys");
}
#[test]
@@ -628,3 +651,13 @@ fn no_probe_is_cloudformation() {
}
assert_eq!(Probe::for_action("cloudformation:DescribeStacks"), None);
}
#[test]
fn the_iam_check_gets_room_for_its_throttled_retries() {
let t = iam_check_timeouts();
assert_eq!(t.operation_timeout(), Some(IAM_CHECK_OPERATION_TIMEOUT));
assert!(IAM_CHECK_OPERATION_TIMEOUT > crate::aws::connection::OPERATION_TIMEOUT);
// Ten adaptive attempts, backoff capped at 20 s, fit inside it.
assert!(IAM_CHECK_OPERATION_TIMEOUT >= std::time::Duration::from_secs(9 * 20 / 2 + 20));
assert_eq!(t.operation_attempt_timeout(), Some(crate::aws::connection::ATTEMPT_TIMEOUT));
}
File diff suppressed because it is too large. Load diff
+34 -2
View File
@@ -208,10 +208,11 @@ pub(crate) fn conditions_hold(stmt: &Value, req: &Request) -> bool {
// A negated operator holds when the key is absent, and
// otherwise when no value matches.
let (matches, negated): (fn(&str, &str) -> bool, bool) = match base {
"StringEquals" => (|p, v| p == v, false),
// IAM's Bool compares "true"/"false"; the checks send strings.
"StringEquals" | "Bool" => (|p, v| p == v, false),
"StringLike" | "ArnLike" => (glob, false),
"StringNotEquals" => (|p, v| p == v, true),
"StringNotLike" => (glob, true),
"StringNotLike" | "ArnNotLike" => (glob, true),
other => panic!("{}: unsupported condition operator {other}", sid(stmt)),
};
entries
@@ -268,6 +269,37 @@ mod tests {
assert!(!glob(&literal, "arn:aws:ec2:us-west-2:1:elastic-ip/eipalloc-1"));
}
#[test]
fn bool_needs_the_key_and_bool_if_exists_does_not() {
let stmt = |op: &str| -> Value {
serde_json::json!({
"Sid": "T", "Effect": "Allow", "Action": "x:Y", "Resource": "*",
"Condition": { op: { "k:Pinned": "true" } }
})
};
let holds = |op: &str, context| {
let c = Check {
action: "x:Y",
resource: "*",
context,
write: true,
};
conditions_hold(
&stmt(op),
&Request {
check: &c,
caller: ME,
owner: ME,
},
)
};
assert!(holds("Bool", &[("k:Pinned", Literal("true"))]));
assert!(!holds("Bool", &[("k:Pinned", Literal("false"))]));
assert!(!holds("Bool", &[]));
assert!(holds("BoolIfExists", &[]));
assert!(!holds("BoolIfExists", &[("k:Pinned", Literal("false"))]));
}
#[test]
fn for_all_values_is_vacuous_on_an_absent_key() {
let stmt: Value = serde_json::json!({
+684
View File
@@ -0,0 +1,684 @@
//! The estimated hourly cost of a Fargate workspace (Q5): on-demand rates
//! from the Price List API (`pricing:GetProducts`, service `AmazonECS`,
//! served from us-east-1 only), cached on disk per region for a day. An EC2
//! workspace (phase 7 E10) is priced as its instance: the `AmazonEC2`
//! on-demand Linux, shared-tenancy price of the user's instance type, cached
//! per region and type the same way.
//!
//! Everything here is best effort. A denied call, no network, a slow answer
//! or a price list we can't read all end in "no estimate" (`None`), never an
//! error: the estimate is a hint next to the settings, not something a start
//! depends on.
use std::collections::HashMap;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::models::project::{Arch, AwsWorkspaceConfig, StorageMode};
/// The Price List API's endpoint region (it also has ap-south-1 and
/// eu-central-1; any of them serves every region's prices).
pub const PRICING_REGION: &str = "us-east-1";
pub const SERVICE_CODE: &str = "AmazonECS";
/// EC2 instances' prices (phase 7 E10).
pub const EC2_SERVICE_CODE: &str = "AmazonEC2";
/// How long a region's rates are used before they are fetched again.
pub const CACHE_TTL: Duration = Duration::from_secs(24 * 60 * 60);
/// The whole fetch (every page), so the command answers quickly offline.
pub const FETCH_TIMEOUT: Duration = Duration::from_secs(8);
/// After a failed fetch, the region is not asked again for this long (the
/// settings call the estimate on every change).
pub const RETRY_AFTER_FAILURE: Duration = Duration::from_secs(5 * 60);
/// Fargate includes 20 GiB of ephemeral storage; only the rest is billed.
pub const FREE_EPHEMERAL_GIB: u32 = 20;
/// The filtered list is 8 rows (one page); more pages than this is not a
/// price list we understand.
const MAX_PAGES: usize = 5;
/// `GetProducts` filters as (type, field, value); `{region}` is the
/// workspace's region. Verified against the live API (2026-11-16): eight
/// rows per region — Linux x86 and ARM vCPU-hours and GB-hours, ephemeral
/// storage GB-hours and three Windows rows, which the parser skips.
pub const FILTERS: &[(&str, &str, &str)] = &[
("TERM_MATCH", "regionCode", "{region}"),
("TERM_MATCH", "termType", "OnDemand"),
("CONTAINS", "usagetype", "Fargate"),
];
/// `GetProducts` filters for one EC2 instance type's on-demand Linux price
/// (`{region}`, `{instance_type}` filled in). Verified against the live API
/// (2026-11-16): exactly one row per type and region (`USW2-BoxUsage:m7i.large`,
/// $0.1008/hour), carrying both its OnDemand and Reserved terms.
pub const EC2_FILTERS: &[(&str, &str, &str)] = &[
("TERM_MATCH", "regionCode", "{region}"),
("TERM_MATCH", "instanceType", "{instance_type}"),
("TERM_MATCH", "operatingSystem", "Linux"),
("TERM_MATCH", "tenancy", "Shared"),
("TERM_MATCH", "preInstalledSw", "NA"),
("TERM_MATCH", "capacitystatus", "Used"),
("TERM_MATCH", "licenseModel", "No License required"),
];
/// `GetProducts` filters for gp3 EBS storage in `{region}`. Verified against
/// the live API (2026-11-16): one row (`USW2-EBS:VolumeUsage.gp3`, $0.08 per
/// GB-month).
pub const GP3_FILTERS: &[(&str, &str, &str)] = &[
("TERM_MATCH", "regionCode", "{region}"),
("TERM_MATCH", "productFamily", "Storage"),
("TERM_MATCH", "volumeApiName", "gp3"),
];
/// The EC2 instance's root volume (`ec2.tf`'s `volume_size`, gp3), billed
/// while the instance exists.
pub const EC2_ROOT_VOLUME_GIB: u32 = 50;
/// Hours in a billing month, as AWS converts GB-month prices.
pub const HOURS_PER_MONTH: f64 = 730.0;
/// One `GetProducts` query: a service and its filters, values filled in.
#[derive(Debug, Clone, PartialEq)]
pub struct Query {
pub service_code: &'static str,
pub filters: Vec<(&'static str, &'static str, String)>,
}
fn fill(filters: &[(&'static str, &'static str, &str)], vars: &[(&str, &str)]) -> Vec<(&'static str, &'static str, String)> {
filters
.iter()
.map(|(kind, field, value)| {
let value = vars
.iter()
.fold(value.to_string(), |v, (name, with)| v.replace(&format!("{{{name}}}"), with));
(*kind, *field, value)
})
.collect()
}
/// The Fargate rates of `region` ([`FILTERS`]).
pub fn fargate_query(region: &str) -> Query {
Query {
service_code: SERVICE_CODE,
filters: fill(FILTERS, &[("region", region)]),
}
}
/// The on-demand price of `instance_type` in `region` ([`EC2_FILTERS`]).
pub fn ec2_query(region: &str, instance_type: &str) -> Query {
Query {
service_code: EC2_SERVICE_CODE,
filters: fill(EC2_FILTERS, &[("region", region), ("instance_type", instance_type)]),
}
}
/// The gp3 storage price of `region` ([`GP3_FILTERS`]).
pub fn gp3_query(region: &str) -> Query {
Query {
service_code: EC2_SERVICE_CODE,
filters: fill(GP3_FILTERS, &[("region", region)]),
}
}
/// One architecture's on-demand rates in a region, in USD.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct FargateRates {
pub vcpu_hour: f64,
pub gb_hour: f64,
pub storage_gb_hour: f64,
}
/// A region's rates per architecture. An architecture the price list did
/// not fully cover is `None`.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct RegionRates {
pub x86_64: Option<FargateRates>,
pub arm64: Option<FargateRates>,
}
impl RegionRates {
pub fn for_arch(&self, arch: Arch) -> Option<FargateRates> {
match arch {
Arch::X86_64 => self.x86_64,
Arch::Arm64 => self.arm64,
}
}
}
/// What the UI shows: the on-demand price per hour of the task while it
/// runs. A gateway sidecar shares the task's CPU and memory, so it adds
/// nothing; Spot isn't in the Price List API (`note_spot`: say it is
/// usually cheaper); EFS is billed per GB-month, not per hour
/// (`efs_billed_separately`). `ec2_instance`: the figure is the user's EC2
/// instance's hourly price, whatever the project reserves of it, and
/// instances scale to zero when no workspace is running (E10);
/// `root_volume_included`: it also has the instance's 50 GiB gp3 root
/// volume (when its price could be read). An instance's public IPv4
/// address (public-mode environments) is never in it.
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct HourlyEstimate {
pub usd_per_hour: f64,
pub note_spot: bool,
pub efs_billed_separately: bool,
pub ec2_instance: bool,
pub root_volume_included: bool,
}
pub fn estimate(cfg: &AwsWorkspaceConfig, rates: &FargateRates) -> HourlyEstimate {
let vcpu = f64::from(cfg.cpu) / 1024.0;
let gb = f64::from(cfg.memory_mib) / 1024.0;
let billed_storage = f64::from(cfg.ephemeral_gib.saturating_sub(FREE_EPHEMERAL_GIB));
HourlyEstimate {
usd_per_hour: vcpu * rates.vcpu_hour
+ gb * rates.gb_hour
+ billed_storage * rates.storage_gb_hour,
note_spot: cfg.spot,
efs_billed_separately: cfg.storage != StorageMode::Ephemeral,
ec2_instance: false,
root_volume_included: false,
}
}
/// An EC2 project's estimate: its instance's on-demand hourly price — the
/// instance is billed whole while it runs, so the reservations, the
/// ephemeral size and Spot (none on EC2) change nothing — plus its
/// [`EC2_ROOT_VOLUME_GIB`] gp3 root volume at `gp3_usd_per_gb_month` (per
/// hour, over [`HOURS_PER_MONTH`]) when that price is known.
pub fn estimate_ec2(
cfg: &AwsWorkspaceConfig,
instance_usd_per_hour: f64,
gp3_usd_per_gb_month: Option<f64>,
) -> HourlyEstimate {
let root = gp3_usd_per_gb_month.map(|p| f64::from(EC2_ROOT_VOLUME_GIB) * p / HOURS_PER_MONTH);
HourlyEstimate {
usd_per_hour: instance_usd_per_hour + root.unwrap_or(0.0),
note_spot: false,
efs_billed_separately: cfg.storage != StorageMode::Ephemeral,
ec2_instance: true,
root_volume_included: root.is_some(),
}
}
/// The gp3 storage price per GB-month in a [`GP3_FILTERS`] price list for
/// `region`; `None` when no row matches.
pub fn parse_gp3_price(region: &str, items: &[String]) -> Option<f64> {
items.iter().find_map(|item| {
let product = serde_json::from_str::<Value>(item).ok()?;
let attrs = &product["product"]["attributes"];
let is = |k: &str, v: &str| attrs[k].as_str() == Some(v);
if !(is("regionCode", region) && is("volumeApiName", "gp3")) {
return None;
}
on_demand_usd(&product).filter(|p| *p > 0.0)
})
}
/// The on-demand hourly price of `instance_type` (Linux, shared tenancy) in
/// an `AmazonEC2` price list for `region`. The filters already narrow it to
/// one row; the attributes are checked again so a looser answer can't price
/// the wrong product. `None` when no row matches.
pub fn parse_ec2_price(region: &str, instance_type: &str, items: &[String]) -> Option<f64> {
items.iter().find_map(|item| {
let product = serde_json::from_str::<Value>(item).ok()?;
let attrs = &product["product"]["attributes"];
let is = |k: &str, v: &str| attrs[k].as_str() == Some(v);
let matches = is("regionCode", region)
&& is("instanceType", instance_type)
&& is("operatingSystem", "Linux")
&& is("tenancy", "Shared")
&& is("preInstalledSw", "NA")
&& is("capacitystatus", "Used")
&& is("licenseModel", "No License required");
if !matches {
return None;
}
on_demand_usd(&product).filter(|p| *p > 0.0)
})
}
/// Whether `instance_type` is safe in a cache file name and a filter value
/// (`m7i.large`): lowercase letters, digits, one dot.
fn plain_instance_type(instance_type: &str) -> bool {
instance_type.split_once('.').is_some_and(|(family, size)| {
let ok = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit());
ok(family) && ok(size)
}) && instance_type.len() <= 32
}
/// The rates in a `GetProducts` price list (each item one product's JSON)
/// for `region`. `None` when no architecture is complete.
pub fn parse_price_list(region: &str, items: &[String]) -> Option<RegionRates> {
#[derive(Default)]
struct Partial {
vcpu: Option<f64>,
gb: Option<f64>,
}
let (mut x86, mut arm, mut storage) = (Partial::default(), Partial::default(), None);
for item in items {
let Ok(product) = serde_json::from_str::<Value>(item) else {
continue;
};
let attrs = &product["product"]["attributes"];
if attrs["regionCode"].as_str() != Some(region) {
continue;
}
if attrs["operatingSystem"]
.as_str()
.is_some_and(|os| os != "Linux")
{
continue;
}
let Some(usage) = attrs["usagetype"].as_str() else {
continue;
};
// `USW2-Fargate-ARM-vCPU-Hours:perCPU`; us-east-1's prefix is
// `USE1-`, Ireland's `EU-`.
let Some((_, kind)) = usage.split_once("Fargate-") else {
continue;
};
let slot = match kind {
"vCPU-Hours:perCPU" => &mut x86.vcpu,
"GB-Hours" => &mut x86.gb,
"ARM-vCPU-Hours:perCPU" => &mut arm.vcpu,
"ARM-GB-Hours" => &mut arm.gb,
"EphemeralStorage-GB-Hours" => &mut storage,
_ => continue,
};
if slot.is_none() {
*slot = on_demand_usd(&product);
}
}
let storage = storage?;
let complete = |p: Partial| {
Some(FargateRates {
vcpu_hour: p.vcpu?,
gb_hour: p.gb?,
storage_gb_hour: storage,
})
};
let rates = RegionRates {
x86_64: complete(x86),
arm64: complete(arm),
};
(rates.x86_64.is_some() || rates.arm64.is_some()).then_some(rates)
}
/// The product's on-demand USD price per unit. A price list gives one term
/// with one dimension for these rows; anything not a sane non-negative
/// number is ignored.
fn on_demand_usd(product: &Value) -> Option<f64> {
product["terms"]["OnDemand"]
.as_object()?
.values()
.filter_map(|term| term["priceDimensions"].as_object())
.flat_map(|dims| dims.values())
.filter_map(|d| d["pricePerUnit"]["USD"].as_str()?.parse::<f64>().ok())
.find(|p| p.is_finite() && (0.0..100.0).contains(p))
}
/// One page of `GetProducts`.
pub struct Page {
pub items: Vec<String>,
pub next_token: Option<String>,
}
/// The Price List API, faked in tests.
pub trait PricingApi {
/// One page of the products `query` matches.
fn get_products(
&self,
query: &Query,
next_token: Option<String>,
) -> impl Future<Output = Result<Page, String>> + Send;
}
/// The real thing: a client in [`PRICING_REGION`] from a connection's
/// `SdkConfig` (`aws::sdk_config(conn, PRICING_REGION)`).
pub struct SdkPricingApi {
client: aws_sdk_pricing::Client,
}
impl SdkPricingApi {
pub fn new(cfg: &aws_config::SdkConfig) -> Self {
Self {
client: aws_sdk_pricing::Client::new(cfg),
}
}
}
impl PricingApi for SdkPricingApi {
async fn get_products(&self, query: &Query, next_token: Option<String>) -> Result<Page, String> {
use aws_sdk_pricing::types::{Filter, FilterType};
let mut req = self
.client
.get_products()
.service_code(query.service_code)
.format_version("aws_v1")
.max_results(100)
.set_next_token(next_token);
for (kind, field, value) in &query.filters {
let filter = Filter::builder()
.r#type(FilterType::from(*kind))
.field(*field)
.value(value)
.build()
.map_err(|e| e.to_string())?;
req = req.filters(filter);
}
let out = req
.send()
.await
.map_err(|e| super::classify_sdk_error("pricing:GetProducts", &e).to_string())?;
Ok(Page {
items: out.price_list().to_vec(),
next_token: out.next_token().map(str::to_string),
})
}
}
/// Every page `query` matches.
async fn fetch_items<A: PricingApi>(api: &A, query: &Query) -> Result<Vec<String>, String> {
let mut items = Vec::new();
let mut token = None;
for _ in 0..MAX_PAGES {
let page = api.get_products(query, token).await?;
items.extend(page.items);
token = page.next_token.filter(|t| !t.is_empty());
if token.is_none() {
return Ok(items);
}
}
Err(format!("the {} price list has more than {MAX_PAGES} pages", query.service_code))
}
/// Every page for `region`, parsed (the live tests' direct call).
#[cfg_attr(not(test), allow(dead_code))]
pub async fn fetch_rates<A: PricingApi>(api: &A, region: &str) -> Result<RegionRates, String> {
let items = fetch_items(api, &fargate_query(region)).await?;
parse_price_list(region, &items).ok_or_else(|| format!("no Fargate rates for {region} in the price list"))
}
/// The on-demand hourly price of `instance_type` in `region` (the live
/// tests' direct call).
#[cfg_attr(not(test), allow(dead_code))]
pub async fn fetch_ec2_price<A: PricingApi>(api: &A, region: &str, instance_type: &str) -> Result<f64, String> {
if !plain_instance_type(instance_type) {
return Err(format!("{instance_type:?} is not an instance type"));
}
let items = fetch_items(api, &ec2_query(region, instance_type)).await?;
parse_ec2_price(region, instance_type, &items)
.ok_or_else(|| format!("no {instance_type} price for {region} in the price list"))
}
/// One cached answer: Fargate rates under `<region>.json`, an instance's
/// price under `<region>--ec2-<type>.json`. `key` is what the file must say
/// it holds (the region, or `<region> <type>`), and what failures are
/// remembered under.
struct CacheKey {
key: String,
file: String,
}
impl CacheKey {
fn fargate(region: &str) -> Self {
Self {
key: region.to_string(),
file: format!("{region}.json"),
}
}
fn ec2(region: &str, instance_type: &str) -> Self {
Self {
key: format!("{region} {instance_type}"),
file: format!("{region}--ec2-{instance_type}.json"),
}
}
fn gp3(region: &str) -> Self {
Self {
key: format!("{region} gp3"),
file: format!("{region}--ebs-gp3.json"),
}
}
}
/// A cache file. `key` was `region` before EC2 prices were cached too (an
/// older file still reads).
#[derive(Debug, Serialize, Deserialize)]
struct CacheFile<V> {
#[serde(alias = "region")]
key: String,
/// Unix seconds.
fetched_at: u64,
rates: V,
}
/// The per-region rate files and the memory of recent failures.
pub struct RatesCache {
dir: PathBuf,
failed_at: Mutex<HashMap<String, SystemTime>>,
}
/// `<data dir>/triple-c/aws-pricing/`.
pub fn default_cache_dir() -> Option<PathBuf> {
Some(dirs::data_dir()?.join("triple-c").join("aws-pricing"))
}
fn unix_secs(t: SystemTime) -> u64 {
t.duration_since(UNIX_EPOCH).map_or(0, |d| d.as_secs())
}
impl RatesCache {
pub fn new(dir: PathBuf) -> Self {
Self {
dir,
failed_at: Mutex::new(HashMap::new()),
}
}
#[cfg(test)]
pub fn dir(&self) -> &Path {
&self.dir
}
/// The cached rates and whether they are still fresh at `now`. A
/// missing, unreadable or corrupt file, or one for another region, is
/// no cache. The region must already be validated (it names a file).
pub fn read(&self, region: &str, now: SystemTime) -> Option<(RegionRates, bool)> {
self.read_key(&CacheKey::fargate(region), now)
}
#[cfg(test)]
pub fn write(&self, region: &str, rates: &RegionRates, now: SystemTime) -> Result<(), String> {
self.write_key(&CacheKey::fargate(region), rates, now)
}
/// [`Self::read`] for an instance type's hourly price.
pub fn read_ec2(&self, region: &str, instance_type: &str, now: SystemTime) -> Option<(f64, bool)> {
if !plain_instance_type(instance_type) {
return None;
}
self.read_key(&CacheKey::ec2(region, instance_type), now)
}
#[cfg(test)]
pub fn write_ec2(&self, region: &str, instance_type: &str, usd_per_hour: f64, now: SystemTime) -> Result<(), String> {
if !plain_instance_type(instance_type) {
return Err(format!("{instance_type:?} is not an instance type"));
}
self.write_key(&CacheKey::ec2(region, instance_type), &usd_per_hour, now)
}
fn read_key<V: DeserializeOwned>(&self, key: &CacheKey, now: SystemTime) -> Option<(V, bool)> {
let text = std::fs::read_to_string(self.dir.join(&key.file)).ok()?;
let file: CacheFile<V> = serde_json::from_str(&text).ok()?;
if file.key != key.key {
return None;
}
let now = unix_secs(now);
// A timestamp from the future (a clock change) counts as stale.
let fresh = file.fetched_at <= now && now - file.fetched_at < CACHE_TTL.as_secs();
Some((file.rates, fresh))
}
fn write_key<V: Serialize>(&self, key: &CacheKey, rates: &V, now: SystemTime) -> Result<(), String> {
std::fs::create_dir_all(&self.dir).map_err(|e| e.to_string())?;
let file = CacheFile {
key: key.key.clone(),
fetched_at: unix_secs(now),
rates,
};
let text = serde_json::to_string_pretty(&file).map_err(|e| e.to_string())?;
// Write then rename, so a concurrent reader never sees half a file.
let tmp = self
.dir
.join(format!(".{}.tmp-{}", key.file, std::process::id()));
std::fs::write(&tmp, text).map_err(|e| e.to_string())?;
std::fs::rename(&tmp, self.dir.join(&key.file)).map_err(|e| {
let _ = std::fs::remove_file(&tmp);
e.to_string()
})
}
fn recently_failed(&self, region: &str, now: SystemTime) -> bool {
let map = self.failed_at.lock().unwrap_or_else(|e| e.into_inner());
map.get(region).is_some_and(|t| {
now.duration_since(*t)
.is_ok_and(|age| age < RETRY_AFTER_FAILURE)
})
}
fn note_failure(&self, region: &str, now: SystemTime, failed: bool) {
let mut map = self.failed_at.lock().unwrap_or_else(|e| e.into_inner());
if failed {
map.insert(region.to_string(), now);
} else {
map.remove(region);
}
}
/// Fresh cached rates, else a fetch (bounded by [`FETCH_TIMEOUT`])
/// that refreshes the cache, else the stale cache, else `None`. `api`
/// is `None` when no client could be built (the stale cache still
/// answers). A region whose fetch failed is not asked again for
/// [`RETRY_AFTER_FAILURE`].
pub async fn rates<A: PricingApi>(
&self,
api: Option<&A>,
region: &str,
now: SystemTime,
) -> Option<RegionRates> {
self.rates_with::<A, _>(async { api }, region, now).await
}
/// [`Self::rates`] with the client built by `api`, which is awaited only
/// when the cache can't answer, and within the same [`FETCH_TIMEOUT`] as
/// the fetch (building one reads credentials, which can hang too).
pub async fn rates_with<A: PricingApi, B: std::borrow::Borrow<A>>(
&self,
api: impl Future<Output = Option<B>>,
region: &str,
now: SystemTime,
) -> Option<RegionRates> {
let parse = |items: &[String]| {
parse_price_list(region, items).ok_or_else(|| format!("no Fargate rates for {region} in the price list"))
};
self.cached::<A, B, _>(&CacheKey::fargate(region), api, fargate_query(region), parse, now)
.await
}
/// [`Self::rates_with`] for `instance_type`'s on-demand hourly price in
/// `region` (E10): the same cache rules, failure back-off and timeout.
pub async fn ec2_price_with<A: PricingApi, B: std::borrow::Borrow<A>>(
&self,
api: impl Future<Output = Option<B>>,
region: &str,
instance_type: &str,
now: SystemTime,
) -> Option<f64> {
if !plain_instance_type(instance_type) {
return None;
}
let parse = |items: &[String]| {
parse_ec2_price(region, instance_type, items)
.ok_or_else(|| format!("no {instance_type} price for {region} in the price list"))
};
let query = ec2_query(region, instance_type);
self.cached::<A, B, _>(&CacheKey::ec2(region, instance_type), api, query, parse, now)
.await
}
/// [`Self::rates_with`] for `region`'s gp3 storage price per GB-month
/// (an EC2 instance's root volume).
pub async fn gp3_price_with<A: PricingApi, B: std::borrow::Borrow<A>>(
&self,
api: impl Future<Output = Option<B>>,
region: &str,
now: SystemTime,
) -> Option<f64> {
let parse = |items: &[String]| {
parse_gp3_price(region, items).ok_or_else(|| format!("no gp3 price for {region} in the price list"))
};
self.cached::<A, B, _>(&CacheKey::gp3(region), api, gp3_query(region), parse, now)
.await
}
/// Fresh cache, else a fetch of `query` (client build included, within
/// [`FETCH_TIMEOUT`]) parsed by `parse` that refreshes the cache, else
/// the stale cache, else `None`; a key whose fetch failed is not asked
/// again for [`RETRY_AFTER_FAILURE`].
async fn cached<A: PricingApi, B: std::borrow::Borrow<A>, V: Serialize + DeserializeOwned + Clone>(
&self,
key: &CacheKey,
api: impl Future<Output = Option<B>>,
query: Query,
parse: impl FnOnce(&[String]) -> Result<V, String>,
now: SystemTime,
) -> Option<V> {
let cached = self.read_key::<V>(key, now);
let stale = match cached {
Some((rates, true)) => return Some(rates),
Some((rates, false)) => Some(rates),
None => None,
};
let what = &key.key;
if self.recently_failed(what, now) {
return stale;
}
let fetch = async {
let api = api.await?;
Some(fetch_items(api.borrow(), &query).await.and_then(|items| parse(&items)))
};
let fetched = match tokio::time::timeout(FETCH_TIMEOUT, fetch).await {
Ok(Some(r)) => r,
// No client could be built: a failure like any other (building
// one reads credentials, which can be slow), and the stale cache
// answers.
Ok(None) => {
log::info!("No prices for {what}: no Pricing client could be built");
self.note_failure(what, now, true);
return stale;
}
Err(_) => Err(format!("timed out after {}s", FETCH_TIMEOUT.as_secs())),
};
match fetched {
Ok(rates) => {
self.note_failure(what, now, false);
if let Err(e) = self.write_key(key, &rates, now) {
log::warn!("Could not cache the prices for {what}: {e}");
}
Some(rates)
}
Err(e) => {
log::info!("No prices for {what}: {e}");
self.note_failure(what, now, true);
stale
}
}
}
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,6 @@
{
"PriceList": [
"{\"product\":{\"productFamily\":\"Storage\",\"attributes\":{\"maxThroughputvolume\":\"2000 MiB/s\",\"volumeType\":\"General Purpose\",\"maxIopsvolume\":\"80000\",\"usagetype\":\"USW2-EBS:VolumeUsage.gp3\",\"locationType\":\"AWS Region\",\"maxVolumeSize\":\"64 TiB\",\"storageMedia\":\"SSD-backed\",\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonEC2\",\"volumeApiName\":\"gp3\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Compute Cloud\",\"operation\":\"\",\"group\":\"EBS Storage\"},\"sku\":\"BB8UJWJ4XPFJB95G\"},\"serviceCode\":\"AmazonEC2\",\"terms\":{\"OnDemand\":{\"BB8UJWJ4XPFJB95G.JRTCKXETXF\":{\"priceDimensions\":{\"BB8UJWJ4XPFJB95G.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"GB-Mo\",\"endRange\":\"Inf\",\"description\":\"$0.08 per GB-month of General Purpose (gp3) provisioned storage - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"BB8UJWJ4XPFJB95G.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0800000000\"}}},\"sku\":\"BB8UJWJ4XPFJB95G\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261114012926\",\"publicationDate\":\"2026-11-14T01:29:26Z\"}"
],
"FormatVersion": "aws_v1"
}
@@ -0,0 +1,7 @@
{
"FormatVersion": "aws_v1",
"PriceList": [
"{\"product\":{\"productFamily\":\"Compute Instance\",\"attributes\":{\"enhancedNetworkingSupported\":\"Yes\",\"intelTurboAvailable\":\"Yes\",\"memory\":\"8 GiB\",\"dedicatedEbsThroughput\":\"Up to 10000 Mbps\",\"vcpu\":\"2\",\"classicnetworkingsupport\":\"false\",\"capacitystatus\":\"Used\",\"locationType\":\"AWS Region\",\"storage\":\"EBS only\",\"instanceFamily\":\"General purpose\",\"operatingSystem\":\"Linux\",\"intelAvx2Available\":\"Yes\",\"instanceFamilyCategory\":\"General Purpose\",\"regionCode\":\"us-west-2\",\"physicalProcessor\":\"Intel Xeon Scalable (Sapphire Rapids)\",\"clockSpeed\":\"3.2 GHz\",\"ecu\":\"NA\",\"networkPerformance\":\"Up to 12500 Megabit\",\"servicename\":\"Amazon Elastic Compute Cloud\",\"gpuMemory\":\"NA\",\"dedicatedEbsThroughputDescription\":\"650 Mbps\",\"vpcnetworkingsupport\":\"true\",\"instanceType\":\"m7i.large\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-BoxUsage:m7i.large\",\"normalizationSizeFactor\":\"4\",\"intelAvxAvailable\":\"Yes\",\"processorFeatures\":\"Intel AVX; Intel AVX2; Intel AVX512; Intel Turbo; Intel AMX\",\"servicecode\":\"AmazonEC2\",\"licenseModel\":\"No License required\",\"currentGeneration\":\"Yes\",\"preInstalledSw\":\"NA\",\"location\":\"US West (Oregon)\",\"processorArchitecture\":\"64-bit\",\"marketoption\":\"OnDemand\",\"operation\":\"RunInstances\",\"availabilityzone\":\"NA\"},\"sku\":\"76SYQ5S6SHTVH6CA\"},\"serviceCode\":\"AmazonEC2\",\"terms\":{\"OnDemand\":{\"76SYQ5S6SHTVH6CA.JRTCKXETXF\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"$0.1008 per On Demand Linux m7i.large Instance Hour\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.1008000000\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}},\"Reserved\":{\"76SYQ5S6SHTVH6CA.VJWZNREJX2\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.VJWZNREJX2.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"USD 0.0 per Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.VJWZNREJX2.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0000000000\"}},\"76SYQ5S6SHTVH6CA.VJWZNREJX2.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.VJWZNREJX2.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"605\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:11Z\",\"offerTermCode\":\"VJWZNREJX2\",\"termAttributes\":{\"LeaseContractLength\":\"1yr\",\"OfferingClass\":\"convertible\",\"PurchaseOption\":\"All Upfront\"}},\"76SYQ5S6SHTVH6CA.BPH4J8HBKS\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.BPH4J8HBKS.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.BPH4J8HBKS.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0457200000\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:09Z\",\"offerTermCode\":\"BPH4J8HBKS\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"No Upfront\"}},\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"USD 0.0 per Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0000000000\"}},\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"1046\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:12Z\",\"offerTermCode\":\"NQ3QZPMQV9\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"All Upfront\"}},\"76SYQ5S6SHTVH6CA.HU7G6KETJZ\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0317500000\"}},\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"278\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:08Z\",\"offerTermCode\":\"HU7G6KETJZ\",\"termAttributes\":{\"LeaseContractLength\":\"1yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"Partial Upfront\"}},\"76SYQ5S6SHTVH6CA.38NPMPTW36\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.Line truncated
"{\"product\":{\"productFamily\":\"Compute Instance\",\"attributes\":{\"enhancedNetworkingSupported\":\"Yes\",\"intelTurboAvailable\":\"No\",\"memory\":\"8 GiB\",\"dedicatedEbsThroughput\":\"Up to 10000 Mbps\",\"vcpu\":\"2\",\"classicnetworkingsupport\":\"false\",\"capacitystatus\":\"Used\",\"locationType\":\"AWS Region\",\"storage\":\"EBS only\",\"instanceFamily\":\"General purpose\",\"operatingSystem\":\"Linux\",\"intelAvx2Available\":\"No\",\"instanceFamilyCategory\":\"General Purpose\",\"regionCode\":\"us-west-2\",\"physicalProcessor\":\"AWS Graviton3 Processor\",\"clockSpeed\":\"2.5 GHz\",\"ecu\":\"NA\",\"networkPerformance\":\"Up to 12500 Megabit\",\"servicename\":\"Amazon Elastic Compute Cloud\",\"gpuMemory\":\"NA\",\"dedicatedEbsThroughputDescription\":\"630 Mbps\",\"vpcnetworkingsupport\":\"true\",\"instanceType\":\"m7g.large\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-BoxUsage:m7g.large\",\"normalizationSizeFactor\":\"4\",\"intelAvxAvailable\":\"No\",\"servicecode\":\"AmazonEC2\",\"licenseModel\":\"No License required\",\"currentGeneration\":\"Yes\",\"preInstalledSw\":\"NA\",\"location\":\"US West (Oregon)\",\"processorArchitecture\":\"64-bit\",\"marketoption\":\"OnDemand\",\"operation\":\"RunInstances\",\"availabilityzone\":\"NA\"},\"sku\":\"NWX8JZYSP4RSV4Q2\"},\"serviceCode\":\"AmazonEC2\",\"terms\":{\"OnDemand\":{\"NWX8JZYSP4RSV4Q2.JRTCKXETXF\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"$0.0816 per On Demand Linux m7g.large Instance Hour\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0816000000\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}},\"Reserved\":{\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0257000000\"}},\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"225\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:10Z\",\"offerTermCode\":\"HU7G6KETJZ\",\"termAttributes\":{\"LeaseContractLength\":\"1yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"Partial Upfront\"}},\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"503\"}},\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0192000000\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:09Z\",\"offerTermCode\":\"R5XV2EPZQZ\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"convertible\",\"PurchaseOption\":\"Partial Upfront\"}},\"NWX8JZYSP4RSV4Q2.38NPMPTW36\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.38NPMPTW36.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.38NPMPTW36.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0171000000\"}},\"NWX8JZYSP4RSV4Q2.38NPMPTW36.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.38NPMPTW36.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"450\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:09Z\",\"offerTermCode\":\"38NPMPTW36\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"Partial Upfront\"}},\"NWX8JZYSP4RSV4Q2.Z2E3P23VKM\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.Z2E3P23VKM.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.Z2E3P23VKM.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0414000000\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:12Z\",\"offerTermCode\":\"Z2E3P23VKM\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"convertible\",\"PurchaseOption\":\"No Upfront\"}},\"NWX8JZYSP4RSV4Q2.NQ3QZPMQV9\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.NQ3QZPMQV9.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"USD 0.0 per Linux/UNIX (Amazon VPC),Line truncated
]
}
@@ -0,0 +1,13 @@
{
"FormatVersion": "aws_v1",
"PriceList": [
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per vCPU per hour\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-Windows-vCPU-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU\",\"operatingSystem\":\"Windows\",\"operation\":\"\"},\"sku\":\"58AWBVRMPKQ3HVG6\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"58AWBVRMPKQ3HVG6.JRTCKXETXF\":{\"priceDimensions\":{\"58AWBVRMPKQ3HVG6.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Windows - vCPU - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"58AWBVRMPKQ3HVG6.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0465520000\"}}},\"sku\":\"58AWBVRMPKQ3HVG6\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per vCPU per hour\",\"cpuArchitecture\":\"ARM\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-ARM-vCPU-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU\",\"operation\":\"\"},\"sku\":\"5UAMYEN99PSY23D6\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"5UAMYEN99PSY23D6.JRTCKXETXF\":{\"priceDimensions\":{\"5UAMYEN99PSY23D6.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - ARM - vCPU - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"5UAMYEN99PSY23D6.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0323800000\"}}},\"sku\":\"5UAMYEN99PSY23D6\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-vCPU-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU\",\"operation\":\"\"},\"sku\":\"EKCCQUSFCBDDTKTS\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"EKCCQUSFCBDDTKTS.JRTCKXETXF\":{\"priceDimensions\":{\"EKCCQUSFCBDDTKTS.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - vCPU - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"EKCCQUSFCBDDTKTS.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0404800000\"}}},\"sku\":\"EKCCQUSFCBDDTKTS\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"OS License Fee per vCPU per hour\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-Windows-OS-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU OS License Fee\",\"operatingSystem\":\"Windows\",\"operation\":\"\"},\"sku\":\"HTBGRCKVHYXUYEAC\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"HTBGRCKVHYXUYEAC.JRTCKXETXF\":{\"priceDimensions\":{\"HTBGRCKVHYXUYEAC.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Windows - OS - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"HTBGRCKVHYXUYEAC.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0460000000\"}}},\"sku\":\"HTBGRCKVHYXUYEAC\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"memorytype\":\"perGB\",\"operation\":\"\"},\"sku\":\"NTC2QZFJW79R5XVK\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"NTC2QZFJW79R5XVK.JRTCKXETXF\":{\"priceDimensions\":{\"NTC2QZFJW79R5XVK.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Memory - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"NTC2QZFJW79R5XVK.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0044450000\"}}},\"sku\":\"NTC2QZFJW79R5XVK\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per GB per hour\",\"cpuArchitecture\":\"ARM\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-ARM-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"memorytype\":\"perGB\",\"operation\":\"\"},\"sku\":\"TAE28FJERF797NWS\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"TAE28FJERF797NWS.JRTCKXETXF\":{\"priceDimensions\":{\"TAE28FJERF797NWS.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - ARM - Memory - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"TAE28FJERF797NWS.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0035600000\"}}},\"sku\":\"TAE28FJERF797NWS\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"usagetype\":\"USW2-Fargate-EphemeralStorage-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"operation\":\"\",\"storagetype\":\"default\"},\"sku\":\"TTWDU5N74ZQ26XMP\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"TTWDU5N74ZQ26XMP.JRTCKXETXF\":{\"priceDimensions\":{\"TTWDU5N74ZQ26XMP.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"GB-Hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Ephemeral Storage - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"TTWDU5N74ZQ26XMP.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0001110000\"}}},\"sku\":\"TTWDU5N74ZQ26XMP\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per GB per hour\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-Windows-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"memorytype\":\"perGB\",\"operatingSystem\":\"Windows\",\"operation\":\"\"},\"sku\":\"ZPUB3WBJWT6K7RSF\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"ZPUB3WBJWT6K7RSF.JRTCKXETXF\":{\"priceDimensions\":{\"ZPUB3WBJWT6K7RSF.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Windows - Memory - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"ZPUB3WBJWT6K7RSF.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0051117500\"}}},\"sku\":\"ZPUB3WBJWT6K7RSF\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}"
]
}
+649
View File
@@ -0,0 +1,649 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use super::*;
use crate::models::project::{Arch, AwsWorkspaceConfig, StorageMode};
/// A real `GetProducts` answer for us-west-2 with [`FILTERS`] (recorded
/// 2026-11-16): the eight Fargate rows, Windows ones included.
const FIXTURE: &str = include_str!("fixtures/get-products-us-west-2.json");
fn fixture_items() -> Vec<String> {
let v: Value = serde_json::from_str(FIXTURE).unwrap();
v["PriceList"]
.as_array()
.unwrap()
.iter()
.map(|s| s.as_str().unwrap().to_string())
.collect()
}
const X86: FargateRates = FargateRates {
vcpu_hour: 0.04048,
gb_hour: 0.004445,
storage_gb_hour: 0.000111,
};
const ARM: FargateRates = FargateRates {
vcpu_hour: 0.03238,
gb_hour: 0.00356,
storage_gb_hour: 0.000111,
};
const RATES: RegionRates = RegionRates {
x86_64: Some(X86),
arm64: Some(ARM),
};
fn close(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-9
}
fn cfg(cpu: u32, memory_mib: u32, ephemeral_gib: u32) -> AwsWorkspaceConfig {
AwsWorkspaceConfig {
cpu,
memory_mib,
ephemeral_gib,
..Default::default()
}
}
// ---- estimate ----
#[test]
fn estimate_is_vcpus_plus_gbs_at_the_pinned_rates() {
// 1 vCPU, 4 GiB, 21 GiB disk (1 billed).
let e = estimate(&cfg(1024, 4096, 21), &X86);
assert!(
close(e.usd_per_hour, 0.04048 + 4.0 * 0.004445 + 0.000111),
"{e:?}"
);
// 0.25 vCPU, 0.5 GiB, the free 20 GiB only.
let e = estimate(&cfg(256, 512, 20), &X86);
assert!(
close(e.usd_per_hour, 0.25 * 0.04048 + 0.5 * 0.004445),
"{e:?}"
);
// 4 vCPU, 16 GiB on ARM, 200 GiB disk (180 billed).
let e = estimate(&cfg(4096, 16384, 200), &ARM);
assert!(
close(
e.usd_per_hour,
4.0 * 0.03238 + 16.0 * 0.00356 + 180.0 * 0.000111
),
"{e:?}"
);
}
#[test]
fn ephemeral_storage_is_billed_only_above_twenty_gib() {
let free = estimate(&cfg(1024, 2048, 20), &X86).usd_per_hour;
let one = estimate(&cfg(1024, 2048, 21), &X86).usd_per_hour;
let fifty = estimate(&cfg(1024, 2048, 50), &X86).usd_per_hour;
assert!(close(one - free, 0.000111));
assert!(close(fifty - free, 30.0 * 0.000111));
}
#[test]
fn notes_follow_spot_and_storage_and_the_sidecar_adds_nothing() {
let mut c = cfg(1024, 4096, 21);
c.storage = StorageMode::Ephemeral;
let base = estimate(&c, &X86);
assert!(!base.note_spot && !base.efs_billed_separately);
c.spot = true;
assert!(estimate(&c, &X86).note_spot);
for storage in [StorageMode::Hybrid, StorageMode::Efs] {
c.storage = storage;
assert!(estimate(&c, &X86).efs_billed_separately, "{storage:?}");
}
c.gateway_sidecar = true;
assert!(close(estimate(&c, &X86).usd_per_hour, base.usd_per_hour));
}
#[test]
fn rates_are_picked_by_arch() {
assert_eq!(RATES.for_arch(Arch::X86_64), Some(X86));
assert_eq!(RATES.for_arch(Arch::Arm64), Some(ARM));
}
// ---- parsing ----
#[test]
fn the_recorded_price_list_parses_to_x86_and_arm_rates() {
let rates = parse_price_list("us-west-2", &fixture_items()).expect("rates");
assert_eq!(rates, RATES);
}
#[test]
fn a_price_list_for_another_region_gives_nothing() {
assert_eq!(parse_price_list("eu-west-1", &fixture_items()), None);
}
#[test]
fn windows_rows_never_count() {
let items: Vec<String> = fixture_items()
.into_iter()
.filter(|s| s.contains("Windows"))
.collect();
assert_eq!(items.len(), 3);
assert_eq!(parse_price_list("us-west-2", &items), None);
}
#[test]
fn an_incomplete_architecture_is_none_and_storage_is_required() {
let no_arm: Vec<String> = fixture_items()
.into_iter()
.filter(|s| !s.contains("Fargate-ARM-"))
.collect();
let rates = parse_price_list("us-west-2", &no_arm).unwrap();
assert_eq!(rates.x86_64, Some(X86));
assert_eq!(rates.arm64, None);
let no_storage: Vec<String> = fixture_items()
.into_iter()
.filter(|s| !s.contains("EphemeralStorage"))
.collect();
assert_eq!(parse_price_list("us-west-2", &no_storage), None);
}
#[test]
fn garbage_and_bad_prices_are_skipped() {
let mut items = vec!["not json".to_string(), "{}".to_string()];
// A negative x86 vCPU price ahead of the real one is ignored.
let bad = fixture_items()
.into_iter()
.find(|s| s.contains("USW2-Fargate-vCPU-Hours"))
.unwrap()
.replace("0.0404800000", "-1");
items.push(bad);
items.extend(fixture_items());
assert_eq!(parse_price_list("us-west-2", &items), Some(RATES));
}
#[test]
fn the_filters_name_the_region_on_demand_and_fargate() {
assert_eq!(PRICING_REGION, "us-east-1");
assert_eq!(SERVICE_CODE, "AmazonECS");
assert!(FILTERS.contains(&("TERM_MATCH", "regionCode", "{region}")));
assert!(FILTERS.contains(&("TERM_MATCH", "termType", "OnDemand")));
assert!(FILTERS.contains(&("CONTAINS", "usagetype", "Fargate")));
}
// ---- the API, faked ----
struct FakeApi {
pages: Mutex<Vec<Result<Page, String>>>,
calls: AtomicUsize,
queries: Mutex<Vec<Query>>,
hang: bool,
}
impl FakeApi {
fn answering(pages: Vec<Result<Page, String>>) -> Self {
Self {
pages: Mutex::new(pages),
calls: AtomicUsize::new(0),
queries: Mutex::new(Vec::new()),
hang: false,
}
}
fn fixture() -> Self {
Self::answering(vec![Ok(Page {
items: fixture_items(),
next_token: None,
})])
}
fn denied() -> Self {
Self::answering(vec![Err("Access denied: pricing:GetProducts".to_string())])
}
fn hanging() -> Self {
Self {
hang: true,
..Self::answering(vec![])
}
}
fn calls(&self) -> usize {
self.calls.load(Ordering::SeqCst)
}
}
impl PricingApi for FakeApi {
async fn get_products(&self, query: &Query, _token: Option<String>) -> Result<Page, String> {
assert!(
query.filters.contains(&("TERM_MATCH", "regionCode", "us-west-2".to_string())),
"{query:?}"
);
self.queries.lock().unwrap().push(query.clone());
self.calls.fetch_add(1, Ordering::SeqCst);
if self.hang {
std::future::pending::<()>().await;
}
let mut pages = self.pages.lock().unwrap();
if pages.is_empty() {
return Err("no more pages".to_string());
}
pages.remove(0)
}
}
#[tokio::test]
async fn pages_are_joined() {
let items = fixture_items();
let (a, b) = items.split_at(4);
let api = FakeApi::answering(vec![
Ok(Page {
items: a.to_vec(),
next_token: Some("t".to_string()),
}),
Ok(Page {
items: b.to_vec(),
next_token: None,
}),
]);
assert_eq!(fetch_rates(&api, "us-west-2").await, Ok(RATES));
assert_eq!(api.calls(), 2);
}
// ---- the cache ----
fn at(secs: u64) -> SystemTime {
UNIX_EPOCH + Duration::from_secs(secs)
}
const T0: u64 = 1_800_000_000;
const DAY: u64 = 24 * 60 * 60;
fn cache() -> (tempfile::TempDir, RatesCache) {
let dir = tempfile::tempdir().unwrap();
let cache = RatesCache::new(dir.path().join("aws-pricing"));
(dir, cache)
}
#[test]
fn a_cache_file_is_fresh_for_a_day() {
let (_d, c) = cache();
assert_eq!(c.read("us-west-2", at(T0)), None);
c.write("us-west-2", &RATES, at(T0)).unwrap();
assert!(c.dir().join("us-west-2.json").is_file());
assert_eq!(c.read("us-west-2", at(T0 + DAY - 1)), Some((RATES, true)));
assert_eq!(c.read("us-west-2", at(T0 + DAY)), Some((RATES, false)));
// From the future (the clock went back): stale.
assert_eq!(c.read("us-west-2", at(T0 - 10)), Some((RATES, false)));
// Another region's file is not this one's.
assert_eq!(c.read("eu-west-1", at(T0)), None);
}
#[test]
fn a_corrupt_or_mislabelled_cache_file_is_no_cache() {
let (_d, c) = cache();
std::fs::create_dir_all(c.dir()).unwrap();
std::fs::write(c.dir().join("us-west-2.json"), "{ not json").unwrap();
assert_eq!(c.read("us-west-2", at(T0)), None);
c.write("eu-west-1", &RATES, at(T0)).unwrap();
std::fs::copy(
c.dir().join("eu-west-1.json"),
c.dir().join("us-west-2.json"),
)
.unwrap();
assert_eq!(c.read("us-west-2", at(T0)), None);
}
#[tokio::test]
async fn a_fresh_cache_answers_without_a_call() {
let (_d, c) = cache();
c.write("us-west-2", &RATES, at(T0)).unwrap();
let api = FakeApi::denied();
assert_eq!(
c.rates(Some(&api), "us-west-2", at(T0 + 60)).await,
Some(RATES)
);
assert_eq!(api.calls(), 0);
}
#[tokio::test]
async fn a_miss_fetches_and_writes_the_cache() {
let (_d, c) = cache();
let api = FakeApi::fixture();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, Some(RATES));
assert_eq!(api.calls(), 1);
assert_eq!(c.read("us-west-2", at(T0 + 1)), Some((RATES, true)));
}
#[tokio::test]
async fn an_expired_cache_is_fetched_again() {
let (_d, c) = cache();
let old = RegionRates {
x86_64: Some(FargateRates {
vcpu_hour: 1.0,
..X86
}),
arm64: None,
};
c.write("us-west-2", &old, at(T0)).unwrap();
let api = FakeApi::fixture();
assert_eq!(
c.rates(Some(&api), "us-west-2", at(T0 + DAY)).await,
Some(RATES)
);
assert_eq!(api.calls(), 1);
assert_eq!(c.read("us-west-2", at(T0 + DAY)), Some((RATES, true)));
}
#[tokio::test]
async fn a_corrupt_cache_is_fetched_again() {
let (_d, c) = cache();
std::fs::create_dir_all(c.dir()).unwrap();
std::fs::write(c.dir().join("us-west-2.json"), "garbage").unwrap();
let api = FakeApi::fixture();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, Some(RATES));
assert_eq!(c.read("us-west-2", at(T0)), Some((RATES, true)));
}
#[tokio::test]
async fn denied_is_none_and_is_not_asked_again_for_a_while() {
let (_d, c) = cache();
let api = FakeApi::denied();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, None);
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0 + 60)).await, None);
assert_eq!(api.calls(), 1, "a recent failure is not retried");
let later = T0 + RETRY_AFTER_FAILURE.as_secs();
assert_eq!(c.rates(Some(&api), "us-west-2", at(later)).await, None);
assert_eq!(api.calls(), 2);
assert!(!c.dir().join("us-west-2.json").exists());
}
#[tokio::test]
async fn a_failed_refresh_falls_back_to_the_stale_cache() {
let (_d, c) = cache();
c.write("us-west-2", &RATES, at(T0)).unwrap();
let api = FakeApi::denied();
assert_eq!(
c.rates(Some(&api), "us-west-2", at(T0 + 2 * DAY)).await,
Some(RATES)
);
assert_eq!(api.calls(), 1);
// No client at all (e.g. the connection's keys are gone): stale still answers.
assert_eq!(
c.rates(None::<&FakeApi>, "us-west-2", at(T0 + 2 * DAY))
.await,
Some(RATES)
);
}
#[tokio::test(start_paused = true)]
async fn a_hanging_api_times_out_to_none() {
let (_d, c) = cache();
let api = FakeApi::hanging();
let started = tokio::time::Instant::now();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, None);
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
assert_eq!(api.calls(), 1);
}
#[tokio::test(start_paused = true)]
async fn building_the_client_counts_against_the_same_timeout() {
let (_d, c) = cache();
c.write("us-west-2", &RATES, at(T0)).unwrap();
let started = tokio::time::Instant::now();
// The client never gets built (credentials that hang): stale answers at
// the fetch's own bound, not later.
let r = c
.rates_with::<FakeApi, FakeApi>(std::future::pending(), "us-west-2", at(T0 + 2 * DAY))
.await;
assert_eq!(r, Some(RATES));
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
// A fresh cache never builds a client at all.
let fresh = c
.rates_with::<FakeApi, FakeApi>(
async { panic!("no client is built for a fresh cache") },
"us-west-2",
at(T0 + 60),
)
.await;
assert_eq!(fresh, Some(RATES));
}
#[tokio::test]
async fn no_client_counts_as_a_failure_and_is_not_asked_again_for_a_while() {
let (_d, c) = cache();
let none = || async { None::<FakeApi> };
assert_eq!(c.rates_with::<FakeApi, FakeApi>(none(), "us-west-2", at(T0)).await, None);
// Within the back-off the client is not even built.
let r = c
.rates_with::<FakeApi, FakeApi>(
async { panic!("no client is built within the back-off") },
"us-west-2",
at(T0 + 60),
)
.await;
assert_eq!(r, None);
// After it, a working client is asked and clears the failure.
let api = FakeApi::fixture();
let later = T0 + RETRY_AFTER_FAILURE.as_secs();
assert_eq!(c.rates(Some(&api), "us-west-2", at(later)).await, Some(RATES));
assert_eq!(api.calls(), 1);
}
// ---- EC2 (phase 7 E10) ----
/// Real `AmazonEC2` `GetProducts` answers for us-west-2 with [`EC2_FILTERS`]
/// (recorded 2026-11-16): one row each for m7i.large and m7g.large.
const EC2_FIXTURE: &str = include_str!("fixtures/ec2-get-products-us-west-2.json");
const M7I_LARGE: f64 = 0.1008;
const M7G_LARGE: f64 = 0.0816;
fn ec2_items() -> Vec<String> {
let v: Value = serde_json::from_str(EC2_FIXTURE).unwrap();
v["PriceList"]
.as_array()
.unwrap()
.iter()
.map(|s| s.as_str().unwrap().to_string())
.collect()
}
fn ec2_cfg(cpu: u32, memory_mib: u32, storage: StorageMode) -> AwsWorkspaceConfig {
AwsWorkspaceConfig {
launch_type: crate::models::project::LaunchType::Ec2,
cpu,
memory_mib,
storage,
..Default::default()
}
}
#[test]
fn the_recorded_ec2_price_list_gives_each_instance_types_hourly_price() {
let items = ec2_items();
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &items), Some(M7I_LARGE));
assert_eq!(parse_ec2_price("us-west-2", "m7g.large", &items), Some(M7G_LARGE));
// Not a type in the list, or another region: nothing.
assert_eq!(parse_ec2_price("us-west-2", "c7i.large", &items), None);
assert_eq!(parse_ec2_price("eu-west-1", "m7i.large", &items), None);
}
#[test]
fn an_ec2_row_for_other_software_tenancy_or_capacity_never_counts() {
let row = ec2_items().into_iter().find(|s| s.contains("m7i.large")).unwrap();
for (from, to) in [
("\"tenancy\":\"Shared\"", "\"tenancy\":\"Dedicated\""),
("\"preInstalledSw\":\"NA\"", "\"preInstalledSw\":\"SQL Web\""),
("\"capacitystatus\":\"Used\"", "\"capacitystatus\":\"UnusedCapacityReservation\""),
("\"operatingSystem\":\"Linux\"", "\"operatingSystem\":\"Windows\""),
("\"licenseModel\":\"No License required\"", "\"licenseModel\":\"Bring your own license\""),
] {
assert!(row.contains(from), "{from}");
let other = row.replace(from, to);
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &[other]), None, "{to}");
}
// A zero price (what an unused reservation row says) is not a price.
let zero = row.replace("0.1008000000", "0.0000000000");
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &[zero]), None);
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &["garbage".into()]), None);
}
#[test]
fn the_ec2_filters_name_linux_shared_tenancy_on_demand_for_the_type() {
assert_eq!(EC2_SERVICE_CODE, "AmazonEC2");
let q = ec2_query("us-west-2", "m7i.large");
assert_eq!(q.service_code, "AmazonEC2");
let f = |field: &'static str, value: &str| ("TERM_MATCH", field, value.to_string());
assert_eq!(
q.filters,
vec![
f("regionCode", "us-west-2"),
f("instanceType", "m7i.large"),
f("operatingSystem", "Linux"),
f("tenancy", "Shared"),
f("preInstalledSw", "NA"),
f("capacitystatus", "Used"),
f("licenseModel", "No License required"),
]
);
// The Fargate query is unchanged.
let fq = fargate_query("us-west-2");
assert_eq!(fq.service_code, "AmazonECS");
assert_eq!(
fq.filters,
vec![
("TERM_MATCH", "regionCode", "us-west-2".to_string()),
("TERM_MATCH", "termType", "OnDemand".to_string()),
("CONTAINS", "usagetype", "Fargate".to_string()),
]
);
}
#[test]
fn an_ec2_estimate_is_the_instances_price_whatever_the_reservations() {
let small = estimate_ec2(&ec2_cfg(512, 1024, StorageMode::Ephemeral), M7I_LARGE, None);
let big = estimate_ec2(&ec2_cfg(1920, 7373, StorageMode::Ephemeral), M7I_LARGE, None);
assert!(close(small.usd_per_hour, 0.1008) && close(big.usd_per_hour, 0.1008));
assert!(small.ec2_instance && !small.note_spot && !small.efs_billed_separately);
assert!(!small.root_volume_included);
// Ephemeral size and Spot don't apply on EC2; EFS is still billed apart.
let mut c = ec2_cfg(1024, 4096, StorageMode::Hybrid);
c.ephemeral_gib = 200;
c.spot = true;
let e = estimate_ec2(&c, M7G_LARGE, None);
assert!(close(e.usd_per_hour, 0.0816));
assert!(e.efs_billed_separately && !e.note_spot && e.ec2_instance);
// Fargate estimates say they are not an instance's.
assert!(!estimate(&cfg(1024, 4096, 21), &X86).ec2_instance);
}
fn ec2_api() -> FakeApi {
FakeApi::answering(vec![Ok(Page {
items: ec2_items(),
next_token: None,
})])
}
#[tokio::test]
async fn an_ec2_price_is_fetched_once_and_cached_per_region_and_type() {
let (_d, c) = cache();
let api = ec2_api();
let price = c.ec2_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", "m7i.large", at(T0)).await;
assert_eq!(price, Some(M7I_LARGE));
assert_eq!(api.calls(), 1);
assert_eq!(api.queries.lock().unwrap()[0], ec2_query("us-west-2", "m7i.large"));
assert!(c.dir().join("us-west-2--ec2-m7i.large.json").is_file());
assert_eq!(c.read_ec2("us-west-2", "m7i.large", at(T0 + DAY - 1)), Some((M7I_LARGE, true)));
assert_eq!(c.read_ec2("us-west-2", "m7i.large", at(T0 + DAY)), Some((M7I_LARGE, false)));
// Fresh: no call. Another type, or the region's Fargate rates, are not it.
let again = c
.ec2_price_with::<FakeApi, FakeApi>(async { panic!("no client for a fresh cache") }, "us-west-2", "m7i.large", at(T0 + 60))
.await;
assert_eq!(again, Some(M7I_LARGE));
assert_eq!(c.read_ec2("us-west-2", "m7g.large", at(T0)), None);
assert_eq!(c.read("us-west-2", at(T0)), None);
// A file renamed to another type's name is no cache.
std::fs::copy(
c.dir().join("us-west-2--ec2-m7i.large.json"),
c.dir().join("us-west-2--ec2-m7g.large.json"),
)
.unwrap();
assert_eq!(c.read_ec2("us-west-2", "m7g.large", at(T0)), None);
}
#[tokio::test]
async fn an_ec2_price_backs_off_after_a_failure_and_falls_back_to_the_stale_cache() {
let (_d, c) = cache();
let denied = FakeApi::denied();
assert_eq!(c.ec2_price_with::<FakeApi, _>(async { Some(&denied) }, "us-west-2", "m7i.large", at(T0)).await, None);
assert_eq!(c.ec2_price_with::<FakeApi, _>(async { Some(&denied) }, "us-west-2", "m7i.large", at(T0 + 60)).await, None);
assert_eq!(denied.calls(), 1, "a recent failure is not retried");
// The failure is per type: another type is still asked.
let other = FakeApi::denied();
let _ = c.ec2_price_with::<FakeApi, _>(async { Some(&other) }, "us-west-2", "m7g.large", at(T0 + 60)).await;
assert_eq!(other.calls(), 1);
// A stale price answers when the refresh fails.
c.write_ec2("us-west-2", "c7i.large", 0.0893, at(T0)).unwrap();
let r = c.ec2_price_with::<FakeApi, _>(async { Some(&denied) }, "us-west-2", "c7i.large", at(T0 + 2 * DAY)).await;
assert_eq!(r, Some(0.0893));
}
#[tokio::test(start_paused = true)]
async fn a_hanging_ec2_price_times_out_to_none() {
let (_d, c) = cache();
let api = FakeApi::hanging();
let started = tokio::time::Instant::now();
assert_eq!(c.ec2_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", "m7i.large", at(T0)).await, None);
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
}
#[tokio::test]
async fn an_instance_type_that_could_name_another_file_is_never_used() {
let (_d, c) = cache();
for bad in ["../x", "m7i.large/..", "M7I.large", "m7i", "", "m7i.large.json", "m7i .large"] {
let r = c
.ec2_price_with::<FakeApi, FakeApi>(async { panic!("no client for {bad:?}") }, "us-west-2", bad, at(T0))
.await;
assert_eq!(r, None, "{bad}");
assert_eq!(c.read_ec2("us-west-2", bad, at(T0)), None);
assert!(c.write_ec2("us-west-2", bad, 1.0, at(T0)).is_err(), "{bad}");
assert!(fetch_ec2_price(&ec2_api(), "us-west-2", bad).await.is_err());
}
assert_eq!(fetch_ec2_price(&ec2_api(), "us-west-2", "m7g.large").await, Ok(M7G_LARGE));
}
#[test]
fn a_fargate_cache_file_written_before_ec2_prices_still_reads() {
let (_d, c) = cache();
std::fs::create_dir_all(c.dir()).unwrap();
let old = serde_json::json!({ "region": "us-west-2", "fetched_at": T0, "rates": RATES });
std::fs::write(c.dir().join("us-west-2.json"), old.to_string()).unwrap();
assert_eq!(c.read("us-west-2", at(T0)), Some((RATES, true)));
}
const GP3_FIXTURE: &str = include_str!("fixtures/ebs-gp3-get-products-us-west-2.json");
fn gp3_items() -> Vec<String> {
let v: Value = serde_json::from_str(GP3_FIXTURE).unwrap();
v["PriceList"].as_array().unwrap().iter().map(|s| s.as_str().unwrap().to_string()).collect()
}
/// The root volume (50 GiB gp3, `ec2.tf`) is in an EC2 estimate when its
/// price is known: $0.08/GB-month in us-west-2 (recorded 2026-11-16) is
/// 50 × 0.08 / 730 ≈ $0.0055/h on top of the instance.
#[test]
fn an_ec2_estimate_adds_the_gp3_root_volume_when_its_price_is_known() {
assert_eq!(parse_gp3_price("us-west-2", &gp3_items()), Some(0.08));
assert_eq!(parse_gp3_price("eu-west-1", &gp3_items()), None, "another region's row");
assert_eq!(parse_gp3_price("us-west-2", &ec2_items()), None, "not a gp3 row");
let e = estimate_ec2(&ec2_cfg(1024, 4096, StorageMode::Ephemeral), M7I_LARGE, Some(0.08));
assert!(close(e.usd_per_hour, 0.1008 + 50.0 * 0.08 / 730.0), "{}", e.usd_per_hour);
assert!(e.root_volume_included && e.ec2_instance);
assert_eq!(gp3_query("us-west-2").service_code, "AmazonEC2");
}
#[tokio::test]
async fn the_gp3_price_is_fetched_once_and_cached_per_region() {
let (_d, c) = cache();
let api = FakeApi::answering(vec![Ok(Page { items: gp3_items(), next_token: None })]);
let price = c.gp3_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", at(T0)).await;
assert_eq!(price, Some(0.08));
assert_eq!(api.queries.lock().unwrap()[0], gp3_query("us-west-2"));
assert!(c.dir().join("us-west-2--ebs-gp3.json").is_file());
let again = c.gp3_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", at(T0 + 1)).await;
assert_eq!(again, Some(0.08));
assert_eq!(api.calls(), 1);
}
+550
View File
@@ -0,0 +1,550 @@
//! What the AWS host asks AWS around a task, besides running it
//! (`tasks.rs`) and preparing its resources (`workspace.rs`): the
//! environment's record, whether the caller is onboarded, the agent token of
//! a running task (P2), waiting for a new access point, and removing a
//! project's resources.
//!
//! Like the other `aws/` modules, the decisions are made by the functions
//! here over a small trait ([`RuntimeApi`]) so a fake exercises them; the SDK
//! calls are in [`SdkRuntimeApi`].
use std::future::Future;
use std::time::Duration;
use aws_config::SdkConfig;
use aws_sdk_secretsmanager::error::ProvideErrorMetadata;
use super::environments::{
get_parameter, knowable, parse_environment_record, user_record_from, EnvironmentInfo, Knowable,
Param, UserRecord,
};
use super::error::{classify_sdk_error, AwsError};
use super::tasks::{find_project_task, TaskApi, STOP_REASON};
use super::tofu_modules::{
access_point_root, workspace_access_point_root, environment_cluster_name, environment_parameter_name, project_secret_name, task_family,
is_user_ec2_cluster, user_file_system_token, user_record_parameter_name, user_role_names,
};
use super::workspace::WorkspaceApi;
/// The secret key holding the agent token (Task 4's payload).
pub const AGENT_TOKEN_KEY: &str = "TRIPLEC_AGENT_TOKEN";
/// A project secret is at most 64 KiB; anything bigger is not ours.
const MAX_SECRET_READ: usize = 70_000;
fn other(message: impl Into<String>) -> AwsError {
AwsError::Other {
message: message.into(),
}
}
pub trait RuntimeApi: Send + Sync {
/// `ssm:GetParameter` of an environment's record; `None` when absent.
fn environment_record(
&self,
env: &str,
) -> impl Future<Output = Result<Option<String>, AwsError>> + Send;
/// `ssm:GetParameter` of the caller's EC2 record in `env`
/// (`user_record_parameter_name`).
fn user_record(
&self,
env: &str,
owner_key: &str,
) -> impl Future<Output = Result<Param, AwsError>> + Send;
/// `iam:GetRole`.
fn role_exists(&self, name: &str) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
/// `secretsmanager:GetSecretValue`'s `SecretString`.
fn secret_string(&self, name: &str) -> impl Future<Output = Result<String, AwsError>> + Send;
/// The access point's `LifeCycleState`; `None` when it is gone.
fn access_point_state(
&self,
id: &str,
) -> impl Future<Output = Result<Option<String>, AwsError>> + Send;
/// Delete at once (`ForceDeleteWithoutRecovery`, ruling R10): a secret
/// left pending deletion would make the project's next start fail, and
/// the app rebuilds it from local data. A secret that does not exist is
/// success.
fn delete_secret(&self, name: &str) -> impl Future<Output = Result<(), AwsError>> + Send;
/// A missing access point is success.
fn delete_access_point(&self, id: &str) -> impl Future<Output = Result<(), AwsError>> + Send;
}
// ------------------------------------------------------------ environment
/// The environment's record, read with one `GetParameter` and validated.
pub async fn read_environment<R: RuntimeApi>(
rt: &R,
env: &str,
account: &str,
region: &str,
) -> Result<EnvironmentInfo, AwsError> {
let value = rt.environment_record(env).await?.ok_or_else(|| {
other(format!(
"The environment \"{env}\" was not found in {region}. It may have been removed; \
check Settings → Hosts."
))
})?;
parse_environment_record(env, &value, account, region).map_err(|e| {
other(format!(
"The record of environment \"{env}\" in {region} is not usable: {e}"
))
})
}
/// The caller's EC2 record in `env`, validated (untrusted: `parse_user_record`).
/// Absent = no EC2 capacity for NEW runs; a task already running on the
/// user's cluster may outlive the record (teardown removes it first).
pub async fn read_user_record<R: RuntimeApi>(
rt: &R,
env: &str,
owner_key: &str,
account: &str,
region: &str,
) -> Result<UserRecord, AwsError> {
Ok(user_record_from(rt.user_record(env, owner_key).await?, env, account, region))
}
/// The per-user EC2 cluster (ARN) a stored task ARN names, when it is one of
/// `env`'s: where a project that ran on EC2 is still looked for after its
/// user record is gone.
pub fn ec2_cluster_of_task(env: &str, task_arn: &str) -> Option<String> {
cluster_of_task(task_arn)
.filter(|c| is_user_ec2_cluster(env, c))
.and_then(|_| cluster_arn_of_task(task_arn))
}
/// Where a project's task may be: `env_cluster` (the environment's), then
/// the user's EC2 cluster from their record, then the per-user cluster
/// `stored_task_arn` names (the record may be gone while that task runs),
/// without repeats.
pub fn project_task_clusters(
env: &str,
env_cluster: String,
ec2_cluster: Option<String>,
stored_task_arn: Option<&str>,
) -> Vec<String> {
let mut out = vec![env_cluster];
for c in [ec2_cluster, stored_task_arn.and_then(|a| ec2_cluster_of_task(env, a))]
.into_iter()
.flatten()
{
if !out.contains(&c) {
out.push(c);
}
}
out
}
/// Whether the caller's task role exists in the environment: the onboarding
/// signal a workspace user can read. A denied lookup is not an answer, so
/// it is let through (RunTask will say what is wrong).
pub async fn check_onboarded<R: RuntimeApi>(
rt: &R,
env: &str,
owner_key: &str,
) -> Result<(), AwsError> {
let (task_role, _) = user_role_names(env, owner_key);
match rt.role_exists(&task_role).await? {
Knowable::No => Err(other(format!(
"You have not joined the environment \"{env}\" yet. Join it in Settings → Hosts \
(or ask an administrator to onboard you), then start the project again."
))),
Knowable::Yes | Knowable::Unknown => Ok(()),
}
}
/// The agent token in a project secret's JSON. The error never echoes the
/// secret.
pub fn agent_token_from_secret(json: &str) -> Result<String, String> {
if json.len() > MAX_SECRET_READ {
return Err("The project secret is larger than a project secret can be".into());
}
let v: serde_json::Value = serde_json::from_str(json)
.map_err(|_| "The project secret is not a JSON object".to_string())?;
v.get(AGENT_TOKEN_KEY)
.and_then(|t| t.as_str())
.filter(|t| !t.is_empty() && t.bytes().all(|b| b.is_ascii_graphic()))
.map(str::to_string)
.ok_or_else(|| "The project secret holds no agent token".to_string())
}
/// The token a running task of this project started with (P2: the caller
/// may read their own secret; `prepare_workspace` wrote it before RunTask).
pub async fn read_agent_token<R: RuntimeApi>(
rt: &R,
env: &str,
owner_key: &str,
project_id: &str,
) -> Result<String, AwsError> {
let name = project_secret_name(env, owner_key, project_id);
let json = rt.secret_string(&name).await?;
agent_token_from_secret(&json).map_err(other)
}
/// After a start this call ran is up and its agent answered with `ours`:
/// make sure the secret still holds `ours`. A start that lost the RunTask
/// race (another machine, the same moment) rotated the token in the secret
/// before it found this task, and every later attach would read a token the
/// task never saw. Returns whether the secret had to be put back.
pub async fn ensure_agent_token<W: WorkspaceApi, R: RuntimeApi>(
work: &W,
rt: &R,
env: &EnvironmentInfo,
owner_user_id: &str,
owner_key: &str,
project_id: &str,
ours: &str,
) -> Result<bool, AwsError> {
let name = project_secret_name(&env.name, owner_key, project_id);
let json = rt.secret_string(&name).await?;
if agent_token_from_secret(&json).ok().as_deref() == Some(ours) {
return Ok(false);
}
let mut v: serde_json::Map<String, serde_json::Value> = serde_json::from_str(&json)
.map_err(|_| other("The project secret is not a JSON object"))?;
v.insert(AGENT_TOKEN_KEY.to_string(), serde_json::Value::String(ours.to_string()));
let repaired = serde_json::to_string(&v).map_err(|e| other(e.to_string()))?;
let tags = super::workspace::workspace_tags(owner_user_id, project_id, &env.name);
work.put_project_secret(&name, &env.kms_key_arn, &repaired, &tags).await?;
Ok(true)
}
/// How long a fresh access point may take to become `available`.
#[derive(Debug, Clone, Copy)]
pub struct PollTiming {
pub interval: Duration,
pub timeout: Duration,
}
impl Default for PollTiming {
fn default() -> Self {
PollTiming {
interval: Duration::from_secs(2),
timeout: Duration::from_secs(90),
}
}
}
/// Wait until the access point is `available` (a task mounting one that is
/// still `creating` fails to start).
pub async fn wait_access_point<R: RuntimeApi>(
rt: &R,
id: &str,
timing: PollTiming,
) -> Result<(), AwsError> {
let deadline = tokio::time::Instant::now() + timing.timeout;
loop {
match rt.access_point_state(id).await?.as_deref() {
Some("available") => return Ok(()),
Some("creating") | Some("updating") => {}
Some(s) => {
return Err(other(format!(
"The project's EFS access point {id} is {s}; start the project again"
)))
}
None => {
return Err(other(format!(
"The project's EFS access point {id} disappeared; start the project again"
)))
}
}
if tokio::time::Instant::now() + timing.interval > deadline {
return Err(other(format!(
"The project's EFS access point {id} did not become available in time; \
start the project again in a minute"
)));
}
tokio::time::sleep(timing.interval).await;
}
}
// ------------------------------------------------------------------ ARNs
/// The cluster *name* of `arn:...:task/<cluster>/<id>` (ECS accepts a name
/// wherever it takes a cluster).
pub fn cluster_of_task(task_arn: &str) -> Option<&str> {
let (_, rest) = task_arn.split_once(":task/")?;
let (cluster, id) = rest.split_once('/')?;
(task_arn.starts_with("arn:") && !cluster.is_empty() && !id.is_empty() && !id.contains('/'))
.then_some(cluster)
}
/// The cluster ARN of a task ARN (same partition, region and account).
pub fn cluster_arn_of_task(task_arn: &str) -> Option<String> {
let cluster = cluster_of_task(task_arn)?;
let (prefix, _) = task_arn.split_once(":task/")?;
Some(format!("{prefix}:cluster/{cluster}"))
}
// ---------------------------------------------------------------- removal
/// What removing a project's AWS resources needs to know.
pub struct RemoveRequest<'a> {
pub env: &'a str,
pub region: &'a str,
pub owner_user_id: &'a str,
pub owner_key: &'a str,
pub project_id: &'a str,
/// The task ARN the store has, if any (stopped too, in case the listing
/// missed it).
pub stored_task_arn: Option<&'a str>,
pub stored_access_point_id: Option<&'a str>,
/// The EFS-mode `/workspace` access point the store has (kept after a
/// switch to Hybrid or Ephemeral, Q1).
pub stored_workspace_access_point_id: Option<&'a str>,
/// The user's EC2 cluster (their record's), looked in too: a project
/// may have run there.
pub ec2_cluster: Option<&'a str>,
/// Delete the EFS access points, home and `/workspace` (ruling R9). The
/// files stay on the user's own file system (only their task role can
/// read it) until offboarding destroys it; a later start of the same
/// project gets new access points onto the same directories.
pub discard_data: bool,
}
/// Stop the project's task, delete its secret (no recovery window, R10) and,
/// with `discard_data`, delete its access points (not the files, R9). Task definition revisions
/// are left registered (R8). A task that can't be stopped stops the removal (nothing
/// billed may be left running); the rest is attempted in full and every
/// failure reported.
pub async fn remove_workspace_resources<T: TaskApi, W: WorkspaceApi, R: RuntimeApi>(
tasks: &T,
work: &W,
rt: &R,
req: &RemoveRequest<'_>,
) -> Result<(), AwsError> {
let cluster = environment_cluster_name(req.env);
let family = task_family(req.env, req.owner_key, req.project_id);
// 1. The task: in the environment's cluster, the user's EC2 cluster (the
// record's, and the stored task's when that is a per-user cluster of
// this environment — the record may be gone).
let clusters = project_task_clusters(
req.env,
cluster.clone(),
req.ec2_cluster.map(str::to_string),
req.stored_task_arn,
);
let mut to_stop: Vec<(String, String)> = Vec::new();
for c in &clusters {
if let Some(t) =
find_project_task(tasks, c, &family, req.owner_user_id, req.project_id).await?
{
to_stop.push((c.clone(), t.arn));
}
}
if let Some(arn) = req.stored_task_arn {
let ours = cluster_of_task(arn) == Some(cluster.as_str())
|| ec2_cluster_of_task(req.env, arn).is_some();
if ours && !to_stop.iter().any(|(_, a)| a == arn) {
let c = cluster_of_task(arn).map_or_else(|| cluster.clone(), str::to_string);
to_stop.push((c, arn.to_string()));
}
}
for (c, arn) in &to_stop {
tasks.stop_task(c, arn, STOP_REASON).await?;
}
let mut failures: Vec<String> = Vec::new();
// 2. Task definition revisions are left ACTIVE (ruling R8): ECS
// authorizes `DeregisterTaskDefinition` against `*` only, with no tag
// condition, so the workspace tier can't retire just its own. They are
// inert (no secret values, only the secret's ARN) and free, and
// `RunOwnTasks` still requires the owner tag, so nobody else can run
// them. An admin can deregister them (`aws/README.md`).
log::info!(
"Project {}: its task definition revisions ({family}:*) are left registered; an \
administrator can deregister them",
req.project_id
);
// 3. The secret.
let secret = project_secret_name(req.env, req.owner_key, req.project_id);
if let Err(e) = rt.delete_secret(&secret).await {
failures.push(format!("secret {secret}: {e}"));
}
// 4. The access points (home, and EFS mode's `/workspace`), only when asked.
if req.discard_data {
let mut ids: Vec<String> = [req.stored_access_point_id, req.stored_workspace_access_point_id]
.into_iter()
.flatten()
.map(String::from)
.collect();
let token = user_file_system_token(req.env, req.owner_key);
match work.find_file_system(&token).await {
Ok(Some(fs)) if fs.owner.as_deref() == Some(req.owner_user_id) => {
let roots = [
access_point_root(req.owner_key, req.project_id),
workspace_access_point_root(req.owner_key, req.project_id),
];
match work.access_points(&fs.id).await {
Ok(points) => ids.extend(
points
.into_iter()
.filter(|a| {
a.file_system_id == fs.id
&& a.root_path.as_ref().is_some_and(|r| roots.contains(r))
&& a.owner.as_deref() == Some(req.owner_user_id)
&& a.project.as_deref() == Some(req.project_id)
})
.map(|a| a.id),
),
Err(e) => failures.push(format!("access points: {e}")),
}
}
Ok(_) => {}
Err(e) => failures.push(format!("file system: {e}")),
}
ids.sort();
ids.dedup();
for id in ids {
if let Err(e) = rt.delete_access_point(&id).await {
failures.push(format!("access point {id}: {e}"));
}
}
}
if failures.is_empty() {
Ok(())
} else {
Err(other(format!(
"Some of the project's AWS resources in {} could not be removed: {}",
req.region,
failures.join("; ")
)))
}
}
// ---------------------------------------------------------------- real API
pub struct SdkRuntimeApi {
ssm: aws_sdk_ssm::Client,
iam: aws_sdk_iam::Client,
secrets: aws_sdk_secretsmanager::Client,
efs: aws_sdk_efs::Client,
}
impl SdkRuntimeApi {
pub fn new(cfg: &SdkConfig) -> Self {
SdkRuntimeApi {
ssm: aws_sdk_ssm::Client::new(cfg),
iam: aws_sdk_iam::Client::new(cfg),
secrets: aws_sdk_secretsmanager::Client::new(cfg),
efs: aws_sdk_efs::Client::new(cfg),
}
}
}
impl RuntimeApi for SdkRuntimeApi {
async fn environment_record(&self, env: &str) -> Result<Option<String>, AwsError> {
const ACTION: &str = "ssm:GetParameter";
match self
.ssm
.get_parameter()
.name(environment_parameter_name(env))
.with_decryption(false)
.send()
.await
{
Ok(out) => Ok(out.parameter().and_then(|p| p.value()).map(str::to_string)),
Err(e)
if e.as_service_error()
.is_some_and(|s| s.is_parameter_not_found()) =>
{
Ok(None)
}
Err(e) => Err(classify_sdk_error(ACTION, &e)),
}
}
async fn user_record(&self, env: &str, owner_key: &str) -> Result<Param, AwsError> {
get_parameter(&self.ssm, &user_record_parameter_name(env, owner_key)).await
}
async fn role_exists(&self, name: &str) -> Result<Knowable, AwsError> {
let res = self.iam.get_role().role_name(name).send().await;
knowable("iam:GetRole", res)
}
async fn secret_string(&self, name: &str) -> Result<String, AwsError> {
const ACTION: &str = "secretsmanager:GetSecretValue";
let out = self
.secrets
.get_secret_value()
.secret_id(name)
.send()
.await
.map_err(|e| classify_sdk_error(ACTION, &e))?;
out.secret_string()
.map(str::to_string)
.ok_or_else(|| other(format!("{ACTION} returned no SecretString")))
}
async fn access_point_state(&self, id: &str) -> Result<Option<String>, AwsError> {
const ACTION: &str = "elasticfilesystem:DescribeAccessPoints";
match self.efs.describe_access_points().access_point_id(id).send().await {
Ok(out) => Ok(out
.access_points()
.iter()
.find(|a| a.access_point_id() == Some(id))
.and_then(|a| a.life_cycle_state())
.map(|s| s.as_str().to_string())),
Err(e)
if e.as_service_error()
.is_some_and(|s| s.is_access_point_not_found()) =>
{
Ok(None)
}
Err(e) => Err(classify_sdk_error(ACTION, &e)),
}
}
async fn delete_secret(&self, name: &str) -> Result<(), AwsError> {
const ACTION: &str = "secretsmanager:DeleteSecret";
match self
.secrets
.delete_secret()
.secret_id(name)
.force_delete_without_recovery(true)
.send()
.await
{
Ok(_) => Ok(()),
Err(e)
if e.as_service_error()
.is_some_and(|s| s.is_resource_not_found_exception()) =>
{
Ok(())
}
// Scheduled by an older version of the app and AWS refused the
// forced delete: nothing more this tier may do (no RestoreSecret).
Err(e)
if e.as_service_error().is_some_and(|s| {
s.is_invalid_request_exception()
&& s.message().is_some_and(|m| m.contains("scheduled for deletion"))
}) =>
{
Ok(())
}
Err(e) => Err(classify_sdk_error(ACTION, &e)),
}
}
async fn delete_access_point(&self, id: &str) -> Result<(), AwsError> {
const ACTION: &str = "elasticfilesystem:DeleteAccessPoint";
match self.efs.delete_access_point().access_point_id(id).send().await {
Ok(_) => Ok(()),
Err(e)
if e.as_service_error()
.is_some_and(|s| s.is_access_point_not_found()) =>
{
Ok(())
}
Err(e) => Err(classify_sdk_error(ACTION, &e)),
}
}
}
#[cfg(test)]
mod tests;
+542
View File
@@ -0,0 +1,542 @@
use std::sync::Mutex;
use aws_sdk_ecs::operation::register_task_definition::RegisterTaskDefinitionInput;
use aws_sdk_ecs::operation::run_task::RunTaskInput;
use super::*;
use crate::aws::tasks::TaskInfo;
use crate::aws::tofu_modules::{owner_key, workspace_access_point_root};
use crate::aws::workspace::{AccessPointRef, FileSystemRef, TaskDefinitionRef};
const REGION: &str = "eu-west-1";
const ACCOUNT: &str = "111122223333";
const USER: &str = "AROAEXAMPLEID:alice@example.com";
const PROJECT: &str = "0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0";
fn ok() -> String {
owner_key(REGION, USER)
}
fn family() -> String {
task_family("dev", &ok(), PROJECT)
}
fn task_arn(id: &str) -> String {
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/triple-c-dev/{id}")
}
#[derive(Default)]
struct Fake {
calls: Mutex<Vec<String>>,
record: Option<String>,
role: Option<Knowable>,
secret: Option<String>,
ap_states: Mutex<Vec<Option<String>>>,
live_tasks: Vec<TaskInfo>,
revisions: Vec<TaskDefinitionRef>,
access_points: Vec<AccessPointRef>,
fail_secret_delete: bool,
/// The clusters ListTasks was asked about, by name.
lists: Mutex<Vec<String>>,
}
impl Fake {
fn log(&self, s: String) {
self.calls.lock().unwrap().push(s);
}
fn calls(&self) -> Vec<String> {
self.calls.lock().unwrap().clone()
}
}
impl RuntimeApi for Fake {
async fn environment_record(&self, env: &str) -> Result<Option<String>, AwsError> {
self.log(format!("record {env}"));
Ok(self.record.clone())
}
async fn user_record(&self, env: &str, owner_key: &str) -> Result<crate::aws::environments::Param, AwsError> {
self.log(format!("user-record {env} {owner_key}"));
Ok(crate::aws::environments::Param::Missing)
}
async fn role_exists(&self, name: &str) -> Result<Knowable, AwsError> {
self.log(format!("role {name}"));
Ok(self.role.clone().unwrap_or(Knowable::Yes))
}
async fn secret_string(&self, name: &str) -> Result<String, AwsError> {
self.log(format!("secret {name}"));
self.secret.clone().ok_or_else(|| other("no secret"))
}
async fn access_point_state(&self, id: &str) -> Result<Option<String>, AwsError> {
self.log(format!("ap {id}"));
let mut s = self.ap_states.lock().unwrap();
Ok(if s.len() > 1 { s.remove(0) } else { s.first().cloned().flatten() })
}
async fn delete_secret(&self, name: &str) -> Result<(), AwsError> {
self.log(format!("delete-secret {name}"));
if self.fail_secret_delete {
return Err(other("denied"));
}
Ok(())
}
async fn delete_access_point(&self, id: &str) -> Result<(), AwsError> {
self.log(format!("delete-ap {id}"));
Ok(())
}
}
impl TaskApi for Fake {
async fn run_task(&self, _req: RunTaskInput) -> Result<TaskInfo, AwsError> {
unreachable!("removal never runs a task")
}
async fn describe(&self, _c: &str, arns: &[String]) -> Result<Vec<TaskInfo>, AwsError> {
Ok(self.live_tasks.iter().filter(|t| arns.contains(&t.arn)).cloned().collect())
}
async fn list_project_tasks(&self, c: &str, _f: &str) -> Result<Vec<String>, AwsError> {
// By cluster name or ARN, like ECS.
let name = c.rsplit_once(":cluster/").map_or(c, |(_, n)| n);
self.lists.lock().unwrap().push(name.to_string());
Ok(self
.live_tasks
.iter()
.filter(|t| cluster_of_task(&t.arn) == Some(name))
.map(|t| t.arn.clone())
.collect())
}
async fn stop_task(&self, cluster: &str, arn: &str, reason: &str) -> Result<(), AwsError> {
self.log(format!("stop {cluster} {arn} {reason}"));
Ok(())
}
async fn log_tail(&self, _g: &str, _s: &str, _l: i32) -> Result<Vec<String>, AwsError> {
Ok(Vec::new())
}
}
impl WorkspaceApi for Fake {
async fn find_file_system(&self, _token: &str) -> Result<Option<FileSystemRef>, AwsError> {
Ok(Some(FileSystemRef { id: "fs-1".into(), owner: Some(USER.into()) }))
}
async fn access_points(&self, _fs: &str) -> Result<Vec<AccessPointRef>, AwsError> {
Ok(self.access_points.clone())
}
async fn create_access_point(
&self,
_fs: &str,
_root: &str,
_tags: &[(String, String)],
) -> Result<String, AwsError> {
unreachable!()
}
async fn project_secret_value(&self, _n: &str) -> Result<Option<String>, AwsError> {
unreachable!()
}
async fn create_project_secret(
&self,
n: &str,
k: &str,
j: &str,
t: &[(String, String)],
) -> Result<Option<String>, AwsError> {
let _ = (n, k, j, t);
unreachable!()
}
async fn delete_project_secret(&self, _n: &str) -> Result<(), AwsError> {
unreachable!()
}
async fn put_project_secret(
&self,
_n: &str,
_k: &str,
_j: &str,
_t: &[(String, String)],
) -> Result<String, AwsError> {
unreachable!()
}
async fn task_definition_revisions(&self, _f: &str) -> Result<Vec<String>, AwsError> {
Ok(self.revisions.iter().map(|r| r.arn.clone()).collect())
}
async fn describe_task_definition(
&self,
arn: &str,
) -> Result<Option<TaskDefinitionRef>, AwsError> {
Ok(self.revisions.iter().find(|r| r.arn == arn).cloned())
}
async fn register_task_definition(
&self,
_req: RegisterTaskDefinitionInput,
) -> Result<String, AwsError> {
unreachable!()
}
}
fn record() -> String {
serde_json::json!({
"schema": 1, "name": "dev", "template_version": 1,
"cluster_arn": format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev"),
"security_group_id": "sg-0123456789abcdef0",
"subnet_ids": ["subnet-0a", "subnet-0b"],
"assign_public_ip": true,
"kms_key_arn": format!("arn:aws:kms:{REGION}:{ACCOUNT}:key/1234-abcd"),
"log_group": "/triple-c/dev", "network_mode": "public", "tenancy": "strict"
})
.to_string()
}
#[tokio::test]
async fn the_environment_is_read_with_one_lookup_and_validated() {
let fake = Fake { record: Some(record()), ..Default::default() };
let env = read_environment(&fake, "dev", ACCOUNT, REGION).await.unwrap();
assert_eq!(env.log_group, "/triple-c/dev");
assert_eq!(fake.calls(), vec!["record dev"]);
let missing = Fake::default();
let e = read_environment(&missing, "dev", ACCOUNT, REGION).await.unwrap_err();
assert!(e.to_string().contains("was not found"), "{e}");
// A record from another account is refused, by field name only.
let foreign = Fake { record: Some(record()), ..Default::default() };
let e = read_environment(&foreign, "dev", "999999999999", REGION).await.unwrap_err();
assert!(e.to_string().contains("cluster_arn"), "{e}");
}
#[tokio::test]
async fn a_missing_task_role_means_not_joined_and_a_denied_lookup_lets_the_start_try() {
let no = Fake { role: Some(Knowable::No), ..Default::default() };
let e = check_onboarded(&no, "dev", &ok()).await.unwrap_err();
assert!(e.to_string().contains("not joined"), "{e}");
assert_eq!(no.calls(), vec![format!("role triple-c-dev-{}-task", ok())]);
let unknown = Fake { role: Some(Knowable::Unknown), ..Default::default() };
assert!(check_onboarded(&unknown, "dev", &ok()).await.is_ok());
}
#[test]
fn the_agent_token_is_read_from_the_secret_without_echoing_it() {
let json = r#"{"TRIPLEC_AGENT_TOKEN":"abc-DEF_123","OTHER":"SENTINEL-VALUE-7f3a"}"#;
assert_eq!(agent_token_from_secret(json).unwrap(), "abc-DEF_123");
for bad in [
r#"{"OTHER":"SENTINEL-VALUE-7f3a"}"#,
r#"{"TRIPLEC_AGENT_TOKEN":""}"#,
r#"{"TRIPLEC_AGENT_TOKEN":"has space"}"#,
"SENTINEL-VALUE-7f3a",
] {
let e = agent_token_from_secret(bad).unwrap_err();
assert!(!e.contains("SENTINEL") && !e.contains("space"), "{e}");
}
}
#[tokio::test]
async fn the_token_comes_from_the_projects_own_secret() {
let fake = Fake {
secret: Some(r#"{"TRIPLEC_AGENT_TOKEN":"tok"}"#.into()),
..Default::default()
};
assert_eq!(read_agent_token(&fake, "dev", &ok(), PROJECT).await.unwrap(), "tok");
assert_eq!(fake.calls(), vec![format!("secret triple-c/dev/{}/{PROJECT}", ok())]);
}
#[tokio::test(start_paused = true)]
async fn a_new_access_point_is_waited_for_until_available() {
let fake = Fake {
ap_states: Mutex::new(vec![Some("creating".into()), Some("creating".into()), Some("available".into())]),
..Default::default()
};
wait_access_point(&fake, "fsap-1", PollTiming::default()).await.unwrap();
assert_eq!(fake.calls().len(), 3);
let stuck = Fake { ap_states: Mutex::new(vec![Some("creating".into())]), ..Default::default() };
let e = wait_access_point(&stuck, "fsap-1", PollTiming::default()).await.unwrap_err();
assert!(e.to_string().contains("did not become available"), "{e}");
let gone = Fake { ap_states: Mutex::new(vec![Some("deleting".into())]), ..Default::default() };
assert!(wait_access_point(&gone, "fsap-1", PollTiming::default()).await.is_err());
}
#[test]
fn task_arns_name_their_cluster() {
let arn = task_arn("abc123");
assert_eq!(cluster_of_task(&arn), Some("triple-c-dev"));
assert_eq!(
cluster_arn_of_task(&arn).as_deref(),
Some(format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev").as_str())
);
assert_eq!(cluster_of_task("arn:aws:ecs:r:a:task/abc"), None, "old format, no cluster");
assert_eq!(cluster_of_task("task/triple-c-dev/abc"), None);
assert_eq!(cluster_of_task("arn:aws:ecs:r:a:task/c/a/b"), None);
}
fn live(id: &str) -> TaskInfo {
TaskInfo {
arn: task_arn(id),
last_status: "RUNNING".into(),
desired_status: "RUNNING".into(),
health: None,
stop_code: None,
stopped_reason: None,
task_definition_arn: format!(
"arn:aws:ecs:{REGION}:{ACCOUNT}:task-definition/{}:2",
family()
),
runtime_id: None,
container_instance_arn: None,
exec_enabled: true,
created_unix: None,
exec_agent_running: false,
exit_code: None,
owner: Some(USER.into()),
project: Some(PROJECT.into()),
}
}
fn rev(n: u32, owner: &str) -> TaskDefinitionRef {
TaskDefinitionRef {
arn: format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task-definition/{}:{n}", family()),
family: family(),
owner: Some(owner.into()),
project: Some(PROJECT.into()),
fingerprint: None,
}
}
#[tokio::test]
async fn removal_stops_the_task_leaves_the_revisions_and_schedules_the_secret() {
let fake = Fake {
live_tasks: vec![live("t1")],
revisions: vec![rev(2, USER), rev(3, "AROAOTHER:mallory"), rev(1, USER)],
access_points: vec![AccessPointRef {
id: "fsap-found".into(),
file_system_id: "fs-1".into(),
root_path: Some(access_point_root(&ok(), PROJECT)),
owner: Some(USER.into()),
project: Some(PROJECT.into()),
state: Some("available".into()),
}],
..Default::default()
};
let key = ok();
let t0 = task_arn("t0");
let mut r = RemoveRequest {
env: "dev",
region: REGION,
owner_user_id: USER,
owner_key: &key,
project_id: PROJECT,
stored_task_arn: Some(&t0),
stored_access_point_id: Some("fsap-stored"),
stored_workspace_access_point_id: None,
ec2_cluster: None,
discard_data: false,
};
remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap();
let calls = fake.calls();
let secret = project_secret_name("dev", &ok(), PROJECT);
assert_eq!(
calls,
vec![
format!("stop triple-c-dev {} {STOP_REASON}", task_arn("t1")),
format!("stop triple-c-dev {} {STOP_REASON}", task_arn("t0")),
format!("delete-secret {secret}"),
],
"no task definition is touched (R8) and the access point is kept"
);
let keep = Fake { access_points: fake.access_points.clone(), ..Default::default() };
r.discard_data = true;
remove_workspace_resources(&keep, &keep, &keep, &r).await.unwrap();
let calls = keep.calls();
assert!(calls.contains(&"delete-ap fsap-found".to_string()), "{calls:?}");
assert!(calls.contains(&"delete-ap fsap-stored".to_string()), "{calls:?}");
}
#[tokio::test]
async fn a_failed_step_is_reported_but_the_rest_still_runs() {
let fake = Fake { fail_secret_delete: true, ..Default::default() };
let key = ok();
let r = RemoveRequest {
env: "dev",
region: REGION,
owner_user_id: USER,
owner_key: &key,
project_id: PROJECT,
stored_task_arn: None,
stored_access_point_id: Some("fsap-1"),
stored_workspace_access_point_id: None,
ec2_cluster: None,
discard_data: true,
};
let e = remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap_err();
assert!(e.to_string().contains("secret"), "{e}");
assert!(fake.calls().contains(&"delete-ap fsap-1".to_string()));
}
#[tokio::test]
async fn removal_with_discard_deletes_both_access_points_and_without_keeps_both() {
let ap = |id: &str, root: String| AccessPointRef {
id: id.into(),
file_system_id: "fs-1".into(),
root_path: Some(root),
owner: Some(USER.into()),
project: Some(PROJECT.into()),
state: Some("available".into()),
};
let points = vec![
ap("fsap-home", access_point_root(&ok(), PROJECT)),
ap("fsap-ws", workspace_access_point_root(&ok(), PROJECT)),
AccessPointRef {
owner: Some("AROAOTHER:mallory".into()),
..ap("fsap-foreign", workspace_access_point_root(&ok(), PROJECT))
},
AccessPointRef {
project: Some("another".into()),
..ap("fsap-other-project", workspace_access_point_root(&ok(), PROJECT))
},
];
let key = ok();
let mut r = RemoveRequest {
env: "dev",
region: REGION,
owner_user_id: USER,
owner_key: &key,
project_id: PROJECT,
stored_task_arn: None,
stored_access_point_id: Some("fsap-home"),
// Kept in the store after switching EFS -> Hybrid.
stored_workspace_access_point_id: Some("fsap-ws-stored"),
ec2_cluster: None,
discard_data: false,
};
let keep = Fake { access_points: points.clone(), ..Default::default() };
remove_workspace_resources(&keep, &keep, &keep, &r).await.unwrap();
assert!(
keep.calls().iter().all(|c| !c.starts_with("delete-ap")),
"{:?}",
keep.calls()
);
r.discard_data = true;
let discard = Fake { access_points: points, ..Default::default() };
remove_workspace_resources(&discard, &discard, &discard, &r).await.unwrap();
let mut deleted: Vec<String> = discard
.calls()
.into_iter()
.filter_map(|c| c.strip_prefix("delete-ap ").map(String::from))
.collect();
deleted.sort();
assert_eq!(deleted, vec!["fsap-home", "fsap-ws", "fsap-ws-stored"]);
}
// ------------------------------------------------------------- EC2 (phase 7)
fn ec2_task_arn(id: &str) -> String {
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/triple-c-dev--u-alice/{id}")
}
#[test]
fn a_stored_task_on_a_per_user_cluster_names_that_cluster() {
assert_eq!(
ec2_cluster_of_task("dev", &ec2_task_arn("t")).as_deref(),
Some(format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice").as_str())
);
// The environment's own cluster, another environment's per-user
// cluster and anything else are not.
assert_eq!(ec2_cluster_of_task("dev", &task_arn("t")), None);
assert_eq!(ec2_cluster_of_task("prod", &ec2_task_arn("t")), None);
let odd = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/triple-c-dev--u-a--b/t");
assert_eq!(ec2_cluster_of_task("dev", &odd), None);
assert_eq!(ec2_cluster_of_task("dev", "not-an-arn"), None);
}
#[tokio::test]
async fn the_user_record_is_read_and_validated() {
struct Rec(Param);
impl Rec {
async fn read(&self) -> UserRecord {
let fake = RecFake(self.0.clone(), Mutex::new(Vec::new()));
let r = read_user_record(&fake, "dev", &ok(), ACCOUNT, REGION).await.unwrap();
assert_eq!(*fake.1.lock().unwrap(), vec![format!("dev {}", ok())]);
r
}
}
let valid = serde_json::json!({
"schema": 1, "instance_type": "m7i.large", "max_instances": 2, "user_tag": "alice",
"cluster_arn": format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice"),
"capacity_provider": "triple-c-dev--u-alice",
})
.to_string();
let UserRecord::Valid(c) = Rec(Param::Value(valid)).read().await else { panic!() };
assert_eq!((c.instance_type.as_str(), c.max_instances), ("m7i.large", 2));
assert_eq!(Rec(Param::Missing).read().await, UserRecord::Absent);
assert!(matches!(Rec(Param::Denied).read().await, UserRecord::Unusable(_)));
assert!(matches!(Rec(Param::Value("{}".into())).read().await, UserRecord::Unusable(_)));
}
/// Only `user_record` is asked of it.
struct RecFake(Param, Mutex<Vec<String>>);
impl RuntimeApi for RecFake {
async fn environment_record(&self, _env: &str) -> Result<Option<String>, AwsError> {
unreachable!()
}
async fn user_record(&self, env: &str, owner_key: &str) -> Result<Param, AwsError> {
self.1.lock().unwrap().push(format!("{env} {owner_key}"));
Ok(self.0.clone())
}
async fn role_exists(&self, _name: &str) -> Result<Knowable, AwsError> {
unreachable!()
}
async fn secret_string(&self, _name: &str) -> Result<String, AwsError> {
unreachable!()
}
async fn access_point_state(&self, _id: &str) -> Result<Option<String>, AwsError> {
unreachable!()
}
async fn delete_secret(&self, _name: &str) -> Result<(), AwsError> {
unreachable!()
}
async fn delete_access_point(&self, _id: &str) -> Result<(), AwsError> {
unreachable!()
}
}
#[tokio::test]
async fn removal_finds_and_stops_a_task_on_the_users_ec2_cluster() {
let key = ok();
let user_cluster = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice");
let base = RemoveRequest {
env: "dev",
region: REGION,
owner_user_id: USER,
owner_key: &key,
project_id: PROJECT,
stored_task_arn: None,
stored_access_point_id: None,
stored_workspace_access_point_id: None,
ec2_cluster: Some(&user_cluster),
discard_data: false,
};
// Found through the record's cluster.
let mut t = live("e1");
t.arn = ec2_task_arn("e1");
let fake = Fake { live_tasks: vec![t.clone()], ..Default::default() };
remove_workspace_resources(&fake, &fake, &fake, &base).await.unwrap();
assert_eq!(*fake.lists.lock().unwrap(), ["triple-c-dev", "triple-c-dev--u-alice"]);
let calls = fake.calls();
assert!(
calls.contains(&format!("stop {user_cluster} {} {STOP_REASON}", ec2_task_arn("e1"))),
"{calls:?}"
);
// The record is gone: the stored ARN still leads to its cluster.
let stored = ec2_task_arn("e1");
let fake = Fake { live_tasks: vec![t], ..Default::default() };
let r = RemoveRequest { ec2_cluster: None, stored_task_arn: Some(&stored), ..base };
remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap();
let stops: Vec<String> = fake.calls().into_iter().filter(|c| c.starts_with("stop")).collect();
assert_eq!(stops.len(), 1, "{stops:?}");
assert!(stops[0].contains(&ec2_task_arn("e1")), "{stops:?}");
// A stored ARN on someone else's kind of cluster is not stopped.
let foreign = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/other-cluster/x");
let fake = Fake::default();
let r = RemoveRequest { ec2_cluster: None, stored_task_arn: Some(&foreign), ..base };
remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap();
assert!(!fake.calls().iter().any(|c| c.starts_with("stop")));
}
+824
View File
@@ -0,0 +1,824 @@
//! A project's ECS task: find the one already running, or run one; wait
//! until it is ready; stop it; and read why it stopped.
//!
//! **A task is trusted by its tags, not its family.** Family names are built
//! from a 12-character id prefix and any workspace user may register into any
//! `triple-c-*` family, so a task listed under the project's family is reused
//! only when DescribeTasks shows the caller's own `triple-c:owner` (which only
//! the caller can set, at RunTask) and this project's `triple-c:project`.
//!
//! **RunTask names an exact revision.** Never a bare family: "the latest
//! revision" of a family is whatever anyone registered last.
//!
//! **A quota refusal is final.** Fargate's vCPU limit (and its kin) surface as
//! [`AwsError::QuotaExceeded`] with a message saying what to do; nothing here
//! retries a RunTask, and the SDK's own retries are made idempotent by a
//! client token.
use std::future::Future;
use std::time::Duration;
use aws_config::SdkConfig;
use aws_sdk_ecs::operation::run_task::RunTaskInput;
use aws_sdk_ecs::types::{
AssignPublicIp, AwsVpcConfiguration, CapacityProviderStrategyItem, DesiredStatus, LaunchType,
ManagedAgentName, NetworkConfiguration, Tag, Task, TaskField,
};
use super::environments::{EnvironmentInfo, UserCompute};
use super::error::{classify_sdk_error, AwsError};
pub use super::tofu_modules::task_log_stream;
use super::workspace::{workspace_tags, CONTAINER_NAME, TAG_OWNER, TAG_PROJECT};
/// The `reason` StopTask records when the app stops a task.
pub const STOP_REASON: &str = "Stopped from Triple-C";
/// Log lines shown when a task stops before it is ready.
pub const LOG_TAIL_LINES: i32 = 50;
const SPOT_PROVIDER: &str = "FARGATE_SPOT";
/// DescribeTasks takes at most 100 tasks per call.
const DESCRIBE_BATCH: usize = 100;
/// ListTasks pages read per status; a project has one task, so this is slack.
const MAX_LIST_PAGES: usize = 5;
fn other(message: impl Into<String>) -> AwsError {
AwsError::Other {
message: message.into(),
}
}
/// What the app needs to know about a task.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskInfo {
pub arn: String,
/// `PROVISIONING`, `PENDING`, `ACTIVATING`, `RUNNING`, `DEACTIVATING`,
/// `STOPPING`, `DEPROVISIONING`, `STOPPED`.
pub last_status: String,
pub desired_status: String,
/// `HEALTHY`, `UNHEALTHY`, `UNKNOWN`.
pub health: Option<String>,
/// ECS `stopCode` (`EssentialContainerExited`, `SpotInterruption`, ...).
pub stop_code: Option<String>,
pub stopped_reason: Option<String>,
pub task_definition_arn: String,
/// The `workspace` container's runtime id (needed for an SSM target).
pub runtime_id: Option<String>,
/// The EC2 container instance the task was placed on; `None` on Fargate,
/// and on EC2 until an instance has room for it.
pub container_instance_arn: Option<String>,
/// The `workspace` container's ExecuteCommandAgent is RUNNING.
pub exec_agent_running: bool,
/// ECS Exec is on for the task (`enableExecuteCommand`). ECS turns it
/// off on a RunTask that has to wait for an EC2 instance (verified live,
/// phase 7), and such a task can never be reached.
pub exec_enabled: bool,
/// When ECS created the task (Unix seconds), if it says.
pub created_unix: Option<i64>,
/// The `workspace` container's exit code, once it has one.
pub exit_code: Option<i32>,
/// Its `triple-c:owner` / `triple-c:project` tags (DescribeTasks with
/// TAGS; RunTask's answer carries the tags it was given).
pub owner: Option<String>,
pub project: Option<String>,
}
impl TaskInfo {
fn is_stopping(&self) -> bool {
self.last_status == "STOPPED" || self.desired_status == "STOPPED"
}
/// On its way up or up, and meant to stay that way.
fn is_live(&self) -> bool {
self.desired_status == "RUNNING"
&& matches!(
self.last_status.as_str(),
"PROVISIONING" | "PENDING" | "ACTIVATING" | "RUNNING"
)
}
fn is_ready(&self) -> bool {
self.last_status == "RUNNING" && self.exec_agent_running && self.runtime_id.is_some()
}
}
fn tag_value(tags: &[Tag], key: &str) -> Option<String> {
tags.iter()
.find(|t| t.key() == Some(key))
.and_then(|t| t.value())
.map(str::to_string)
}
/// An SDK task as a [`TaskInfo`]. The container facts come from the
/// `workspace` container only.
pub(crate) fn task_info(t: &Task) -> TaskInfo {
let workspace = t
.containers()
.iter()
.find(|c| c.name() == Some(CONTAINER_NAME));
TaskInfo {
arn: t.task_arn().unwrap_or_default().to_string(),
last_status: t.last_status().unwrap_or_default().to_string(),
desired_status: t.desired_status().unwrap_or_default().to_string(),
health: t.health_status().map(|h| h.as_str().to_string()),
stop_code: t.stop_code().map(|c| c.as_str().to_string()),
stopped_reason: t.stopped_reason().map(str::to_string),
task_definition_arn: t.task_definition_arn().unwrap_or_default().to_string(),
runtime_id: workspace.and_then(|c| c.runtime_id()).map(str::to_string),
container_instance_arn: t.container_instance_arn().map(str::to_string),
exec_enabled: t.enable_execute_command(),
created_unix: t.created_at().map(|d| d.secs()),
exec_agent_running: workspace.is_some_and(|c| {
c.managed_agents().iter().any(|a| {
a.name() == Some(&ManagedAgentName::ExecuteCommandAgent)
&& a.last_status() == Some("RUNNING")
})
}),
exit_code: workspace.and_then(|c| c.exit_code()),
owner: tag_value(t.tags(), TAG_OWNER),
project: tag_value(t.tags(), TAG_PROJECT),
}
}
/// What running and watching tasks asks AWS. Like `WorkspaceApi`, each
/// method returns a `Send` future and an impl may write them as `async fn`;
/// the decisions are made by the functions below, so a fake exercises them.
pub trait TaskApi: Send + Sync {
/// RunTask. A quota refusal is [`AwsError::QuotaExceeded`].
fn run_task(
&self,
req: RunTaskInput,
) -> impl Future<Output = Result<TaskInfo, AwsError>> + Send;
/// DescribeTasks (with tags). Tasks ECS no longer knows are left out.
fn describe(
&self,
cluster: &str,
arns: &[String],
) -> impl Future<Output = Result<Vec<TaskInfo>, AwsError>> + Send;
/// ListTasks of exactly `family`, desired status RUNNING and PENDING.
fn list_project_tasks(
&self,
cluster: &str,
family: &str,
) -> impl Future<Output = Result<Vec<String>, AwsError>> + Send;
fn stop_task(
&self,
cluster: &str,
arn: &str,
reason: &str,
) -> impl Future<Output = Result<(), AwsError>> + Send;
/// The last `limit` lines of a log stream, oldest first. A stream (or
/// group) that does not exist is no lines, not an error.
fn log_tail(
&self,
group: &str,
stream: &str,
limit: i32,
) -> impl Future<Output = Result<Vec<String>, AwsError>> + Send;
}
// ------------------------------------------------------------- run request
/// The RunTask request: the exact revision, on the environment's cluster and
/// network, with ECS Exec on and exactly the `tags` given (no propagation,
/// no ECS-managed tags, no overrides — P1). Spot runs on the `FARGATE_SPOT`
/// capacity provider with no launch type (the two are exclusive).
pub fn run_task_request(
env: &EnvironmentInfo,
task_def_arn: &str,
spot: bool,
tags: &[(String, String)],
) -> RunTaskInput {
const BUILT: &str = "every required field is set";
let mut req = RunTaskInput::builder()
.cluster(&env.cluster_arn)
.task_definition(task_def_arn)
.count(1)
.enable_execute_command(true)
.network_configuration(
NetworkConfiguration::builder()
.awsvpc_configuration(
AwsVpcConfiguration::builder()
.set_subnets(Some(env.subnet_ids.clone()))
.security_groups(&env.security_group_id)
.assign_public_ip(if env.assign_public_ip {
AssignPublicIp::Enabled
} else {
AssignPublicIp::Disabled
})
.build()
.expect(BUILT),
)
.build(),
)
.set_tags(Some(
tags.iter()
.map(|(k, v)| Tag::builder().key(k).value(v).build())
.collect(),
))
// SDK retries of this call must not start a second task.
.client_token(uuid::Uuid::new_v4().to_string());
req = if spot {
req.capacity_provider_strategy(
CapacityProviderStrategyItem::builder()
.capacity_provider(SPOT_PROVIDER)
.weight(1)
.build()
.expect(BUILT),
)
} else {
req.launch_type(LaunchType::Fargate)
};
req.build().expect(BUILT)
}
/// Where a new task runs.
#[derive(Debug, Clone, Copy)]
pub enum RunOn<'a> {
/// The environment's cluster, Fargate (or `FARGATE_SPOT`).
Fargate { spot: bool },
/// The user's own cluster and capacity provider (phase 7), from their
/// record.
Ec2(&'a UserCompute),
}
impl RunOn<'_> {
/// The cluster a task runs on, and is looked for in.
pub fn cluster<'e>(&'e self, env: &'e EnvironmentInfo) -> &'e str {
match self {
RunOn::Fargate { .. } => &env.cluster_arn,
RunOn::Ec2(c) => &c.cluster_arn,
}
}
}
/// The RunTask request on the user's EC2 capacity: the exact revision, on
/// their cluster, placed by their capacity provider (weight 1). No launch
/// type (exclusive with a strategy) and no network configuration: EC2 tasks
/// use `bridge` networking (ruling R2). ECS Exec, tags, the client token and
/// no overrides as on Fargate (P1).
pub fn ec2_run_task_request(
compute: &UserCompute,
task_def_arn: &str,
tags: &[(String, String)],
) -> RunTaskInput {
const BUILT: &str = "every required field is set";
RunTaskInput::builder()
.cluster(&compute.cluster_arn)
.task_definition(task_def_arn)
.count(1)
.enable_execute_command(true)
.capacity_provider_strategy(
CapacityProviderStrategyItem::builder()
.capacity_provider(&compute.capacity_provider)
.weight(1)
.build()
.expect(BUILT),
)
.set_tags(Some(
tags.iter()
.map(|(k, v)| Tag::builder().key(k).value(v).build())
.collect(),
))
.client_token(uuid::Uuid::new_v4().to_string())
.build()
.expect(BUILT)
}
/// RunTask refused on a per-user cluster: the workspace policy lets a caller
/// run tasks only on the cluster named after their own `triple-c-user`
/// principal tag.
pub const EC2_TAG_MISMATCH: &str = "This EC2 capacity belongs to a different triple-c-user value \
than your sign-in's (or your sign-in has no triple-c-user tag). Ask an admin.";
/// The family of `arn:...:task-definition/<family>:<revision>`, when the
/// revision is a number. `None` for a bare family or anything else.
pub fn revision_family(arn: &str) -> Option<&str> {
if !arn.starts_with("arn:") {
return None;
}
let (_, rest) = arn.rsplit_once(":task-definition/")?;
let (family, rev) = rest.rsplit_once(':')?;
(!family.is_empty()
&& !family.contains('/')
&& !rev.is_empty()
&& rev.bytes().all(|c| c.is_ascii_digit()))
.then_some(family)
}
// ------------------------------------------------------------- find or run
/// The project's live task (provisioning through running, not being
/// stopped), owned by the caller and tagged with this project. A RUNNING
/// one is preferred.
pub async fn find_project_task<A: TaskApi>(
api: &A,
cluster: &str,
family: &str,
owner_user_id: &str,
project_id: &str,
) -> Result<Option<TaskInfo>, AwsError> {
let arns = api.list_project_tasks(cluster, family).await?;
if arns.is_empty() {
return Ok(None);
}
let mut mine: Vec<TaskInfo> = Vec::new();
for chunk in arns.chunks(DESCRIBE_BATCH) {
mine.extend(api.describe(cluster, chunk).await?.into_iter().filter(|t| {
t.owner.as_deref() == Some(owner_user_id)
&& t.project.as_deref() == Some(project_id)
&& revision_family(&t.task_definition_arn) == Some(family)
&& t.is_live()
}));
}
mine.sort_by_key(|t| t.last_status != "RUNNING");
Ok(mine.into_iter().next())
}
/// What [`find_or_run`] did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskStart {
pub task: TaskInfo,
/// An existing task was found (nothing was run). Its agent token is the
/// one it started with — see [`find_project_task`] in the report for
/// why a caller should look before rotating the token.
pub reused: bool,
}
/// The project's live task if there is one (Review Focus 2: never a second
/// task for a project), else RunTask with the exact `task_def_arn` where
/// `run_on` says.
pub async fn find_or_run<A: TaskApi>(
api: &A,
env: &EnvironmentInfo,
owner_user_id: &str,
project_id: &str,
task_def_arn: &str,
run_on: RunOn<'_>,
) -> Result<TaskStart, AwsError> {
let family = revision_family(task_def_arn).ok_or_else(|| {
other(format!(
"\"{task_def_arn}\" is not an exact task definition revision"
))
})?;
if !family.starts_with("triple-c-") {
return Err(other(format!(
"\"{task_def_arn}\" is not a Triple-C task definition"
)));
}
if let Some(task) =
find_project_task(api, run_on.cluster(env), family, owner_user_id, project_id).await?
{
return Ok(TaskStart { task, reused: true });
}
let tags = workspace_tags(owner_user_id, project_id, &env.name);
let task = match run_on {
RunOn::Fargate { spot } => {
api.run_task(run_task_request(env, task_def_arn, spot, &tags)).await?
}
RunOn::Ec2(compute) => api
.run_task(ec2_run_task_request(compute, task_def_arn, &tags))
.await
.map_err(ec2_run_error)?,
};
Ok(TaskStart {
task,
reused: false,
})
}
/// A RunTask error on the user's EC2 capacity, in its terms: access denied is
/// the `triple-c-user` mismatch ([`EC2_TAG_MISMATCH`]); a placement failure
/// (`RESOURCE:CPU`/`RESOURCE:MEMORY`) means no instance can take the task.
pub fn ec2_run_error(e: AwsError) -> AwsError {
match e {
AwsError::AccessDenied { action, message } => AwsError::AccessDenied {
action,
message: format!("{EC2_TAG_MISMATCH} (AWS said: {message})"),
},
AwsError::Other { message } if message.contains("RESOURCE:") => other(format!(
"{message}. No instance of your EC2 capacity has room for this task: lower its CPU \
or memory reservation, or stop another EC2 workspace."
)),
e => e,
}
}
// ------------------------------------------------------------------ quotas
/// Whether an AWS message says an account limit or quota was hit.
pub fn looks_like_quota(text: &str) -> bool {
let t = text.to_lowercase();
[
"you've reached the limit",
"you\u{2019}ve reached the limit",
"limitexceeded",
"limit exceeded",
"quota",
]
.iter()
.any(|s| t.contains(s))
}
/// The error for a RunTask refused on a quota.
pub fn quota_error(detail: &str) -> AwsError {
AwsError::QuotaExceeded {
message: format!(
"AWS would not start the task because an account limit was reached: {detail}. \
Stop another workspace, or ask an administrator to raise the Fargate quota \
(Service Quotas, AWS Fargate) for this region. Triple-C does not retry."
),
}
}
/// `e` as a quota error when its message is one; otherwise unchanged.
pub fn quota_or(e: AwsError) -> AwsError {
match e {
AwsError::Other { ref message } if looks_like_quota(message) => quota_error(message),
e => e,
}
}
/// RunTask's answer: its one task, or an error from its `failures`
/// (`(reason, detail)`), a quota one when they say so.
pub fn run_task_outcome(
tasks: Vec<TaskInfo>,
failures: Vec<(Option<String>, Option<String>)>,
) -> Result<TaskInfo, AwsError> {
if let Some(task) = tasks.into_iter().find(|t| !t.arn.is_empty()) {
return Ok(task);
}
let text = failures
.iter()
.map(|(r, d)| match (r, d) {
(Some(r), Some(d)) => format!("{r} ({d})"),
(Some(r), None) => r.clone(),
(None, Some(d)) => d.clone(),
(None, None) => "unknown failure".into(),
})
.collect::<Vec<_>>()
.join("; ");
if text.is_empty() {
return Err(other("ecs:RunTask returned no task and no failure"));
}
if looks_like_quota(&text) {
return Err(quota_error(&text));
}
Err(other(format!("ECS could not start the task: {text}")))
}
// -------------------------------------------------------------------- wait
/// How [`wait_ready`] polls. Injectable so tests don't wait.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WaitTiming {
pub interval: Duration,
pub timeout: Duration,
}
impl Default for WaitTiming {
fn default() -> Self {
WaitTiming {
interval: Duration::from_secs(5),
timeout: Duration::from_secs(6 * 60),
}
}
}
impl WaitTiming {
/// An EC2 task's wait: a cold start (capacity scaled to zero) launches
/// an instance and pulls the image before the task can run.
pub fn ec2() -> Self {
WaitTiming {
timeout: Duration::from_secs(10 * 60),
..Self::default()
}
}
}
/// An EC2 task still waiting for an instance with room: ECS keeps it
/// PROVISIONING (or PENDING) with no container instance while the capacity
/// provider's managed scaling launches one. (A Fargate task never has a
/// container instance, so ask this only of EC2 tasks.)
pub fn waiting_for_instance(t: &TaskInfo) -> bool {
t.container_instance_arn.is_none()
&& t.desired_status == "RUNNING"
&& matches!(t.last_status.as_str(), "PROVISIONING" | "PENDING")
}
/// How a [`wait_ready`] timeout begins.
pub const WAIT_TIMED_OUT: &str = "The task did not become ready within";
/// Where a project's task logs go: the environment's log group and the
/// project's `awslogs-stream-prefix` (Task 4's `log_stream_prefix`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskLogs {
pub group: String,
pub stream_prefix: String,
}
fn describe_timeout(t: Duration) -> String {
let secs = t.as_secs();
if secs >= 60 && secs % 60 == 0 {
let m = secs / 60;
format!("{m} minute{}", if m == 1 { "" } else { "s" })
} else {
format!("{secs} seconds")
}
}
/// Poll until the task is RUNNING with its exec agent up and a runtime id
/// (what an SSM session needs), calling `progress` with each look. A task
/// that stops (or is being stopped) fails with its reason and the last
/// [`LOG_TAIL_LINES`] lines of its log; one that never gets there fails
/// after `timing.timeout` with its last status. The task is left as it is
/// either way: stopping it is the caller's call.
pub async fn wait_ready<A: TaskApi>(
api: &A,
cluster: &str,
arn: &str,
logs: &TaskLogs,
timing: WaitTiming,
progress: impl Fn(&TaskInfo),
) -> Result<TaskInfo, AwsError> {
let deadline = tokio::time::Instant::now() + timing.timeout;
let wanted = [arn.to_string()];
loop {
let Some(t) = api
.describe(cluster, &wanted)
.await?
.into_iter()
.find(|t| t.arn == arn)
else {
return Err(other(format!(
"The task {arn} is no longer known to ECS; start the project again"
)));
};
progress(&t);
if t.is_stopping() {
return Err(stopped_error(api, logs, &t).await);
}
if t.is_ready() {
return Ok(t);
}
if tokio::time::Instant::now() + timing.interval > deadline {
return Err(other(format!(
"{WAIT_TIMED_OUT} {} (last status {}{}). It may still \
be starting; check the ECS console, or stop the project and start it again.",
describe_timeout(timing.timeout),
t.last_status,
if t.last_status == "RUNNING" && !t.exec_agent_running {
", ECS Exec agent not running yet"
} else {
""
}
)));
}
tokio::time::sleep(timing.interval).await;
}
}
async fn stopped_error<A: TaskApi>(api: &A, logs: &TaskLogs, t: &TaskInfo) -> AwsError {
let reason = t
.stopped_reason
.clone()
.unwrap_or_else(|| "no reason given".into());
let code = t
.stop_code
.as_deref()
.map(|c| format!(" ({c})"))
.unwrap_or_default();
let stream = task_log_stream(&logs.stream_prefix, &t.arn);
let tail = match api.log_tail(&logs.group, &stream, LOG_TAIL_LINES).await {
Ok(lines) if lines.is_empty() => "\n(The task wrote no log lines.)".to_string(),
Ok(lines) => format!("\nLast log lines:\n{}", lines.join("\n")),
Err(e) => format!("\n(Its log could not be read: {e})"),
};
other(format!(
"The task stopped before it was ready: {reason}{code}.{tail}"
))
}
// ----------------------------------------------------------- stop / watch
/// The entrypoint's exit status after an idle stop (`container/entrypoint.sh`,
/// `stop_keepalive`). A stop from ECS (SIGTERM) exits 0; this one is distinct
/// so an idle stop (`EssentialContainerExited`) is not mistaken for a crash.
pub const IDLE_EXIT_CODE: i32 = 75;
/// The entrypoint's exit status after the environment's lifetime cap stopped
/// the workspace (`TRIPLEC_MAX_LIFETIME_MINUTES`, enforced by the in-container
/// idle watcher — advisory, see the phase 6 constraints, Q8).
pub const LIFETIME_EXIT_CODE: i32 = 76;
/// Why a task stopped, as the app reports it (spec, "Lifecycle and status").
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StopKind {
/// Exited on its own (the essential container ended, or AWS retired it),
/// with ECS's stopped reason when it gave one.
Exited(Option<String>),
/// The workspace's own idle stop ([`IDLE_EXIT_CODE`]).
Idle,
/// The environment's maximum workspace lifetime ([`LIFETIME_EXIT_CODE`]).
LifetimeCap,
/// A Fargate Spot interruption (P8).
Spot,
/// Never got going: the project goes to Error with this reason.
FailedToStart(String),
/// Stopped by someone (the app, the console, the CLI).
UserInitiated,
}
pub fn classify_stop(info: &TaskInfo) -> StopKind {
match info.stop_code.as_deref() {
Some("SpotInterruption") => StopKind::Spot,
Some("TaskFailedToStart") => StopKind::FailedToStart(
info.stopped_reason
.clone()
.filter(|r| !r.trim().is_empty())
.unwrap_or_else(|| "The task failed to start".into()),
),
Some("UserInitiated") => StopKind::UserInitiated,
Some("EssentialContainerExited") if info.exit_code == Some(IDLE_EXIT_CODE) => StopKind::Idle,
Some("EssentialContainerExited") if info.exit_code == Some(LIFETIME_EXIT_CODE) => {
StopKind::LifetimeCap
}
_ => StopKind::Exited(info.stopped_reason.clone().filter(|r| !r.trim().is_empty())),
}
}
/// Of `arns`, the ones that are STOPPED or that ECS no longer knows (it
/// forgets stopped tasks after about an hour), each with why it stopped (a
/// forgotten task: [`StopKind::Exited`] without a reason). A task merely
/// being stopped is not yet reported. For the AWS death watch.
pub async fn stopped_among<A: TaskApi>(
api: &A,
cluster: &str,
arns: &[String],
) -> Result<Vec<(String, StopKind)>, AwsError> {
let mut stopped = Vec::new();
for chunk in arns.chunks(DESCRIBE_BATCH) {
let known = api.describe(cluster, chunk).await?;
for arn in chunk {
match known.iter().find(|t| &t.arn == arn) {
Some(t) if t.last_status != "STOPPED" => {}
Some(t) => stopped.push((arn.clone(), classify_stop(t))),
None => stopped.push((arn.clone(), StopKind::Exited(None))),
}
}
}
Ok(stopped)
}
// ---------------------------------------------------------------- real API
/// The real thing, from a connection's `SdkConfig`.
pub struct SdkTaskApi {
ecs: aws_sdk_ecs::Client,
logs: aws_sdk_cloudwatchlogs::Client,
}
impl SdkTaskApi {
pub fn new(cfg: &SdkConfig) -> Self {
SdkTaskApi {
ecs: aws_sdk_ecs::Client::new(cfg),
logs: aws_sdk_cloudwatchlogs::Client::new(cfg),
}
}
}
impl TaskApi for SdkTaskApi {
async fn run_task(&self, req: RunTaskInput) -> Result<TaskInfo, AwsError> {
const ACTION: &str = "ecs:RunTask";
// The fluent builder takes no whole input; copy every field.
let out = self
.ecs
.run_task()
.set_capacity_provider_strategy(req.capacity_provider_strategy)
.set_cluster(req.cluster)
.set_count(req.count)
.set_enable_ecs_managed_tags(req.enable_ecs_managed_tags)
.set_enable_execute_command(req.enable_execute_command)
.set_group(req.group)
.set_launch_type(req.launch_type)
.set_network_configuration(req.network_configuration)
.set_overrides(req.overrides)
.set_placement_constraints(req.placement_constraints)
.set_placement_strategy(req.placement_strategy)
.set_platform_version(req.platform_version)
.set_propagate_tags(req.propagate_tags)
.set_reference_id(req.reference_id)
.set_started_by(req.started_by)
.set_tags(req.tags)
.set_task_definition(req.task_definition)
.set_client_token(req.client_token)
.set_volume_configurations(req.volume_configurations)
.send()
.await
.map_err(|e| quota_or(classify_sdk_error(ACTION, &e)))?;
run_task_outcome(
out.tasks().iter().map(task_info).collect(),
out.failures()
.iter()
.map(|f| (f.reason().map(String::from), f.detail().map(String::from)))
.collect(),
)
}
async fn describe(&self, cluster: &str, arns: &[String]) -> Result<Vec<TaskInfo>, AwsError> {
const ACTION: &str = "ecs:DescribeTasks";
let mut out = Vec::new();
for chunk in arns.chunks(DESCRIBE_BATCH) {
let page = self
.ecs
.describe_tasks()
.cluster(cluster)
.set_tasks(Some(chunk.to_vec()))
.include(TaskField::Tags)
.send()
.await
.map_err(|e| classify_sdk_error(ACTION, &e))?;
out.extend(page.tasks().iter().map(task_info));
}
Ok(out)
}
async fn list_project_tasks(
&self,
cluster: &str,
family: &str,
) -> Result<Vec<String>, AwsError> {
const ACTION: &str = "ecs:ListTasks";
let mut found: Vec<String> = Vec::new();
for status in [DesiredStatus::Running, DesiredStatus::Pending] {
let mut token: Option<String> = None;
for _ in 0..MAX_LIST_PAGES {
let page = self
.ecs
.list_tasks()
.cluster(cluster)
.family(family)
.desired_status(status.clone())
.set_next_token(token.take())
.send()
.await
.map_err(|e| classify_sdk_error(ACTION, &e))?;
for a in page.task_arns() {
if !found.contains(a) {
found.push(a.clone());
}
}
match page.next_token() {
Some(t) => token = Some(t.to_string()),
None => break,
}
}
}
Ok(found)
}
async fn stop_task(&self, cluster: &str, arn: &str, reason: &str) -> Result<(), AwsError> {
const ACTION: &str = "ecs:StopTask";
self.ecs
.stop_task()
.cluster(cluster)
.task(arn)
.reason(reason)
.send()
.await
.map_err(|e| classify_sdk_error(ACTION, &e))?;
Ok(())
}
async fn log_tail(
&self,
group: &str,
stream: &str,
limit: i32,
) -> Result<Vec<String>, AwsError> {
const ACTION: &str = "logs:GetLogEvents";
let out = self
.logs
.get_log_events()
.log_group_name(group)
.log_stream_name(stream)
.start_from_head(false)
.limit(limit)
.send()
.await;
match out {
Ok(page) => Ok(page
.events()
.iter()
.filter_map(|e| e.message())
.map(str::to_string)
.collect()),
Err(e)
if e.as_service_error()
.is_some_and(|s| s.is_resource_not_found_exception()) =>
{
Ok(Vec::new())
}
Err(e) => Err(classify_sdk_error(ACTION, &e)),
}
}
}
#[cfg(test)]
mod tests;
+883
View File
@@ -0,0 +1,883 @@
use std::sync::Mutex;
use std::time::Duration;
use aws_sdk_ecs::types::{
AssignPublicIp, Container, LaunchType, ManagedAgent, ManagedAgentName, Tag, Task, TaskStopCode,
};
use super::*;
use crate::aws::environments::EnvironmentInfo;
use crate::aws::tofu_modules::{log_stream_prefix, owner_key};
const ACCOUNT: &str = "111122223333";
const REGION: &str = "eu-west-1";
const USER: &str = "AROAEXAMPLEID:alice@example.com";
const OTHER: &str = "AROAOTHERID:mallory";
const PROJECT_ID: &str = "0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0";
const FAMILY: &str = "triple-c-dev-abcdef012345-0f1e2d3c-4b5";
fn cluster() -> String {
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev")
}
fn td_arn(rev: u32) -> String {
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task-definition/{FAMILY}:{rev}")
}
fn task_arn(id: &str) -> String {
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/triple-c-dev/{id}")
}
fn env_info() -> EnvironmentInfo {
EnvironmentInfo {
schema: 1,
name: "dev".into(),
template_version: 1,
cluster_arn: cluster(),
security_group_id: "sg-0123456789abcdef0".into(),
subnet_ids: vec!["subnet-0a".into(), "subnet-0b".into()],
assign_public_ip: true,
kms_key_arn: format!("arn:aws:kms:{REGION}:{ACCOUNT}:key/1234"),
log_group: "/triple-c/dev".into(),
network_mode: "public".into(),
tenancy: "strict".into(),
max_workspace_hours: 0,
}
}
fn is_quota_error(e: &AwsError) -> bool {
matches!(e, AwsError::QuotaExceeded { .. })
}
fn info(id: &str, last: &str) -> TaskInfo {
TaskInfo {
arn: task_arn(id),
last_status: last.into(),
desired_status: if last == "STOPPED" {
"STOPPED"
} else {
"RUNNING"
}
.into(),
health: None,
stop_code: None,
stopped_reason: None,
task_definition_arn: td_arn(3),
runtime_id: None,
container_instance_arn: None,
exec_enabled: true,
created_unix: None,
exec_agent_running: false,
exit_code: None,
owner: Some(USER.into()),
project: Some(PROJECT_ID.into()),
}
}
fn ready(id: &str) -> TaskInfo {
TaskInfo {
runtime_id: Some(format!("{id}-123")),
exec_agent_running: true,
..info(id, "RUNNING")
}
}
/// A scripted `TaskApi`: `listed` answers ListTasks, `described` answers
/// every DescribeTasks in turn (the last one repeats), `run` answers RunTask.
#[derive(Default)]
struct Fake {
listed: Vec<String>,
described: Mutex<Vec<Vec<TaskInfo>>>,
run: Mutex<Option<Result<TaskInfo, AwsError>>>,
logs: Vec<String>,
calls: Mutex<Vec<String>>,
run_requests: Mutex<Vec<RunTaskInput>>,
log_requests: Mutex<Vec<(String, String, i32)>>,
}
impl Fake {
fn calls(&self) -> Vec<String> {
self.calls.lock().unwrap().clone()
}
fn describing(self, answers: Vec<Vec<TaskInfo>>) -> Self {
*self.described.lock().unwrap() = answers;
self
}
fn running(self, r: Result<TaskInfo, AwsError>) -> Self {
*self.run.lock().unwrap() = Some(r);
self
}
}
impl TaskApi for Fake {
async fn run_task(&self, req: RunTaskInput) -> Result<TaskInfo, AwsError> {
self.calls.lock().unwrap().push("run".into());
self.run_requests.lock().unwrap().push(req);
self.run
.lock()
.unwrap()
.clone()
.unwrap_or_else(|| Ok(info("new", "PROVISIONING")))
}
async fn describe(&self, cluster: &str, arns: &[String]) -> Result<Vec<TaskInfo>, AwsError> {
self.calls
.lock()
.unwrap()
.push(format!("describe {cluster} {}", arns.join(",")));
let mut d = self.described.lock().unwrap();
let answer = if d.len() > 1 {
d.remove(0)
} else {
d.first().cloned().unwrap_or_default()
};
Ok(answer
.into_iter()
.filter(|t| arns.contains(&t.arn))
.collect())
}
async fn list_project_tasks(
&self,
cluster: &str,
family: &str,
) -> Result<Vec<String>, AwsError> {
self.calls
.lock()
.unwrap()
.push(format!("list {cluster} {family}"));
Ok(self.listed.clone())
}
async fn stop_task(&self, _cluster: &str, arn: &str, reason: &str) -> Result<(), AwsError> {
self.calls
.lock()
.unwrap()
.push(format!("stop {arn} {reason}"));
Ok(())
}
async fn log_tail(
&self,
group: &str,
stream: &str,
limit: i32,
) -> Result<Vec<String>, AwsError> {
self.log_requests
.lock()
.unwrap()
.push((group.into(), stream.into(), limit));
Ok(self.logs.clone())
}
}
fn tags() -> Vec<(String, String)> {
crate::aws::workspace::workspace_tags(USER, PROJECT_ID, "dev")
}
// ------------------------------------------------------------ run request
#[test]
fn on_demand_runs_fargate_on_the_environment_network_with_exec_and_the_three_tags() {
let req = run_task_request(&env_info(), &td_arn(3), false, &tags());
assert_eq!(req.cluster(), Some(cluster().as_str()));
assert_eq!(req.task_definition(), Some(td_arn(3).as_str()));
assert_eq!(req.launch_type(), Some(&LaunchType::Fargate));
assert!(req.capacity_provider_strategy().is_empty());
assert_eq!(req.enable_execute_command(), Some(true));
assert_eq!(req.count(), Some(1));
let vpc = req
.network_configuration()
.and_then(|n| n.awsvpc_configuration())
.expect("awsvpc");
assert_eq!(vpc.subnets(), ["subnet-0a", "subnet-0b"]);
assert_eq!(vpc.security_groups(), ["sg-0123456789abcdef0"]);
assert_eq!(vpc.assign_public_ip(), Some(&AssignPublicIp::Enabled));
let got: Vec<(String, String)> = req
.tags()
.iter()
.map(|t| (t.key().unwrap().to_string(), t.value().unwrap().to_string()))
.collect();
assert_eq!(got, tags());
// Nothing project-shaped rides along (P1), and tags are given, not propagated.
assert!(req.overrides().is_none());
assert!(req.propagate_tags().is_none());
assert!(req.enable_ecs_managed_tags().is_none());
}
#[test]
fn spot_uses_the_fargate_spot_capacity_provider_and_no_launch_type() {
let req = run_task_request(&env_info(), &td_arn(3), true, &tags());
assert_eq!(req.launch_type(), None);
let strategy = req.capacity_provider_strategy();
assert_eq!(strategy.len(), 1);
assert_eq!(strategy[0].capacity_provider(), "FARGATE_SPOT");
assert_eq!(strategy[0].weight(), 1);
}
#[test]
fn a_private_environment_gets_no_public_ip() {
let mut env = env_info();
env.assign_public_ip = false;
let req = run_task_request(&env, &td_arn(3), false, &tags());
let vpc = req
.network_configuration()
.unwrap()
.awsvpc_configuration()
.unwrap();
assert_eq!(vpc.assign_public_ip(), Some(&AssignPublicIp::Disabled));
}
// ------------------------------------------------------------ find or run
#[tokio::test]
async fn an_existing_running_task_is_reused_and_nothing_is_run() {
let api = Fake {
listed: vec![task_arn("t1")],
..Default::default()
}
.describing(vec![vec![ready("t1")]]);
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(4), RunOn::Fargate { spot: false })
.await
.unwrap();
assert!(got.reused);
assert_eq!(got.task.arn, task_arn("t1"));
assert!(!api.calls().contains(&"run".to_string()));
assert_eq!(api.calls()[0], format!("list {} {FAMILY}", cluster()));
}
#[tokio::test]
async fn a_pending_task_is_reused_too() {
let api = Fake {
listed: vec![task_arn("t1")],
..Default::default()
}
.describing(vec![vec![info("t1", "PENDING")]]);
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
.await
.unwrap();
assert!(got.reused);
assert_eq!(got.task.last_status, "PENDING");
assert!(!api.calls().contains(&"run".to_string()));
}
#[tokio::test]
async fn a_task_of_another_owner_or_project_in_the_family_is_never_reused() {
let mut foreign = info("theirs", "RUNNING");
foreign.owner = Some(OTHER.into());
let mut other_project = info("other-project", "RUNNING");
other_project.project = Some("ffffffff-0000-0000-0000-000000000000".into());
let mut untagged = info("untagged", "RUNNING");
untagged.owner = None;
untagged.project = None;
let api = Fake {
listed: vec![
task_arn("theirs"),
task_arn("other-project"),
task_arn("untagged"),
],
..Default::default()
}
.describing(vec![vec![foreign, other_project, untagged]]);
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
.await
.unwrap();
assert!(!got.reused);
assert_eq!(got.task.arn, task_arn("new"));
assert_eq!(api.calls().last().unwrap(), "run");
}
#[tokio::test]
async fn a_task_already_stopping_is_not_reused() {
let mut stopping = info("t1", "RUNNING");
stopping.desired_status = "STOPPED".into();
let api = Fake {
listed: vec![task_arn("t1"), task_arn("t2")],
..Default::default()
}
.describing(vec![vec![stopping, info("t2", "DEPROVISIONING")]]);
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
.await
.unwrap();
assert!(!got.reused);
}
#[tokio::test]
async fn a_running_task_is_preferred_over_a_pending_one() {
let api = Fake {
listed: vec![task_arn("p"), task_arn("r")],
..Default::default()
}
.describing(vec![vec![info("p", "PENDING"), ready("r")]]);
let got = find_project_task(&api, &cluster(), FAMILY, USER, PROJECT_ID)
.await
.unwrap()
.unwrap();
assert_eq!(got.arn, task_arn("r"));
}
#[tokio::test]
async fn nothing_listed_means_no_describe_and_a_run_with_the_exact_revision_and_tags() {
let api = Fake::default();
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(7), RunOn::Fargate { spot: true })
.await
.unwrap();
assert!(!got.reused);
assert_eq!(api.calls().len(), 2, "{:?}", api.calls());
let reqs = api.run_requests.lock().unwrap();
assert_eq!(reqs[0].task_definition(), Some(td_arn(7).as_str()));
assert_eq!(reqs[0].launch_type(), None);
let keys: Vec<&str> = reqs[0].tags().iter().filter_map(|t| t.key()).collect();
assert_eq!(
keys,
["triple-c:environment", "triple-c:owner", "triple-c:project"]
);
assert_eq!(
reqs[0]
.tags()
.iter()
.find(|t| t.key() == Some("triple-c:owner"))
.unwrap()
.value(),
Some(USER)
);
}
#[tokio::test]
async fn a_bare_family_or_foreign_arn_is_refused_before_any_call() {
let api = Fake::default();
for bad in [
FAMILY.to_string(),
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task-definition/{FAMILY}"),
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task-definition/{FAMILY}:latest"),
"arn:aws:ecs:eu-west-1:1:task-definition/someone-else:1".to_string(),
] {
let err = find_or_run(&api, &env_info(), USER, PROJECT_ID, &bad, RunOn::Fargate { spot: false })
.await
.unwrap_err();
assert!(matches!(err, AwsError::Other { .. }), "{bad}: {err:?}");
}
assert!(api.calls().is_empty());
}
#[tokio::test]
async fn a_quota_refusal_is_reported_once_without_retrying() {
let api = Fake::default().running(Err(quota_error(
"You've reached the limit on the number of vCPUs you can run concurrently",
)));
let err = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
.await
.unwrap_err();
assert!(is_quota_error(&err));
let text = err.to_string();
assert!(text.contains("limit"), "{text}");
assert!(text.contains("quota"), "{text}");
assert!(text.contains("vCPUs"), "{text}");
assert_eq!(
api.calls().iter().filter(|c| *c == "run").count(),
1,
"no retry"
);
}
// ------------------------------------------------------------ quota mapping
#[test]
fn quota_texts_are_recognised_and_others_are_not() {
for q in [
"You've reached the limit on the number of vCPUs you can run concurrently",
"You\u{2019}ve reached the limit on the number of tasks you can run concurrently",
"LimitExceededException: too many",
"Service quota exceeded for Fargate On-Demand vCPU",
"limit exceeded",
] {
assert!(looks_like_quota(q), "{q}");
}
for not in [
"Capacity is unavailable at this time. Please try again later",
"AccessDeniedException: not authorized",
"CannotPullContainerError",
] {
assert!(!looks_like_quota(not), "{not}");
}
}
#[test]
fn quota_mapping_turns_other_errors_into_quota_errors_and_leaves_the_rest() {
let q = quota_or(AwsError::Other {
message: "ClientException: You've reached the limit on the number of vCPUs".into(),
});
assert!(is_quota_error(&q));
// The frontend's `quota_exceeded` kind (lib/types.ts).
let v = serde_json::to_value(&q).unwrap();
assert_eq!(v["kind"], "quota_exceeded");
assert!(v["message"].as_str().unwrap().contains("vCPUs"));
let denied = AwsError::AccessDenied {
action: "ecs:RunTask".into(),
message: "limit".into(),
};
assert_eq!(quota_or(denied.clone()), denied);
assert!(!is_quota_error(&AwsError::Other {
message: "x".into()
}));
}
#[test]
fn run_task_failures_become_errors_and_quota_failures_quota_errors() {
let ok = run_task_outcome(vec![info("a", "PROVISIONING")], vec![]).unwrap();
assert_eq!(ok.arn, task_arn("a"));
let err = run_task_outcome(
vec![],
vec![(Some("RESOURCE:ENI".into()), Some("no ENIs left".into()))],
)
.unwrap_err();
assert!(!is_quota_error(&err));
assert!(err.to_string().contains("RESOURCE:ENI"), "{err}");
assert!(err.to_string().contains("no ENIs left"), "{err}");
let err = run_task_outcome(
vec![],
vec![(
Some("You've reached the limit on the number of tasks you can run concurrently".into()),
None,
)],
)
.unwrap_err();
assert!(is_quota_error(&err));
let err = run_task_outcome(vec![], vec![]).unwrap_err();
assert!(err.to_string().contains("no task"), "{err}");
}
// ------------------------------------------------------------ wait
fn timing() -> WaitTiming {
WaitTiming {
interval: Duration::from_secs(5),
timeout: Duration::from_secs(360),
}
}
fn logs() -> TaskLogs {
TaskLogs {
group: "/triple-c/dev".into(),
stream_prefix: log_stream_prefix(&owner_key(REGION, USER), PROJECT_ID),
}
}
#[tokio::test(start_paused = true)]
async fn wait_polls_until_running_with_the_exec_agent_and_a_runtime_id() {
let mut half = info("t1", "RUNNING");
half.runtime_id = Some("rt".into());
let api = Fake::default().describing(vec![
vec![info("t1", "PROVISIONING")],
vec![info("t1", "PENDING")],
vec![half],
vec![ready("t1")],
]);
let seen = Mutex::new(Vec::new());
let t0 = tokio::time::Instant::now();
let got = wait_ready(
&api,
&cluster(),
&task_arn("t1"),
&logs(),
timing(),
|t: &TaskInfo| seen.lock().unwrap().push(t.last_status.clone()),
)
.await
.unwrap();
assert_eq!(got, ready("t1"));
assert_eq!(
*seen.lock().unwrap(),
["PROVISIONING", "PENDING", "RUNNING", "RUNNING"]
);
assert_eq!(t0.elapsed(), Duration::from_secs(15));
}
#[tokio::test(start_paused = true)]
async fn a_task_that_stops_while_waiting_reports_its_reason_and_last_log_lines() {
let mut stopped = info("t1", "STOPPED");
stopped.stop_code = Some("EssentialContainerExited".into());
stopped.stopped_reason = Some("Essential container in task exited".into());
let api = Fake {
logs: vec!["Triple-C: starting".into(), "fatal: boom".into()],
..Default::default()
}
.describing(vec![vec![info("t1", "PENDING")], vec![stopped]]);
let err = wait_ready(
&api,
&cluster(),
&task_arn("t1"),
&logs(),
timing(),
|_: &TaskInfo| {},
)
.await
.unwrap_err();
let text = err.to_string();
assert!(
text.contains("Essential container in task exited"),
"{text}"
);
assert!(text.contains("Triple-C: starting\nfatal: boom"), "{text}");
let reqs = api.log_requests.lock().unwrap();
assert_eq!(
*reqs,
[(
"/triple-c/dev".to_string(),
format!("{}/workspace/t1", logs().stream_prefix),
50
)]
);
}
#[tokio::test(start_paused = true)]
async fn a_task_being_stopped_fails_the_wait_at_once() {
let mut stopping = info("t1", "RUNNING");
stopping.desired_status = "STOPPED".into();
stopping.stop_code = Some("SpotInterruption".into());
stopping.stopped_reason = Some("Your Spot Task was interrupted.".into());
let api = Fake::default().describing(vec![vec![stopping]]);
let err = wait_ready(
&api,
&cluster(),
&task_arn("t1"),
&logs(),
timing(),
|_: &TaskInfo| {},
)
.await
.unwrap_err();
assert!(
err.to_string().contains("Your Spot Task was interrupted."),
"{err}"
);
}
#[tokio::test(start_paused = true)]
async fn a_task_that_never_gets_ready_times_out_with_its_last_status() {
let api = Fake::default().describing(vec![vec![info("t1", "PENDING")]]);
let t0 = tokio::time::Instant::now();
let err = wait_ready(
&api,
&cluster(),
&task_arn("t1"),
&logs(),
timing(),
|_: &TaskInfo| {},
)
.await
.unwrap_err();
assert!(t0.elapsed() >= Duration::from_secs(360));
assert!(t0.elapsed() < Duration::from_secs(366));
let text = err.to_string();
assert!(text.contains("6 minutes"), "{text}");
assert!(text.contains("PENDING"), "{text}");
}
#[tokio::test(start_paused = true)]
async fn a_task_that_vanishes_is_an_error_not_a_hang() {
let api = Fake::default().describing(vec![vec![]]);
let err = wait_ready(
&api,
&cluster(),
&task_arn("t1"),
&logs(),
timing(),
|_: &TaskInfo| {},
)
.await
.unwrap_err();
assert!(err.to_string().contains("no longer"), "{err}");
}
#[test]
fn the_default_wait_is_five_seconds_for_six_minutes() {
let t = WaitTiming::default();
assert_eq!(t.interval, Duration::from_secs(5));
assert_eq!(t.timeout, Duration::from_secs(360));
}
// ------------------------------------------------------------ stop / logs
#[tokio::test]
async fn stop_passes_the_reason() {
let api = Fake::default();
api.stop_task(&cluster(), &task_arn("t1"), STOP_REASON)
.await
.unwrap();
assert_eq!(
api.calls(),
[format!("stop {} Stopped from Triple-C", task_arn("t1"))]
);
}
#[test]
fn the_log_stream_is_prefix_container_task_id() {
assert_eq!(
task_log_stream("abc/p", &task_arn("0123abcd")),
"abc/p/workspace/0123abcd"
);
}
#[tokio::test]
async fn stopped_among_reports_stopped_and_vanished_tasks_only() {
let api = Fake::default().describing(vec![vec![
ready("up"),
info("down", "STOPPED"),
info("pending", "PENDING"),
]]);
let arns = vec![
task_arn("up"),
task_arn("down"),
task_arn("pending"),
task_arn("gone"),
];
let mut got = stopped_among(&api, &cluster(), &arns).await.unwrap();
got.sort_by(|a, b| a.0.cmp(&b.0));
assert_eq!(
got,
[
(task_arn("down"), StopKind::Exited(None)),
(task_arn("gone"), StopKind::Exited(None)),
]
);
}
// ------------------------------------------------------------ classify
#[test]
fn classify_stop_follows_the_spec_table() {
let with = |code: Option<&str>, reason: Option<&str>| TaskInfo {
stop_code: code.map(String::from),
stopped_reason: reason.map(String::from),
..info("t", "STOPPED")
};
assert_eq!(
classify_stop(&with(Some("EssentialContainerExited"), Some("Essential container in task exited"))),
StopKind::Exited(Some("Essential container in task exited".into()))
);
// The entrypoint's idle stop exits with its own status.
let idle = TaskInfo { exit_code: Some(IDLE_EXIT_CODE), ..with(Some("EssentialContainerExited"), None) };
assert_eq!(classify_stop(&idle), StopKind::Idle);
// So does its lifetime-cap stop.
let capped = TaskInfo { exit_code: Some(LIFETIME_EXIT_CODE), ..with(Some("EssentialContainerExited"), None) };
assert_eq!(classify_stop(&capped), StopKind::LifetimeCap);
let user_capped = TaskInfo { exit_code: Some(LIFETIME_EXIT_CODE), ..with(Some("UserInitiated"), None) };
assert_eq!(classify_stop(&user_capped), StopKind::UserInitiated);
let crashed = TaskInfo { exit_code: Some(137), ..with(Some("EssentialContainerExited"), None) };
assert_eq!(classify_stop(&crashed), StopKind::Exited(None));
// Only with that stop code: a user stop of a task that exits 75 is a user stop.
let user = TaskInfo { exit_code: Some(IDLE_EXIT_CODE), ..with(Some("UserInitiated"), None) };
assert_eq!(classify_stop(&user), StopKind::UserInitiated);
assert_eq!(
classify_stop(&with(Some("SpotInterruption"), None)),
StopKind::Spot
);
assert_eq!(
classify_stop(&with(
Some("TaskFailedToStart"),
Some("CannotPullContainerError: nope")
)),
StopKind::FailedToStart("CannotPullContainerError: nope".into())
);
assert!(matches!(
classify_stop(&with(Some("TaskFailedToStart"), None)),
StopKind::FailedToStart(r) if !r.is_empty()
));
assert_eq!(
classify_stop(&with(Some("UserInitiated"), Some("Stopped from Triple-C"))),
StopKind::UserInitiated
);
assert_eq!(
classify_stop(&with(Some("TerminationNotice"), None)),
StopKind::Exited(None)
);
assert_eq!(
classify_stop(&with(Some("ServiceSchedulerInitiated"), None)),
StopKind::Exited(None)
);
assert_eq!(classify_stop(&with(None, Some(" "))), StopKind::Exited(None));
}
// ------------------------------------------------------------ SDK mapping
#[test]
fn task_info_reads_the_workspace_container_and_its_exec_agent() {
let task = Task::builder()
.task_arn(task_arn("t1"))
.last_status("RUNNING")
.desired_status("RUNNING")
.health_status(aws_sdk_ecs::types::HealthStatus::Healthy)
.stop_code(TaskStopCode::SpotInterruption)
.stopped_reason("r")
.task_definition_arn(td_arn(3))
.containers(
Container::builder()
.name("sidecar")
.runtime_id("wrong")
.exit_code(1)
.build(),
)
.containers(
Container::builder()
.name("workspace")
.runtime_id("rt-1")
.exit_code(75)
.managed_agents(
ManagedAgent::builder()
.name(ManagedAgentName::ExecuteCommandAgent)
.last_status("RUNNING")
.build(),
)
.build(),
)
.tags(Tag::builder().key("triple-c:owner").value(USER).build())
.tags(
Tag::builder()
.key("triple-c:project")
.value(PROJECT_ID)
.build(),
)
.build();
let i = task_info(&task);
assert_eq!(i.arn, task_arn("t1"));
assert_eq!(i.health.as_deref(), Some("HEALTHY"));
assert_eq!(i.stop_code.as_deref(), Some("SpotInterruption"));
assert_eq!(i.runtime_id.as_deref(), Some("rt-1"));
assert!(i.exec_agent_running);
assert_eq!(i.exit_code, Some(75), "the workspace container's, not the sidecar's");
assert_eq!(i.owner.as_deref(), Some(USER));
assert_eq!(i.project.as_deref(), Some(PROJECT_ID));
let bare = task_info(&Task::builder().task_arn("x").last_status("PENDING").build());
assert!(!bare.exec_agent_running);
assert_eq!(bare.runtime_id, None);
assert_eq!(bare.exit_code, None);
assert_eq!(bare.owner, None);
}
// ------------------------------------------------------------- EC2 (phase 7)
fn user_cluster() -> String {
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice")
}
fn compute() -> UserCompute {
UserCompute {
instance_type: "m7i.large".into(),
max_instances: 2,
user_tag: "alice".into(),
cluster_arn: user_cluster(),
capacity_provider: "triple-c-dev--u-alice".into(),
}
}
#[test]
fn an_ec2_run_uses_the_users_cluster_and_capacity_provider_and_no_network() {
let c = compute();
let req = ec2_run_task_request(&c, &td_arn(3), &tags());
assert_eq!(req.cluster(), Some(user_cluster().as_str()));
assert_eq!(req.task_definition(), Some(td_arn(3).as_str()));
assert_eq!(req.launch_type(), None, "exclusive with a capacity provider strategy");
let strategy = req.capacity_provider_strategy();
assert_eq!(strategy.len(), 1);
assert_eq!(strategy[0].capacity_provider(), "triple-c-dev--u-alice");
assert_eq!(strategy[0].weight(), 1);
assert!(req.network_configuration().is_none(), "bridge networking (R2)");
assert_eq!(req.enable_execute_command(), Some(true));
assert_eq!(req.count(), Some(1));
assert!(req.client_token().is_some_and(|t| !t.is_empty()));
let got: Vec<(String, String)> = req
.tags()
.iter()
.map(|t| (t.key().unwrap().to_string(), t.value().unwrap().to_string()))
.collect();
assert_eq!(got, tags());
assert!(req.overrides().is_none());
assert!(req.propagate_tags().is_none());
assert!(req.enable_ecs_managed_tags().is_none());
}
#[tokio::test]
async fn an_ec2_find_or_run_looks_and_runs_on_the_users_cluster() {
let api = Fake::default();
let c = compute();
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Ec2(&c))
.await
.unwrap();
assert!(!got.reused);
assert_eq!(api.calls(), vec![format!("list {} {FAMILY}", user_cluster()), "run".to_string()]);
let req = &api.run_requests.lock().unwrap()[0];
assert_eq!(req.cluster(), Some(user_cluster().as_str()));
assert_eq!(req.capacity_provider_strategy()[0].capacity_provider(), "triple-c-dev--u-alice");
assert_eq!(RunOn::Ec2(&c).cluster(&env_info()), user_cluster());
assert_eq!(RunOn::Fargate { spot: true }.cluster(&env_info()), cluster());
}
#[tokio::test]
async fn access_denied_on_the_users_cluster_says_the_triple_c_user_value_is_wrong() {
let api = Fake::default().running(Err(AwsError::AccessDenied {
action: "ecs:RunTask".into(),
message: "not authorized to perform ecs:RunTask on cluster/triple-c-dev--u-alice".into(),
}));
let c = compute();
let e = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Ec2(&c))
.await
.unwrap_err();
let AwsError::AccessDenied { action, message } = &e else { panic!("{e:?}") };
assert_eq!(action, "ecs:RunTask");
assert!(message.starts_with(EC2_TAG_MISMATCH), "{message}");
assert!(message.contains("not authorized"), "AWS's words are kept: {message}");
assert!(EC2_TAG_MISMATCH.contains("different triple-c-user value"));
assert!(EC2_TAG_MISMATCH.contains("Ask an admin"));
// The same refusal on Fargate is left as it is.
let api = Fake::default().running(Err(AwsError::AccessDenied {
action: "ecs:RunTask".into(),
message: "nope".into(),
}));
let e = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
.await
.unwrap_err();
assert_eq!(e, AwsError::AccessDenied { action: "ecs:RunTask".into(), message: "nope".into() });
}
#[test]
fn a_placement_failure_on_ec2_says_no_instance_has_room() {
let e = ec2_run_error(
run_task_outcome(Vec::new(), vec![(Some("RESOURCE:MEMORY".into()), None)]).unwrap_err(),
);
assert!(e.to_string().contains("RESOURCE:MEMORY"), "{e}");
assert!(e.to_string().contains("No instance of your EC2 capacity has room"), "{e}");
// Anything else passes through.
let q = quota_error("limit");
assert_eq!(ec2_run_error(q.clone()), q);
}
#[test]
fn an_ec2_task_without_an_instance_is_waiting_for_one() {
let provisioning = info("t", "PROVISIONING");
assert!(waiting_for_instance(&provisioning));
assert!(waiting_for_instance(&info("t", "PENDING")));
let placed = TaskInfo {
container_instance_arn: Some(format!(
"arn:aws:ecs:{REGION}:{ACCOUNT}:container-instance/triple-c-dev--u-alice/abc"
)),
..info("t", "PENDING")
};
assert!(!waiting_for_instance(&placed));
assert!(!waiting_for_instance(&info("t", "RUNNING")));
assert!(!waiting_for_instance(&info("t", "STOPPED")));
// The EC2 wait leaves room for an instance launch and an image pull.
assert_eq!(WaitTiming::ec2().timeout, Duration::from_secs(600));
assert_eq!(WaitTiming::ec2().interval, WaitTiming::default().interval);
}
#[test]
fn task_info_reads_the_container_instance() {
let t = Task::builder()
.task_arn(task_arn("t"))
.container_instance_arn("arn:aws:ecs:eu-west-1:111122223333:container-instance/c/1")
.build();
assert_eq!(
task_info(&t).container_instance_arn.as_deref(),
Some("arn:aws:ecs:eu-west-1:111122223333:container-instance/c/1")
);
assert_eq!(task_info(&Task::builder().build()).container_instance_arn, None);
}
+261 -8
View File
@@ -15,10 +15,17 @@ use crate::models::host_connection::validate_supported_region;
/// `locals.template_version` (and the `triple-c:template-version` default
/// tag) of `aws/terraform/environment`.
pub const ENVIRONMENT_MODULE_VERSION: u32 = 1;
/// The same for the user-onboarding module.
pub const ENVIRONMENT_MODULE_VERSION: u32 = 2;
/// The same for the user-onboarding module. 2: optional per-user EC2
/// capacity (phase 7); an onboarding without it plans as version 1 did,
/// apart from this tag.
#[allow(dead_code)] // the onboarding module's version; checked against its HCL by a test
pub const ONBOARD_MODULE_VERSION: u32 = 1;
pub const ONBOARD_MODULE_VERSION: u32 = 2;
/// The same for the permissions-boundary module. 2: the instance role's ECS
/// agent actions, on its own cluster (phase 7, E4); an account admin
/// re-applies the boundary once.
#[allow(dead_code)] // checked against its HCL by a test; nothing reads the tag yet
pub const BOUNDARY_MODULE_VERSION: u32 = 2;
/// The permissions boundary every module-created role carries (no path).
/// Equal to [`super::policies::PERMISSIONS_BOUNDARY_NAME`]; a test checks it.
pub const BOUNDARY_POLICY_NAME: &str = "triple-c-permissions-boundary";
@@ -52,6 +59,7 @@ const USER_ONBOARD_FILES: &[(&str, &str)] = module!("user-onboard":
"main.tf",
"iam.tf",
"efs.tf",
"ec2.tf",
"outputs.tf",
".terraform.lock.hcl",
);
@@ -96,6 +104,16 @@ pub fn validate_environment_name(name: &str) -> Result<String, String> {
}
}
/// An environment's ECS cluster name (the environment module's).
pub fn environment_cluster_name(env: &str) -> String {
format!("triple-c-{env}")
}
/// An environment's CloudWatch log group (the environment module's).
pub fn environment_log_group(env: &str) -> String {
format!("/triple-c/{env}")
}
/// The SSM parameter holding an environment's record.
pub fn environment_parameter_name(env: &str) -> String {
format!("/triple-c/environments/{env}")
@@ -122,6 +140,201 @@ pub fn user_role_names(env: &str, owner_key: &str) -> (String, String) {
)
}
/// The principal tag every EC2 user's sign-in carries (phase 7, amended E1):
/// an admin sets it (an IAM Identity Center attribute for access control,
/// or a tag on the IAM user or role), and the workspace policy lets a
/// caller run tasks only on the per-user cluster named after its value.
#[allow(dead_code)] // the onboarding module (Task 3) and the UI read it
pub const EC2_USER_TAG_KEY: &str = "triple-c-user";
/// Checks a `triple-c-user` tag value: 1 to 32 characters of
/// `[A-Za-z0-9-]`, not starting or ending with a hyphen and without `--`
/// (the separator in [`user_ec2_stem`], which environment names can't hold
/// either, so a per-user name can be read back only one way).
pub fn validate_ec2_user_tag(tag: &str) -> Result<String, String> {
let ok = (1..=32).contains(&tag.len())
&& tag.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-')
&& !tag.starts_with('-')
&& !tag.ends_with('-')
&& !tag.contains("--");
if ok {
Ok(tag.to_string())
} else {
Err(format!(
"\"{}\" is not a valid triple-c-user tag: use 1 to 32 letters, digits and single \
hyphens, not starting or ending with a hyphen",
tag.escape_debug()
))
}
}
/// The name of a user's EC2 cluster, capacity provider, Auto Scaling group
/// and launch template: `triple-c-<env>--u-<user tag>` (at most 65
/// characters). Environment names and user tags never contain `--`, so the
/// workspace policy's `cluster/triple-c-*--u-*` matches exactly the
/// per-user clusters, and `triple-c-*--u-<tag>` exactly that user's.
pub fn user_ec2_stem(env: &str, user_tag: &str) -> String {
format!("triple-c-{env}--u-{user_tag}")
}
/// Whether `cluster` (a cluster *name*) is one of `env`'s per-user EC2
/// clusters ([`user_ec2_stem`] of a valid tag).
pub fn is_user_ec2_cluster(env: &str, cluster: &str) -> bool {
cluster
.strip_prefix(&format!("{}--u-", environment_cluster_name(env)))
.is_some_and(|tag| validate_ec2_user_tag(tag).is_ok())
}
/// A user's EC2 instance role (and instance profile): named by owner key,
/// not the user tag, because role names are account-global and at most 64
/// characters (this is at most 51). The admin's PassRole to EC2 names
/// `role/triple-c-*-instance`.
#[allow(dead_code)] // the onboarding module builds it; pinned by tests
pub fn user_instance_role_name(env: &str, owner_key: &str) -> String {
format!("triple-c-{env}-{owner_key}-instance")
}
/// The SSM parameter holding a user's EC2 record (E11), written by their
/// onboarding module only while it has EC2 capacity: a String of JSON with
/// exactly [`USER_RECORD_FIELDS`]. Workspace users can read it
/// (`parameter/triple-c/*`) though not the state bucket; it lives outside
/// `/triple-c/environments/`, which environment detection lists.
#[allow(dead_code)] // the app reads it from Task 4 on
pub fn user_record_parameter_name(env: &str, owner_key: &str) -> String {
format!("/triple-c/users/{env}/{owner_key}")
}
/// The fields of a user's EC2 record, schema 1: `instance_type`,
/// `cluster_arn` (the user's cluster, [`user_ec2_stem`]),
/// `capacity_provider` (its one provider, the same name) and `user_tag`
/// (the `triple-c-user` value the cluster is named after) and
/// `max_instances` (the group's maximum), so a re-apply can keep them.
/// `environments::parse_user_record` reads them; a test pins the HCL to it.
#[allow(dead_code)]
pub const USER_RECORD_FIELDS: [&str; 6] = [
"schema",
"instance_type",
"cluster_arn",
"capacity_provider",
"user_tag",
"max_instances",
];
/// The most instances a user's EC2 capacity may run at once.
pub const EC2_MAX_INSTANCES: u32 = 4;
/// A user's optional EC2 capacity, as the onboarding module takes it.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Ec2Compute {
/// One of the types `ec2_instance_spec` knows.
pub instance_type: String,
/// 1 to [`EC2_MAX_INSTANCES`].
pub max_instances: u32,
/// The user's `triple-c-user` principal tag value.
pub user_tag: String,
}
/// Whether a project id is used in names as it is: 1 to 64 characters of
/// `[A-Za-z0-9-]`. Every UUID (how the app makes ids) is.
fn is_clean_project_id(id: &str) -> bool {
(1..=64).contains(&id.len()) && id.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-')
}
/// 16 hex characters of `sha256(id)`.
fn project_id_hash(id: &str) -> String {
Sha256::digest(id.as_bytes())
.iter()
.take(8)
.map(|b| format!("{b:02x}"))
.collect()
}
/// A project id as a name component, safe in a secret name, an EFS path and
/// a log stream. A clean id (see [`is_clean_project_id`]) is used as is.
/// Anything else — only a hand-edited `projects.json` has one — becomes its
/// first 40 characters with everything outside `[A-Za-z0-9-]` replaced by
/// `-`, then `_` and a hash of the whole id. Clean ids never contain `_`, so
/// the two forms can't collide, and ids that sanitise alike (`a/b`, `a:b`)
/// still differ by the hash.
fn project_component(project_id: &str) -> String {
if is_clean_project_id(project_id) {
return project_id.to_string();
}
let prefix: String = project_id
.chars()
.take(40)
.map(|c| {
if c.is_ascii_alphanumeric() || c == '-' {
c
} else {
'-'
}
})
.collect();
format!("{prefix}_{}", project_id_hash(project_id))
}
/// A project's ECS task definition family:
/// `triple-c-<env>-<owner_key>-<first 12 of the project id>` (an id that
/// isn't clean contributes `_` and 11 hex of its hash instead). At most 55
/// characters of `[A-Za-z0-9_-]`. Other users can register revisions into
/// any `triple-c-*` family, so a family is never trusted by name alone (see
/// `aws::workspace`), and two of a user's projects could share 12 leading
/// characters, so a family's tasks and revisions are told apart by their
/// `triple-c:project` tag.
pub fn task_family(env: &str, owner_key: &str, project_id: &str) -> String {
let id: String = if is_clean_project_id(project_id) {
project_id.chars().take(12).collect()
} else {
format!("_{}", &project_id_hash(project_id)[..11])
};
format!("triple-c-{env}-{owner_key}-{id}")
}
/// The project's Secrets Manager secret (P1): `triple-c/<env>/<owner_key>/<project id>`.
pub fn project_secret_name(env: &str, owner_key: &str, project_id: &str) -> String {
format!(
"triple-c/{env}/{owner_key}/{}",
project_component(project_id)
)
}
/// The root of the project's EFS access point (P4), at most 90 characters.
pub fn access_point_root(owner_key: &str, project_id: &str) -> String {
format!("/triple-c/{owner_key}/{}", project_component(project_id))
}
/// The root of the project's EFS-mode `/workspace` access point (Q1):
/// `/triple-c-workspaces/<owner_key>/<project>`, a sibling tree of the home
/// roots (under [`access_point_root`] it would sit inside the home access
/// point). At most 100 characters.
pub fn workspace_access_point_root(owner_key: &str, project_id: &str) -> String {
format!(
"/triple-c-workspaces/{owner_key}/{}",
project_component(project_id)
)
}
/// The container's `awslogs-stream-prefix`; ECS names the stream
/// `<prefix>/workspace/<task id>`.
pub fn log_stream_prefix(owner_key: &str, project_id: &str) -> String {
format!("{owner_key}/{}", project_component(project_id))
}
/// The awslogs stream of a task's `workspace` container:
/// `<prefix>/<container>/<task-id>`, the task id being the last segment of
/// its ARN.
pub fn task_log_stream(stream_prefix: &str, task_arn: &str) -> String {
let task_id = task_arn.rsplit('/').next().unwrap_or(task_arn);
format!("{stream_prefix}/workspace/{task_id}")
}
/// The `creation_token` of the user's file system (the onboarding module's
/// `local.name`).
pub fn user_file_system_token(env: &str, owner_key: &str) -> String {
format!("triple-c-{env}-{owner_key}")
}
/// The per-account, per-region state bucket.
pub fn state_bucket_name(account: &str, region: &str) -> String {
format!("triple-c-tfstate-{account}-{region}")
@@ -174,14 +387,23 @@ fn is_aws_id(id: &str, prefix: &str) -> bool {
})
}
/// The longest lifetime cap an environment can carry, in hours (a week).
pub const MAX_WORKSPACE_HOURS: u32 = 168;
/// The environment module's `terraform.tfvars.json`. Applies the same rules
/// as the module's variable validations, so a bad value fails here first.
pub fn environment_vars(
env: &str,
region: &str,
network: &NetworkChoice,
max_workspace_hours: u32,
) -> Result<serde_json::Value, String> {
let env = validate_environment_name(env)?;
if max_workspace_hours > MAX_WORKSPACE_HOURS {
return Err(format!(
"The maximum workspace lifetime is at most {MAX_WORKSPACE_HOURS} hours (0 = no cap)"
));
}
let region = validate_supported_region(region)?;
let (mode, vpc_id, subnet_ids, assign_public_ip) = match network {
NetworkChoice::Public => ("public", String::new(), Vec::new(), false),
@@ -222,20 +444,51 @@ pub fn environment_vars(
"vpc_id": vpc_id,
"subnet_ids": subnet_ids,
"assign_public_ip": assign_public_ip,
"max_workspace_hours": max_workspace_hours,
}))
}
/// The onboarding module's `terraform.tfvars.json` for the user whose
/// `aws:userid` is `owner_id`. Bedrock stays on the workspace user's own
/// credentials unless a later setting opts the task role in.
pub fn onboarding_vars(env: &str, region: &str, owner_id: &str) -> serde_json::Value {
json!({
/// `aws:userid` is `owner_id`, with their EC2 capacity or none (empty
/// `ec2_instance_type`: the module makes nothing for EC2). Applies the same
/// rules as the module's EC2 variable validations. Bedrock stays on the
/// workspace user's own credentials unless a later setting opts the task
/// role in.
pub fn onboarding_vars(
env: &str,
region: &str,
owner_id: &str,
ec2: Option<&Ec2Compute>,
) -> Result<serde_json::Value, String> {
let (instance_type, max_instances, user_tag) = match ec2 {
None => (String::new(), 1, String::new()),
Some(c) => {
if crate::models::project::ec2_instance_spec(&c.instance_type).is_none() {
return Err(format!(
"\"{}\" is not an instance type Triple-C supports (m7i, m7g, c7i, c7g, r7i, \
r7g from large to 4xlarge; t3, t4g from large to 2xlarge)",
c.instance_type.escape_debug()
));
}
if !(1..=EC2_MAX_INSTANCES).contains(&c.max_instances) {
return Err(format!(
"The number of instances must be from 1 to {EC2_MAX_INSTANCES}"
));
}
let tag = validate_ec2_user_tag(&c.user_tag)?;
(c.instance_type.clone(), c.max_instances, tag)
}
};
Ok(json!({
"environment_name": env,
"region": region,
"owner_id": owner_id,
"owner_key": owner_key(region, owner_id),
"bedrock_on_task_role": false,
})
"ec2_instance_type": instance_type,
"ec2_max_instances": max_instances,
"ec2_user_tag": user_tag,
}))
}
/// The boundary module's `terraform.tfvars.json`: the shipped boundary
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -34,6 +34,11 @@ pub async fn get_container_info(
.projects_store
.get(&project_id)
.ok_or_else(|| format!("Project {} not found", project_id))?;
// An AWS project has no Docker container (its own name could match a
// stale one from before it moved to AWS).
if project.is_aws() {
return Ok(None);
}
docker::get_container_info(&project).await
}
@@ -30,7 +30,7 @@ use std::sync::{Mutex, OnceLock};
use std::time::Duration;
use aws_config::SdkConfig;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tauri::{AppHandle, Emitter, State};
@@ -38,17 +38,20 @@ use aws_sdk_s3::error::ProvideErrorMetadata;
use tokio::sync::oneshot;
use crate::aws::environments::{
ensure_state_bucket, scan, state_bucket_policy, LIVE_LOCK_SECS, DetectedEnvironment,
EnvironmentAction, EnvironmentInfo, EnvironmentScan, EnvironmentState, Knowable, LocalRun,
OnboardingState, SdkDetectApi,
cluster_holder, ec2_name_holders, ensure_state_bucket, scan, state_bucket_policy,
user_record_from, DetectedEnvironment, EnvironmentAction, EnvironmentInfo, EnvironmentScan,
EnvironmentState, Knowable, LocalRun, NameHolder, OnboardApi, OnboardingState, SdkDetectApi,
SdkOnboardApi, UserCompute, UserRecord, LIVE_LOCK_SECS,
};
use crate::aws::tofu::{
self, backend_hcl, StartError, TofuEvent, TofuOp, TofuOutcome, Workdir,
};
use crate::aws::tofu_modules::{
boundary_vars, environment_vars, module_files, onboarding_vars, owner_key, state_bucket_name,
state_key, validate_environment_name, Deployment, ModuleKind, NetworkChoice,
state_key, user_ec2_stem, user_record_parameter_name, validate_ec2_user_tag,
validate_environment_name, Deployment, Ec2Compute, ModuleKind, NetworkChoice,
};
use crate::models::project::{ec2_instance_types, Ec2InstanceType, DEFAULT_EC2_INSTANCE_TYPE};
use crate::aws::{caller_identity, sdk_config, AwsError, CallerIdentity};
use crate::models::{validate_supported_region, AwsAuth, HostConnection, HostKind};
use crate::storage::secure;
@@ -178,8 +181,9 @@ pub(crate) fn network_from_record(
subnet_ids: info.subnet_ids.clone(),
assign_public_ip: info.assign_public_ip,
};
// The same checks as the apply's variables.
environment_vars(&info.name, "us-east-1", &choice)?;
// The same checks as the apply's variables. Only the network is
// checked here, so the lifetime cap is a valid placeholder (0).
environment_vars(&info.name, "us-east-1", &choice, 0)?;
Ok(choice)
}
_ => Err("The environment record names an unknown network mode".into()),
@@ -271,7 +275,7 @@ pub(crate) fn onboarding_unlock_refusal(
}
let env = find(scan, name).ok_or_else(|| format!("No environment named \"{name}\" was found"))?;
match &env.onboarding {
Some(OnboardingState::Incomplete { lock: Some(l) })
Some(OnboardingState::Incomplete { lock: Some(l), .. })
if !l.local_process && env.onboarding_actions.contains(&EnvironmentAction::Unlock) =>
{
Ok(())
@@ -297,6 +301,139 @@ pub(crate) fn onboard_refusal(scan: &EnvironmentScan, name: &str) -> Result<(),
Ok(())
}
/// The EC2 capacity an onboarding asks for: none (Fargate only), or EC2
/// with these values. A command given no choice keeps what the user's
/// record holds ([`resolve_ec2`]).
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Ec2Choice {
None,
Ec2(Ec2Compute),
}
/// What an onboarding apply uses: the explicit choice, or with none given
/// the recorded capacity (a re-apply keeps it unless changed). With none
/// given and a record that exists but can't be used, it refuses rather than
/// apply without EC2, which would tear the capacity down.
pub(crate) fn resolve_ec2(
choice: Option<Ec2Choice>,
record: &UserRecord,
) -> Result<Option<Ec2Compute>, String> {
match (choice, record) {
(Some(Ec2Choice::None), _) => Ok(None),
(Some(Ec2Choice::Ec2(c)), _) => Ok(Some(c)),
(None, UserRecord::Absent) => Ok(None),
(None, UserRecord::Valid(r)) => Ok(Some(Ec2Compute {
instance_type: r.instance_type.clone(),
max_instances: r.max_instances,
user_tag: r.user_tag.clone(),
})),
(None, UserRecord::Unusable(reason)) => Err(format!(
"Your EC2 record can't be used ({reason}), so Triple-C can't keep your EC2 capacity \
as it is; choose the EC2 capacity explicitly"
)),
}
}
/// R3 (amended): an apply with EC2 capacity would adopt (or fail on) a
/// per-user cluster or Auto Scaling group named `stem` that already exists.
/// Each one found must be the caller's: its `triple-c:owner` tag (the
/// module's default tags) is `owner_id`, or, for an untagged one only, the
/// caller's record names the cluster. A half-built capacity of the caller's
/// own (a failed apply that wrote no record yet) is therefore re-applied;
/// one tagged as someone else's is always refused (a record can't vouch for
/// it), and an untagged one is refused unless the record names it.
pub(crate) fn ec2_collision_refusal(
stem: &str,
holders: &[NameHolder],
owner_id: &str,
recorded: Option<&UserCompute>,
) -> Result<(), String> {
let suffix = format!(":cluster/{stem}");
let recorded_here = recorded.is_some_and(|r| r.cluster_arn.ends_with(&suffix));
for h in holders {
match h.owner.as_deref() {
Some(o) if o == owner_id => continue,
None if recorded_here => continue,
_ => {}
}
let whose = if h.owner.is_some() {
"belongs to another onboarding"
} else {
"exists but isn't tagged as any Triple-C user's"
};
return Err(format!(
"An {} named {stem} already {whose}. The triple-c-user value is probably wrong: use \
the value an admin set as your triple-c-user sign-in tag",
h.kind
));
}
Ok(())
}
/// Everything a self-onboarding apply needs settled before it starts: the
/// caller's record, the capacity ([`resolve_ec2`]), the module variables
/// and, with EC2 capacity, the name check ([`ec2_collision_refusal`]).
/// Check-then-apply leaves a narrow race (another admin creating the same
/// names in between); only admins apply, and the module would then fail on
/// create rather than adopt.
pub(crate) async fn prepare_onboarding<A: OnboardApi>(
api: &A,
env: &str,
region: &str,
account: &str,
owner_id: &str,
choice: Option<Ec2Choice>,
) -> Result<Value, AwsError> {
let okey = owner_key(region, owner_id);
let read = api.parameter(&user_record_parameter_name(env, &okey)).await?;
let record = user_record_from(read, env, account, region);
let compute = resolve_ec2(choice, &record).map_err(other)?;
let vars = onboarding_vars(env, region, owner_id, compute.as_ref()).map_err(other)?;
if let Some(c) = &compute {
// The caller's own triple-c-user tag can't be read (session tags
// aren't visible to GetCallerIdentity); a wrong value fails later
// as access denied.
let stem = user_ec2_stem(env, &c.user_tag);
let holders = ec2_name_holders(api, &stem).await?;
ec2_collision_refusal(&stem, &holders, owner_id, record.valid()).map_err(other)?;
}
Ok(vars)
}
/// The bundle's version of the check, with what a workspace user can read
/// (the cluster, via `ecs:Describe*`, and the record; not the Auto Scaling
/// group): a warning to show, never a refusal. The admin runs the full
/// check from the README.
pub(crate) async fn bundle_ec2_warning<A: OnboardApi>(
api: &A,
env: &str,
region: &str,
account: &str,
owner_id: &str,
compute: &Ec2Compute,
) -> Option<String> {
let stem = user_ec2_stem(env, &compute.user_tag);
let checked = async {
let holder = cluster_holder(api, &stem).await?;
let read = api
.parameter(&user_record_parameter_name(env, &owner_key(region, owner_id)))
.await?;
let record = user_record_from(read, env, account, region);
Ok::<_, AwsError>(
ec2_collision_refusal(&stem, &holder.into_iter().collect::<Vec<_>>(), owner_id, record.valid())
.err(),
)
};
match checked.await {
Ok(warning) => warning,
Err(_) => Some(format!(
"Triple-C couldn't check whether the EC2 name {stem} is already in use; the README has \
the admin check it"
)),
}
}
/// `AlreadyRunning` is told apart before it becomes an `AwsError`.
pub(crate) fn start_error(e: StartError, label: &str) -> AwsError {
match e {
@@ -414,7 +551,54 @@ const README: &str = "README.txt";
const BACKEND: &str = "backend.hcl";
const TFVARS: &str = "terraform.tfvars.json";
fn bundle_readme(d: Deployment, account: &str, region: &str, bucket: &str, key: &str) -> String {
/// The onboarding bundle's EC2 step: before applying with EC2 capacity, the
/// admin checks that the per-user names are free or already this user's
/// (the module can't: a name taken by someone else's onboarding would be
/// adopted, and a collision only fails at create). Ownership is the
/// resources' `triple-c:owner` tag, or failing that the user's record, as
/// in [`ec2_collision_refusal`]. `user_tag` is the bundle's `ec2_user_tag`
/// when set and valid; otherwise the commands show `<tag>` for the admin to
/// fill in.
fn ec2_collision_step(
env: &str,
owner_key: &str,
owner_id: &str,
region: &str,
user_tag: Option<&str>,
) -> String {
let stem = user_ec2_stem(env, user_tag.unwrap_or("<tag>"));
let record = user_record_parameter_name(env, owner_key);
format!(
"EC2 capacity (only if ec2_instance_type is set)
------------------------------------------------
ec2_user_tag must be the value of the user's triple-c-user principal tag.
BEFORE tofu apply, check that the per-user EC2 names are not already in use
by another onboarding. Run:
aws ecs describe-clusters --region {region} --clusters {stem} --include TAGS --query 'clusters[?status!=`INACTIVE`].[clusterName,tags[?key==`triple-c:owner`].value|[0]]' --output text
aws autoscaling describe-auto-scaling-groups --region {region} --auto-scaling-group-names {stem} --query 'AutoScalingGroups[].[AutoScalingGroupName,Tags[?Key==`triple-c:owner`].Value|[0]]' --output text
aws ssm get-parameter --region {region} --name {record} --query Parameter.Value --output text
If either of the first two prints {stem}, apply ONLY if the owner printed
next to it is this user's owner_id ({owner_id}) or the third (this user's
record) succeeds and its cluster_arn ends in cluster/{stem}; otherwise the
name belongs to someone else's onboarding (or to nobody's): stop and
choose the correct ec2_user_tag. (The app runs the same check before
applying; run the check right before the apply, since nothing holds the
name in between.)
"
)
}
fn bundle_readme(
d: Deployment,
account: &str,
region: &str,
bucket: &str,
key: &str,
vars: &Value,
) -> String {
let what = match d {
Deployment::Environment(env) => format!(
"the Triple-C environment \"{env}\" (ECS cluster, security group, KMS key, log \
@@ -441,6 +625,16 @@ fn bundle_readme(d: Deployment, account: &str, region: &str, bucket: &str, key:
format!(r#"{{"LocationConstraint":"{region}",{tags}}}"#)
};
let policy = state_bucket_policy(bucket);
let ec2_step = match d {
Deployment::User { env, owner_key } => {
let tag = vars["ec2_user_tag"]
.as_str()
.filter(|t| validate_ec2_user_tag(t).is_ok());
let owner_id = vars["owner_id"].as_str().unwrap_or("<owner_id>");
ec2_collision_step(env, owner_key, owner_id, region, tag)
}
_ => String::new(),
};
let credentials = match d {
Deployment::Boundary => format!(
"- Credentials of an account administrator for account {account} WITHOUT
@@ -488,7 +682,7 @@ AWS CLI v2 recent enough that create-bucket accepts Tags, e.g. 2.39):
aws s3api put-public-access-block --bucket {bucket} --region {region} --expected-bucket-owner {account} --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
aws s3api put-bucket-policy --bucket {bucket} --region {region} --expected-bucket-owner {account} --policy '{policy}'
2. Deploy
{ec2_step}2. Deploy
---------
From this folder:
@@ -528,7 +722,7 @@ fn bundle_files(
let json = serde_json::to_string_pretty(vars)
.map_err(|e| format!("Could not encode the module variables: {e}"))?;
files.push((TFVARS.into(), json + "\n"));
files.push((README.into(), bundle_readme(d, account, &region, &bucket, &key)));
files.push((README.into(), bundle_readme(d, account, &region, &bucket, &key, vars)));
Ok(files)
}
@@ -1034,13 +1228,20 @@ pub async fn deploy_aws_environment(
region: String,
name: String,
network: NetworkChoice,
max_workspace_hours: Option<u32>,
app: AppHandle,
state: State<'_, AppState>,
) -> Result<String, AwsError> {
let ctx = context(&state, &connection_id, &region).await?;
let found = detect(&ctx).await?;
deploy_refusal(&found, &name).map_err(other)?;
let vars = environment_vars(&name, &ctx.region, &network).map_err(other)?;
let vars = environment_vars(
&name,
&ctx.region,
&network,
max_workspace_hours.unwrap_or(0),
)
.map_err(other)?;
let key = DeploymentKey::Environment(name);
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
}
@@ -1052,6 +1253,7 @@ pub async fn update_aws_environment(
connection_id: String,
region: String,
name: String,
max_workspace_hours: Option<u32>,
app: AppHandle,
state: State<'_, AppState>,
) -> Result<String, AwsError> {
@@ -1063,7 +1265,9 @@ pub async fn update_aws_environment(
.as_ref()
.ok_or_else(|| other("The environment has no readable record"))?;
let network = recorded_network(&ctx, info).await?;
let vars = environment_vars(&env.name, &ctx.region, &network).map_err(other)?;
// The recorded cap stays unless the caller changes it.
let hours = max_workspace_hours.unwrap_or(info.max_workspace_hours);
let vars = environment_vars(&env.name, &ctx.region, &network, hours).map_err(other)?;
let key = DeploymentKey::Environment(env.name.clone());
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
}
@@ -1077,6 +1281,7 @@ pub async fn repair_aws_environment(
region: String,
name: String,
network: Option<NetworkChoice>,
max_workspace_hours: Option<u32>,
app: AppHandle,
state: State<'_, AppState>,
) -> Result<String, AwsError> {
@@ -1092,7 +1297,11 @@ pub async fn repair_aws_environment(
))
}
};
let vars = environment_vars(&env.name, &ctx.region, &network).map_err(other)?;
// A valid record's cap stays unless the caller changes it.
let hours = max_workspace_hours
.or(env.info.as_ref().map(|i| i.max_workspace_hours))
.unwrap_or(0);
let vars = environment_vars(&env.name, &ctx.region, &network, hours).map_err(other)?;
let key = DeploymentKey::Environment(env.name.clone());
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
}
@@ -1113,7 +1322,8 @@ pub async fn destroy_incomplete_aws_environment(
let env = destroy_refusal(&found, &name).map_err(other)?;
// A destroy plans the removal of everything in state whatever the
// network variables say; they only have to be valid.
let vars = environment_vars(&env.name, &ctx.region, &NetworkChoice::Public).map_err(other)?;
let vars =
environment_vars(&env.name, &ctx.region, &NetworkChoice::Public, 0).map_err(other)?;
let key = DeploymentKey::Environment(env.name.clone());
opened(launch(app, &ctx, &key, &vars, TofuOp::Destroy).await)
}
@@ -1300,12 +1510,15 @@ pub async fn cancel_aws_deployment(
}
/// Onboard the caller to `environment`, as the owner their own
/// `aws:userid` names.
/// `aws:userid` names, with the EC2 capacity `ec2` asks for (omitted: keep
/// what their user record holds, none without one). With EC2 capacity the
/// per-user names must be free or already theirs (R3).
#[tauri::command]
pub async fn onboard_aws_user(
connection_id: String,
region: String,
environment: String,
ec2: Option<Ec2Choice>,
app: AppHandle,
state: State<'_, AppState>,
) -> Result<String, AwsError> {
@@ -1314,7 +1527,15 @@ pub async fn onboard_aws_user(
let owner = validate_owner_id(&ctx.who.user_id).map_err(other)?;
let found = detect(&ctx).await?;
onboard_refusal(&found, &env).map_err(other)?;
let vars = onboarding_vars(&env, &ctx.region, &owner);
let vars = prepare_onboarding(
&SdkOnboardApi::new(&ctx.cfg),
&env,
&ctx.region,
&ctx.who.account,
&owner,
ec2,
)
.await?;
let key = DeploymentKey::User {
owner_key: owner_key(&ctx.region, &owner),
env,
@@ -1322,6 +1543,22 @@ pub async fn onboard_aws_user(
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
}
/// The EC2 instance types onboarding offers (the app's built-in table) and
/// the one it offers first.
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct Ec2InstanceTable {
pub default_type: &'static str,
pub types: Vec<Ec2InstanceType>,
}
#[tauri::command]
pub fn list_ec2_instance_types() -> Ec2InstanceTable {
Ec2InstanceTable {
default_type: DEFAULT_EC2_INSTANCE_TYPE,
types: ec2_instance_types(),
}
}
/// Deploy (or re-apply) the account's permissions boundary. Refused when
/// the boundary exists but this region's bucket has no state for it.
#[tauri::command]
@@ -1361,8 +1598,10 @@ async fn pick_folder(window: &tauri::Window) -> Option<PathBuf> {
/// Write an admin bundle (D5) for `kind` into a folder the user picks:
/// `environment` names the environment (environment and onboarding),
/// `owner_id` the user to onboard (an `aws:userid`), `network` an
/// environment's network (public when omitted). `None` when the dialog was
/// dismissed; otherwise the folder written.
/// environment's network (public when omitted), `ec2` an onboarding's EC2
/// capacity (none when omitted; the bundle's README has the admin check
/// its names first). `None` when the dialog was dismissed; otherwise the
/// folder written and any warning.
#[allow(clippy::too_many_arguments)]
#[tauri::command]
pub async fn save_aws_admin_bundle(
@@ -1372,9 +1611,11 @@ pub async fn save_aws_admin_bundle(
environment: Option<String>,
owner_id: Option<String>,
network: Option<NetworkChoice>,
max_workspace_hours: Option<u32>,
ec2: Option<Ec2Compute>,
window: tauri::Window,
state: State<'_, AppState>,
) -> Result<Option<String>, AwsError> {
) -> Result<Option<SavedBundle>, AwsError> {
let need_env = || {
environment
.as_deref()
@@ -1386,8 +1627,13 @@ pub async fn save_aws_admin_bundle(
let (key, vars) = match kind {
ModuleKind::Environment => {
let env = need_env()?;
let vars = environment_vars(&env, &region_ok, &network.unwrap_or(NetworkChoice::Public))
.map_err(other)?;
let vars = environment_vars(
&env,
&region_ok,
&network.unwrap_or(NetworkChoice::Public),
max_workspace_hours.unwrap_or(0),
)
.map_err(other)?;
(DeploymentKey::Environment(env), vars)
}
ModuleKind::UserOnboard => {
@@ -1397,7 +1643,7 @@ pub async fn save_aws_admin_bundle(
.map(str::trim)
.ok_or_else(|| other("The user's owner id (aws:userid) is required"))
.and_then(|o| validate_owner_id(o).map_err(other))?;
let vars = onboarding_vars(&env, &region_ok, &owner);
let vars = onboarding_vars(&env, &region_ok, &owner, ec2.as_ref()).map_err(other)?;
(
DeploymentKey::User {
owner_key: owner_key(&region_ok, &owner),
@@ -1409,11 +1655,29 @@ pub async fn save_aws_admin_bundle(
ModuleKind::PermissionsBoundary => (DeploymentKey::Boundary, boundary_vars(&region_ok)),
};
let ctx = context(&state, &connection_id, &region).await?;
let warning = match (&key, &ec2, &owner_id) {
(DeploymentKey::User { env, .. }, Some(c), Some(owner)) => {
let api = SdkOnboardApi::new(&ctx.cfg);
bundle_ec2_warning(&api, env, &ctx.region, &ctx.who.account, owner.trim(), c).await
}
_ => None,
};
let Some(dir) = pick_folder(&window).await else {
return Ok(None);
};
write_bundle(&dir, key.deployment(), &ctx.who.account, &ctx.region, &vars).map_err(other)?;
Ok(Some(dir.to_string_lossy().into_owned()))
Ok(Some(SavedBundle {
folder: dir.to_string_lossy().into_owned(),
warning,
}))
}
/// A bundle written: its folder and, for an onboarding with EC2 capacity,
/// a warning when its EC2 name looks taken ([`bundle_ec2_warning`]).
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct SavedBundle {
pub folder: String,
pub warning: Option<String>,
}
#[cfg(test)]
@@ -6,7 +6,7 @@ use serde_json::json;
use super::*;
use crate::aws::tofu::Workdir;
use crate::aws::environments::{
EnvironmentState, Knowable, LockInfo, OnboardingState,
EnvironmentState, Knowable, LockInfo, NamedResource, OnboardingState, Param, UserCompute,
};
use crate::aws::tofu_modules::{
environment_vars, module_files, onboarding_vars, owner_key, state_key, ModuleKind,
@@ -32,6 +32,7 @@ fn info(name: &str, mode: &str, assign_public_ip: bool) -> EnvironmentInfo {
log_group: format!("/triple-c/{name}"),
network_mode: mode.into(),
tenancy: "strict".into(),
max_workspace_hours: 0,
}
}
@@ -165,6 +166,7 @@ fn update_reuses_the_recorded_network() {
"dev",
REGION,
&network_from_record(&existing, Some("vpc-0123456789abcdef0")).unwrap(),
0,
)
.unwrap();
assert_eq!(vars["network_mode"], "existing");
@@ -193,7 +195,7 @@ fn bundles_contain_the_module_backend_vars_and_readme() {
let dir = tempfile::tempdir().unwrap();
let ok = owner_key(REGION, USER_ID);
let d = Deployment::User { env: "dev", owner_key: &ok };
let vars = onboarding_vars("dev", REGION, USER_ID);
let vars = onboarding_vars("dev", REGION, USER_ID, None).unwrap();
let written = write_bundle(dir.path(), d, ACCOUNT, REGION, &vars).unwrap();
for (name, text) in module_files(ModuleKind::UserOnboard) {
@@ -251,6 +253,45 @@ fn bundles_contain_the_module_backend_vars_and_readme() {
}
assert!(!readme.contains("put-bucket-tagging"), "a later tag can't satisfy the create");
// The EC2 name-collision check (the app does it too, Task 4): the
// exact per-user names, and this user's record to compare against.
let record = format!("/triple-c/users/dev/{ok}");
for needle in [
"BEFORE tofu apply",
&format!("aws ecs describe-clusters --region {REGION} --clusters triple-c-dev--u-<tag>"),
&format!(
"aws autoscaling describe-auto-scaling-groups --region {REGION} \
--auto-scaling-group-names triple-c-dev--u-<tag>"
),
&format!("aws ssm get-parameter --region {REGION} --name {record}"),
"cluster/triple-c-dev--u-<tag>",
"--include TAGS",
"triple-c:owner",
&format!("owner_id ({USER_ID})"),
] {
assert!(readme.contains(needle), "README lacks {needle:?}:\n{readme}");
}
// With a tag in the variables, the commands name it.
let mut tagged = vars.clone();
tagged["ec2_instance_type"] = "m7i.large".into();
tagged["ec2_user_tag"] = "alice".into();
let other = tempfile::tempdir().unwrap();
write_bundle(other.path(), d, ACCOUNT, REGION, &tagged).unwrap();
let readme_tagged = fs::read_to_string(other.path().join("README.txt")).unwrap();
assert!(readme_tagged.contains("--clusters triple-c-dev--u-alice "), "{readme_tagged}");
assert!(readme_tagged.contains("--auto-scaling-group-names triple-c-dev--u-alice "));
assert!(!readme_tagged.contains("<tag>"));
// The check comes before the apply it guards.
let check = readme.find("BEFORE tofu apply").unwrap();
let apply = readme.find(" tofu apply").unwrap();
assert!(check < apply, "{readme}");
// Not in the other bundles.
let env_dir = tempfile::tempdir().unwrap();
let env_vars = environment_vars("dev", REGION, &NetworkChoice::Public, 0).unwrap();
write_bundle(env_dir.path(), Deployment::Environment("dev"), ACCOUNT, REGION, &env_vars).unwrap();
let env_readme = fs::read_to_string(env_dir.path().join("README.txt")).unwrap();
assert!(!env_readme.contains("EC2 capacity"), "{env_readme}");
// Writing again over our own files is fine...
write_bundle(dir.path(), d, ACCOUNT, REGION, &vars).unwrap();
// ...but an unrelated file (or folder) means the wrong folder.
@@ -266,7 +307,7 @@ fn bundles_contain_the_module_backend_vars_and_readme() {
#[test]
fn a_us_east_1_bundle_creates_its_bucket_without_a_location_constraint() {
let dir = tempfile::tempdir().unwrap();
let vars = environment_vars("dev", "us-east-1", &NetworkChoice::Public).unwrap();
let vars = environment_vars("dev", "us-east-1", &NetworkChoice::Public, 0).unwrap();
write_bundle(dir.path(), Deployment::Environment("dev"), ACCOUNT, "us-east-1", &vars).unwrap();
let readme = fs::read_to_string(dir.path().join("README.txt")).unwrap();
assert!(readme.contains("aws s3api create-bucket"), "{readme}");
@@ -505,12 +546,12 @@ fn destroy_is_refused_while_users_are_onboarded() {
fn only_your_own_stale_onboarding_lock_can_be_removed() {
use EnvironmentAction::*;
let mut stale = env("dev", EnvironmentState::Ready, vec![Join]);
stale.onboarding = Some(OnboardingState::Incomplete { lock: Some(lock(9000, false)) });
stale.onboarding = Some(OnboardingState::Incomplete { lock: Some(lock(9000, false)), ec2: None });
stale.onboarding_actions = vec![Unlock];
let mut live = env("live", EnvironmentState::Ready, vec![Join]);
live.onboarding = Some(OnboardingState::InProgress { lock: Some(lock(10, false)) });
let mut none = env("none", EnvironmentState::Ready, vec![Join]);
none.onboarding = Some(OnboardingState::Incomplete { lock: None });
none.onboarding = Some(OnboardingState::Incomplete { lock: None, ec2: None });
let s = scan(Knowable::Yes, vec![stale, live, none]);
let mine = owner_key(REGION, USER_ID);
assert!(onboarding_unlock_refusal(&s, "dev", &mine).is_ok());
@@ -629,3 +670,319 @@ fn ending_a_launch_never_clears_a_following_slot() {
assert_eq!(slots.begin_launch(dir).unwrap_err(), SlotBusy::Following);
slots.end_launch(Path::new("/w/env/none")); // nothing there: no-op
}
fn recorded(env: &str, tag: &str, max: u32) -> UserCompute {
let stem = crate::aws::tofu_modules::user_ec2_stem(env, tag);
UserCompute {
instance_type: "m7g.xlarge".into(),
max_instances: max,
user_tag: tag.into(),
cluster_arn: format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/{stem}"),
capacity_provider: stem,
}
}
#[test]
fn a_reapply_keeps_the_recorded_compute_unless_changed() {
let rec = UserRecord::Valid(recorded("dev", "alice", 3));
let kept = Ec2Compute {
instance_type: "m7g.xlarge".into(),
max_instances: 3,
user_tag: "alice".into(),
};
// Omitted: keep what the record says (or none without one).
assert_eq!(resolve_ec2(None, &rec), Ok(Some(kept.clone())));
assert_eq!(resolve_ec2(None, &UserRecord::Absent), Ok(None));
// Explicitly none: the re-apply removes it.
assert_eq!(resolve_ec2(Some(Ec2Choice::None), &rec), Ok(None));
// A new choice wins.
let new = Ec2Compute {
instance_type: "m7i.large".into(),
max_instances: 1,
user_tag: "alice".into(),
};
assert_eq!(
resolve_ec2(Some(Ec2Choice::Ec2(new.clone())), &rec),
Ok(Some(new.clone()))
);
// A record that exists but can't be used is never read as "none" (that
// would tear the capacity down); an explicit choice still goes.
let bad = UserRecord::Unusable("schema is not 1".into());
let e = resolve_ec2(None, &bad).unwrap_err();
assert!(e.contains("schema is not 1") && e.contains("explicitly"), "{e}");
assert_eq!(resolve_ec2(Some(Ec2Choice::None), &bad), Ok(None));
assert_eq!(resolve_ec2(Some(Ec2Choice::Ec2(new.clone())), &bad), Ok(Some(new.clone())));
// The kept values reach the module's variables unchanged.
let vars = onboarding_vars("dev", REGION, USER_ID, resolve_ec2(None, &rec).unwrap().as_ref()).unwrap();
assert_eq!(vars["ec2_instance_type"], "m7g.xlarge");
assert_eq!(vars["ec2_max_instances"], 3);
assert_eq!(vars["ec2_user_tag"], "alice");
// The UI's JSON for each choice.
let none: Ec2Choice = serde_json::from_value(json!({"kind": "none"})).unwrap();
assert_eq!(none, Ec2Choice::None);
let ec2: Ec2Choice = serde_json::from_value(json!({
"kind": "ec2", "instance_type": "m7i.large", "max_instances": 1, "user_tag": "alice"
}))
.unwrap();
assert_eq!(ec2, Ec2Choice::Ec2(new));
}
fn held(kind: &'static str, owner: Option<&str>) -> NameHolder {
NameHolder {
kind,
owner: owner.map(String::from),
}
}
#[test]
fn ec2_names_held_by_another_owner_are_refused() {
let stem = "triple-c-dev--u-alice";
let me = USER_ID;
// Free names: fine, with or without a record.
assert!(ec2_collision_refusal(stem, &[], me, None).is_ok());
assert!(ec2_collision_refusal(stem, &[], me, Some(&recorded("dev", "bob", 1))).is_ok());
let cluster = |o| held("ECS cluster", o);
let group = |o| held("Auto Scaling group", o);
// Tagged as mine: a half-built capacity (no record yet) is re-applied.
assert!(ec2_collision_refusal(stem, &[cluster(Some(me)), group(Some(me))], me, None).is_ok());
// Another owner's, or untagged: refused, naming the resource.
let e = ec2_collision_refusal(stem, &[cluster(Some("AROAOTHER:bob"))], me, None).unwrap_err();
assert!(e.contains("ECS cluster") && e.contains(stem) && e.contains("another onboarding"), "{e}");
assert!(e.contains("triple-c-user"), "{e}");
let e = ec2_collision_refusal(stem, &[cluster(Some(me)), group(None)], me, None).unwrap_err();
assert!(e.contains("Auto Scaling group") && e.contains("isn't tagged"), "{e}");
// The record naming the cluster is the fallback for an untagged one
// only: one tagged as someone else's is refused even then.
let mine = recorded("dev", "alice", 2);
assert!(ec2_collision_refusal(stem, &[cluster(None), group(None)], me, Some(&mine)).is_ok());
assert!(ec2_collision_refusal(stem, &[cluster(None), group(Some(me))], me, Some(&mine)).is_ok());
let e = ec2_collision_refusal(stem, &[cluster(None), group(Some("x"))], me, Some(&mine)).unwrap_err();
assert!(e.contains("Auto Scaling group") && e.contains("another onboarding"), "{e}");
// A record naming another cluster doesn't help.
assert!(ec2_collision_refusal(stem, &[cluster(None)], me, Some(&recorded("dev", "bob", 1))).is_err());
// A cluster that merely ends in the same text is not the same cluster.
let mut lookalike = recorded("dev", "alice", 1);
lookalike.cluster_arn = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/x{stem}");
assert!(ec2_collision_refusal(stem, &[cluster(None)], me, Some(&lookalike)).is_err());
}
/// An `OnboardApi` answering from fixed data, recording what it was asked.
#[derive(Default)]
struct FakeOnboard {
record: Option<String>,
record_denied: bool,
cluster: Option<NamedResource>,
group: Option<NamedResource>,
group_denied: bool,
calls: std::sync::Mutex<Vec<String>>,
}
impl FakeOnboard {
fn calls(&self) -> Vec<String> {
self.calls.lock().unwrap().clone()
}
}
impl OnboardApi for FakeOnboard {
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
self.calls.lock().unwrap().push(format!("parameter {name}"));
Ok(match (&self.record, self.record_denied) {
(_, true) => Param::Denied,
(Some(v), _) => Param::Value(v.clone()),
(None, _) => Param::Missing,
})
}
async fn cluster(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
self.calls.lock().unwrap().push(format!("cluster {name}"));
Ok(self.cluster.clone())
}
async fn group(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
self.calls.lock().unwrap().push(format!("group {name}"));
if self.group_denied {
return Err(AwsError::AccessDenied {
action: "autoscaling:DescribeAutoScalingGroups".into(),
message: "denied".into(),
});
}
Ok(self.group.clone())
}
}
fn named(status: Option<&str>, owner: Option<&str>) -> NamedResource {
NamedResource {
status: status.map(String::from),
tags: owner
.map(|o| vec![("triple-c:owner".to_string(), o.to_string())])
.unwrap_or_default(),
}
}
fn record_json(env: &str, tag: &str) -> String {
let r = recorded(env, tag, 2);
json!({
"schema": 1, "instance_type": r.instance_type, "cluster_arn": r.cluster_arn,
"capacity_provider": r.capacity_provider, "user_tag": r.user_tag, "max_instances": 2,
})
.to_string()
}
fn prepare(api: &FakeOnboard, choice: Option<Ec2Choice>) -> Result<Value, AwsError> {
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(prepare_onboarding(api, "dev", REGION, ACCOUNT, USER_ID, choice))
}
fn alice() -> Option<Ec2Choice> {
Some(Ec2Choice::Ec2(Ec2Compute {
instance_type: "m7i.large".into(),
max_instances: 1,
user_tag: "alice".into(),
}))
}
#[test]
fn onboarding_checks_the_ec2_names_before_it_applies() {
let stem = "triple-c-dev--u-alice";
// Fargate only: no name lookups at all.
let api = FakeOnboard::default();
let vars = prepare(&api, Some(Ec2Choice::None)).unwrap();
assert_eq!(vars["ec2_instance_type"], "");
assert!(api.calls().iter().all(|c| c.starts_with("parameter ")), "{:?}", api.calls());
// Free names: the vars come back.
let api = FakeOnboard::default();
let vars = prepare(&api, alice()).unwrap();
assert_eq!(vars["ec2_user_tag"], "alice");
assert!(api.calls().contains(&format!("cluster {stem}")));
assert!(api.calls().contains(&format!("group {stem}")));
// Another owner's cluster: refused, so nothing is launched.
let api = FakeOnboard {
cluster: Some(named(Some("ACTIVE"), Some("AROAOTHER:bob"))),
..Default::default()
};
let e = prepare(&api, alice()).unwrap_err();
assert!(format!("{e:?}").contains("another onboarding"), "{e:?}");
// My own half-built cluster and group (a failed apply, no record yet).
let api = FakeOnboard {
cluster: Some(named(Some("PROVISIONING"), Some(USER_ID))),
group: Some(named(None, Some(USER_ID))),
..Default::default()
};
assert!(prepare(&api, alice()).is_ok());
// Untagged: refused unless my record names it.
let untagged = || FakeOnboard {
cluster: Some(named(Some("ACTIVE"), None)),
..Default::default()
};
assert!(prepare(&untagged(), alice()).is_err());
let api = FakeOnboard {
record: Some(record_json("dev", "alice")),
..untagged()
};
assert!(prepare(&api, alice()).is_ok());
// A deleted (INACTIVE) cluster doesn't hold the name; every other
// status does.
let api = FakeOnboard {
cluster: Some(named(Some("INACTIVE"), None)),
..Default::default()
};
assert!(prepare(&api, alice()).is_ok());
for status in ["ACTIVE", "PROVISIONING", "DEPROVISIONING", "FAILED"] {
let api = FakeOnboard {
cluster: Some(named(Some(status), Some("AROAOTHER:bob"))),
..Default::default()
};
assert!(prepare(&api, alice()).is_err(), "{status}");
}
// An Auto Scaling group alone is enough to refuse.
let api = FakeOnboard {
group: Some(named(None, Some("AROAOTHER:bob"))),
..Default::default()
};
assert!(prepare(&api, alice()).is_err());
// A denied group lookup is an error, never "free".
let api = FakeOnboard {
group_denied: true,
..Default::default()
};
assert!(prepare(&api, alice()).is_err());
}
#[test]
fn capacity_kept_from_the_record_is_checked_too() {
let stem = "triple-c-dev--u-alice";
// Omitted choice + a record: the record's capacity, and its names are
// checked (here another owner's group appeared under it).
let api = FakeOnboard {
record: Some(record_json("dev", "alice")),
cluster: Some(named(Some("ACTIVE"), Some(USER_ID))),
group: Some(named(None, Some(USER_ID))),
..Default::default()
};
let vars = prepare(&api, None).unwrap();
assert_eq!(vars["ec2_instance_type"], "m7g.xlarge");
assert_eq!(vars["ec2_max_instances"], 2);
assert!(api.calls().contains(&format!("group {stem}")));
// A record that can't be used (unreadable or invalid) and no choice:
// refused before any name lookup or launch.
for api in [
FakeOnboard {
record_denied: true,
..Default::default()
},
FakeOnboard {
record: Some("{\"schema\": 9}".into()),
..Default::default()
},
] {
let e = prepare(&api, None).unwrap_err();
assert!(format!("{e:?}").contains("explicitly"), "{e:?}");
assert!(api.calls().iter().all(|c| c.starts_with("parameter ")));
}
}
#[test]
fn a_bundle_warns_when_its_cluster_name_looks_taken() {
let run = |api: &FakeOnboard| {
let c = Ec2Compute {
instance_type: "m7i.large".into(),
max_instances: 1,
user_tag: "alice".into(),
};
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(bundle_ec2_warning(api, "dev", REGION, ACCOUNT, USER_ID, &c))
};
assert_eq!(run(&FakeOnboard::default()), None);
let api = FakeOnboard {
cluster: Some(named(Some("ACTIVE"), Some("AROAOTHER:bob"))),
..Default::default()
};
assert!(run(&api).unwrap().contains("another onboarding"));
// Only the cluster half: a workspace user can't read Auto Scaling.
assert!(!api.calls().iter().any(|c| c.starts_with("group ")));
let api = FakeOnboard {
cluster: Some(named(Some("ACTIVE"), Some(USER_ID))),
..Default::default()
};
assert_eq!(run(&api), None);
}
#[test]
fn the_ui_gets_the_apps_instance_table() {
let table = list_ec2_instance_types();
assert_eq!(table.types, crate::models::project::ec2_instance_types());
assert_eq!(table.types.len(), 30);
assert_eq!(table.default_type, "m7i.large");
assert!(table.types.iter().any(|t| t.instance_type == table.default_type));
}
+200 -4
View File
@@ -2,15 +2,19 @@
//! sign-in and the permission check.
use std::future::Future;
use std::sync::OnceLock;
use std::time::SystemTime;
use tauri::State;
use crate::aws::permissions::{check_permissions, PermissionReport, SdkPermissionApi};
use crate::aws::policies::{policy_files, Tier};
use crate::aws::pricing::{self, HourlyEstimate, RatesCache, SdkPricingApi};
use crate::aws::{caller_identity, sdk_config, sdk_config_for_keys, AwsError, CallerIdentity};
use crate::models::project::{ec2_instance_spec, LaunchType};
use crate::models::{
validate_connection_name, validate_profile_name, validate_supported_region, AppSettings, AwsAuth,
HostConnection, HostKind,
AwsWorkspaceConfig, HostConnection, HostKind,
};
use crate::storage::secure::{self, AwsKeys};
use crate::storage::settings_store::SettingsStore;
@@ -301,6 +305,97 @@ pub fn aws_policy_document(tier: Tier) -> Vec<PolicyFile> {
.collect()
}
/// The cost estimate's rate cache, `<data dir>/triple-c/aws-pricing/`, shared
/// by every call (it also remembers recent failures). `None` without a data
/// folder.
fn pricing_cache() -> Option<&'static RatesCache> {
static CACHE: OnceLock<Option<RatesCache>> = OnceLock::new();
CACHE
.get_or_init(|| pricing::default_cache_dir().map(RatesCache::new))
.as_ref()
}
/// The estimated on-demand hourly cost of `config` as a Fargate task in
/// `region` (Q5). It takes the config rather than a project id because
/// Project Home's Host settings ask while the user edits, and the Overview
/// asks for the saved one; `connection_id` is the project's connection, whose
/// credentials sign the Price List call (made in us-east-1). **Never an
/// error:** a bad region, a missing connection, a denied or slow call (8 s
/// bound) or an unreadable price list is `None`, shown as "Estimate
/// unavailable". Rates are cached a day per region.
///
/// An EC2 project (E10) is priced as `instance_type` (the user's, from their
/// onboarding record, which the UI already holds): its on-demand hourly
/// price, the same cache and back-off rules. No instance type, or one not in
/// the app's table, is `None`.
#[tauri::command]
pub async fn aws_cost_estimate(
connection_id: String,
region: String,
config: AwsWorkspaceConfig,
instance_type: Option<String>,
state: State<'_, AppState>,
) -> Result<Option<HourlyEstimate>, String> {
let Some(cache) = pricing_cache() else {
return Ok(None);
};
let settings = state.settings_store.get();
Ok(cost_estimate(
cache,
&connection_id,
&region,
&config,
instance_type.as_deref(),
&settings,
SystemTime::now(),
)
.await)
}
async fn cost_estimate(
cache: &RatesCache,
connection_id: &str,
region: &str,
config: &AwsWorkspaceConfig,
instance_type: Option<&str>,
settings: &AppSettings,
now: SystemTime,
) -> Option<HourlyEstimate> {
let region = validate_supported_region(region).ok()?;
// Only now touch the connection (a keys connection reads the keychain),
// and inside the fetch's 8 s bound: reading a profile's credentials can
// hang as well (an SSO or process provider).
let api = || async {
let conn = aws_conn(connection_id, settings).ok()?;
let cfg = sdk_config(&conn, pricing::PRICING_REGION).await.ok()?;
Some(SdkPricingApi::new(&cfg))
};
if config.launch_type == LaunchType::Ec2 {
let instance_type = instance_type.filter(|t| ec2_instance_spec(t).is_some())?;
let instance = async {
match cache.read_ec2(&region, instance_type, now) {
Some((price, true)) => Some(price),
_ => {
cache
.ec2_price_with::<SdkPricingApi, _>(api(), &region, instance_type, now)
.await
}
}
};
// The root volume's price is best effort: without it the estimate
// is the instance's alone (and says so).
let gp3 = cache.gp3_price_with::<SdkPricingApi, _>(api(), &region, now);
let (price, gp3) = tokio::join!(instance, gp3);
return Some(pricing::estimate_ec2(config, price?, gp3));
}
let rates = match cache.read(&region, now) {
Some((rates, true)) => Some(rates),
// Without a connection, a stale cache still answers.
_ => cache.rates_with::<SdkPricingApi, _>(api(), &region, now).await,
}?;
Some(pricing::estimate(config, &rates.for_arch(config.arch)?))
}
#[cfg(test)]
mod tests {
use super::*;
@@ -542,16 +637,117 @@ mod tests {
fn the_policy_document_is_every_file_of_the_tier() {
let admin = aws_policy_document(Tier::EnvironmentAdmin);
let names: Vec<&str> = admin.iter().map(|f| f.name.as_str()).collect();
assert_eq!(names, ["environment-admin.json", "environment-admin-network.json"]);
assert_eq!(
names,
[
"environment-admin.json",
"environment-admin-network.json",
"environment-admin-compute.json",
"environment-admin-scaling.json"
]
);
for f in &admin {
serde_json::from_str::<serde_json::Value>(&f.json).expect("policy JSON");
}
let ws = aws_policy_document(Tier::WorkspaceUser);
assert_eq!(ws.len(), 1);
assert_eq!(ws[0].name, "workspace-user.json");
let names: Vec<&str> = ws.iter().map(|f| f.name.as_str()).collect();
assert_eq!(names, ["workspace-user.json", "workspace-user-tasks.json"]);
for f in &ws {
serde_json::from_str::<serde_json::Value>(&f.json).expect("policy JSON");
}
assert_eq!(
serde_json::to_value(&ws[0]).unwrap()["name"],
"workspace-user.json"
);
}
fn sample_rates() -> pricing::RegionRates {
let x86 = pricing::FargateRates {
vcpu_hour: 0.04,
gb_hour: 0.004,
storage_gb_hour: 0.0001,
};
pricing::RegionRates {
x86_64: Some(x86),
arm64: None,
}
}
#[tokio::test]
async fn the_cost_estimate_answers_from_a_fresh_cache_without_a_connection() {
let dir = tempfile::tempdir().unwrap();
let cache = RatesCache::new(dir.path().to_path_buf());
let now = SystemTime::now();
cache.write("us-west-2", &sample_rates(), now).unwrap();
let settings = AppSettings::default();
let config = AwsWorkspaceConfig {
cpu: 1024,
memory_mib: 2048,
ephemeral_gib: 20,
spot: true,
..Default::default()
};
let e = cost_estimate(&cache, "gone", "us-west-2", &config, None, &settings, now)
.await
.expect("estimate");
assert!((e.usd_per_hour - (0.04 + 2.0 * 0.004)).abs() < 1e-9);
assert!(e.note_spot && e.efs_billed_separately);
// An architecture the price list lacked has no estimate.
let arm = AwsWorkspaceConfig {
arch: crate::models::Arch::Arm64,
..config.clone()
};
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &arm, None, &settings, now).await, None);
}
#[tokio::test]
async fn the_cost_estimate_is_none_never_an_error() {
let dir = tempfile::tempdir().unwrap();
let cache = RatesCache::new(dir.path().to_path_buf());
let settings = AppSettings::default();
let config = AwsWorkspaceConfig::default();
let now = SystemTime::now();
// No cache and no such connection.
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &config, None, &settings, now).await, None);
// A region that isn't one (it would name the cache file).
cache.write("us-west-2", &sample_rates(), now).unwrap();
for bad in ["../us-west-2", "", "us-west-2/x"] {
assert_eq!(cost_estimate(&cache, "gone", bad, &config, None, &settings, now).await, None);
}
}
/// E10: an EC2 project is priced as the user's instance type, from the
/// same cache; without a known type there is no estimate.
#[tokio::test]
async fn an_ec2_projects_estimate_is_its_instance_types_price() {
let dir = tempfile::tempdir().unwrap();
let cache = RatesCache::new(dir.path().to_path_buf());
let now = SystemTime::now();
cache.write("us-west-2", &sample_rates(), now).unwrap();
cache.write_ec2("us-west-2", "m7i.large", 0.1008, now).unwrap();
let settings = AppSettings::default();
let config = AwsWorkspaceConfig {
launch_type: LaunchType::Ec2,
cpu: 1024,
memory_mib: 2048,
storage: crate::models::project::StorageMode::Ephemeral,
..Default::default()
};
let e = cost_estimate(&cache, "gone", "us-west-2", &config, Some("m7i.large"), &settings, now)
.await
.expect("estimate");
assert!((e.usd_per_hour - 0.1008).abs() < 1e-9);
assert!(e.ec2_instance && !e.note_spot && !e.efs_billed_separately);
// No instance type, one not in the table, or one with no cached price
// (and no connection to ask): no estimate — never Fargate's.
for t in [None, Some("x9.huge"), Some("m7g.large")] {
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &config, t, &settings, now).await, None, "{t:?}");
}
// A Fargate project ignores an instance type.
let fargate = AwsWorkspaceConfig { launch_type: LaunchType::Fargate, ..config.clone() };
let f = cost_estimate(&cache, "gone", "us-west-2", &fargate, Some("m7i.large"), &settings, now)
.await
.expect("fargate");
assert!(!f.ec2_instance);
}
}
@@ -432,6 +432,11 @@ pub async fn get_container_staleness(
.projects_store
.get(&project_id)
.ok_or_else(|| format!("Project {} not found", project_id))?;
// An AWS task has no local container or snapshot to compare: no claim
// either way ("unknown"), and nothing is probed.
if project.is_aws() {
return Ok(ContainerStaleness::default());
}
let settings = state.settings_store.get();
let base_image = crate::models::container_config::resolve_image_name(
&settings.image_source,
@@ -708,6 +713,9 @@ pub async fn migrate_project_to_base(
app_handle: tauri::AppHandle,
state: State<'_, AppState>,
) -> Result<MigrationReport, String> {
if let Some(p) = state.projects_store.get(&project_id) {
crate::commands::project_commands::refuse_aws(&p, "Updating to the current base image")?;
}
let _guard = match ActiveGuard::acquire(&project_id) {
Ok(guard) => guard,
Err(busy) => return Ok(MigrationReport::failed_preflight(&busy)),
+1
View File
@@ -15,6 +15,7 @@ pub mod migration_commands;
pub mod notes_commands;
pub mod project_commands;
pub mod settings_commands;
pub mod stop_guard;
pub mod settings_export_commands;
pub mod stt_commands;
pub mod terminal_commands;
File diff suppressed because it is too large. Load diff
+460
View File
@@ -0,0 +1,460 @@
//! The AWS stop guard: what `triple-c-wip-push` says about a workspace's
//! unsaved git work, and the push a Stop may run first.
//!
//! The decisions are made over [`WipRunner`] so a fake target exercises them;
//! [`ContainerTarget`] runs the script through the agent as `claude` (the
//! script needs the user's credential helper and `~/.ssh`).
use std::future::Future;
use std::time::Duration;
use crate::host::ContainerTarget;
use crate::models::{RepoState, UnsavedWork, WipChoice};
/// The script, baked into the image (`container/triple-c-wip-push`).
pub const WIP_PUSH: &str = "/usr/local/bin/triple-c-wip-push";
/// A dry run only reads `git status`; it should be quick.
pub const DRY_RUN_TIMEOUT: Duration = Duration::from_secs(30);
/// A push goes over the network, once per dirty repo.
pub const PUSH_TIMEOUT: Duration = Duration::from_secs(300);
/// Cap on the push output carried into an error.
const MAX_OUTPUT: usize = 4000;
/// How much sooner than the client's timeout the in-container `timeout`
/// ends the script (its TERM, then 5 s to the KILL): an exec the client gives
/// up on is not killed by the agent, so the container bounds it itself.
const IN_CONTAINER_MARGIN: Duration = Duration::from_secs(10);
/// Where the entrypoint's stop hook looks for the app's decision (Task 7's
/// file, which the idle watcher also writes `idle` to).
pub const STOP_REASON_FILE: &str = "/run/triple-c/stop-reason";
/// Recorded after the guard's push succeeded: the hook does not push again.
pub const MARKER_PUSHED: &str = "pushed";
/// Recorded for "Stop without pushing": the hook does not push.
pub const MARKER_DISCARD: &str = "discard";
/// The workspace, as the stop guard uses it.
pub trait WipRunner: Sync {
/// Run the WIP script as `claude`: `(combined output, exit code)`. The
/// caller waits at most `limit`; the run must not outlive it.
fn run(
&self,
args: Vec<String>,
limit: Duration,
) -> impl Future<Output = Result<(String, i64), String>> + Send;
/// Record `marker` in [`STOP_REASON_FILE`] (as root: the directory is root's).
fn write_stop_reason(&self, marker: &str) -> impl Future<Output = Result<(), String>> + Send;
/// Remove [`STOP_REASON_FILE`] (as root), so the hook pushes as configured again.
fn clear_stop_reason(&self) -> impl Future<Output = Result<(), String>> + Send;
}
impl WipRunner for ContainerTarget {
async fn run(&self, args: Vec<String>, limit: Duration) -> Result<(String, i64), String> {
self.exec_oneshot_as("claude", wip_command(args, limit), Vec::new()).await
}
async fn write_stop_reason(&self, marker: &str) -> Result<(), String> {
let (out, code) = self
.exec_oneshot_as("root", stop_reason_command(marker), Vec::new())
.await?;
if code == 0 {
Ok(())
} else {
Err(format!("exit {code}: {}", cap(&out)))
}
}
async fn clear_stop_reason(&self) -> Result<(), String> {
let (out, code) = self
.exec_oneshot_as("root", clear_stop_reason_command(), Vec::new())
.await?;
if code == 0 {
Ok(())
} else {
Err(format!("exit {code}: {}", cap(&out)))
}
}
}
/// The script's argv under coreutils `timeout`, ending it
/// [`IN_CONTAINER_MARGIN`] before the client's `limit` (KILL 5 s after the
/// TERM), so a push the app gave up on is not left running in the workspace.
pub fn wip_command(args: Vec<String>, limit: Duration) -> Vec<String> {
let secs = limit.saturating_sub(IN_CONTAINER_MARGIN).as_secs().max(1);
let mut cmd = vec![
"/usr/bin/timeout".to_string(),
"-k".to_string(),
"5".to_string(),
secs.to_string(),
WIP_PUSH.to_string(),
];
cmd.extend(args);
cmd
}
/// The root command that forgets a recorded marker (a fixed argv).
pub fn clear_stop_reason_command() -> Vec<String> {
vec!["rm".into(), "-f".into(), STOP_REASON_FILE.into()]
}
/// After StopTask failed: the workspace keeps running, so a marker this stop
/// recorded must not suppress the push of a later stop (Spot, ECS, idle) the
/// user never chose for. Best effort; logged.
pub async fn forget_choice<R: WipRunner>(runner: Option<&R>, choice: Option<WipChoice>) {
if choice.is_none() {
return;
}
let Some(r) = runner else {
log::warn!("Stop failed and the workspace is unreachable; its stop marker may remain");
return;
};
match tokio::time::timeout(DRY_RUN_TIMEOUT, r.clear_stop_reason()).await {
Ok(Ok(())) => {}
Ok(Err(e)) => log::warn!("Could not remove the stop marker after a failed stop: {e}"),
Err(_) => log::warn!("Removing the stop marker after a failed stop timed out"),
}
}
/// The root command that records `marker`; the value travels as `$1`, never
/// inside the script. The directory stays 0711 (the entrypoint's mode): the
/// `claude` user reads the run id and its WIP record in it by name (the
/// marker itself is 0600).
pub fn stop_reason_command(marker: &str) -> Vec<String> {
vec![
"sh".into(),
"-c".into(),
format!(
"umask 077 && mkdir -p /run/triple-c && chmod 711 /run/triple-c && printf '%s\\n' \"$1\" > {STOP_REASON_FILE}"
),
"sh".into(),
marker.into(),
]
}
/// The repository lines of a dry run. Lines that are not JSON objects (a
/// warning on stderr) are skipped; a JSON line of the wrong shape is an error.
pub fn parse_dry_run(output: &str) -> Result<Vec<RepoState>, String> {
output
.lines()
.map(str::trim)
.filter(|l| l.starts_with('{'))
.map(|l| {
serde_json::from_str::<RepoState>(l)
.map_err(|e| format!("Unexpected output from the WIP check: {e}"))
})
.collect()
}
fn unchecked(reason: String) -> UnsavedWork {
UnsavedWork { checked: false, repos: Vec::new(), error: Some(reason) }
}
/// Ask the workspace what a stop would lose. `runner` is `None` when the
/// workspace's agent is not reachable (no tunnel).
pub async fn unsaved_work<R: WipRunner>(runner: Option<&R>, timeout: Duration) -> UnsavedWork {
let Some(runner) = runner else {
return unchecked(
"Triple-C is not connected to the workspace, so it can't check for unsaved work."
.into(),
);
};
let res = tokio::time::timeout(timeout, runner.run(vec!["--dry-run".into()], timeout)).await;
match res {
Err(_) => unchecked("The workspace did not answer the check for unsaved work in time.".into()),
Ok(Err(e)) => unchecked(format!("The workspace could not be asked about unsaved work: {e}")),
Ok(Ok((out, code))) if code != 0 => unchecked(format!(
"The check for unsaved work failed (exit {code}): {}",
cap(&out)
)),
Ok(Ok((out, _))) => match parse_dry_run(&out) {
Ok(repos) => UnsavedWork { checked: true, repos, error: None },
Err(e) => unchecked(e),
},
}
}
fn cap(s: &str) -> String {
let t = s.trim();
if t.len() <= MAX_OUTPUT {
return t.to_string();
}
let mut end = t.len() - MAX_OUTPUT;
while !t.is_char_boundary(end) {
end += 1;
}
format!("…{}", &t[end..])
}
/// What Stop does before stopping, given the user's choice. `Push` runs the
/// push and fails (aborting the stop) with its output when it does not
/// succeed; `Discard` and no choice do nothing.
pub async fn before_stop<R: WipRunner>(
runner: Option<&R>,
choice: Option<WipChoice>,
timeout: Duration,
) -> Result<(), String> {
if choice != Some(WipChoice::Push) {
return Ok(());
}
let runner = runner.ok_or_else(|| {
"Triple-C is not connected to the workspace, so it can't push unsaved work. The \
workspace was not stopped."
.to_string()
})?;
let res =
tokio::time::timeout(timeout, runner.run(vec!["--reason".into(), "stop".into()], timeout)).await;
match res {
Err(_) => Err("Pushing unsaved work did not finish in time. The workspace was not stopped.".into()),
Ok(Err(e)) => Err(format!("Could not push unsaved work: {e}. The workspace was not stopped.")),
Ok(Ok((out, 0))) => {
log::info!("WIP push before stop: {}", cap(&out));
Ok(())
}
Ok(Ok((out, code))) => Err(format!(
"Pushing unsaved work failed (exit {code}), so the workspace was not stopped:\n{}",
cap(&out)
)),
}
}
/// The note a stop returns when the hook's own push may still run.
pub const MARKER_NOT_RECORDED: &str = "Triple-C could not tell the workspace what you chose, so its own \
push of work in progress on shutdown may still run (if it is turned on).";
/// Everything the guard does before StopTask: `Push` pushes (a failure is an
/// `Err` and the stop must not happen), then records `pushed`; `Discard`
/// records `discard`; no choice does nothing. A marker that can't be
/// written does not stop the stop: `Ok(Some(note))` says the hook may push.
pub async fn prepare_stop<R: WipRunner>(
runner: Option<&R>,
choice: Option<WipChoice>,
push_timeout: Duration,
) -> Result<Option<String>, String> {
let marker = match choice {
None => return Ok(None),
Some(WipChoice::Push) => {
before_stop(runner, choice, push_timeout).await?;
MARKER_PUSHED
}
Some(WipChoice::Discard) => MARKER_DISCARD,
};
let written = match runner {
Some(r) => tokio::time::timeout(DRY_RUN_TIMEOUT, r.write_stop_reason(marker))
.await
.unwrap_or_else(|_| Err("timed out".into())),
None => Err("not connected to the workspace".into()),
};
match written {
Ok(()) => Ok(None),
Err(e) => {
log::warn!("Could not record the stop guard's choice ({marker}): {e}");
Ok(Some(MARKER_NOT_RECORDED.to_string()))
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use std::sync::Mutex;
pub(crate) struct Fake {
pub reply: Result<(String, i64), String>,
pub calls: Mutex<Vec<Vec<String>>>,
pub hang: bool,
pub markers: Mutex<Vec<String>>,
pub marker_fails: bool,
}
impl Fake {
pub fn new(reply: Result<(String, i64), String>) -> Self {
Fake { reply, calls: Mutex::new(Vec::new()), hang: false, markers: Mutex::new(Vec::new()), marker_fails: false }
}
fn markers(&self) -> Vec<String> {
self.markers.lock().unwrap().clone()
}
fn calls(&self) -> Vec<Vec<String>> {
self.calls.lock().unwrap().clone()
}
}
impl WipRunner for Fake {
async fn run(&self, args: Vec<String>, _limit: Duration) -> Result<(String, i64), String> {
self.calls.lock().unwrap().push(args);
if self.hang {
std::future::pending::<()>().await;
}
self.reply.clone()
}
async fn write_stop_reason(&self, marker: &str) -> Result<(), String> {
if self.marker_fails {
return Err("agent unreachable".into());
}
self.markers.lock().unwrap().push(marker.to_string());
Ok(())
}
async fn clear_stop_reason(&self) -> Result<(), String> {
self.markers.lock().unwrap().push("<cleared>".to_string());
Ok(())
}
}
const T: Duration = Duration::from_secs(5);
#[test]
fn a_dry_run_is_parsed_skipping_non_json_lines() {
let out = "warning: something\n{\"folder\":\"api\",\"dirty\":true,\"unpushed\":0,\"branch\":\"main\"}\n\
{\"folder\":\"web\",\"dirty\":false,\"unpushed\":2,\"branch\":\"HEAD\"}\n";
let repos = parse_dry_run(out).unwrap();
assert_eq!(repos.len(), 2);
assert_eq!(repos[0], RepoState { folder: "api".into(), dirty: true, unpushed: 0, branch: "main".into(), cloning: false });
assert_eq!(repos[1].unpushed, 2);
assert!(parse_dry_run("{\"folder\":1}").is_err());
assert!(parse_dry_run("").unwrap().is_empty());
}
#[tokio::test]
async fn the_check_runs_a_dry_run_and_reports_the_repos() {
let fake = Fake::new(Ok(("{\"folder\":\"a\",\"dirty\":false,\"unpushed\":0,\"branch\":\"main\"}".into(), 0)));
let w = unsaved_work(Some(&fake), T).await;
assert!(w.checked && w.error.is_none());
assert_eq!(w.repos.len(), 1);
assert_eq!(fake.calls(), vec![vec!["--dry-run".to_string()]]);
}
#[tokio::test]
async fn an_unreachable_workspace_is_unchecked_with_a_reason() {
let w = unsaved_work::<Fake>(None, T).await;
assert!(!w.checked);
assert!(w.error.as_deref().unwrap().contains("not connected"));
let failing = Fake::new(Err("connection refused".into()));
let w = unsaved_work(Some(&failing), T).await;
assert!(!w.checked && w.error.unwrap().contains("connection refused"));
let nonzero = Fake::new(Ok(("boom".into(), 2)));
let w = unsaved_work(Some(&nonzero), T).await;
assert!(!w.checked && w.error.unwrap().contains("exit 2"));
}
#[tokio::test(start_paused = true)]
async fn a_check_that_hangs_times_out_unchecked() {
let mut fake = Fake::new(Ok((String::new(), 0)));
fake.hang = true;
let w = unsaved_work(Some(&fake), Duration::from_secs(1)).await;
assert!(!w.checked && w.error.unwrap().contains("in time"));
}
#[tokio::test]
async fn push_runs_the_script_with_reason_stop() {
let fake = Fake::new(Ok(("pushed".into(), 0)));
before_stop(Some(&fake), Some(WipChoice::Push), T).await.unwrap();
assert_eq!(fake.calls(), vec![vec!["--reason".to_string(), "stop".to_string()]]);
}
#[tokio::test]
async fn a_failed_push_aborts_with_its_output() {
let fake = Fake::new(Ok(("triple-c-wip-push: api: push to origin failed: denied".into(), 1)));
let e = before_stop(Some(&fake), Some(WipChoice::Push), T).await.unwrap_err();
assert!(e.contains("push to origin failed: denied"), "{e}");
assert!(e.contains("not stopped"));
let gone = Fake::new(Err("tunnel closed".into()));
assert!(before_stop(Some(&gone), Some(WipChoice::Push), T).await.unwrap_err().contains("tunnel closed"));
assert!(before_stop::<Fake>(None, Some(WipChoice::Push), T).await.is_err());
}
#[tokio::test]
async fn discard_or_no_choice_runs_nothing() {
let fake = Fake::new(Err("must not run".into()));
before_stop(Some(&fake), Some(WipChoice::Discard), T).await.unwrap();
before_stop(Some(&fake), None, T).await.unwrap();
before_stop::<Fake>(None, None, T).await.unwrap();
assert!(fake.calls().is_empty());
}
#[test]
fn the_script_is_bounded_inside_the_workspace_before_the_client_gives_up() {
let cmd = wip_command(vec!["--reason".into(), "stop".into()], PUSH_TIMEOUT);
assert_eq!(cmd, vec!["/usr/bin/timeout", "-k", "5", "290", WIP_PUSH, "--reason", "stop"]);
let secs: u64 = cmd[3].parse().unwrap();
assert!(secs + 5 < PUSH_TIMEOUT.as_secs(), "TERM and KILL land before the client's timeout");
let dry = wip_command(vec!["--dry-run".into()], DRY_RUN_TIMEOUT);
assert_eq!(&dry[3..], &["20", WIP_PUSH, "--dry-run"]);
assert_eq!(wip_command(Vec::new(), Duration::from_secs(3))[3], "1", "never 0 (no limit)");
// The image has coreutils' timeout where the argv says.
let df = include_str!("../../../../container/Dockerfile");
assert!(df.contains("ubuntu:24.04") || df.contains("ubuntu:noble"), "coreutils at /usr/bin");
}
#[test]
fn a_cloning_repo_parses_with_its_flag_and_older_lines_without_it() {
let out = "{\"folder\":\"half\",\"dirty\":false,\"unpushed\":0,\"branch\":\"\",\"cloning\":true}\n\
{\"folder\":\"a\",\"dirty\":true,\"unpushed\":0,\"branch\":\"main\"}";
let repos = parse_dry_run(out).unwrap();
assert!(repos[0].cloning && !repos[1].cloning);
}
#[test]
fn long_output_keeps_its_end() {
let s = format!("{}END", "x".repeat(10_000));
let c = cap(&s);
assert!(c.ends_with("END") && c.len() < 4100);
}
#[tokio::test]
async fn a_successful_push_records_pushed_and_stop_without_pushing_records_discard() {
let fake = Fake::new(Ok(("pushed".into(), 0)));
assert_eq!(prepare_stop(Some(&fake), Some(WipChoice::Push), T).await.unwrap(), None);
assert_eq!(fake.markers(), vec![MARKER_PUSHED]);
let fake = Fake::new(Err("must not run".into()));
assert_eq!(prepare_stop(Some(&fake), Some(WipChoice::Discard), T).await.unwrap(), None);
assert!(fake.calls().is_empty());
assert_eq!(fake.markers(), vec![MARKER_DISCARD]);
assert_eq!(prepare_stop(Some(&fake), None, T).await.unwrap(), None);
assert_eq!(fake.markers().len(), 1, "no choice records nothing");
}
#[tokio::test]
async fn a_failed_push_records_nothing() {
let fake = Fake::new(Ok(("denied".into(), 1)));
assert!(prepare_stop(Some(&fake), Some(WipChoice::Push), T).await.is_err());
assert!(fake.markers().is_empty());
}
#[tokio::test]
async fn an_unwritable_marker_still_stops_and_says_the_hook_may_push() {
let mut fake = Fake::new(Ok((String::new(), 0)));
fake.marker_fails = true;
let note = prepare_stop(Some(&fake), Some(WipChoice::Discard), T).await.unwrap();
assert!(note.unwrap().contains("may still run"));
let note = prepare_stop::<Fake>(None, Some(WipChoice::Discard), T).await.unwrap();
assert!(note.is_some());
}
#[test]
fn the_marker_is_written_as_an_argument_to_the_file_the_hook_reads() {
let cmd = stop_reason_command("pushed");
assert_eq!(&cmd[..2], &["sh".to_string(), "-c".to_string()]);
assert!(cmd[2].contains("> /run/triple-c/stop-reason") && cmd[2].contains("\"$1\""));
assert_eq!(cmd.last().unwrap(), "pushed");
// The entrypoint matches exactly these values.
let ep = include_str!("../../../../container/entrypoint.sh");
assert!(ep.contains("pushed|discard)") && ep.contains(STOP_REASON_FILE));
}
#[tokio::test]
async fn a_failed_stop_forgets_the_recorded_choice() {
let fake = Fake::new(Ok((String::new(), 0)));
prepare_stop(Some(&fake), Some(WipChoice::Discard), T).await.unwrap();
forget_choice(Some(&fake), Some(WipChoice::Discard)).await;
assert_eq!(fake.markers(), vec![MARKER_DISCARD, "<cleared>"]);
// An unguarded stop recorded nothing, so there is nothing to forget.
let fake = Fake::new(Ok((String::new(), 0)));
forget_choice(Some(&fake), None).await;
assert!(fake.markers().is_empty());
forget_choice::<Fake>(None, Some(WipChoice::Push)).await;
assert_eq!(clear_stop_reason_command(), vec!["rm", "-f", "/run/triple-c/stop-reason"]);
}
}
@@ -160,7 +160,9 @@ pub async fn open_terminal_session(
.get(&project_id)
.ok_or_else(|| format!("Project {} not found", project_id))?;
let target = ContainerTarget::for_project(&project)
// Waits while an AWS project's tunnel is being re-opened after a restart.
let target = ContainerTarget::for_project_waiting(&project)
.await
.ok_or_else(|| "Container not running".to_string())?;
if let Some(id) = resume_session_id.as_deref() {
@@ -180,6 +182,10 @@ pub async fn open_terminal_session(
),
};
// AWS copy-at-connect: the credentials go into this terminal's own
// environment (never the agent's). Empty for everything else.
let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await;
let output_event = format!("terminal-output-{}", session_id);
let exit_event = format!("terminal-exit-{}", session_id);
let app_handle_output = app_handle.clone();
@@ -191,6 +197,7 @@ pub async fn open_terminal_session(
&target,
&session_id,
cmd,
env,
move |data| {
let _ = app_handle_output.emit(&output_event, data);
},
+143 -9
View File
@@ -32,6 +32,7 @@ use serde::Serialize;
use tauri::{AppHandle, Emitter};
use tokio::sync::watch;
use crate::aws::tasks::StopKind;
use crate::host::watch::ExitSource;
use crate::host::ContainerTarget;
use crate::models::{Project, ProjectStatus};
@@ -68,6 +69,33 @@ pub enum ExitReason {
pub struct ContainerExited<'a> {
pub project: &'a Project,
pub reason: ExitReason,
/// AWS: why ECS says the task stopped (P8). `None` on Docker, or when
/// the watch has no reason on record.
pub stop: Option<StopNote>,
}
/// Why an AWS task stopped, for the UI's toast.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StopNote {
/// `spot`, `idle`, `lifetime`, `failed` (the project goes to Error), `user` (someone
/// stopped it: no toast) or `exited` (on its own).
pub kind: &'static str,
/// ECS's stopped reason, or a fixed text.
pub message: Option<String>,
}
impl StopNote {
pub fn of(kind: &StopKind) -> StopNote {
let (kind, message) = match kind {
StopKind::Spot => ("spot", Some("Spot interruption".to_string())),
StopKind::Idle => ("idle", None),
StopKind::LifetimeCap => ("lifetime", None),
StopKind::FailedToStart(r) => ("failed", Some(r.clone())),
StopKind::UserInitiated => ("user", None),
StopKind::Exited(r) => ("exited", r.clone()),
};
StopNote { kind, message }
}
}
fn project_busy(project_id: &str) -> bool {
@@ -143,11 +171,11 @@ async fn unless_cancelled<T>(
/// (see [`STABLE_AFTER`]).
async fn watch_once(
store: &ProjectsStore,
source: &ExitSource,
cancel: &mut watch::Receiver<bool>,
sweep_reason: ExitReason,
on_change: &impl Fn(&Project, ExitReason),
) -> Option<bool> {
let source = ExitSource::Docker;
if !unless_cancelled(cancel, source.reachable()).await? {
return Some(false);
}
@@ -187,6 +215,7 @@ async fn watch_once(
/// test can drive it without a Tauri app.
pub(crate) async fn run(
store: Arc<ProjectsStore>,
source: ExitSource,
mut cancel: watch::Receiver<bool>,
on_change: impl Fn(&Project, ExitReason),
) {
@@ -197,7 +226,7 @@ pub(crate) async fn run(
let mut sweep_reason = ExitReason::FoundStopped;
let mut failures: u32 = 0;
loop {
let Some(healthy) = watch_once(&store, &mut cancel, sweep_reason, &on_change).await else {
let Some(healthy) = watch_once(&store, &source, &mut cancel, sweep_reason, &on_change).await else {
return;
};
sweep_reason = ExitReason::Exited;
@@ -209,21 +238,60 @@ pub(crate) async fn run(
}
}
/// Start the watcher for the life of the app.
/// Start a watcher on `source` for the life of the app. One per host kind:
/// Docker's event stream, and (Task 6) the AWS task poll.
pub fn spawn(
app: AppHandle,
store: Arc<ProjectsStore>,
source: ExitSource,
cancel: watch::Receiver<bool>,
) -> tauri::async_runtime::JoinHandle<()> {
tauri::async_runtime::spawn(async move {
run(store, cancel, |p, reason| {
let store_for_aws = store.clone();
run(store, source, cancel, move |p, reason| {
log::warn!(
"Project '{}' ({}): its container stopped without the app asking ({:?}) — marking Stopped",
p.name,
p.id,
reason
);
let _ = app.emit(CONTAINER_EXITED_EVENT, ContainerExited { project: p, reason });
// Why ECS stopped it, read before the tunnel (and with it the
// registry's record) goes.
let kind = p
.is_aws()
.then(|| p.container_id.as_deref().and_then(crate::host::tunnels::stop_kind))
.flatten();
let mut shown = p.clone();
// A task that never got going is an error, not a stop (P8).
if let Some(StopKind::FailedToStart(_)) = kind {
if store_for_aws.update_status(&p.id, ProjectStatus::Error).is_ok() {
shown.status = ProjectStatus::Error;
}
}
// An AWS task that stopped: its tunnel leads nowhere now, and its
// task ARN is history (the data's access point stays).
if p.is_aws() {
if let Some(a) = p.aws.as_ref() {
let _ = store_for_aws.set_aws_runtime(
&p.id,
None,
a.access_point_id.clone(),
a.task_definition_arn.clone(),
a.workspace_access_point_id.clone(),
);
}
let id = p.id.clone();
if let Some(arn) = p.aws.as_ref().and_then(|a| a.task_arn.clone()) {
tauri::async_runtime::spawn(async move {
crate::host::tunnels::close_if_task(&id, &arn).await
});
}
}
if let Some(store_p) = store_for_aws.get(&p.id) {
shown = store_p;
}
let stop = kind.as_ref().map(StopNote::of);
let _ = app.emit(CONTAINER_EXITED_EVENT, ContainerExited { project: &shown, reason, stop });
})
.await;
})
@@ -244,15 +312,39 @@ mod tests {
#[test]
fn the_event_says_why_so_the_ui_can_tell_news_from_housekeeping() {
let p = Project::new("demo".to_string(), Vec::new());
let v = serde_json::to_value(ContainerExited { project: &p, reason: ExitReason::Exited }).unwrap();
let v = serde_json::to_value(ContainerExited { project: &p, reason: ExitReason::Exited, stop: None })
.unwrap();
assert_eq!(v["reason"], "exited");
assert_eq!(v["project"]["name"], "demo");
let v = serde_json::to_value(ContainerExited { project: &p, reason: ExitReason::FoundStopped })
.unwrap();
assert!(v["stop"].is_null());
let v = serde_json::to_value(ContainerExited {
project: &p,
reason: ExitReason::FoundStopped,
stop: Some(StopNote::of(&StopKind::Spot)),
})
.unwrap();
assert_eq!(v["reason"], "found_stopped");
assert_eq!(v["stop"], serde_json::json!({ "kind": "spot", "message": "Spot interruption" }));
assert_eq!(CONTAINER_EXITED_EVENT, "project-container-exited");
}
#[test]
fn every_stop_kind_has_a_note_for_the_ui() {
let cases = [
(StopKind::Spot, "spot", Some("Spot interruption")),
(StopKind::Idle, "idle", None),
(StopKind::LifetimeCap, "lifetime", None),
(StopKind::FailedToStart("CannotPull".into()), "failed", Some("CannotPull")),
(StopKind::UserInitiated, "user", None),
(StopKind::Exited(Some("OOM".into())), "exited", Some("OOM")),
(StopKind::Exited(None), "exited", None),
];
for (k, kind, message) in cases {
let n = StopNote::of(&k);
assert_eq!((n.kind, n.message.as_deref()), (kind, message), "{k:?}");
}
}
/// The shutdown path: a host call that never answers (a wedged daemon)
/// must give way to cancellation at once, not hold teardown.
#[tokio::test]
@@ -270,6 +362,48 @@ mod tests {
assert_eq!(unless_cancelled(&mut rx, async { 1 }).await, None);
}
/// The AWS source drives the same loop: it is "reachable" without a
/// ping, polls its lookup every interval, and gives way to cancellation.
/// No project is Running, so nothing is confirmed (and nothing reaches
/// Docker or AWS).
#[tokio::test(start_paused = true)]
async fn an_aws_source_polls_its_lookup_until_cancelled() {
use crate::host::watch::{AwsTaskLookup, AwsWatch, TaskGroup, WatchedTask};
use futures_util::future::BoxFuture;
use std::sync::atomic::{AtomicUsize, Ordering};
struct Lookup(AtomicUsize);
impl AwsTaskLookup for Lookup {
fn running_tasks(&self) -> Vec<WatchedTask> {
self.0.fetch_add(1, Ordering::SeqCst);
Vec::new()
}
fn stopped<'a>(
&'a self,
_: &'a TaskGroup,
_: &'a [String],
) -> BoxFuture<'a, Result<Vec<String>, String>> {
Box::pin(async { Ok(Vec::new()) })
}
}
let dir = std::env::temp_dir().join(format!("triple-c-watch-aws-{}", uuid::Uuid::new_v4().simple()));
std::fs::create_dir_all(&dir).unwrap();
let store = Arc::new(ProjectsStore::over_file_for_test(dir.join("projects.json"), Vec::new()));
let lookup = Arc::new(Lookup(AtomicUsize::new(0)));
let source = ExitSource::Aws(AwsWatch::every(lookup.clone(), Duration::from_secs(15)));
let (cancel_tx, cancel_rx) = watch::channel(false);
let watcher = tokio::spawn(run(store, source, cancel_rx, |_, _| panic!("nothing stopped")));
tokio::time::sleep(Duration::from_secs(46)).await;
assert_eq!(lookup.0.load(Ordering::SeqCst), 3);
let _ = cancel_tx.send(true);
tokio::time::timeout(Duration::from_secs(1), watcher)
.await
.expect("stops at once")
.unwrap();
std::fs::remove_dir_all(&dir).ok();
}
/// End to end against a real daemon: a Running project whose container is
/// killed from outside the app is flipped to Stopped and reported as a live
/// exit; one found already stopped on the first look is reported as such;
@@ -302,7 +436,7 @@ mod tests {
let (cancel_tx, cancel_rx) = watch::channel(false);
let (seen_tx, mut seen_rx) = tokio::sync::mpsc::unbounded_channel::<(String, ExitReason)>();
let watcher = tokio::spawn(run(store.clone(), cancel_rx, move |p, why| {
let watcher = tokio::spawn(run(store.clone(), ExitSource::Docker, cancel_rx, move |p, why| {
let _ = seen_tx.send((p.id.clone(), why));
}));
let first = tokio::time::timeout(Duration::from_secs(10), seen_rx.recv()).await;
File diff suppressed because it is too large. Load diff
+2 -1
View File
@@ -108,7 +108,8 @@ fn sha256_hex(input: &str) -> String {
/// loops that walk the store, and leading dots are stripped so a hidden file
/// does not stay hidden.
///
/// `entrypoint.sh` reimplements exactly this in a few lines of shell (it has to
/// `entrypoint-lib.sh` (`ca_normalise_name`, sourced by `entrypoint.sh`)
/// reimplements exactly this in a few lines of shell (it has to
/// rename the files inside the container); the two must agree, which is what
/// the unit tests below pin down.
pub fn container_cert_name(file_name: &str) -> String {
+99 -329
View File
@@ -9,6 +9,7 @@ use sha2::{Sha256, Digest};
use super::ca_certs;
use super::client::get_docker;
use super::workspace_env::{workspace_env, WorkspaceEnvInputs};
use crate::models::AppSettings;
use crate::models::{Backend, BedrockAuthMethod, ClaudeCodeSettings, ContainerInfo, EnvVar, GlobalAwsSettings, GlobalLlamaCppSettings, GlobalOllamaSettings, GlobalOpenAiCompatibleSettings, PortMapping, Project, ProjectPath};
@@ -139,7 +140,7 @@ The user can click a file path in this terminal to open it in a viewer (while a
/// instructions, appending port mapping docs, and appending scheduler docs.
/// Used by both create_container() and container_needs_recreation() to ensure
/// the same value is produced in both paths.
fn build_claude_instructions(
pub(super) fn build_claude_instructions(
global_instructions: Option<&str>,
project_instructions: Option<&str>,
port_mappings: &[PortMapping],
@@ -244,7 +245,11 @@ pub(crate) const LABEL_SCRUB: &str = "triple-c.scrub";
/// cannot travel onto a snapshot. See the `LABEL` block in the Dockerfile.
pub(crate) const LABEL_BASE: &str = "triple-c.base";
const RESERVED_ENV_PREFIXES: &[&str] = &["ANTHROPIC_", "AWS_", "GIT_", "HOST_", "TRIPLE_C_"];
/// `TRIPLEC_` is the AWS workspace's own namespace (`TRIPLEC_HOST`,
/// `TRIPLEC_FILES`, `TRIPLEC_AGENT_TOKEN`, … — see `container/entrypoint.sh`):
/// a custom var there could switch a local container into AWS mode or forge
/// the inputs the entrypoint trusts.
const RESERVED_ENV_PREFIXES: &[&str] = &["ANTHROPIC_", "AWS_", "GIT_", "HOST_", "TRIPLE_C_", "TRIPLEC_"];
/// Exact env var names Triple-C manages itself. Not covered by
/// [`RESERVED_ENV_PREFIXES`] because they don't share those prefixes.
@@ -356,7 +361,7 @@ fn model_alias_fingerprint_part(
}
/// Whether `key` is an env var name Triple-C reserves for itself.
fn is_reserved_env_key(key: &str) -> bool {
pub(super) fn is_reserved_env_key(key: &str) -> bool {
let upper = key.to_uppercase();
RESERVED_ENV_PREFIXES.iter().any(|p| upper.starts_with(p))
|| RESERVED_ENV_EXACT.iter().any(|e| upper == *e)
@@ -375,7 +380,7 @@ fn is_reserved_env_key(key: &str) -> bool {
///
/// Empty stays empty rather than becoming the hash of the empty string: an empty
/// label is how every other `triple-c.*` key says "nothing configured".
fn compute_env_fingerprint(custom_env_vars: &[EnvVar]) -> String {
pub(super) fn compute_env_fingerprint(custom_env_vars: &[EnvVar]) -> String {
let mut parts: Vec<String> = Vec::new();
for env_var in custom_env_vars {
let key = env_var.key.trim();
@@ -401,7 +406,7 @@ fn compute_env_fingerprint(custom_env_vars: &[EnvVar]) -> String {
///
/// The returned token is never logged and never leaves this module except as
/// the env var value handed to Docker.
fn shared_claude_auth(project: &Project) -> Option<(String, String)> {
pub(super) fn shared_claude_auth(project: &Project) -> Option<(String, String)> {
if project.backend != Backend::Anthropic || !project.use_shared_auth_token {
return None;
}
@@ -432,7 +437,7 @@ fn claude_token_label(project: &Project) -> String {
/// Merge global and per-project custom environment variables.
/// Per-project variables override global variables with the same key.
fn merge_custom_env_vars(global: &[EnvVar], project: &[EnvVar]) -> Vec<EnvVar> {
pub(super) fn merge_custom_env_vars(global: &[EnvVar], project: &[EnvVar]) -> Vec<EnvVar> {
let mut merged: std::collections::HashMap<String, EnvVar> = std::collections::HashMap::new();
for ev in global {
let key = ev.key.trim().to_string();
@@ -494,7 +499,7 @@ fn sha256_hex(input: &str) -> String {
/// Resolve a per-project string value with a global fallback. Returns `None`
/// when both are blank, otherwise the per-project value if set, else the global.
fn resolve_with_global<'a>(per_project: Option<&'a str>, global: Option<&'a str>) -> Option<&'a str> {
pub(super) fn resolve_with_global<'a>(per_project: Option<&'a str>, global: Option<&'a str>) -> Option<&'a str> {
let project_val = per_project.map(str::trim).filter(|s| !s.is_empty());
if project_val.is_some() {
return project_val;
@@ -659,7 +664,7 @@ fn compute_ports_fingerprint(port_mappings: &[PortMapping]) -> String {
/// to the global set and never turn a globally-enabled setting off. `None` at
/// project level means "inherit", which is now a state the project can
/// actually be in rather than the only state an off switch could produce.
fn merge_claude_code_settings(
pub(super) fn merge_claude_code_settings(
global: Option<&ClaudeCodeSettings>,
project: Option<&ClaudeCodeSettings>,
) -> Option<ClaudeCodeSettings> {
@@ -759,7 +764,7 @@ fn compute_claude_code_settings_fingerprint(
///
/// The other two are documented as "set to `1` to …" with no meaning attached
/// to `0`, so `0` is unambiguously neutral and is stated outright.
fn claude_code_env_vars(settings: Option<&ClaudeCodeSettings>) -> Vec<String> {
pub(super) fn claude_code_env_vars(settings: Option<&ClaudeCodeSettings>) -> Vec<String> {
let owned;
let s = match settings {
Some(s) => s,
@@ -822,7 +827,7 @@ fn claude_code_env_vars(settings: Option<&ClaudeCodeSettings>) -> Vec<String> {
///
/// Returns a `String` rather than an `Option<String>`: there is no longer any
/// input for which this produces nothing to say.
fn build_claude_code_settings_json(
pub(super) fn build_claude_code_settings_json(
settings: Option<&ClaudeCodeSettings>,
sandbox_enabled: bool,
) -> String {
@@ -1080,7 +1085,7 @@ type VpnHostConfigParts = (
/// `MISSION_CONTROL_ENABLED` twenty lines above shows the failure this avoids —
/// it is pushed only when true, so a snapshot's baked `=1` survives the toggle
/// going off.
fn vpn_env_var(enabled: bool) -> String {
pub(super) fn vpn_env_var(enabled: bool) -> String {
format!("VPN_SUPPORT_ENABLED={}", u8::from(enabled))
}
@@ -1107,6 +1112,37 @@ fn vpn_host_config(enabled: bool) -> VpnHostConfigParts {
)
}
/// What a VPN client needs, as plain data, for hosts that aren't Docker (an
/// AWS EC2 task definition): the capabilities, the devices as
/// `(host path, container path, cgroup permissions)`, and the sysctls,
/// sorted. Derived from [`vpn_host_config`] so the two can't drift.
pub(crate) struct VpnRequirements {
pub capabilities: Vec<String>,
pub devices: Vec<(String, String, String)>,
pub sysctls: Vec<(String, String)>,
}
pub(crate) fn vpn_requirements() -> VpnRequirements {
let (caps, devices, sysctls) = vpn_host_config(true);
let mut sysctls: Vec<(String, String)> = sysctls.unwrap_or_default().into_iter().collect();
sysctls.sort();
VpnRequirements {
capabilities: caps.unwrap_or_default(),
devices: devices
.unwrap_or_default()
.into_iter()
.map(|d| {
(
d.path_on_host.unwrap_or_default(),
d.path_in_container.unwrap_or_default(),
d.cgroup_permissions.unwrap_or_default(),
)
})
.collect(),
sysctls,
}
}
/// Turn the daemon's device-passthrough failure into an explanation.
///
/// **This fires on `start`, not `create`.** Verified against Docker 29.7:
@@ -1266,212 +1302,24 @@ pub async fn create_container(
log::debug!("Skipping HOST_UID/HOST_GID on Windows — Docker Desktop's Linux VM handles user mapping");
}
if let Some(ref token) = project.git_token {
env_vars.push(format!("GIT_TOKEN={}", token));
}
// Per-project git user overrides global defaults
let effective_git_name = project.git_user_name.as_deref().or(default_git_user_name);
let effective_git_email = project.git_user_email.as_deref().or(default_git_user_email);
if let Some(name) = effective_git_name {
env_vars.push(format!("GIT_USER_NAME={}", name));
}
if let Some(email) = effective_git_email {
env_vars.push(format!("GIT_USER_EMAIL={}", email));
}
// Bedrock configuration
if project.backend == Backend::Bedrock {
if let Some(ref bedrock) = project.bedrock_config {
env_vars.push("CLAUDE_CODE_USE_BEDROCK=1".to_string());
// AWS region: per-project overrides global
let region = if !bedrock.aws_region.is_empty() {
Some(bedrock.aws_region.clone())
} else {
global_aws.aws_region.clone()
};
if let Some(ref r) = region {
env_vars.push(format!("AWS_REGION={}", r));
}
match bedrock.auth_method {
BedrockAuthMethod::StaticCredentials => {
// Static/session credentials are NOT injected as env vars.
// They are written to ~/.aws/credentials by
// sync_bedrock_credentials() on every container
// start, so rotated/updated keys are picked up without a
// full container recreation (and never get baked into the
// snapshot image). The empty values set by the
// MANAGED_AUTH_KEYS neutralization pass below are ignored by
// the AWS SDK, which falls through to the credentials file.
}
BedrockAuthMethod::Profile => {
// Per-project profile overrides global
let profile = bedrock.aws_profile.as_ref()
.or(global_aws.aws_profile.as_ref());
if let Some(p) = profile {
env_vars.push(format!("AWS_PROFILE={}", p));
}
env_vars.push("AWS_SSO_AUTH_REFRESH_CMD=triple-c-sso-refresh".to_string());
}
BedrockAuthMethod::BearerToken => {
if let Some(ref token) = bedrock.aws_bearer_token {
env_vars.push(format!("AWS_BEARER_TOKEN_BEDROCK={}", token));
}
}
}
if let Some(model) = resolve_with_global(
bedrock.model_id.as_deref(),
global_aws.default_model_id.as_deref(),
) {
env_vars.push(format!("ANTHROPIC_MODEL={}", model));
}
if bedrock.disable_prompt_caching {
env_vars.push("DISABLE_PROMPT_CACHING=1".to_string());
}
if let Some(ref tier) = bedrock.service_tier {
let trimmed = tier.trim();
if !trimmed.is_empty() {
env_vars.push(format!("ANTHROPIC_BEDROCK_SERVICE_TIER={}", trimmed));
}
}
}
}
// ── Custom-endpoint backends ─────────────────────────────────────────────
// Ollama, llama.cpp and the OpenAI-Compatible gateway all point Claude Code
// at a non-Anthropic server via ANTHROPIC_BASE_URL. Each resolves its model
// id here; the model-alias vars are emitted once below, from
// `alias_model` / `alias_haiku`, so the three backends cannot drift apart.
let mut alias_model: Option<String> = None;
let mut alias_haiku: Option<String> = None;
// Ollama configuration
if project.backend == Backend::Ollama {
if let Some(ref ollama) = project.ollama_config {
if let Some(url) = resolve_with_global(
Some(&ollama.base_url),
global_ollama.base_url.as_deref(),
) {
env_vars.push(format!("ANTHROPIC_BASE_URL={}", url));
}
env_vars.push("ANTHROPIC_AUTH_TOKEN=ollama".to_string());
if let Some(model) = resolve_with_global(
ollama.model_id.as_deref(),
global_ollama.default_model_id.as_deref(),
) {
env_vars.push(format!("ANTHROPIC_MODEL={}", model));
alias_model = Some(model.to_string());
}
alias_haiku = resolve_with_global(
ollama.haiku_model_id.as_deref(),
global_ollama.default_haiku_model_id.as_deref(),
)
.map(str::to_string);
}
}
// llama.cpp (llama-server) configuration
if project.backend == Backend::LlamaCpp {
if let Some(ref cfg) = project.llamacpp_config {
if let Some(url) = resolve_with_global(
Some(&cfg.base_url),
global_llamacpp.base_url.as_deref(),
) {
env_vars.push(format!("ANTHROPIC_BASE_URL={}", url));
}
// llama-server only enforces an Authorization header when it was
// started with `--api-key` (default: none), so the value here is
// ignored in the common case. Claude Code still refuses to run
// against a custom base URL with no credential at all, so a
// placeholder is always sent — same trick as the Ollama branch
// above, which sends the literal "ollama".
env_vars.push("ANTHROPIC_AUTH_TOKEN=llama.cpp".to_string());
if let Some(model) = resolve_with_global(
cfg.model_id.as_deref(),
global_llamacpp.default_model_id.as_deref(),
) {
env_vars.push(format!("ANTHROPIC_MODEL={}", model));
alias_model = Some(model.to_string());
}
alias_haiku = resolve_with_global(
cfg.haiku_model_id.as_deref(),
global_llamacpp.default_haiku_model_id.as_deref(),
)
.map(str::to_string);
}
}
// OpenAI Compatible configuration
if project.backend == Backend::OpenAiCompatible {
if let Some(ref config) = project.openai_compatible_config {
if let Some(url) = resolve_with_global(
Some(&config.base_url),
global_openai_compatible.base_url.as_deref(),
) {
env_vars.push(format!("ANTHROPIC_BASE_URL={}", url));
}
if let Some(ref key) = config.api_key {
env_vars.push(format!("ANTHROPIC_AUTH_TOKEN={}", key));
}
if let Some(model) = resolve_with_global(
config.model_id.as_deref(),
global_openai_compatible.default_model_id.as_deref(),
) {
env_vars.push(format!("ANTHROPIC_MODEL={}", model));
alias_model = Some(model.to_string());
}
alias_haiku = resolve_with_global(
config.haiku_model_id.as_deref(),
global_openai_compatible.default_haiku_model_id.as_deref(),
)
.map(str::to_string);
}
}
// Model aliases — the fix for background Claude Code calls against a local
// server. Only for backends that talk to a custom endpoint: Anthropic and
// Bedrock reach servers that really do host the Anthropic model ids, so
// they keep Claude Code's own defaults. Anything not emitted here is
// blanked by the MANAGED_AUTH_KEYS pass below, so switching *away* from a
// custom endpoint clears the aliases out of the snapshot image too.
if project.backend.uses_custom_endpoint() {
for (key, value) in
compute_model_aliases(alias_model.as_deref(), alias_haiku.as_deref())
{
env_vars.push(format!("{}={}", key, value));
}
}
// Shared Claude Code OAuth token (Anthropic backend only, opt-out per
// project). Injected *before* the neutralization pass below so that pass
// sees it as already-set; when it is absent the pass actively blanks the
// variable instead of leaving a stale one baked into the snapshot image.
// project). Read here — it lives in the keychain — and handed to
// `workspace_env`, which injects it before the neutralization pass.
let shared_claude = shared_claude_auth(project);
if let Some((ref token, _)) = shared_claude {
env_vars.push(format!("{}={}", CLAUDE_OAUTH_TOKEN_ENV, token));
if shared_claude.is_some() {
log::info!(
"Injecting the shared Claude authentication token into the container for project {}",
project.id
);
}
// ── Corporate CA certificates ───────────────────────────────────────────
// Resolved here (rather than down with the mounts) so the env vars land
// *before* the neutralization pass below and are seen as already-set.
// Corporate CA certificates, resolved here (not in `workspace_env`) because
// the mount below needs the same resolution.
//
// A bad path is a hard error, not a warning: behind a TLS-terminating
// proxy a container without the CA fails every HTTPS call — npm, pip, git,
// and Claude Code's own API requests — each in its own confusing way. One
// message naming the path is far kinder.
//
// The values are set here rather than exported by the entrypoint because a
// terminal session is a `docker exec`, which sees the container's
// configured env and nothing the entrypoint exported. Same lesson as
// `$BROWSER` and the URL relay shim.
let effective_ca_path =
resolve_with_global(project.ca_cert_path.as_deref(), default_ca_cert_path);
let resolved_ca = ca_certs::resolve(effective_ca_path)?;
@@ -1483,110 +1331,40 @@ pub async fn create_container(
project.id
);
}
for (key, value) in ca_certs::ca_env_vars(resolved_ca.as_ref()) {
env_vars.push(format!("{}={}", key, value));
}
// ── Neutralize stale backend auth env vars ──────────────────────────────
// When a project switches backends (e.g. Bedrock → Anthropic) the container
// is recreated *from a snapshot image* committed off the previous container,
// and `docker commit` copies that container's full ENV into the image. So
// any auth var set under the old backend (e.g. CLAUDE_CODE_USE_BEDROCK=1,
// AWS_PROFILE, a model alias) survives in the image ENV and stays active
// unless we explicitly override it at create time.
//
// This pass is about *staleness*, not secrecy. It fixes the container it is
// building and does nothing to the image, so it is not — and never was —
// a defence against a credential baked into a snapshot. That is
// `commit_container_snapshot`'s job, via SECRET_ENV_KEYS.
// Create-time env takes precedence over image ENV, so we set every managed
// auth key the *current* backend did NOT set to an empty value, clearing the
// stale baked-in one.
const MANAGED_AUTH_KEYS: &[&str] = &[
"CLAUDE_CODE_USE_BEDROCK",
"AWS_REGION",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_PROFILE",
"AWS_BEARER_TOKEN_BEDROCK",
"AWS_SSO_AUTH_REFRESH_CMD",
"ANTHROPIC_BASE_URL",
"ANTHROPIC_AUTH_TOKEN",
"ANTHROPIC_MODEL",
"DISABLE_PROMPT_CACHING",
"ANTHROPIC_BEDROCK_SERVICE_TIER",
// Switching from a custom-endpoint backend to Anthropic or Bedrock must
// *clear* the aliases, not merely stop setting them: a stale
// ANTHROPIC_DEFAULT_HAIKU_MODEL baked into the snapshot image would
// keep pointing background calls at a model id the new backend has
// never heard of.
ANTHROPIC_DEFAULT_OPUS_MODEL,
ANTHROPIC_DEFAULT_SONNET_MODEL,
ANTHROPIC_DEFAULT_HAIKU_MODEL,
ANTHROPIC_DEFAULT_FABLE_MODEL,
// Revoking the shared token, opting a project out, or switching away
// from the Anthropic backend must *clear* this, not merely stop setting
// it — otherwise the value committed into the snapshot image keeps
// authenticating the container with a credential the user removed.
CLAUDE_OAUTH_TOKEN_ENV,
];
// Same reasoning for the CA vars — `ca_env_vars` already emits them empty
// when no CA is configured, so this list is belt-and-braces for a snapshot
// committed by a build that predates the feature.
let managed_keys: Vec<&str> = MANAGED_AUTH_KEYS
.iter()
.copied()
.chain(ca_certs::CA_ENV_KEYS.iter().copied())
.collect();
let already_set: std::collections::HashSet<String> = env_vars
.iter()
.filter_map(|e| e.split('=').next().map(|k| k.to_string()))
.collect();
for key in &managed_keys {
if !already_set.contains(*key) {
env_vars.push(format!("{}=", key));
}
}
// Everything project-shaped (backend auth, CA vars, custom env, instructions,
// Claude Code settings, ...) comes from the one host-neutral builder, so the
// AWS host can put the same pairs in its secret.
env_vars.extend(
workspace_env(
project,
&WorkspaceEnvInputs {
global_aws,
global_ollama,
global_llamacpp,
global_openai_compatible,
global_claude_instructions,
global_custom_env_vars,
timezone,
global_claude_code_settings,
default_git_user_name,
default_git_user_email,
resolved_ca: resolved_ca.as_ref(),
shared_claude_token: shared_claude.as_ref().map(|(token, _)| token.as_str()),
},
)
.into_iter()
.map(|(key, value)| format!("{}={}", key, value)),
);
// Custom environment variables (global + per-project, project overrides global for same key)
let merged_env = merge_custom_env_vars(global_custom_env_vars, &project.custom_env_vars);
for env_var in &merged_env {
let key = env_var.key.trim();
if key.is_empty() {
continue;
}
if is_reserved_env_key(key) {
log::warn!("Skipping reserved env var: {}", key);
continue;
}
env_vars.push(format!("{}={}", key, env_var.value));
}
let custom_env_fingerprint = compute_env_fingerprint(&merged_env);
env_vars.push(format!("TRIPLE_C_CUSTOM_ENV={}", custom_env_fingerprint));
// Container timezone
if let Some(tz) = timezone {
if !tz.is_empty() {
env_vars.push(format!("TZ={}", tz));
}
}
// Mission Control env var
if project.mission_control_enabled {
env_vars.push("MISSION_CONTROL_ENABLED=1".to_string());
}
env_vars.push(vpn_env_var(project.vpn_support_enabled));
// Permission mode — read by triple-c-task-runner for scheduled (headless)
// Claude Code runs. Interactive terminals get the flags directly instead.
env_vars.push(format!(
"TRIPLE_C_PERMISSION_MODE={}",
project.effective_permission_mode().as_env_value()
// The labels below fingerprint the inputs that went into the env; derive
// them with the same helpers `workspace_env` used, so both always agree.
let effective_git_name = project.git_user_name.as_deref().or(default_git_user_name);
let effective_git_email = project.git_user_email.as_deref().or(default_git_user_email);
let custom_env_fingerprint = compute_env_fingerprint(&merge_custom_env_vars(
global_custom_env_vars,
&project.custom_env_vars,
));
// Claude instructions (global + per-project, plus port mapping info + scheduler docs)
let combined_instructions = build_claude_instructions(
global_claude_instructions,
project.claude_instructions.as_deref(),
@@ -1594,34 +1372,10 @@ pub async fn create_container(
project.mission_control_enabled,
project.sandbox_mode_enabled,
);
if let Some(ref instructions) = combined_instructions {
env_vars.push(format!("CLAUDE_INSTRUCTIONS={}", instructions));
}
// Claude Code settings (global + per-project merged)
let merged_cc_settings = merge_claude_code_settings(
global_claude_code_settings,
project.claude_code_settings.as_ref(),
);
// Env-var-based settings, read directly by Claude Code. Extracted and unit
// tested for the `vpn_host_config` reason: a container is created once, by
// a very long function, and a variable emitted with the wrong value here is
// invisible until someone wonders why a switch does nothing.
env_vars.extend(claude_code_env_vars(merged_cc_settings.as_ref()));
// settings.json-based settings (applied by the entrypoint). Always emitted,
// even with no `ClaudeCodeSettings` struct present: the payload asserts the
// *whole* managed key set, so "no settings" still has to be stated over a
// settings.json left behind on the config volume by a previous config.
// Split so the payload is safe under an older entrypoint that has never
// heard of the null-means-delete convention — see
// `split_claude_code_settings_payload`.
let (cc_settings_set, cc_settings_clear) = split_claude_code_settings_payload(
&build_claude_code_settings_json(merged_cc_settings.as_ref(), project.sandbox_mode_enabled),
);
env_vars.push(format!("CLAUDE_CODE_SETTINGS_JSON={}", cc_settings_set));
env_vars.push(format!("CLAUDE_CODE_SETTINGS_CLEAR={}", cc_settings_clear));
let mut mounts: Vec<Mount> = Vec::new();
@@ -4377,6 +4131,22 @@ mod tests {
assert_eq!(vpn_env_var(false), "VPN_SUPPORT_ENABLED=0");
}
#[test]
fn the_aws_workspace_namespace_is_reserved_from_custom_env() {
for key in ["TRIPLEC_HOST", "TRIPLEC_FILES", "TRIPLEC_AGENT_TOKEN", "triplec_repos", "TRIPLEC_ANYTHING"] {
assert!(is_reserved_env_key(key), "{key}");
}
assert_eq!(
compute_env_fingerprint(&[EnvVar {
key: "TRIPLEC_HOST".to_string(),
value: "aws".to_string(),
}]),
"",
"a reserved key never reaches the container"
);
assert!(!is_reserved_env_key("TRIPLECX"));
}
#[test]
fn the_vpn_skill_flag_is_reserved_from_custom_env() {
// entrypoint.sh installs and removes the pia-vpn skill from this
+37 -1
View File
@@ -58,10 +58,30 @@ const GATEWAY_LITELLM_VERSION: &str = "v1.106.6";
const GATEWAY_REGISTRY_DIGEST: &str =
"sha256:37e2dafb088a7723beb998590682957386dfc83ffea1848486f8e0e5cfac0b06";
fn registry_image_ref() -> String {
/// The digest-pinned registry image. The AWS gateway sidecar runs it too
/// (`aws::workspace`); the index covers linux/amd64 and linux/arm64.
pub(crate) fn registry_image_ref() -> String {
format!("{}@{}", GATEWAY_REGISTRY_REPO, GATEWAY_REGISTRY_DIGEST)
}
/// The environment variables the AWS gateway sidecar gets its two keys in
/// (from the project secret). [`sidecar_config`] names them with LiteLLM's
/// `os.environ/<NAME>` references, so the config itself holds no key.
pub(crate) const SIDECAR_API_KEY_ENV: &str = "TRIPLEC_GATEWAY_API_KEY";
pub(crate) const SIDECAR_MASTER_KEY_ENV: &str = "TRIPLEC_GATEWAY_MASTER_KEY";
/// The LiteLLM config for the AWS gateway sidecar: [`render_config`]'s, with
/// the provider key and master key as `os.environ/…` references (LiteLLM
/// resolves both: `litellm_params.api_key` and `general_settings.master_key`).
/// Holds no secret, so it can be sized and shown in errors.
pub(crate) fn sidecar_config(settings: &GatewaySettings) -> String {
render_config(
settings,
&format!("os.environ/{SIDECAR_API_KEY_ENV}"),
&format!("os.environ/{SIDECAR_MASTER_KEY_ENV}"),
)
}
/// Local builds are tagged with the pin plus a hash of the embedded build
/// context (`triple-c-gateway:v1.106.6-1a2b3c4d`), never a floating tag. A
/// build made by an app version with a different pin *or* a different
@@ -1175,6 +1195,22 @@ mod tests {
assert!(!render_config(&s, "k", "m").contains("api_base"));
}
#[test]
fn sidecar_config_references_the_keys_and_holds_none() {
let yaml = sidecar_config(&settings());
assert!(yaml.contains("api_key: \"os.environ/TRIPLEC_GATEWAY_API_KEY\""));
assert!(yaml.contains("master_key: \"os.environ/TRIPLEC_GATEWAY_MASTER_KEY\""));
// Otherwise exactly what Docker's gateway gets.
assert_eq!(
yaml,
render_config(
&settings(),
"os.environ/TRIPLEC_GATEWAY_API_KEY",
"os.environ/TRIPLEC_GATEWAY_MASTER_KEY"
)
);
}
#[test]
fn yaml_str_escapes_injection_attempts() {
let hostile = "a\"\nmaster_key: \"pwned";
+24 -6
View File
@@ -10,6 +10,10 @@ use crate::models::container_config;
const DOCKERFILE: &str = include_str!("../../../../container/Dockerfile");
const ENTRYPOINT: &str = include_str!("../../../../container/entrypoint.sh");
/// Sourced by the entrypoint and by `triple-c-apply-connect`; holds the
/// `sync_host_ssh` block the tests below run.
const ENTRYPOINT_LIB: &str = include_str!("../../../../container/entrypoint-lib.sh");
const APPLY_CONNECT: &str = include_str!("../../../../container/triple-c-apply-connect");
const SCHEDULER: &str = include_str!("../../../../container/triple-c-scheduler");
const TASK_RUNNER: &str = include_str!("../../../../container/triple-c-task-runner");
const OSC52_CLIPBOARD: &str = include_str!("../../../../container/osc52-clipboard");
@@ -17,6 +21,9 @@ const AUDIO_SHIM: &str = include_str!("../../../../container/audio-shim");
const SSO_REFRESH: &str = include_str!("../../../../container/triple-c-sso-refresh");
const TRIPLE_C_OPEN: &str = include_str!("../../../../container/triple-c-open");
const PLAYWRIGHT_HEAL: &str = include_str!("../../../../container/triple-c-playwright-heal");
const WIP_PUSH: &str = include_str!("../../../../container/triple-c-wip-push");
const IDLE_WATCH: &str = include_str!("../../../../container/triple-c-idle-watch");
const WORKSPACE_STATUS: &str = include_str!("../../../../container/triple-c-workspace-status");
static MISSION_CONTROL_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../../container/mission-control");
static SKILLS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../../container/skills");
@@ -238,6 +245,8 @@ fn create_build_context() -> Result<Vec<u8>, std::io::Error> {
append_file_to_archive(&mut archive, "Dockerfile", DOCKERFILE.as_bytes(), 0o644)?;
append_file_to_archive(&mut archive, "entrypoint.sh", ENTRYPOINT.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "entrypoint-lib.sh", ENTRYPOINT_LIB.as_bytes(), 0o644)?;
append_file_to_archive(&mut archive, "triple-c-apply-connect", APPLY_CONNECT.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-scheduler", SCHEDULER.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-task-runner", TASK_RUNNER.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "osc52-clipboard", OSC52_CLIPBOARD.as_bytes(), 0o755)?;
@@ -245,6 +254,14 @@ fn create_build_context() -> Result<Vec<u8>, std::io::Error> {
append_file_to_archive(&mut archive, "triple-c-sso-refresh", SSO_REFRESH.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-open", TRIPLE_C_OPEN.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-playwright-heal", PLAYWRIGHT_HEAL.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-wip-push", WIP_PUSH.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-idle-watch", IDLE_WATCH.as_bytes(), 0o755)?;
append_file_to_archive(
&mut archive,
"triple-c-workspace-status",
WORKSPACE_STATUS.as_bytes(),
0o755,
)?;
append_embedded_dir(&mut archive, &MISSION_CONTROL_DIR, "mission-control")?;
append_embedded_dir(&mut archive, &SKILLS_DIR, "skills")?;
@@ -301,17 +318,18 @@ mod build_context_tests {
#[cfg(all(test, unix))]
mod tests {
use super::ENTRYPOINT;
use super::ENTRYPOINT_LIB;
use std::fs;
use std::path::Path;
use std::process::Command;
/// The `sync_host_ssh` function from `entrypoint.sh`, cut out between its
/// markers so it can be run against temp dirs instead of the real ~/.ssh.
/// The `sync_host_ssh` function from `entrypoint-lib.sh` (which
/// `entrypoint.sh` sources), cut out between its markers so it can be run
/// against temp dirs instead of the real ~/.ssh.
fn sync_fn() -> &'static str {
let start = ENTRYPOINT.find("# >>> sync_host_ssh").expect("start marker");
let end = ENTRYPOINT.find("# <<< sync_host_ssh").expect("end marker");
&ENTRYPOINT[start..end]
let start = ENTRYPOINT_LIB.find("# >>> sync_host_ssh").expect("start marker");
let end = ENTRYPOINT_LIB.find("# <<< sync_host_ssh").expect("end marker");
&ENTRYPOINT_LIB[start..end]
}
fn sync(src: &Path, dest: &Path) -> String {
+3
View File
@@ -2,12 +2,15 @@ pub mod ca_certs;
pub mod client;
pub mod events;
pub mod container;
pub mod workspace_env;
pub mod image;
pub mod exec;
pub mod gateway;
pub mod legacy_cleanup;
pub mod migration;
pub mod stt;
#[cfg(all(test, unix))]
mod aws_mode_tests;
/// The container user `claude`'s home directory, as the image creates it.
pub(crate) const CONTAINER_HOME: &str = "/home/claude";
+726
View File
@@ -0,0 +1,726 @@
//! The container environment a project gets, built once for every host.
//!
//! Docker passes these pairs as the container's `Env`; the AWS host stores the
//! very same pairs in a per-project Secrets Manager secret. Both therefore call
//! [`workspace_env`], so a backend, a CA bundle or a custom variable cannot
//! reach one host and not the other.
//!
//! What is *not* here: `COLORTERM`, `HOST_UID` and `HOST_GID` (host-specific,
//! assembled by `create_container`), and anything that needs the host — the
//! corporate CA is resolved from disk and the shared Claude token read from
//! the keychain by the caller, then handed in through [`WorkspaceEnvInputs`].
//! Order is part of the contract: it is the order `docker inspect` shows.
use super::ca_certs::{self, ResolvedCa};
use super::container::{
build_claude_code_settings_json, build_claude_instructions, claude_code_env_vars,
compute_env_fingerprint, compute_model_aliases, is_reserved_env_key, merge_claude_code_settings,
merge_custom_env_vars, resolve_with_global, split_claude_code_settings_payload, vpn_env_var,
ANTHROPIC_DEFAULT_FABLE_MODEL, ANTHROPIC_DEFAULT_HAIKU_MODEL, ANTHROPIC_DEFAULT_OPUS_MODEL,
ANTHROPIC_DEFAULT_SONNET_MODEL, CLAUDE_OAUTH_TOKEN_ENV,
};
use crate::models::{
Backend, BedrockAuthMethod, ClaudeCodeSettings, EnvVar, GlobalAwsSettings,
GlobalLlamaCppSettings, GlobalOllamaSettings, GlobalOpenAiCompatibleSettings, Project,
};
/// Everything besides the project that shapes its environment.
pub(crate) struct WorkspaceEnvInputs<'a> {
pub global_aws: &'a GlobalAwsSettings,
pub global_ollama: &'a GlobalOllamaSettings,
pub global_llamacpp: &'a GlobalLlamaCppSettings,
pub global_openai_compatible: &'a GlobalOpenAiCompatibleSettings,
pub global_claude_instructions: Option<&'a str>,
pub global_custom_env_vars: &'a [EnvVar],
pub timezone: Option<&'a str>,
pub global_claude_code_settings: Option<&'a ClaudeCodeSettings>,
pub default_git_user_name: Option<&'a str>,
pub default_git_user_email: Option<&'a str>,
/// The corporate CA, already resolved from disk (`ca_certs::resolve`).
pub resolved_ca: Option<&'a ResolvedCa>,
/// The shared Claude OAuth token, already read from the keychain and
/// already filtered by `shared_claude_auth` (backend, opt-out, non-blank).
pub shared_claude_token: Option<&'a str>,
}
/// `"KEY=VALUE"` to a pair, splitting at the first `=` (values may contain `=`).
fn split_pair(entry: &str) -> (String, String) {
match entry.split_once('=') {
Some((k, v)) => (k.to_string(), v.to_string()),
None => (entry.to_string(), String::new()),
}
}
/// The `KEY=VALUE` pairs a project's container gets, in order, excluding the
/// host-specific ones. See the module docs.
pub(crate) fn workspace_env(
project: &Project,
inputs: &WorkspaceEnvInputs,
) -> Vec<(String, String)> {
let WorkspaceEnvInputs {
global_aws,
global_ollama,
global_llamacpp,
global_openai_compatible,
global_claude_instructions,
global_custom_env_vars,
timezone,
global_claude_code_settings,
default_git_user_name,
default_git_user_email,
resolved_ca,
shared_claude_token,
} = *inputs;
let mut env: Vec<(String, String)> = Vec::new();
if let Some(ref token) = project.git_token {
env.push(("GIT_TOKEN".to_string(), token.to_string()));
}
// Per-project git user overrides global defaults
let effective_git_name = project.git_user_name.as_deref().or(default_git_user_name);
let effective_git_email = project.git_user_email.as_deref().or(default_git_user_email);
if let Some(name) = effective_git_name {
env.push(("GIT_USER_NAME".to_string(), name.to_string()));
}
if let Some(email) = effective_git_email {
env.push(("GIT_USER_EMAIL".to_string(), email.to_string()));
}
// Bedrock configuration
if project.backend == Backend::Bedrock {
if let Some(ref bedrock) = project.bedrock_config {
env.push(("CLAUDE_CODE_USE_BEDROCK".to_string(), "1".to_string()));
// AWS region: per-project overrides global
let region = if !bedrock.aws_region.is_empty() {
Some(bedrock.aws_region.clone())
} else {
global_aws.aws_region.clone()
};
if let Some(ref r) = region {
env.push(("AWS_REGION".to_string(), r.to_string()));
}
match bedrock.auth_method {
BedrockAuthMethod::StaticCredentials => {
// Static/session credentials are NOT injected as env vars.
// They are written to ~/.aws/credentials by
// sync_bedrock_credentials() on every container
// start, so rotated/updated keys are picked up without a
// full container recreation (and never get baked into the
// snapshot image). The empty values set by the
// MANAGED_AUTH_KEYS neutralization pass below are ignored by
// the AWS SDK, which falls through to the credentials file.
}
BedrockAuthMethod::Profile => {
// Per-project profile overrides global
let profile = bedrock.aws_profile.as_ref()
.or(global_aws.aws_profile.as_ref());
if let Some(p) = profile {
env.push(("AWS_PROFILE".to_string(), p.to_string()));
}
env.push(("AWS_SSO_AUTH_REFRESH_CMD".to_string(), "triple-c-sso-refresh".to_string()));
}
BedrockAuthMethod::BearerToken => {
if let Some(ref token) = bedrock.aws_bearer_token {
env.push(("AWS_BEARER_TOKEN_BEDROCK".to_string(), token.to_string()));
}
}
}
if let Some(model) = resolve_with_global(
bedrock.model_id.as_deref(),
global_aws.default_model_id.as_deref(),
) {
env.push(("ANTHROPIC_MODEL".to_string(), model.to_string()));
}
if bedrock.disable_prompt_caching {
env.push(("DISABLE_PROMPT_CACHING".to_string(), "1".to_string()));
}
if let Some(ref tier) = bedrock.service_tier {
let trimmed = tier.trim();
if !trimmed.is_empty() {
env.push(("ANTHROPIC_BEDROCK_SERVICE_TIER".to_string(), trimmed.to_string()));
}
}
}
}
// ── Custom-endpoint backends ─────────────────────────────────────────────
// Ollama, llama.cpp and the OpenAI-Compatible gateway all point Claude Code
// at a non-Anthropic server via ANTHROPIC_BASE_URL. Each resolves its model
// id here; the model-alias vars are emitted once below, from
// `alias_model` / `alias_haiku`, so the three backends cannot drift apart.
let mut alias_model: Option<String> = None;
let mut alias_haiku: Option<String> = None;
// Ollama configuration
if project.backend == Backend::Ollama {
if let Some(ref ollama) = project.ollama_config {
if let Some(url) = resolve_with_global(
Some(&ollama.base_url),
global_ollama.base_url.as_deref(),
) {
env.push(("ANTHROPIC_BASE_URL".to_string(), url.to_string()));
}
env.push(("ANTHROPIC_AUTH_TOKEN".to_string(), "ollama".to_string()));
if let Some(model) = resolve_with_global(
ollama.model_id.as_deref(),
global_ollama.default_model_id.as_deref(),
) {
env.push(("ANTHROPIC_MODEL".to_string(), model.to_string()));
alias_model = Some(model.to_string());
}
alias_haiku = resolve_with_global(
ollama.haiku_model_id.as_deref(),
global_ollama.default_haiku_model_id.as_deref(),
)
.map(str::to_string);
}
}
// llama.cpp (llama-server) configuration
if project.backend == Backend::LlamaCpp {
if let Some(ref cfg) = project.llamacpp_config {
if let Some(url) = resolve_with_global(
Some(&cfg.base_url),
global_llamacpp.base_url.as_deref(),
) {
env.push(("ANTHROPIC_BASE_URL".to_string(), url.to_string()));
}
// llama-server only enforces an Authorization header when it was
// started with `--api-key` (default: none), so the value here is
// ignored in the common case. Claude Code still refuses to run
// against a custom base URL with no credential at all, so a
// placeholder is always sent — same trick as the Ollama branch
// above, which sends the literal "ollama".
env.push(("ANTHROPIC_AUTH_TOKEN".to_string(), "llama.cpp".to_string()));
if let Some(model) = resolve_with_global(
cfg.model_id.as_deref(),
global_llamacpp.default_model_id.as_deref(),
) {
env.push(("ANTHROPIC_MODEL".to_string(), model.to_string()));
alias_model = Some(model.to_string());
}
alias_haiku = resolve_with_global(
cfg.haiku_model_id.as_deref(),
global_llamacpp.default_haiku_model_id.as_deref(),
)
.map(str::to_string);
}
}
// OpenAI Compatible configuration
if project.backend == Backend::OpenAiCompatible {
if let Some(ref config) = project.openai_compatible_config {
if let Some(url) = resolve_with_global(
Some(&config.base_url),
global_openai_compatible.base_url.as_deref(),
) {
env.push(("ANTHROPIC_BASE_URL".to_string(), url.to_string()));
}
if let Some(ref key) = config.api_key {
env.push(("ANTHROPIC_AUTH_TOKEN".to_string(), key.to_string()));
}
if let Some(model) = resolve_with_global(
config.model_id.as_deref(),
global_openai_compatible.default_model_id.as_deref(),
) {
env.push(("ANTHROPIC_MODEL".to_string(), model.to_string()));
alias_model = Some(model.to_string());
}
alias_haiku = resolve_with_global(
config.haiku_model_id.as_deref(),
global_openai_compatible.default_haiku_model_id.as_deref(),
)
.map(str::to_string);
}
}
// Model aliases — the fix for background Claude Code calls against a local
// server. Only for backends that talk to a custom endpoint: Anthropic and
// Bedrock reach servers that really do host the Anthropic model ids, so
// they keep Claude Code's own defaults. Anything not emitted here is
// blanked by the MANAGED_AUTH_KEYS pass below, so switching *away* from a
// custom endpoint clears the aliases out of the snapshot image too.
if project.backend.uses_custom_endpoint() {
for (key, value) in
compute_model_aliases(alias_model.as_deref(), alias_haiku.as_deref())
{
env.push((key.to_string(), value));
}
}
// Shared Claude Code OAuth token (Anthropic backend only, opt-out per
// project). Injected *before* the neutralization pass below so that pass
// sees it as already-set; when it is absent the pass actively blanks the
// variable instead of leaving a stale one baked into the snapshot image.
if let Some(token) = shared_claude_token {
env.push((CLAUDE_OAUTH_TOKEN_ENV.to_string(), token.to_string()));
}
// ── Corporate CA certificates ───────────────────────────────────────────
// Resolved here (rather than down with the mounts) so the env vars land
// *before* the neutralization pass below and are seen as already-set.
//
// A bad path is a hard error, not a warning: behind a TLS-terminating
// proxy a container without the CA fails every HTTPS call — npm, pip, git,
// and Claude Code's own API requests — each in its own confusing way. One
// message naming the path is far kinder.
//
// The values are set here rather than exported by the entrypoint because a
// terminal session is a `docker exec`, which sees the container's
// configured env and nothing the entrypoint exported. Same lesson as
// `$BROWSER` and the URL relay shim.
for (key, value) in ca_certs::ca_env_vars(resolved_ca) {
env.push((key.to_string(), value));
}
// ── Neutralize stale backend auth env vars ──────────────────────────────
// When a project switches backends (e.g. Bedrock → Anthropic) the container
// is recreated *from a snapshot image* committed off the previous container,
// and `docker commit` copies that container's full ENV into the image. So
// any auth var set under the old backend (e.g. CLAUDE_CODE_USE_BEDROCK=1,
// AWS_PROFILE, a model alias) survives in the image ENV and stays active
// unless we explicitly override it at create time.
//
// This pass is about *staleness*, not secrecy. It fixes the container it is
// building and does nothing to the image, so it is not — and never was —
// a defence against a credential baked into a snapshot. That is
// `commit_container_snapshot`'s job, via SECRET_ENV_KEYS.
// Create-time env takes precedence over image ENV, so we set every managed
// auth key the *current* backend did NOT set to an empty value, clearing the
// stale baked-in one.
const MANAGED_AUTH_KEYS: &[&str] = &[
"CLAUDE_CODE_USE_BEDROCK",
"AWS_REGION",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_PROFILE",
"AWS_BEARER_TOKEN_BEDROCK",
"AWS_SSO_AUTH_REFRESH_CMD",
"ANTHROPIC_BASE_URL",
"ANTHROPIC_AUTH_TOKEN",
"ANTHROPIC_MODEL",
"DISABLE_PROMPT_CACHING",
"ANTHROPIC_BEDROCK_SERVICE_TIER",
// Switching from a custom-endpoint backend to Anthropic or Bedrock must
// *clear* the aliases, not merely stop setting them: a stale
// ANTHROPIC_DEFAULT_HAIKU_MODEL baked into the snapshot image would
// keep pointing background calls at a model id the new backend has
// never heard of.
ANTHROPIC_DEFAULT_OPUS_MODEL,
ANTHROPIC_DEFAULT_SONNET_MODEL,
ANTHROPIC_DEFAULT_HAIKU_MODEL,
ANTHROPIC_DEFAULT_FABLE_MODEL,
// Revoking the shared token, opting a project out, or switching away
// from the Anthropic backend must *clear* this, not merely stop setting
// it — otherwise the value committed into the snapshot image keeps
// authenticating the container with a credential the user removed.
CLAUDE_OAUTH_TOKEN_ENV,
];
// Same reasoning for the CA vars — `ca_env` already emits them empty
// when no CA is configured, so this list is belt-and-braces for a snapshot
// committed by a build that predates the feature.
let managed_keys: Vec<&str> = MANAGED_AUTH_KEYS
.iter()
.copied()
.chain(ca_certs::CA_ENV_KEYS.iter().copied())
.collect();
let already_set: std::collections::HashSet<String> =
env.iter().map(|(k, _)| k.clone()).collect();
for key in &managed_keys {
if !already_set.contains(*key) {
env.push((key.to_string(), String::new()));
}
}
// Custom environment variables (global + per-project, project overrides global for same key)
let merged_env = merge_custom_env_vars(global_custom_env_vars, &project.custom_env_vars);
for env_var in &merged_env {
let key = env_var.key.trim();
if key.is_empty() {
continue;
}
if is_reserved_env_key(key) {
log::warn!("Skipping reserved env var: {}", key);
continue;
}
env.push((key.to_string(), env_var.value.clone()));
}
let custom_env_fingerprint = compute_env_fingerprint(&merged_env);
env.push(("TRIPLE_C_CUSTOM_ENV".to_string(), custom_env_fingerprint.to_string()));
// Container timezone
if let Some(tz) = timezone {
if !tz.is_empty() {
env.push(("TZ".to_string(), tz.to_string()));
}
}
// Mission Control env var
if project.mission_control_enabled {
env.push(("MISSION_CONTROL_ENABLED".to_string(), "1".to_string()));
}
env.push(split_pair(&vpn_env_var(project.vpn_support_enabled)));
// Permission mode — read by triple-c-task-runner for scheduled (headless)
// Claude Code runs. Interactive terminals get the flags directly instead.
env.push((
"TRIPLE_C_PERMISSION_MODE".to_string(),
project.effective_permission_mode().as_env_value().to_string(),
));
// Claude instructions (global + per-project, plus port mapping info + scheduler docs)
let combined_instructions = build_claude_instructions(
global_claude_instructions,
project.claude_instructions.as_deref(),
&project.port_mappings,
project.mission_control_enabled,
project.sandbox_mode_enabled,
);
if let Some(ref instructions) = combined_instructions {
env.push(("CLAUDE_INSTRUCTIONS".to_string(), instructions.to_string()));
}
// Claude Code settings (global + per-project merged)
let merged_cc_settings = merge_claude_code_settings(
global_claude_code_settings,
project.claude_code_settings.as_ref(),
);
// Env-var-based settings, read directly by Claude Code. Extracted and unit
// tested for the `vpn_host_config` reason: a container is created once, by
// a very long function, and a variable emitted with the wrong value here is
// invisible until someone wonders why a switch does nothing.
env.extend(
claude_code_env_vars(merged_cc_settings.as_ref())
.iter()
.map(|entry| split_pair(entry)),
);
// settings.json-based settings (applied by the entrypoint). Always emitted,
// even with no `ClaudeCodeSettings` struct present: the payload asserts the
// *whole* managed key set, so "no settings" still has to be stated over a
// settings.json left behind on the config volume by a previous config.
// Split so the payload is safe under an older entrypoint that has never
// heard of the null-means-delete convention — see
// `split_claude_code_settings_payload`.
let (cc_settings_set, cc_settings_clear) = split_claude_code_settings_payload(
&build_claude_code_settings_json(merged_cc_settings.as_ref(), project.sandbox_mode_enabled),
);
env.push(("CLAUDE_CODE_SETTINGS_JSON".to_string(), cc_settings_set.to_string()));
env.push(("CLAUDE_CODE_SETTINGS_CLEAR".to_string(), cc_settings_clear.to_string()));
env
}
/// The names of the custom variables [`workspace_env`] emits for `project`
/// (global + per-project, trimmed, reserved names skipped): the ones
/// copy-at-connect treats as credentials.
pub(crate) fn custom_env_keys(project: &Project, global_custom_env_vars: &[EnvVar]) -> Vec<String> {
let mut keys: Vec<String> = merge_custom_env_vars(global_custom_env_vars, &project.custom_env_vars)
.iter()
.map(|e| e.key.trim().to_string())
.filter(|k| !k.is_empty() && !is_reserved_env_key(k))
.collect();
keys.sort();
keys.dedup();
keys
}
/// [`workspace_env`] with its inputs resolved from `settings` exactly as
/// `create_container` resolves them (the CA from disk, the shared Claude
/// token from the keychain), for a host that does not create a Docker
/// container. Also returns the resolved CA, whose files the caller ships.
pub(crate) fn workspace_env_from_settings(
project: &Project,
settings: &crate::models::AppSettings,
) -> Result<crate::host::aws_host::AwsEnv, String> {
let effective_ca_path =
resolve_with_global(project.ca_cert_path.as_deref(), settings.ca_cert_path.as_deref());
let resolved_ca = ca_certs::resolve(effective_ca_path)?;
let pairs = workspace_env_with_ca(project, settings, resolved_ca.as_ref());
Ok((pairs, resolved_ca))
}
/// [`workspace_env`] resolved from `settings` like
/// [`workspace_env_from_settings`] but with the CA given (`None`: no CA
/// variables) — so nothing is read from disk. The keychain is (the shared
/// Claude token). Copy-at-connect's terminal environment uses it: the
/// credential pairs never depend on the CA.
pub(crate) fn workspace_env_with_ca(
project: &Project,
settings: &crate::models::AppSettings,
resolved_ca: Option<&ResolvedCa>,
) -> Vec<(String, String)> {
let shared_claude = super::container::shared_claude_auth(project);
workspace_env(
project,
&WorkspaceEnvInputs {
global_aws: &settings.global_aws,
global_ollama: &settings.global_ollama,
global_llamacpp: &settings.global_llamacpp,
global_openai_compatible: &settings.global_openai_compatible,
global_claude_instructions: settings.global_claude_instructions.as_deref(),
global_custom_env_vars: &settings.global_custom_env_vars,
timezone: settings.timezone.as_deref(),
global_claude_code_settings: settings.global_claude_code_settings.as_ref(),
default_git_user_name: settings.default_git_user_name.as_deref(),
default_git_user_email: settings.default_git_user_email.as_deref(),
resolved_ca,
shared_claude_token: shared_claude.as_ref().map(|(token, _)| token.as_str()),
},
)
}
#[cfg(test)]
mod tests {
use super::super::ca_certs::ResolvedCa;
use crate::models::*;
use std::path::PathBuf;
/// Every input the env block reads, owned, so a table row is one value.
pub(crate) struct Case {
pub name: &'static str,
pub project: Project,
pub global_aws: GlobalAwsSettings,
pub global_ollama: GlobalOllamaSettings,
pub global_llamacpp: GlobalLlamaCppSettings,
pub global_openai: GlobalOpenAiCompatibleSettings,
pub global_instructions: Option<&'static str>,
pub global_env: Vec<EnvVar>,
pub timezone: Option<&'static str>,
pub global_cc: Option<ClaudeCodeSettings>,
pub default_git_name: Option<&'static str>,
pub default_git_email: Option<&'static str>,
pub ca: Option<ResolvedCa>,
pub shared_token: Option<(String, String)>,
}
fn case(name: &'static str, project: Project) -> Case {
Case {
name,
project,
global_aws: GlobalAwsSettings::default(),
global_ollama: GlobalOllamaSettings::default(),
global_llamacpp: GlobalLlamaCppSettings::default(),
global_openai: GlobalOpenAiCompatibleSettings::default(),
global_instructions: None,
global_env: Vec::new(),
timezone: None,
global_cc: None,
default_git_name: None,
default_git_email: None,
ca: None,
shared_token: None,
}
}
fn proj(backend: Backend) -> Project {
let mut p = Project::new("demo".to_string(), Vec::new());
p.backend = backend;
p
}
fn bedrock(method: BedrockAuthMethod) -> BedrockConfig {
BedrockConfig {
auth_method: method,
aws_region: "us-east-1".into(),
aws_access_key_id: Some("AKIAFAKE".into()),
aws_secret_access_key: Some("secretfake".into()),
aws_session_token: None,
aws_profile: Some("proj-profile".into()),
aws_bearer_token: Some("bearerfake".into()),
model_id: Some("anthropic.claude-x".into()),
disable_prompt_caching: true,
service_tier: Some(" priority ".into()),
}
}
pub(crate) fn cases() -> Vec<Case> {
let mut v = Vec::new();
let mut c = case("anthropic_shared_token", proj(Backend::Anthropic));
c.shared_token = Some(("sk-shared-token".into(), "v7".into()));
v.push(c);
v.push(case("anthropic_plain", proj(Backend::Anthropic)));
let mut p = proj(Backend::Bedrock);
p.bedrock_config = Some(bedrock(BedrockAuthMethod::StaticCredentials));
let mut c = case("bedrock_static_keys", p);
c.global_aws.default_model_id = Some("global-model".into());
v.push(c);
let mut p = proj(Backend::Bedrock);
let mut b = bedrock(BedrockAuthMethod::Profile);
b.aws_region = String::new();
b.aws_profile = None;
b.model_id = None;
b.disable_prompt_caching = false;
b.service_tier = Some(" ".into());
p.bedrock_config = Some(b);
let mut c = case("bedrock_profile_global_fallbacks", p);
c.global_aws.aws_region = Some("eu-west-1".into());
c.global_aws.aws_profile = Some("global-profile".into());
c.global_aws.default_model_id = Some("global-model".into());
v.push(c);
let mut p = proj(Backend::Bedrock);
p.bedrock_config = Some(bedrock(BedrockAuthMethod::BearerToken));
v.push(case("bedrock_bearer", p));
let mut p = proj(Backend::Ollama);
p.ollama_config = Some(OllamaConfig {
base_url: "http://ollama.local:11434".into(),
model_id: Some("qwen3".into()),
haiku_model_id: None,
});
let mut c = case("ollama", p);
c.global_ollama.default_haiku_model_id = Some("tiny".into());
v.push(c);
let mut p = proj(Backend::LlamaCpp);
p.llamacpp_config = Some(LlamaCppConfig {
base_url: String::new(),
model_id: None,
haiku_model_id: Some("llama-small".into()),
});
let mut c = case("llamacpp_global_url", p);
c.global_llamacpp.base_url = Some("http://llama:8080".into());
c.global_llamacpp.default_model_id = Some("llama-big".into());
v.push(c);
let mut p = proj(Backend::OpenAiCompatible);
p.openai_compatible_config = Some(OpenAiCompatibleConfig {
base_url: "https://gw.example/v1".into(),
api_key: Some("sk-gateway".into()),
model_id: Some("gpt-x".into()),
haiku_model_id: Some("gpt-mini".into()),
});
v.push(case("openai_compatible", p));
let mut p = proj(Backend::Anthropic);
p.custom_env_vars = vec![
EnvVar { key: "FOO".into(), value: "project".into() },
EnvVar { key: " SPACED ".into(), value: "x y".into() },
EnvVar { key: "".into(), value: "dropped".into() },
EnvVar { key: "HOME".into(), value: "reserved".into() },
EnvVar { key: "CLAUDE_CODE_OAUTH_TOKEN".into(), value: "reserved".into() },
];
let mut c = case("custom_env", p);
c.global_env = vec![
EnvVar { key: "FOO".into(), value: "global".into() },
EnvVar { key: "BAR".into(), value: "b".into() },
];
v.push(c);
let mut p = proj(Backend::Anthropic);
p.claude_instructions = Some("project rules".into());
p.mission_control_enabled = true;
p.sandbox_mode_enabled = true;
p.port_mappings = vec![PortMapping {
host_port: 8080,
container_port: 3000,
protocol: "tcp".into(),
}];
let mut c = case("instructions_and_mission_control", p);
c.global_instructions = Some("global rules\nline two");
v.push(c);
let mut p = proj(Backend::Anthropic);
p.claude_code_settings = Some(ClaudeCodeSettings {
effort: Some("high".into()),
focus_mode: Some(false),
..Default::default()
});
let mut c = case("claude_code_settings", p);
c.global_cc = Some(ClaudeCodeSettings {
tui_mode: Some("fullscreen".into()),
env_scrub: Some(true),
prompt_caching_1h: Some(true),
..Default::default()
});
v.push(c);
let mut p = proj(Backend::Anthropic);
p.vpn_support_enabled = true;
p.git_token = Some("ghp_faketoken".into());
p.git_user_name = Some("Project User".into());
let mut c = case("vpn_git_timezone", p);
c.timezone = Some("Europe/Berlin");
c.default_git_name = Some("Default Name");
c.default_git_email = Some("default@example.com");
v.push(c);
let mut c = case("corporate_ca", proj(Backend::Anthropic));
c.ca = Some(ResolvedCa {
host_path: "/certs".into(),
is_dir: true,
mount_target: "/tmp/.host-ca".into(),
cert_files: vec![PathBuf::from("/certs/a.crt")],
});
v.push(c);
v
}
fn golden_path(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("src/docker/workspace_env_golden")
.join(format!("{name}.txt"))
}
/// `merge_custom_env_vars` collects a `HashMap`, so the custom variables
/// come out in a different order every run (before the extraction as
/// well). Sort that one run — between the last managed blank and the
/// fingerprint — so the rest of the order is still compared exactly.
fn normalise(mut env: Vec<String>) -> Vec<String> {
let end = env.iter().position(|e| e.starts_with("TRIPLE_C_CUSTOM_ENV=")).unwrap();
let start = env.iter().position(|e| e.starts_with("CLAUDE_CODE_OAUTH_TOKEN=")).unwrap() + 1;
env[start..end].sort();
env
}
/// The pre-refactor block's output, recorded before the extraction (see
/// `workspace_env_golden/`), must be reproduced exactly, order included.
#[test]
fn workspace_env_matches_what_create_container_built_before_the_extraction() {
for c in cases() {
let inputs = super::WorkspaceEnvInputs {
global_aws: &c.global_aws,
global_ollama: &c.global_ollama,
global_llamacpp: &c.global_llamacpp,
global_openai_compatible: &c.global_openai,
global_claude_instructions: c.global_instructions,
global_custom_env_vars: &c.global_env,
timezone: c.timezone,
global_claude_code_settings: c.global_cc.as_ref(),
default_git_user_name: c.default_git_name,
default_git_user_email: c.default_git_email,
resolved_ca: c.ca.as_ref(),
shared_claude_token: c.shared_token.as_ref().map(|(t, _)| t.as_str()),
};
let got: Vec<String> = super::workspace_env(&c.project, &inputs)
.into_iter()
.map(|(k, v)| format!("{k}={v}"))
.collect();
let want = std::fs::read_to_string(golden_path(c.name)).unwrap();
let want: Vec<String> = want
.lines()
.filter(|l| *l != "[" && *l != "]")
.map(|l| {
let l = l.trim().trim_end_matches(',');
serde_json::from_str::<String>(l).unwrap_or_else(|_| panic!("{l}"))
})
.collect();
assert_eq!(normalise(got), normalise(want), "case {}", c.name);
}
}
}
@@ -0,0 +1,33 @@
[
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_MODEL=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"CLAUDE_CODE_OAUTH_TOKEN=sk-shared-token",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_MODEL=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"CLAUDE_CODE_USE_BEDROCK=1",
"AWS_REGION=us-east-1",
"AWS_BEARER_TOKEN_BEDROCK=bearerfake",
"ANTHROPIC_MODEL=anthropic.claude-x",
"DISABLE_PROMPT_CACHING=1",
"ANTHROPIC_BEDROCK_SERVICE_TIER=priority",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"CLAUDE_CODE_USE_BEDROCK=1",
"AWS_REGION=eu-west-1",
"AWS_PROFILE=global-profile",
"AWS_SSO_AUTH_REFRESH_CMD=triple-c-sso-refresh",
"ANTHROPIC_MODEL=global-model",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_BEARER_TOKEN_BEDROCK=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"CLAUDE_CODE_USE_BEDROCK=1",
"AWS_REGION=us-east-1",
"ANTHROPIC_MODEL=anthropic.claude-x",
"DISABLE_PROMPT_CACHING=1",
"ANTHROPIC_BEDROCK_SERVICE_TIER=priority",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_MODEL=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=1",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1",
"ENABLE_PROMPT_CACHING_1H=1",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"effortLevel\":\"high\",\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false,\"tui\":\"fullscreen\"}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt",
"REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt",
"SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_MODEL=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,37 @@
[
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_MODEL=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"BAR=b",
"FOO=project",
"SPACED=x y",
"HOME=reserved",
"TRIPLE_C_CUSTOM_ENV=63ce864a8c48a386cb039d4878e3885d5a823d4bb52047ba0e18cb87b9f3035b",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,34 @@
[
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_MODEL=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"MISSION_CONTROL_ENABLED=1",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=global rules\nline two\n\n## Mission Control\n\nThe `/workspace/mission-control/` directory contains **Flight Control** — an AI-first development methodology for structured project management. Use it for all project work.\n\n### How It Works\n\n- **Mission Control is a tool, not a project.** It provides skills and methodology for managing other projects.\n- All Flight Control skills are installed as personal skills in `~/.claude/skills/` and are automatically available as `/slash-commands`\n- The methodology docs and project registry live in `/workspace/mission-control/`\n\n### When to Use\n\nWhen working on any project that has a `.flightops/` directory, follow the Flight Control methodology:\n1. Read the project's `.flightops/ARTIFACTS.md` to understand artifact storage\n2. Read `.flightops/FLIGHT_OPERATIONS.md` for the implementation workflow\n3. Use Mission Control skills for planning and execution\n\n### Available Skills\n\n| Skill | When to Use |\n|-------|-------------|\n| `/init-project` | Setting up a new project for Flight Control |\n| `/mission` | Defining new work outcomes (days-to-weeks scope) |\n| `/flight` | Creating technical specs from missions (hours-to-days scope) |\n| `/leg` | Generating implementation steps from flights (minutes-to-hours scope) |\n| `/agentic-workflow` | Executing legs with multi-agent workflow (implement, review, commit) |\n| `/flight-debrief` | Post-flight analysis after a flight lands |\n| `/mission-debrief` | Post-mission retrospective after completion |\n| `/daily-briefing` | Cross-project status report |\n\n### Key Rules\n\n- **Planning skills produce artifacts only** — never modify source code directly\n- **Phase gates require human confirmation** — missions before flights, flights before legs\n- **Legs are immutable once in-flight** — create new ones instead of modifying\n- **`/agentic-workflow` orchestrates implementation** — it spawns separate Developer and Reviewer agents\n- **Artifacts live in the target project** — not in mission-control\n\nproject rules\n\n## Flight Operations\n\nThis project uses **Flight Control** (bundled with Triple-C) for structured development.\n\n**Before any mission/flight/leg work, read these files in order:**\n1. `.flightops/README.md` — What the flightops directory contains\n2. `.flightops/FLIGHT_OPERATIONS.md` — **The workflow you MUST follow**\n3. `.flightops/ARTIFACTS.md` — Where all artifacts are stored\n4. `.flightops/agent-crews/` — Project crew definitions for each phase (read the relevant crew file)\n\n## Available Port Mappings\nThe following ports are mapped from the host to this container. Use these container ports when starting services that need to be accessible from the host:\n- Host port 8080 -> Container port 3000 (tcp)\n\n## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relLine truncated
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"allowUnsandboxedCommands\":false,\"enableWeakerNestedSandbox\":true,\"enabled\":true},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"ANTHROPIC_BASE_URL=http://llama:8080",
"ANTHROPIC_AUTH_TOKEN=llama.cpp",
"ANTHROPIC_MODEL=llama-big",
"ANTHROPIC_DEFAULT_OPUS_MODEL=llama-big",
"ANTHROPIC_DEFAULT_SONNET_MODEL=llama-big",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=llama-small",
"ANTHROPIC_DEFAULT_FABLE_MODEL=llama-big",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"ANTHROPIC_BASE_URL=http://ollama.local:11434",
"ANTHROPIC_AUTH_TOKEN=ollama",
"ANTHROPIC_MODEL=qwen3",
"ANTHROPIC_DEFAULT_OPUS_MODEL=qwen3",
"ANTHROPIC_DEFAULT_SONNET_MODEL=qwen3",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=tiny",
"ANTHROPIC_DEFAULT_FABLE_MODEL=qwen3",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,33 @@
[
"ANTHROPIC_BASE_URL=https://gw.example/v1",
"ANTHROPIC_AUTH_TOKEN=sk-gateway",
"ANTHROPIC_MODEL=gpt-x",
"ANTHROPIC_DEFAULT_OPUS_MODEL=gpt-x",
"ANTHROPIC_DEFAULT_SONNET_MODEL=gpt-x",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=gpt-mini",
"ANTHROPIC_DEFAULT_FABLE_MODEL=gpt-x",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"VPN_SUPPORT_ENABLED=0",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
@@ -0,0 +1,37 @@
[
"GIT_TOKEN=ghp_faketoken",
"GIT_USER_NAME=Project User",
"GIT_USER_EMAIL=default@example.com",
"NODE_EXTRA_CA_CERTS=",
"REQUESTS_CA_BUNDLE=",
"SSL_CERT_FILE=",
"CLAUDE_CODE_USE_BEDROCK=",
"AWS_REGION=",
"AWS_ACCESS_KEY_ID=",
"AWS_SECRET_ACCESS_KEY=",
"AWS_SESSION_TOKEN=",
"AWS_PROFILE=",
"AWS_BEARER_TOKEN_BEDROCK=",
"AWS_SSO_AUTH_REFRESH_CMD=",
"ANTHROPIC_BASE_URL=",
"ANTHROPIC_AUTH_TOKEN=",
"ANTHROPIC_MODEL=",
"DISABLE_PROMPT_CACHING=",
"ANTHROPIC_BEDROCK_SERVICE_TIER=",
"ANTHROPIC_DEFAULT_OPUS_MODEL=",
"ANTHROPIC_DEFAULT_SONNET_MODEL=",
"ANTHROPIC_DEFAULT_HAIKU_MODEL=",
"ANTHROPIC_DEFAULT_FABLE_MODEL=",
"CLAUDE_CODE_OAUTH_TOKEN=",
"TRIPLE_C_CUSTOM_ENV=",
"TZ=Europe/Berlin",
"VPN_SUPPORT_ENABLED=1",
"TRIPLE_C_PERMISSION_MODE=default",
"CLAUDE_INSTRUCTIONS=## Scheduled Tasks\n\nThis container supports scheduled tasks via `triple-c-scheduler`. You can set up recurring or one-time tasks that run as separate Claude Code agents.\n\n### Commands\n- `triple-c-scheduler add --name \"NAME\" --schedule \"CRON\" --prompt \"TASK\"` — Add a recurring task\n- `triple-c-scheduler add --name \"NAME\" --at \"YYYY-MM-DD HH:MM\" --prompt \"TASK\"` — Add a one-time task\n- `triple-c-scheduler list` — List all scheduled tasks, with a running/idle status column\n- `triple-c-scheduler remove --id ID` — Remove a task\n- `triple-c-scheduler enable --id ID` / `triple-c-scheduler disable --id ID` — Toggle tasks\n- `triple-c-scheduler status [--id ID] [--watch]` — Show what is running right now, and for how long\n- `triple-c-scheduler logs [--id ID] [--tail N]` — View execution logs\n- `triple-c-scheduler run --id ID` — Manually trigger a task immediately (streams its log)\n- `triple-c-scheduler notifications [--clear]` — View or clear completion notifications\n\n### Cron format\nStandard 5-field cron: `minute hour day-of-month month day-of-week`\nExamples: `*/30 * * * *` (every 30 min), `0 9 * * 1-5` (9am weekdays), `0 */2 * * *` (every 2 hours)\n\n### One-time tasks\nUse `--at \"YYYY-MM-DD HH:MM\"` instead of `--schedule`. The task automatically removes itself after execution.\n\n### Working directory\nUse `--working-dir /workspace/project` to set where the task runs (default: /workspace).\n\n### Checking results\nWhile a task is running, `triple-c-scheduler status` reports it with elapsed time — a log that has stopped growing is normal, because `claude -p` writes its answer only at the end, so use `status` rather than log silence to tell a slow run from a dead one. After tasks run, check notifications with `triple-c-scheduler notifications` and detailed output with `triple-c-scheduler logs`.\n\n### Timezone\nScheduled times use the container's configured timezone (check with `date`). If no timezone is configured, UTC is used.\n\n## Referring to files\n\nThe user can click a file path in this terminal to open it in a viewer (while a program holds the mouse: Shift+click, or Option+click on macOS). Paths relative to the project work. When you mention a file outside the project (for example in `/tmp`, including your scratchpad directory, or under `~`), print its full absolute path, not a shortened or relative one, so it can be opened.",
"CLAUDE_CODE_NO_FLICKER=",
"CLAUDE_CODE_ENABLE_AWAY_SUMMARY=",
"CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0",
"ENABLE_PROMPT_CACHING_1H=0",
"CLAUDE_CODE_SETTINGS_JSON={\"autoScrollEnabled\":true,\"sandbox\":{\"enabled\":false},\"showThinkingSummaries\":false}",
"CLAUDE_CODE_SETTINGS_CLEAR=[\"awaySummaryEnabled\",\"effortLevel\",\"tui\",\"viewMode\"]",
]
+3 -1
View File
@@ -51,8 +51,10 @@ pub async fn poll_file(
"poll".to_string(),
container_path.to_string(),
];
// A background poll: an open viewer must not keep an idle AWS workspace
// up. Opening and saving a file are the user's own actions and count.
let (stdout, stderr, code) = container
.exec_oneshot_streams_as("claude", cmd, Vec::new())
.exec_oneshot_streams_background("claude", cmd)
.await?;
if code != 0 && code != 4 {
return Err(format!(
+46 -8
View File
@@ -277,20 +277,35 @@ async fn exec_oneshot_raw(
cmd: Vec<String>,
env: Vec<String>,
limit: usize,
) -> Result<OneshotOutput, String> {
exec_oneshot_raw_marked(ep, user, cmd, env, limit, false).await
}
/// The agent's background marker (protocol spec, "Activity"): a request that
/// carries it never counts as activity for idle stop.
pub const BACKGROUND_HEADER: &str = "X-Triple-C-Background";
async fn exec_oneshot_raw_marked(
ep: &AgentEndpoint,
user: &str,
cmd: Vec<String>,
env: Vec<String>,
limit: usize,
background: bool,
) -> Result<OneshotOutput, String> {
const CONTEXT: &str = "Failed to start exec";
let spec = ExecSpec::new(cmd).user(user).working_dir(None).env(env).tty(false);
let mut body = spec_json(&spec);
body.insert("limit".into(), serde_json::json!(limit));
let resp = send(
CONTEXT,
http()
.post(ep.url("v1/exec"))
.header(reqwest::header::AUTHORIZATION, ep.auth())
.json(&body),
)
.await?;
let mut req = http()
.post(ep.url("v1/exec"))
.header(reqwest::header::AUTHORIZATION, ep.auth())
.json(&body);
if background {
req = req.header(BACKGROUND_HEADER, "1");
}
let resp = send(CONTEXT, req).await?;
let status = resp.status();
if status == reqwest::StatusCode::PAYLOAD_TOO_LARGE {
@@ -352,6 +367,18 @@ pub async fn exec_oneshot_limited(
Ok((out.text(), out.exit_code))
}
/// [`exec_oneshot_limited`] marked as a background poll, so it never keeps
/// an idle workspace up (the auth bridge's `/proc/net` reads).
pub async fn exec_oneshot_background(
ep: &AgentEndpoint,
user: &str,
cmd: Vec<String>,
limit: usize,
) -> Result<(String, i64), String> {
let out = exec_oneshot_raw_marked(ep, user, cmd, Vec::new(), limit, true).await?;
Ok((out.text(), out.exit_code))
}
/// `exec_oneshot_as`: combined output under [`MAX_ONESHOT_OUTPUT`].
pub async fn exec_oneshot_as(
ep: &AgentEndpoint,
@@ -373,6 +400,17 @@ pub async fn exec_oneshot_streams_as(
Ok((out.stdout(), out.stderr(), out.exit_code))
}
/// [`exec_oneshot_streams_as`] marked as a background poll (the file
/// viewer's change check), so it never keeps an idle workspace up.
pub async fn exec_oneshot_streams_background(
ep: &AgentEndpoint,
user: &str,
cmd: Vec<String>,
) -> Result<(String, String, i64), String> {
let out = exec_oneshot_raw_marked(ep, user, cmd, Vec::new(), MAX_ONESHOT_OUTPUT, true).await?;
Ok((out.stdout(), out.stderr(), out.exit_code))
}
// ── detached execs, status, resize ───────────────────────────────────────────
#[derive(Deserialize)]
+36 -1
View File
@@ -22,6 +22,8 @@ const TOKEN: &str = "fake-agent-token-for-unit-tests-only-0000";
struct Seen {
auth: Vec<String>,
exec_bodies: Vec<Value>,
/// The background marker of each `/v1/exec`, if any.
exec_background: Vec<Option<String>>,
detached_bodies: Vec<Value>,
status_polls: u32,
resizes: Vec<(String, Value)>,
@@ -62,7 +64,13 @@ async fn exec(State(f): St, h: HeaderMap, Json(body): Json<Value>) -> Response {
if !f.authed(&h) {
return unauthorized();
}
f.seen.lock().unwrap().exec_bodies.push(body.clone());
{
let mut seen = f.seen.lock().unwrap();
seen.exec_bodies.push(body.clone());
seen.exec_background.push(
h.get("x-triple-c-background").and_then(|v| v.to_str().ok()).map(str::to_string),
);
}
match body["cmd"][0].as_str().unwrap_or("") {
"sh" => Json(json!({"chunks": [{"s": "out", "d": b64(b"1000\n1001\n")}], "exit_code": 0}))
.into_response(),
@@ -304,6 +312,33 @@ async fn other_errors_carry_the_agents_message_and_no_token() {
assert_eq!(e, "Failed to start exec: boom");
}
#[tokio::test]
async fn only_a_background_poll_carries_the_background_marker() {
let (ep, fake) = fake_agent().await;
let t = target(&ep);
t.exec_oneshot_limited("claude", cmd("sh"), 1000).await.unwrap();
t.exec_oneshot_background("claude", cmd("sh"), 1000).await.unwrap();
let seen = fake.seen.lock().unwrap();
assert_eq!(seen.exec_background, [None, Some("1".to_string())]);
assert_eq!(seen.exec_bodies[0], seen.exec_bodies[1], "the request is otherwise the same");
}
#[tokio::test]
async fn the_file_viewers_poll_is_a_background_request_and_its_open_and_save_are_not() {
let (ep, fake) = fake_agent().await;
let t = target(&ep);
// The 2 s change check, as the viewer runs it.
let _ = crate::file_viewer::poll::poll_file(&t, "/workspace/x").await;
t.exec_oneshot_streams_background("claude", cmd("sh")).await.unwrap();
// What opening or saving goes through: user actions, which count.
t.exec_oneshot_streams_as("claude", cmd("sh"), Vec::new()).await.unwrap();
let seen = fake.seen.lock().unwrap();
assert_eq!(seen.exec_background, [Some("1".to_string()), Some("1".to_string()), None]);
let src = |s: &str| s.contains("exec_oneshot_streams_as(");
assert!(!src(include_str!("../../file_viewer/poll.rs")), "the poll stays a background request");
assert!(src(include_str!("../../file_viewer/resolve.rs")) && src(include_str!("../../file_viewer/write.rs")));
}
// ── one-shot execs ───────────────────────────────────────────────────────────
#[tokio::test]
File diff suppressed because it is too large. Load diff
+299
View File
@@ -0,0 +1,299 @@
//! Copy-at-connect (phase 6, ruling Q2): an AWS project whose credentials
//! are "copied when you connect" keeps them out of its Secrets Manager
//! secret (`aws::workspace`, `PayloadExtras::connect_only`) and brings them
//! in after every connect instead — a start, an attach, the background
//! re-attach when the app starts.
//!
//! The credentials are [`ConnectBundle`]: the SSH and CA files (the same
//! [`workspace_files`](super::workspace_files) the secret would carry) and
//! the credential environment pairs (`SECRET_ENV_KEYS` and every custom
//! variable, `aws::workspace::credential_pairs`). [`push_connect_bundle`]
//! uploads them through the agent, as root, into this connect's own root-only
//! staging dir on the task's `/dev/shm` tmpfs and runs `triple-c-apply-connect
//! <nonce>` (container), which
//! installs the CA, merges the SSH files, writes the git credential helper
//! and re-clones what failed. The terminals the app opens get the pairs in
//! their own `ExecSpec.env` ([`terminal_env`]); nothing puts them into the
//! agent's environment, so other execs, cron jobs and the scheduler never
//! see them. **Values are never logged**: errors carry names and the
//! script's own (value-free) output only.
use std::time::Duration;
use zeroize::Zeroizing;
use super::super::{tunnels, ContainerTarget};
use crate::aws::workspace::{credential_pairs, validate_file_key};
use crate::models::project::SecretsDelivery;
use crate::models::{AppSettings, Project};
/// The in-container half (container/triple-c-apply-connect).
pub const APPLY_CONNECT: &str = "/usr/local/bin/triple-c-apply-connect";
/// Staging (root 0700, on the tmpfs the entrypoint made for the agent
/// token): each connect gets `<nonce>/` with `ssh/`, `ca/` and `connect.env`.
pub const CONNECT_STAGE: &str = "/dev/shm/triple-c/connect";
pub const CONNECT_ENV_FILE: &str = "connect.env";
/// Every staged file is root 0600.
const STAGED_MODE: u32 = 0o600;
/// The whole push (a few uploads and two execs through the tunnel).
pub const PUSH_TIMEOUT: Duration = Duration::from_secs(120);
/// The most of the script's output a warning carries.
const OUTPUT_TAIL_LINES: usize = 10;
/// A credential value, zeroed when dropped (the settings export's
/// precedent). It narrows, not closes, the window: the values were read from
/// the keychain into a `Project`, and the copies an `ExecSpec` or an upload
/// body needs are plain buffers owned by those layers.
pub type SecretBytes = Zeroizing<Vec<u8>>;
pub type SecretString = Zeroizing<String>;
/// What copy-at-connect copies in.
#[derive(Clone, Default)]
pub struct ConnectBundle {
/// `ssh/<file>` / `ca/<file>`, as in the secret's `TRIPLEC_FILES`.
pub files: Vec<(String, SecretBytes)>,
/// The credential environment pairs.
pub env: Vec<(String, SecretString)>,
}
/// Names only: a bundle holds keys and tokens.
impl std::fmt::Debug for ConnectBundle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConnectBundle")
.field("files", &self.files.iter().map(|(k, _)| k.as_str()).collect::<Vec<_>>())
.field("env", &self.env.iter().map(|(k, _)| k.as_str()).collect::<Vec<_>>())
.finish()
}
}
/// Whether `project` copies its credentials in at connect.
pub fn copies_at_connect(project: &Project) -> bool {
project.is_aws()
&& project
.aws
.as_ref()
.is_some_and(|a| a.secrets_delivery == SecretsDelivery::CopyAtConnect)
}
/// The bundle for `project`, whose keychain secrets must already be loaded
/// (`load_secrets_for_project`): the same sources the secret would use.
pub fn connect_credentials(project: &Project, settings: &AppSettings) -> Result<ConnectBundle, String> {
let (_, ca) = super::aws_env(project, settings)?;
let ssh = project
.ssh_key_path
.as_deref()
.or(settings.default_ssh_key_path.as_deref());
let files = super::workspace_files(ssh, ca.as_ref())?
.into_iter()
.map(|(k, v)| (k, Zeroizing::new(v)))
.collect();
Ok(ConnectBundle { files, env: connect_env(project, settings) })
}
/// Only the credential pairs of `project` (secrets loaded): no file is read,
/// so a missing SSH or CA file never keeps the API keys from a terminal.
pub fn connect_env(project: &Project, settings: &AppSettings) -> Vec<(String, SecretString)> {
let pairs = crate::docker::workspace_env::workspace_env_with_ca(project, settings, None);
let custom =
crate::docker::workspace_env::custom_env_keys(project, &settings.global_custom_env_vars);
// With the gateway sidecar, the task's own ANTHROPIC_AUTH_TOKEN (the
// sidecar's master key, kept in the secret) is the right one; the
// OpenAI-compatible key would override it with one the sidecar refuses.
let sidecar = project.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
credential_pairs(&pairs, &custom)
.into_iter()
.filter(|(k, _)| !(sidecar && k == "ANTHROPIC_AUTH_TOKEN"))
.map(|(k, v)| (k, Zeroizing::new(v)))
.collect()
}
/// `project` as the store holds it, with its keychain secrets read in.
fn with_secrets(project: &Project) -> Project {
let mut p = project.clone();
crate::commands::project_commands::load_secrets_for_project(&mut p);
p
}
/// `connect.env`: one `KEY=<base64 of the value>` line per pair, so a value
/// with a newline or `=` reads back whole and nothing has to be quoted.
pub fn connect_env_file(env: &[(String, SecretString)]) -> SecretBytes {
use base64::Engine;
let b64 = base64::engine::general_purpose::STANDARD;
let mut out = Zeroizing::new(String::new());
for (k, v) in env {
out.push_str(k);
out.push('=');
b64.encode_string(v.as_bytes(), &mut out);
out.push('\n');
}
Zeroizing::new(out.as_bytes().to_vec())
}
/// `KEY=VALUE` strings for an `ExecSpec`.
pub fn exec_env(env: &[(String, SecretString)]) -> Vec<String> {
env.iter().map(|(k, v)| format!("{k}={}", v.as_str())).collect()
}
/// What a push needs from the container: root execs and root uploads.
pub trait ConnectTarget: Send + Sync {
fn exec_root(
&self,
cmd: Vec<String>,
) -> impl std::future::Future<Output = Result<(String, i64), String>> + Send;
fn upload_root(
&self,
dir: &str,
name: &str,
data: &[u8],
mode: u32,
) -> impl std::future::Future<Output = Result<(), String>> + Send;
}
impl ConnectTarget for ContainerTarget {
async fn exec_root(&self, cmd: Vec<String>) -> Result<(String, i64), String> {
self.exec_oneshot_as("root", cmd, Vec::new()).await
}
async fn upload_root(&self, dir: &str, name: &str, data: &[u8], mode: u32) -> Result<(), String> {
self.upload_bytes_with_ids(dir, name, data, mode, (0, 0)).await.map(|_| ())
}
}
fn tail(output: &str) -> String {
let lines: Vec<&str> = output.lines().filter(|l| !l.trim().is_empty()).collect();
lines[lines.len().saturating_sub(OUTPUT_TAIL_LINES)..].join("\n")
}
async fn run_apply<T: ConnectTarget>(t: &T, args: &[&str]) -> Result<String, String> {
let mut cmd = vec![APPLY_CONNECT.to_string()];
cmd.extend(args.iter().map(|a| a.to_string()));
let (out, code) = t.exec_root(cmd).await?;
match code {
0 => Ok(out),
// `exec` of a missing file: an image from before copy-at-connect.
126 | 127 => Err(
"This workspace's image has no triple-c-apply-connect (it predates copying \
credentials at connect); use a newer image, or store the credentials in AWS"
.into(),
),
_ => Err(format!("triple-c-apply-connect failed ({code}):\n{}", tail(&out))),
}
}
/// `--prepare`'s answer: this connect's nonce, 16 hex digits on the last line.
pub(crate) fn parse_nonce(out: &str) -> Result<String, String> {
let last = out.lines().rev().map(str::trim).find(|l| !l.is_empty()).unwrap_or_default();
if last.len() == 16 && last.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) {
Ok(last.to_string())
} else {
Err(format!("triple-c-apply-connect --prepare gave no connect id:\n{}", tail(out)))
}
}
/// Upload `bundle` and apply it (see the module docs): `--prepare` (which
/// names this connect's own staging dir), the files and `connect.env` (root
/// 0600) into that dir, then the apply of exactly that dir — so two
/// connects at once (two machines) never mix their files.
pub async fn push_connect_bundle<T: ConnectTarget>(t: &T, bundle: &ConnectBundle) -> Result<(), String> {
let nonce = parse_nonce(&run_apply(t, &["--prepare"]).await?)?;
let dir = format!("{CONNECT_STAGE}/{nonce}");
for (key, data) in &bundle.files {
validate_file_key(key)?;
let (kind, name) = key.split_once('/').expect("validated");
t.upload_root(&format!("{dir}/{kind}"), name, data, STAGED_MODE)
.await
.map_err(|e| format!("Could not copy {key} into the workspace: {e}"))?;
}
t.upload_root(&dir, CONNECT_ENV_FILE, &connect_env_file(&bundle.env), STAGED_MODE)
.await
.map_err(|e| format!("Could not copy the credential variables into the workspace: {e}"))?;
let out = run_apply(t, &[&nonce]).await?;
for line in out.lines().filter(|l| l.contains("warning")) {
log::warn!("{line}");
}
Ok(())
}
/// Copy `project`'s credentials into its connected workspace. `Ok(false)`
/// when there is nothing to do (not copy-at-connect, or no tunnel).
pub async fn apply_connect(project: &Project, settings: &AppSettings) -> Result<bool, String> {
if !copies_at_connect(project) {
return Ok(false);
}
let Some(ep) = tunnels::get_endpoint(&project.id) else {
return Ok(false);
};
let (p, s) = (project.clone(), settings.clone());
let bundle = tokio::task::spawn_blocking(move || connect_credentials(&with_secrets(&p), &s))
.await
.map_err(|e| format!("Could not gather the credentials: {e}"))??;
let target = ContainerTarget::Agent(ep);
tokio::time::timeout(PUSH_TIMEOUT, push_connect_bundle(&target, &bundle))
.await
.map_err(|_| format!("Copying the credentials took more than {} s", PUSH_TIMEOUT.as_secs()))??;
log::info!(
"Project {}: credentials copied into the workspace ({} file(s), {} variable(s))",
project.id,
bundle.files.len(),
bundle.env.len()
);
Ok(true)
}
/// After a connect: [`apply_connect`] in the background, `on_warning` on a
/// failure while the workspace is still connected (a Stop meanwhile is not
/// worth a warning). `false` (nothing spawned) unless the project copies at
/// connect and is connected.
pub fn spawn_connect_push(
project: &Project,
settings: &AppSettings,
on_warning: impl FnOnce(String) + Send + 'static,
) -> bool {
if !copies_at_connect(project) || tunnels::get_endpoint(&project.id).is_none() {
return false;
}
let (project, settings) = (project.clone(), settings.clone());
tauri::async_runtime::spawn(async move {
if let Err(e) = apply_connect(&project, &settings).await {
log::warn!("Project {}: could not copy the credentials in: {e}", project.id);
if tunnels::get_endpoint(&project.id).is_some() {
on_warning(e);
}
}
});
true
}
/// The environment a terminal the app opens adds to its exec: the
/// credential pairs for a copy-at-connect project, nothing otherwise (they
/// are already in the task's environment). Only the pairs ([`connect_env`]:
/// no SSH or CA file is read); the keychain is read off the async runtime.
pub async fn terminal_env(project: &Project, settings: &AppSettings) -> Vec<String> {
if !copies_at_connect(project) {
return Vec::new();
}
let (p, s) = (project.clone(), settings.clone());
let gathered = tokio::task::spawn_blocking(move || connect_env(&with_secrets(&p), &s))
.await
.map_err(|e| format!("could not gather them: {e}"));
terminal_env_with(project, || gathered)
}
/// [`terminal_env`] over a given source of the pairs.
pub fn terminal_env_with(
project: &Project,
credentials: impl FnOnce() -> Result<Vec<(String, SecretString)>, String>,
) -> Vec<String> {
if !copies_at_connect(project) {
return Vec::new();
}
match credentials() {
Ok(env) => exec_env(&env),
Err(e) => {
log::warn!(
"Project {}: the terminal opens without the project's credentials: {e}",
project.id
);
Vec::new()
}
}
}
+455
View File
@@ -0,0 +1,455 @@
//! An AWS workspace's clone and setup script, as the start follows them and
//! Project Home shows them (phase 6, Q4).
//!
//! The entrypoint clones in the background and then runs the setup script;
//! `triple-c-workspace-status` (container) prints both as one JSON line,
//! mirrored by [`WorkspaceStatus`].
//!
//! **The start does not wait for them** (ruling R5, amending Q4): it ends
//! after "Connecting…" as in phase 5 — project Running, project lock
//! released, terminals and Stop usable. [`spawn_follow`] then follows in the
//! background: a read through the agent every [`FOLLOW_INTERVAL`] as a
//! **background** exec (never activity for idle stop), a log line only when
//! it changes, until the clone is done and no setup runs, [`FOLLOW_LIMIT`]
//! passes, the agent is gone (tunnel dropped, task died, image without the
//! script) or its token is cancelled (`tunnels::cancel_follow` on Stop and
//! Remove; any tunnel close; app exit). Its only output is the final warning
//! ([`follow_warning`]); Project Home's Overview card polls the status itself
//! while the project is Running, so progress shows there.
use std::future::Future;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use tokio_util::sync::CancellationToken;
use super::super::{tunnels, ContainerTarget};
use crate::models::Project;
/// The in-container status reader (Task 4).
pub const STATUS_SCRIPT: &str = "/usr/local/bin/triple-c-workspace-status";
pub const FOLLOW_INTERVAL: Duration = Duration::from_secs(2);
/// The setup script's own timeout is 30 minutes, after the clone.
pub const FOLLOW_LIMIT: Duration = Duration::from_secs(35 * 60);
/// One status read through the agent.
const READ_TIMEOUT: Duration = Duration::from_secs(20);
/// Reads failing in a row before the follow gives up (quietly).
const MAX_READ_FAILURES: u32 = 5;
/// The script prints at most ~20 KB (20 tail lines of ≤ 1000 characters).
const STATUS_OUTPUT_LIMIT: usize = 256 * 1024;
/// A setup log line shown as progress is cut to this many characters.
const MAX_PROGRESS_LINE: usize = 200;
/// Setup log lines a warning carries.
const WARNING_TAIL_LINES: usize = 10;
pub const PROGRESS_CLONING: &str = "Cloning repos…";
pub const PROGRESS_SETUP: &str = "Running setup script…";
// ──────────────────────────────────────────────────────────────── model
/// `triple-c-workspace-status`'s JSON. Unknown fields are ignored and
/// missing ones default, so an older or newer image never breaks a read.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct WorkspaceStatus {
pub clone: CloneStatus,
pub setup: SetupStatus,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct CloneStatus {
/// Every repo has been tried.
pub done: bool,
/// The repos tried so far, in order.
pub repos: Vec<RepoProgress>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct RepoProgress {
pub folder: String,
/// `cloning`, `cloned`, `failed` or `exists`.
pub state: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct SetupStatus {
pub state: SetupState,
/// The script's exit code: 0 when `ok`, the code when `failed` (if known).
pub code: Option<i64>,
/// The setup log's last lines.
pub tail: Vec<String>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SetupState {
/// No setup script.
#[default]
None,
Running,
Ok,
/// Also any state this app does not know.
#[serde(other)]
Failed,
}
impl RepoProgress {
fn finished(&self) -> bool {
matches!(self.state.as_str(), "cloned" | "failed" | "exists")
}
}
impl WorkspaceStatus {
/// Nothing more will change: the clone is done and no setup is running.
pub fn settled(&self) -> bool {
self.clone.done && self.setup.state != SetupState::Running
}
pub fn failed_repos(&self) -> Vec<&str> {
self.clone
.repos
.iter()
.filter(|r| r.state == "failed")
.map(|r| r.folder.as_str())
.collect()
}
/// The start's progress line for this state (`None` once settled).
/// `total` is how many repos the project configures; the status only
/// lists the ones started so far.
pub fn progress_line(&self, total: usize) -> Option<String> {
if !self.clone.done {
let n = total.max(self.clone.repos.len());
if n == 0 {
return Some(PROGRESS_CLONING.into());
}
let finished = self.clone.repos.iter().filter(|r| r.finished()).count();
return Some(format!("{PROGRESS_CLONING} ({}/{n})", (finished + 1).min(n)));
}
(self.setup.state == SetupState::Running).then(|| PROGRESS_SETUP.into())
}
/// The setup log's last non-blank line, cleaned for a progress line.
fn last_log_line(&self) -> Option<String> {
self.setup
.tail
.iter()
.rev()
.map(|l| clean_line(l))
.find(|l| !l.is_empty())
}
/// What went wrong, for the user; `None` when nothing did.
pub fn problems(&self) -> Option<String> {
let mut parts = Vec::new();
let failed = self.failed_repos();
if !failed.is_empty() {
parts.push(format!(
"Cloning failed for {}: {}. The workspace started without {}.",
if failed.len() == 1 { "a repo" } else { "some repos" },
failed.join(", "),
if failed.len() == 1 { "it" } else { "them" },
));
}
if self.setup.state == SetupState::Failed {
let code = self.setup.code.map(|c| format!(" (exit {c})")).unwrap_or_default();
let mut s = format!("The setup script failed{code}.");
let tail: Vec<&String> = self.setup.tail.iter().rev().take(WARNING_TAIL_LINES).collect();
if !tail.is_empty() {
s.push_str("\nLast lines:\n");
s.push_str(
&tail.into_iter().rev().map(String::as_str).collect::<Vec<_>>().join("\n"),
);
}
parts.push(s);
}
(!parts.is_empty()).then(|| parts.join("\n\n"))
}
}
/// One line of the setup log as a progress message: control characters
/// dropped, trimmed, at most [`MAX_PROGRESS_LINE`] characters.
fn clean_line(line: &str) -> String {
let cleaned: String = line
.chars()
.map(|c| if c == '\t' { ' ' } else { c })
.filter(|c| !c.is_control())
.collect();
let t = cleaned.trim();
if t.chars().count() > MAX_PROGRESS_LINE {
let mut s: String = t.chars().take(MAX_PROGRESS_LINE - 1).collect();
s.push('…');
s
} else {
t.to_string()
}
}
/// Parse the script's output: its last non-blank line is the JSON (anything
/// before it — a stray warning — is ignored).
pub fn parse_status(output: &str) -> Result<WorkspaceStatus, String> {
let line = output
.lines()
.rev()
.find(|l| !l.trim().is_empty())
.ok_or("The workspace status was empty")?;
serde_json::from_str(line.trim()).map_err(|e| format!("The workspace status could not be read: {e}"))
}
// ───────────────────────────────────────────────────────────── reading
/// One read of the status.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StatusRead {
Status(WorkspaceStatus),
/// No agent to ask (tunnel gone), or an image without the script.
Gone,
/// A read that failed; may be retried.
Failed(String),
}
pub trait StatusSource: Send + Sync {
fn read(&self) -> impl Future<Output = StatusRead> + Send;
}
/// The command that runs the script; exit 127 when the image predates it.
fn status_command() -> Vec<String> {
vec![
"sh".into(),
"-c".into(),
format!("[ -x {STATUS_SCRIPT} ] || exit 127; exec {STATUS_SCRIPT}"),
]
}
/// Read the status through `target`, as a background exec.
pub async fn read_status(target: &ContainerTarget) -> StatusRead {
let res = tokio::time::timeout(
READ_TIMEOUT,
target.exec_oneshot_background("claude", status_command(), STATUS_OUTPUT_LIMIT),
)
.await;
match res {
Err(_) => StatusRead::Failed("The workspace status read timed out".into()),
Ok(Err(e)) => StatusRead::Failed(e),
Ok(Ok((out, 0))) => match parse_status(&out) {
Ok(s) => StatusRead::Status(s),
Err(e) => StatusRead::Failed(e),
},
Ok(Ok((_, 126 | 127))) => StatusRead::Gone,
Ok(Ok((_, code))) => StatusRead::Failed(format!("The workspace status exited with {code}")),
}
}
/// The project's registered tunnel, looked up on every read: a stop or a
/// dropped tunnel unregisters it, and the follow then ends.
struct TunnelStatusSource<'a> {
project_id: &'a str,
}
impl StatusSource for TunnelStatusSource<'_> {
async fn read(&self) -> StatusRead {
match tunnels::get_endpoint(self.project_id) {
Some(ep) => read_status(&ContainerTarget::Agent(ep)).await,
None => StatusRead::Gone,
}
}
}
/// The status of an AWS project's workspace for Project Home: `None` for a
/// local project, one with no tunnel, or an image without the script.
pub async fn workspace_status(project: &Project) -> Result<Option<WorkspaceStatus>, String> {
if !project.is_aws() {
return Ok(None);
}
match (TunnelStatusSource { project_id: &project.id }).read().await {
StatusRead::Status(s) => Ok(Some(s)),
StatusRead::Gone => Ok(None),
StatusRead::Failed(e) => Err(e),
}
}
// ──────────────────────────────────────────────────────────── following
#[derive(Debug, Clone, Copy)]
pub struct FollowTiming {
pub interval: Duration,
pub limit: Duration,
}
impl Default for FollowTiming {
fn default() -> Self {
Self { interval: FOLLOW_INTERVAL, limit: FOLLOW_LIMIT }
}
}
/// How a follow ended.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FollowEnd {
Settled {
status: WorkspaceStatus,
/// The very first read was already settled: nothing happened in
/// this follow (an attach to a workspace that finished long ago).
first_read: bool,
},
/// [`FollowTiming::limit`] passed; the last status read, if any.
TimedOut(Option<WorkspaceStatus>),
/// The agent went away, kept failing, or the follow was cancelled:
/// nothing to say.
Lost,
}
/// Follow `src` until it settles, the limit passes, it is lost or `cancel`
/// fires, calling `progress` with each new progress line and each new setup
/// log line.
pub async fn follow_workspace_status<S: StatusSource>(
src: &S,
total_repos: usize,
timing: FollowTiming,
cancel: &CancellationToken,
progress: &(dyn Fn(&str) + Send + Sync),
) -> FollowEnd {
tokio::select! {
biased;
_ = cancel.cancelled() => FollowEnd::Lost,
end = follow_inner(src, total_repos, timing, progress) => end,
}
}
async fn follow_inner<S: StatusSource>(
src: &S,
total_repos: usize,
timing: FollowTiming,
progress: &(dyn Fn(&str) + Send + Sync),
) -> FollowEnd {
let deadline = tokio::time::Instant::now() + timing.limit;
let mut last_line: Option<String> = None;
let mut last_log: Option<String> = None;
let mut last_status: Option<WorkspaceStatus> = None;
let mut failures = 0u32;
let mut reads = 0u32;
loop {
match src.read().await {
StatusRead::Status(s) => {
failures = 0;
reads += 1;
if s.settled() {
return FollowEnd::Settled { status: s, first_read: reads == 1 };
}
if let Some(line) = s.progress_line(total_repos) {
if last_line.as_deref() != Some(line.as_str()) {
progress(&line);
last_line = Some(line);
}
}
if s.setup.state == SetupState::Running {
if let Some(log) = s.last_log_line() {
if last_log.as_deref() != Some(log.as_str()) {
progress(&log);
last_log = Some(log);
}
}
}
last_status = Some(s);
}
StatusRead::Gone => return FollowEnd::Lost,
StatusRead::Failed(e) => {
failures += 1;
log::debug!("Workspace status read failed ({failures}): {e}");
if failures >= MAX_READ_FAILURES {
log::info!("Stopped following the workspace's start: {e}");
return FollowEnd::Lost;
}
}
}
if tokio::time::Instant::now() + timing.interval > deadline {
return FollowEnd::TimedOut(last_status);
}
tokio::time::sleep(timing.interval).await;
}
}
/// What the user is told after a follow; `None` when all went well, the
/// follow was lost, or the workspace had settled before this start —
/// a failure is reported only when this follow saw it happen, or the
/// task is one this start ran (`created`). Project Home still shows an old
/// failure on demand.
pub fn follow_warning(end: &FollowEnd, limit: Duration, created: bool) -> Option<String> {
match end {
FollowEnd::Settled { status, first_read } => {
if *first_read && !created {
None
} else {
status.problems()
}
}
FollowEnd::TimedOut(s) => {
let what = match s {
Some(s) if !s.clone.done => "still cloning its repos",
_ => "still running its setup script",
};
let mut msg = format!(
"The workspace is {what} after {} minutes. It is running; Project Home shows the \
progress.",
limit.as_secs() / 60
);
if let Some(p) = s.as_ref().and_then(WorkspaceStatus::problems) {
msg.push_str("\n\n");
msg.push_str(&p);
}
Some(msg)
}
FollowEnd::Lost => None,
}
}
/// The follow of one project's workspace. `follower` is its registration
/// with the tunnel registry (`tunnels::begin_follow`), made by the caller
/// before the follow is spawned so registrations keep the order of the starts
/// (a quick Stop→Start can't leave the old follower registered last).
/// `on_warning` gets the warning, if any.
pub async fn run_follow<S: StatusSource>(
src: &S,
project_id: &str,
follower: (u64, CancellationToken),
total_repos: usize,
created: bool,
timing: FollowTiming,
on_warning: impl FnOnce(String),
) {
let (id, cancel) = follower;
let log_line = |m: &str| log::debug!("Project {project_id}: {m}");
let end = follow_workspace_status(src, total_repos, timing, &cancel, &log_line).await;
tunnels::end_follow(project_id, id);
if let Some(w) = follow_warning(&end, timing.limit, created) {
on_warning(w);
}
}
/// After a successful start of an AWS project: follow its clone and setup
/// script in the background (see the module docs). `false` (nothing
/// spawned) for a local project or one with no tunnel.
pub fn spawn_follow(
project: &Project,
on_warning: impl FnOnce(String) + Send + 'static,
) -> bool {
if !project.is_aws() || tunnels::get_endpoint(&project.id).is_none() {
return false;
}
let project_id = project.id.clone();
let total = project.aws.as_ref().map_or(0, |a| a.repos.len());
let created = tunnels::ran_task(&project_id);
// Registered now, not in the spawned task: see `run_follow`.
let follower = tunnels::begin_follow(&project_id);
tauri::async_runtime::spawn(async move {
let src = TunnelStatusSource { project_id: &project_id };
run_follow(&src, &project_id, follower, total, created, FollowTiming::default(), on_warning)
.await;
});
true
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,285 @@
use std::sync::Mutex;
use super::*;
use crate::host::tunnels;
/// Returns its scripted reads in order, then repeats the last one.
struct Scripted {
reads: Mutex<Vec<StatusRead>>,
count: Mutex<usize>,
}
impl Scripted {
fn new(reads: Vec<StatusRead>) -> Self {
Self { reads: Mutex::new(reads), count: Mutex::new(0) }
}
fn count(&self) -> usize {
*self.count.lock().unwrap()
}
}
impl StatusSource for Scripted {
async fn read(&self) -> StatusRead {
*self.count.lock().unwrap() += 1;
let mut r = self.reads.lock().unwrap();
if r.len() > 1 {
r.remove(0)
} else {
r[0].clone()
}
}
}
fn st(json: &str) -> StatusRead {
StatusRead::Status(parse_status(json).unwrap())
}
async fn follow(src: &Scripted, total: usize) -> (FollowEnd, Vec<String>) {
let lines = Mutex::new(Vec::new());
let progress = |m: &str| lines.lock().unwrap().push(m.to_string());
let cancel = CancellationToken::new();
let end = follow_workspace_status(src, total, FollowTiming::default(), &cancel, &progress).await;
(end, lines.into_inner().unwrap())
}
const NOT_STARTED: &str = r#"{"clone":{"done":false,"repos":[]},"setup":{"state":"running","code":null,"tail":[]}}"#;
const CLONING_1: &str = r#"{"clone":{"done":false,"repos":[{"folder":"a","state":"cloning"}]},"setup":{"state":"running","code":null,"tail":[]}}"#;
const CLONING_2: &str = r#"{"clone":{"done":false,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"cloning"}]},"setup":{"state":"running","code":null,"tail":[]}}"#;
const SETUP_1: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"failed"}]},"setup":{"state":"running","code":null,"tail":["Reading package lists...",""," Setting up jq "]}}"#;
const SETUP_2: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"failed"}]},"setup":{"state":"running","code":null,"tail":["Setting up jq","done"]}}"#;
const SETUP_FAILED: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"failed"}]},"setup":{"state":"failed","code":2,"tail":["Setting up jq","E: oops"]}}"#;
const ALL_OK: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"}]},"setup":{"state":"none","code":null,"tail":[]}}"#;
#[tokio::test(start_paused = true)]
async fn follow_emits_each_change_once_and_stops_when_settled() {
let src = Scripted::new(vec![
st(NOT_STARTED),
st(CLONING_1),
st(CLONING_1),
st(CLONING_2),
st(SETUP_1),
st(SETUP_1),
st(SETUP_2),
st(SETUP_FAILED),
]);
let started = tokio::time::Instant::now();
let (end, lines) = follow(&src, 2).await;
assert_eq!(
lines,
[
"Cloning repos… (1/2)",
"Cloning repos… (2/2)",
"Running setup script…",
"Setting up jq",
"done",
]
);
assert_eq!(src.count(), 8);
// Seven waits of 2 s between eight reads.
assert_eq!(started.elapsed(), FOLLOW_INTERVAL * 7);
let FollowEnd::Settled { status: s, first_read: false } = &end else { panic!("{end:?}") };
// Seen failing in this follow: reported even on an attach.
let w = follow_warning(&end, FOLLOW_LIMIT, false).unwrap();
assert!(w.contains("Cloning failed for a repo: b."), "{w}");
assert!(w.contains("The setup script failed (exit 2)."), "{w}");
assert!(w.ends_with("Last lines:\nSetting up jq\nE: oops"), "{w}");
assert_eq!(s.failed_repos(), ["b"]);
}
#[tokio::test(start_paused = true)]
async fn a_workspace_already_settled_ends_at_once_without_a_warning() {
let src = Scripted::new(vec![st(ALL_OK)]);
let started = tokio::time::Instant::now();
let (end, lines) = follow(&src, 1).await;
assert!(lines.is_empty());
assert_eq!(started.elapsed(), std::time::Duration::ZERO);
assert_eq!(follow_warning(&end, FOLLOW_LIMIT, true), None);
}
#[tokio::test(start_paused = true)]
async fn an_old_failure_warns_only_for_a_task_this_start_ran() {
let src = Scripted::new(vec![st(SETUP_FAILED)]);
let (end, lines) = follow(&src, 2).await;
assert!(lines.is_empty());
assert!(matches!(end, FollowEnd::Settled { first_read: true, .. }), "{end:?}");
// Attached to a workspace that failed long ago: no toast.
assert_eq!(follow_warning(&end, FOLLOW_LIMIT, false), None);
// This start ran the task and it failed before the first read.
let w = follow_warning(&end, FOLLOW_LIMIT, true).unwrap();
assert!(w.contains("The setup script failed (exit 2)."), "{w}");
}
#[tokio::test(start_paused = true)]
async fn follow_times_out_after_35_minutes() {
let src = Scripted::new(vec![st(CLONING_1)]);
let started = tokio::time::Instant::now();
let (end, lines) = follow(&src, 3).await;
assert_eq!(lines, ["Cloning repos… (1/3)"]);
assert!(matches!(end, FollowEnd::TimedOut(Some(_))), "{end:?}");
assert!(started.elapsed() <= FOLLOW_LIMIT);
assert!(started.elapsed() > FOLLOW_LIMIT - FOLLOW_INTERVAL * 2);
let w = follow_warning(&end, FOLLOW_LIMIT, false).unwrap();
assert!(w.contains("still cloning its repos after 35 minutes"), "{w}");
}
#[tokio::test(start_paused = true)]
async fn a_timed_out_setup_says_so_and_carries_any_clone_failure() {
let src = Scripted::new(vec![st(SETUP_2)]);
let (end, _) = follow(&src, 2).await;
let w = follow_warning(&end, FOLLOW_LIMIT, false).unwrap();
assert!(w.contains("still running its setup script"), "{w}");
assert!(w.contains("Cloning failed for a repo: b."), "{w}");
}
#[tokio::test(start_paused = true)]
async fn a_lost_agent_ends_the_follow_quietly() {
let src = Scripted::new(vec![st(CLONING_1), StatusRead::Gone]);
let (end, lines) = follow(&src, 1).await;
assert_eq!(end, FollowEnd::Lost);
assert_eq!(lines, ["Cloning repos… (1/1)"]);
assert_eq!(follow_warning(&end, FOLLOW_LIMIT, false), None);
}
#[tokio::test(start_paused = true)]
async fn failing_reads_are_retried_then_give_up_quietly() {
let fail = || StatusRead::Failed("tunnel hiccup".into());
// Four failures, then a good read: the count resets.
let src = Scripted::new(vec![fail(), fail(), fail(), fail(), st(ALL_OK)]);
assert!(matches!(follow(&src, 1).await.0, FollowEnd::Settled { .. }));
let src = Scripted::new(vec![fail()]);
let (end, _) = follow(&src, 1).await;
assert_eq!(end, FollowEnd::Lost);
assert_eq!(src.count(), MAX_READ_FAILURES as usize);
}
#[test]
fn status_parses_the_scripts_output_and_tolerates_drift() {
let s = parse_status(&format!("a stray warning\n{SETUP_FAILED}\n\n")).unwrap();
assert_eq!(s.setup.state, SetupState::Failed);
assert_eq!(s.setup.code, Some(2));
assert!(s.clone.done);
// Unknown fields and states, missing fields.
let s = parse_status(r#"{"clone":{"done":true,"extra":1},"setup":{"state":"paused"},"new":{}}"#)
.unwrap();
assert!(s.clone.repos.is_empty());
assert_eq!(s.setup.state, SetupState::Failed);
assert_eq!(parse_status("{}").unwrap(), WorkspaceStatus::default());
assert!(parse_status("").is_err());
assert!(parse_status("not json").is_err());
}
#[test]
fn progress_lines_count_repos_and_clean_log_lines() {
let s = parse_status(NOT_STARTED).unwrap();
assert_eq!(s.progress_line(0).as_deref(), Some("Cloning repos…"));
assert_eq!(s.progress_line(3).as_deref(), Some("Cloning repos… (1/3)"));
let s = parse_status(ALL_OK).unwrap();
assert!(s.settled());
assert_eq!(s.progress_line(1), None);
let mut s = parse_status(SETUP_2).unwrap();
s.setup.tail = vec![format!("\x1b[31m{}\x1b[0m", "x".repeat(500))];
let l = s.last_log_line().unwrap();
assert_eq!(l.chars().count(), MAX_PROGRESS_LINE);
assert!(l.starts_with("[31mxxx") && l.ends_with('…'), "{l}");
assert!(!l.contains('\x1b'));
}
#[test]
fn status_serialises_in_the_scripts_shape() {
let s = parse_status(SETUP_FAILED).unwrap();
let v = serde_json::to_value(&s).unwrap();
assert_eq!(v, serde_json::from_str::<serde_json::Value>(SETUP_FAILED).unwrap());
}
#[test]
fn the_status_command_reports_a_missing_script_as_127() {
let cmd = status_command();
assert_eq!(cmd[..2], ["sh", "-c"]);
assert!(cmd[2].contains("|| exit 127") && cmd[2].contains(STATUS_SCRIPT));
}
#[tokio::test]
async fn a_local_project_has_no_workspace_status_and_no_follower() {
let p = Project::new("p".into(), vec![]);
assert_eq!(workspace_status(&p).await, Ok(None));
assert!(!spawn_follow(&p, |_| panic!("no warning for Docker")));
assert!(!tunnels::is_following(&p.id));
}
/// Never answers: a follow in progress until something ends it.
struct Hang;
impl StatusSource for Hang {
async fn read(&self) -> StatusRead {
std::future::pending().await
}
}
#[tokio::test(start_paused = true)]
async fn stop_ends_a_follow_in_progress_without_a_warning() {
let id = "follow-stop-test";
let task = tokio::spawn(async move {
let warned = std::sync::Arc::new(Mutex::new(false));
let w = warned.clone();
let follower = tunnels::begin_follow(id);
run_follow(&Hang, id, follower, 1, true, FollowTiming::default(), move |_| {
*w.lock().unwrap() = true
})
.await;
let v = *warned.lock().unwrap();
v
});
tokio::time::sleep(FOLLOW_INTERVAL * 3).await;
assert!(tunnels::is_following(id));
// What Stop and Remove call (`stop_project_container`, `stop_project`,
// `remove_resources`); closing the tunnel does the same.
tunnels::cancel_follow(id);
let warned = tokio::time::timeout(std::time::Duration::from_secs(1), task)
.await
.expect("the follow ends at once")
.unwrap();
assert!(!warned);
assert!(!tunnels::is_following(id));
}
#[tokio::test(start_paused = true)]
async fn a_new_follow_replaces_the_old_one() {
let id = "follow-replace-test";
let follower = tunnels::begin_follow(id);
let first = tokio::spawn(run_follow(&Hang, id, follower, 1, true, FollowTiming::default(), |_| {}));
tokio::time::sleep(FOLLOW_INTERVAL).await;
let (_, second) = tunnels::begin_follow(id);
tokio::time::timeout(std::time::Duration::from_secs(1), first).await.unwrap().unwrap();
// The old follower's end does not unregister the new one.
assert!(tunnels::is_following(id));
assert!(!second.is_cancelled());
tunnels::cancel_follow(id);
assert!(second.is_cancelled());
}
#[tokio::test(start_paused = true)]
async fn followers_registered_in_start_order_win_in_that_order_however_they_are_scheduled() {
// A quick Stop→Start: the old start's follower is registered first, the
// new one second, but the new one's task happens to run first.
let id = "follow-order-test";
let old = tunnels::begin_follow(id);
let new = tunnels::begin_follow(id);
let new_token = new.1.clone();
let new_task = tokio::spawn(run_follow(&Hang, id, new, 1, true, FollowTiming::default(), |_| {}));
tokio::time::sleep(FOLLOW_INTERVAL).await;
let old_task = tokio::spawn(run_follow(&Hang, id, old, 1, true, FollowTiming::default(), |_| {}));
// The old one was cancelled at registration: it ends at once and leaves
// the new one registered and running.
tokio::time::timeout(std::time::Duration::from_secs(1), old_task).await.unwrap().unwrap();
assert!(tunnels::is_following(id));
assert!(!new_token.is_cancelled());
assert!(!new_task.is_finished());
tunnels::cancel_follow(id);
tokio::time::timeout(std::time::Duration::from_secs(1), new_task).await.unwrap().unwrap();
}
#[test]
fn the_follower_takes_no_project_lock() {
// Stop must never wait for it (ruling R5).
let src = include_str!("../status.rs");
assert!(!src.contains("project_lock"));
}
File diff suppressed because it is too large. Load diff
+66 -8
View File
@@ -17,12 +17,15 @@ use super::ContainerTarget;
use crate::docker;
use crate::models::{AppSettings, Project};
use crate::models::container_config;
use crate::storage::projects_store::ProjectsStore;
/// The host a project runs on. One variant today; an AWS variant will carry
/// the connection, region and environment (see the spec's data model).
/// The host a project runs on: the local Docker daemon, or an ECS Fargate
/// task in an AWS environment (`aws_host.rs`; the connection, region and
/// environment come from the project).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProjectHost {
Docker,
Aws,
}
/// An agent target is a way *into* a container, not a handle on its
@@ -41,8 +44,12 @@ pub struct DiscardLeftovers {
}
impl ProjectHost {
pub fn for_project(_project: &Project) -> Self {
ProjectHost::Docker
pub fn for_project(project: &Project) -> Self {
if project.is_aws() {
ProjectHost::Aws
} else {
ProjectHost::Docker
}
}
/// Make sure the project's container exists, matches the project's
@@ -50,16 +57,20 @@ impl ProjectHost {
///
/// `progress` receives the same human-readable steps the start view has
/// always shown. Failure leaves the caller to reset the project status.
/// `store`: the AWS host records a task's ARN in it as soon as the task
/// exists, so a crash during the long wait leaves it findable.
pub async fn ensure_started(
&self,
project: &Project,
settings: &AppSettings,
store: &ProjectsStore,
progress: &(dyn Fn(&str) + Send + Sync),
) -> Result<ContainerTarget, String> {
match self {
ProjectHost::Docker => docker_ensure_started(project, settings, progress)
.await
.map(ContainerTarget::docker),
ProjectHost::Aws => super::aws_host::ensure_started(project, settings, store, progress).await,
}
}
@@ -72,24 +83,66 @@ impl ProjectHost {
ProjectHost::Docker => Ok(docker::find_existing_container(project)
.await?
.map(ContainerTarget::docker)),
// The tunnel registry: an AWS project's target exists while its
// tunnel is open.
ProjectHost::Aws => Ok(ContainerTarget::for_project(project)),
}
}
/// Stop the container behind `target`. For an agent target that is the
/// ECS task behind a registered tunnel (`StopTask`, reason "Stopped from
/// Triple-C"), and the tunnel is closed.
pub async fn stop(&self, target: &ContainerTarget) -> Result<(), String> {
match target {
ContainerTarget::Docker { container_id } => docker::stop_container(container_id).await,
ContainerTarget::Agent(_) => Err(AGENT_LIFECYCLE.replace("{op}", "Stopping")),
ContainerTarget::Agent(ep) => super::aws_host::stop_endpoint(ep)
.await
.unwrap_or_else(|| Err(AGENT_LIFECYCLE.replace("{op}", "Stopping"))),
}
}
/// Stop a project's container whether or not the app holds a target for
/// it. Docker: the stored container. AWS: the tunnel's task, else the
/// stored task ARN, else the project's live task — a project whose
/// tunnel could not be re-opened must still be stoppable.
pub async fn stop_project(&self, project: &Project, settings: &AppSettings) -> Result<(), String> {
match self {
ProjectHost::Docker => match ContainerTarget::for_project(project) {
Some(t) => self.stop(&t).await,
None => Ok(()),
},
ProjectHost::Aws => super::aws_host::stop_project(project, settings).await,
}
}
/// Stop (errors ignored — a stopped container is the goal) and remove.
/// An agent target is stopped; its resources go with
/// [`Self::remove_aws_resources`].
pub async fn remove(&self, target: &ContainerTarget) -> Result<(), String> {
match target {
ContainerTarget::Docker { container_id } => {
let _ = docker::stop_container(container_id).await;
docker::remove_container(container_id).await
}
ContainerTarget::Agent(_) => Err(AGENT_LIFECYCLE.replace("{op}", "Removing")),
ContainerTarget::Agent(ep) => super::aws_host::stop_endpoint(ep)
.await
.unwrap_or_else(|| Err(AGENT_LIFECYCLE.replace("{op}", "Removing"))),
}
}
/// AWS: stop the task and remove the project's resources — task
/// definition revisions, the secret (deleted at once) and, only
/// with `discard_data`, the EFS access points, home and EFS-mode `/workspace` (not their files). Docker: nothing (its state
/// goes with [`Self::discard_project_state`]).
pub async fn remove_aws_resources(
&self,
project: &Project,
settings: &AppSettings,
discard_data: bool,
) -> Result<(), String> {
match self {
ProjectHost::Docker => Ok(()),
ProjectHost::Aws => super::aws_host::remove_resources(project, settings, discard_data).await,
}
}
@@ -107,6 +160,8 @@ impl ProjectHost {
left.volumes = docker::remove_project_volumes(project).await;
left
}
// Removed by `remove_aws_resources`, which needs the settings.
ProjectHost::Aws => DiscardLeftovers::default(),
}
}
@@ -118,6 +173,7 @@ impl ProjectHost {
docker::remove_legacy_mcp_containers(&project.id).await;
docker::remove_legacy_project_network(&project.id).await;
}
ProjectHost::Aws => {}
}
}
}
@@ -277,6 +333,8 @@ mod tests {
async fn a_project_container_round_trips_through_the_host() {
let project = Project::new("lifecycle-live-test".to_string(), Vec::new());
let settings = AppSettings::default();
let store_dir = tempfile::tempdir().expect("temp dir");
let store = ProjectsStore::over_file_for_test(store_dir.path().join("projects.json"), Vec::new());
let host = ProjectHost::for_project(&project);
let steps = std::sync::Mutex::new(Vec::<String>::new());
let progress = |m: &str| steps.lock().unwrap().push(m.to_string());
@@ -289,7 +347,7 @@ mod tests {
let outcome = async {
check(host.find_current(&project).await?.is_none(), "no container before the first start")?;
let first = host.ensure_started(&project, &settings, &progress).await?;
let first = host.ensure_started(&project, &settings, &store, &progress).await?;
check(first.is_running().await?, "running after start")?;
let (out, code) = first
.exec_oneshot_env_status(vec!["id".into(), "-un".into()], vec![])
@@ -301,7 +359,7 @@ mod tests {
check(first.has_exited().await, "exited after stop")?;
// Same config, so the existing container is started, not recreated.
let second = host.ensure_started(&project, &settings, &progress).await?;
let second = host.ensure_started(&project, &settings, &store, &progress).await?;
check(second == first, "restart reuses the container")?;
check(second.is_running().await?, "running after restart")?;
+54 -4
View File
@@ -28,12 +28,12 @@
//! may still do so.
pub mod agent;
pub mod aws_host;
pub mod lifecycle;
pub mod port_forwards;
pub mod sessions;
// Nothing in the app opens an SSM tunnel until the AWS host lands (phase 5 of
// the AWS spec); its tests exercise it meanwhile. Remove this allow then.
#[allow(dead_code)]
pub mod ssm;
pub mod tunnels;
pub mod watch;
pub use crate::docker::exec::{
@@ -154,11 +154,28 @@ impl ContainerTarget {
}
}
/// The target for a project's container, if it has one yet.
/// The target for a project's container, if it has one yet. An AWS
/// project's is its registered tunnel's agent endpoint (`tunnels.rs`):
/// none while no tunnel is open.
pub fn for_project(project: &Project) -> Option<Self> {
if project.is_aws() {
return tunnels::get_endpoint(&project.id).map(ContainerTarget::Agent);
}
project.container_id.as_deref().map(Self::docker)
}
/// [`Self::for_project`], waiting (up to `aws_host::REATTACH_WAIT`) for an
/// AWS project whose tunnel is being re-opened in the background after a
/// restart. For a Docker project it is `for_project`.
pub async fn for_project_waiting(project: &Project) -> Option<Self> {
if project.is_aws() {
return tunnels::wait_endpoint(&project.id, aws_host::REATTACH_WAIT)
.await
.map(ContainerTarget::Agent);
}
Self::for_project(project)
}
/// The host-specific identifier (a Docker container id today). For logs
/// and for keying per-container state; never parse it.
pub fn id(&self) -> &str {
@@ -254,6 +271,23 @@ impl ContainerTarget {
}
}
/// [`Self::exec_oneshot_limited`] for a background poll: on the agent it
/// carries the background marker, so it never counts as activity for
/// idle stop. Docker has no idle stop; the call is the same there.
pub async fn exec_oneshot_background(
&self,
user: &str,
cmd: Vec<String>,
limit: usize,
) -> Result<(String, i64), String> {
match self {
ContainerTarget::Docker { container_id } => {
docker::exec::exec_oneshot_limited(container_id, user, cmd, limit).await
}
ContainerTarget::Agent(ep) => agent::exec_oneshot_background(ep, user, cmd, limit).await,
}
}
/// stdout and stderr kept apart: `(stdout, stderr, exit_code)`.
pub async fn exec_oneshot_streams_as(
&self,
@@ -269,6 +303,22 @@ impl ContainerTarget {
}
}
/// [`Self::exec_oneshot_streams_as`] for a background poll (the file
/// viewer's change check): on the agent it carries the background marker,
/// so it never counts as activity for idle stop. The same call on Docker.
pub async fn exec_oneshot_streams_background(
&self,
user: &str,
cmd: Vec<String>,
) -> Result<(String, String, i64), String> {
match self {
ContainerTarget::Docker { container_id } => {
docker::exec::exec_oneshot_streams_as(container_id, user, cmd, Vec::new()).await
}
ContainerTarget::Agent(ep) => agent::exec_oneshot_streams_background(ep, user, cmd).await,
}
}
/// Start a command and return without waiting or reading its output.
/// Returns the exec id.
pub async fn exec_detached(&self, spec: ExecSpec) -> Result<String, String> {
+390
View File
@@ -0,0 +1,390 @@
//! A project's port mappings on a host with no Docker port publishing (AWS).
//!
//! Docker publishes a mapping itself (`0.0.0.0:<host>` → the container's
//! network interface). A Fargate task has no such thing, so each TCP mapping
//! becomes a host **loopback** listener (`127.0.0.1` and `::1`, never a
//! wildcard) carried into the task over `socat` on an attached exec — the
//! auth bridge's [`PortForward`], which works through the agent like any
//! other exec (constraints Q6). Nothing is published beyond this machine,
//! and the UI says so.
//!
//! ## Two differences from Docker, both deliberate
//!
//! * **It reaches the workspace's loopback, not its network interface**
//! (`socat … TCP:127.0.0.1:<port>` inside the task). So a mapping can
//! reach a service that bound loopback precisely to be private — which is
//! why the agent's port, the browser view's range and (when the project
//! runs it) the gateway sidecar's [`GATEWAY_SIDECAR_PORT`] are refused.
//! * **No gate** ([`PortForward::bind_mapping`]): unlike the auth bridge, a
//! client need not speak first and an HTTP request is not checked for a
//! cross-site origin — Docker's publishing has neither, and a
//! server-speaks-first protocol (MySQL, SMTP) must work. What a mapping
//! exposes is what the user chose to expose, to this machine only.
//!
//! The tunnel's bounds still apply: 16 concurrent connections per mapping,
//! and a connection the workspace sends nothing on for 600 s is closed.
//!
//! ## Lifetime
//!
//! One set per project, opened after every connect (the start, and the
//! background re-attach after an app restart) and on a save of the mappings
//! while connected. Opens are serialised per project and re-read the stored
//! mappings under that lock, so the last save wins; an open keeps the
//! forwards it already has for an unchanged mapping (with their live
//! connections) and rebinds everything when the tunnel changed. The set is
//! closed with the project's tunnel — `tunnels::close`, `close_if_task` (the
//! task died) and `close_all` (app exit) call [`close`] / [`close_all`] — so
//! Stop, Remove, a task death and exit need nothing of their own.
//!
//! ## Idle stop
//!
//! An open listener costs the workspace nothing: no agent request is made
//! until a client connects. A connection is an attach, which counts as
//! activity while bytes flow — a client using a mapping keeps the workspace
//! from its idle stop; one connected but silent does not.
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use crate::auth_bridge::proc_net::PortFamily;
use crate::auth_bridge::tunnel::PortForward;
use crate::auth_bridge::{RESERVED_CONTAINER_PORTS, RESERVED_HOST_PORTS};
use crate::models::PortMapping;
use crate::storage::projects_store::ProjectsStore;
use super::ContainerTarget;
/// What became of one mapping: `error` is `None` when its forward is open.
#[derive(Debug, Clone, PartialEq)]
pub struct PortForwardResult {
pub mapping: PortMapping,
pub error: Option<String>,
}
/// The container port the gateway sidecar (Task 8, `gateway_sidecar`)
/// listens on, on the workspace's loopback.
pub const GATEWAY_SIDECAR_PORT: u16 = 4000;
/// A project's open forwards and the target (tunnel endpoint) they lead to.
struct OpenSet {
target_id: String,
forwards: Vec<PortForward>,
}
type Registry = HashMap<String, OpenSet>;
fn registry() -> &'static Mutex<Registry> {
static R: std::sync::OnceLock<Mutex<Registry>> = std::sync::OnceLock::new();
R.get_or_init(Default::default)
}
fn lock() -> std::sync::MutexGuard<'static, Registry> {
registry().lock().unwrap_or_else(|e| e.into_inner())
}
/// The per-project lock every open and close holds, so two opens (a save on
/// every blur, a save during a start's open) never interleave.
fn project_lock(project_id: &str) -> Arc<tokio::sync::Mutex<()>> {
static L: std::sync::OnceLock<Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>> =
std::sync::OnceLock::new();
L.get_or_init(Default::default)
.lock()
.unwrap_or_else(|e| e.into_inner())
.entry(project_id.to_string())
.or_default()
.clone()
}
/// Why one mapping can't be forwarded to an AWS workspace, if it can't.
/// `None` when it is fine.
fn mapping_problem(m: &PortMapping, gateway_sidecar: bool) -> Option<String> {
if !m.protocol.eq_ignore_ascii_case("tcp") {
return Some(format!(
"{} → {} ({}): only TCP ports can be forwarded to an AWS workspace",
m.host_port,
m.container_port,
m.protocol.to_ascii_uppercase()
));
}
if RESERVED_CONTAINER_PORTS.contains(&m.container_port) {
return Some(format!(
"Container port {} is reserved for the browser view ({}–{})",
m.container_port,
RESERVED_CONTAINER_PORTS.start(),
RESERVED_CONTAINER_PORTS.end()
));
}
if m.container_port == super::ssm::AGENT_PORT {
return Some(format!(
"Container port {} is the workspace agent's own port",
super::ssm::AGENT_PORT
));
}
if gateway_sidecar && m.container_port == GATEWAY_SIDECAR_PORT {
return Some(format!(
"Container port {GATEWAY_SIDECAR_PORT} is the gateway sidecar's, which holds the \
model provider's key"
));
}
if RESERVED_HOST_PORTS.contains(&m.host_port) {
return Some(format!(
"Host port {} is reserved for the browser view ({}–{})",
m.host_port,
RESERVED_HOST_PORTS.start(),
RESERVED_HOST_PORTS.end()
));
}
None
}
/// A row the editor saved before both its ports were typed (the editor
/// saves on blur): nothing to check or forward yet, and nothing to say.
fn incomplete(m: &PortMapping) -> bool {
m.host_port == 0 || m.container_port == 0
}
/// An AWS project's port mappings, checked in full: TCP only, no reserved
/// port (the browser view's two ranges, the agent's port, the gateway
/// sidecar's when it runs), and no host port twice. The first problem,
/// worded for the user.
pub fn validate_aws_port_mappings(mappings: &[PortMapping], gateway_sidecar: bool) -> Result<(), String> {
validate_changed(mappings, gateway_sidecar, |_| true)
}
/// [`validate_aws_port_mappings`] on a save, against what is stored:
/// `stored` is `None` when the project was not on AWS before (everything is
/// checked). Otherwise only rows the save adds or edits are — a stored row
/// this check would refuse (a UDP mapping kept from before) does not block
/// every unrelated save; it is skipped, with a warning, at each connect.
/// Turning the gateway sidecar on checks everything again.
pub fn validate_aws_port_mappings_update(
stored: Option<(&[PortMapping], bool)>,
mappings: &[PortMapping],
gateway_sidecar: bool,
) -> Result<(), String> {
match stored {
Some((old, old_gateway)) if old_gateway == gateway_sidecar => {
validate_changed(mappings, gateway_sidecar, |m| !old.contains(m))
}
_ => validate_aws_port_mappings(mappings, gateway_sidecar),
}
}
fn validate_changed(
mappings: &[PortMapping],
gateway_sidecar: bool,
changed: impl Fn(&PortMapping) -> bool,
) -> Result<(), String> {
let mut seen: HashMap<u16, bool> = HashMap::new();
for m in mappings.iter().filter(|m| !incomplete(m)) {
let is_changed = changed(m);
if is_changed {
if let Some(problem) = mapping_problem(m, gateway_sidecar) {
return Err(problem);
}
}
match seen.get(&m.host_port) {
Some(&other_changed) if is_changed || other_changed => {
return Err(format!("Host port {} is mapped twice", m.host_port))
}
Some(_) => {}
None => {
seen.insert(m.host_port, is_changed);
}
}
}
Ok(())
}
/// Whether `target` is still the project's connected workspace.
fn still_connected(project_id: &str, target: &ContainerTarget) -> bool {
match target {
ContainerTarget::Agent(ep) => {
super::tunnels::get_endpoint(project_id).is_some_and(|e| e.base_url == ep.base_url)
}
ContainerTarget::Docker { .. } => false,
}
}
/// Open `mappings` for `project_id` into `target` (its connected workspace).
/// See [`open_latest`]; the gateway sidecar is taken as off. Test helper.
#[cfg(test)]
pub async fn open_for(
project_id: &str,
target: &ContainerTarget,
mappings: &[PortMapping],
) -> Vec<PortForwardResult> {
let mappings = mappings.to_vec();
open_latest(project_id, target, move || Some((mappings, false))).await
}
/// Open the project's mappings into `target`, replacing the set it had.
/// `read` gives the mappings and whether the gateway sidecar runs, and is
/// called **under the project's lock** — so the last of several overlapping
/// opens applies what is stored last. `None` from it closes the set.
///
/// One result per mapping, in order: a refused mapping, a host port mapped
/// twice or a busy host port is an error for that mapping only, and the
/// others still open. A mapping already open into the same target is kept,
/// live connections and all. If the project's tunnel is no longer `target`
/// once they are bound (a Stop or a newer connect raced this), nothing is
/// kept.
pub async fn open_latest(
project_id: &str,
target: &ContainerTarget,
read: impl FnOnce() -> Option<(Vec<PortMapping>, bool)> + Send,
) -> Vec<PortForwardResult> {
let plock = project_lock(project_id);
let _held = plock.lock().await;
let Some((mappings, gateway_sidecar)) = read() else {
close_unlocked(project_id).await;
return Vec::new();
};
// What can be forwarded, in order; the first row wins a host port.
let mut results: Vec<PortForwardResult> = Vec::new();
let mut wanted: Vec<(usize, u16, u16)> = Vec::new();
let mut taken = HashSet::new();
for m in mappings.iter().filter(|m| !incomplete(m)) {
let error = if let Some(problem) = mapping_problem(m, gateway_sidecar) {
Some(problem)
} else if !taken.insert(m.host_port) {
Some(format!(
"{} → {}: host port {} is already used by another mapping",
m.host_port, m.container_port, m.host_port
))
} else {
wanted.push((results.len(), m.host_port, m.container_port));
None
};
results.push(PortForwardResult { mapping: m.clone(), error });
}
// Keep what is already open into this target and still wanted; free
// every other host port first, so a changed mapping can rebind it.
let previous = lock().remove(project_id);
let mut kept: HashMap<(u16, u16), PortForward> = HashMap::new();
let mut dropped = Vec::new();
if let Some(prev) = previous {
let same_target = prev.target_id == target.id();
for f in prev.forwards {
let key = (f.port, f.container_port);
if same_target && wanted.iter().any(|&(_, h, c)| (h, c) == key) {
kept.insert(key, f);
} else {
dropped.push(f);
}
}
}
shutdown_all(dropped).await;
let mut opened = Vec::new();
for (i, host_port, container_port) in wanted {
if let Some(f) = kept.remove(&(host_port, container_port)) {
opened.push(f);
continue;
}
match PortForward::bind_mapping(target.clone(), host_port, container_port, PortFamily::V4).await {
Ok(fwd) => {
if let Some(w) = &fwd.ipv6_warning {
log::warn!("Project {project_id}: port mapping {host_port}: {w}");
}
log::info!(
"Project {project_id}: forwarding localhost:{host_port} → workspace port {container_port}"
);
opened.push(fwd);
}
Err(e) => {
results[i].error = Some(if e.kind() == std::io::ErrorKind::AddrInUse {
format!(
"{host_port} → {container_port}: host port {host_port} is already in use on this machine"
)
} else {
format!("{host_port} → {container_port}: could not listen on host port {host_port}: {e}")
});
}
}
}
lock().insert(
project_id.to_string(),
OpenSet { target_id: target.id().to_string(), forwards: opened },
);
// Checked *after* the set is registered: a tunnel close that ran before
// this sees it gone here; one that runs after removes it itself.
if !still_connected(project_id, target) {
close_unlocked(project_id).await;
for r in results.iter_mut().filter(|r| r.error.is_none()) {
r.error = Some("the workspace disconnected".into());
}
}
results
}
async fn shutdown_all(set: Vec<PortForward>) {
futures_util::future::join_all(set.into_iter().map(|mut f| async move { f.shutdown().await }))
.await;
}
/// Close the project's forwards (its ports are free when this returns).
/// Waits for an open in progress.
pub async fn close(project_id: &str) {
let plock = project_lock(project_id);
let _held = plock.lock().await;
close_unlocked(project_id).await;
}
async fn close_unlocked(project_id: &str) {
let set = lock().remove(project_id);
if let Some(set) = set {
shutdown_all(set.forwards).await;
}
}
/// Close every project's forwards. The shutdown path's step (via
/// `tunnels::close_all`). An open in progress finds the tunnel gone and
/// closes its own set.
pub async fn close_all() {
let all: Vec<PortForward> = lock().drain().flat_map(|(_, v)| v.forwards).collect();
shutdown_all(all).await;
}
/// After a connect (or a save of the mappings while connected): open an
/// AWS project's mappings, **as stored when the open runs**, into its
/// connected workspace. The warning detail for the mappings that failed, if
/// any did; `None` as well for a local project (Docker publishes its own) or
/// a workspace that is not (or no longer) connected.
pub async fn open_for_project(store: &ProjectsStore, project_id: &str) -> Option<String> {
let project = store.get(project_id).filter(|p| p.is_aws())?;
let target = ContainerTarget::Agent(super::tunnels::get_endpoint(&project.id)?);
let results = open_latest(project_id, &target, || {
store.get(project_id).filter(|p| p.is_aws()).map(|p| {
let gateway = p.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
(p.port_mappings, gateway)
})
})
.await;
if !still_connected(project_id, &target) {
return None;
}
failure_detail(&results)
}
/// The host ports open for the project.
#[cfg(test)]
pub fn open_ports(project_id: &str) -> Vec<u16> {
let mut ports: Vec<u16> =
lock().get(project_id).map(|v| v.forwards.iter().map(|f| f.port).collect()).unwrap_or_default();
ports.sort_unstable();
ports
}
/// The `workspace-warning` detail for the mappings that failed, if any did.
pub fn failure_detail(results: &[PortForwardResult]) -> Option<String> {
let lines: Vec<&str> = results.iter().filter_map(|r| r.error.as_deref()).collect();
(!lines.is_empty()).then(|| lines.join("\n"))
}
#[cfg(test)]
#[path = "port_forwards/tests.rs"]
mod tests;
@@ -0,0 +1,396 @@
//! Port mappings against real loopback sockets and a **fake** agent that
//! answers `/v1/attach` by echoing stdin back as stdout — the socat it was
//! asked to run is recorded, never run.
use super::*;
use crate::storage::projects_store::ProjectsStore;
use crate::host::agent::AgentEndpoint;
use crate::host::tunnels;
use axum::extract::ws::{Message as WsMessage, WebSocket, WebSocketUpgrade};
use axum::extract::State;
use axum::response::Response;
use axum::routing::get;
use axum::Router;
use futures_util::SinkExt;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
#[derive(Default)]
struct Fake {
/// Every request the agent saw, of any kind.
requests: AtomicUsize,
/// The command of each attach.
cmds: std::sync::Mutex<Vec<Vec<String>>>,
}
async fn attach_ws(State(f): State<Arc<Fake>>, ws: WebSocketUpgrade) -> Response {
f.requests.fetch_add(1, Ordering::SeqCst);
ws.on_upgrade(move |socket| echo(f, socket))
}
async fn echo(f: Arc<Fake>, mut ws: WebSocket) {
let Some(Ok(WsMessage::Text(hello))) = ws.recv().await else { return };
let hello: serde_json::Value = serde_json::from_str(hello.as_str()).unwrap();
let cmd = hello["cmd"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap().to_string())
.collect();
f.cmds.lock().unwrap().push(cmd);
let _ = ws.send(WsMessage::Text(r#"{"type":"started","exec_id":"a1"}"#.into())).await;
while let Some(Ok(msg)) = ws.recv().await {
match msg {
WsMessage::Binary(b) => {
let mut out = vec![1u8];
out.extend_from_slice(&b);
if ws.send(WsMessage::Binary(out.into())).await.is_err() {
return;
}
}
WsMessage::Text(t) if t.as_str().contains("close_stdin") => break,
_ => {}
}
}
let _ = ws.send(WsMessage::Text(r#"{"type":"exit","code":0}"#.into())).await;
let _ = ws.close().await;
}
async fn counted(State(f): State<Arc<Fake>>) -> &'static str {
f.requests.fetch_add(1, Ordering::SeqCst);
"{}"
}
/// A fake agent, registered as `project_id`'s connected tunnel; its target.
async fn connected_agent(project_id: &str) -> (ContainerTarget, Arc<Fake>, Arc<AtomicUsize>) {
let fake = Arc::new(Fake::default());
let app = Router::new()
.route("/v1/attach", get(attach_ws))
.fallback(counted)
.with_state(fake.clone());
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let closes = Arc::new(AtomicUsize::new(0));
tunnels::register(project_id, tunnels::tests::fake_tunnel(port, closes.clone()));
(ContainerTarget::Agent(AgentEndpoint::loopback(port, "tok")), fake, closes)
}
fn pid(tag: &str) -> String {
format!("port-forwards-test-{tag}-{}", uuid::Uuid::new_v4())
}
/// A host port nothing listens on right now.
fn free_port() -> u16 {
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
}
fn tcp(host_port: u16, container_port: u16) -> PortMapping {
PortMapping { host_port, container_port, protocol: "tcp".into() }
}
async fn round_trip(port: u16, msg: &[u8]) -> Vec<u8> {
let mut c = TcpStream::connect(("127.0.0.1", port)).await.expect("connect");
c.write_all(msg).await.unwrap();
let mut buf = vec![0u8; msg.len()];
tokio::time::timeout(std::time::Duration::from_secs(5), c.read_exact(&mut buf))
.await
.expect("echo in time")
.expect("echo");
buf
}
fn port_is_free(port: u16) -> bool {
std::net::TcpListener::bind(("127.0.0.1", port)).is_ok()
}
#[tokio::test]
async fn a_busy_host_port_fails_its_mapping_alone_and_the_other_forwards() {
let p = pid("busy");
let (target, fake, _) = connected_agent(&p).await;
let busy = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let busy_port = busy.local_addr().unwrap().port();
let free = free_port();
let results = open_for(&p, &target, &[tcp(busy_port, 3000), tcp(free, 5432)]).await;
assert_eq!(results.len(), 2);
let e = results[0].error.as_deref().expect("the busy port fails");
assert!(e.contains("already in use"), "{e}");
assert!(results[1].error.is_none(), "{:?}", results[1].error);
assert_eq!(failure_detail(&results).as_deref(), Some(e));
assert_eq!(open_ports(&p), vec![free]);
// Bytes go through the agent's attach, to the *container* port.
assert_eq!(round_trip(free, b"ping").await, b"ping");
assert_eq!(
fake.cmds.lock().unwrap().as_slice(),
[vec!["socat".to_string(), "-".into(), "TCP:127.0.0.1:5432".into()]]
);
// Stop / Remove / task death all close the tunnel: the mappings go too.
tunnels::close(&p).await;
assert!(open_ports(&p).is_empty());
assert!(port_is_free(free));
}
#[tokio::test]
async fn a_server_speaks_first_protocol_is_not_held_back() {
// Unlike the auth bridge, a mapping forwards a client that has said
// nothing yet (MySQL, SMTP): the attach opens on connect.
let p = pid("silent");
let (target, fake, _) = connected_agent(&p).await;
let port = free_port();
assert!(open_for(&p, &target, &[tcp(port, 3306)]).await[0].error.is_none());
let _client = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(3);
while fake.cmds.lock().unwrap().is_empty() {
assert!(tokio::time::Instant::now() < deadline, "no attach for a silent client");
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
tunnels::close(&p).await;
}
#[tokio::test]
async fn an_open_listener_with_no_connection_makes_no_agent_request() {
// So it can never count as activity and keep a workspace from its idle stop.
let p = pid("idle");
let (target, fake, _) = connected_agent(&p).await;
let port = free_port();
assert!(open_for(&p, &target, &[tcp(port, 8080)]).await[0].error.is_none());
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
assert_eq!(fake.requests.load(Ordering::SeqCst), 0);
tunnels::close(&p).await;
}
#[tokio::test]
async fn udp_reserved_and_duplicate_mappings_are_refused_per_mapping() {
let p = pid("refused");
let (target, _, _) = connected_agent(&p).await;
let ok = free_port();
let udp = PortMapping { host_port: free_port(), container_port: 53, protocol: "udp".into() };
let results = open_for(
&p,
&target,
&[
udp,
tcp(ok, 8080),
tcp(ok, 9090),
tcp(free_port(), *RESERVED_CONTAINER_PORTS.start()),
tcp(free_port(), crate::host::ssm::AGENT_PORT),
tcp(*RESERVED_HOST_PORTS.start(), 8080),
tcp(0, 0),
],
)
.await;
let errors: Vec<Option<&str>> = results.iter().map(|r| r.error.as_deref()).collect();
assert_eq!(errors.len(), 6, "an unfinished row is skipped: {errors:?}");
assert!(errors[0].unwrap().contains("only TCP"), "{errors:?}");
assert!(errors[1].is_none());
assert!(errors[2].unwrap().contains("another mapping"), "{errors:?}");
assert!(errors[3].unwrap().contains("browser view"), "{errors:?}");
assert!(errors[4].unwrap().contains("agent"), "{errors:?}");
assert!(errors[5].unwrap().contains("browser view"), "{errors:?}");
assert_eq!(open_ports(&p), vec![ok]);
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_reconnect_replaces_the_set_on_the_same_host_ports() {
let p = pid("replace");
let (target, _, _) = connected_agent(&p).await;
let port = free_port();
assert!(open_for(&p, &target, &[tcp(port, 8080)]).await[0].error.is_none());
// A background re-attach registers a new tunnel and opens again.
let (target2, fake2, _) = connected_agent(&p).await;
let again = open_for(&p, &target2, &[tcp(port, 8080)]).await;
assert!(again[0].error.is_none(), "{:?}", again[0].error);
assert_eq!(round_trip(port, b"hi").await, b"hi");
assert_eq!(fake2.cmds.lock().unwrap().len(), 1, "the new set reaches the new tunnel");
tunnels::close(&p).await;
assert!(port_is_free(port));
}
#[tokio::test]
async fn nothing_is_kept_when_the_workspace_is_no_longer_connected() {
let p = pid("gone");
let (target, _, _) = connected_agent(&p).await;
tunnels::close(&p).await; // a Stop raced the connect
let port = free_port();
let results = open_for(&p, &target, &[tcp(port, 8080)]).await;
assert!(results[0].error.as_deref().unwrap().contains("disconnected"));
assert!(open_ports(&p).is_empty());
assert!(port_is_free(port));
}
#[tokio::test]
async fn a_task_death_closes_the_mappings_but_a_stale_report_does_not() {
let p = pid("death");
let (target, _, _) = connected_agent(&p).await;
let port = free_port();
open_for(&p, &target, &[tcp(port, 8080)]).await;
tunnels::close_if_task(&p, "arn:some-older-task").await;
assert_eq!(open_ports(&p), vec![port]);
let ContainerTarget::Agent(ep) = &target else { unreachable!() };
let (_, _, _, arn) = tunnels::task_of(ep).unwrap();
tunnels::close_if_task(&p, &arn).await;
assert!(open_ports(&p).is_empty());
assert!(port_is_free(port));
}
#[test]
fn an_aws_project_saves_only_tcp_unreserved_unique_mappings() {
let v = |m: &[PortMapping]| validate_aws_port_mappings(m, false);
assert!(v(&[tcp(3000, 3000), tcp(8081, 8080), tcp(0, 0), tcp(5000, 0), tcp(4001, 4000)]).is_ok());
let udp = PortMapping { host_port: 5353, container_port: 5353, protocol: "udp".into() };
let e = v(&[udp]).unwrap_err();
assert!(e.contains("only TCP") && e.contains("UDP"), "{e}");
assert!(v(&[tcp(3000, 3000), tcp(3000, 4000)]).unwrap_err().contains("mapped twice"));
assert!(v(&[tcp(4000, 39321)]).is_err());
assert!(v(&[tcp(4000, 47900)]).is_err());
assert!(v(&[tcp(47820, 4000)]).is_err());
}
#[test]
fn the_gateway_sidecars_port_is_refused_while_the_project_runs_it() {
// A mapping reaches the workspace's *loopback*, where the sidecar holds
// the provider key behind 127.0.0.1:4000.
let m = [tcp(4001, GATEWAY_SIDECAR_PORT)];
assert!(validate_aws_port_mappings(&m, false).is_ok());
let e = validate_aws_port_mappings(&m, true).unwrap_err();
assert!(e.contains("gateway sidecar"), "{e}");
// Turning the sidecar on re-checks rows that were already stored.
assert!(validate_aws_port_mappings_update(Some((&m, false)), &m, true).is_err());
assert!(validate_aws_port_mappings_update(Some((&m, false)), &m, false).is_ok());
}
#[test]
fn a_stored_row_that_would_be_refused_does_not_block_other_saves() {
let udp = PortMapping { host_port: 5353, container_port: 5353, protocol: "udp".into() };
let stored = vec![udp.clone(), tcp(3000, 3000)];
// Unchanged, plus a new valid row: fine.
let saved = vec![udp.clone(), tcp(3000, 3000), tcp(8080, 8080)];
assert!(validate_aws_port_mappings_update(Some((&stored, false)), &saved, false).is_ok());
// A new UDP row, or an edit to one, is refused.
let edited = PortMapping { host_port: 5354, ..udp.clone() };
let e = validate_aws_port_mappings_update(Some((&stored, false)), &[edited, tcp(3000, 3000)], false);
assert!(e.unwrap_err().contains("only TCP"));
// A new row taking a stored row's host port is refused.
let dup = vec![udp.clone(), tcp(3000, 3000), tcp(3000, 4000)];
assert!(validate_aws_port_mappings_update(Some((&stored, false)), &dup, false)
.unwrap_err()
.contains("mapped twice"));
// Newly on AWS: everything is checked.
assert!(validate_aws_port_mappings_update(None, &stored, false).is_err());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn overlapping_opens_converge_on_the_last_stored_set_without_false_errors() {
// The editor saves on every blur; each save opens. Each open re-reads
// the stored mappings under the project's lock, so overlapping opens
// neither fight over a host port nor leave an older save's set behind.
let p = pid("concurrent");
let (target, _, _) = connected_agent(&p).await;
let (a, b, c) = (free_port(), free_port(), free_port());
let sets = [
vec![tcp(a, 8080), tcp(b, 8081)],
vec![tcp(b, 8081), tcp(c, 8082)],
vec![tcp(a, 8085), tcp(c, 8082)],
vec![tcp(b, 8090), tcp(c, 8082)],
];
let stored = Arc::new(std::sync::Mutex::new(sets[0].clone()));
let go = Arc::new(tokio::sync::Barrier::new(8));
let mut opens = Vec::new();
for i in 0..8 {
let (p2, t2, s2, go2) = (p.clone(), target.clone(), stored.clone(), go.clone());
let mine = sets[i % sets.len()].clone();
opens.push(tokio::spawn(async move {
go2.wait().await;
open_latest(&p2, &t2, move || {
let mut s = s2.lock().unwrap();
*s = mine; // this save lands, then its open reads the store
Some((s.clone(), false))
})
.await
}));
}
for o in opens {
let results = o.await.unwrap();
assert!(results.iter().all(|r| r.error.is_none()), "{results:?}");
}
let last = stored.lock().unwrap().clone();
let mut want: Vec<u16> = last.iter().map(|m| m.host_port).collect();
want.sort_unstable();
assert_eq!(open_ports(&p), want, "the registry holds the last stored set");
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_save_keeps_unchanged_forwards_and_their_live_connections() {
let p = pid("diff");
let (target, fake, _) = connected_agent(&p).await;
let (a, b) = (free_port(), free_port());
open_for(&p, &target, &[tcp(a, 8080)]).await;
let mut client = TcpStream::connect(("127.0.0.1", a)).await.unwrap();
client.write_all(b"one").await.unwrap();
let mut buf = [0u8; 3];
client.read_exact(&mut buf).await.unwrap();
let r = open_for(&p, &target, &[tcp(a, 8080), tcp(b, 8081)]).await;
assert!(r.iter().all(|r| r.error.is_none()), "{r:?}");
// The same connection still carries bytes: its forward was kept.
client.write_all(b"two").await.unwrap();
client.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"two");
assert_eq!(fake.cmds.lock().unwrap().len(), 1, "no new attach for the kept connection");
// A changed container port rebinds the same host port.
let r = open_for(&p, &target, &[tcp(a, 9090)]).await;
assert!(r[0].error.is_none(), "{:?}", r[0].error);
assert_eq!(round_trip(a, b"x").await, b"x");
assert_eq!(fake.cmds.lock().unwrap().last().unwrap()[2], "TCP:127.0.0.1:9090");
assert!(port_is_free(b));
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_stored_project_is_read_when_the_open_runs() {
let p = pid("store");
let dir = tempfile::tempdir().unwrap();
let mut project = crate::models::Project::new("x".into(), vec![]);
project.id = p.clone();
project.host_id = Some("h1".into());
project.aws_placement =
Some(crate::models::AwsPlacement { region: "us-west-2".into(), environment: "dev".into() });
project.aws = Some(Default::default());
let (port, busy) = (free_port(), std::net::TcpListener::bind("127.0.0.1:0").unwrap());
let busy_port = busy.local_addr().unwrap().port();
project.port_mappings = vec![tcp(port, 8080), tcp(busy_port, 8081)];
let store = ProjectsStore::over_file_for_test(dir.path().join("projects.json"), vec![project]);
assert_eq!(open_for_project(&store, &p).await, None, "not connected: nothing, no warning");
let (_target, _, _) = connected_agent(&p).await;
let warning = open_for_project(&store, &p).await.expect("the busy port is reported");
assert!(warning.contains(&busy_port.to_string()) && warning.contains("already in use"), "{warning}");
assert_eq!(open_ports(&p), vec![port]);
tunnels::close(&p).await;
}
#[tokio::test]
async fn the_auth_bridges_forward_still_waits_for_a_client_to_speak() {
// The generalised `PortForward::bind` keeps the bridge's gate: a silent
// client costs no attach (contrast `a_server_speaks_first_protocol_…`).
let p = pid("bridge");
let (target, fake, _) = connected_agent(&p).await;
let port = free_port();
let mut fwd = PortForward::bind(target, port, port, PortFamily::V4).await.unwrap();
assert_eq!((fwd.port, fwd.container_port), (port, port));
let _client = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
assert_eq!(fake.requests.load(Ordering::SeqCst), 0);
fwd.shutdown().await;
tunnels::close(&p).await;
}
+28 -3
View File
@@ -9,7 +9,13 @@ use std::sync::Arc;
use tokio::io::AsyncWriteExt;
use tokio::sync::{mpsc, Mutex};
use super::{AttachedExec, ContainerTarget};
use super::{AttachedExec, ContainerTarget, ExecSpec};
/// A session's exec: as `claude` in `/workspace` (what `attach_exec` runs),
/// with `env` added. Values in `env` are never logged.
pub fn session_spec(cmd: Vec<String>, tty: bool, env: Vec<String>) -> ExecSpec {
ExecSpec::new(cmd).tty(tty).env(env)
}
pub struct ExecSession {
pub exec_id: String,
@@ -46,18 +52,22 @@ impl ExecSessionManager {
}
}
/// A terminal: a tty exec of `cmd` with `env` added to the container's
/// environment (an AWS copy-at-connect project's credentials,
/// `aws_host::terminal_env`; empty otherwise).
pub async fn create_session<F>(
&self,
target: &ContainerTarget,
session_id: &str,
cmd: Vec<String>,
env: Vec<String>,
on_output: F,
on_exit: Box<dyn FnOnce() + Send>,
) -> Result<(), String>
where
F: Fn(Vec<u8>) + Send + 'static,
{
self.create_session_with_tty(target, session_id, cmd, true, on_output, on_exit)
self.create_session_spec(target, session_id, session_spec(cmd, true, env), on_output, on_exit)
.await
}
@@ -70,6 +80,21 @@ impl ExecSessionManager {
on_output: F,
on_exit: Box<dyn FnOnce() + Send>,
) -> Result<(), String>
where
F: Fn(Vec<u8>) + Send + 'static,
{
self.create_session_spec(target, session_id, session_spec(cmd, tty, Vec::new()), on_output, on_exit)
.await
}
async fn create_session_spec<F>(
&self,
target: &ContainerTarget,
session_id: &str,
spec: ExecSpec,
on_output: F,
on_exit: Box<dyn FnOnce() + Send>,
) -> Result<(), String>
where
F: Fn(Vec<u8>) + Send + 'static,
{
@@ -77,7 +102,7 @@ impl ExecSessionManager {
exec_id,
mut output,
mut input,
} = target.attach_exec(cmd, tty).await?;
} = target.attach(spec).await?;
let (input_tx, mut input_rx) = mpsc::unbounded_channel::<Vec<u8>>();
let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1);
+9
View File
@@ -115,6 +115,10 @@ impl EcsTask {
/// ARN), and check it can take a session now: the task is running and
/// its ECS Exec agent is up. Fargate starts that agent a few seconds after
/// the container, so "not ready yet" is a normal answer just after start.
///
/// The AWS host builds its target from `aws::tasks::wait_ready`'s answer
/// instead (`aws_host::ecs_task`); this stays for the live tests.
#[allow(dead_code)]
pub async fn resolve(
ecs: &aws_sdk_ecs::Client,
cluster: &str,
@@ -175,6 +179,7 @@ impl EcsTask {
/// The part of an ARN after the last `marker` (`.../cluster/name` → `name`),
/// or the whole string when it is not an ARN.
#[allow(dead_code)] // `EcsTask::resolve`
fn last_arn_segment<'a>(s: &'a str, marker: &str) -> &'a str {
if !s.starts_with("arn:") {
return s;
@@ -606,6 +611,7 @@ pub enum TunnelState {
pub struct PortForward {
local_port: u16,
state: watch::Receiver<TunnelState>,
#[allow(dead_code)] // `plugin_pid()`, for diagnostics and the tests
plugin_pid: watch::Receiver<Option<u32>>,
supervisor: Option<tokio::task::JoinHandle<()>>,
_cancel: DropGuard,
@@ -678,6 +684,7 @@ impl PortForward {
}
}
#[allow(dead_code)] // diagnostics and the tests (Task 8 may show the state)
pub fn local_port(&self) -> u16 {
self.local_port
}
@@ -688,11 +695,13 @@ impl PortForward {
AgentEndpoint::loopback(self.local_port, token)
}
#[allow(dead_code)] // diagnostics and the tests (Task 8 may show the state)
pub fn state(&self) -> watch::Receiver<TunnelState> {
self.state.clone()
}
/// The running plugin's process id, if one is running.
#[allow(dead_code)] // diagnostics and the tests (Task 8 may show the state)
pub fn plugin_pid(&self) -> Option<u32> {
*self.plugin_pid.borrow()
}
+570
View File
@@ -0,0 +1,570 @@
//! The live SSM tunnels to AWS projects' agents, by project id.
//!
//! An AWS project's [`ContainerTarget`](super::ContainerTarget) is an agent
//! endpoint at the loopback end of a [`PortForward`](super::ssm::PortForward);
//! this registry is where `ContainerTarget::for_project` finds it, where
//! `ProjectHost::stop` finds the task behind an endpoint, and what the
//! shutdown path closes (every forward runs a `session-manager-plugin` child
//! that would otherwise outlive the app — see `ssm.rs`).
//!
//! It also remembers which targets the AWS death watch found **stopped**, so
//! `ContainerTarget::has_exited` can answer for an agent target (R3): only
//! the host that runs the task can confirm a death, never the agent.
use std::collections::{HashMap, HashSet};
use std::sync::Mutex;
use std::time::Duration;
use aws_config::SdkConfig;
use futures_util::future::BoxFuture;
use tokio::sync::watch;
use tokio_util::sync::CancellationToken;
use super::agent::AgentEndpoint;
use crate::aws::tasks::StopKind;
use super::ssm::{EcsTask, PortForward};
/// Something that owns a tunnel's resources: the real [`PortForward`], or a
/// test stand-in.
pub trait TunnelHandle: Send + Sync {
fn close(self: Box<Self>) -> BoxFuture<'static, ()>;
}
impl TunnelHandle for PortForward {
fn close(self: Box<Self>) -> BoxFuture<'static, ()> {
Box::pin(async move { (*self).close().await })
}
}
/// One project's tunnel and the task it reaches.
pub struct LiveTunnel {
/// The SSM target (kept for diagnostics; the forward already uses it).
#[allow(dead_code)]
pub task: EcsTask,
pub task_arn: String,
pub cluster_arn: String,
pub connection_id: String,
pub region: String,
/// The connection's SDK config, so a stop needs no settings lookup.
pub sdk: SdkConfig,
pub forward: Box<dyn TunnelHandle>,
pub endpoint: AgentEndpoint,
/// Runtime handles for the store (`set_aws_runtime`).
pub access_point_id: Option<String>,
pub task_definition_arn: String,
/// EFS storage's `/workspace` access point.
pub workspace_access_point_id: Option<String>,
/// The start that opened this tunnel ran the task (rather than
/// attaching to one already running, or re-attaching after a restart).
pub created: bool,
}
/// Where an endpoint's task is: `(connection, region, cluster, task ARN)`.
pub type TaskOf = (String, String, String, String);
#[derive(Default)]
struct Registry {
live: HashMap<String, LiveTunnel>,
/// Target ids (endpoint base URLs) whose task the AWS watch saw STOPPED,
/// and why.
stopped: HashMap<String, StopKind>,
/// Projects whose tunnel is being re-opened in the background.
pending: HashSet<String>,
/// Bumped by every [`close`] (a stop or remove), every [`register`] (a
/// start's own tunnel) and [`cancel_reattach`]: a background re-attach
/// that began under an older generation must never register.
generation: HashMap<String, u64>,
/// The background clone/setup follower per project (`aws_host::status`),
/// with the id [`begin_follow`] gave it. Cancelled by a new follower,
/// [`cancel_follow`] (Stop, Remove), and whenever the tunnel is taken
/// out or every tunnel closed.
followers: HashMap<String, (u64, CancellationToken)>,
next_follower: u64,
}
fn registry() -> &'static Mutex<Registry> {
static R: std::sync::OnceLock<Mutex<Registry>> = std::sync::OnceLock::new();
R.get_or_init(Default::default)
}
fn lock() -> std::sync::MutexGuard<'static, Registry> {
registry().lock().unwrap_or_else(|e| e.into_inner())
}
/// Wakes [`wait_endpoint`] callers when the registry changes.
fn changed() -> &'static watch::Sender<u64> {
static C: std::sync::OnceLock<watch::Sender<u64>> = std::sync::OnceLock::new();
C.get_or_init(|| watch::channel(0).0)
}
fn notify() {
changed().send_modify(|n| *n = n.wrapping_add(1));
}
/// The target id an AWS project has while no tunnel is registered for it
/// (see `aws_host::unattached_container_id`): never a loopback URL, so a
/// port a new tunnel reuses can't be confused with it.
pub fn unattached_id(project_id: &str) -> String {
format!("http://unattached.invalid/{project_id}/")
}
/// Forget that the watch saw the task behind `target_id` stopped. For a
/// project's [`unattached_id`], which (unlike an endpoint) is reused by
/// every later unattached spell of the same project: a stale entry would
/// make a later live task read as stopped.
pub fn forget_stopped(target_id: &str) {
lock().stopped.remove(target_id);
}
/// Register `tunnel` for `project_id`, returning the one it replaces (the
/// caller closes it).
pub fn register(project_id: &str, tunnel: LiveTunnel) -> Option<LiveTunnel> {
let old = {
let mut r = lock();
*r.generation.entry(project_id.to_string()).or_default() += 1;
r.stopped.remove(tunnel.endpoint.base_url.as_str());
r.stopped.remove(&unattached_id(project_id));
r.pending.remove(project_id);
r.live.insert(project_id.to_string(), tunnel)
};
notify();
old
}
pub fn get_endpoint(project_id: &str) -> Option<AgentEndpoint> {
lock().live.get(project_id).map(|t| t.endpoint.clone())
}
/// `(task ARN, access point id, task definition ARN, workspace access point
/// id)` of the project's tunnel.
pub fn runtime(project_id: &str) -> Option<(String, Option<String>, String, Option<String>)> {
lock().live.get(project_id).map(|t| {
(
t.task_arn.clone(),
t.access_point_id.clone(),
t.task_definition_arn.clone(),
t.workspace_access_point_id.clone(),
)
})
}
/// The project whose tunnel is `ep` and its task.
pub fn task_of(ep: &AgentEndpoint) -> Option<TaskOf> {
lock().live.values().find(|t| t.endpoint.base_url == ep.base_url).map(|t| {
(
t.connection_id.clone(),
t.region.clone(),
t.cluster_arn.clone(),
t.task_arn.clone(),
)
})
}
/// The project id and SDK config behind `ep`.
pub(crate) fn owner_of(ep: &AgentEndpoint) -> Option<(String, SdkConfig)> {
lock()
.live
.iter()
.find(|(_, t)| t.endpoint.base_url == ep.base_url)
.map(|(id, t)| (id.clone(), t.sdk.clone()))
}
/// A background re-attach of `project_id` begins: marks it pending and
/// returns the generation [`register_if_current`] must still see.
pub fn begin_reattach(project_id: &str) -> u64 {
let g = {
let mut r = lock();
r.pending.insert(project_id.to_string());
*r.generation.entry(project_id.to_string()).or_default()
};
notify();
g
}
/// [`register`], unless the project was closed (stopped, removed) since
/// `generation` was taken: then the tunnel is handed back for the caller to
/// close, and nothing is registered.
pub fn register_if_current(
project_id: &str,
generation: u64,
tunnel: LiveTunnel,
) -> Result<Option<LiveTunnel>, Box<LiveTunnel>> {
let old = {
let mut r = lock();
if r.generation.get(project_id).copied().unwrap_or_default() != generation {
return Err(Box::new(tunnel));
}
r.stopped.remove(tunnel.endpoint.base_url.as_str());
r.stopped.remove(&unattached_id(project_id));
r.pending.remove(project_id);
r.live.insert(project_id.to_string(), tunnel)
};
notify();
Ok(old)
}
/// [`close`], but only when the registered tunnel leads to `task_arn` (a
/// death report about an older task must not close a newer one's tunnel).
pub async fn close_if_task(project_id: &str, task_arn: &str) {
let taken = {
let mut r = lock();
if !r.live.get(project_id).is_some_and(|t| t.task_arn == task_arn) {
return;
}
take(&mut r, project_id)
};
notify();
// The project's port mappings lead into that task: they go with it.
super::port_forwards::close(project_id).await;
if let Some(t) = taken {
t.forward.close().await;
}
}
/// Whether the project's registered tunnel came from a start that ran its
/// task (see [`LiveTunnel::created`]).
pub fn ran_task(project_id: &str) -> bool {
lock().live.get(project_id).is_some_and(|t| t.created)
}
/// A later start found the tunnel already up: it ran no task itself.
pub fn mark_attached(project_id: &str) {
if let Some(t) = lock().live.get_mut(project_id) {
t.created = false;
}
}
/// Register a background follower for `project_id`, cancelling the one
/// before it. Returns its id (for [`end_follow`]) and its token.
pub fn begin_follow(project_id: &str) -> (u64, CancellationToken) {
let mut r = lock();
r.next_follower += 1;
let id = r.next_follower;
let token = CancellationToken::new();
if let Some((_, old)) = r.followers.insert(project_id.to_string(), (id, token.clone())) {
old.cancel();
}
(id, token)
}
/// A follower finished: forget it, unless a newer one has replaced it.
pub fn end_follow(project_id: &str, id: u64) {
let mut r = lock();
if r.followers.get(project_id).is_some_and(|(i, _)| *i == id) {
r.followers.remove(project_id);
}
}
/// End the project's follower, if one runs (Stop, Remove).
pub fn cancel_follow(project_id: &str) {
cancel_follow_locked(&mut lock(), project_id);
}
fn cancel_follow_locked(r: &mut Registry, project_id: &str) {
if let Some((_, t)) = r.followers.remove(project_id) {
t.cancel();
}
}
/// Whether a follower runs for the project.
#[cfg(test)]
pub fn is_following(project_id: &str) -> bool {
lock().followers.contains_key(project_id)
}
/// Under the lock: invalidate a re-attach in flight and take the tunnel out.
fn take(r: &mut Registry, project_id: &str) -> Option<LiveTunnel> {
cancel_follow_locked(r, project_id);
r.pending.remove(project_id);
*r.generation.entry(project_id.to_string()).or_default() += 1;
let t = r.live.remove(project_id);
if let Some(ref t) = t {
r.stopped.remove(t.endpoint.base_url.as_str());
}
t
}
/// A start is about to attach itself: any background re-attach in flight
/// is cancelled (it will hand its tunnel back and close it).
pub fn cancel_reattach(project_id: &str) {
{
let mut r = lock();
r.pending.remove(project_id);
*r.generation.entry(project_id.to_string()).or_default() += 1;
}
notify();
}
/// A background re-attach that began at `generation` gave up: clear the
/// pending flag, unless something newer has taken over since.
pub fn end_reattach(project_id: &str, generation: u64) {
{
let mut r = lock();
if r.generation.get(project_id).copied().unwrap_or_default() == generation {
r.pending.remove(project_id);
}
}
notify();
}
/// Take the project's tunnel out and close it (and its port mappings'
/// forwards), and invalidate a background re-attach in flight.
pub async fn close(project_id: &str) {
let taken = take(&mut lock(), project_id);
notify();
super::port_forwards::close(project_id).await;
if let Some(t) = taken {
t.forward.close().await;
}
}
/// Close every tunnel, concurrently. The shutdown path's step.
pub async fn close_all() {
let all: Vec<LiveTunnel> = {
let mut r = lock();
r.pending.clear();
r.stopped.clear();
for (_, (_, t)) in r.followers.drain() {
t.cancel();
}
for g in r.generation.values_mut() {
*g += 1;
}
r.live.drain().map(|(_, t)| t).collect()
};
notify();
super::port_forwards::close_all().await;
futures_util::future::join_all(all.into_iter().map(|t| t.forward.close())).await;
}
/// The AWS watch saw the task behind target `target_id` STOPPED, for `why`.
pub fn mark_stopped(target_id: &str, why: StopKind) {
lock().stopped.insert(target_id.to_string(), why);
}
/// Whether the AWS watch has confirmed the task behind `target_id` stopped.
pub fn is_stopped(target_id: &str) -> bool {
lock().stopped.contains_key(target_id)
}
/// Why the task behind `target_id` stopped, once the watch has seen it.
pub fn stop_kind(target_id: &str) -> Option<StopKind> {
lock().stopped.get(target_id).cloned()
}
pub fn is_pending(project_id: &str) -> bool {
lock().pending.contains(project_id)
}
/// The project's endpoint, waiting up to `timeout` while a background
/// re-attach is still opening it. `None` when there is none.
pub async fn wait_endpoint(project_id: &str, timeout: Duration) -> Option<AgentEndpoint> {
let mut rx = changed().subscribe();
let deadline = tokio::time::Instant::now() + timeout;
loop {
if let Some(ep) = get_endpoint(project_id) {
return Some(ep);
}
if !is_pending(project_id) {
return None;
}
if tokio::time::timeout_at(deadline, rx.changed()).await.is_err() {
return None;
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
/// A stand-in tunnel that counts its closes.
pub(crate) struct FakeTunnel(pub Arc<AtomicUsize>);
impl TunnelHandle for FakeTunnel {
fn close(self: Box<Self>) -> BoxFuture<'static, ()> {
let n = self.0.clone();
Box::pin(async move {
n.fetch_add(1, Ordering::SeqCst);
})
}
}
pub(crate) fn fake_tunnel(port: u16, closes: Arc<AtomicUsize>) -> LiveTunnel {
LiveTunnel {
task: EcsTask {
cluster: "triple-c-dev".into(),
task_id: format!("t{port}"),
runtime_id: "r".into(),
},
task_arn: format!("arn:aws:ecs:eu-west-1:111122223333:task/triple-c-dev/t{port}"),
cluster_arn: "arn:aws:ecs:eu-west-1:111122223333:cluster/triple-c-dev".into(),
connection_id: "c1".into(),
region: "eu-west-1".into(),
sdk: SdkConfig::builder().build(),
forward: Box::new(FakeTunnel(closes)),
endpoint: AgentEndpoint::loopback(port, "tok"),
access_point_id: Some("fsap-1".into()),
task_definition_arn: "arn:td:1".into(),
workspace_access_point_id: None,
created: true,
}
}
fn pid(tag: &str) -> String {
format!("tunnels-test-{tag}-{}", uuid::Uuid::new_v4())
}
#[tokio::test]
async fn a_registered_tunnel_is_found_by_project_and_by_endpoint_and_closed_once() {
let closes = Arc::new(AtomicUsize::new(0));
let p = pid("reg");
assert!(get_endpoint(&p).is_none());
assert!(register(&p, fake_tunnel(61001, closes.clone())).is_none());
let ep = get_endpoint(&p).unwrap();
assert_eq!(ep, AgentEndpoint::loopback(61001, "tok"));
let (conn, region, _, arn) = task_of(&ep).unwrap();
assert_eq!((conn.as_str(), region.as_str()), ("c1", "eu-west-1"));
assert!(arn.ends_with("/t61001"));
assert_eq!(runtime(&p).unwrap().1.as_deref(), Some("fsap-1"));
close(&p).await;
close(&p).await;
assert_eq!(closes.load(Ordering::SeqCst), 1);
assert!(get_endpoint(&p).is_none() && task_of(&ep).is_none());
}
#[tokio::test]
async fn closing_the_tunnel_ends_its_follower_and_an_attach_clears_created() {
let closes = Arc::new(AtomicUsize::new(0));
let p = pid("follow");
register(&p, fake_tunnel(61009, closes.clone()));
assert!(ran_task(&p));
mark_attached(&p);
assert!(!ran_task(&p));
let (_, token) = begin_follow(&p);
assert!(is_following(&p) && !token.is_cancelled());
close(&p).await;
assert!(token.is_cancelled());
assert!(!is_following(&p));
}
#[tokio::test]
async fn re_registering_hands_back_the_old_tunnel_and_clears_a_stale_stop() {
let closes = Arc::new(AtomicUsize::new(0));
let p = pid("rereg");
register(&p, fake_tunnel(61002, closes.clone()));
let id = AgentEndpoint::loopback(61003, "tok").base_url.to_string();
mark_stopped(&id, StopKind::Spot);
assert_eq!(stop_kind(&id), Some(StopKind::Spot));
let old = register(&p, fake_tunnel(61003, closes.clone())).unwrap();
assert_eq!(old.endpoint.base_url.port(), Some(61002));
assert!(!is_stopped(&id), "a new task on a reused port is not stopped");
old.forward.close().await;
close(&p).await;
assert_eq!(closes.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn a_registering_tunnel_clears_a_stop_seen_under_the_projects_unattached_id() {
let p = pid("unattached-stale");
let placeholder = unattached_id(&p);
mark_stopped(&placeholder, StopKind::Spot);
let closes = Arc::new(AtomicUsize::new(0));
register(&p, fake_tunnel(61301, closes.clone()));
assert!(!is_stopped(&placeholder), "a later unattached spell must not read as stopped");
close(&p).await;
// A re-attach registering clears it too; and it can be forgotten directly.
let q = pid("unattached-stale-reattach");
mark_stopped(&unattached_id(&q), StopKind::Spot);
let g = begin_reattach(&q);
assert!(register_if_current(&q, g, fake_tunnel(61302, closes.clone())).is_ok());
assert!(!is_stopped(&unattached_id(&q)));
close(&q).await;
mark_stopped(&unattached_id(&q), StopKind::Idle);
forget_stopped(&unattached_id(&q));
assert!(!is_stopped(&unattached_id(&q)));
}
#[tokio::test]
async fn a_reattach_that_began_before_a_stop_does_not_register() {
let closes = Arc::new(AtomicUsize::new(0));
let p = pid("gen");
let g = begin_reattach(&p);
assert!(is_pending(&p));
close(&p).await; // the user stopped the project meanwhile
let back = register_if_current(&p, g, fake_tunnel(61005, closes.clone()));
let t = back.err().expect("handed back, not registered");
assert!(get_endpoint(&p).is_none() && !is_pending(&p));
t.forward.close().await;
assert_eq!(closes.load(Ordering::SeqCst), 1);
let g = begin_reattach(&p);
assert!(register_if_current(&p, g, fake_tunnel(61006, closes.clone())).is_ok());
assert!(get_endpoint(&p).is_some());
close(&p).await;
}
#[tokio::test]
async fn a_starts_tunnel_survives_a_reattach_that_finishes_after_it() {
let closes = Arc::new(AtomicUsize::new(0));
// Start cancels the pending re-attach, then registers its own.
let p = pid("start-wins");
let g = begin_reattach(&p);
cancel_reattach(&p);
assert!(!is_pending(&p));
register(&p, fake_tunnel(61010, closes.clone()));
let late = register_if_current(&p, g, fake_tunnel(61011, closes.clone()));
late.err().expect("the late re-attach is refused").forward.close().await;
assert_eq!(get_endpoint(&p).unwrap().base_url.port(), Some(61010), "Start's tunnel stays");
assert_eq!(closes.load(Ordering::SeqCst), 1, "only the late tunnel was closed");
// A stale end of that re-attach does not clear a newer one's pending flag.
let g2 = begin_reattach(&p);
end_reattach(&p, g);
assert!(is_pending(&p));
end_reattach(&p, g2);
assert!(!is_pending(&p));
close(&p).await;
// Even without a cancel, a plain register makes an older re-attach stale.
let p = pid("register-bumps");
let g = begin_reattach(&p);
register(&p, fake_tunnel(61012, closes.clone()));
assert!(register_if_current(&p, g, fake_tunnel(61013, closes.clone())).is_err());
assert_eq!(get_endpoint(&p).unwrap().base_url.port(), Some(61012));
close(&p).await;
}
#[tokio::test]
async fn a_death_report_closes_only_the_tunnel_of_the_task_that_died() {
let closes = Arc::new(AtomicUsize::new(0));
let p = pid("died");
register(&p, fake_tunnel(61007, closes.clone()));
close_if_task(&p, "arn:aws:ecs:eu-west-1:111122223333:task/triple-c-dev/older").await;
assert!(get_endpoint(&p).is_some(), "a newer task's tunnel stays");
close_if_task(&p, "arn:aws:ecs:eu-west-1:111122223333:task/triple-c-dev/t61007").await;
assert!(get_endpoint(&p).is_none());
assert_eq!(closes.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn waiting_for_an_endpoint_ends_when_it_is_registered_or_the_reattach_gives_up() {
let p = pid("wait");
assert!(wait_endpoint(&p, Duration::from_secs(5)).await.is_none(), "nothing pending");
begin_reattach(&p);
let p2 = p.clone();
let waiter = tokio::spawn(async move { wait_endpoint(&p2, Duration::from_secs(5)).await });
tokio::time::sleep(Duration::from_millis(20)).await;
register(&p, fake_tunnel(61004, Arc::new(AtomicUsize::new(0))));
assert_eq!(waiter.await.unwrap().unwrap().base_url.port(), Some(61004));
close(&p).await;
let g = begin_reattach(&p);
let p2 = p.clone();
let waiter = tokio::spawn(async move { wait_endpoint(&p2, Duration::from_secs(5)).await });
tokio::time::sleep(Duration::from_millis(20)).await;
end_reattach(&p, g);
assert!(waiter.await.unwrap().is_none());
}
}
+283 -9
View File
@@ -3,21 +3,140 @@
//! reported dead (replayable from a point in time), and a positive-exit check
//! used to confirm each report before anything changes.
//!
//! Docker pushes `die` events. An AWS host will have no push stream from the
//! app's point of view; its [`deaths`] will be a `DescribeTasks` poll that
//! yields stopped tasks, which `container_watch` handles the same way because
//! every report is confirmed with [`ContainerTarget::has_exited`] anyway.
//! Docker pushes `die` events. AWS has no push stream from the app's point of
//! view; its [`ExitSource::deaths`] is a `DescribeTasks` poll (every 15 s) over
//! the tasks of Running AWS projects that yields the stopped ones, which
//! `container_watch` handles the same way because every report is confirmed
//! with [`ContainerTarget::has_exited`] anyway.
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::Duration;
use futures_util::future::BoxFuture;
use futures_util::{Stream, StreamExt};
use std::pin::Pin;
use super::ContainerTarget;
use crate::docker;
/// Which host's deaths to watch. One variant today.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// How often the AWS watch asks ECS (spec, "Lifecycle and status").
pub const AWS_POLL_INTERVAL: Duration = Duration::from_secs(15);
/// Which host's deaths to watch.
#[derive(Clone)]
pub enum ExitSource {
Docker,
Aws(AwsWatch),
}
impl std::fmt::Debug for ExitSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExitSource::Docker => f.write_str("Docker"),
ExitSource::Aws(w) => write!(f, "Aws(every {:?})", w.interval),
}
}
}
/// Where a task's DescribeTasks goes: the connection and region its
/// credentials and endpoint come from, and the cluster.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TaskGroup {
pub connection_id: String,
pub region: String,
pub cluster_arn: String,
}
/// A Running AWS project's task, and the target a death is reported as.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WatchedTask {
pub target: ContainerTarget,
pub group: TaskGroup,
pub task_arn: String,
}
/// The project-to-task lookup the AWS watch runs on, injected so it can be
/// faked (Task 6 backs it with the tunnel registry / project store and
/// `aws::tasks::stopped_among`).
pub trait AwsTaskLookup: Send + Sync {
/// The tasks of the projects that are Running now. Read on every poll.
fn running_tasks(&self) -> Vec<WatchedTask>;
/// Of `arns` (all in `group`, at most 100), the ones that are STOPPED or
/// gone. `Err` when ECS could not be asked.
fn stopped<'a>(
&'a self,
group: &'a TaskGroup,
arns: &'a [String],
) -> BoxFuture<'a, Result<Vec<String>, String>>;
}
/// The AWS death poll: a lookup and how often to run it.
#[derive(Clone)]
pub struct AwsWatch {
lookup: Arc<dyn AwsTaskLookup>,
interval: Duration,
}
impl AwsWatch {
pub fn new(lookup: Arc<dyn AwsTaskLookup>) -> Self {
Self::every(lookup, AWS_POLL_INTERVAL)
}
pub fn every(lookup: Arc<dyn AwsTaskLookup>, interval: Duration) -> Self {
AwsWatch { lookup, interval }
}
}
/// DescribeTasks' batch limit.
const DESCRIBE_BATCH: usize = 100;
/// One poll: the targets of Running projects whose task stopped. Tasks are
/// asked about per group (connection + region + cluster), 100 at a time. A
/// group that can't be asked (expired SSO, network) is logged and skipped —
/// it confirms nothing, and the other groups are still watched; the next
/// poll asks again.
pub async fn poll_stopped(lookup: &dyn AwsTaskLookup) -> Vec<ContainerTarget> {
let mut groups: HashMap<TaskGroup, Vec<WatchedTask>> = HashMap::new();
for t in lookup.running_tasks() {
groups.entry(t.group.clone()).or_default().push(t);
}
let mut dead = Vec::new();
for (group, tasks) in groups {
for chunk in tasks.chunks(DESCRIBE_BATCH) {
let arns: Vec<String> = chunk.iter().map(|t| t.task_arn.clone()).collect();
match lookup.stopped(&group, &arns).await {
Ok(stopped) => dead.extend(
chunk
.iter()
.filter(|t| stopped.contains(&t.task_arn))
.map(|t| t.target.clone()),
),
Err(e) => log::warn!(
"Container watch: could not check AWS tasks in {} ({}): {e}",
group.region,
group.cluster_arn
),
}
}
}
dead
}
/// Polls forever, `interval` apart (the first after one interval: the
/// watcher sweeps on connect), yielding each stopped task's target.
fn aws_deaths(watch: AwsWatch) -> DeathStream {
Box::pin(futures_util::stream::unfold(
(watch, VecDeque::new()),
|(watch, mut queue)| async move {
loop {
if let Some(t) = queue.pop_front() {
return Some((Ok(t), (watch, queue)));
}
tokio::time::sleep(watch.interval).await;
queue.extend(poll_stopped(&*watch.lookup).await);
}
},
))
}
pub type DeathStream = Pin<Box<dyn Stream<Item = Result<ContainerTarget, String>> + Send>>;
@@ -28,6 +147,9 @@ impl ExitSource {
pub async fn reachable(&self) -> bool {
match self {
ExitSource::Docker => docker::events::ping().await,
// No one endpoint to ping: each poll asks every group, and a
// group that fails is skipped rather than ending the stream.
ExitSource::Aws(_) => true,
}
}
@@ -38,6 +160,9 @@ impl ExitSource {
ExitSource::Docker => Box::pin(
docker::events::container_deaths(since).map(|r| r.map(ContainerTarget::docker)),
),
// ECS has no replay: a task that stopped while the stream was
// down is still STOPPED (or gone) on the next poll.
ExitSource::Aws(watch) => aws_deaths(watch.clone()),
}
}
}
@@ -52,9 +177,158 @@ impl ContainerTarget {
ContainerTarget::Docker { container_id } => docker::events::has_exited(container_id).await,
// The agent can only say "I answer" or "I don't"; silence is not a
// positive exit (an SSM blip looks the same). Only the host that
// runs the container can confirm a death — an AWS host answers
// from ECS — so through the agent alone this never confirms one.
ContainerTarget::Agent(_) => false,
// runs the container can confirm a death: the AWS watch records
// the targets whose task ECS reports STOPPED in the tunnel
// registry (R3), and nothing else ever confirms one.
ContainerTarget::Agent(ep) => super::tunnels::is_stopped(ep.base_url.as_str()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
fn group(conn: &str, region: &str) -> TaskGroup {
TaskGroup {
connection_id: conn.into(),
region: region.into(),
cluster_arn: format!("arn:aws:ecs:{region}:111122223333:cluster/triple-c-dev"),
}
}
fn task(n: usize, g: &TaskGroup) -> WatchedTask {
WatchedTask {
target: ContainerTarget::docker(format!("target-{:012}", n)),
group: g.clone(),
task_arn: format!("arn:aws:ecs:{}:111122223333:task/triple-c-dev/t{n}", g.region),
}
}
/// Running tasks are fixed; `dead` are the stopped ARNs; a group whose
/// region is `failing` errors. Every `stopped` call is recorded.
struct FakeLookup {
tasks: Mutex<Vec<WatchedTask>>,
dead: Vec<String>,
failing: &'static str,
asked: Mutex<Vec<(TaskGroup, usize)>>,
polls: Mutex<usize>,
}
impl FakeLookup {
fn new(tasks: Vec<WatchedTask>, dead: Vec<String>) -> Self {
FakeLookup {
tasks: Mutex::new(tasks),
dead,
failing: "",
asked: Mutex::new(Vec::new()),
polls: Mutex::new(0),
}
}
}
impl AwsTaskLookup for FakeLookup {
fn running_tasks(&self) -> Vec<WatchedTask> {
*self.polls.lock().unwrap() += 1;
self.tasks.lock().unwrap().clone()
}
fn stopped<'a>(
&'a self,
group: &'a TaskGroup,
arns: &'a [String],
) -> BoxFuture<'a, Result<Vec<String>, String>> {
Box::pin(async move {
self.asked.lock().unwrap().push((group.clone(), arns.len()));
if group.region == self.failing {
return Err("SSO session expired".into());
}
Ok(arns.iter().filter(|a| self.dead.contains(a)).cloned().collect())
})
}
}
#[tokio::test]
async fn a_poll_asks_each_group_once_and_reports_only_stopped_tasks() {
let (a, b) = (group("c1", "eu-west-1"), group("c2", "us-east-1"));
let tasks = vec![task(1, &a), task(2, &b), task(3, &a)];
let dead = vec![tasks[0].task_arn.clone(), tasks[1].task_arn.clone()];
let lookup = FakeLookup::new(tasks.clone(), dead);
let mut got = poll_stopped(&lookup).await;
got.sort_by(|x, y| x.id().cmp(y.id()));
assert_eq!(got, vec![tasks[0].target.clone(), tasks[1].target.clone()]);
let mut asked = lookup.asked.lock().unwrap().clone();
asked.sort_by(|x, y| x.0.region.cmp(&y.0.region));
assert_eq!(asked, vec![(a, 2), (b, 1)]);
}
#[tokio::test]
async fn a_group_that_cannot_be_asked_confirms_nothing_and_the_others_still_report() {
let (a, b) = (group("c1", "eu-west-1"), group("c2", "us-east-1"));
let tasks = vec![task(1, &a), task(2, &b)];
let dead = tasks.iter().map(|t| t.task_arn.clone()).collect();
let mut lookup = FakeLookup::new(tasks.clone(), dead);
lookup.failing = "eu-west-1";
assert_eq!(poll_stopped(&lookup).await, vec![tasks[1].target.clone()]);
}
#[tokio::test]
async fn tasks_are_described_a_hundred_at_a_time() {
let a = group("c1", "eu-west-1");
let lookup = FakeLookup::new((0..250).map(|n| task(n, &a)).collect(), Vec::new());
assert!(poll_stopped(&lookup).await.is_empty());
let sizes: Vec<usize> = lookup.asked.lock().unwrap().iter().map(|(_, n)| *n).collect();
assert_eq!(sizes, vec![100, 100, 50]);
}
#[tokio::test]
async fn no_running_aws_projects_means_no_aws_calls() {
let lookup = FakeLookup::new(Vec::new(), Vec::new());
assert!(poll_stopped(&lookup).await.is_empty());
assert!(lookup.asked.lock().unwrap().is_empty());
}
#[tokio::test(start_paused = true)]
async fn the_aws_stream_polls_every_interval_and_yields_each_death() {
let a = group("c1", "eu-west-1");
let tasks = vec![task(1, &a), task(2, &a)];
let lookup = Arc::new(FakeLookup::new(
tasks.clone(),
vec![tasks[0].task_arn.clone(), tasks[1].task_arn.clone()],
));
let source = ExitSource::Aws(AwsWatch::new(lookup.clone()));
assert!(source.reachable().await);
let mut deaths = source.deaths(0);
let t0 = tokio::time::Instant::now();
let first = deaths.next().await.unwrap().unwrap();
assert_eq!(t0.elapsed(), AWS_POLL_INTERVAL, "the first poll waits one interval");
let second = deaths.next().await.unwrap().unwrap();
assert_eq!(t0.elapsed(), AWS_POLL_INTERVAL, "one poll, both reported");
let mut got = vec![first, second];
got.sort_by(|x, y| x.id().cmp(y.id()));
assert_eq!(got, vec![tasks[0].target.clone(), tasks[1].target.clone()]);
assert_eq!(*lookup.polls.lock().unwrap(), 1);
// Once the projects are no longer Running, polls go on and report nothing.
lookup.tasks.lock().unwrap().clear();
let quiet = tokio::time::timeout(AWS_POLL_INTERVAL * 3 + Duration::from_secs(1), deaths.next()).await;
assert!(quiet.is_err(), "nothing to report");
assert_eq!(*lookup.polls.lock().unwrap(), 4);
}
#[test]
fn the_aws_poll_runs_every_fifteen_seconds() {
assert_eq!(AWS_POLL_INTERVAL, Duration::from_secs(15));
}
#[tokio::test]
async fn an_agent_target_never_confirms_an_exit_on_its_own() {
// R3: Task 6 wires the tunnel registry into this arm.
let ep = crate::host::agent::AgentEndpoint::new(
url::Url::parse("http://127.0.0.1:47901").unwrap(),
"fake-token",
)
.unwrap();
assert!(!ContainerTarget::Agent(ep).has_exited().await);
}
}
+27
View File
@@ -329,6 +329,20 @@ pub fn run() {
lifecycle_setup.track(container_watch::spawn(
app.handle().clone(),
projects_store_setup.clone(),
crate::host::watch::ExitSource::Docker,
lifecycle_setup.cancellation(),
));
// The same for AWS projects: a DescribeTasks poll every 15 s over
// the tasks of Running AWS projects (none: no AWS calls at all).
lifecycle_setup.track(container_watch::spawn(
app.handle().clone(),
projects_store_setup.clone(),
crate::host::watch::ExitSource::Aws(crate::host::watch::AwsWatch::new(
std::sync::Arc::new(crate::host::aws_host::StoreTaskLookup {
projects: projects_store_setup.clone(),
settings: settings_store_setup.clone(),
}),
)),
lifecycle_setup.cancellation(),
));
@@ -508,6 +522,9 @@ pub fn run() {
exec_manager.close_all_sessions(),
auth_bridge.stop_all(),
browser_view::manager().stop_all(),
// Each AWS tunnel is a session-manager-plugin
// child that `exit` would not end.
crate::host::tunnels::close_all(),
);
};
@@ -534,8 +551,16 @@ pub fn run() {
commands::project_commands::update_project,
commands::project_commands::start_project_container,
commands::project_commands::stop_project_container,
commands::project_commands::check_unsaved_work,
commands::project_commands::aws_task_log,
commands::project_commands::aws_workspace_status,
commands::project_commands::aws_environment_max_hours,
commands::project_commands::aws_ec2_capacity,
commands::project_commands::ec2_fit_problem,
commands::project_commands::remove_aws_resources,
commands::project_commands::rebuild_project_container,
commands::project_commands::reconcile_project_statuses,
commands::project_commands::reconcile_aws_projects,
// Notes
commands::notes_commands::list_notes,
commands::notes_commands::save_note,
@@ -610,6 +635,7 @@ pub fn run() {
commands::host_commands::aws_connection_sso_login,
commands::host_commands::check_aws_permissions,
commands::host_commands::aws_policy_document,
commands::host_commands::aws_cost_estimate,
commands::environment_commands::detect_aws_environments,
commands::environment_commands::deploy_aws_environment,
commands::environment_commands::update_aws_environment,
@@ -620,6 +646,7 @@ pub fn run() {
commands::environment_commands::cancel_aws_deployment,
commands::environment_commands::push_aws_saved_state,
commands::environment_commands::onboard_aws_user,
commands::environment_commands::list_ec2_instance_types,
commands::environment_commands::deploy_aws_permissions_boundary,
commands::environment_commands::save_aws_admin_bundle,
commands::settings_commands::detect_host_timezone,
+818
View File
@@ -452,10 +452,510 @@ pub struct Project {
/// Global marketplace installs this project opts out of.
#[serde(default)]
pub marketplace_disabled: Vec<super::marketplace::MarketplaceItemRef>,
/// The host connection this project runs on. `None` is the local Docker
/// host; `Some(id)` names an entry of `AppSettings.hosts`.
#[serde(default)]
pub host_id: Option<String>,
/// Region and environment of an AWS-hosted project. Set together with
/// `host_id` and `aws`, or not at all.
#[serde(default)]
pub aws_placement: Option<AwsPlacement>,
#[serde(default)]
pub aws: Option<AwsWorkspaceConfig>,
pub created_at: String,
pub updated_at: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AwsPlacement {
pub region: String,
pub environment: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum Arch {
#[default]
X86_64,
Arm64,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum StorageMode {
Ephemeral,
#[default]
Hybrid,
/// Hybrid plus `/workspace` on EFS (a second access point).
Efs,
}
/// How an AWS project's credentials and files reach the workspace.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum SecretsDelivery {
/// In the project's Secrets Manager secret.
#[default]
SecretsManager,
/// Kept out of the secret; the app copies them in after each connect.
CopyAtConnect,
}
/// One git repository cloned into the workspace. `branch: None` means the
/// remote's default branch.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RepoSource {
pub url: String,
#[serde(default)]
pub branch: Option<String>,
pub folder: String,
}
fn default_aws_cpu() -> u32 {
1024
}
fn default_aws_memory_mib() -> u32 {
4096
}
fn default_aws_ephemeral_gib() -> u32 {
21
}
/// Where an AWS workspace's task runs.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LaunchType {
#[default]
Fargate,
Ec2,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AwsWorkspaceConfig {
/// Fargate (default; absent in old JSON) or the user's own EC2 instances.
#[serde(default)]
pub launch_type: LaunchType,
#[serde(default = "default_aws_cpu")]
pub cpu: u32,
#[serde(default = "default_aws_memory_mib")]
pub memory_mib: u32,
#[serde(default)]
pub arch: Arch,
#[serde(default)]
pub storage: StorageMode,
#[serde(default = "default_aws_ephemeral_gib")]
pub ephemeral_gib: u32,
#[serde(default)]
pub spot: bool,
#[serde(default)]
pub idle_stop_minutes: Option<u32>,
#[serde(default)]
pub repos: Vec<RepoSource>,
/// Push uncommitted work to a WIP branch when the task stops. Defaults on.
#[serde(default = "default_full_permissions")]
pub wip_push_on_stop: bool,
/// Store-owned runtime state: see `ProjectsStore::set_aws_runtime`.
#[serde(default)]
pub task_arn: Option<String>,
#[serde(default)]
pub access_point_id: Option<String>,
#[serde(default)]
pub task_definition_arn: Option<String>,
/// Store-owned like `access_point_id`: the EFS-mode `/workspace` access point.
#[serde(default)]
pub workspace_access_point_id: Option<String>,
#[serde(default)]
pub secrets_delivery: SecretsDelivery,
/// Run after the clone, as `claude` (AWS only).
#[serde(default)]
pub setup_script: Option<String>,
/// Run the LiteLLM gateway as a sidecar container.
#[serde(default)]
pub gateway_sidecar: bool,
}
/// Largest setup script, in bytes.
pub const MAX_SETUP_SCRIPT_BYTES: usize = 32_768;
impl Default for AwsWorkspaceConfig {
fn default() -> Self {
Self {
launch_type: LaunchType::default(),
cpu: default_aws_cpu(),
memory_mib: default_aws_memory_mib(),
arch: Arch::default(),
storage: StorageMode::default(),
ephemeral_gib: default_aws_ephemeral_gib(),
spot: false,
idle_stop_minutes: None,
repos: Vec::new(),
wip_push_on_stop: true,
task_arn: None,
access_point_id: None,
task_definition_arn: None,
workspace_access_point_id: None,
secrets_delivery: SecretsDelivery::default(),
setup_script: None,
gateway_sidecar: false,
}
}
}
/// The valid Fargate memory sizes (MiB) for a CPU size, smallest first.
/// 256 CPU is the irregular one: 512, 1024 or 2048 only.
fn fargate_memory_sizes(cpu: u32) -> Option<Vec<u32>> {
let (min, max, step) = match cpu {
256 => return Some(vec![512, 1024, 2048]),
512 => (1024, 4096, 1024),
1024 => (2048, 8192, 1024),
2048 => (4096, 16384, 1024),
4096 => (8192, 30720, 1024),
8192 => (16384, 61440, 4096),
16384 => (32768, 122880, 8192),
_ => return None,
};
Some((min..=max).step_by(step as usize).collect())
}
fn validate_repo_url(url: &str) -> Result<(), String> {
let bad = |m: &str| Err(format!("Repository URL \"{}\": {}", url.escape_debug(), m));
if url.is_empty() || url.len() > 2048 {
return bad("must be 1 to 2048 characters");
}
if url.chars().any(|c| c.is_control() || c.is_whitespace()) {
return bad("must not contain whitespace or control characters");
}
if url.starts_with('-') {
return bad("must not start with '-'");
}
let ok = if let Some(rest) = url.strip_prefix("https://") {
// Credentials in the URL would sit in the project secret and in
// `.git/config` in the clear, and leak into every clone's remote.
let authority = rest.split(['/', '?', '#']).next().unwrap_or("");
if authority.contains('@') {
return bad(
"must not carry credentials (user:password@ or token@); put the token in the \
project's Git HTTPS token setting (Access) instead",
);
}
!rest.is_empty()
} else if let Some(rest) = url.strip_prefix("ssh://") {
!rest.is_empty()
} else if let Some((host, path)) = url.split_once(':') {
// scp-like `git@host:path`
!host.is_empty() && !host.contains('/') && !path.is_empty() && host.contains('@')
} else {
false
};
if ok {
Ok(())
} else {
bad("use https://..., ssh://... or git@host:path")
}
}
/// `git check-ref-format --branch` rules.
fn validate_branch(branch: &str) -> Result<(), String> {
let bad = |m: &str| Err(format!("Branch \"{}\": {}", branch.escape_debug(), m));
if branch.is_empty() || branch.len() > 255 {
return bad("must be 1 to 255 characters");
}
if branch.starts_with('-') || branch.starts_with('/') || branch.ends_with('/') {
return bad("must not start with '-' or start/end with '/'");
}
// `HEAD` passes check-ref-format but `git clone --branch HEAD` fails
// (and a branch named HEAD is refused by `git branch`).
if branch == "HEAD" {
return bad("HEAD is not a branch; leave the branch empty for the default branch");
}
if branch == "@" || branch.ends_with('.') || branch.contains("..") || branch.contains("@{") || branch.contains("//") {
return bad("is not a valid branch name");
}
if branch
.chars()
.any(|c| c.is_control() || c.is_whitespace() || "~^:?*[\\".contains(c))
{
return bad("contains a character git does not allow");
}
for comp in branch.split('/') {
if comp.starts_with('.') || comp.ends_with(".lock") {
return bad("components must not start with '.' or end with '.lock'");
}
}
Ok(())
}
fn validate_repo_folder(folder: &str) -> Result<(), String> {
let ok = !folder.is_empty()
&& folder.len() <= 64
&& folder != "."
&& folder != ".."
&& folder
.bytes()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, b'.' | b'_' | b'-'));
if ok {
Ok(())
} else {
Err(format!(
"Repository folder \"{}\" must be 1 to 64 characters of letters, digits, '.', '_' or '-', and not '.' or '..'",
folder.escape_debug()
))
}
}
/// "512, 1024 or 2048 MiB", or "2048 to 8192 MiB in steps of 1024".
fn describe_sizes(sizes: &[u32]) -> String {
let (first, last) = (sizes[0], sizes[sizes.len() - 1]);
if sizes.len() <= 3 {
let head: Vec<String> = sizes[..sizes.len() - 1].iter().map(u32::to_string).collect();
return format!("{} or {} MiB", head.join(", "), last);
}
format!("{} to {} MiB in steps of {}", first, last, sizes[1] - sizes[0])
}
/// CPU units and memory (MiB) held back on an EC2 instance for the ECS agent
/// and the OS.
pub const EC2_RESERVED_CPU_UNITS: u32 = 128;
pub const EC2_RESERVED_MEMORY_MIB: u32 = 256;
/// The instance families the built-in table knows, in the order the app
/// lists them; a family ending in `g` is Graviton (ARM).
const EC2_FAMILIES: [&str; 8] = ["m7i", "c7i", "r7i", "t3", "m7g", "c7g", "r7g", "t4g"];
const EC2_SIZES: [&str; 4] = ["large", "xlarge", "2xlarge", "4xlarge"];
/// The instance type onboarding offers first (x86; `m7g.large` is its ARM
/// counterpart).
pub const DEFAULT_EC2_INSTANCE_TYPE: &str = "m7i.large";
/// One row of the built-in instance table, for the onboarding UI.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct Ec2InstanceType {
pub instance_type: String,
pub arch: Arch,
pub vcpu: u32,
pub memory_mib: u32,
}
/// Every type [`ec2_instance_spec`] knows (x86 families first), so the UI's
/// list and the app's validation share one table.
pub fn ec2_instance_types() -> Vec<Ec2InstanceType> {
EC2_FAMILIES
.iter()
.flat_map(|family| EC2_SIZES.iter().map(move |size| format!("{family}.{size}")))
.filter_map(|instance_type| {
let (cpu, memory_mib) = ec2_instance_spec(&instance_type)?;
let arch = if instance_type.split('.').next()?.ends_with('g') {
Arch::Arm64
} else {
Arch::X86_64
};
Some(Ec2InstanceType {
instance_type,
arch,
vcpu: cpu / 1024,
memory_mib,
})
})
.collect()
}
/// `(vcpu * 1024, memory MiB)` of a common EC2 instance type, or None when the
/// type is not in the built-in table (m7i, m7g, c7i, c7g, r7i, r7g, t3, t4g).
pub fn ec2_instance_spec(instance_type: &str) -> Option<(u32, u32)> {
let (family, size) = instance_type.split_once('.')?;
let vcpu: u32 = match size {
"large" => 2,
"xlarge" => 4,
"2xlarge" => 8,
"4xlarge" => 16,
_ => return None,
};
// GiB per vCPU: general purpose 4, compute 2, memory 8.
let gib_per_vcpu: u32 = match family {
"m7i" | "m7g" => 4,
"c7i" | "c7g" => 2,
"r7i" | "r7g" => 8,
"t3" | "t4g" => {
// Burstable families: memory is 4 GiB per vCPU at xlarge and above,
// 8 GiB at large; AWS offers no 4xlarge in these families.
return match size {
"large" => Some((2048, 8192)),
"xlarge" => Some((4096, 16384)),
"2xlarge" => Some((8192, 32768)),
_ => None,
};
}
_ => return None,
};
Some((vcpu * 1024, vcpu * gib_per_vcpu * 1024))
}
/// Whether an EC2 workspace's reservations fit an instance type.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Fit {
Ok,
/// The reservations exceed what the instance leaves for tasks.
TooBig { available_cpu: u32, available_memory_mib: u32 },
/// The type is not in the table: the caller shows a "must fit" warning.
UnknownType,
}
/// Memory (MiB) an instance of `memory_mib` nominal memory keeps from tasks
/// in [`fits_instance`]: a tenth of it (what ECS doesn't register, with
/// room to spare), and never less than [`EC2_RESERVED_MEMORY_MIB`].
pub fn ec2_memory_reserve_mib(memory_mib: u32) -> u32 {
(memory_mib / 10).max(EC2_RESERVED_MEMORY_MIB)
}
/// Check an EC2 config's reservations against `instance_type`, less the agent
/// and OS reserve. A Fargate config always fits (no instance involved).
///
/// Nominal sizes only, so memory keeps a margin: ECS registers less memory
/// than the instance's nominal (the ECS-optimized AMI and kernel keep some —
/// about 7.5 GiB of an 8 GiB type is registered), and a task asking for more
/// than the registered amount stays PROVISIONING with nothing able to place
/// it. The registered figure can't be read before an instance exists (scale
/// to zero), so the reserve is [`ec2_memory_reserve_mib`]: 10 % of the
/// nominal memory, at least [`EC2_RESERVED_MEMORY_MIB`].
pub fn fits_instance(cfg: &AwsWorkspaceConfig, instance_type: &str) -> Fit {
if cfg.launch_type != LaunchType::Ec2 {
return Fit::Ok;
}
let Some((available_cpu, available_memory_mib)) = ec2_available(instance_type) else {
return Fit::UnknownType;
};
if cfg.cpu > available_cpu || cfg.memory_mib > available_memory_mib {
Fit::TooBig { available_cpu, available_memory_mib }
} else {
Fit::Ok
}
}
/// What `instance_type` leaves for one workspace's reservations, as
/// `(CPU units, MiB)`: [`fits_instance`]'s limits (the agent's CPU and the
/// memory margin taken off). `None` for a type not in the table.
pub fn ec2_available(instance_type: &str) -> Option<(u32, u32)> {
let (cpu, mem) = ec2_instance_spec(instance_type)?;
Some((cpu - EC2_RESERVED_CPU_UNITS, mem - ec2_memory_reserve_mib(mem)))
}
/// Why `cfg`'s reservations don't fit `instance_type`, or `None` when they
/// do (or the type is unknown, or `cfg` is Fargate). The one wording of the
/// fit rule: the start's refusal and Host settings both use it.
pub fn ec2_fit_problem(cfg: &AwsWorkspaceConfig, instance_type: &str) -> Option<String> {
match fits_instance(cfg, instance_type) {
Fit::TooBig { available_cpu, available_memory_mib } => Some(format!(
"This project reserves {} CPU units and {} MiB of memory, more than your EC2 \
instance type {instance_type} has for a workspace ({available_cpu} CPU units, \
{available_memory_mib} MiB).",
cfg.cpu, cfg.memory_mib
)),
Fit::Ok | Fit::UnknownType => None,
}
}
pub fn validate_aws_workspace(cfg: &AwsWorkspaceConfig) -> Result<(), String> {
match cfg.launch_type {
LaunchType::Fargate => {
let sizes = fargate_memory_sizes(cfg.cpu).ok_or_else(|| {
format!(
"{} is not a Fargate CPU size (256, 512, 1024, 2048, 4096, 8192 or 16384)",
cfg.cpu
)
})?;
if !sizes.contains(&cfg.memory_mib) {
return Err(format!(
"{} MiB of memory is not valid with {} CPU units: use {}",
cfg.memory_mib,
cfg.cpu,
describe_sizes(&sizes)
));
}
if !(21..=200).contains(&cfg.ephemeral_gib) {
return Err("Ephemeral storage must be 21 to 200 GiB".into());
}
}
LaunchType::Ec2 => {
// Reservations on the user's instance; the instance type is not on
// the project, so `fits_instance` does the fit check later.
if cfg.cpu == 0 || cfg.memory_mib == 0 {
return Err("An EC2 workspace needs a positive CPU and memory reservation".into());
}
if cfg.spot {
return Err("Spot is not available on EC2 yet".into());
}
}
}
if let Some(m) = cfg.idle_stop_minutes {
if !(5..=1440).contains(&m) {
return Err("Idle stop must be 5 to 1440 minutes".into());
}
}
if cfg.repos.len() > 20 {
return Err("At most 20 repositories are allowed".into());
}
let mut seen = std::collections::HashSet::new();
for r in &cfg.repos {
validate_repo_url(&r.url)?;
if let Some(b) = &r.branch {
validate_branch(b)?;
}
validate_repo_folder(&r.folder)?;
if !seen.insert(r.folder.as_str()) {
return Err(format!("Repository folder \"{}\" is used twice", r.folder));
}
}
if let Some(script) = &cfg.setup_script {
if script.len() > MAX_SETUP_SCRIPT_BYTES {
return Err(format!(
"The setup script is {} bytes; the limit is {}",
script.len(),
MAX_SETUP_SCRIPT_BYTES
));
}
if script.contains('\0') {
return Err("The setup script must not contain NUL characters".into());
}
}
if cfg.gateway_sidecar && (cfg.cpu < 1024 || cfg.memory_mib < 2048) {
return Err("The LLM gateway sidecar needs at least 1 vCPU (1024) and 2 GiB (2048 MiB)".into());
}
Ok(())
}
/// What Stop does with an AWS workspace's unsaved git work (the stop guard).
/// A local project ignores it.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum WipChoice {
/// Run `triple-c-wip-push --reason stop` first; a failure aborts the stop.
Push,
/// Stop without pushing from the app.
Discard,
}
/// One repository line of `triple-c-wip-push --dry-run`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RepoState {
pub folder: String,
pub dirty: bool,
pub unpushed: u64,
pub branch: String,
/// The start's background clone is still writing it: nothing to push.
#[serde(default)]
pub cloning: bool,
}
/// What `check_unsaved_work` found. `checked: false` (with `error`) when the
/// workspace could not be asked — the stop guard then cannot offer a push.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct UnsavedWork {
pub checked: bool,
pub repos: Vec<RepoState>,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ProjectStatus {
@@ -701,6 +1201,9 @@ impl Project {
renamed_session_names: HashMap::new(),
marketplace_installs: Vec::new(),
marketplace_disabled: Vec::new(),
host_id: None,
aws_placement: None,
aws: None,
created_at: now.clone(),
updated_at: now,
}
@@ -717,6 +1220,11 @@ impl Project {
})
}
/// Whether this project runs on an AWS host rather than local Docker.
pub fn is_aws(&self) -> bool {
self.host_id.is_some()
}
pub fn container_name(&self) -> String {
format!("triple-c-{}", self.id)
}
@@ -1011,4 +1519,314 @@ mod tests {
assert!(!obj.contains_key("auth_bridge_enabled"));
assert!(!obj.contains_key("browser_view_enabled"));
}
// ── AWS host ────────────────────────────────────────────────────────────
fn cfg(cpu: u32, mem: u32) -> AwsWorkspaceConfig {
AwsWorkspaceConfig { cpu, memory_mib: mem, ..Default::default() }
}
fn repo(url: &str, branch: Option<&str>, folder: &str) -> RepoSource {
RepoSource { url: url.into(), branch: branch.map(String::from), folder: folder.into() }
}
#[test]
fn an_old_project_without_host_fields_loads_as_local() {
let p = Project::new("demo".to_string(), Vec::new());
let mut v = serde_json::to_value(&p).unwrap();
let o = v.as_object_mut().unwrap();
o.remove("host_id");
o.remove("aws_placement");
o.remove("aws");
let loaded: Project = serde_json::from_value(v).unwrap();
assert_eq!(loaded.host_id, None);
assert!(loaded.aws.is_none() && loaded.aws_placement.is_none());
assert!(!loaded.is_aws());
}
#[test]
fn an_empty_aws_object_gets_the_defaults() {
let c: AwsWorkspaceConfig = serde_json::from_str("{}").unwrap();
assert!(c.wip_push_on_stop);
assert_eq!((c.cpu, c.memory_mib, c.ephemeral_gib), (1024, 4096, 21));
assert_eq!(c.arch, Arch::X86_64);
assert_eq!(c.storage, StorageMode::Hybrid);
assert!(!c.spot && c.idle_stop_minutes.is_none() && c.repos.is_empty());
assert!(validate_aws_workspace(&c).is_ok());
let off: AwsWorkspaceConfig = serde_json::from_str(r#"{"wip_push_on_stop":false}"#).unwrap();
assert!(!off.wip_push_on_stop);
}
#[test]
fn phase6_fields_default_for_old_json_and_efs_round_trips() {
let c: AwsWorkspaceConfig = serde_json::from_str(r#"{"storage":"hybrid","cpu":512}"#).unwrap();
assert_eq!(c.secrets_delivery, SecretsDelivery::SecretsManager);
assert!(!c.gateway_sidecar && c.setup_script.is_none() && c.workspace_access_point_id.is_none());
let e: AwsWorkspaceConfig = serde_json::from_str(r#"{"storage":"efs","secrets_delivery":"copy_at_connect"}"#).unwrap();
assert_eq!(e.storage, StorageMode::Efs);
assert_eq!(e.secrets_delivery, SecretsDelivery::CopyAtConnect);
assert_eq!(serde_json::to_string(&StorageMode::Efs).unwrap(), "\"efs\"");
assert_eq!(serde_json::to_string(&SecretsDelivery::CopyAtConnect).unwrap(), "\"copy_at_connect\"");
let back: AwsWorkspaceConfig = serde_json::from_str(&serde_json::to_string(&e).unwrap()).unwrap();
assert_eq!(back, e);
}
#[test]
fn setup_script_limits() {
let mut c = AwsWorkspaceConfig::default();
c.setup_script = Some("a".repeat(MAX_SETUP_SCRIPT_BYTES));
assert!(validate_aws_workspace(&c).is_ok());
c.setup_script = Some("a".repeat(MAX_SETUP_SCRIPT_BYTES + 1));
assert!(validate_aws_workspace(&c).unwrap_err().contains("limit"));
c.setup_script = Some("echo\0hi".into());
assert!(validate_aws_workspace(&c).unwrap_err().contains("NUL"));
}
#[test]
fn gateway_sidecar_needs_one_vcpu_and_two_gib() {
let mut c = cfg(512, 2048);
c.gateway_sidecar = true;
assert!(validate_aws_workspace(&c).is_err());
c = cfg(1024, 2048);
c.gateway_sidecar = true;
assert!(validate_aws_workspace(&c).is_ok());
c = cfg(256, 2048);
c.gateway_sidecar = true;
assert!(validate_aws_workspace(&c).is_err());
}
#[test]
fn enums_serialize_snake_case_and_is_aws_follows_host_id() {
assert_eq!(serde_json::to_string(&Arch::Arm64).unwrap(), "\"arm64\"");
assert_eq!(serde_json::to_string(&Arch::X86_64).unwrap(), "\"x86_64\"");
assert_eq!(serde_json::to_string(&StorageMode::Hybrid).unwrap(), "\"hybrid\"");
let mut p = Project::new("d".into(), Vec::new());
p.host_id = Some("h1".into());
assert!(p.is_aws());
}
fn ec2(cpu: u32, mem: u32) -> AwsWorkspaceConfig {
AwsWorkspaceConfig { launch_type: LaunchType::Ec2, cpu, memory_mib: mem, ..Default::default() }
}
#[test]
fn old_json_is_fargate_and_ec2_round_trips() {
let c: AwsWorkspaceConfig = serde_json::from_str("{}").unwrap();
assert_eq!(c.launch_type, LaunchType::Fargate);
let e: AwsWorkspaceConfig = serde_json::from_str(r#"{"launch_type":"ec2"}"#).unwrap();
assert_eq!(e.launch_type, LaunchType::Ec2);
assert!(serde_json::to_string(&e).unwrap().contains(r#""launch_type":"ec2""#));
}
#[test]
fn ec2_validation_shape() {
// Reservations need not be a Fargate pair.
assert!(validate_aws_workspace(&ec2(1500, 3000)).is_ok());
assert!(validate_aws_workspace(&ec2(0, 3000)).is_err());
assert!(validate_aws_workspace(&ec2(1024, 0)).is_err());
let mut s = ec2(1024, 2048);
s.spot = true;
assert!(validate_aws_workspace(&s).unwrap_err().contains("Spot"));
// Ephemeral size is ignored on EC2.
let mut e = ec2(1024, 2048);
e.ephemeral_gib = 5;
assert!(validate_aws_workspace(&e).is_ok());
// Fargate still rejects the non-pair.
assert!(validate_aws_workspace(&cfg(1500, 3000)).is_err());
}
#[test]
fn the_instance_type_list_is_the_table() {
let list = ec2_instance_types();
assert_eq!(list.len(), 30);
for t in &list {
assert_eq!(
ec2_instance_spec(&t.instance_type),
Some((t.vcpu * 1024, t.memory_mib)),
"{}",
t.instance_type
);
}
// And the other way: every type the spec table sizes is listed (a
// family added to one but not the other fails here).
let families = [
"m7i", "m7g", "m7a", "m6i", "c7i", "c7g", "c7a", "r7i", "r7g", "r7a", "t3", "t3a",
"t4g", "p5", "g6",
];
for family in families {
for size in ["medium", "large", "xlarge", "2xlarge", "4xlarge", "8xlarge", "metal"] {
let t = format!("{family}.{size}");
if ec2_instance_spec(&t).is_some() {
assert!(list.iter().any(|row| row.instance_type == t), "{t} missing");
}
}
}
let arch = |name: &str| list.iter().find(|t| t.instance_type == name).unwrap().arch;
assert_eq!(arch("m7i.large"), Arch::X86_64);
assert_eq!(arch("t3.xlarge"), Arch::X86_64);
assert_eq!(arch("m7g.large"), Arch::Arm64);
assert_eq!(arch("t4g.2xlarge"), Arch::Arm64);
assert!(list.iter().any(|t| t.instance_type == DEFAULT_EC2_INSTANCE_TYPE));
assert_eq!(arch(DEFAULT_EC2_INSTANCE_TYPE), Arch::X86_64);
let first = &list[0];
assert_eq!(
serde_json::to_value(first).unwrap(),
serde_json::json!({"instance_type": "m7i.large", "arch": "x86_64", "vcpu": 2, "memory_mib": 8192})
);
}
#[test]
fn instance_table_values() {
assert_eq!(ec2_instance_spec("m7i.large"), Some((2048, 8192)));
assert_eq!(ec2_instance_spec("m7g.4xlarge"), Some((16384, 65536)));
assert_eq!(ec2_instance_spec("c7i.xlarge"), Some((4096, 8192)));
assert_eq!(ec2_instance_spec("r7g.2xlarge"), Some((8192, 65536)));
assert_eq!(ec2_instance_spec("t3.large"), Some((2048, 8192)));
assert_eq!(ec2_instance_spec("t4g.2xlarge"), Some((8192, 32768)));
assert_eq!(ec2_instance_spec("t3.4xlarge"), None);
assert_eq!(ec2_instance_spec("p5.48xlarge"), None);
assert_eq!(ec2_instance_spec("m7i"), None);
}
#[test]
fn fit_checks_reserve_for_the_agent() {
// m7i.large: 2048 / 8192 less 128 CPU units and 10 % of the memory
// (819 MiB: ECS registers less than the nominal 8 GiB).
assert_eq!(fits_instance(&ec2(1920, 7373), "m7i.large"), Fit::Ok);
assert_eq!(
fits_instance(&ec2(1921, 4096), "m7i.large"),
Fit::TooBig { available_cpu: 1920, available_memory_mib: 7373 }
);
assert!(matches!(fits_instance(&ec2(1024, 7374), "m7i.large"), Fit::TooBig { .. }));
// The reserve is never under the agent's 256 MiB, and grows with
// the instance (r7i.4xlarge: 128 GiB nominal).
assert_eq!(ec2_memory_reserve_mib(2048), 256);
assert_eq!(ec2_memory_reserve_mib(131072), 13107);
assert_eq!(fits_instance(&ec2(1024, 2048), "z9.huge"), Fit::UnknownType);
// Fargate configs have no instance to fit.
assert_eq!(fits_instance(&cfg(16384, 122880), "m7i.large"), Fit::Ok);
}
#[test]
fn the_fit_problem_words_the_fit_rule_once() {
assert_eq!(ec2_available("m7i.large"), Some((1920, 7373)));
assert_eq!(ec2_available("t4g.large"), Some((1920, 7373)));
assert_eq!(ec2_available("z9.huge"), None);
assert_eq!(ec2_fit_problem(&ec2(1920, 7373), "m7i.large"), None);
assert_eq!(
ec2_fit_problem(&ec2(1024, 7374), "m7i.large").as_deref(),
Some(
"This project reserves 1024 CPU units and 7374 MiB of memory, more than your EC2 \
instance type m7i.large has for a workspace (1920 CPU units, 7373 MiB)."
)
);
// Unknown types and Fargate configs: no problem to report here.
assert_eq!(ec2_fit_problem(&ec2(99999, 99999), "z9.huge"), None);
assert_eq!(ec2_fit_problem(&cfg(16384, 122880), "m7i.large"), None);
}
#[test]
fn cpu_and_memory_must_be_a_fargate_pair() {
// Shared with the frontend's copy (`lib/awsWorkspace.test.ts`).
let fixture: serde_json::Value =
serde_json::from_str(include_str!("../../../src/test/fixtures/fargate-pairs.json")).unwrap();
let pairs = |k: &str| -> Vec<(u32, u32)> {
fixture[k]
.as_array()
.unwrap()
.iter()
.map(|p| (p[0].as_u64().unwrap() as u32, p[1].as_u64().unwrap() as u32))
.collect()
};
for (c, m) in pairs("valid") {
assert!(validate_aws_workspace(&cfg(c, m)).is_ok(), "{c}/{m} should be valid");
}
for (c, m) in pairs("invalid") {
assert!(validate_aws_workspace(&cfg(c, m)).is_err(), "{c}/{m} should be rejected");
}
let e = validate_aws_workspace(&cfg(256, 1536)).unwrap_err();
assert!(e.ends_with("use 512, 1024 or 2048 MiB"), "{e}");
let e = validate_aws_workspace(&cfg(1024, 2500)).unwrap_err();
assert!(e.ends_with("use 2048 to 8192 MiB in steps of 1024"), "{e}");
}
#[test]
fn ephemeral_storage_is_21_to_200() {
for (g, ok) in [(20, false), (21, true), (200, true), (201, false)] {
let c = AwsWorkspaceConfig { ephemeral_gib: g, ..Default::default() };
assert_eq!(validate_aws_workspace(&c).is_ok(), ok, "{g}");
}
}
#[test]
fn idle_stop_is_5_to_1440_or_off() {
for (m, ok) in [(None, true), (Some(4), false), (Some(5), true), (Some(1440), true), (Some(1441), false)] {
let c = AwsWorkspaceConfig { idle_stop_minutes: m, ..Default::default() };
assert_eq!(validate_aws_workspace(&c).is_ok(), ok, "{m:?}");
}
}
fn with_repos(repos: Vec<RepoSource>) -> AwsWorkspaceConfig {
AwsWorkspaceConfig { repos, ..Default::default() }
}
#[test]
fn repo_folders_follow_the_charset_and_are_unique() {
let ok = |f: &str| validate_aws_workspace(&with_repos(vec![repo("https://h/r.git", None, f)])).is_ok();
assert!(ok("my-repo_1.x"));
assert!(ok(&"a".repeat(64)));
for bad in ["", ".", "..", "a/b", "a b", "ü", &"a".repeat(65)] {
assert!(!ok(bad), "{bad:?}");
}
let dup = with_repos(vec![repo("https://h/a", None, "x"), repo("https://h/b", None, "x")]);
assert!(validate_aws_workspace(&dup).unwrap_err().contains("twice"));
let two = with_repos(vec![repo("https://h/a", None, "x"), repo("https://h/b", None, "y")]);
assert!(validate_aws_workspace(&two).is_ok());
}
#[test]
fn repo_urls_are_https_scp_or_ssh() {
let ok = |u: &str| validate_aws_workspace(&with_repos(vec![repo(u, None, "x")])).is_ok();
for good in ["https://github.com/o/r.git", "git@github.com:o/r.git", "ssh://git@h/o/r.git"] {
assert!(ok(good), "{good}");
}
for bad in ["", "http://h/r", "file:///etc", "/local/path", "--upload-pack=x", "github.com:o/r", "https://h/r x", "ext::sh -c id", "https://"] {
assert!(!ok(bad), "{bad:?}");
}
}
#[test]
fn an_https_url_with_credentials_is_refused_pointing_at_the_token_setting() {
let check = |u: &str| validate_aws_workspace(&with_repos(vec![repo(u, None, "x")]));
for bad in [
"https://user:pass@github.com/o/r.git",
"https://ghp_abc@github.com/o/r.git",
"https://oauth2:tok@gitlab.com/o/r",
] {
let e = check(bad).unwrap_err();
assert!(e.contains("Git HTTPS token"), "{e}");
}
// An @ in the path is not userinfo; ssh and scp forms keep their user.
for good in ["https://h/o/r@v2.git", "ssh://git@h/o/r.git", "git@github.com:o/r.git"] {
assert!(check(good).is_ok(), "{good}");
}
}
#[test]
fn branches_follow_check_ref_format() {
let ok = |b: Option<&str>| validate_aws_workspace(&with_repos(vec![repo("https://h/r", b, "x")])).is_ok();
for good in [None, Some("main"), Some("feat/aws-1"), Some("v1.2.3"), Some("a@b")] {
assert!(ok(good), "{good:?}");
}
for bad in ["", "-x", "a..b", "a b", "a~1", "a^", "a:b", "a?", "a*", "a[", "a\\b", "a.lock", "x/.hidden", "a/", "/a", "a//b", "a@{1}", "@", "a.", "HEAD"] {
assert!(!ok(Some(bad)), "{bad:?}");
}
}
#[test]
fn at_most_20_repos() {
let mk = |n: usize| with_repos((0..n).map(|i| repo("https://h/r", None, &format!("r{i}"))).collect());
assert!(validate_aws_workspace(&mk(20)).is_ok());
assert!(validate_aws_workspace(&mk(21)).is_err());
}
}
+98 -21
View File
@@ -93,6 +93,46 @@ fn record_corrupt_load(file_path: &Path, now: &chrono::DateTime<chrono::Utc>) {
}
}
/// The load's rewrite of stale transient statuses; `true` when it changed
/// any.
pub(crate) fn settle_stale_statuses(projects: &mut [Project]) -> bool {
// Reconcile stale transient statuses: on a cold app start no Docker
// operations can be in flight, so Starting/Stopping are always stale.
// Running/Error are left as-is and reconciled against Docker later
// via the reconcile_project_statuses command.
//
// An AWS project goes to **Error**, not Stopped: an app that quit
// mid-start (or mid-stop) may have left a billed task running, with
// or without its ARN recorded yet. Error is what a failed start that
// left a task already uses; `reconcile_aws_projects` then asks ECS
// (by ARN, else by the project's tags) and settles it Running or
// Stopped. Stopped would hide the live task.
let mut changed = false;
for p in projects.iter_mut() {
match p.status {
crate::models::ProjectStatus::Starting | crate::models::ProjectStatus::Stopping => {
let to = if p.is_aws() {
crate::models::ProjectStatus::Error
} else {
crate::models::ProjectStatus::Stopped
};
log::warn!(
"Reconciling stale '{}' status for project '{}' ({}) → {:?}",
serde_json::to_string(&p.status).unwrap_or_default().trim_matches('"'),
p.name,
p.id,
to
);
p.status = to;
p.updated_at = chrono::Utc::now().to_rfc3339();
changed = true;
}
_ => {}
}
}
changed
}
pub struct ProjectsStore {
projects: Mutex<Vec<Project>>,
file_path: PathBuf,
@@ -155,28 +195,8 @@ impl ProjectsStore {
(Vec::new(), false)
};
// Reconcile stale transient statuses: on a cold app start no Docker
// operations can be in flight, so Starting/Stopping are always stale.
// Running/Error are left as-is and reconciled against Docker later
// via the reconcile_project_statuses command.
let mut projects = projects;
let mut needs_save = needs_save;
for p in projects.iter_mut() {
match p.status {
crate::models::ProjectStatus::Starting | crate::models::ProjectStatus::Stopping => {
log::warn!(
"Reconciling stale '{}' status for project '{}' ({}) → Stopped",
serde_json::to_string(&p.status).unwrap_or_default().trim_matches('"'),
p.name,
p.id
);
p.status = crate::models::ProjectStatus::Stopped;
p.updated_at = chrono::Utc::now().to_rfc3339();
needs_save = true;
}
_ => {}
}
}
let needs_save = settle_stale_statuses(&mut projects) || needs_save;
let store = Self {
projects: Mutex::new(projects),
@@ -440,6 +460,34 @@ impl ProjectsStore {
Ok(())
}
/// Granular setter for the AWS runtime handles of a project, store-owned
/// like `container_id`. Fails if the project is not an AWS project.
pub fn set_aws_runtime(
&self,
project_id: &str,
task_arn: Option<String>,
access_point_id: Option<String>,
task_definition_arn: Option<String>,
workspace_access_point_id: Option<String>,
) -> Result<(), String> {
let mut projects = self.lock();
let p = projects
.iter_mut()
.find(|p| p.id == project_id)
.ok_or_else(|| format!("Project {} not found", project_id))?;
let aws = p
.aws
.as_mut()
.ok_or_else(|| format!("Project {} is not an AWS project", project_id))?;
aws.task_arn = task_arn;
aws.access_point_id = access_point_id;
aws.task_definition_arn = task_definition_arn;
aws.workspace_access_point_id = workspace_access_point_id;
p.updated_at = chrono::Utc::now().to_rfc3339();
self.save(&projects)?;
Ok(())
}
pub fn set_container_id(&self, project_id: &str, container_id: Option<String>) -> Result<(), String> {
let mut projects = self.lock();
if let Some(p) = projects.iter_mut().find(|p| p.id == project_id) {
@@ -847,4 +895,33 @@ mod tests {
assert!(sight_and_mark(&store, &full[..12], |_| false).is_some());
fs::remove_dir_all(&dir).ok();
}
#[test]
fn set_aws_runtime_writes_the_handles_and_only_for_aws_projects() {
let dir = temp_dir("aws-runtime");
let mut aws = Project::new("a".to_string(), Vec::new());
aws.host_id = Some("h".into());
aws.aws = Some(Default::default());
let local = Project::new("l".to_string(), Vec::new());
let (aid, lid) = (aws.id.clone(), local.id.clone());
let store = store_over(&dir, vec![aws, local]);
store
.set_aws_runtime(&aid, Some("t".into()), Some("ap".into()), Some("td".into()), Some("wap".into()))
.unwrap();
let a = store.get(&aid).unwrap().aws.unwrap();
assert_eq!(
(a.task_arn.as_deref(), a.access_point_id.as_deref(), a.task_definition_arn.as_deref()),
(Some("t"), Some("ap"), Some("td"))
);
let on_disk: Vec<Project> =
serde_json::from_str(&fs::read_to_string(dir.join("projects.json")).unwrap()).unwrap();
assert_eq!(on_disk[0].aws.as_ref().unwrap().task_arn.as_deref(), Some("t"));
store.set_aws_runtime(&aid, None, None, None, None).unwrap();
assert!(store.get(&aid).unwrap().aws.unwrap().task_arn.is_none());
assert!(store.set_aws_runtime(&lid, None, None, None, None).is_err());
assert!(store.set_aws_runtime("nope", None, None, None, None).is_err());
fs::remove_dir_all(&dir).ok();
}
}
+7 -1
View File
@@ -295,7 +295,8 @@ async fn handle_open(
return Err(format!("Project '{}' is not running", project.name));
}
let container = crate::host::ContainerTarget::for_project(&project)
let container = crate::host::ContainerTarget::for_project_waiting(&project)
.await
.ok_or_else(|| "Container not running".to_string())?;
let cmd = match session_type {
@@ -303,6 +304,10 @@ async fn handle_open(
_ => build_terminal_cmd(&project, &state.settings_store),
};
// AWS copy-at-connect: the credentials go into this terminal's own
// environment (never the agent's). Empty for everything else.
let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await;
let session_id = uuid::Uuid::new_v4().to_string();
let project_name = project.name.clone();
@@ -331,6 +336,7 @@ async fn handle_open(
&container,
&session_id,
cmd,
env,
on_output,
on_exit,
)
+8 -1
View File
@@ -23,9 +23,10 @@ import { useContainerProgress } from "./hooks/useContainerProgress";
import { useKeyboardShortcuts } from "./hooks/useKeyboardShortcuts";
import { useMarketplaceSyncToasts } from "./hooks/useMarketplace";
import { useProjectStatusEvents } from "./hooks/useProjectStatusEvents";
import { useWorkspaceWarnings } from "./hooks/useWorkspaceWarnings";
import MarketplaceView from "./components/marketplace/MarketplaceView";
import { useAppState, isHomeTab, tabKeyId, homeTabKey, MARKETPLACE_TAB_KEY } from "./store/appState";
import { reconcileProjectStatuses } from "./lib/tauri-commands";
import { reconcileAwsProjects, reconcileProjectStatuses } from "./lib/tauri-commands";
export default function App() {
const { checkDocker, checkImage, startDockerPolling } = useDocker();
@@ -77,10 +78,16 @@ export default function App() {
useKeyboardShortcuts();
useMarketplaceSyncToasts();
useProjectStatusEvents();
useWorkspaceWarnings();
// Initialize on mount
useEffect(() => {
loadSettings();
// AWS projects don't need Docker: ECS says which tasks run, and their
// tunnels re-open in the background.
reconcileAwsProjects()
.then(setProjects)
.catch((e) => console.error("Failed to reconcile AWS projects:", e));
let stopPolling: (() => void) | undefined;
checkDocker().then((available) => {
if (available) {
@@ -1,6 +1,9 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor, act } from "@testing-library/react";
import AddProjectDialog from "./AddProjectDialog";
import { useAppState } from "../../store/appState";
import type { AppSettings, EnvironmentScan } from "../../lib/types";
import { environment } from "../../test/awsFixtures";
const add = vi.fn();
@@ -12,6 +15,11 @@ vi.mock("@tauri-apps/plugin-dialog", () => ({
open: vi.fn(async () => null),
}));
const detectAwsEnvironments = vi.fn();
vi.mock("../../lib/tauri-commands", () => ({
detectAwsEnvironments: (c: string, r: string) => detectAwsEnvironments(c, r),
}));
/** A promise whose resolution this test controls, so `loading` can be held open. */
function deferred() {
let resolve!: (v: unknown) => void;
@@ -116,3 +124,113 @@ describe("AddProjectDialog", () => {
expect(submitButton()).toHaveAccessibleDescription("");
});
});
describe("AddProjectDialog on AWS", () => {
const scan = (envs: EnvironmentScan["environments"]): EnvironmentScan => ({
region: "eu-west-1",
account: "111122223333",
owner_id: "AROA:alice",
owner_key: "abc",
state_bucket: "yes",
boundary: "yes",
environments: envs,
});
beforeEach(() => {
vi.clearAllMocks();
useAppState.setState({
appSettings: {
hosts: [
{
id: "c1",
name: "Work account",
kind: { type: "aws", auth: { method: "profile", name: "work" }, default_region: "eu-west-1" },
},
],
} as unknown as AppSettings,
});
});
function chooseAws() {
fireEvent.change(screen.getByLabelText("Runs on"), { target: { value: "c1" } });
}
it("sends the host, placement and workspace with repositories instead of folders", async () => {
detectAwsEnvironments.mockResolvedValue(
scan([environment("staging", "deploying", true), environment("dev", "ready", true)]),
);
add.mockResolvedValue({ id: "p1" });
const onClose = vi.fn();
render(<AddProjectDialog onClose={onClose} />);
chooseAws();
expect(screen.getByLabelText("Region")).toHaveValue("eu-west-1");
await waitFor(() => expect(screen.getByLabelText("Environment")).toHaveValue("dev"));
expect(detectAwsEnvironments).toHaveBeenCalledWith("c1", "eu-west-1");
expect(
(screen.getByRole("option", { name: /staging/ }) as HTMLOptionElement).disabled,
).toBe(true);
expect(screen.queryByLabelText("Folder 1 host path")).toBeNull();
fireEvent.change(screen.getByLabelText("Project name"), { target: { value: "api" } });
fireEvent.change(screen.getByLabelText("Repository 1 URL"), {
target: { value: "git@github.com:org/api.git" },
});
fireEvent.change(screen.getByLabelText("Repository 1 branch"), { target: { value: "develop" } });
fireEvent.click(submitButton());
await waitFor(() => expect(add).toHaveBeenCalled());
const [name, paths, host] = add.mock.calls[0];
expect(name).toBe("api");
expect(paths).toEqual([]);
expect(host.hostId).toBe("c1");
expect(host.awsPlacement).toEqual({ region: "eu-west-1", environment: "dev" });
expect(host.aws.repos).toEqual([
{ url: "git@github.com:org/api.git", branch: "develop", folder: "api" },
]);
expect(host.aws).toMatchObject({ cpu: 1024, memory_mib: 4096, wip_push_on_stop: true, task_arn: null });
await waitFor(() => expect(onClose).toHaveBeenCalled());
});
it("points at Settings → Hosts when no environment is usable", async () => {
detectAwsEnvironments.mockResolvedValue(scan([environment("dev", "ready", false)]));
render(<AddProjectDialog onClose={vi.fn()} />);
chooseAws();
expect(await screen.findByText(/Settings → Hosts/)).toBeInTheDocument();
expect(screen.queryByLabelText("Environment")).toBeNull();
fireEvent.change(screen.getByLabelText("Project name"), { target: { value: "api" } });
fireEvent.click(submitButton());
expect(await screen.findByRole("alert")).toHaveTextContent(/Choose an environment/);
expect(add).not.toHaveBeenCalled();
});
it("shows the backend's validation message when the add is refused", async () => {
detectAwsEnvironments.mockResolvedValue(scan([environment("dev", "update_available", true)]));
add.mockRejectedValue('Repository URL "ftp://x": use https://..., ssh://... or git@host:path');
render(<AddProjectDialog onClose={vi.fn()} />);
chooseAws();
await waitFor(() => expect(screen.getByLabelText("Environment")).toHaveValue("dev"));
fireEvent.change(screen.getByLabelText("Project name"), { target: { value: "api" } });
fireEvent.change(screen.getByLabelText("Repository 1 URL"), { target: { value: "ftp://x" } });
fireEvent.click(submitButton());
expect(await screen.findByRole("alert")).toHaveTextContent(/use https:\/\//);
});
it("forgets the environment when the region changes until that region's scan is done", async () => {
detectAwsEnvironments.mockResolvedValueOnce(scan([environment("dev", "ready", true)]));
let finish!: (v: EnvironmentScan) => void;
detectAwsEnvironments.mockReturnValueOnce(new Promise((r) => (finish = r)));
render(<AddProjectDialog onClose={vi.fn()} />);
chooseAws();
await waitFor(() => expect(screen.getByLabelText("Environment")).toHaveValue("dev"));
fireEvent.change(screen.getByLabelText("Project name"), { target: { value: "api" } });
const region = screen.getByLabelText("Region");
fireEvent.change(region, { target: { value: "us-east-1" } });
fireEvent.blur(region);
expect(await screen.findByText(/Looking for environments in us-east-1/)).toBeInTheDocument();
fireEvent.click(submitButton());
expect(await screen.findByRole("alert")).toHaveTextContent(/Choose an environment/);
expect(add).not.toHaveBeenCalled();
await act(async () => finish(scan([environment("prod", "ready", true)])));
expect(screen.getByLabelText("Environment")).toHaveValue("prod");
});
});
+259 -58
View File
@@ -1,10 +1,15 @@
import { useId, useRef, useState } from "react";
import { useEffect, useId, useRef, useState } from "react";
import { open } from "@tauri-apps/plugin-dialog";
import { useProjects } from "../../hooks/useProjects";
import type { ProjectPath } from "../../lib/types";
import type { DetectedEnvironment, HostConnection, ProjectPath, RepoSource } from "../../lib/types";
import { detectAwsEnvironments } from "../../lib/tauri-commands";
import { awsErrorMessage, toAwsError } from "../../lib/aws-error";
import { cleanRepos, defaultAwsWorkspace, environmentSelectable } from "../../lib/awsWorkspace";
import { useAppState } from "../../store/appState";
import Modal from "../ui/Modal";
import Button from "../ui/Button";
import { inputClass, monoInputClass } from "../ui/Field";
import { inputClass, monoInputClass, selectClass } from "../ui/Field";
import { RepoRows, emptyRepo } from "./RepoListEditor";
interface Props {
onClose: () => void;
@@ -15,12 +20,36 @@ interface PathEntry {
mount_name: string;
}
/** Why an environment can't take a new project, for its disabled option. */
function unavailableReason(env: DetectedEnvironment): string {
const st = env.state.state;
if (st !== "ready" && st !== "update_available") return st.replace(/_/g, " ");
return "you have not joined it";
}
type Scan =
| { status: "idle" }
| { status: "loading" }
| { status: "error"; message: string }
| { status: "done"; environments: DetectedEnvironment[] };
function basenameFromPath(p: string): string {
return p.replace(/[/\\]$/, "").split(/[/\\]/).pop() || "";
}
/** Stable, so the store selector does not return a new array each render. */
const NO_HOSTS: HostConnection[] = [];
export default function AddProjectDialog({ onClose }: Props) {
const { add } = useProjects();
const hosts = useAppState((s) => s.appSettings?.hosts ?? NO_HOSTS);
const [hostId, setHostId] = useState<string>("");
const connection = hosts.find((h) => h.id === hostId) ?? null;
const [region, setRegion] = useState("");
const [scanRegion, setScanRegion] = useState("");
const [scan, setScan] = useState<Scan>({ status: "idle" });
const [environment, setEnvironment] = useState("");
const [repos, setRepos] = useState<RepoSource[]>([emptyRepo()]);
const [name, setName] = useState("");
const [pathEntries, setPathEntries] = useState<PathEntry[]>([
{ host_path: "", mount_name: "" },
@@ -30,6 +59,41 @@ export default function AddProjectDialog({ onClose }: Props) {
const nameInputRef = useRef<HTMLInputElement>(null);
const formId = useId();
// Look for environments when the connection or the region (on blur) changes.
useEffect(() => {
if (!connection || !scanRegion) {
setScan({ status: "idle" });
return;
}
let cancelled = false;
// An environment chosen in another region is not one in this region.
setEnvironment("");
setScan({ status: "loading" });
detectAwsEnvironments(connection.id, scanRegion).then(
(found) => {
if (cancelled) return;
setScan({ status: "done", environments: found.environments });
const first = found.environments.find(environmentSelectable);
setEnvironment(first?.name ?? "");
},
(e) => {
if (!cancelled) setScan({ status: "error", message: awsErrorMessage(toAwsError(e)) });
},
);
return () => {
cancelled = true;
};
}, [connection, scanRegion]);
const chooseHost = (id: string) => {
setHostId(id);
setEnvironment("");
const conn = hosts.find((h) => h.id === id);
const r = conn?.kind.default_region ?? "";
setRegion(r);
setScanRegion(r);
};
const handleBrowse = async (index: number) => {
const selected = await open({ directory: true, multiple: false });
if (typeof selected === "string") {
@@ -63,6 +127,39 @@ export default function AddProjectDialog({ onClose }: Props) {
setError("Project name is required");
return;
}
if (connection) {
// Only an environment from a finished scan of the region shown counts.
if (region.trim() !== scanRegion) {
setScanRegion(region.trim());
setError("Choose an environment in the new region first");
return;
}
if (scan.status !== "done" || !environment) {
setError("Choose an environment to run the project in");
return;
}
const cleaned = cleanRepos(repos);
if (cleaned.some((r) => !r.url)) {
setError("Every repository needs a URL");
return;
}
setLoading(true);
setError(null);
try {
await add(name.trim(), [], {
hostId: connection.id,
awsPlacement: { region: scanRegion, environment },
aws: { ...defaultAwsWorkspace(), repos: cleaned },
});
onClose();
} catch (err) {
// The backend's validation message (repository URL, folder, …).
setError(String(err));
} finally {
setLoading(false);
}
return;
}
const validPaths: ProjectPath[] = pathEntries
.filter((p) => p.host_path.trim())
.map((p) => ({
@@ -137,62 +234,166 @@ export default function AddProjectDialog({ onClose }: Props) {
/>
</div>
<div>
<span className="block text-[13px] font-medium mb-1">Folders</span>
<div className="space-y-2">
{pathEntries.map((entry, i) => (
<div
key={i}
className="space-y-1.5 p-2 bg-[var(--bg-primary)] rounded-[var(--radius-control)] border border-[var(--border-color)]"
>
<div className="flex gap-1.5">
<input
value={entry.host_path}
onChange={(e) => updateEntry(i, "host_path", e.target.value)}
placeholder="/path/to/folder"
aria-label={`Folder ${i + 1} host path`}
className={inputClass}
/>
<Button size="md" onClick={() => handleBrowse(i)}>
Browse
</Button>
{pathEntries.length > 1 && (
<Button
size="md"
variant="danger"
aria-label={`Remove folder ${i + 1}`}
onClick={() =>
setPathEntries(pathEntries.filter((_, j) => j !== i))
}
>
Remove
</Button>
)}
</div>
<div className="flex items-center gap-1.5">
<span className="text-xs text-[var(--text-secondary)] flex-shrink-0 font-mono">
/workspace/
</span>
<input
value={entry.mount_name}
onChange={(e) => updateEntry(i, "mount_name", e.target.value)}
placeholder="mount-name"
aria-label={`Folder ${i + 1} mount name`}
className={monoInputClass}
/>
</div>
</div>
))}
{hosts.length > 0 && (
<div>
<label
htmlFor={`${formId}-host`}
className="block text-[13px] font-medium mb-1"
>
Runs on
</label>
<select
id={`${formId}-host`}
value={hostId}
onChange={(e) => chooseHost(e.target.value)}
className={`w-full ${selectClass}`}
>
<option value="">This machine (Docker)</option>
{hosts.map((h) => (
<option key={h.id} value={h.id}>
{h.name} (AWS)
</option>
))}
</select>
</div>
<Button
className="mt-2"
onClick={() =>
setPathEntries([...pathEntries, { host_path: "", mount_name: "" }])
}
>
+ Add folder
</Button>
</div>
)}
{connection ? (
<>
<div>
<label
htmlFor={`${formId}-region`}
className="block text-[13px] font-medium mb-1"
>
Region
</label>
<input
id={`${formId}-region`}
value={region}
onChange={(e) => setRegion(e.target.value)}
onBlur={() => setScanRegion(region.trim())}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
setScanRegion(region.trim());
}
}}
className={monoInputClass}
/>
</div>
<div>
<label
htmlFor={`${formId}-env`}
className="block text-[13px] font-medium mb-1"
>
Environment
</label>
{scan.status === "loading" && (
<p className="text-xs text-[var(--text-secondary)]">
Looking for environments in {scanRegion}…
</p>
)}
{scan.status === "error" && (
<p className="text-xs text-[var(--error)]">{scan.message}</p>
)}
{scan.status === "done" && (
<>
{scan.environments.some(environmentSelectable) ? (
<select
id={`${formId}-env`}
value={environment}
onChange={(e) => setEnvironment(e.target.value)}
className={`w-full ${selectClass}`}
>
{scan.environments.map((env) => (
<option
key={env.name}
value={env.name}
disabled={!environmentSelectable(env)}
>
{environmentSelectable(env)
? env.name
: `${env.name} — ${unavailableReason(env)}`}
</option>
))}
</select>
) : (
<p className="text-xs text-[var(--text-secondary)]">
No environment in {scanRegion} is ready for you to use. Deploy or join
one in Settings → Hosts.
</p>
)}
</>
)}
</div>
<div>
<span className="block text-[13px] font-medium mb-1">Repositories</span>
<p className="mb-1.5 text-xs text-[var(--text-secondary)]">
Cloned into <span className="font-mono">/workspace/&lt;folder&gt;</span> when
the workspace starts. Memory, storage and the rest are in the project&rsquo;s
Config tab.
</p>
<RepoRows rows={repos} onRowsChange={setRepos} />
</div>
</>
) : (
<div>
<span className="block text-[13px] font-medium mb-1">Folders</span>
<div className="space-y-2">
{pathEntries.map((entry, i) => (
<div
key={i}
className="space-y-1.5 p-2 bg-[var(--bg-primary)] rounded-[var(--radius-control)] border border-[var(--border-color)]"
>
<div className="flex gap-1.5">
<input
value={entry.host_path}
onChange={(e) => updateEntry(i, "host_path", e.target.value)}
placeholder="/path/to/folder"
aria-label={`Folder ${i + 1} host path`}
className={inputClass}
/>
<Button size="md" onClick={() => handleBrowse(i)}>
Browse
</Button>
{pathEntries.length > 1 && (
<Button
size="md"
variant="danger"
aria-label={`Remove folder ${i + 1}`}
onClick={() =>
setPathEntries(pathEntries.filter((_, j) => j !== i))
}
>
Remove
</Button>
)}
</div>
<div className="flex items-center gap-1.5">
<span className="text-xs text-[var(--text-secondary)] flex-shrink-0 font-mono">
/workspace/
</span>
<input
value={entry.mount_name}
onChange={(e) => updateEntry(i, "mount_name", e.target.value)}
placeholder="mount-name"
aria-label={`Folder ${i + 1} mount name`}
className={monoInputClass}
/>
</div>
</div>
))}
</div>
<Button
className="mt-2"
onClick={() =>
setPathEntries([...pathEntries, { host_path: "", mount_name: "" }])
}
>
+ Add folder
</Button>
</div>
)}
{error && (
<div
@@ -0,0 +1,53 @@
import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import ConfirmRemoveModal, { ConfirmForgetAwsModal } from "./ConfirmRemoveModal";
describe("ConfirmRemoveModal", () => {
it("keeps an AWS workspace's access point unless asked", () => {
const onConfirm = vi.fn();
render(<ConfirmRemoveModal projectName="api" aws savedFiles onConfirm={onConfirm} onCancel={vi.fn()} />);
const box = screen.getByRole("checkbox", { name: /file-system access point/ });
expect(box).not.toBeChecked();
fireEvent.click(screen.getByRole("button", { name: "Remove" }));
fireEvent.click(box);
fireEvent.click(screen.getByRole("button", { name: "Remove" }));
expect(onConfirm.mock.calls).toEqual([[{ discardData: false }], [{ discardData: true }]]);
});
it("says both access points go when the workspace kept /workspace on EFS", () => {
render(
<ConfirmRemoveModal projectName="api" aws savedFiles workspaceFiles onConfirm={vi.fn()} onCancel={vi.fn()} />,
);
expect(screen.getByRole("checkbox", { name: /file-system access points/ })).toBeInTheDocument();
expect(screen.getByText(/\/workspace/)).toBeInTheDocument();
});
it("offers no access-point choice for an Ephemeral AWS workspace", () => {
const onConfirm = vi.fn();
render(<ConfirmRemoveModal projectName="api" aws onConfirm={onConfirm} onCancel={vi.fn()} />);
expect(screen.queryByRole("checkbox")).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Remove" }));
expect(onConfirm).toHaveBeenCalledWith({ discardData: false });
});
it("offers no access-point choice for a local project", () => {
render(<ConfirmRemoveModal projectName="api" onConfirm={vi.fn()} onCancel={vi.fn()} />);
expect(screen.queryByRole("checkbox")).toBeNull();
expect(screen.getByText(/mounted in, not copied/)).toBeInTheDocument();
});
it("says what is left in AWS when the connection is gone", () => {
const onConfirm = vi.fn();
render(
<ConfirmForgetAwsModal
projectName="api"
reason="This project's AWS connection no longer exists"
onConfirm={onConfirm}
onCancel={vi.fn()}
/>,
);
expect(screen.getByText(/task definitions, its secret and its saved files/)).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Remove and leave them in AWS" }));
expect(onConfirm).toHaveBeenCalled();
});
});
@@ -1,13 +1,31 @@
import { useState } from "react";
import Modal from "../ui/Modal";
import Button from "../ui/Button";
interface Props {
projectName: string;
onConfirm: () => void;
/** An AWS project: its task, task definitions and secret go instead of Docker state. */
aws?: boolean;
/** AWS: the workspace may have an EFS access point (offers removing it). */
savedFiles?: boolean;
/** AWS: `/workspace` may be on EFS too (EFS storage, now or before). */
workspaceFiles?: boolean;
onConfirm: (opts: { discardData: boolean }) => void;
onCancel: () => void;
}
export default function ConfirmRemoveModal({ projectName, onConfirm, onCancel }: Props) {
const REMOVE_CLASS =
"bg-[var(--error-emphasis)] text-white border border-transparent hover:opacity-90";
export default function ConfirmRemoveModal({
projectName,
aws = false,
savedFiles = false,
workspaceFiles = false,
onConfirm,
onCancel,
}: Props) {
const [discardData, setDiscardData] = useState(false);
return (
<Modal
title="Remove Project"
@@ -20,32 +38,104 @@ export default function ConfirmRemoveModal({ projectName, onConfirm, onCancel }:
</Button>
<Button
size="md"
onClick={onConfirm}
className="bg-[var(--error-emphasis)] text-white border border-transparent hover:opacity-90"
onClick={() => onConfirm({ discardData: savedFiles && discardData })}
className={REMOVE_CLASS}
>
Remove
</Button>
</>
}
>
{/*
Everything remove_project() destroys, named. It removes the container,
*both* named volumes (triple-c-home-{id} and triple-c-claude-config-{id}),
the triple-c-snapshot-{id} image and the project's keychain secrets — so
an accurate warning has to reach past "the config volume". The last
sentence is the reassuring half and matters just as much: project folders
are bind mounts from the host and nothing here touches them.
*/}
<p className="text-[13px] text-[var(--text-secondary)]">
Are you sure you want to remove{" "}
<strong className="text-[var(--text-primary)]">{projectName}</strong>? This deletes
its container, both of its volumes and its saved container image — so the home
directory, the Claude login and config, installed skills, session transcripts,
scheduled tasks and any stored credentials all go with it.
</p>
{aws ? (
<>
<p className="text-[13px] text-[var(--text-secondary)]">
Are you sure you want to remove{" "}
<strong className="text-[var(--text-primary)]">{projectName}</strong>? This stops its
workspace on AWS and deletes its secret. Its task definitions stay registered (they hold no secrets and cost
nothing); an administrator can deregister them. Work that was not pushed to a
remote is lost.
</p>
{savedFiles && (
<label className="mt-3 flex items-start gap-2 text-[13px] text-[var(--text-primary)]">
<input
type="checkbox"
className="mt-0.5"
checked={discardData}
onChange={(e) => setDiscardData(e.target.checked)}
/>
<span>
Also remove the workspace&rsquo;s file-system access points
<span className="block text-xs text-[var(--text-secondary)]">
The saved files themselves (Claude login, history, SSH keys
{workspaceFiles ? ", and the /workspace files" : ""}) stay on your own EFS file
system, readable only by your workspace role, until an administrator offboards
you.
</span>
</span>
</label>
)}
</>
) : (
<>
{/*
Everything remove_project() destroys, named. It removes the container,
*both* named volumes (triple-c-home-{id} and triple-c-claude-config-{id}),
the triple-c-snapshot-{id} image and the project's keychain secrets — so
an accurate warning has to reach past "the config volume". The last
sentence is the reassuring half and matters just as much: project folders
are bind mounts from the host and nothing here touches them.
*/}
<p className="text-[13px] text-[var(--text-secondary)]">
Are you sure you want to remove{" "}
<strong className="text-[var(--text-primary)]">{projectName}</strong>? This deletes
its container, both of its volumes and its saved container image — so the home
directory, the Claude login and config, installed skills, session transcripts,
scheduled tasks and any stored credentials all go with it.
</p>
<p className="mt-2 text-[13px] text-[var(--text-secondary)]">
Your project folders on this machine are mounted in, not copied, and are left
untouched.
</p>
</>
)}
</Modal>
);
}
/** The second confirm when an AWS project's connection is gone. */
export function ConfirmForgetAwsModal({
projectName,
reason,
onConfirm,
onCancel,
}: {
projectName: string;
reason: string;
onConfirm: () => void;
onCancel: () => void;
}) {
return (
<Modal
title="Remove without its AWS resources?"
onClose={onCancel}
widthClassName="w-[28rem]"
footer={
<>
<Button size="md" variant="ghost" onClick={onCancel}>
Cancel
</Button>
<Button size="md" onClick={onConfirm} className={REMOVE_CLASS}>
Remove and leave them in AWS
</Button>
</>
}
>
<p className="text-[13px] text-[var(--text-secondary)]">{reason}</p>
<p className="mt-2 text-[13px] text-[var(--text-secondary)]">
Your project folders on this machine are mounted in, not copied, and are left
untouched.
Removing <strong className="text-[var(--text-primary)]">{projectName}</strong> now
leaves its AWS resources in the account: the task (which may still be running and
billed), its task definitions, its secret and its saved files. You would have to
remove them in AWS yourself.
</p>
</Modal>
);
@@ -149,4 +149,11 @@ describe("PermissionModeControl", () => {
/Caution/i,
);
});
it("shows the sandbox OFF on an AWS project whatever the setting says", () => {
const aws = { ...baseProject, host_id: "aws-1", sandbox_mode_enabled: true, permission_mode: "bypass" as const };
render(<PermissionModeControl project={aws} onChange={vi.fn()} />);
expect(screen.getByTestId("sandbox-state")).toHaveTextContent(/Sandbox OFF/);
expect(screen.getByTestId("permission-mode-hint")).toHaveTextContent(/Caution/i);
});
});
@@ -1,5 +1,6 @@
import type { PermissionMode, Project } from "../../lib/types";
import SegmentedControl, { type Segment } from "../ui/SegmentedControl";
import { sandboxEffective } from "../../lib/awsWorkspace";
export const PERMISSION_MODES: Segment<PermissionMode>[] = [
{ value: "plan", label: "Plan", hint: "Claude proposes a plan and makes no changes." },
@@ -56,7 +57,7 @@ export default function PermissionModeControl({
disabledReason,
}: Props) {
const mode = effectivePermissionMode(project);
const sandboxOn = project.sandbox_mode_enabled;
const sandboxOn = sandboxEffective(project);
const uncontainedBypass = mode === "bypass" && !sandboxOn;
const segments = PERMISSION_MODES.map((segment) =>
@@ -7,6 +7,8 @@ interface Props {
portMappings: PortMapping[];
disabled: boolean;
disabledReason?: string;
/** AWS: only TCP is forwarded (UDP is refused on save). */
tcpOnly?: boolean;
onSave: (mappings: PortMapping[]) => Promise<unknown>;
}
@@ -14,6 +16,7 @@ export default function PortMappingsEditor({
portMappings: initial,
disabled,
disabledReason,
tcpOnly = false,
onSave,
}: Props) {
const [mappings, setMappings] = useState<PortMapping[]>(initial);
@@ -93,7 +96,7 @@ export default function PortMappingsEditor({
className={`w-[22%] ${selectClass}`}
>
<option value="tcp">TCP</option>
<option value="udp">UDP</option>
{(!tcpOnly || pm.protocol === "udp") && <option value="udp">UDP</option>}
</select>
<Button
variant="danger"
+23 -3
View File
@@ -4,6 +4,8 @@ import { useAppState, homeTabKey } from "../../store/appState";
import { useProjectActions } from "../../hooks/useProjectActions";
import { ProjectStatusIndicator } from "../ui/StatusIndicator";
import { useUnavailable } from "../ui/unavailable";
import { isAwsProject } from "../../lib/awsWorkspace";
import StopGuardModal from "./StopGuardModal";
interface Props {
project: Project;
@@ -23,8 +25,13 @@ export default function ProjectRow({ project }: Props) {
progress: s.containerProgress[project.id],
})),
);
const { busy, handleStart, handleStop, openClaudeTerminal } =
const { busy, handleStart, handleStop, openClaudeTerminal, stopGuard, resolveStopGuard } =
useProjectActions(project);
const aws = isAwsProject(project);
// An AWS project mounts nothing; its clones are what /workspace holds.
const folders = aws
? (project.aws?.repos ?? []).map((r) => r.folder)
: project.paths.map((pp) => pp.mount_name);
const isSelected =
activeTabKey === homeTabKey(project.id) || selectedProjectId === project.id;
@@ -64,12 +71,17 @@ export default function ProjectRow({ project }: Props) {
<span className="w-[3.75rem] flex-shrink-0" aria-hidden="true" />
</div>
<div className="mt-0.5 ml-4 space-y-0.5 min-w-0">
{project.paths.map((pp, i) => (
{aws && (
<div className="text-xs text-[var(--text-secondary)] truncate">
AWS · {project.aws_placement?.environment ?? "?"} ({project.aws_placement?.region ?? "?"})
</div>
)}
{folders.map((folder, i) => (
<div
key={i}
className="text-xs text-[var(--text-secondary)] truncate font-mono"
>
/workspace/{pp.mount_name}
/workspace/{folder}
</div>
))}
<div className="text-xs">
@@ -149,6 +161,14 @@ export default function ProjectRow({ project }: Props) {
</button>
{terminal.reasonNode}
</div>
{stopGuard && (
<StopGuardModal
projectName={project.name}
work={stopGuard}
wipPushOnStop={project.aws?.wip_push_on_stop ?? false}
onChoose={resolveStopGuard}
/>
)}
</div>
);
}
@@ -0,0 +1,139 @@
import { useEffect, useRef, useState } from "react";
import type { RepoSource } from "../../lib/types";
import { cleanRepos, repoFolderFromUrl } from "../../lib/awsWorkspace";
import Button from "../ui/Button";
import { inputClass, monoInputClass } from "../ui/Field";
export interface RepoRowsProps {
rows: RepoSource[];
onRowsChange: (rows: RepoSource[]) => void;
/** Called when focus leaves a field (save-on-blur editors). */
onCommit?: () => void;
disabled?: boolean;
}
export const emptyRepo = (): RepoSource => ({ url: "", branch: null, folder: "" });
/** Rows of URL / branch / folder. The folder follows the URL until it is edited. */
export function RepoRows({ rows, onRowsChange, onCommit, disabled = false }: RepoRowsProps) {
const update = (i: number, patch: Partial<RepoSource>) => {
const next = [...rows];
next[i] = { ...next[i], ...patch };
onRowsChange(next);
};
return (
<div className="space-y-2">
{rows.map((r, i) => (
<div
key={i}
className="space-y-1.5 p-2 bg-[var(--bg-primary)] rounded-[var(--radius-control)] border border-[var(--border-color)]"
>
<input
value={r.url}
aria-label={`Repository ${i + 1} URL`}
placeholder="git@github.com:org/repo.git or https://…"
disabled={disabled}
onChange={(e) => {
const url = e.target.value;
const followed = !r.folder || r.folder === repoFolderFromUrl(r.url);
update(i, { url, ...(followed ? { folder: repoFolderFromUrl(url) } : {}) });
}}
onBlur={onCommit}
className={monoInputClass}
/>
<div className="flex items-center gap-1.5">
<input
value={r.branch ?? ""}
aria-label={`Repository ${i + 1} branch`}
placeholder="default branch"
disabled={disabled}
onChange={(e) => update(i, { branch: e.target.value })}
onBlur={onCommit}
className={`w-40 ${inputClass}`}
/>
<span className="text-xs text-[var(--text-secondary)] flex-shrink-0 font-mono">
/workspace/
</span>
<input
value={r.folder}
aria-label={`Repository ${i + 1} folder`}
placeholder="folder"
disabled={disabled}
onChange={(e) => update(i, { folder: e.target.value })}
onBlur={onCommit}
className={monoInputClass}
/>
<Button
size="md"
variant="danger"
disabled={disabled}
aria-label={`Remove repository ${i + 1}`}
onClick={() => {
onRowsChange(rows.filter((_, j) => j !== i));
// A removal is a finished edit; `onRowsChange` has already
// handed the shorter list over.
onCommit?.();
}}
>
Remove
</Button>
</div>
</div>
))}
<Button disabled={disabled} onClick={() => onRowsChange([...rows, emptyRepo()])}>
+ Add repository
</Button>
</div>
);
}
function sameRepos(a: RepoSource[], b: RepoSource[]): boolean {
return (
a.length === b.length &&
a.every((r, i) => r.url === b[i].url && r.branch === b[i].branch && r.folder === b[i].folder)
);
}
/**
* The Config tab's save-on-blur repository list. Saves when every row that
* has anything in it has a URL; the backend's validation message (bad URL,
* folder used twice…) comes back through the save's toast.
*
* Rows in progress survive a refresh of the project list: they are replaced
* from `repos` only when the stored value actually changed.
*/
export default function RepoListEditor({
repos,
onSave,
disabled = false,
}: {
repos: RepoSource[];
onSave: (repos: RepoSource[]) => Promise<boolean>;
disabled?: boolean;
}) {
const [rows, setRows] = useState<RepoSource[]>(repos);
// The latest rows, so a commit right after an edit (a removal) sees them.
const rowsRef = useRef(rows);
const storedRef = useRef(repos);
useEffect(() => {
if (sameRepos(repos, storedRef.current)) return;
storedRef.current = repos;
rowsRef.current = repos;
setRows(repos);
}, [repos]);
const change = (next: RepoSource[]) => {
rowsRef.current = next;
setRows(next);
};
const commit = () => {
const cleaned = cleanRepos(rowsRef.current);
if (cleaned.some((r) => !r.url)) return;
if (sameRepos(cleaned, storedRef.current)) return;
void onSave(cleaned);
};
return <RepoRows rows={rows} onRowsChange={change} onCommit={commit} disabled={disabled} />;
}
@@ -0,0 +1,67 @@
import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import StopGuardModal from "./StopGuardModal";
import type { UnsavedWork } from "../../lib/types";
const dirty: UnsavedWork = {
checked: true,
repos: [
{ folder: "api", branch: "main", dirty: true, unpushed: 2 },
{ folder: "clean", branch: "main", dirty: false, unpushed: 0 },
],
error: null,
};
describe("StopGuardModal", () => {
it("lists the repositories at risk and defaults to pushing", async () => {
const onChoose = vi.fn();
render(<StopGuardModal projectName="api" work={dirty} onChoose={onChoose} />);
expect(screen.getByText("/workspace/api")).toBeInTheDocument();
expect(screen.getByText(/uncommitted changes, 2 unpushed commits/)).toBeInTheDocument();
expect(screen.queryByText("/workspace/clean")).toBeNull();
const push = screen.getByRole("button", { name: "Push to WIP branch and stop" });
await waitFor(() => expect(push).toHaveFocus());
fireEvent.click(push);
expect(onChoose).toHaveBeenCalledWith("push");
});
it("lists a repository still cloning as having nothing to push", () => {
const work: UnsavedWork = {
...dirty,
repos: [...dirty.repos, { folder: "big", branch: "", dirty: false, unpushed: 0, cloning: true }],
};
render(<StopGuardModal projectName="api" work={work} onChoose={vi.fn()} />);
expect(screen.getByText("/workspace/big")).toBeInTheDocument();
expect(screen.getByText("still cloning — nothing to push")).toBeInTheDocument();
expect(screen.getByText("/workspace/api")).toBeInTheDocument();
});
it("offers Stop without pushing and Cancel", () => {
const onChoose = vi.fn();
render(<StopGuardModal projectName="api" work={dirty} onChoose={onChoose} />);
fireEvent.click(screen.getByRole("button", { name: "Stop without pushing" }));
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(onChoose.mock.calls).toEqual([["discard"], [null]]);
});
it("hides Push and says why when the workspace could not be checked", async () => {
const onChoose = vi.fn();
render(
<StopGuardModal
projectName="api"
work={{ checked: false, repos: [], error: "Triple-C is not connected to the workspace" }}
wipPushOnStop
onChoose={onChoose}
/>,
);
expect(screen.queryByRole("button", { name: /Push/ })).toBeNull();
expect(screen.getByText(/own push of work in progress still runs/)).toBeInTheDocument();
expect(screen.getByText("Triple-C is not connected to the workspace")).toBeInTheDocument();
expect(screen.getByText(/could not check/)).toBeInTheDocument();
const stop = screen.getByRole("button", { name: "Stop anyway" });
await waitFor(() => expect(stop).toHaveFocus());
// Nothing is recorded: the shutdown push the copy promises still runs.
fireEvent.click(stop);
expect(onChoose).toHaveBeenCalledWith("unguarded");
});
});
@@ -0,0 +1,120 @@
import { useRef } from "react";
import type { UnsavedWork, WipChoice } from "../../lib/types";
import Modal from "../ui/Modal";
import Button from "../ui/Button";
export type StopGuardChoice = WipChoice | "unguarded" | null;
interface Props {
projectName: string;
work: UnsavedWork;
/** The project's "push work in progress when the workspace stops" setting. */
wipPushOnStop?: boolean;
/** `null` = Cancel. `"unguarded"` = stop without recording any choice
* (the unchecked case: the workspace's own shutdown push still runs). */
onChoose: (choice: StopGuardChoice) => void;
}
function describeRepo(dirty: boolean, unpushed: number): string {
const parts: string[] = [];
if (dirty) parts.push("uncommitted changes");
if (unpushed > 0) parts.push(`${unpushed} unpushed commit${unpushed === 1 ? "" : "s"}`);
return parts.join(", ");
}
/**
* Asked before an AWS workspace stops when its repositories hold work no
* remote has, or when Triple-C could not check. Pushing to a WIP branch is
* the default (it has focus); it is not offered when the check could not
* run, since the push goes the same way the check did.
*/
export default function StopGuardModal({ projectName, work, wipPushOnStop = false, onChoose }: Props) {
const pushRef = useRef<HTMLButtonElement>(null);
const stopRef = useRef<HTMLButtonElement>(null);
const atRisk = work.repos.filter((r) => !r.cloning && (r.dirty || r.unpushed > 0));
const cloning = work.repos.filter((r) => r.cloning);
return (
<Modal
title="Unsaved work"
onClose={() => onChoose(null)}
widthClassName="w-[30rem]"
initialFocusRef={work.checked ? pushRef : stopRef}
footer={
<>
<Button size="md" variant="ghost" onClick={() => onChoose(null)}>
Cancel
</Button>
<Button
ref={stopRef}
size="md"
variant={work.checked ? "danger" : "primary"}
onClick={() => onChoose(work.checked ? "discard" : "unguarded")}
>
{work.checked ? "Stop without pushing" : "Stop anyway"}
</Button>
{work.checked && (
<Button ref={pushRef} size="md" variant="primary" onClick={() => onChoose("push")}>
Push to WIP branch and stop
</Button>
)}
</>
}
>
{work.checked ? (
<>
<p className="text-[13px] text-[var(--text-secondary)]">
Stopping <strong className="text-[var(--text-primary)]">{projectName}</strong> ends
its workspace. These repositories have work that no remote has yet:
</p>
<ul className="mt-2 space-y-1">
{atRisk.map((r) => (
<li key={r.folder} className="text-[13px] text-[var(--text-primary)]">
<span className="font-mono">/workspace/{r.folder}</span>{" "}
<span className="text-[var(--text-secondary)]">
({r.branch}): {describeRepo(r.dirty, r.unpushed)}
</span>
</li>
))}
{cloning.map((r) => (
<li key={r.folder} className="text-[13px] text-[var(--text-primary)]">
<span className="font-mono">/workspace/{r.folder}</span>{" "}
<span className="text-[var(--text-secondary)]">
still cloning — nothing to push
</span>
</li>
))}
</ul>
<p className="mt-3 text-[13px] text-[var(--text-secondary)]">
Push to WIP branch saves a snapshot of each repository to a{" "}
<span className="font-mono">triple-c/wip/…</span> branch on its remote, without
touching your branches. If the push fails, the workspace keeps running. Stop
without pushing skips that push, including the one the workspace would make as it
shuts down.
</p>
</>
) : (
<>
<p className="text-[13px] text-[var(--text-secondary)]">
Triple-C could not check{" "}
<strong className="text-[var(--text-primary)]">{projectName}</strong> for unsaved
work, so it can&rsquo;t push it to a WIP branch first.
</p>
{work.error && (
<p
role="note"
className="mt-2 px-2 py-1.5 text-xs text-[var(--warning)] bg-[var(--warning-muted)] border border-[var(--warning)]/30 rounded-[var(--radius-control)]"
>
{work.error}
</p>
)}
<p className="mt-2 text-[13px] text-[var(--text-secondary)]">
Uncommitted or unpushed work in the workspace may be lost if you stop it now.
{wipPushOnStop &&
" The workspace's own push of work in progress still runs as it shuts down, if it can reach the remote."}
</p>
</>
)}
</Modal>
);
}
@@ -0,0 +1,159 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { act, render, screen } from "@testing-library/react";
import type { AwsWorkspaceConfig, HourlyEstimate } from "../../../lib/types";
import { defaultAwsWorkspace } from "../../../lib/awsWorkspace";
const awsCostEstimate =
vi.fn<
(id: string, region: string, config: AwsWorkspaceConfig, instanceType: string | null) => Promise<HourlyEstimate | null>
>();
vi.mock("../../../lib/tauri-commands", () => ({
awsCostEstimate: (id: string, region: string, config: AwsWorkspaceConfig, instanceType: string | null) =>
awsCostEstimate(id, region, config, instanceType),
}));
import AwsCostEstimate, { ESTIMATE_DEBOUNCE_MS, formatHourly } from "./AwsCostEstimate";
const config = (over: Partial<AwsWorkspaceConfig> = {}): AwsWorkspaceConfig => ({
...defaultAwsWorkspace(),
storage: "ephemeral",
...over,
});
async function settle() {
await act(async () => {
vi.advanceTimersByTime(ESTIMATE_DEBOUNCE_MS);
});
await act(async () => {});
}
beforeEach(() => {
vi.useFakeTimers();
awsCostEstimate.mockReset();
});
afterEach(() => vi.useRealTimers());
describe("AwsCostEstimate", () => {
it("formats small and larger figures", () => {
expect(formatHourly(0.05826)).toBe("$0.058");
expect(formatHourly(0.2468)).toBe("$0.25");
expect(formatHourly(2.5)).toBe("$2.50");
});
it("shows the on-demand hourly figure for the connection, region and config", async () => {
awsCostEstimate.mockResolvedValue({
usd_per_hour: 0.05826,
note_spot: false,
efs_billed_separately: false,
ec2_instance: false,
root_volume_included: false,
});
const c = config();
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={c} />);
expect(screen.getByText("Estimating…")).toBeInTheDocument();
await settle();
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "us-west-2", c, null);
expect(screen.getByText("≈ $0.058/hour")).toBeInTheDocument();
expect(screen.queryByText(/Spot/)).toBeNull();
expect(screen.queryByText(/EFS/)).toBeNull();
});
it("notes Spot, EFS and the gateway sidecar", async () => {
awsCostEstimate.mockResolvedValue({
usd_per_hour: 0.05,
note_spot: true,
efs_billed_separately: true,
ec2_instance: false,
root_volume_included: false,
});
render(
<AwsCostEstimate
connectionId="c1"
region="us-west-2"
config={config({ spot: true, storage: "hybrid", gateway_sidecar: true })}
/>,
);
await settle();
expect(
screen.getByText("≈ $0.050/hour (on-demand; Spot is usually up to 70% cheaper)"),
).toBeInTheDocument();
expect(screen.getByText(/EFS storage is billed separately/)).toBeInTheDocument();
expect(screen.getByText(/gateway sidecar shares the task's CPU and memory/)).toBeInTheDocument();
});
it("says the estimate is unavailable for null or a failed call, never an error", async () => {
awsCostEstimate.mockResolvedValueOnce(null);
const { unmount } = render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config()} />);
await settle();
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
unmount();
awsCostEstimate.mockRejectedValueOnce("boom");
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config()} />);
await settle();
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
});
it("without a connection or region it does not ask", async () => {
render(<AwsCostEstimate connectionId={null} region="us-west-2" config={config()} />);
await settle();
expect(awsCostEstimate).not.toHaveBeenCalled();
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
});
it("asks once per settled change of a cost field, not for unrelated edits", async () => {
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1, note_spot: false, efs_billed_separately: false, ec2_instance: false, root_volume_included: false });
const { rerender } = render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config()} />);
await settle();
expect(awsCostEstimate).toHaveBeenCalledTimes(1);
// An unrelated field: no new call.
rerender(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ setup_script: "x" })} />);
await settle();
expect(awsCostEstimate).toHaveBeenCalledTimes(1);
// Two quick CPU changes: one call, with the last.
rerender(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ cpu: 2048 })} />);
rerender(
<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ cpu: 4096, memory_mib: 8192 })} />,
);
await settle();
expect(awsCostEstimate).toHaveBeenCalledTimes(2);
expect(awsCostEstimate.mock.calls[1][2].cpu).toBe(4096);
});
it("says when an EC2 estimate lacks the root volume's price", async () => {
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1008, note_spot: false, efs_billed_separately: false, ec2_instance: true, root_volume_included: false });
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ launch_type: "ec2" })} instanceType="m7i.large" />);
await settle();
expect(screen.getByText(/Its 50 GiB gp3 root volume \(about \$4 a month\) is not included/)).toBeInTheDocument();
expect(screen.getByText(/public IPv4 address adds/)).toBeInTheDocument();
});
it("prices an EC2 project as its instance type, and says instances scale to zero", async () => {
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1008, note_spot: false, efs_billed_separately: true, ec2_instance: true, root_volume_included: true });
const c = config({ launch_type: "ec2", storage: "hybrid" });
const { rerender } = render(
<AwsCostEstimate connectionId="c1" region="us-west-2" config={c} instanceType="m7i.large" />,
);
await settle();
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "us-west-2", c, "m7i.large");
expect(screen.getByText("≈ $0.10/hour")).toBeInTheDocument();
expect(screen.getByText(/Instances scale to zero when no workspace is running/)).toBeInTheDocument();
expect(screen.getByText(/EFS storage is billed separately/)).toBeInTheDocument();
expect(screen.getByText(/Includes its 50 GiB gp3 root volume/)).toBeInTheDocument();
expect(screen.getByText(/public IPv4 address adds \$0.005\/hour/)).toBeInTheDocument();
expect(screen.queryByText(/While the task runs/)).toBeNull();
// The reservations don't change an instance's price: no new call.
rerender(
<AwsCostEstimate connectionId="c1" region="us-west-2" config={{ ...c, cpu: 512, memory_mib: 1024 }} instanceType="m7i.large" />,
);
await settle();
expect(awsCostEstimate).toHaveBeenCalledTimes(1);
});
it("does not ask for an EC2 project whose instance type is unknown", async () => {
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ launch_type: "ec2" })} instanceType={null} />);
await settle();
expect(awsCostEstimate).not.toHaveBeenCalled();
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
});
});
@@ -0,0 +1,96 @@
import { useEffect, useState } from "react";
import type { AwsWorkspaceConfig, HourlyEstimate } from "../../../lib/types";
import { awsCostEstimate } from "../../../lib/tauri-commands";
/** Edits settle this long before the estimate is asked for again. */
export const ESTIMATE_DEBOUNCE_MS = 400;
/** "$0.058" below ten cents (two decimals would round most tasks to $0.05/$0.06), "$0.25" above. */
export function formatHourly(usd: number): string {
return `$${usd.toFixed(usd < 0.1 ? 3 : 2)}`;
}
interface Props {
/** The project's connection; null when it was removed. */
connectionId: string | null;
region: string | null | undefined;
config: AwsWorkspaceConfig;
/** An EC2 project's instance type (the user's); null when unknown. */
instanceType?: string | null;
}
type State = { kind: "loading" } | { kind: "done"; estimate: HourlyEstimate | null };
/**
* The estimated on-demand cost per hour of an AWS project's Fargate task (Q5), from the Price
* List API through `aws_cost_estimate` (cached a day per region). Best effort: a denied call, no
* network or no connection reads "Estimate unavailable" — never an error toast, and nothing waits
* on it. Re-asked only when a field that changes the price changes, after edits settle.
*/
export default function AwsCostEstimate({ connectionId, region, config, instanceType = null }: Props) {
const [state, setState] = useState<State>({ kind: "loading" });
const { launch_type, cpu, memory_mib, arch, ephemeral_gib, spot, storage } = config;
const ec2 = launch_type === "ec2";
// Only what the backend prices or notes; the rest of the config is sent but changes nothing.
// An EC2 project is priced as its instance (E10): only the type and storage matter.
const key = JSON.stringify(
ec2
? [connectionId, region, launch_type, instanceType, storage]
: [connectionId, region, launch_type, cpu, memory_mib, arch, ephemeral_gib, spot, storage],
);
useEffect(() => {
if (!connectionId || !region || (ec2 && !instanceType)) {
setState({ kind: "done", estimate: null });
return;
}
let cancelled = false;
setState({ kind: "loading" });
const timer = setTimeout(() => {
awsCostEstimate(connectionId, region, config, ec2 ? instanceType : null)
.catch(() => null)
.then((estimate) => {
if (!cancelled) setState({ kind: "done", estimate });
});
}, ESTIMATE_DEBOUNCE_MS);
return () => {
cancelled = true;
clearTimeout(timer);
};
// `key` covers every field of `config` the estimate depends on.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key]);
const estimate = state.kind === "done" ? state.estimate : null;
return (
<div className="space-y-0.5 text-xs">
<p className="text-[var(--text-primary)]">
{state.kind === "loading"
? "Estimating…"
: estimate
? `≈ ${formatHourly(estimate.usd_per_hour)}/hour${
estimate.note_spot ? " (on-demand; Spot is usually up to 70% cheaper)" : ""
}`
: "Estimate unavailable"}
</p>
{estimate?.ec2_instance && (
<p className="text-[var(--text-secondary)]">
While your instance runs. Instances scale to zero when no workspace is running.
{estimate.root_volume_included
? " Includes its 50 GiB gp3 root volume."
: " Its 50 GiB gp3 root volume (about $4 a month) is not included."}
{" In an environment with public networking, the instance's public IPv4 address adds $0.005/hour."}
{estimate.efs_billed_separately && " EFS storage is billed separately."}
</p>
)}
{estimate && !estimate.ec2_instance && (
<p className="text-[var(--text-secondary)]">
While the task runs; nothing while it is stopped.
{estimate.efs_billed_separately && " EFS storage is billed separately."}
{config.gateway_sidecar &&
" The gateway sidecar shares the task's CPU and memory, so it adds nothing."}
</p>
)}
</div>
);
}
@@ -0,0 +1,151 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { act, render, screen } from "@testing-library/react";
import type { Project, WorkspaceStatus } from "../../../lib/types";
const awsWorkspaceStatus = vi.fn<(id: string) => Promise<WorkspaceStatus | null>>();
vi.mock("../../../lib/tauri-commands", () => ({
awsWorkspaceStatus: (id: string) => awsWorkspaceStatus(id),
}));
import AwsWorkspaceStatusCard, { STATUS_POLL_MS } from "./AwsWorkspaceStatusCard";
const project = (over: Partial<Project> = {}): Project =>
({
id: "p1",
name: "demo",
status: "running",
host_id: "conn-1",
container_id: "http://127.0.0.1:1",
aws: { repos: [{}, {}, {}] },
...over,
}) as unknown as Project;
const CLONING: WorkspaceStatus = {
clone: {
done: false,
repos: [
{ folder: "api", state: "cloned" },
{ folder: "web", state: "cloning" },
],
},
setup: { state: "running", code: null, tail: [] },
};
const FAILED: WorkspaceStatus = {
clone: {
done: true,
repos: [
{ folder: "api", state: "cloned" },
{ folder: "web", state: "failed" },
],
},
setup: { state: "failed", code: 2, tail: ["Setting up jq", "E: Unable to locate package nope"] },
};
async function flush() {
await act(async () => {
await Promise.resolve();
await Promise.resolve();
});
}
describe("AwsWorkspaceStatusCard", () => {
beforeEach(() => {
vi.useFakeTimers();
awsWorkspaceStatus.mockReset();
});
afterEach(() => {
vi.useRealTimers();
});
it("shows a workspace still cloning, then a failed clone and setup", async () => {
awsWorkspaceStatus.mockResolvedValueOnce(CLONING).mockResolvedValue(FAILED);
render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(screen.getByText("Still cloning repos (1 of 3 done)")).toBeTruthy();
expect(screen.getByText("cloning…")).toBeTruthy();
expect(screen.getByText("Setup script waits for the clone")).toBeTruthy();
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2);
expect(screen.getByText("Clone failed for web")).toBeTruthy();
expect(screen.getByText("Setup script failed (exit 2)")).toBeTruthy();
expect(screen.getByText(/E: Unable to locate package nope/)).toBeTruthy();
});
it("renders nothing and does not poll for a stopped project", async () => {
awsWorkspaceStatus.mockResolvedValue(FAILED);
const { container } = render(<AwsWorkspaceStatusCard project={project({ status: "stopped" })} />);
await flush();
expect(awsWorkspaceStatus).not.toHaveBeenCalled();
expect(container.innerHTML).toBe("");
});
it("renders nothing when there is nothing to say", async () => {
awsWorkspaceStatus.mockResolvedValue({
clone: { done: true, repos: [] },
setup: { state: "none", code: null, tail: [] },
});
const { container } = render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(container.innerHTML).toBe("");
});
it("pauses while the window is hidden", async () => {
awsWorkspaceStatus.mockResolvedValue(CLONING);
render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
const state = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS * 3);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
state.mockReturnValue("visible");
await act(async () => {
document.dispatchEvent(new Event("visibilitychange"));
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2);
state.mockRestore();
});
it("never runs two polls at once when the window is hidden and shown mid-read", async () => {
let resolve: (s: WorkspaceStatus) => void = () => {};
awsWorkspaceStatus.mockImplementation(() => new Promise((r) => (resolve = r)));
render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
// Hidden and shown again while the first read is still out.
const state = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
await act(async () => {
document.dispatchEvent(new Event("visibilitychange"));
});
state.mockReturnValue("visible");
await act(async () => {
document.dispatchEvent(new Event("visibilitychange"));
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
// The read ends: one chain goes on, one read per interval.
await act(async () => resolve(CLONING));
await flush();
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2);
await act(async () => resolve(CLONING));
await flush();
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(3);
state.mockRestore();
});
});
@@ -0,0 +1,136 @@
import { useEffect, useState } from "react";
import type { Project, WorkspaceStatus } from "../../../lib/types";
import { awsWorkspaceStatus } from "../../../lib/tauri-commands";
/** How often the card re-reads the status while it is on screen. */
export const STATUS_POLL_MS = 5000;
const REPO_STATE: Record<string, string> = {
cloning: "cloning…",
cloned: "cloned",
failed: "failed",
exists: "already there",
};
/**
* An AWS workspace's background clone and setup script (phase 6, Q4/R5):
* the start ends at "Connecting…" and this card is where their progress
* shows, re-read every 5 s while the project is Running and the window is
* visible. Each read is a background exec, so the
* card never keeps an idle workspace up. Renders nothing for a local project,
* a workspace that cannot be asked, or one with no repos and no setup script.
*/
export default function AwsWorkspaceStatusCard({ project }: { project: Project }) {
const running = project.status === "running";
const [status, setStatus] = useState<WorkspaceStatus | null>(null);
useEffect(() => {
setStatus(null);
if (!running) return;
let cancelled = false;
let timer: ReturnType<typeof setTimeout> | null = null;
// A read in progress: hiding and showing the window during it must not
// start a second poll chain beside this one.
let inFlight = false;
const visible = () => document.visibilityState !== "hidden";
const poll = async () => {
timer = null;
if (!visible()) return; // resumed by visibilitychange
inFlight = true;
try {
const s = await awsWorkspaceStatus(project.id);
if (!cancelled) setStatus(s);
} catch {
// A failed read keeps the last status; the next poll tries again.
} finally {
inFlight = false;
}
if (!cancelled) timer = setTimeout(poll, STATUS_POLL_MS);
};
const onVisibility = () => {
if (visible() && timer === null && !inFlight && !cancelled) void poll();
};
void poll();
document.addEventListener("visibilitychange", onVisibility);
return () => {
cancelled = true;
if (timer !== null) clearTimeout(timer);
document.removeEventListener("visibilitychange", onVisibility);
};
}, [project.id, running, project.container_id]);
if (!running || !status) return null;
const { clone, setup } = status;
if (clone.done && clone.repos.length === 0 && setup.state === "none") return null;
const failedRepos = clone.repos.filter((r) => r.state === "failed");
const finished = clone.repos.filter((r) => r.state !== "cloning").length;
const total = Math.max(project.aws?.repos.length ?? 0, clone.repos.length);
const cloneLine = !clone.done
? total > 0
? `Still cloning repos (${finished} of ${total} done)`
: "Still cloning repos"
: failedRepos.length > 0
? `Clone failed for ${failedRepos.map((r) => r.folder).join(", ")}`
: clone.repos.length > 0
? "Repos cloned"
: null;
const setupLine =
setup.state === "running"
? clone.done
? "Setup script running…"
: "Setup script waits for the clone"
: setup.state === "ok"
? "Setup script finished"
: setup.state === "failed"
? `Setup script failed${setup.code != null ? ` (exit ${setup.code})` : ""}`
: null;
const problem = failedRepos.length > 0 || setup.state === "failed";
return (
<section
aria-label="Workspace start-up"
className="p-3 border rounded-[var(--radius-panel)] bg-[var(--bg-secondary)] text-xs space-y-2"
style={{ borderColor: problem ? "var(--warning)" : "var(--border-color)" }}
>
<h2 className="text-[11px] font-semibold uppercase tracking-wide text-[var(--text-secondary)]">
Workspace start-up
</h2>
{cloneLine && (
<p className="text-[var(--text-primary)]">
{failedRepos.length > 0 && (
<span aria-hidden="true" className="mr-1 text-[var(--warning)]">
▲
</span>
)}
{cloneLine}
</p>
)}
{clone.repos.length > 0 && (!clone.done || failedRepos.length > 0) && (
<ul className="space-y-0.5 text-[var(--text-secondary)]">
{clone.repos.map((r) => (
<li key={r.folder}>
<span className="font-mono text-[var(--text-primary)]">{r.folder}</span>{" "}
{REPO_STATE[r.state] ?? r.state}
</li>
))}
</ul>
)}
{setupLine && (
<p className="text-[var(--text-primary)]">
{setup.state === "failed" && (
<span aria-hidden="true" className="mr-1 text-[var(--warning)]">
▲
</span>
)}
{setupLine}
</p>
)}
{(setup.state === "running" || setup.state === "failed") && setup.tail.length > 0 && (
<pre className="max-h-40 overflow-auto whitespace-pre-wrap break-all p-2 rounded-[var(--radius-control)] bg-[var(--bg-primary)] text-[var(--text-secondary)] font-mono text-[11px]">
{(setup.state === "running" ? setup.tail.slice(-3) : setup.tail).join("\n")}
</pre>
)}
</section>
);
}
@@ -6,6 +6,8 @@ import ModelSection from "./config/ModelSection";
import AccessSection from "./config/AccessSection";
import RuntimeSection from "./config/RuntimeSection";
import MarketplaceSection from "./config/MarketplaceSection";
import HostSection from "./config/HostSection";
import { isAwsProject } from "../../../lib/awsWorkspace";
interface Props {
project: Project;
@@ -40,6 +42,9 @@ export default function ConfigTab({ project, save, saveState }: Props) {
</div>
<WorkspaceSection project={project} save={save} disabled={disabled} />
{isAwsProject(project) && (
<HostSection project={project} save={save} disabled={disabled} />
)}
<ModelSection project={project} save={save} disabled={disabled} />
<AccessSection
project={project}
Loaded 100 of 180 files, more files were not shown because too many files have changed in this diff. Show more