Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,25 @@ jobs:
- name: Run parity script self-tests
run: bash scripts/test-azure-role-parity.sh

# Assert that the AWS IAM action lists are identical across the CFN stack,
# the TF lambda/fargate modules, and the federation CFN/TF/CLI templates.
# Fast (shell only), so it always runs.
aws-iam-parity:
name: AWS IAM actions parity (CFN vs TF)
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
with:
persist-credentials: false

- name: Assert CFN/TF actions parity
run: bash scripts/check-aws-iam-parity.sh

- name: Run parity script self-tests
run: bash scripts/test-aws-iam-parity.sh

# Summary job - all checks must pass
ci-success:
name: CI Success
Expand All @@ -493,6 +512,7 @@ jobs:
- security-scan
- e2e-tests
- azure-role-parity
- aws-iam-parity
if: always()

steps:
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
CloudFormation deploy scripts had no registration call at all).
Re-download the bundle from the CUDly UI and the new copy will register
your account automatically with no manual edits required.
- **Federation IaC bundles deployed before #1219 need to be re-applied** to
pick up reconciled IAM action grants. Older bundles silently degrade on
federated accounts: the Cost Explorer `Get*Coverage` actions are missing
(coverage-targeted sizing assumes zero existing coverage), and the
cross-account CloudFormation flavor lacks the optional `EnableOrgDiscovery`
parameter and the legacy CE statement that the original `CUDly-CrossAccount`
template carried. Re-download the federation bundle from the CUDly UI and
re-apply (`terraform apply` / `aws cloudformation update-stack`) -- no
manual edits required, no changes to existing CUDly resources. Customers
on the runtime CloudFormation stack or Terraform lambda/fargate modules
also need an update to pick up the new `ec2:*ReservedInstancesExchangeQuote`
actions, `ec2:DescribeRegions`, `rds:DescribeDBInstances`, and the
new-style `es:*ReservedInstance*` OpenSearch actions (replacing the legacy
`es:*ReservedElasticsearch*` names).

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions cloudformation/stacks/CUDly/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ Resources:
- ec2:DescribeReservedInstancesOfferings
- ec2:DescribeReservedInstances
- ec2:PurchaseReservedInstancesOffering
- ec2:GetReservedInstancesExchangeQuote
- ec2:AcceptReservedInstancesExchangeQuote
- ec2:DescribeRegions
- ec2:DescribeInstanceTypeOfferings
Resource: "*"
Expand Down Expand Up @@ -464,6 +466,7 @@ Resources:
Action:
- savingsplans:DescribeSavingsPlans
- savingsplans:CreateSavingsPlan
- savingsplans:DescribeSavingsPlansOfferings
- savingsplans:DescribeSavingsPlansOfferingRates
Resource: "*"

Expand All @@ -474,6 +477,7 @@ Resources:
- sts:GetCallerIdentity
- organizations:ListAccounts
- organizations:DescribeAccount
- organizations:DescribeOrganization
Resource: "*"

# Cross-account role assumption for multi-account plans
Expand Down
32 changes: 32 additions & 0 deletions iac/federation/aws-cross-account/cloudformation/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,24 @@ Parameters:
Description: Human-readable name for this account in CUDly.
Default: ""

EnableOrgDiscovery:
Type: String
Description: >
Grant organizations:ListAccounts and organizations:DescribeOrganization so
CUDly can enumerate all accounts in the AWS Organization through this role.
Set to "true" only when this role is deployed in an Organizations management
or delegated-administrator account. Leave "false" in member-account
deployments. Mirrors enable_org_discovery in the Terraform module.
Default: "false"
AllowedValues: ["true", "false"]

Conditions:
DoRegister: !And
- !Not [!Equals [!Ref CUDlyAPIURL, ""]]
- !Not [!Equals [!Ref ContactEmail, ""]]

OrgDiscoveryEnabled: !Equals [!Ref EnableOrgDiscovery, "true"]

Resources:
CUDlyPolicy:
Type: AWS::IAM::ManagedPolicy
Expand All @@ -52,6 +65,16 @@ Resources:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: CostExplorer
Effect: Allow
Action:
- ce:GetReservationPurchaseRecommendation
- ce:GetReservationUtilization
- ce:GetReservationCoverage
- ce:GetSavingsPlansPurchaseRecommendation
- ce:GetSavingsPlansUtilization
- ce:GetSavingsPlansCoverage
Resource: "*"
- Sid: EC2Reservations
Effect: Allow
Action:
Expand Down Expand Up @@ -106,6 +129,15 @@ Resources:
- es:DescribeReservedInstanceOfferings
- es:DescribeReservedInstances
Resource: "*"
- !If
- OrgDiscoveryEnabled
- Sid: OrganizationsDiscovery
Effect: Allow
Action:
- organizations:ListAccounts
- organizations:DescribeOrganization
Resource: "*"
- !Ref AWS::NoValue

CUDlyRole:
Type: AWS::IAM::Role
Expand Down
13 changes: 13 additions & 0 deletions iac/federation/aws-cross-account/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ resource "aws_iam_policy" "cudly" {
Version = "2012-10-17"
Statement = concat(
[
{
Sid = "CostExplorer"
Effect = "Allow"
Action = [
"ce:GetReservationPurchaseRecommendation",
"ce:GetReservationUtilization",
"ce:GetReservationCoverage",
"ce:GetSavingsPlansPurchaseRecommendation",
"ce:GetSavingsPlansUtilization",
"ce:GetSavingsPlansCoverage",
]
Resource = "*"
},
{
Sid = "EC2Reservations"
Effect = "Allow"
Expand Down
10 changes: 10 additions & 0 deletions iac/federation/aws-target/cloudformation/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ Resources:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: CostExplorer
Effect: Allow
Action:
- ce:GetReservationPurchaseRecommendation
- ce:GetReservationUtilization
- ce:GetReservationCoverage
- ce:GetSavingsPlansPurchaseRecommendation
- ce:GetSavingsPlansUtilization
- ce:GetSavingsPlansCoverage
Resource: "*"
- Sid: EC2Reservations
Effect: Allow
Action:
Expand Down
13 changes: 13 additions & 0 deletions iac/federation/aws-target/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ resource "aws_iam_policy" "cudly" {
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "CostExplorer"
Effect = "Allow"
Action = [
"ce:GetReservationPurchaseRecommendation",
"ce:GetReservationUtilization",
"ce:GetReservationCoverage",
"ce:GetSavingsPlansPurchaseRecommendation",
"ce:GetSavingsPlansUtilization",
"ce:GetSavingsPlansCoverage",
]
Resource = "*"
},
{
Sid = "EC2Reservations"
Effect = "Allow"
Expand Down
3 changes: 3 additions & 0 deletions internal/iacfiles/templates/aws-cross-account-cli.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ PERMISSIONS_POLICY=$(cat <<'JSON'
"Effect": "Allow",
"Resource": "*",
"Action": [
"ce:GetReservationPurchaseRecommendation","ce:GetReservationUtilization",
"ce:GetReservationCoverage","ce:GetSavingsPlansPurchaseRecommendation",
"ce:GetSavingsPlansUtilization","ce:GetSavingsPlansCoverage",
"ec2:PurchaseReservedInstancesOffering","ec2:DescribeReservedInstancesOfferings",
"ec2:DescribeReservedInstances","ec2:DescribeInstanceTypeOfferings",
"ec2:GetReservedInstancesExchangeQuote","ec2:AcceptReservedInstancesExchangeQuote",
Expand Down
3 changes: 3 additions & 0 deletions internal/iacfiles/templates/aws-wif-cli.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ PERMISSIONS_POLICY=$(cat <<'JSON'
"Effect": "Allow",
"Resource": "*",
"Action": [
"ce:GetReservationPurchaseRecommendation","ce:GetReservationUtilization",
"ce:GetReservationCoverage","ce:GetSavingsPlansPurchaseRecommendation",
"ce:GetSavingsPlansUtilization","ce:GetSavingsPlansCoverage",
"ec2:PurchaseReservedInstancesOffering","ec2:DescribeReservedInstancesOfferings",
"ec2:DescribeReservedInstances","ec2:DescribeInstanceTypeOfferings",
"ec2:GetReservedInstancesExchangeQuote","ec2:AcceptReservedInstancesExchangeQuote",
Expand Down
162 changes: 162 additions & 0 deletions scripts/check-aws-iam-parity.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/usr/bin/env bash
# check-aws-iam-parity.sh
#
# Asserts that the AWS IAM action lists granted to the CUDly runtime and to the
# customer-deployed federation roles are identical across every IaC flavor that
# encodes them, so CloudFormation and Terraform deployments of the same code
# cannot silently drift apart (the AWS sibling of check-azure-role-parity.sh).
#
# Three comparisons:
#
# 1. runtime : cloudformation/stacks/CUDly/template.yaml
# == terraform/modules/compute/aws/lambda/main.tf
# == terraform/modules/compute/aws/fargate/main.tf
# 2. federation cross-account pair (incl. the optional org-discovery
# statement, which exists in both as an opt-in):
# iac/federation/aws-cross-account/cloudformation/template.yaml
# == iac/federation/aws-cross-account/terraform/main.tf
# 3. federation core (org discovery excluded: the CLI quick-onboarding
# scripts and the aws-target WIF flavor intentionally do not offer it):
# both files from (2)
# == iac/federation/aws-target/cloudformation/template.yaml
# == iac/federation/aws-target/terraform/main.tf
# == internal/iacfiles/templates/aws-cross-account-cli.sh.tmpl
# == internal/iacfiles/templates/aws-wif-cli.sh.tmpl
#
# Only actions in the cloud-API namespaces the application code calls are
# compared (see ACTION_PREFIXES). Platform plumbing (logs, dynamodb, ses, sns,
# secretsmanager, sts, ssmmessages, lambda) legitimately differs per deployment
# flavor and is excluded.
#
# Exit 0 = all lists match.
# Exit 1 = drift found; the diff is printed to stderr.
# Exit 2 = usage / environment error.
#
# Usage:
# scripts/check-aws-iam-parity.sh [--root <path>]
#
# --root lets the test harness point at a fixture tree that mirrors the repo
# layout without touching the real sources.

set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

while [[ $# -gt 0 ]]; do
case "$1" in
--root)
if [[ $# -lt 2 || -z "${2:-}" ]]; then
echo "ERROR: --root requires a path value" >&2
exit 2
fi
REPO_ROOT="$2"
shift 2
;;
*) echo "Unknown flag: $1" >&2; exit 2 ;;
esac
Comment thread
coderabbitai[bot] marked this conversation as resolved.
done

# IAM action namespaces owned by the application code. Keep in sync with the
# services the providers/aws code actually calls.
ACTION_PREFIXES='ce|ec2|rds|elasticache|es|redshift|memorydb|savingsplans|organizations'

# --- extraction ---------------------------------------------------------------
# Pull every token shaped like <prefix>:<CamelCaseAction> out of a file,
# regardless of whether it is YAML, HCL, or an embedded JSON heredoc. IAM
# actions always start with an uppercase letter after the colon, which keeps
# ARNs (region segments are lowercase) out of the match.

extract_actions() {
local file="$1"
local exclude_orgs="${2:-}"

if [[ ! -f "$file" ]]; then
echo "ERROR: file not found: $file" >&2
exit 2
fi

local actions
actions=$(grep -oE "(^|[^A-Za-z])(${ACTION_PREFIXES}):[A-Z][A-Za-z]+" "$file" \
| sed 's/^[^a-zA-Z]//' \
| sort -u)

if [[ "$exclude_orgs" == "no-orgs" ]]; then
actions=$(printf '%s\n' "$actions" | grep -v '^organizations:' || true)
fi

if [[ -z "$actions" ]]; then
echo "ERROR: no IAM actions extracted from: $file" >&2
echo " Did the policy move or change format?" >&2
exit 2
fi

printf '%s\n' "$actions"
}

# --- comparison ---------------------------------------------------------------

FAILURES=0

compare_pair() {
local label="$1" ref_name="$2" ref_actions="$3" other_name="$4" other_actions="$5"

local diff_out
diff_out=$(diff <(printf '%s\n' "$ref_actions") <(printf '%s\n' "$other_actions") || true)

if [[ -n "$diff_out" ]]; then
{
echo "ERROR [$label]: IAM action drift between:"
echo " < $ref_name"
echo " > $other_name"
echo "$diff_out"
echo ""
} >&2
FAILURES=$((FAILURES + 1))
fi
}

# --- 1. runtime: CFN stack vs TF lambda vs TF fargate --------------------------

CFN_STACK="$REPO_ROOT/cloudformation/stacks/CUDly/template.yaml"
TF_LAMBDA="$REPO_ROOT/terraform/modules/compute/aws/lambda/main.tf"
TF_FARGATE="$REPO_ROOT/terraform/modules/compute/aws/fargate/main.tf"

cfn_stack_actions=$(extract_actions "$CFN_STACK")
tf_lambda_actions=$(extract_actions "$TF_LAMBDA")
tf_fargate_actions=$(extract_actions "$TF_FARGATE")

compare_pair "runtime" "$CFN_STACK" "$cfn_stack_actions" "$TF_LAMBDA" "$tf_lambda_actions"
compare_pair "runtime" "$TF_LAMBDA" "$tf_lambda_actions" "$TF_FARGATE" "$tf_fargate_actions"

# --- 2. federation cross-account pair (all namespaces) -------------------------

FED_XACC_CFN="$REPO_ROOT/iac/federation/aws-cross-account/cloudformation/template.yaml"
FED_XACC_TF="$REPO_ROOT/iac/federation/aws-cross-account/terraform/main.tf"

fed_cfn_actions=$(extract_actions "$FED_XACC_CFN")
fed_tf_actions=$(extract_actions "$FED_XACC_TF")

compare_pair "federation cross-account" "$FED_XACC_CFN" "$fed_cfn_actions" "$FED_XACC_TF" "$fed_tf_actions"

# --- 3. federation core across all flavors (org discovery excluded) ------------

FED_WIF_CFN="$REPO_ROOT/iac/federation/aws-target/cloudformation/template.yaml"
FED_WIF_TF="$REPO_ROOT/iac/federation/aws-target/terraform/main.tf"
FED_XACC_CLI="$REPO_ROOT/internal/iacfiles/templates/aws-cross-account-cli.sh.tmpl"
FED_WIF_CLI="$REPO_ROOT/internal/iacfiles/templates/aws-wif-cli.sh.tmpl"

fed_core_ref=$(extract_actions "$FED_XACC_CFN" no-orgs)

for f in "$FED_XACC_TF" "$FED_WIF_CFN" "$FED_WIF_TF" "$FED_XACC_CLI" "$FED_WIF_CLI"; do
compare_pair "federation core" "$FED_XACC_CFN" "$fed_core_ref" "$f" "$(extract_actions "$f" no-orgs)"
done

# --- result --------------------------------------------------------------------

if [[ "$FAILURES" -gt 0 ]]; then
echo "FAILED: $FAILURES IAM parity comparison(s) drifted. Update the lagging file(s) so all lists match." >&2
exit 1
fi

echo "OK: AWS IAM action lists are in parity across CloudFormation, Terraform, and CLI onboarding templates."
exit 0
Loading
Loading