feat(aws): state bucket, and detection of environments, locks and onboarding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-14 22:16:05 -08:00
1 parent b7cdf02859
commit f26388969b
11 files changed
+1898 -17

No files matched your search

+121 -3
View File
@@ -298,7 +298,7 @@ dependencies = [
"fastrand",
"hex",
"http",
"sha1",
"sha1 0.10.6",
"time",
"tokio",
"tracing",
@@ -350,6 +350,7 @@ dependencies = [
"aws-credential-types",
"aws-sigv4",
"aws-smithy-async",
"aws-smithy-eventstream",
"aws-smithy-http",
"aws-smithy-runtime",
"aws-smithy-runtime-api",
@@ -438,6 +439,42 @@ dependencies = [
"tracing",
]
[[package]]
name = "aws-sdk-s3"
version = "1.159.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d5c431c5db2f1f6f536c2a19425575e28273859c6ab399d9ac957f73678c7db"
dependencies = [
"arc-swap",
"aws-credential-types",
"aws-runtime",
"aws-sigv4",
"aws-smithy-async",
"aws-smithy-checksums",
"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-smithy-xml",
"aws-types",
"bytes",
"fastrand",
"hex",
"hmac",
"http",
"http-body",
"lru",
"percent-encoding",
"regex-lite",
"sha2 0.11.1",
"tracing",
"url",
]
[[package]]
name = "aws-sdk-secretsmanager"
version = "1.128.0"
@@ -565,6 +602,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a4393e6addc3380632887b0998d06ad5f099ab5c478be0a3eeedecf8604161"
dependencies = [
"aws-credential-types",
"aws-smithy-eventstream",
"aws-smithy-http",
"aws-smithy-runtime-api",
"aws-smithy-types",
@@ -590,12 +628,45 @@ dependencies = [
"tokio",
]
[[package]]
name = "aws-smithy-checksums"
version = "0.65.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab2f2d12a431aab3703d55277278bad45f02208569b11506220b95fdc1ef0507"
dependencies = [
"aws-smithy-http",
"aws-smithy-types",
"bytes",
"crc-fast",
"hex",
"http",
"http-body",
"http-body-util",
"md-5",
"pin-project-lite",
"sha1 0.11.0",
"sha2 0.11.1",
"tracing",
]
[[package]]
name = "aws-smithy-eventstream"
version = "0.61.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c8a643d0e114cae39e45415e3225793a6396698b7779a0c9e3ede731d8fc71"
dependencies = [
"aws-smithy-types",
"bytes",
"crc32fast",
]
[[package]]
name = "aws-smithy-http"
version = "0.64.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19af609dd5edf8b056cd0bb001c706c8b3b1458055328b334ec13d7f46f6aaa8"
dependencies = [
"aws-smithy-eventstream",
"aws-smithy-runtime-api",
"aws-smithy-types",
"bytes",
@@ -806,7 +877,7 @@ dependencies = [
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sha1",
"sha1 0.10.6",
"sync_wrapper",
"tokio",
"tokio-tungstenite",
@@ -1359,6 +1430,16 @@ dependencies = [
"libc",
]
[[package]]
name = "crc-fast"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e75b2483e97a5a7da73ac68a05b629f9c53cff58d8ed1c77866079e18b00dba5"
dependencies = [
"digest 0.10.7",
"spin",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -4272,6 +4353,15 @@ version = "0.4.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
[[package]]
name = "lru"
version = "0.18.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef9ac18847474e638e3702b76c65d4eb93428471a74778ef0f1be711717f89b5"
dependencies = [
"hashbrown 0.17.1",
]
[[package]]
name = "lru-slab"
version = "0.1.2"
@@ -4332,6 +4422,16 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
[[package]]
name = "md-5"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69b6441f590336821bb897fb28fc622898ccceb1d6cea3fde5ea86b090c4de98"
dependencies = [
"cfg-if",
"digest 0.11.3",
]
[[package]]
name = "memchr"
version = "2.8.0"
@@ -6208,6 +6308,17 @@ dependencies = [
"digest 0.10.7",
]
[[package]]
name = "sha1"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214"
dependencies = [
"cfg-if",
"cpufeatures 0.3.1",
"digest 0.11.3",
]
[[package]]
name = "sha1dc"
version = "0.1.4"
@@ -6362,6 +6473,12 @@ dependencies = [
"system-deps",
]
[[package]]
name = "spin"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "023a211cb3138dbc438680b32560ad89f699977624c9f8dbb95a47d5b4c07dd3"
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
@@ -7278,6 +7395,7 @@ dependencies = [
"aws-sdk-ecs",
"aws-sdk-iam",
"aws-sdk-kms",
"aws-sdk-s3",
"aws-sdk-secretsmanager",
"aws-sdk-ssm",
"aws-sdk-sts",
@@ -7339,7 +7457,7 @@ dependencies = [
"httparse",
"log",
"rand 0.9.5",
"sha1",
"sha1 0.10.6",
"thiserror 2.0.18",
"utf-8",
]
+1
View File
@@ -76,6 +76,7 @@ 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"] }
aws-sdk-s3 = { version = "1.159.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
# Linux only: WebKitGTK refuses getUserMedia until the embedder enables media
# streams and answers the permission request (see src/linux_media.rs). Same
+999
View File
@@ -0,0 +1,999 @@
//! The state bucket, and what the account holds: environments (by their SSM
//! record and their state object), interrupted or running deployments (by
//! the S3-native `.tflock` objects) and the caller's own onboarding.
//!
//! Everything read back from AWS here is untrusted: an environment record or
//! a lock object is written by whoever can write the parameter or the
//! bucket. Records are validated field by field (reasons name the field,
//! never echo the value), lock contents are capped and sanitized, and only
//! key shapes the app itself builds (`tofu_modules::state_key`) are listed.
//!
//! Workspace users can read only the records and `iam:GetRole` (D8): a
//! denied lookup is `Unknown` (or no objects at all), never "missing".
use std::collections::BTreeMap;
use std::future::Future;
use aws_config::SdkConfig;
use aws_sdk_s3::config::http::HttpResponse;
use aws_sdk_s3::error::{ProvideErrorMetadata, SdkError};
use aws_sdk_s3::types::{
BucketLocationConstraint, BucketVersioningStatus, CreateBucketConfiguration,
PublicAccessBlockConfiguration, ServerSideEncryption, ServerSideEncryptionByDefault,
ServerSideEncryptionConfiguration, ServerSideEncryptionRule, Tag, Tagging,
VersioningConfiguration,
};
use serde::{Deserialize, Serialize};
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,
};
use crate::models::host_connection::validate_supported_region;
/// An environment record (the module's SSM parameter) or a lock object
/// larger than this is refused unread.
const MAX_UNTRUSTED_BYTES: usize = 4096;
/// A lock string shown to the user is at most this many characters.
const MAX_LOCK_STRING_CHARS: usize = 128;
/// A lock younger than this is a deploy in progress somewhere else; an older
/// one is an apply that died holding it.
const LIVE_LOCK_SECS: u64 = 2 * 3600;
const RECORD_SCHEMA: u64 = 1;
/// Bounds on what one scan lists, so a bucket or parameter path full of
/// junk can't make it unbounded.
const MAX_RECORDS: usize = 500;
const MAX_OBJECTS_PER_PREFIX: usize = 5000;
const MAX_LOCKS: usize = 200;
/// An environment's record, schema 1 (`aws/terraform/environment/main.tf`).
#[allow(dead_code)] // used by phase-4 commands
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct EnvironmentInfo {
pub schema: u32,
pub name: String,
pub template_version: u32,
pub cluster_arn: String,
pub security_group_id: String,
pub subnet_ids: Vec<String>,
pub assign_public_ip: bool,
pub kms_key_arn: String,
pub log_group: String,
pub network_mode: String,
pub tenancy: String,
}
fn field<'a>(obj: &'a Map<String, Value>, name: &str) -> Result<&'a Value, String> {
obj.get(name).ok_or_else(|| format!("{name} is missing"))
}
fn str_field<'a>(obj: &'a Map<String, Value>, name: &str) -> Result<&'a str, String> {
field(obj, name)?
.as_str()
.ok_or_else(|| format!("{name} is not a string"))
}
/// `<prefix>` followed by 1 to 32 lowercase hex characters.
fn is_hex_id(s: &str, prefix: &str) -> bool {
s.strip_prefix(prefix).is_some_and(|hex| {
!hex.is_empty()
&& hex.len() <= 32
&& hex.bytes().all(|c| matches!(c, b'0'..=b'9' | b'a'..=b'f'))
})
}
/// Validate an environment record read from the parameter for `path_name`.
/// Every field is checked against what the module writes, and every ARN
/// must be in `account` and `region`. The error names the field, never its
/// value.
#[allow(dead_code)] // used by phase-4 commands
pub fn parse_environment_record(
path_name: &str,
value: &str,
account: &str,
region: &str,
) -> Result<EnvironmentInfo, String> {
let name = validate_environment_name(path_name)
.map_err(|_| "the parameter's path is not a valid environment name".to_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());
}
if str_field(obj, "name")? != name {
return Err("name does not match the parameter's path".into());
}
let template_version = field(obj, "template_version")?
.as_u64()
.and_then(|v| u32::try_from(v).ok())
.filter(|v| *v >= 1)
.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}") {
return Err(
"cluster_arn is not this environment's cluster in this account and region".into(),
);
}
let security_group_id = str_field(obj, "security_group_id")?;
if !is_hex_id(security_group_id, "sg-") {
return Err("security_group_id is not a security group id".into());
}
let subnet_ids: Vec<String> = field(obj, "subnet_ids")?
.as_array()
.filter(|a| a.len() == 2)
.and_then(|a| {
a.iter()
.map(|s| {
s.as_str()
.filter(|s| is_hex_id(s, "subnet-"))
.map(String::from)
})
.collect::<Option<Vec<_>>>()
})
.ok_or_else(|| "subnet_ids is not exactly 2 subnet ids".to_string())?;
let assign_public_ip = field(obj, "assign_public_ip")?
.as_bool()
.ok_or_else(|| "assign_public_ip is not true or false".to_string())?;
let kms_key_arn = str_field(obj, "kms_key_arn")?;
let key_prefix = format!("arn:aws:kms:{region}:{account}:key/");
let key_ok = kms_key_arn.strip_prefix(&key_prefix).is_some_and(|id| {
!id.is_empty()
&& id.len() <= 64
&& id
.bytes()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')
});
if !key_ok {
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}") {
return Err("log_group is not this environment's log group".into());
}
let network_mode = str_field(obj, "network_mode")?;
if !matches!(network_mode, "public" | "private-nat" | "existing") {
return Err("network_mode is not a known network mode".into());
}
if str_field(obj, "tenancy")? != "strict" {
return Err("tenancy is not strict".into());
}
Ok(EnvironmentInfo {
schema: RECORD_SCHEMA as u32,
name: name.clone(),
template_version,
cluster_arn: cluster_arn.to_string(),
security_group_id: security_group_id.to_string(),
subnet_ids,
assign_public_ip,
kms_key_arn: kms_key_arn.to_string(),
log_group: log_group.to_string(),
network_mode: network_mode.to_string(),
tenancy: "strict".to_string(),
})
}
/// A held state lock, as shown to the user.
#[allow(dead_code)] // used by phase-4 commands
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct LockInfo {
pub who: String,
pub created: String,
pub operation: String,
/// From the lock object's `LastModified`, not its (untrusted) `Created`.
pub age_secs: u64,
/// This machine's runner is applying the deployment.
pub local_process: bool,
}
/// Control characters and the invisible/bidi formatting characters that
/// could disguise text are dropped; the rest is cut to
/// [`MAX_LOCK_STRING_CHARS`].
fn sanitize_lock_string(s: &str) -> String {
s.chars()
.filter(|c| {
!c.is_control()
&& !matches!(
*c,
'\u{200b}'..='\u{200f}'
| '\u{202a}'..='\u{202e}'
| '\u{2060}'..='\u{2064}'
| '\u{2066}'..='\u{2069}'
| '\u{feff}'
)
})
.take(MAX_LOCK_STRING_CHARS)
.collect()
}
/// A lock from its object's contents (S3-native locking's JSON: `ID`,
/// `Operation`, `Info`, `Who`, `Version`, `Created`, `Path`). Contents that
/// are too large or not JSON still mean a held lock, with nothing to show.
#[allow(dead_code)] // used by phase-4 commands
pub fn lock_info(json: &str, age_secs: u64, local_process: bool) -> LockInfo {
let parsed = (json.len() <= MAX_UNTRUSTED_BYTES)
.then(|| serde_json::from_str::<Value>(json).ok())
.flatten();
let get = |k: &str| {
parsed
.as_ref()
.and_then(|v| v.get(k))
.and_then(Value::as_str)
.map(sanitize_lock_string)
.unwrap_or_default()
};
LockInfo {
who: get("Who"),
created: get("Created"),
operation: get("Operation"),
age_secs,
local_process,
}
}
#[allow(dead_code)] // used by phase-4 commands
#[derive(Serialize, Clone, Debug, PartialEq)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum EnvironmentState {
/// Record and state, at the app's module version.
Ready,
/// The lock is held by a local runner or recently by someone else.
Deploying { lock: Option<LockInfo> },
/// Record and state at an older module version.
UpdateAvailable,
/// State without a record (a failed apply), or a stale lock.
Incomplete { lock: Option<LockInfo> },
/// A record whose state this caller can't see (another bucket, an admin
/// bundle, or no bucket access).
ExternallyManaged,
/// The record failed validation.
UnreadableRecord { reason: String },
}
#[allow(dead_code)] // used by phase-4 commands
#[derive(Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum EnvironmentAction {
Join,
Update,
Repair,
Destroy,
Follow,
Unlock,
}
#[allow(dead_code)] // used by phase-4 commands
#[derive(Serialize, Clone, Debug, PartialEq)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum OnboardingState {
Joined,
NotJoined,
InProgress,
Incomplete,
}
#[allow(dead_code)] // used by phase-4 commands
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct DetectedEnvironment {
pub name: String,
pub template_version: Option<u32>,
pub state: EnvironmentState,
pub actions: Vec<EnvironmentAction>,
pub info: Option<EnvironmentInfo>,
/// The caller's onboarding; resolved only for joinable environments.
pub onboarding: Option<OnboardingState>,
}
#[allow(dead_code)] // used by phase-4 commands
#[derive(Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Knowable {
Yes,
No,
/// Denied: this caller can't tell.
Unknown,
}
#[allow(dead_code)] // used by phase-4 commands
#[derive(Serialize, Clone, Debug)]
pub struct EnvironmentScan {
pub region: String,
pub account: String,
/// `aws:userid`.
pub owner_id: String,
pub owner_key: String,
pub state_bucket: Knowable,
pub boundary: Knowable,
pub environments: Vec<DetectedEnvironment>,
}
/// The state bucket's listing under `environments/`, `users/` and
/// `boundary/`.
#[allow(dead_code)] // used by phase-4 commands
#[derive(Debug, Default)]
pub struct StateObjects {
/// State object keys.
pub states: Vec<String>,
/// (`<state key>.tflock`, its contents, age in seconds from its
/// `LastModified`).
pub locks: Vec<(String, String, u64)>,
}
impl StateObjects {
fn has_state(&self, key: &str) -> bool {
self.states.iter().any(|k| k == key)
}
/// The lock on `state_key`, as (contents, age).
fn lock(&self, state_key: &str) -> Option<(&str, u64)> {
self.locks
.iter()
.find(|(k, _, _)| k.strip_suffix(".tflock") == Some(state_key))
.map(|(_, json, age)| (json.as_str(), *age))
}
}
/// The environment name of an `environments/<env>/terraform.tfstate[.tflock]`
/// key, if it is one the app could have written.
fn environment_of_key(key: &str) -> Option<&str> {
let key = key.strip_suffix(".tflock").unwrap_or(key);
let env = key
.strip_prefix("environments/")?
.strip_suffix("/terraform.tfstate")?;
(validate_environment_name(env).is_ok()).then_some(env)
}
/// Whether this machine's runner is applying a deployment
/// (`tofu::is_running(&tofu::workdir_for(..))`).
pub type RunningLocally<'a> = &'a (dyn Fn(Deployment) -> bool + Sync);
/// One environment from its record (if any), the bucket listing (if this
/// caller can read it) and whether a local runner is applying it.
fn detect_one(
name: &str,
record: Option<Result<EnvironmentInfo, String>>,
objects: Option<&StateObjects>,
running_locally: RunningLocally,
current_version: u32,
) -> DetectedEnvironment {
use EnvironmentAction::*;
let key = state_key(Deployment::Environment(name));
let local = running_locally(Deployment::Environment(name));
let lock = objects
.and_then(|o| o.lock(&key))
.map(|(json, age)| lock_info(json, age, local));
let has_state = objects.is_some_and(|o| o.has_state(&key));
let (info, reason) = match record {
Some(Ok(info)) => (Some(info), None),
Some(Err(reason)) => (None, Some(reason)),
None => (None, None),
};
let template_version = info.as_ref().map(|i| i.template_version);
let (state, actions) = if local {
(EnvironmentState::Deploying { lock }, vec![Follow])
} else if lock.as_ref().is_some_and(|l| l.age_secs < LIVE_LOCK_SECS) {
(EnvironmentState::Deploying { lock }, vec![])
} else if lock.is_some() {
// Stale, and not ours (a local runner was handled above).
(
EnvironmentState::Incomplete { lock },
vec![Repair, Destroy, Unlock],
)
} else if let Some(reason) = reason {
// A re-apply rewrites the record, so Repair is offered when the
// state is ours. Never Destroy: the environment may be working (D6).
let actions = if has_state { vec![Repair] } else { vec![] };
(EnvironmentState::UnreadableRecord { reason }, actions)
} else if let Some(info) = &info {
if !has_state {
(EnvironmentState::ExternallyManaged, vec![Join])
} else if info.template_version < current_version {
(EnvironmentState::UpdateAvailable, vec![Join, Update])
} else {
(EnvironmentState::Ready, vec![Join])
}
} else {
// Only reachable with state and no record: a failed apply.
(
EnvironmentState::Incomplete { lock: None },
vec![Repair, Destroy],
)
};
DetectedEnvironment {
name: name.to_string(),
template_version,
state,
actions,
info,
onboarding: None,
}
}
/// Combine the records and the bucket listing into one list of
/// environments, sorted by name. `objects` is `None` when the caller can't
/// read the bucket (or there is none). Onboarding is left unresolved
/// ([`scan`] does that).
#[allow(dead_code)] // used by phase-4 commands
pub fn merge(
records: Vec<(String, Result<EnvironmentInfo, String>)>,
objects: Option<&StateObjects>,
running_locally: RunningLocally,
current_version: u32,
) -> Vec<DetectedEnvironment> {
let mut by_name: BTreeMap<String, Option<Result<EnvironmentInfo, String>>> = BTreeMap::new();
for (name, record) in records {
by_name.insert(name, Some(record));
}
if let Some(o) = objects {
let keys = o.states.iter().chain(o.locks.iter().map(|(k, _, _)| k));
for env in keys.filter_map(|k| environment_of_key(k)) {
by_name.entry(env.to_string()).or_insert(None);
}
}
by_name
.into_iter()
.map(|(name, record)| detect_one(&name, record, objects, running_locally, current_version))
.collect()
}
/// The caller's onboarding from the bucket alone, or `None` when only
/// `iam:GetRole` on the task role can tell.
fn onboarding_from_objects(
objects: Option<&StateObjects>,
deployment: Deployment,
local: bool,
) -> Option<OnboardingState> {
if local {
return Some(OnboardingState::InProgress);
}
let objects = objects?;
let key = state_key(deployment);
match objects.lock(&key) {
Some((_, age)) if age < LIVE_LOCK_SECS => Some(OnboardingState::InProgress),
Some(_) => Some(OnboardingState::Incomplete),
None if objects.has_state(&key) => Some(OnboardingState::Joined),
None => None,
}
}
/// What detection asks AWS. Each method returns a `Send` future so [`scan`]
/// is `Send` (a Tauri command needs that) for any implementation; an impl
/// may still write them as `async fn`. Access denied is an answer
/// (`Unknown` / `None`), not an error.
#[allow(dead_code)] // used by phase-4 commands
pub trait DetectApi: Send + Sync {
/// (parameter name, value) under `/triple-c/environments/`.
fn environment_records(
&self,
) -> impl Future<Output = Result<Vec<(String, String)>, AwsError>> + Send;
/// `None` = the bucket is missing or this caller can't list it.
fn state_objects(
&self,
bucket: &str,
) -> impl Future<Output = Result<Option<StateObjects>, AwsError>> + Send;
fn bucket_exists(
&self,
bucket: &str,
) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
fn boundary_exists(
&self,
account: &str,
) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
fn role_exists(&self, name: &str) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
}
/// Everything the account holds for this caller in `region`.
#[allow(dead_code)] // used by phase-4 commands
pub async fn scan<D: DetectApi>(
d: &D,
who: &CallerIdentity,
region: &str,
running_locally: RunningLocally<'_>,
) -> Result<EnvironmentScan, AwsError> {
let prefix = environment_parameter_name("");
let records = d
.environment_records()
.await?
.into_iter()
.filter_map(|(param, value)| {
let name = param.strip_prefix(&prefix)?;
if validate_environment_name(name).is_err() {
log::warn!("Ignoring an environment record under an invalid name");
return None;
}
let parsed = parse_environment_record(name, &value, &who.account, region);
Some((name.to_string(), parsed))
})
.collect();
let bucket = state_bucket_name(&who.account, region);
let state_bucket = d.bucket_exists(&bucket).await?;
let objects = match state_bucket {
Knowable::Yes => d.state_objects(&bucket).await?,
_ => None,
};
let boundary = d.boundary_exists(&who.account).await?;
let mut environments = merge(
records,
objects.as_ref(),
running_locally,
super::tofu_modules::ENVIRONMENT_MODULE_VERSION,
);
let key = owner_key(&who.user_id);
for env in &mut environments {
if !env.actions.contains(&EnvironmentAction::Join) {
continue;
}
let deployment = Deployment::User {
env: &env.name,
owner_key: &key,
};
let from_objects =
onboarding_from_objects(objects.as_ref(), deployment, running_locally(deployment));
env.onboarding = Some(match from_objects {
Some(state) => state,
None => {
let (task_role, _) = user_role_names(&env.name, &key);
match d.role_exists(&task_role).await? {
Knowable::Yes => OnboardingState::Joined,
Knowable::No | Knowable::Unknown => OnboardingState::NotJoined,
}
}
});
}
Ok(EnvironmentScan {
region: region.to_string(),
account: who.account.clone(),
owner_id: who.user_id.clone(),
owner_key: key,
state_bucket,
boundary,
environments,
})
}
/// What a failed lookup means.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Lookup {
Missing,
Denied,
Error,
}
/// From a service error's code and HTTP status. HeadBucket has no body, so
/// its 403/404 carry no code; a 403 is also what an `expected_bucket_owner`
/// mismatch gives, which is right: a bucket of another account's is not
/// ours to know about.
fn lookup_outcome(code: Option<&str>, status: Option<u16>) -> Lookup {
const MISSING: &[&str] = &["NotFound", "NoSuchEntity", "NoSuchBucket", "NoSuchKey"];
const DENIED: &[&str] = &["AccessDenied", "AccessDeniedException", "Forbidden"];
match (code, status) {
(Some(c), _) if MISSING.contains(&c) => Lookup::Missing,
(Some(c), _) if DENIED.contains(&c) => Lookup::Denied,
(None, Some(404)) => Lookup::Missing,
(None, Some(403)) => Lookup::Denied,
(_, Some(403)) => Lookup::Denied,
_ => Lookup::Error,
}
}
/// Code and HTTP status of an SDK error (no status for a non-service
/// error). `SdkError` is the same type in every SDK crate.
fn error_parts<E: ProvideErrorMetadata>(
err: &SdkError<E, HttpResponse>,
) -> (Option<String>, Option<u16>) {
(
err.code().map(str::to_string),
err.raw_response().map(|r| r.status().as_u16()),
)
}
/// Yes / No (missing) / Unknown (denied) from a lookup's result.
fn knowable<T, E>(
action: &str,
res: Result<T, SdkError<E, HttpResponse>>,
) -> Result<Knowable, AwsError>
where
E: ProvideErrorMetadata + std::error::Error + Send + Sync + 'static,
{
match res {
Ok(_) => Ok(Knowable::Yes),
Err(e) => {
let (code, status) = error_parts(&e);
match lookup_outcome(code.as_deref(), status) {
Lookup::Missing => Ok(Knowable::No),
Lookup::Denied => Ok(Knowable::Unknown),
Lookup::Error => Err(classify_sdk_error(action, &e)),
}
}
}
}
/// The real thing, from a connection's `SdkConfig`.
#[allow(dead_code)] // used by phase-4 commands
pub struct SdkDetectApi {
ssm: aws_sdk_ssm::Client,
s3: aws_sdk_s3::Client,
iam: aws_sdk_iam::Client,
/// `expected_bucket_owner` on every S3 call.
account: String,
}
impl SdkDetectApi {
#[allow(dead_code)] // used by phase-4 commands
pub fn new(cfg: &SdkConfig, account: &str) -> Self {
SdkDetectApi {
ssm: aws_sdk_ssm::Client::new(cfg),
s3: aws_sdk_s3::Client::new(cfg),
iam: aws_sdk_iam::Client::new(cfg),
account: account.to_string(),
}
}
/// Keys (and their age) under `prefix`, or `None` when listing is
/// denied or the bucket is gone.
async fn list(
&self,
bucket: &str,
prefix: &str,
) -> Result<Option<Vec<(String, u64, i64)>>, AwsError> {
const ACTION: &str = "s3:ListBucket";
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
let mut out = Vec::new();
let mut token: Option<String> = None;
loop {
let res = self
.s3
.list_objects_v2()
.bucket(bucket)
.prefix(prefix)
.expected_bucket_owner(&self.account)
.set_continuation_token(token.take())
.send()
.await;
let page = match res {
Ok(p) => p,
Err(e) => {
let (code, status) = error_parts(&e);
return match lookup_outcome(code.as_deref(), status) {
Lookup::Missing | Lookup::Denied => Ok(None),
Lookup::Error => Err(classify_sdk_error(ACTION, &e)),
};
}
};
for obj in page.contents() {
let Some(key) = obj.key() else { continue };
let age = obj
.last_modified()
.map(|t| now.saturating_sub(t.secs()).max(0) as u64)
.unwrap_or(0);
out.push((key.to_string(), age, obj.size().unwrap_or(0)));
}
if out.len() >= MAX_OBJECTS_PER_PREFIX {
log::warn!("The state bucket lists more than {MAX_OBJECTS_PER_PREFIX} objects under {prefix}; ignoring the rest");
break;
}
match page.next_continuation_token() {
Some(t) if page.is_truncated() == Some(true) => token = Some(t.to_string()),
_ => break,
}
}
Ok(Some(out))
}
/// A lock object's contents; empty when it is too large to read or can't
/// be read, `None` when it was released since the listing.
async fn lock_contents(
&self,
bucket: &str,
key: &str,
size: i64,
) -> Result<Option<String>, AwsError> {
const ACTION: &str = "s3:GetObject";
if size < 0 || size as usize > MAX_UNTRUSTED_BYTES {
return Ok(Some(String::new()));
}
let res = self
.s3
.get_object()
.bucket(bucket)
.key(key)
.expected_bucket_owner(&self.account)
.send()
.await;
let obj = match res {
Ok(o) => o,
Err(e) => {
let (code, status) = error_parts(&e);
return match lookup_outcome(code.as_deref(), status) {
Lookup::Missing => Ok(None),
Lookup::Denied => Ok(Some(String::new())),
Lookup::Error => Err(classify_sdk_error(ACTION, &e)),
};
}
};
if obj
.content_length()
.is_some_and(|n| n < 0 || n as usize > MAX_UNTRUSTED_BYTES)
{
return Ok(Some(String::new()));
}
let bytes = obj
.body
.collect()
.await
.map_err(|e| AwsError::Other {
message: format!("Could not read a state lock: {e}"),
})?
.into_bytes();
if bytes.len() > MAX_UNTRUSTED_BYTES {
return Ok(Some(String::new()));
}
Ok(Some(String::from_utf8_lossy(&bytes).into_owned()))
}
}
impl DetectApi for SdkDetectApi {
async fn environment_records(&self) -> Result<Vec<(String, String)>, AwsError> {
const ACTION: &str = "ssm:GetParametersByPath";
let path = environment_parameter_name("");
let mut out = Vec::new();
let mut token: Option<String> = None;
loop {
let page = self
.ssm
.get_parameters_by_path()
.path(path.trim_end_matches('/'))
.recursive(false)
.with_decryption(false)
.set_next_token(token.take())
.send()
.await
.map_err(|e| classify_sdk_error(ACTION, &e))?;
for p in page.parameters() {
if let (Some(name), Some(value)) = (p.name(), p.value()) {
out.push((name.to_string(), value.to_string()));
}
}
if out.len() >= MAX_RECORDS {
log::warn!("More than {MAX_RECORDS} environment records; ignoring the rest");
break;
}
match page.next_token() {
Some(t) => token = Some(t.to_string()),
None => break,
}
}
Ok(out)
}
async fn state_objects(&self, bucket: &str) -> Result<Option<StateObjects>, AwsError> {
let mut objects = StateObjects::default();
let mut lock_keys = Vec::new();
for prefix in ["environments/", "users/", "boundary/"] {
let Some(listed) = self.list(bucket, prefix).await? else {
return Ok(None);
};
for (key, age, size) in listed {
if key.ends_with("/terraform.tfstate") {
objects.states.push(key);
} else if key.ends_with("/terraform.tfstate.tflock") {
lock_keys.push((key, age, size));
}
}
}
if lock_keys.len() > MAX_LOCKS {
log::warn!("More than {MAX_LOCKS} state locks; ignoring the rest");
lock_keys.truncate(MAX_LOCKS);
}
for (key, age, size) in lock_keys {
if let Some(json) = self.lock_contents(bucket, &key, size).await? {
objects.locks.push((key, json, age));
}
}
Ok(Some(objects))
}
async fn bucket_exists(&self, bucket: &str) -> Result<Knowable, AwsError> {
let res = self
.s3
.head_bucket()
.bucket(bucket)
.expected_bucket_owner(&self.account)
.send()
.await;
knowable("s3:ListBucket", res)
}
async fn boundary_exists(&self, account: &str) -> Result<Knowable, AwsError> {
let arn = format!("arn:aws:iam::{account}:policy/{BOUNDARY_POLICY_NAME}");
let res = self.iam.get_policy().policy_arn(arn).send().await;
knowable("iam:GetPolicy", res)
}
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)
}
}
/// `s3:CreateBucket`'s answer, by error code: `Some(Ok)` for a bucket this
/// account already owns, `Some(Err)` for a name another account holds,
/// `None` for an ordinary error to classify.
fn create_bucket_outcome(code: Option<&str>, bucket: &str) -> Option<Result<(), AwsError>> {
match code {
Some("BucketAlreadyOwnedByYou") => Some(Ok(())),
Some("BucketAlreadyExists") => Some(Err(AwsError::StateBucketTaken {
bucket: bucket.to_string(),
})),
_ => None,
}
}
const STATE_TAG: (&str, &str) = ("triple-c:component", "state");
fn state_tag() -> Tag {
Tag::builder()
.key(STATE_TAG.0)
.value(STATE_TAG.1)
.build()
.expect("key and value are set")
}
/// The component tag, and a `LocationConstraint` everywhere but
/// `us-east-1` (which refuses one).
fn create_bucket_configuration(region: &str) -> CreateBucketConfiguration {
let b = CreateBucketConfiguration::builder().tags(state_tag());
if region == "us-east-1" {
b.build()
} else {
b.location_constraint(BucketLocationConstraint::from(region))
.build()
}
}
/// Deny every request to the bucket that doesn't use TLS.
fn state_bucket_policy(bucket: &str) -> String {
json!({
"Version": "2012-10-17",
"Statement": [{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
format!("arn:aws:s3:::{bucket}"),
format!("arn:aws:s3:::{bucket}/*"),
],
"Condition": { "Bool": { "aws:SecureTransport": "false" } },
}],
})
.to_string()
}
/// Create (or adopt) `triple-c-tfstate-<account>-<region>` and configure it:
/// the component tag, versioning, SSE-S3, all four public-access blocks and
/// a TLS-only policy. Idempotent. Every call after the create carries
/// `expected_bucket_owner`, so a bucket of another account's is never
/// configured or trusted.
#[allow(dead_code)] // used by phase-4 commands
pub async fn ensure_state_bucket(
cfg: &SdkConfig,
account: &str,
region: &str,
) -> Result<String, AwsError> {
let region =
validate_supported_region(region).map_err(|message| AwsError::Other { message })?;
let bucket = state_bucket_name(account, &region);
let s3 = aws_sdk_s3::Client::new(cfg);
if let Err(e) = s3
.create_bucket()
.bucket(&bucket)
.create_bucket_configuration(create_bucket_configuration(&region))
.send()
.await
{
match create_bucket_outcome(e.code(), &bucket) {
Some(Ok(())) => {}
Some(Err(taken)) => return Err(taken),
None => return Err(classify_sdk_error("s3:CreateBucket", &e)),
}
}
// us-east-1 answers a create of a bucket this account owns with success,
// not BucketAlreadyOwnedByYou: confirm ownership before configuring.
s3.head_bucket()
.bucket(&bucket)
.expected_bucket_owner(account)
.send()
.await
.map_err(|e| classify_sdk_error("s3:ListBucket", &e))?;
s3.put_bucket_tagging()
.bucket(&bucket)
.expected_bucket_owner(account)
.tagging(
Tagging::builder()
.tag_set(state_tag())
.build()
.expect("tag set is set"),
)
.send()
.await
.map_err(|e| classify_sdk_error("s3:PutBucketTagging", &e))?;
s3.put_bucket_versioning()
.bucket(&bucket)
.expected_bucket_owner(account)
.versioning_configuration(
VersioningConfiguration::builder()
.status(BucketVersioningStatus::Enabled)
.build(),
)
.send()
.await
.map_err(|e| classify_sdk_error("s3:PutBucketVersioning", &e))?;
s3.put_bucket_encryption()
.bucket(&bucket)
.expected_bucket_owner(account)
.server_side_encryption_configuration(
ServerSideEncryptionConfiguration::builder()
.rules(
ServerSideEncryptionRule::builder()
.apply_server_side_encryption_by_default(
ServerSideEncryptionByDefault::builder()
.sse_algorithm(ServerSideEncryption::Aes256)
.build()
.expect("algorithm is set"),
)
.build(),
)
.build()
.expect("rules are set"),
)
.send()
.await
.map_err(|e| classify_sdk_error("s3:PutEncryptionConfiguration", &e))?;
s3.put_public_access_block()
.bucket(&bucket)
.expected_bucket_owner(account)
.public_access_block_configuration(
PublicAccessBlockConfiguration::builder()
.block_public_acls(true)
.ignore_public_acls(true)
.block_public_policy(true)
.restrict_public_buckets(true)
.build(),
)
.send()
.await
.map_err(|e| classify_sdk_error("s3:PutBucketPublicAccessBlock", &e))?;
s3.put_bucket_policy()
.bucket(&bucket)
.expected_bucket_owner(account)
.policy(state_bucket_policy(&bucket))
.send()
.await
.map_err(|e| classify_sdk_error("s3:PutBucketPolicy", &e))?;
Ok(bucket)
}
#[cfg(test)]
mod tests;
+703
View File
@@ -0,0 +1,703 @@
use std::collections::HashMap;
use std::sync::Mutex;
use serde_json::{json, Value};
use super::*;
use crate::aws::tofu_modules::{owner_key, user_role_names};
const ACCOUNT: &str = "123456789012";
const REGION: &str = "eu-west-1";
const USER_ID: &str = "AROAEXAMPLEID:alice";
fn record(name: &str, version: u32) -> Value {
json!({
"schema": 1,
"name": name,
"template_version": version,
"cluster_arn": format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-{name}"),
"security_group_id": "sg-0123456789abcdef0",
"subnet_ids": ["subnet-0123456789abcdef0", "subnet-0fedcba9876543210"],
"assign_public_ip": true,
"kms_key_arn": format!(
"arn:aws:kms:{REGION}:{ACCOUNT}:key/1234abcd-12ab-34cd-56ef-1234567890ab"
),
"log_group": format!("/triple-c/{name}"),
"network_mode": "public",
"tenancy": "strict",
})
}
fn parsed(name: &str, version: u32) -> EnvironmentInfo {
parse_environment_record(name, &record(name, version).to_string(), ACCOUNT, REGION).unwrap()
}
fn with(name: &str, field: &str, value: Value) -> String {
let mut r = record(name, 1);
r[field] = value;
r.to_string()
}
fn without(name: &str, field: &str) -> String {
let mut r = record(name, 1);
r.as_object_mut().unwrap().remove(field);
r.to_string()
}
#[test]
fn untrusted_environment_records_are_rejected() {
let valid = parsed("dev", 1);
assert_eq!(valid.name, "dev");
assert_eq!(valid.template_version, 1);
assert_eq!(valid.subnet_ids.len(), 2);
assert!(valid.assign_public_ip);
assert_eq!(valid.tenancy, "strict");
let other_account = "999999999999";
let secret = "s3cr3t-value-never-echoed";
let cases: Vec<(&str, String, &str)> = vec![
(
"too large",
format!("{{\"pad\":\"{}\"}}", "x".repeat(5000)),
"4 KiB",
),
("not json", "not json at all".into(), "JSON"),
("json but not an object", "[1,2]".into(), "JSON"),
("schema 2", with("dev", "schema", json!(2)), "schema"),
("schema missing", without("dev", "schema"), "schema"),
(
"name differs from the path",
with("dev", "name", json!("prod")),
"name",
),
(
"cluster in another account",
with(
"dev",
"cluster_arn",
json!(format!(
"arn:aws:ecs:{REGION}:{other_account}:cluster/triple-c-dev"
)),
),
"cluster_arn",
),
(
"cluster in another region",
with(
"dev",
"cluster_arn",
json!(format!(
"arn:aws:ecs:us-east-1:{ACCOUNT}:cluster/triple-c-dev"
)),
),
"cluster_arn",
),
(
"kms key in another account",
with(
"dev",
"kms_key_arn",
json!(format!("arn:aws:kms:{REGION}:{other_account}:key/abcd")),
),
"kms_key_arn",
),
(
"kms arn of another service",
with(
"dev",
"kms_key_arn",
json!(format!("arn:aws:s3:{REGION}:{ACCOUNT}:key/abcd")),
),
"kms_key_arn",
),
(
"one subnet",
with("dev", "subnet_ids", json!(["subnet-0123abcd"])),
"subnet_ids",
),
(
"three subnets",
with(
"dev",
"subnet_ids",
json!(["subnet-0123abcd", "subnet-0123abce", "subnet-0123abcf"]),
),
"subnet_ids",
),
(
"a subnet that isn't one",
with("dev", "subnet_ids", json!(["subnet-0123abcd", secret])),
"subnet_ids",
),
(
"uppercase subnet hex",
with(
"dev",
"subnet_ids",
json!(["subnet-0123ABCD", "subnet-0123abcd"]),
),
"subnet_ids",
),
(
"security group that isn't one",
with("dev", "security_group_id", json!(secret)),
"security_group_id",
),
(
"security group of the wrong type",
with("dev", "security_group_id", json!(7)),
"security_group_id",
),
(
"shared tenancy",
with("dev", "tenancy", json!("shared")),
"tenancy",
),
(
"log group outside /triple-c/",
with("dev", "log_group", json!(secret)),
"log_group",
),
(
"unknown network mode",
with("dev", "network_mode", json!(secret)),
"network_mode",
),
(
"assign_public_ip not a bool",
with("dev", "assign_public_ip", json!("yes")),
"assign_public_ip",
),
(
"template_version not a number",
with("dev", "template_version", json!(secret)),
"template_version",
),
];
for (what, value, field) in cases {
let err = parse_environment_record("dev", &value, ACCOUNT, REGION)
.expect_err(&format!("accepted: {what}"));
assert!(err.contains(field), "{what}: {err:?} should name {field}");
assert!(!err.contains(secret), "{what}: {err:?} echoes the value");
assert!(
!err.contains(other_account),
"{what}: {err:?} echoes the value"
);
}
// The path's name is itself checked.
assert!(
parse_environment_record("Bad Name", &record("dev", 1).to_string(), ACCOUNT, REGION)
.is_err()
);
}
fn nobody_local(_: Deployment) -> bool {
false
}
fn env_key(env: &str) -> String {
state_key(Deployment::Environment(env))
}
fn lock_json(who: &str) -> String {
json!({
"ID": "8a1c-uuid",
"Operation": "OperationTypeApply",
"Info": "",
"Who": who,
"Version": "1.10.6",
"Created": "2026-11-14T10:00:00Z",
"Path": "triple-c-tfstate/environments/x/terraform.tfstate",
})
.to_string()
}
fn objects(states: &[String], locks: &[(String, u64)]) -> StateObjects {
StateObjects {
states: states.to_vec(),
locks: locks
.iter()
.map(|(k, age)| (format!("{k}.tflock"), lock_json("admin@laptop"), *age))
.collect(),
}
}
fn find<'a>(envs: &'a [DetectedEnvironment], name: &str) -> &'a DetectedEnvironment {
envs.iter()
.find(|e| e.name == name)
.unwrap_or_else(|| panic!("{name} not listed in {envs:#?}"))
}
#[test]
fn a_record_without_state_and_state_without_a_record_are_both_listed() {
let objs = objects(&[env_key("orphan")], &[]);
let envs = merge(
vec![("shared".into(), Ok(parsed("shared", 1)))],
Some(&objs),
&nobody_local,
1,
);
assert_eq!(envs.len(), 2, "{envs:#?}");
let shared = find(&envs, "shared");
assert_eq!(shared.state, EnvironmentState::ExternallyManaged);
assert_eq!(shared.actions, vec![EnvironmentAction::Join]);
assert_eq!(shared.template_version, Some(1));
assert!(shared.info.is_some());
let orphan = find(&envs, "orphan");
assert_eq!(orphan.state, EnvironmentState::Incomplete { lock: None });
assert_eq!(
orphan.actions,
vec![EnvironmentAction::Repair, EnvironmentAction::Destroy]
);
assert_eq!(orphan.template_version, None);
assert!(orphan.info.is_none());
// Without bucket access (a workspace user) a record is externally
// managed too, and nothing else is listed.
let envs = merge(
vec![("shared".into(), Ok(parsed("shared", 1)))],
None,
&nobody_local,
1,
);
assert_eq!(envs.len(), 1);
assert_eq!(envs[0].state, EnvironmentState::ExternallyManaged);
// An unreadable record is listed with its reason.
let envs = merge(
vec![("bad".into(), Err("tenancy is not strict".into()))],
None,
&nobody_local,
1,
);
assert_eq!(
envs[0].state,
EnvironmentState::UnreadableRecord {
reason: "tenancy is not strict".into()
}
);
assert!(envs[0].actions.is_empty());
}
#[test]
fn interrupted_deployments_get_the_right_actions() {
let stale = 3 * 3600;
let fresh = 60;
// A stale lock, no local process: Incomplete, with Unlock.
let objs = objects(&[env_key("a")], &[(env_key("a"), stale)]);
let envs = merge(Vec::new(), Some(&objs), &nobody_local, 1);
let a = find(&envs, "a");
let EnvironmentState::Incomplete { lock: Some(lock) } = &a.state else {
panic!("{a:#?}");
};
assert_eq!(lock.who, "admin@laptop");
assert_eq!(lock.operation, "OperationTypeApply");
assert_eq!(lock.created, "2026-11-14T10:00:00Z");
assert_eq!(lock.age_secs, stale);
assert!(!lock.local_process);
assert_eq!(
a.actions,
vec![
EnvironmentAction::Repair,
EnvironmentAction::Destroy,
EnvironmentAction::Unlock
]
);
// A stale lock on an environment that has a record is still Incomplete.
let envs = merge(
vec![("a".into(), Ok(parsed("a", 1)))],
Some(&objs),
&nobody_local,
1,
);
assert!(matches!(
find(&envs, "a").state,
EnvironmentState::Incomplete { lock: Some(_) }
));
// A fresh lock (a first apply: no state object yet): Deploying, nothing
// to do but wait.
let objs = objects(&[], &[(env_key("b"), fresh)]);
let envs = merge(Vec::new(), Some(&objs), &nobody_local, 1);
let b = find(&envs, "b");
let EnvironmentState::Deploying { lock: Some(lock) } = &b.state else {
panic!("{b:#?}");
};
assert!(!lock.local_process);
assert!(b.actions.is_empty());
// A local process, even under an old lock: Deploying, Follow.
let objs = objects(&[env_key("c")], &[(env_key("c"), stale)]);
let local_c = |d: Deployment| d == Deployment::Environment("c");
let envs = merge(Vec::new(), Some(&objs), &local_c, 1);
let c = find(&envs, "c");
let EnvironmentState::Deploying { lock: Some(lock) } = &c.state else {
panic!("{c:#?}");
};
assert!(lock.local_process);
assert_eq!(c.actions, vec![EnvironmentAction::Follow]);
}
#[test]
fn lock_contents_are_capped_and_sanitized() {
let nasty = json!({
"Who": format!("evil\u{1b}[31m\u{202e}name\n{}", "w".repeat(1000)),
"Operation": "OperationTypeApply",
"Created": "2026-11-14T10:00:00Z",
})
.to_string();
let lock = lock_info(&nasty, 10, false);
assert!(!lock.who.chars().any(|c| c.is_control()), "{:?}", lock.who);
assert!(!lock.who.contains('\u{202e}'));
assert!(lock.who.starts_with("evil[31mname"), "{:?}", lock.who);
assert!(lock.who.chars().count() <= 128);
// Oversized or unparsable contents are a held lock with nothing to say.
let huge = json!({ "Who": "x".repeat(5000) }).to_string();
for text in [huge.as_str(), "not json", ""] {
let lock = lock_info(text, 7, false);
assert_eq!(lock.who, "");
assert_eq!(lock.age_secs, 7);
}
}
#[test]
fn an_older_module_offers_update() {
let objs = objects(&[env_key("old"), env_key("new")], &[]);
let envs = merge(
vec![
("old".into(), Ok(parsed("old", 1))),
("new".into(), Ok(parsed("new", 2))),
],
Some(&objs),
&nobody_local,
2,
);
let old = find(&envs, "old");
assert_eq!(old.state, EnvironmentState::UpdateAvailable);
assert_eq!(
old.actions,
vec![EnvironmentAction::Join, EnvironmentAction::Update]
);
assert_eq!(old.template_version, Some(1));
let new = find(&envs, "new");
assert_eq!(new.state, EnvironmentState::Ready);
assert_eq!(new.actions, vec![EnvironmentAction::Join]);
}
/// A `DetectApi` that answers from fixed data and records what it was asked.
struct FakeDetect {
records: Vec<(String, String)>,
objects: Option<StateObjects>,
bucket: Knowable,
boundary: Knowable,
roles: HashMap<String, Knowable>,
calls: Mutex<Vec<String>>,
}
impl FakeDetect {
fn new() -> Self {
FakeDetect {
records: Vec::new(),
objects: None,
bucket: Knowable::Yes,
boundary: Knowable::Yes,
roles: HashMap::new(),
calls: Mutex::new(Vec::new()),
}
}
fn with_record(mut self, name: &str, version: u32) -> Self {
self.records.push((
environment_parameter_name(name),
record(name, version).to_string(),
));
self
}
fn calls(&self) -> Vec<String> {
self.calls.lock().unwrap().clone()
}
fn log(&self, call: String) {
self.calls.lock().unwrap().push(call);
}
}
impl DetectApi for FakeDetect {
async fn environment_records(&self) -> Result<Vec<(String, String)>, AwsError> {
self.log("records".into());
Ok(self.records.clone())
}
async fn state_objects(&self, bucket: &str) -> Result<Option<StateObjects>, AwsError> {
self.log(format!("objects {bucket}"));
Ok(self.objects.as_ref().map(|o| StateObjects {
states: o.states.clone(),
locks: o.locks.clone(),
}))
}
async fn bucket_exists(&self, bucket: &str) -> Result<Knowable, AwsError> {
self.log(format!("bucket {bucket}"));
Ok(self.bucket.clone())
}
async fn boundary_exists(&self, account: &str) -> Result<Knowable, AwsError> {
self.log(format!("boundary {account}"));
Ok(self.boundary.clone())
}
async fn role_exists(&self, name: &str) -> Result<Knowable, AwsError> {
self.log(format!("role {name}"));
Ok(self.roles.get(name).cloned().unwrap_or(Knowable::No))
}
}
fn who() -> CallerIdentity {
CallerIdentity {
account: ACCOUNT.into(),
arn: format!("arn:aws:sts::{ACCOUNT}:assumed-role/Admin/alice"),
user_id: USER_ID.into(),
}
}
fn run_scan(d: &FakeDetect, local: &(dyn Fn(Deployment) -> bool + Sync)) -> EnvironmentScan {
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(scan(d, &who(), REGION, local))
.unwrap()
}
fn user_key(env: &str) -> String {
state_key(Deployment::User {
env,
owner_key: &owner_key(USER_ID),
})
}
fn task_role(env: &str) -> String {
user_role_names(env, &owner_key(USER_ID)).0
}
#[test]
fn onboarding_is_found_by_state_then_by_role() {
let mut d = FakeDetect::new()
.with_record("bystate", 1)
.with_record("byrole", 1)
.with_record("denied", 1)
.with_record("absent", 1)
.with_record("joining", 1);
d.objects = Some(StateObjects {
states: vec![
env_key("bystate"),
env_key("byrole"),
env_key("denied"),
env_key("absent"),
env_key("joining"),
user_key("bystate"),
],
locks: vec![(
format!("{}.tflock", user_key("joining")),
lock_json("me"),
30,
)],
});
d.roles.insert(task_role("byrole"), Knowable::Yes);
d.roles.insert(task_role("denied"), Knowable::Unknown);
let scan = run_scan(&d, &nobody_local);
assert_eq!(scan.owner_id, USER_ID);
assert_eq!(scan.owner_key, owner_key(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("denied"), Some(OnboardingState::NotJoined));
assert_eq!(onboarding("absent"), Some(OnboardingState::NotJoined));
assert_eq!(onboarding("joining"), Some(OnboardingState::InProgress));
// The state key answered for `bystate` and the lock for `joining`, so
// only the others cost a GetRole.
let roles: Vec<String> = d
.calls()
.into_iter()
.filter(|c| c.starts_with("role "))
.collect();
assert!(!roles.contains(&format!("role {}", task_role("bystate"))));
assert!(!roles.contains(&format!("role {}", task_role("joining"))));
assert!(roles.contains(&format!("role {}", task_role("byrole"))));
assert_eq!(roles.len(), 3, "{roles:?}");
// A local onboarding run is InProgress; a stale onboarding lock is
// Incomplete.
let mut d = FakeDetect::new().with_record("x", 1).with_record("y", 1);
d.objects = Some(StateObjects {
states: vec![env_key("x"), env_key("y")],
locks: vec![(
format!("{}.tflock", user_key("y")),
lock_json("me"),
3 * 3600,
)],
});
let ok = owner_key(USER_ID);
let local_x = move |dep: Deployment| {
dep == Deployment::User {
env: "x",
owner_key: &ok,
}
};
let scan = run_scan(&d, &local_x);
assert_eq!(
find(&scan.environments, "x").onboarding,
Some(OnboardingState::InProgress)
);
assert_eq!(
find(&scan.environments, "y").onboarding,
Some(OnboardingState::Incomplete)
);
// Not joinable, not resolved.
let mut d = FakeDetect::new();
d.objects = Some(StateObjects {
states: vec![env_key("orphan")],
locks: Vec::new(),
});
let scan = run_scan(&d, &nobody_local);
assert_eq!(find(&scan.environments, "orphan").onboarding, None);
}
#[test]
fn denied_lookups_are_unknown_not_missing() {
// A workspace user: HeadBucket and GetPolicy are denied.
let mut d = FakeDetect::new().with_record("shared", 1);
d.bucket = Knowable::Unknown;
d.boundary = Knowable::Unknown;
d.roles.insert(task_role("shared"), Knowable::Unknown);
let scan = run_scan(&d, &nobody_local);
assert_eq!(scan.state_bucket, Knowable::Unknown);
assert_eq!(scan.boundary, Knowable::Unknown);
assert_eq!(scan.account, ACCOUNT);
assert_eq!(scan.region, REGION);
let shared = find(&scan.environments, "shared");
assert_eq!(shared.state, EnvironmentState::ExternallyManaged);
assert_eq!(shared.onboarding, Some(OnboardingState::NotJoined));
// An unknown bucket isn't listed.
assert!(
!d.calls().iter().any(|c| c.starts_with("objects ")),
"{:?}",
d.calls()
);
assert!(d
.calls()
.contains(&format!("bucket {}", state_bucket_name(ACCOUNT, REGION))));
// A missing bucket is No, and isn't listed either.
let mut d = FakeDetect::new().with_record("shared", 1);
d.bucket = Knowable::No;
d.boundary = Knowable::No;
let scan = run_scan(&d, &nobody_local);
assert_eq!(scan.state_bucket, Knowable::No);
assert_eq!(scan.boundary, Knowable::No);
// The SDK side: what each answer means.
assert_eq!(
lookup_outcome(Some("AccessDenied"), Some(403)),
Lookup::Denied
);
assert_eq!(lookup_outcome(None, Some(403)), Lookup::Denied);
assert_eq!(
lookup_outcome(Some("AccessDeniedException"), Some(400)),
Lookup::Denied
);
assert_eq!(lookup_outcome(Some("NotFound"), Some(404)), Lookup::Missing);
assert_eq!(lookup_outcome(None, Some(404)), Lookup::Missing);
assert_eq!(
lookup_outcome(Some("NoSuchEntity"), Some(404)),
Lookup::Missing
);
assert_eq!(
lookup_outcome(Some("NoSuchBucket"), Some(404)),
Lookup::Missing
);
assert_eq!(lookup_outcome(Some("Throttling"), Some(400)), Lookup::Error);
assert_eq!(lookup_outcome(None, None), Lookup::Error);
}
#[test]
fn records_under_unexpected_paths_are_skipped() {
let d = FakeDetect {
records: vec![
("/triple-c/environments/Bad Name".into(), "{}".into()),
("/triple-c/environments/a/b".into(), "{}".into()),
("/elsewhere/dev".into(), "{}".into()),
(
environment_parameter_name("ok"),
record("ok", 1).to_string(),
),
],
..FakeDetect::new()
};
let scan = run_scan(&d, &nobody_local);
let names: Vec<&str> = scan.environments.iter().map(|e| e.name.as_str()).collect();
assert_eq!(names, vec!["ok"]);
}
#[test]
fn only_buckets_owned_by_you_count_as_created() {
let bucket = state_bucket_name(ACCOUNT, REGION);
assert_eq!(
create_bucket_outcome(Some("BucketAlreadyOwnedByYou"), &bucket),
Some(Ok(()))
);
let taken = create_bucket_outcome(Some("BucketAlreadyExists"), &bucket);
assert_eq!(
taken,
Some(Err(AwsError::StateBucketTaken {
bucket: bucket.clone()
}))
);
assert!(taken.unwrap().unwrap_err().to_string().contains(&bucket));
// Anything else is classified as an ordinary SDK error.
assert_eq!(create_bucket_outcome(Some("AccessDenied"), &bucket), None);
assert_eq!(create_bucket_outcome(None, &bucket), None);
}
#[test]
fn the_state_bucket_is_configured_as_specified() {
// No LocationConstraint in us-east-1; the region everywhere else. The
// component tag either way.
let east = create_bucket_configuration("us-east-1");
assert!(east.location_constraint().is_none());
let west = create_bucket_configuration(REGION);
assert_eq!(west.location_constraint().map(|c| c.as_str()), Some(REGION));
for cfg in [&east, &west] {
let tags: Vec<(&str, &str)> = cfg.tags().iter().map(|t| (t.key(), t.value())).collect();
assert_eq!(tags, vec![("triple-c:component", "state")]);
}
let bucket = state_bucket_name(ACCOUNT, REGION);
let policy: Value = serde_json::from_str(&state_bucket_policy(&bucket)).unwrap();
let st = &policy["Statement"][0];
assert_eq!(st["Effect"], "Deny");
assert_eq!(st["Principal"], "*");
assert_eq!(st["Action"], "s3:*");
assert_eq!(
st["Resource"],
json!([
format!("arn:aws:s3:::{bucket}"),
format!("arn:aws:s3:::{bucket}/*")
])
);
assert_eq!(st["Condition"]["Bool"]["aws:SecureTransport"], "false");
}
#[test]
fn the_scan_serializes_for_the_frontend() {
let d = FakeDetect::new().with_record("dev", 1);
let scan = run_scan(&d, &nobody_local);
let v = serde_json::to_value(&scan).unwrap();
assert_eq!(v["state_bucket"], "yes");
assert_eq!(v["environments"][0]["state"]["state"], "externally_managed");
assert_eq!(v["environments"][0]["actions"], json!(["join"]));
assert_eq!(v["environments"][0]["onboarding"]["state"], "not_joined");
}
+10
View File
@@ -24,6 +24,11 @@ pub enum AwsError {
action: String,
message: String,
},
/// `s3:CreateBucket` said `BucketAlreadyExists`: the state bucket's
/// name belongs to another AWS account. Never treated as success.
StateBucketTaken {
bucket: String,
},
Other {
message: String,
},
@@ -53,6 +58,11 @@ impl std::fmt::Display for AwsError {
AwsError::AccessDenied { action, message } => {
write!(f, "Access denied for {action}: {message}")
}
AwsError::StateBucketTaken { bucket } => write!(
f,
"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),
}
}
+1
View File
@@ -4,6 +4,7 @@
//!
mod connection;
pub mod environments;
mod error;
mod identity;
#[cfg(test)]
+23 -14
View File
@@ -320,9 +320,29 @@ fn safe_segment(s: &str) -> bool {
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
}
/// The work folder of one deployment, `<root>/work/<bucket>/<state key
/// without .tfstate>/`, without creating or writing anything: what
/// [`materialize`] writes into and what [`is_running`] asks about.
#[allow(dead_code)] // used by phase-4 commands
pub fn workdir_for(root: &Path, deployment: Deployment, bucket: &str) -> Result<Workdir, AwsError> {
if !safe_segment(bucket) {
return Err(other("Invalid state bucket name"));
}
let key = state_key(deployment);
let stem = key
.strip_suffix(".tfstate")
.ok_or_else(|| other("Invalid state key"))?;
if !stem.split('/').all(safe_segment) {
return Err(other("Invalid deployment name"));
}
let mut dir = root.join("work").join(bucket);
dir.extend(stem.split('/'));
Ok(Workdir { dir })
}
/// Write `kind`'s module into `<root>/work/<bucket>/<state key without
/// .tfstate>/` with `backend.hcl` and `terraform.tfvars.json` (0600 on
/// Unix). Refuses while a run is in progress there. `.tf` files and the lock
/// .tfstate>/` ([`workdir_for`]) with `backend.hcl` and
/// `terraform.tfvars.json` (0600 on Unix). Refuses while a run is in progress there. `.tf` files and the lock
/// file left by an older module version are removed first, so a file a later
/// version dropped can't linger and still be loaded.
#[allow(dead_code)] // used by phase-4 commands
@@ -334,23 +354,12 @@ pub fn materialize(
region: &str,
vars: &Value,
) -> Result<Workdir, AwsError> {
if !safe_segment(bucket) {
return Err(other("Invalid state bucket name"));
}
let wd = workdir_for(root, deployment, bucket)?;
if region.trim() != region {
return Err(other("Invalid region"));
}
let region = validate_supported_region(region).map_err(other)?;
let key = state_key(deployment);
let stem = key
.strip_suffix(".tfstate")
.ok_or_else(|| other("Invalid state key"))?;
if !stem.split('/').all(safe_segment) {
return Err(other("Invalid deployment name"));
}
let mut dir = root.join("work").join(bucket);
dir.extend(stem.split('/'));
let wd = Workdir { dir };
if is_running(&wd) {
return Err(other("A deploy is already running for this deployment"));
}
+36
View File
@@ -406,6 +406,42 @@ fn materialize_refuses_names_that_leave_the_work_folder() {
.is_err());
}
#[test]
fn workdir_for_names_the_materialized_folder_without_writing() {
let tmp = tempfile::tempdir().unwrap();
let bucket = "triple-c-tfstate-123456789012-us-east-1";
let user = Deployment::User {
env: "dev",
owner_key: "0123456789ab",
};
let wd = workdir_for(tmp.path(), user, bucket).unwrap();
assert_eq!(
wd.dir,
tmp.path()
.join(format!("work/{bucket}/users/dev/0123456789ab/terraform"))
);
assert!(
!tmp.path().join("work").exists(),
"workdir_for wrote something"
);
assert!(!is_running(&wd));
let made = materialize(
tmp.path(),
ModuleKind::UserOnboard,
user,
bucket,
"us-east-1",
&serde_json::json!({}),
)
.unwrap();
assert_eq!(made, wd);
assert!(workdir_for(tmp.path(), Deployment::Environment(".."), bucket).is_err());
assert!(workdir_for(tmp.path(), Deployment::Boundary, "../x").is_err());
}
fn append(path: &std::path::Path, lines: &[String]) {
let mut f = std::fs::OpenOptions::new()
.create(true)
@@ -42,6 +42,8 @@ export function awsErrorMessage(e: AwsError): string {
return `Profile ${e.profile} was not found in your AWS config, or has no credentials.`;
case "keys_missing":
return "The access keys for this connection are missing from the keychain. Remove and re-add the connection.";
case "state_bucket_taken":
return `The state bucket name ${e.bucket} is already taken by another AWS account.`;
default:
return e.message;
}
+1
View File
@@ -7,6 +7,7 @@ const KINDS: Record<AwsError["kind"], true> = {
keys_missing: true,
invalid_credentials: true,
access_denied: true,
state_bucket_taken: true,
other: true,
};
+1
View File
@@ -1148,6 +1148,7 @@ export type AwsError =
| { kind: "keys_missing" }
| { kind: "invalid_credentials"; message: string }
| { kind: "access_denied"; action: string; message: string }
| { kind: "state_bucket_taken"; bucket: string }
| { kind: "other"; message: string };
export interface CallerIdentity {