diff --git a/app/src-tauri/src/aws/permissions.rs b/app/src-tauri/src/aws/permissions.rs index cf51d1b..8d4038c 100644 --- a/app/src-tauri/src/aws/permissions.rs +++ b/app/src-tauri/src/aws/permissions.rs @@ -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 @@ -175,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 diff --git a/app/src-tauri/src/aws/permissions/tests.rs b/app/src-tauri/src/aws/permissions/tests.rs index ca2217e..497b268 100644 --- a/app/src-tauri/src/aws/permissions/tests.rs +++ b/app/src-tauri/src/aws/permissions/tests.rs @@ -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] diff --git a/app/src-tauri/src/aws/policies.rs b/app/src-tauri/src/aws/policies.rs index 37465e7..e0120a8 100644 --- a/app/src-tauri/src/aws/policies.rs +++ b/app/src-tauri/src/aws/policies.rs @@ -242,13 +242,57 @@ const FROM_LAUNCH_TEMPLATE: Context = &[( "ec2:LaunchTemplate", Literal("arn:aws:ec2:{region}:{account}:launch-template/lt-00000000000000000"), )]; -/// ...and the image is Amazon's. +const PROBE_LAUNCH_TEMPLATE_ARN: ContextValue = + Literal("arn:aws:ec2:{region}:{account}:launch-template/lt-00000000000000000"); +/// The instance and its volume carry the component tag, and the instance +/// is of a supported type. +const INSTANCE_LAUNCH: Context = &[ + ( + "aws:RequestTag/triple-c:component", + Literal("user-onboarding"), + ), + ("ec2:LaunchTemplate", PROBE_LAUNCH_TEMPLATE_ARN), + ("ec2:InstanceType", Literal("m7i.large")), +]; +const VOLUME_LAUNCH: Context = &[ + ( + "aws:RequestTag/triple-c:component", + Literal("user-onboarding"), + ), + ("ec2:LaunchTemplate", PROBE_LAUNCH_TEMPLATE_ARN), +]; +/// The environment's (component-tagged) tasks group, named by the template. +const SECURITY_GROUP_LAUNCH: Context = &[ + ("ec2:LaunchTemplate", PROBE_LAUNCH_TEMPLATE_ARN), + ("ec2:IsLaunchTemplateResource", Literal("true")), + (COMPONENT_RESOURCE_TAG, Literal("environment")), +]; +/// Amazon's image, named by the template. const AMAZON_IMAGE_FROM_LAUNCH_TEMPLATE: Context = &[ ("ec2:Owner", Literal("amazon")), + ("ec2:LaunchTemplate", PROBE_LAUNCH_TEMPLATE_ARN), + ("ec2:IsLaunchTemplateResource", Literal("true")), +]; +/// A group created with the component tag, its template pinned to a +/// version, of a supported type, with no load balancer. +const GROUP_CREATE: Context = &[ ( - "ec2:LaunchTemplate", - Literal("arn:aws:ec2:{region}:{account}:launch-template/lt-00000000000000000"), + "aws:RequestTag/triple-c:component", + Literal("user-onboarding"), ), + ( + "autoscaling:LaunchTemplateVersionSpecified", + Literal("true"), + ), + ("autoscaling:InstanceTypes", Literal("m7i.large")), +]; +const GROUP_UPDATE: Context = &[ + (COMPONENT_RESOURCE_TAG, Literal("user-onboarding")), + ( + "autoscaling:LaunchTemplateVersionSpecified", + Literal("true"), + ), + ("autoscaling:InstanceTypes", Literal("m7i.large")), ]; const PASSED_TO_EC2: Context = &[("iam:PassedToService", Literal("ec2.amazonaws.com"))]; @@ -867,11 +911,6 @@ const ENVIRONMENT_ADMIN_CHECKS: &[Check] = &[ PROBE_LAUNCH_TEMPLATE, COMPONENT_TAGGED, ), - write( - "ec2:ModifyLaunchTemplate", - PROBE_LAUNCH_TEMPLATE, - COMPONENT_TAGGED, - ), write( "ec2:DeleteLaunchTemplate", PROBE_LAUNCH_TEMPLATE, @@ -883,8 +922,8 @@ const ENVIRONMENT_ADMIN_CHECKS: &[Check] = &[ // its template: RunInstances, only from a Triple-C launch template and // only Amazon's images, and the tags the launch puts on. write("ec2:RunInstances", PROBE_LAUNCH_TEMPLATE, COMPONENT_TAGGED), - write("ec2:RunInstances", PROBE_INSTANCE, FROM_LAUNCH_TEMPLATE), - write("ec2:RunInstances", PROBE_VOLUME, FROM_LAUNCH_TEMPLATE), + write("ec2:RunInstances", PROBE_INSTANCE, INSTANCE_LAUNCH), + write("ec2:RunInstances", PROBE_VOLUME, VOLUME_LAUNCH), write( "ec2:RunInstances", PROBE_NETWORK_INTERFACE, @@ -894,7 +933,7 @@ const ENVIRONMENT_ADMIN_CHECKS: &[Check] = &[ write( "ec2:RunInstances", PROBE_SECURITY_GROUP, - FROM_LAUNCH_TEMPLATE, + SECURITY_GROUP_LAUNCH, ), write( "ec2:RunInstances", @@ -916,17 +955,13 @@ const ENVIRONMENT_ADMIN_CHECKS: &[Check] = &[ PROBE_NETWORK_INTERFACE, &[("ec2:CreateAction", Literal("RunInstances"))], ), - // Auto Scaling groups: `triple-c-*`, created with the component tag, - // changed and deleted only when they carry it. + // Auto Scaling groups: `triple-c-*`, created with the component tag + // (and pinned to a template version, of a supported type, with no load + // balancer), changed, tagged and deleted only when they carry it. write( "autoscaling:CreateAutoScalingGroup", PROBE_AUTO_SCALING_GROUP, - COMPONENT_REQUESTED, - ), - write( - "autoscaling:CreateOrUpdateTags", - PROBE_AUTO_SCALING_GROUP, - COMPONENT_REQUESTED, + GROUP_CREATE, ), write( "autoscaling:CreateOrUpdateTags", @@ -936,7 +971,7 @@ const ENVIRONMENT_ADMIN_CHECKS: &[Check] = &[ write( "autoscaling:UpdateAutoScalingGroup", PROBE_AUTO_SCALING_GROUP, - COMPONENT_TAGGED, + GROUP_UPDATE, ), write( "autoscaling:SetInstanceProtection", @@ -1912,9 +1947,6 @@ mod tests { "ecs:SubmitAttachmentStateChanges", "ecs:UpdateContainerInstancesState", "ecs:TagResource", - // Session Manager on the instance. - "ssm:UpdateInstanceInformation", - "ec2messages:GetMessages", ] { assert!( granted.iter().any(|a| glob(a, need)), @@ -1965,11 +1997,19 @@ mod tests { } } - /// The instance role's ECS agent (E4) reaches only Triple-C clusters and - /// their container instances, and tags only the instance it registers. + /// The instance role's ECS agent (E4) reaches only its own cluster — + /// the one its role's `triple-c:cluster` tag names, which must be a + /// `triple-c-*` cluster — and that cluster's container instances, and + /// tags only the instance it registers. A user who is root on their own + /// instance (Docker access) holds that role's credentials, so another + /// user's cluster must be out of its reach: otherwise it could register + /// into it and have the other user's tasks placed on its instance. #[test] - fn the_boundary_keeps_the_ecs_agent_on_triple_c_clusters() { + fn the_boundary_keeps_the_ecs_agent_on_its_own_cluster() { let stmts = statements_of(&boundary()); + const TAG: &str = "aws:PrincipalTag/triple-c:cluster"; + const MINE: Context = &[(TAG, Literal("triple-c-probe-0123456789ab"))]; + const PROD: Context = &[(TAG, Literal("prod"))]; let within = |action: &'static str, resource: &'static str, context: Context| { evaluate_in( &stmts, @@ -1980,41 +2020,81 @@ mod tests { }, ) }; + const CLUSTER: &str = "arn:aws:ecs:{region}:{account}:cluster/triple-c-probe-0123456789ab"; const INSTANCE: &str = "arn:aws:ecs:{region}:{account}:container-instance/\ triple-c-probe-0123456789ab/0123456789abcdef0123456789abcdef"; - const OTHER_CLUSTER: &str = "arn:aws:ecs:{region}:{account}:cluster/prod"; - const OTHER_INSTANCE: &str = "arn:aws:ecs:{region}:{account}:container-instance/\ + const OTHER_USERS_CLUSTER: &str = + "arn:aws:ecs:{region}:{account}:cluster/triple-c-probe-ba9876543210"; + const OTHER_USERS_INSTANCE: &str = "arn:aws:ecs:{region}:{account}:container-instance/\ + triple-c-probe-ba9876543210/0123456789abcdef0123456789abcdef"; + const PROD_CLUSTER: &str = "arn:aws:ecs:{region}:{account}:cluster/prod"; + const PROD_INSTANCE: &str = "arn:aws:ecs:{region}:{account}:container-instance/\ prod/0123456789abcdef0123456789abcdef"; - const AT_REGISTER: Context = &[("ecs:CreateAction", Literal("RegisterContainerInstance"))]; - for action in [ - "ecs:RegisterContainerInstance", - "ecs:DeregisterContainerInstance", - "ecs:SubmitTaskStateChange", + let at_register = |tag: Context| -> Context { + let mut v = tag.to_vec(); + v.push(("ecs:CreateAction", Literal("RegisterContainerInstance"))); + Box::leak(v.into_boxed_slice()) + }; + for (action, own, other, prod) in [ + ( + "ecs:RegisterContainerInstance", + CLUSTER, + OTHER_USERS_CLUSTER, + PROD_CLUSTER, + ), + ( + "ecs:DeregisterContainerInstance", + CLUSTER, + OTHER_USERS_CLUSTER, + PROD_CLUSTER, + ), + ( + "ecs:SubmitTaskStateChange", + CLUSTER, + OTHER_USERS_CLUSTER, + PROD_CLUSTER, + ), + ("ecs:Poll", INSTANCE, OTHER_USERS_INSTANCE, PROD_INSTANCE), + ( + "ecs:StartTelemetrySession", + INSTANCE, + OTHER_USERS_INSTANCE, + PROD_INSTANCE, + ), + ( + "ecs:UpdateContainerInstancesState", + INSTANCE, + OTHER_USERS_INSTANCE, + PROD_INSTANCE, + ), ] { - assert!(within(action, PROBE_CLUSTER, &[]), "{action}"); - assert!(!within(action, OTHER_CLUSTER, &[]), "{action} elsewhere"); + assert!(within(action, own, MINE), "{action}"); + assert!( + !within(action, other, MINE), + "{action} on another user's cluster" + ); + assert!(!within(action, own, &[]), "{action} without the role tag"); + // A role tagged with a non-Triple-C cluster gets nothing. + assert!( + !within(action, prod, PROD), + "{action} on a non-Triple-C cluster" + ); } - for action in [ - "ecs:Poll", - "ecs:StartTelemetrySession", - "ecs:UpdateContainerInstancesState", - ] { - assert!(within(action, INSTANCE, &[]), "{action}"); - assert!(!within(action, OTHER_INSTANCE, &[]), "{action} elsewhere"); - } - assert!(within("ecs:TagResource", INSTANCE, AT_REGISTER)); - assert!(!within("ecs:TagResource", OTHER_INSTANCE, AT_REGISTER)); + assert!(within("ecs:TagResource", INSTANCE, at_register(MINE))); for (resource, context) in [ - (INSTANCE, &[][..]), - (PROBE_CLUSTER, AT_REGISTER), - (PROBE_TASK, &[][..]), + (OTHER_USERS_INSTANCE, at_register(MINE)), + (PROD_INSTANCE, at_register(PROD)), + (INSTANCE, MINE), + (CLUSTER, at_register(MINE)), + (PROBE_TASK, at_register(MINE)), ] { assert!( !within("ecs:TagResource", resource, context), "ecs:TagResource on {resource} with {context:?}" ); } - // The agent never runs, stops or changes tasks or clusters itself. + // The agent never runs, stops or changes tasks or clusters itself, + // and has no Session Manager channel of its own. for action in [ "ecs:RunTask", "ecs:StartTask", @@ -2023,8 +2103,11 @@ mod tests { "ecs:DeleteCluster", "ecs:ExecuteCommand", ] { - assert!(!within(action, PROBE_CLUSTER, &[]), "{action}"); - assert!(!within(action, PROBE_TASK, &[]), "{action}"); + assert!(!within(action, CLUSTER, MINE), "{action}"); + assert!(!within(action, PROBE_TASK, MINE), "{action}"); + } + for action in ["ssm:UpdateInstanceInformation", "ec2messages:GetMessages"] { + assert!(!within(action, "*", MINE), "{action}"); } } @@ -2457,27 +2540,63 @@ mod tests { #[test] fn per_user_compute_is_tag_and_name_scoped() { let tier = Tier::EnvironmentAdmin; - for (action, resource) in [ - ("ec2:CreateLaunchTemplate", PROBE_LAUNCH_TEMPLATE), - ( + assert!(allowed( + tier, + &write( + "ec2:CreateLaunchTemplate", + PROBE_LAUNCH_TEMPLATE, + COMPONENT_REQUESTED + ), + ME, + ME + )); + assert!(!allowed( + tier, + &write("ec2:CreateLaunchTemplate", PROBE_LAUNCH_TEMPLATE, &[]), + ME, + ME + )); + let untagged_group = without(&GROUP_CREATE, "aws:RequestTag/triple-c:component"); + assert!(!allowed( + tier, + &write( "autoscaling:CreateAutoScalingGroup", PROBE_AUTO_SCALING_GROUP, + untagged_group ), + ME, + ME + )); + // Tags go on a group only once it is Triple-C's: no request-tag-only + // tagging of an existing (untagged) `triple-c-*` group. + assert!(!allowed( + tier, + &write( + "autoscaling:CreateOrUpdateTags", + PROBE_AUTO_SCALING_GROUP, + COMPONENT_REQUESTED + ), + ME, + ME + )); + // The default version never moves: a group on `$Default` would + // launch whatever it points at. + for action in [ + "ec2:ModifyLaunchTemplate", + "ec2:DeleteLaunchTemplateVersions", ] { - assert!(allowed( - tier, - &write(action, resource, COMPONENT_REQUESTED), - ME, - ME - )); assert!( - !allowed(tier, &write(action, resource, &[]), ME, ME), - "untagged {action}" + !allowed( + tier, + &write(action, PROBE_LAUNCH_TEMPLATE, COMPONENT_TAGGED), + ME, + ME + ), + "{action}" ); } for (action, resource) in [ ("ec2:CreateLaunchTemplateVersion", PROBE_LAUNCH_TEMPLATE), - ("ec2:ModifyLaunchTemplate", PROBE_LAUNCH_TEMPLATE), ("ec2:DeleteLaunchTemplate", PROBE_LAUNCH_TEMPLATE), ("ec2:CreateTags", PROBE_LAUNCH_TEMPLATE), ("ec2:DeleteTags", PROBE_LAUNCH_TEMPLATE), @@ -2567,19 +2686,238 @@ mod tests { )); } + /// `context` without `key`. + fn without(context: Context, key: &str) -> Context { + Box::leak( + context + .iter() + .filter(|(k, _)| *k != key) + .copied() + .collect::>() + .into_boxed_slice(), + ) + } + + /// `context` with `key` set to `value` (replacing any value it had). + fn with(context: Context, key: &'static str, value: &'static str) -> Context { + let mut v = without(context, key).to_vec(); + v.push((key, Literal(value))); + Box::leak(v.into_boxed_slice()) + } + + /// The instance types a list in the compute file allows, for each list. + fn policy_instance_type_lists() -> Vec> { + let mut lists = Vec::new(); + for s in allows(Tier::EnvironmentAdmin) { + if let Some(ops) = s["Condition"].as_object() { + for entries in ops.values() { + for key in ["ec2:InstanceType", "autoscaling:InstanceTypes"] { + if let Some(v) = entries.get(key) { + lists.push(strings(v).into_iter().collect()); + } + } + } + } + } + lists + } + + /// The policy allows exactly the instance types the app knows how to + /// size (`ec2_instance_spec`): no GPU, no metal, nothing above 4xlarge. + #[test] + fn supported_instance_types_are_the_apps_table() { + let mut known = BTreeSet::new(); + for family in [ + "m7i", "m7g", "c7i", "c7g", "r7i", "r7g", "t3", "t4g", "m7a", "p5", "g6", + ] { + for size in [ + "medium", + "large", + "xlarge", + "2xlarge", + "4xlarge", + "8xlarge", + "48xlarge", + "metal-24xl", + ] { + let t = format!("{family}.{size}"); + if crate::models::project::ec2_instance_spec(&t).is_some() { + known.insert(t); + } + } + } + assert_eq!(known.len(), 30); + let lists = policy_instance_type_lists(); + assert_eq!(lists.len(), 3, "RunInstances, group create, group update"); + for list in lists { + assert_eq!(list, known); + } + } + + /// Creating or updating a group: pinned to a template version (a group + /// on `$Latest`/`$Default` would launch any later version, whose + /// instance profile was never checked), no load balancers, supported + /// types only. + #[test] + fn groups_are_pinned_supported_and_unbalanced() { + let tier = Tier::EnvironmentAdmin; + for (action, ok) in [ + ("autoscaling:CreateAutoScalingGroup", GROUP_CREATE), + ("autoscaling:UpdateAutoScalingGroup", GROUP_UPDATE), + ] { + let attempt = |context| { + allowed( + tier, + &write(action, PROBE_AUTO_SCALING_GROUP, context), + ME, + ME, + ) + }; + assert!(attempt(ok), "{action}"); + let version = "autoscaling:LaunchTemplateVersionSpecified"; + assert!(!attempt(with(ok, version, "false")), "{action} on $Latest"); + assert!( + !attempt(with( + ok, + "autoscaling:TargetGroupARNs", + "arn:aws:elasticloadbalancing:x" + )), + "{action} with a target group" + ); + assert!( + !attempt(with(ok, "autoscaling:LoadBalancerNames", "web")), + "{action} with a load balancer" + ); + for t in ["p5.48xlarge", "m7i.8xlarge", "m7i.metal-24xl", "g6.xlarge"] { + assert!( + !attempt(with(ok, "autoscaling:InstanceTypes", t)), + "{action} with {t}" + ); + } + } + // Creating always names the template version; an update may leave + // the template alone (a size change, the drain before destroy). + assert!(!allowed( + tier, + &write( + "autoscaling:CreateAutoScalingGroup", + PROBE_AUTO_SCALING_GROUP, + without(GROUP_CREATE, "autoscaling:LaunchTemplateVersionSpecified") + ), + ME, + ME + )); + assert!(allowed( + tier, + &write( + "autoscaling:UpdateAutoScalingGroup", + PROBE_AUTO_SCALING_GROUP, + COMPONENT_TAGGED + ), + ME, + ME + )); + } + /// RunInstances (which creating an Auto Scaling group checks the caller - /// for) works only from a Triple-C launch template, with Amazon's - /// images: never a bare launch, nor someone else's template or image. + /// for) works only from a Triple-C launch template: the instance and + /// its volume tagged and of a supported type, the environment's own + /// security group and Amazon's image, both named by the template. Never + /// a bare launch, nor someone else's template, group or image. #[test] fn instances_launch_only_from_triple_c_launch_templates() { let tier = Tier::EnvironmentAdmin; - for resource in [ - PROBE_INSTANCE, - PROBE_VOLUME, - PROBE_NETWORK_INTERFACE, - PROBE_SUBNET, - PROBE_SECURITY_GROUP, + for (resource, ok) in [ + (PROBE_INSTANCE, INSTANCE_LAUNCH), + (PROBE_VOLUME, VOLUME_LAUNCH), + (PROBE_INSTANCE, without(INSTANCE_LAUNCH, "ec2:InstanceType")), ] { + assert!(allowed( + tier, + &write("ec2:RunInstances", resource, ok), + ME, + ME + )); + assert!( + !allowed( + tier, + &write( + "ec2:RunInstances", + resource, + without(ok, "aws:RequestTag/triple-c:component") + ), + ME, + ME + ), + "untagged {resource}" + ); + } + for t in [ + "p5.48xlarge", + "m7i.8xlarge", + "m7i.metal-24xl", + "inf2.xlarge", + ] { + assert!( + !allowed( + tier, + &write( + "ec2:RunInstances", + PROBE_INSTANCE, + with(INSTANCE_LAUNCH, "ec2:InstanceType", t) + ), + ME, + ME + ), + "{t}" + ); + } + for (resource, ok) in [ + (PROBE_SECURITY_GROUP, SECURITY_GROUP_LAUNCH), + (PROBE_IMAGE, AMAZON_IMAGE_FROM_LAUNCH_TEMPLATE), + ] { + assert!(allowed( + tier, + &write("ec2:RunInstances", resource, ok), + ME, + ME + )); + // A request override (not the template's) is refused. + assert!(!allowed( + tier, + &write( + "ec2:RunInstances", + resource, + with(ok, "ec2:IsLaunchTemplateResource", "false") + ), + ME, + ME + )); + assert!(!allowed( + tier, + &write( + "ec2:RunInstances", + resource, + without(ok, "ec2:IsLaunchTemplateResource") + ), + ME, + ME + )); + } + assert!( + !allowed( + tier, + &write( + "ec2:RunInstances", + PROBE_SECURITY_GROUP, + without(SECURITY_GROUP_LAUNCH, COMPONENT_RESOURCE_TAG) + ), + ME, + ME + ), + "someone else's security group" + ); + for resource in [PROBE_NETWORK_INTERFACE, PROBE_SUBNET] { assert!(allowed( tier, &write("ec2:RunInstances", resource, FROM_LAUNCH_TEMPLATE), diff --git a/app/src-tauri/src/aws/policy_eval.rs b/app/src-tauri/src/aws/policy_eval.rs index fa3ab92..a804c1c 100644 --- a/app/src-tauri/src/aws/policy_eval.rs +++ b/app/src-tauri/src/aws/policy_eval.rs @@ -208,7 +208,8 @@ 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), @@ -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!({ diff --git a/app/src-tauri/src/aws/tofu_modules/tests.rs b/app/src-tauri/src/aws/tofu_modules/tests.rs index 575f6a3..b1a9a31 100644 --- a/app/src-tauri/src/aws/tofu_modules/tests.rs +++ b/app/src-tauri/src/aws/tofu_modules/tests.rs @@ -1892,7 +1892,7 @@ const MODULE_CALLS: &[(&str, &[Check])] = &[ ("backend.s3", BACKEND), ]; -// Phase 7 (per-user EC2 capacity, E1–E5, E11): the calls the onboarding +// Phase 7 (per-user EC2 capacity, E1–E5, E11; fix round 1): the calls the onboarding // module's EC2 resources will make, written down with the policy (Task 2) // before the resources exist. Task 3 moves each entry into MODULE_CALLS // under its type (merging with the existing `aws_ecs_cluster`, @@ -1925,12 +1925,46 @@ const FROM_TEMPLATE: Context = &[( "ec2:LaunchTemplate", Literal("arn:aws:ec2:{region}:{account}:launch-template/lt-00000000000000000"), )]; +const LT_ARN: ContextValue = + Literal("arn:aws:ec2:{region}:{account}:launch-template/lt-00000000000000000"); +/// The template's tag specifications tag the instance and its volume. +const TAGGED_INSTANCE: Context = &[ + (REQUEST_COMPONENT, Literal("user-onboarding")), + ("ec2:LaunchTemplate", LT_ARN), + ("ec2:InstanceType", Literal("m7i.large")), +]; +const TAGGED_VOLUME: Context = &[ + (REQUEST_COMPONENT, Literal("user-onboarding")), + ("ec2:LaunchTemplate", LT_ARN), +]; +/// The environment's tasks group, named in the template. +const TEMPLATE_GROUP: Context = &[ + ("ec2:LaunchTemplate", LT_ARN), + ("ec2:IsLaunchTemplateResource", Literal("true")), + (RESOURCE_COMPONENT, Literal("environment")), +]; const AMAZON_IMAGE: Context = &[ ("ec2:Owner", Literal("amazon")), + ("ec2:LaunchTemplate", LT_ARN), + ("ec2:IsLaunchTemplateResource", Literal("true")), +]; +/// The group pins the template to a version (`latest_version`, never +/// `$Latest`/`$Default`) and has no load balancer. +const GROUP_CREATE: Context = &[ + (REQUEST_COMPONENT, Literal("user-onboarding")), ( - "ec2:LaunchTemplate", - Literal("arn:aws:ec2:{region}:{account}:launch-template/lt-00000000000000000"), + "autoscaling:LaunchTemplateVersionSpecified", + Literal("true"), ), + ("autoscaling:InstanceTypes", Literal("m7i.large")), +]; +const GROUP_UPDATE: Context = &[ + (RESOURCE_COMPONENT, Literal("user-onboarding")), + ( + "autoscaling:LaunchTemplateVersionSpecified", + Literal("true"), + ), + ("autoscaling:InstanceTypes", Literal("m7i.large")), ]; const TO_EC2: Context = &[("iam:PassedToService", Literal("ec2.amazonaws.com"))]; @@ -2003,7 +2037,6 @@ const EC2_COMPUTE_CALLS: &[(&str, &[Check])] = &[ LAUNCH_TEMPLATE, USER_TAGGED, ), - call("ec2:ModifyLaunchTemplate", LAUNCH_TEMPLATE, USER_TAGGED), call("ec2:CreateTags", LAUNCH_TEMPLATE, USER_TAGGED), call("ec2:DeleteTags", LAUNCH_TEMPLATE, USER_TAGGED), call("ec2:DeleteLaunchTemplate", LAUNCH_TEMPLATE, USER_TAGGED), @@ -2021,12 +2054,7 @@ const EC2_COMPUTE_CALLS: &[(&str, &[Check])] = &[ call( "autoscaling:CreateAutoScalingGroup", AUTO_SCALING_GROUP, - USER_CREATE, - ), - call( - "autoscaling:CreateOrUpdateTags", - AUTO_SCALING_GROUP, - USER_CREATE, + GROUP_CREATE, ), call( "iam:CreateServiceLinkedRole", @@ -2034,11 +2062,11 @@ const EC2_COMPUTE_CALLS: &[(&str, &[Check])] = &[ &[("iam:AWSServiceName", Literal("autoscaling.amazonaws.com"))], ), call("ec2:RunInstances", LAUNCH_TEMPLATE, USER_TAGGED), - call("ec2:RunInstances", INSTANCE, FROM_TEMPLATE), - call("ec2:RunInstances", VOLUME, FROM_TEMPLATE), + call("ec2:RunInstances", INSTANCE, TAGGED_INSTANCE), + call("ec2:RunInstances", VOLUME, TAGGED_VOLUME), call("ec2:RunInstances", ENI, FROM_TEMPLATE), call("ec2:RunInstances", SUBNET, FROM_TEMPLATE), - call("ec2:RunInstances", SG, FROM_TEMPLATE), + call("ec2:RunInstances", SG, TEMPLATE_GROUP), call("ec2:RunInstances", IMAGE, AMAZON_IMAGE), call("ec2:CreateTags", INSTANCE, created_by!("RunInstances")), call("ec2:CreateTags", VOLUME, created_by!("RunInstances")), @@ -2046,6 +2074,12 @@ const EC2_COMPUTE_CALLS: &[(&str, &[Check])] = &[ call("iam:PassRole", INSTANCE_ROLE, TO_EC2), call("autoscaling:DescribeAutoScalingGroups", "*", NONE), call("autoscaling:DescribeScalingActivities", "*", NONE), + call( + "autoscaling:UpdateAutoScalingGroup", + AUTO_SCALING_GROUP, + GROUP_UPDATE, + ), + // The drain before destroy leaves the template alone. call( "autoscaling:UpdateAutoScalingGroup", AUTO_SCALING_GROUP, @@ -2073,6 +2107,9 @@ const EC2_COMPUTE_CALLS: &[(&str, &[Check])] = &[ "aws_iam_role (instance)", &[ call("iam:CreateRole", INSTANCE_ROLE, BOUNDED), + // `triple-c:cluster = `: the boundary keeps + // the ECS agent on that cluster. + call("iam:TagRole", INSTANCE_ROLE, NONE), call("iam:PutRolePolicy", INSTANCE_ROLE, BOUNDED), call("iam:GetRole", INSTANCE_ROLE, NONE), call("iam:ListInstanceProfilesForRole", INSTANCE_ROLE, NONE), diff --git a/aws/README.md b/aws/README.md index 80d2c8b..7b5d4d7 100644 --- a/aws/README.md +++ b/aws/README.md @@ -147,9 +147,16 @@ and nothing runs with more than the admin holds. It covers: `/triple-c/users//`); - per-user EC2 capacity (`environment-admin-compute.json`, phase 7): the user's own `triple-c--` cluster and capacity provider, a component-tagged launch template - and `triple-c-*` Auto Scaling group, the `triple-c-*` instance profile, passing - `triple-c-*-instance` roles to EC2, reading the ECS-optimized AMI's public SSM parameter, and - the Auto Scaling and ECS service-linked roles. + and `triple-c-*` Auto Scaling group (pinned to a template version, no load balancer, supported + instance types only), the `triple-c-*` instance profile, passing `triple-c-*-instance` roles to + EC2, reading the ECS-optimized AMI's public SSM parameter, and the Auto Scaling and ECS + service-linked roles. + +**Supported instance types.** Launches and groups are limited to the types the app sizes +(`ec2_instance_spec` in `models/project.rs`, pinned to the policy by a test): `m7i`, `m7g`, `c7i`, +`c7g`, `r7i`, `r7g` in `large` to `4xlarge`, and `t3`, `t4g` in `large` to `2xlarge`. No GPU, +accelerator or metal types. An onboarding with any other type is denied at apply; supporting one +means adding it to that table and to the three lists in the compute file together. **Clearing workspace task definitions.** Removing a project leaves its revisions registered (family `triple-c---`). The admin tier may deregister any @@ -165,8 +172,10 @@ Deregistered revisions become INACTIVE; ECS keeps them until they are deleted wi A task still running from a revision keeps running. Not in the policy: ECR (tasks pull from GHCR). Phase 7's EC2 capacity added custom ECS capacity -providers and passing roles to EC2, the latter only for `triple-c-*-instance` roles (task and -execution roles still go to `ecs-tasks.amazonaws.com` only). +providers and passing roles to EC2. The `triple-c-*-instance` name on that PassRole keeps a +user's task and execution roles from being handed to an instance by mistake; it is **not** a +fence against the admin, who can create a bounded role under any `triple-c-*` name. What fences +an instance role is the permissions boundary, as for every Triple-C role. ### What each module resource needs @@ -204,9 +213,9 @@ the proof; an action it shows missing is added to the table, the policy, the che | `aws_ecs_cluster` (the user's) | as the environment's cluster, on `cluster/triple-c--` | | `aws_ecs_capacity_provider` | `ecs:CreateCapacityProvider`, `TagResource` (at create), `ListTagsForResource`, `UpdateCapacityProvider`, `UntagResource`, `DeleteCapacityProvider` (`capacity-provider/triple-c-*`), `DescribeCapacityProviders` (`*`); `iam:CreateServiceLinkedRole` for `ecs.amazonaws.com` (managed scaling runs as ECS's service-linked role) | | `aws_ecs_cluster_capacity_providers` (the user's) | `ecs:PutClusterCapacityProviders` (the cluster and, expected, the capacity provider) | -| `aws_launch_template` | `ec2:CreateLaunchTemplate` (request tag), `CreateTags` (at create), `CreateLaunchTemplateVersion`, `ModifyLaunchTemplate`, `CreateTags`/`DeleteTags`, `DeleteLaunchTemplate` (tagged), `Describe*` | -| `aws_autoscaling_group` | `autoscaling:CreateAutoScalingGroup`, `CreateOrUpdateTags` (`triple-c-*`, request tag), `UpdateAutoScalingGroup`, `SetInstanceProtection`, `CreateOrUpdateTags`/`DeleteTags`, `DeleteAutoScalingGroup` (tagged), `Describe*` (`*`); `iam:CreateServiceLinkedRole` for `autoscaling.amazonaws.com`. Creating a group checks that the caller could launch its template: `ec2:RunInstances` on the tagged launch template, on instance, volume, network interface, subnet and security group only with `ec2:LaunchTemplate` set, and on images only Amazon's (`ec2:Owner = amazon`); `ec2:CreateTags` at `RunInstances`; `iam:PassRole` of the instance role to `ec2.amazonaws.com`. ASG tags are **not** the provider's `default_tags`: the module must set the component tag on the group itself | -| `aws_iam_role` (instance), `aws_iam_role_policy` | as the other per-user roles (bounded), on `triple-c---instance` | +| `aws_launch_template` | `ec2:CreateLaunchTemplate` (request tag), `CreateTags` (at create), `CreateLaunchTemplateVersion`, `CreateTags`/`DeleteTags`, `DeleteLaunchTemplate` (tagged), `Describe*`. Never `ModifyLaunchTemplate`: the default version never moves (the module must not set `update_default_version` or `default_version`) | +| `aws_autoscaling_group` | `autoscaling:CreateAutoScalingGroup` (`triple-c-*`, request tag, `autoscaling:LaunchTemplateVersionSpecified = true`, no `TargetGroupARNs`/`LoadBalancerNames`, `autoscaling:InstanceTypes` supported), `UpdateAutoScalingGroup` (tagged; the same, except that an update which leaves the template alone passes the version check), `SetInstanceProtection`, `CreateOrUpdateTags`/`DeleteTags`, `DeleteAutoScalingGroup` (tagged), `Describe*` (`*`); `iam:CreateServiceLinkedRole` for `autoscaling.amazonaws.com`. Creating a group checks that the caller could launch its template: `ec2:RunInstances` on the tagged launch template; on the instance and its volume only with the component request tag, `ec2:LaunchTemplate` set and (instance) a supported `ec2:InstanceType`; on the network interface and subnet with `ec2:LaunchTemplate`; on the security group only the template's own (`ec2:IsLaunchTemplateResource`) component-tagged group; on images only Amazon's, named by the template; `ec2:CreateTags` at `RunInstances`; `iam:PassRole` of the instance role to `ec2.amazonaws.com`. ASG tags are **not** the provider's `default_tags`: the module must set the component tag on the group itself | +| `aws_iam_role` (instance), `aws_iam_role_policy` | as the other per-user roles (bounded), on `triple-c---instance`, tagged `triple-c:cluster = triple-c--` (`iam:TagRole`) | | `aws_iam_instance_profile` | `iam:CreateInstanceProfile`, `TagInstanceProfile`, `AddRoleToInstanceProfile` (expected to need `iam:PassRole` of the role too), `GetInstanceProfile`, `ListInstanceProfileTags`, `UntagInstanceProfile`, `RemoveRoleFromInstanceProfile`, `DeleteInstanceProfile` (`instance-profile/triple-c-*`) | | `data.aws_ssm_parameter` (AMI) | `ssm:GetParameter`/`GetParameters` on the public `parameter/aws/service/ecs/optimized-ami/*` | | `aws_ssm_parameter` (user record) | as the environment record, on `/triple-c/users//` | @@ -242,11 +251,17 @@ the role carries the `triple-c-permissions-boundary` managed policy as its permi - invoking Bedrock models; - for a user's EC2 instance role (phase 7, E4): the ECS agent's calls (`RegisterContainerInstance`, `DeregisterContainerInstance`, `Poll`, `StartTelemetrySession`, - `Submit*`, `UpdateContainerInstancesState`) only on `triple-c-*` clusters and their container - instances, `ecs:TagResource` only on such a container instance as it registers - (`ecs:CreateAction = RegisterContainerInstance`), `ecs:DiscoverPollEndpoint`, and Session - Manager on the instance (`ssm:UpdateInstanceInformation`, the six `ec2messages:` actions) for an - admin's troubleshooting. ECS Exec into an EC2 task uses the task role, as on Fargate. + `Submit*`, `UpdateContainerInstancesState`) only on **the role's own cluster**, the one its + `triple-c:cluster` tag names (`cluster/${aws:PrincipalTag/triple-c:cluster}` and that + cluster's container instances; the tag must match `triple-c-*`), `ecs:TagResource` only on + such a container instance as it registers (`ecs:CreateAction = RegisterContainerInstance`), + and `ecs:DiscoverPollEndpoint`. A user with Docker access is root on their own instance and + so holds its role's credentials; the tag keeps them from registering that instance into + another user's cluster (and so having that user's tasks placed on it). An untagged instance + role can do nothing with ECS. The admin sets the tag when it creates the role (the admin + tier already tags `triple-c-*` roles freely, and a workspace user cannot tag roles at all). + **No Session Manager on the instance** (plan E4 amended in fix round 1): nobody is granted + host sessions, and ECS Exec into an EC2 task uses the task role, as on Fargate. - The boundary grants nothing in IAM. - Explicit Deny statements stop the admin from: - removing the boundary from a `triple-c-*` role; @@ -264,9 +279,13 @@ created only *inside* a Triple-C (tagged) VPC, subnet and address, and an intern attached, a route added or a route table associated only on tagged ones, so the admin cannot re-route a VPC that isn't Triple-C's. Tagging is allowed only as part of the creating call (`ec2:CreateAction`, `elasticfilesystem:CreateAction`), or on a resource that is already -Triple-C's. Launch templates (phase 7) are created only with the component tag and changed, -launched from or deleted only when they carry it; Auto Scaling groups must be named `triple-c-*` -and also carry it (request tag to create, resource tag to change, protect or delete). Resources +Triple-C's. Launch templates (phase 7) are created only with the component tag and versioned, +launched from or deleted only when they carry it; their default version never changes. Auto +Scaling groups must be named `triple-c-*` and also carry it (request tag to create; resource tag +to change, protect, tag or delete, so an existing untagged `triple-c-*` group can't be claimed by +tagging it). A group must name its template's version explicitly: a group on `$Latest` or +`$Default` would launch any later version, whose instance profile nobody checked, as Auto +Scaling's service-linked role. Resources that cannot carry a tag are authorized through one that does: routes and route-table associations through the route table (and subnet), mount targets and the file-system policy through the file system, KMS aliases through the key, role policies through the @@ -305,12 +324,14 @@ account-wide: and mount targets are created and deleted under this statement alone. - Reads on `*`: `ec2:Describe*`, `elasticfilesystem:Describe*`, the KMS key reads and `ListAliases`, `logs:DescribeLogGroups` and `ssm:DescribeParameters`. -- `ec2:RunInstances` on instances, volumes, network interfaces, subnets and security groups - anywhere, as long as the launch comes from a launch template (`ec2:LaunchTemplate`), whose own - side must be a component-tagged template, and with Amazon's images only. The subnets and groups - are not tag-checked because an `existing`-network environment's are the user's own. The admin - never calls `RunInstances` itself: creating an Auto Scaling group checks that the caller could, - and the group's launches run as Auto Scaling's service-linked role. +- `ec2:RunInstances`, directly as well as through a group: only from a component-tagged launch + template, the instance and its volume carrying the component tag, a supported instance type, + Amazon's image and the environment's (tagged) security group, both as the template names them + (`ec2:IsLaunchTemplateResource`), and only a `triple-c-*-instance` role passed. The network + interface and **subnet are not tag-checked**: the group supplies the subnets (not the template), + and an `existing`-network environment's subnets are the user's own, untagged. The modules never + call `RunInstances` themselves: creating a group checks that the caller could, and the group's + launches run as Auto Scaling's service-linked role. - `iam:CreateServiceLinkedRole`, only for `autoscaling.amazonaws.com` and `ecs.amazonaws.com` (AWS fixes those roles' policies). - Reads on `*`: `autoscaling:Describe*`, `ecs:DescribeCapacityProviders`; and the public @@ -437,8 +458,8 @@ aws iam create-policy --policy-name triple-c-permissions-boundary \ --policy-document file://policies/permissions-boundary.json ``` -**Phase 7 changed the boundary** (version 2: the instance role's ECS agent and Session Manager -actions). An account administrator re-applies it once (the boundary module from the app, or +**Phase 7 changed the boundary** (version 2: the instance role's ECS agent, on the cluster its +`triple-c:cluster` tag names). An account administrator re-applies it once (the boundary module from the app, or `aws iam create-policy-version --policy-arn arn:aws:iam:::policy/triple-c-permissions-boundary --policy-document file://policies/permissions-boundary.json --set-as-default`) before anyone onboards with EC2 capacity; Fargate users are unaffected. The module tags the policy @@ -458,7 +479,7 @@ environment admin needs every tier file (about 20,200), and a user's share 2,048 may hold 6,144 characters, not counting whitespace. Measured 2026-11-16 without whitespace, `workspace-user.json` is 6,036 (108 characters of headroom), `workspace-user-tasks.json` 1,293 (including `ReadFargatePrices`), `environment-admin.json` 5,805 (339), `environment-admin-network.json` 3,340, -`environment-admin-compute.json` 3,705 and `permissions-boundary.json` 2,625; a unit test (`every_policy_fits_a_managed_policy`) +`environment-admin-compute.json` 6,020 (124) and `permissions-boundary.json` 2,651; a unit test (`every_policy_fits_a_managed_policy`) fails before any file outgrows the limit. A tier that outgrows its files gains another file, which the app then lists with the others. For IAM Identity Center, add the files as inline policies on the permission set (the "up to 32,768 characters" limit is unverified) or reference the managed policies. @@ -531,6 +552,30 @@ expected, not verified: `container-instance//` and the rest against the cluster; the boundary allows both. - **The public AMI parameter's ARN** has no account (`arn:aws:ssm:::parameter/aws/…`); `arn:aws:ssm:*::parameter/aws/service/ecs/optimized-ami/*` is expected to match it. +- **Version pinning.** `autoscaling:LaunchTemplateVersionSpecified` is expected to be `true` for a + numeric version and `false` for `$Latest`/`$Default`, and absent on an update that names no + template (allowed by `BoolIfExists`, so the drain before destroy works). Probe: create a group + with `$Latest` as the admin; it must be denied. +- **`ec2:IsLaunchTemplateResource`** is expected to be `true` on the image and security group the + template names, and `aws:RequestTag` to carry the template's tag specifications for the instance + and volume. If the group's launch check doesn't send either, that statement denies the create. +- **`autoscaling:InstanceTypes`** is expected to be absent (vacuously allowed) unless a request + names instance types (a mixed-instances override); the instance type itself is enforced at + `RunInstances`. +- **Tags at group create.** If `CreateAutoScalingGroup` with tags also needs + `autoscaling:CreateOrUpdateTags`, it is denied (that is allowed only on an already-tagged + group); then add it to the create statement with the same conditions. +- **A `triple-c-*` capacity provider pointing at someone else's group.** Whether + `CreateCapacityProvider` checks the caller's rights on the named Auto Scaling group is unknown; + if it does not, an admin could put a non-Triple-C group under a Triple-C capacity provider (and + ECS's managed scaling would then scale it). Probe: create one naming an untagged group. +- **`aws:PrincipalTag` for the instance role**: the role's session is expected to carry its role + tags, so `${aws:PrincipalTag/triple-c:cluster}` resolves for the agent's calls. +- **IMDS and Docker access.** Tasks use `awsvpc`, with `ECS_AWSVPC_BLOCK_IMDS=true` and a hop + limit of 1 so a task can't read the instance role. A project with Docker access mounts the + host's socket, so its user can start a host-network container and read IMDS (and so the + instance role): accepted, since Docker access is root on the user's own instance; the boundary + keeps that role on the user's own cluster. **Phase 6** diff --git a/aws/policies/environment-admin-compute.json b/aws/policies/environment-admin-compute.json index 0f31763..1adfa35 100644 --- a/aws/policies/environment-admin-compute.json +++ b/aws/policies/environment-admin-compute.json @@ -58,7 +58,6 @@ "Effect": "Allow", "Action": [ "ec2:CreateLaunchTemplateVersion", - "ec2:ModifyLaunchTemplate", "ec2:DeleteLaunchTemplate", "ec2:CreateTags", "ec2:DeleteTags", @@ -74,21 +73,92 @@ } }, { - "Sid": "LaunchOnlyFromLaunchTemplates", + "Sid": "LaunchSupportedInstances", "Effect": "Allow", "Action": [ "ec2:RunInstances" ], "Resource": [ "arn:aws:ec2:*:*:instance/*", - "arn:aws:ec2:*:*:volume/*", + "arn:aws:ec2:*:*:volume/*" + ], + "Condition": { + "Null": { + "aws:RequestTag/triple-c:component": "false" + }, + "ArnLike": { + "ec2:LaunchTemplate": "arn:aws:ec2:*:*:launch-template/*" + }, + "StringEqualsIfExists": { + "ec2:InstanceType": [ + "m7i.large", + "m7i.xlarge", + "m7i.2xlarge", + "m7i.4xlarge", + "m7g.large", + "m7g.xlarge", + "m7g.2xlarge", + "m7g.4xlarge", + "c7i.large", + "c7i.xlarge", + "c7i.2xlarge", + "c7i.4xlarge", + "c7g.large", + "c7g.xlarge", + "c7g.2xlarge", + "c7g.4xlarge", + "r7i.large", + "r7i.xlarge", + "r7i.2xlarge", + "r7i.4xlarge", + "r7g.large", + "r7g.xlarge", + "r7g.2xlarge", + "r7g.4xlarge", + "t3.large", + "t3.xlarge", + "t3.2xlarge", + "t4g.large", + "t4g.xlarge", + "t4g.2xlarge" + ] + } + } + }, + { + "Sid": "LaunchIntoSubnets", + "Effect": "Allow", + "Action": [ + "ec2:RunInstances" + ], + "Resource": [ "arn:aws:ec2:*:*:network-interface/*", - "arn:aws:ec2:*:*:subnet/*", + "arn:aws:ec2:*:*:subnet/*" + ], + "Condition": { + "ArnLike": { + "ec2:LaunchTemplate": "arn:aws:ec2:*:*:launch-template/*" + } + } + }, + { + "Sid": "LaunchWithTripleCGroups", + "Effect": "Allow", + "Action": [ + "ec2:RunInstances" + ], + "Resource": [ "arn:aws:ec2:*:*:security-group/*" ], "Condition": { "ArnLike": { "ec2:LaunchTemplate": "arn:aws:ec2:*:*:launch-template/*" + }, + "Bool": { + "ec2:IsLaunchTemplateResource": "true" + }, + "Null": { + "aws:ResourceTag/triple-c:component": "false" } } }, @@ -107,22 +177,117 @@ }, "ArnLike": { "ec2:LaunchTemplate": "arn:aws:ec2:*:*:launch-template/*" + }, + "Bool": { + "ec2:IsLaunchTemplateResource": "true" } } }, { - "Sid": "CreateTaggedAutoScalingGroups", + "Sid": "CreatePinnedGroups", "Effect": "Allow", "Action": [ - "autoscaling:CreateAutoScalingGroup", - "autoscaling:CreateOrUpdateTags" + "autoscaling:CreateAutoScalingGroup" ], "Resource": [ "arn:aws:autoscaling:*:*:autoScalingGroup:*:autoScalingGroupName/triple-c-*" ], "Condition": { "Null": { - "aws:RequestTag/triple-c:component": "false" + "aws:RequestTag/triple-c:component": "false", + "autoscaling:TargetGroupARNs": "true", + "autoscaling:LoadBalancerNames": "true" + }, + "Bool": { + "autoscaling:LaunchTemplateVersionSpecified": "true" + }, + "ForAllValues:StringEquals": { + "autoscaling:InstanceTypes": [ + "m7i.large", + "m7i.xlarge", + "m7i.2xlarge", + "m7i.4xlarge", + "m7g.large", + "m7g.xlarge", + "m7g.2xlarge", + "m7g.4xlarge", + "c7i.large", + "c7i.xlarge", + "c7i.2xlarge", + "c7i.4xlarge", + "c7g.large", + "c7g.xlarge", + "c7g.2xlarge", + "c7g.4xlarge", + "r7i.large", + "r7i.xlarge", + "r7i.2xlarge", + "r7i.4xlarge", + "r7g.large", + "r7g.xlarge", + "r7g.2xlarge", + "r7g.4xlarge", + "t3.large", + "t3.xlarge", + "t3.2xlarge", + "t4g.large", + "t4g.xlarge", + "t4g.2xlarge" + ] + } + } + }, + { + "Sid": "UpdatePinnedGroups", + "Effect": "Allow", + "Action": [ + "autoscaling:UpdateAutoScalingGroup" + ], + "Resource": [ + "arn:aws:autoscaling:*:*:autoScalingGroup:*:autoScalingGroupName/triple-c-*" + ], + "Condition": { + "Null": { + "aws:ResourceTag/triple-c:component": "false", + "autoscaling:TargetGroupARNs": "true", + "autoscaling:LoadBalancerNames": "true" + }, + "BoolIfExists": { + "autoscaling:LaunchTemplateVersionSpecified": "true" + }, + "ForAllValues:StringEquals": { + "autoscaling:InstanceTypes": [ + "m7i.large", + "m7i.xlarge", + "m7i.2xlarge", + "m7i.4xlarge", + "m7g.large", + "m7g.xlarge", + "m7g.2xlarge", + "m7g.4xlarge", + "c7i.large", + "c7i.xlarge", + "c7i.2xlarge", + "c7i.4xlarge", + "c7g.large", + "c7g.xlarge", + "c7g.2xlarge", + "c7g.4xlarge", + "r7i.large", + "r7i.xlarge", + "r7i.2xlarge", + "r7i.4xlarge", + "r7g.large", + "r7g.xlarge", + "r7g.2xlarge", + "r7g.4xlarge", + "t3.large", + "t3.xlarge", + "t3.2xlarge", + "t4g.large", + "t4g.xlarge", + "t4g.2xlarge" + ] } } }, @@ -130,7 +295,6 @@ "Sid": "ManageTripleCAutoScalingGroups", "Effect": "Allow", "Action": [ - "autoscaling:UpdateAutoScalingGroup", "autoscaling:SetInstanceProtection", "autoscaling:CreateOrUpdateTags", "autoscaling:DeleteTags", diff --git a/aws/policies/permissions-boundary.json b/aws/policies/permissions-boundary.json index de69b2c..c198a0f 100644 --- a/aws/policies/permissions-boundary.json +++ b/aws/policies/permissions-boundary.json @@ -118,7 +118,7 @@ ] }, { - "Sid": "EcsAgentOnTripleCClusters", + "Sid": "EcsAgentOnItsOwnCluster", "Effect": "Allow", "Action": [ "ecs:RegisterContainerInstance", @@ -129,9 +129,14 @@ "ecs:UpdateContainerInstancesState" ], "Resource": [ - "arn:aws:ecs:*:*:cluster/triple-c-*", - "arn:aws:ecs:*:*:container-instance/triple-c-*/*" - ] + "arn:aws:ecs:*:*:cluster/${aws:PrincipalTag/triple-c:cluster}", + "arn:aws:ecs:*:*:container-instance/${aws:PrincipalTag/triple-c:cluster}/*" + ], + "Condition": { + "StringLike": { + "aws:PrincipalTag/triple-c:cluster": "triple-c-*" + } + } }, { "Sid": "EcsAgentTagsItsInstance", @@ -140,26 +145,22 @@ "ecs:TagResource" ], "Resource": [ - "arn:aws:ecs:*:*:container-instance/triple-c-*/*" + "arn:aws:ecs:*:*:container-instance/${aws:PrincipalTag/triple-c:cluster}/*" ], "Condition": { "StringEquals": { "ecs:CreateAction": "RegisterContainerInstance" + }, + "StringLike": { + "aws:PrincipalTag/triple-c:cluster": "triple-c-*" } } }, { - "Sid": "InstanceAgents", + "Sid": "EcsAgentDiscovery", "Effect": "Allow", "Action": [ - "ecs:DiscoverPollEndpoint", - "ssm:UpdateInstanceInformation", - "ec2messages:AcknowledgeMessage", - "ec2messages:DeleteMessage", - "ec2messages:FailMessage", - "ec2messages:GetEndpoint", - "ec2messages:GetMessages", - "ec2messages:SendReply" + "ecs:DiscoverPollEndpoint" ], "Resource": [ "*"