From 6dd107f24edd80966defc0f1f4d1ec41431d483f Mon Sep 17 00:00:00 2001 From: "stackguardian-qa[bot]" <84170665+stackguardian-qa[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:35:17 +0000 Subject: [PATCH] sgcode-agent: generate Azure Subscription Policy Baseline (1 resource) --- .sg/DOCUMENTATION.md | 138 ++++++++++++++++++ .terraform.lock.hcl | 20 +++ environments/sg.tfvars | 9 ++ imports.sh | 3 + main.tf | 152 ++------------------ modules/policy_assignment/main.tf | 7 + modules/policy_assignment/outputs.tf | 3 + modules/policy_assignment/variables.tf | 21 +++ outputs.tf | 9 -- providers.tf | 4 + variables.tf | 188 +------------------------ versions.tf | 8 ++ 12 files changed, 228 insertions(+), 334 deletions(-) create mode 100644 .sg/DOCUMENTATION.md create mode 100644 .terraform.lock.hcl create mode 100644 environments/sg.tfvars create mode 100644 imports.sh create mode 100644 modules/policy_assignment/main.tf create mode 100644 modules/policy_assignment/outputs.tf create mode 100644 modules/policy_assignment/variables.tf create mode 100644 providers.tf create mode 100644 versions.tf diff --git a/.sg/DOCUMENTATION.md b/.sg/DOCUMENTATION.md new file mode 100644 index 0000000..ce5a790 --- /dev/null +++ b/.sg/DOCUMENTATION.md @@ -0,0 +1,138 @@ +# Documentation: test-terraform-cli + +## 1. Overview + +This stack manages one Azure resource: a **subscription-scoped Azure Policy assignment** (`SecurityCenterBuiltIn`), the default Microsoft Defender for Cloud (ASC Default) policy set on subscription `618c8e34-b87b-44e8-bb3a-b2de95df60ed`. + +The workflow was: + +1. Code was generated from discovered cloud resources (`azurerm_policy_assignment`, ID `SecurityCenterBuiltIn`). +2. The existing resource was imported into Terraform state via `imports.sh`. +3. The configuration was reconciled until `plan` reported no changes. + +Final reconciliation result: **No changes. Your infrastructure matches the configuration.** + +## 2. Resources + +| Terraform address | Provider type | Real-world name / ID | Purpose | +|---|---|---|---| +| `module.policy_assignment["asc_default"].azurerm_policy_assignment.this` | `azurerm_policy_assignment` | Name `SecurityCenterBuiltIn`; ID `/subscriptions/618c8e34-b87b-44e8-bb3a-b2de95df60ed/providers/Microsoft.Authorization/policyAssignments/SecurityCenterBuiltIn` | Assigns the Azure built-in policy set `1f3afdf9-d0c9-4c3d-847f-89da613e70a8` (ASC Default, audit policies) to the subscription. | + +Only one resource exists in the root configuration. The `policy_assignment` module is instantiated once, with `for_each` key `asc_default`. + +## 3. Module structure + +### Root (`./`) + +- `main.tf`: one `module "policy_assignment"` block using `for_each = var.policy_assignments` and passing `name`, `scope`, `policy_definition_id`, `description`, `display_name` from each map entry. +- `variables.tf`: declares `policy_assignments` (a `map(object)`, default `{}`). +- `outputs.tf`: present but empty. No outputs are exported. +- `versions.tf`: requires provider `hashicorp/azurerm` `~> 2.99`. +- `providers.tf`: configures `azurerm` with `skip_provider_registration = true` and an empty `features {}` block. + +### `modules/policy_assignment/` + +- `main.tf`: a single `azurerm_policy_assignment` resource named `this`, with arguments taken directly from the module variables. +- `variables.tf`: `name`, `scope`, `policy_definition_id` (required strings); `description`, `display_name` (optional strings, default `null`). +- `outputs.tf`: output `id` = `azurerm_policy_assignment.this.id`. + +### External modules + +No external (`git::`) modules are called from the root configuration. The discovery metadata lists a `test-terraform-cli` module (`git::https://github.com/StackGuardian/test-terraform-cli.git?ref=main`) that covers `null_resource`. The root code does not reference it, and no `null_resource` exists in this stack. It is not used and is documented here only for completeness. + +## 4. How import works + +`imports.sh` runs one import command: + +```sh +"$1" import -var-file environments/sg.tfvars \ + 'module.policy_assignment["asc_default"].azurerm_policy_assignment.this' \ + '/subscriptions/618c8e34-b87b-44e8-bb3a-b2de95df60ed/providers/Microsoft.Authorization/policyAssignments/SecurityCenterBuiltIn' +``` + +- `$1` is the IaC binary path (for example, `tofu`). The script takes it as an argument. +- The first argument after `import` is the module-qualified Terraform address. The second is the Azure ARM resource ID. Use the full ARM ID, not the short name. +- The script was run once to populate state. It does not need to run again while state is intact. + +**To re-import the resource if state is lost** (after `init`, with the same variables in place): + +```sh +tofu import -var-file=environments/sg.tfvars \ + 'module.policy_assignment["asc_default"].azurerm_policy_assignment.this' \ + '/subscriptions/618c8e34-b87b-44e8-bb3a-b2de95df60ed/providers/Microsoft.Authorization/policyAssignments/SecurityCenterBuiltIn' +``` + +Then run `plan` to confirm no changes. + +## 5. How to use the code + +Run these commands from the working directory. Replace `tofu` with your binary path if needed. + +```sh +# 1. Initialise providers and modules +tofu init + +# 2. Preview changes using the shipped variable values +tofu plan -var-file=environments/sg.tfvars + +# 3. Apply changes +tofu apply -var-file=environments/sg.tfvars +``` + +### Targeting another environment (for example, dev → prod) + +No `.tf` edits are required: + +1. Copy `environments/sg.tfvars` to a new file, for example `environments/prod.tfvars`. +2. Edit the values in the copy. The key fields are `policy_assignments..name`, `scope` (subscription ID), `policy_definition_id`, `description`, and `display_name`. The map key (for example, `asc_default`) becomes the `for_each` key in the state address. +3. Plan against the new file: + + ```sh + tofu plan -var-file=environments/prod.tfvars + ``` + +4. Note that the import in `imports.sh` targets the `asc_default` address and the `sg` subscription ID. For a different environment, the existing state is not reused. Import the target resource with the matching address and ARM ID before planning, or the plan will propose creating a new assignment. + +## 6. Variables + +| Variable | Type | Controls | Sensitive? | +|---|---|---|---| +| `policy_assignments` | `map(object({ name, scope, policy_definition_id, description?, display_name? }))` | The set of policy assignments to create. Each map key becomes a `for_each` instance (`module.policy_assignment[""]`). | No | + +Fields of each entry: + +- `name`: the policy assignment name in Azure (`SecurityCenterBuiltIn`). +- `scope`: the ARM scope to assign to (`/subscriptions/618c8e34-…`). +- `policy_definition_id`: the policy or policy set definition ID (`/providers/Microsoft.Authorization/policySetDefinitions/1f3afdf9-…`). +- `description`: optional text. Set in `sg.tfvars`. +- `display_name`: optional label. Set in `sg.tfvars`. + +**Secrets:** no variable in this configuration is marked `sensitive`. The shipped `environments/sg.tfvars` contains no secret values, and no `secrets.auto.tfvars` is required. If you add a sensitive variable later, provide its value in a `secrets.auto.tfvars` file or with `-var` before planning. Do not commit that file. + +## 7. Infrastructure graph + +``` +subscription 618c8e34-b87b-44e8-bb3a-b2de95df60ed (scope, not managed by this stack) +└── policySetDefinitions/1f3afdf9-… (policy_definition_id, referenced, not managed) + ▲ + │ referenced by + │ +module.policy_assignment["asc_default"] (module.policy_assignment, for_each key "asc_default") +└── azurerm_policy_assignment.this (name "SecurityCenterBuiltIn", scope = subscription) +``` + +There are no dependencies between managed resources. The only reference is that the assignment points at the subscription scope and at the policy set definition. Both of these are external to the stack. + +## 8. Notable decisions & caveats + +- **Provider version pinned to `~> 2.99`.** The `azurerm_policy_assignment` resource was removed in azurerm 3.0 and later. Do not upgrade the provider without rewriting this resource. +- **`skip_provider_registration = true`** is set in `providers.tf`. Automatic resource-provider registration returned HTTP 403 during discovery, so it is disabled. +- **Import ID format.** The import uses the full ARM resource ID, not the short name `SecurityCenterBuiltIn`. +- **Module wrapping.** The single resource is wrapped in `modules/policy_assignment` and called with `for_each` over `var.policy_assignments`. This keeps the stack extensible to more assignments, at the cost of longer state addresses. +- **Empty root outputs.** `outputs.tf` is empty. The module's `id` output is not surfaced at the root. +- **No `lifecycle { ignore_changes }` blocks.** Every attribute is managed. Any attribute changed out-of-band will show up as drift in `plan`. +- **Omitted computed attributes.** Only configurable arguments are declared. Read-only attributes (for example, the assignment `identity` and `location` when not set) are not modelled. Azure may return values for them that are not reflected in code. +- **Descriptive text is copied from Azure.** The `description` value in `sg.tfvars` is the default ASC text as returned by Azure, including the URL. Editing it in code causes an in-place update. +- **Local state file present.** `terraform.tfstate` is in the working directory. Treat it as sensitive and keep it out of version control unless you use a remote backend. +- **Stale discovery metadata.** The module index in the discovery data lists an unrelated `null_resource` module. It is not used by this stack (see section 3). +- **Current drift:** none. The final reconciliation reported no changes. diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..0143476 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,20 @@ +# This file is maintained automatically by "tofu init". +# Manual edits may be lost in future updates. + +provider "registry.opentofu.org/hashicorp/azurerm" { + version = "2.99.0" + constraints = "~> 2.99" + hashes = [ + "h1:DXxpZ+z+5rAeMMVznlsqoUShm+hMOso4SmaPaBKRBw0=", + "zh:0080eddc51a962378290c63e68322246db051fb665d7ac661923bd0f9e1921f3", + "zh:07e97d6314fb57df5aa01b5b27001b66b52112ea87b5baec86700fdb52a15d93", + "zh:40247cb2ea313a28390e2537527f4d021512afba7729a43be60e1d489e6f2e88", + "zh:53b44ddc730d0690e1c5a348da948d259b62cecbabf2aa52383b79c12c61d133", + "zh:675d52b17771112a93f6a0932b4044a94ba2eba8b7a933e377670c467be5f46c", + "zh:8316ecaa287005bb00b5f444196030f59e1a103436b0891da801b6dd1e0cd6eb", + "zh:9e5e0cdc04a56602df374a22323c74e09acfa417a73f0aab01e1efbb75d42073", + "zh:c89de310886924955f926d7115f0bd28e4e9851a502c4b0abdfb5295432a59b4", + "zh:cc2310ad6cf6f83a18f424938c42f831898e33b96bb2269eb8a0cac8b8a06b3c", + "zh:d917637ef4d4095615793019cf1339ffa7486555677f3a5e81d435320e146fd3", + ] +} diff --git a/environments/sg.tfvars b/environments/sg.tfvars new file mode 100644 index 0000000..e0d7a9c --- /dev/null +++ b/environments/sg.tfvars @@ -0,0 +1,9 @@ +policy_assignments = { + asc_default = { + name = "SecurityCenterBuiltIn" + scope = "/subscriptions/618c8e34-b87b-44e8-bb3a-b2de95df60ed" + policy_definition_id = "/providers/Microsoft.Authorization/policySetDefinitions/1f3afdf9-d0c9-4c3d-847f-89da613e70a8" + description = "This is the default set of policies monitored by Azure Security Center. It was automatically assigned as part of onboarding to Security Center. The default assignment contains only audit policies. For more information please visit https://aka.ms/ascpolicies" + display_name = "ASC Default (subscription: 618c8e34-b87b-44e8-bb3a-b2de95df60ed)" + } +} diff --git a/imports.sh b/imports.sh new file mode 100644 index 0000000..e0dd955 --- /dev/null +++ b/imports.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -e +"$1" import -var-file environments/sg.tfvars 'module.policy_assignment["asc_default"].azurerm_policy_assignment.this' '/subscriptions/618c8e34-b87b-44e8-bb3a-b2de95df60ed/providers/Microsoft.Authorization/policyAssignments/SecurityCenterBuiltIn' diff --git a/main.tf b/main.tf index 04fd429..7885253 100644 --- a/main.tf +++ b/main.tf @@ -1,144 +1,10 @@ -module "stackguardian_workflow_group" { - for_each = toset(var.workflow_groups) - source = "./stackguardian_workflow_group" - workflow_group_name = each.value -} - -locals { - aws_provider_region = try([ - for connector in values(var.cloud_connectors) : coalesce(connector.aws_region, "eu-central-1") - if contains(["AWS_STATIC", "AWS_RBAC", "AWS_OIDC"], connector.kind) - ][0], "eu-central-1") - has_gcp_connector = anytrue([for connector in values(var.cloud_connectors) : connector.kind == "GCP_OIDC"]) -} - -# Terraform 1.5 cannot express cross-variable validation. This resource makes -# collection-wide violations hard plan failures rather than advisory checks. -resource "terraform_data" "validate_onboarding" { - input = { - cloud_connector_keys = keys(var.cloud_connectors) - vcs_connector_keys = nonsensitive(keys(var.vcs_connectors)) - } - - lifecycle { - precondition { - condition = length(setintersection(toset(keys(var.cloud_connectors)), toset(nonsensitive(keys(var.vcs_connectors))))) == 0 - error_message = "cloud_connectors and vcs_connectors must not use the same key." - } - } -} - -module "aws_rbac" { - for_each = { for name, connector in var.cloud_connectors : name => connector if connector.kind == "AWS_RBAC" } - - source = "./aws_rbac" - - iam_role_name = each.value.iam_role_name - role_external_id = each.value.aws_external_id - policy_arn = coalesce(each.value.policy_arn, "arn:aws:iam::aws:policy/ReadOnlyAccess") - trusted_account_ids = coalesce(each.value.trusted_account_ids, ["476299211833", "163602625436"]) -} - -module "aws_static" { - for_each = { for name, connector in var.cloud_connectors : name => connector if connector.kind == "AWS_STATIC" } - - source = "./aws_static" - - aws_region = coalesce(each.value.aws_region, "eu-central-1") - iam_user = coalesce(each.value.iam_user_name, each.key) - allow_static_credentials = each.value.allow_static_credentials -} - -module "aws_oidc" { - for_each = { for name, connector in var.cloud_connectors : name => connector if connector.kind == "AWS_OIDC" } - - source = "./aws_oidc" - - aws_region = coalesce(each.value.aws_region, "eu-central-1") - iam_role_name = each.value.iam_role_name - stackguardian_org_name = var.stackguardian_org_name - policy_arn = coalesce(each.value.policy_arn, "arn:aws:iam::aws:policy/ReadOnlyAccess") -} - -module "azure_oidc" { - for_each = { for name, connector in var.cloud_connectors : name => connector if connector.kind == "AZURE_OIDC" } - - source = "./azure_oidc" - - subscription_id = each.value.azure_subscription_id - tenant_id = each.value.azure_tenant_id - application_display_name = coalesce(each.value.application_display_name, each.key) - stackguardian_org_name = var.stackguardian_org_name - role_definition_name = coalesce(each.value.role_definition_name, "Contributor") -} - -module "azure_static" { - for_each = { for name, connector in var.cloud_connectors : name => connector if connector.kind == "AZURE_STATIC" } - - source = "./azure_static" - - subscription_id = each.value.azure_subscription_id - tenant_id = each.value.azure_tenant_id - application_display_name = coalesce(each.value.application_display_name, each.key) - role_definition_name = coalesce(each.value.role_definition_name, "Contributor") - allow_static_credentials = each.value.allow_static_credentials -} - -module "gcp_oidc" { - for_each = { for name, connector in var.cloud_connectors : name => connector if connector.kind == "GCP_OIDC" } - - source = "./gcp_oidc" - - project_id = each.value.gcp_project_id - stackguardian_org_name = var.stackguardian_org_name - service_account_id = each.value.gcp_service_account_id - workload_identity_pool_id = each.value.gcp_workload_pool_id - workload_identity_pool_provider_id = each.value.gcp_provider_id - workload_identity_pool_display_name = each.key - project_role = coalesce(each.value.gcp_project_role, "roles/owner") -} - -module "stackguardian_connector_cloud" { - for_each = var.cloud_connectors - - source = "./stackguardian_connector_cloud" - connector_name = each.key - connector_kind = each.value.kind - allow_static_credentials = coalesce(each.value.allow_static_credentials, false) - aws_access_key_id = each.value.kind == "AWS_STATIC" ? module.aws_static[each.key].access_key_id : null - aws_secret_access_key = each.value.kind == "AWS_STATIC" ? module.aws_static[each.key].secret_access_key : null - aws_region = each.value.kind == "AWS_STATIC" ? coalesce(each.value.aws_region, "eu-central-1") : null - azure_tenant_id = contains(["AZURE_STATIC", "AZURE_OIDC"], each.value.kind) ? each.value.azure_tenant_id : null - azure_subscription_id = contains(["AZURE_STATIC", "AZURE_OIDC"], each.value.kind) ? each.value.azure_subscription_id : null - azure_client_id = contains(["AZURE_STATIC", "AZURE_OIDC"], each.value.kind) ? (each.value.kind == "AZURE_STATIC" ? module.azure_static[each.key].client_id : module.azure_oidc[each.key].client_id) : null - azure_client_secret = each.value.kind == "AZURE_STATIC" ? module.azure_static[each.key].client_secret_value : null - aws_role_arn = each.value.kind == "AWS_RBAC" ? module.aws_rbac[each.key].iam_role_arn : each.value.kind == "AWS_OIDC" ? module.aws_oidc[each.key].oidc_role_arn : null - aws_external_id = each.value.kind == "AWS_RBAC" ? each.value.aws_external_id : null - gcp_config_file_content = each.value.kind == "GCP_OIDC" ? module.gcp_oidc[each.key].external_account_config : null -} - -module "stackguardian_connector_vcs" { - source = "./stackguardian_connector_vcs" - vcs_connectors = var.vcs_connectors -} - -module "stackguardian_role" { - for_each = var.roles - - source = "./stackguardian_role" - org_name = var.stackguardian_org_name - role_name = each.key - workflow_groups = [for name in each.value.workflow_groups : module.stackguardian_workflow_group[name].workflow_groups] - cloud_connectors = [for name in each.value.cloud_connectors : module.stackguardian_connector_cloud[name].connector_name] - vcs_connectors = [for name in each.value.vcs_connectors : nonsensitive(module.stackguardian_connector_vcs.connector_vcs[index(nonsensitive(keys(var.vcs_connectors)), name)])] - template_list = each.value.template_list -} - -module "stackguardian_role_assignment" { - for_each = var.subjects - - source = "./stackguardian_role_assignment" - roles = [for name in each.value.roles : module.stackguardian_role[name].role] - subject = each.key - entity_type = each.value.entity_type +module "policy_assignment" { + source = "./modules/policy_assignment" + for_each = var.policy_assignments + + name = each.value.name + scope = each.value.scope + policy_definition_id = each.value.policy_definition_id + description = each.value.description + display_name = each.value.display_name } diff --git a/modules/policy_assignment/main.tf b/modules/policy_assignment/main.tf new file mode 100644 index 0000000..ba399dd --- /dev/null +++ b/modules/policy_assignment/main.tf @@ -0,0 +1,7 @@ +resource "azurerm_policy_assignment" "this" { + name = var.name + scope = var.scope + policy_definition_id = var.policy_definition_id + description = var.description + display_name = var.display_name +} diff --git a/modules/policy_assignment/outputs.tf b/modules/policy_assignment/outputs.tf new file mode 100644 index 0000000..30bb01f --- /dev/null +++ b/modules/policy_assignment/outputs.tf @@ -0,0 +1,3 @@ +output "id" { + value = azurerm_policy_assignment.this.id +} diff --git a/modules/policy_assignment/variables.tf b/modules/policy_assignment/variables.tf new file mode 100644 index 0000000..1ef4bc9 --- /dev/null +++ b/modules/policy_assignment/variables.tf @@ -0,0 +1,21 @@ +variable "name" { + type = string +} + +variable "scope" { + type = string +} + +variable "policy_definition_id" { + type = string +} + +variable "description" { + type = string + default = null +} + +variable "display_name" { + type = string + default = null +} diff --git a/outputs.tf b/outputs.tf index afbc154..e69de29 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,9 +0,0 @@ -output "role_permissions" { - description = "Generated permission documents keyed by role name." - value = { for name, role in module.stackguardian_role : name => role.allowed_permissions } -} - -output "subject_roles" { - description = "Assigned role names keyed by subject." - value = { for subject, assignment in module.stackguardian_role_assignment : subject => assignment.roles } -} diff --git a/providers.tf b/providers.tf new file mode 100644 index 0000000..e6833e6 --- /dev/null +++ b/providers.tf @@ -0,0 +1,4 @@ +provider "azurerm" { + skip_provider_registration = true + features {} +} diff --git a/variables.tf b/variables.tf index ea0460f..f481f7c 100644 --- a/variables.tf +++ b/variables.tf @@ -1,186 +1,10 @@ -variable "stackguardian_api_key" { - type = string - description = "StackGuardian API key used by the root provider configuration." - sensitive = true - - validation { - condition = can(regex("^sgu_[A-Za-z0-9]+$", var.stackguardian_api_key)) - error_message = "stackguardian_api_key must start with sgu_ followed by alphanumeric characters." - } -} - -variable "stackguardian_org_name" { - type = string - description = "StackGuardian organization name." - - validation { - condition = can(regex("^[A-Za-z0-9][A-Za-z0-9_-]{0,49}$", var.stackguardian_org_name)) - error_message = "stackguardian_org_name must be 1-50 letters, numbers, underscores, or hyphens." - } -} - -variable "stackguardian_api_uri" { - type = string - description = "StackGuardian API endpoint." - default = "https://api.app.stackguardian.io" - - validation { - condition = can(regex("^https://[^/]+(?:/.*)?$", var.stackguardian_api_uri)) - error_message = "stackguardian_api_uri must be an HTTPS URL." - } -} - -variable "workflow_groups" { - type = list(string) - description = "Workflow groups created by this root." - default = [] - - validation { - condition = length(var.workflow_groups) == length(toset(var.workflow_groups)) && alltrue([for name in var.workflow_groups : length(trimspace(name)) > 0]) - error_message = "workflow_groups must contain unique, non-empty names." - } -} - -variable "cloud_connectors" { - description = "Keyed cloud connectors. Terraform creates and registers the selected cloud identity; static kinds are legacy-only." - type = map(object({ - kind = string - allow_static_credentials = optional(bool) - aws_region = optional(string) - iam_role_name = optional(string) - iam_user_name = optional(string) - policy_arn = optional(string) - aws_external_id = optional(string) - trusted_account_ids = optional(list(string)) - azure_subscription_id = optional(string) - azure_tenant_id = optional(string) - application_display_name = optional(string) - role_definition_name = optional(string) - gcp_project_id = optional(string) - gcp_service_account_id = optional(string) - gcp_workload_pool_id = optional(string) - gcp_provider_id = optional(string) - gcp_project_role = optional(string) - })) - default = {} - - validation { - condition = alltrue([for name in keys(var.cloud_connectors) : can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", name))]) - error_message = "cloud_connectors keys must be 1-100 character StackGuardian resource names." - } - - validation { - condition = alltrue([for connector in values(var.cloud_connectors) : contains(["AWS_STATIC", "AWS_RBAC", "AWS_OIDC", "AZURE_STATIC", "AZURE_OIDC", "GCP_OIDC"], connector.kind)]) - error_message = "cloud_connectors[*].kind must be AWS_STATIC, AWS_RBAC, AWS_OIDC, AZURE_STATIC, AZURE_OIDC, or GCP_OIDC." - } - - validation { - condition = alltrue([for connector in values(var.cloud_connectors) : - (connector.kind == "AWS_STATIC" && connector.allow_static_credentials == true && - (connector.aws_region == null || can(regex("^[a-z]{2}(-gov)?-[a-z]+-\\d$", connector.aws_region))) && - (connector.iam_user_name == null || can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", connector.iam_user_name))) && - connector.iam_role_name == null && connector.policy_arn == null && connector.aws_external_id == null && connector.trusted_account_ids == null && connector.azure_subscription_id == null && connector.azure_tenant_id == null && connector.application_display_name == null && connector.role_definition_name == null && connector.gcp_project_id == null && connector.gcp_service_account_id == null && connector.gcp_workload_pool_id == null && connector.gcp_provider_id == null && connector.gcp_project_role == null) || - (connector.kind == "AWS_RBAC" && try(length(trimspace(connector.iam_role_name)) > 0, false) && try(length(trimspace(connector.aws_external_id)) > 0, false) && - (connector.aws_region == null || can(regex("^[a-z]{2}(-gov)?-[a-z]+-\\d$", connector.aws_region))) && - (connector.policy_arn == null || length(trimspace(connector.policy_arn)) > 0) && - (connector.trusted_account_ids == null || (length(connector.trusted_account_ids) == length(toset(connector.trusted_account_ids)) && alltrue([for account_id in connector.trusted_account_ids : can(regex("^\\d{12}$", account_id))]))) && - connector.allow_static_credentials == null && connector.iam_user_name == null && connector.azure_subscription_id == null && connector.azure_tenant_id == null && connector.application_display_name == null && connector.role_definition_name == null && connector.gcp_project_id == null && connector.gcp_service_account_id == null && connector.gcp_workload_pool_id == null && connector.gcp_provider_id == null && connector.gcp_project_role == null) || - (connector.kind == "AWS_OIDC" && try(can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", connector.iam_role_name)), false) && - (connector.aws_region == null || can(regex("^[a-z]{2}(-gov)?-[a-z]+-\\d$", connector.aws_region))) && - (connector.policy_arn == null || length(trimspace(connector.policy_arn)) > 0) && - connector.allow_static_credentials == null && connector.iam_user_name == null && connector.aws_external_id == null && connector.trusted_account_ids == null && connector.azure_subscription_id == null && connector.azure_tenant_id == null && connector.application_display_name == null && connector.role_definition_name == null && connector.gcp_project_id == null && connector.gcp_service_account_id == null && connector.gcp_workload_pool_id == null && connector.gcp_provider_id == null && connector.gcp_project_role == null) || - (connector.kind == "AZURE_STATIC" && connector.allow_static_credentials == true && try(can(regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", connector.azure_subscription_id)), false) && try(can(regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", connector.azure_tenant_id)), false) && - (connector.application_display_name == null || can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", connector.application_display_name))) && - (connector.role_definition_name == null || length(trimspace(connector.role_definition_name)) > 0) && - connector.aws_region == null && connector.iam_role_name == null && connector.iam_user_name == null && connector.policy_arn == null && connector.aws_external_id == null && connector.trusted_account_ids == null && connector.gcp_project_id == null && connector.gcp_service_account_id == null && connector.gcp_workload_pool_id == null && connector.gcp_provider_id == null && connector.gcp_project_role == null) || - (connector.kind == "AZURE_OIDC" && try(can(regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", connector.azure_subscription_id)), false) && try(can(regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", connector.azure_tenant_id)), false) && - (connector.application_display_name == null || can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", connector.application_display_name))) && - (connector.role_definition_name == null || length(trimspace(connector.role_definition_name)) > 0) && - connector.allow_static_credentials == null && connector.aws_region == null && connector.iam_role_name == null && connector.iam_user_name == null && connector.policy_arn == null && connector.aws_external_id == null && connector.trusted_account_ids == null && connector.gcp_project_id == null && connector.gcp_service_account_id == null && connector.gcp_workload_pool_id == null && connector.gcp_provider_id == null && connector.gcp_project_role == null) || - (connector.kind == "GCP_OIDC" && try(can(regex("^[a-z][a-z0-9-]{4,28}[a-z0-9]$", connector.gcp_project_id)), false) && try(length(trimspace(connector.gcp_service_account_id)) > 0, false) && try(length(trimspace(connector.gcp_workload_pool_id)) > 0, false) && try(length(trimspace(connector.gcp_provider_id)) > 0, false) && - (connector.gcp_project_role == null || length(trimspace(connector.gcp_project_role)) > 0) && - connector.allow_static_credentials == null && connector.aws_region == null && connector.iam_role_name == null && connector.iam_user_name == null && connector.policy_arn == null && connector.aws_external_id == null && connector.trusted_account_ids == null && connector.azure_subscription_id == null && connector.azure_tenant_id == null && connector.application_display_name == null && connector.role_definition_name == null) - ]) - error_message = "Each cloud connector must provide only the properties valid for its kind, including required IDs and static credential acknowledgement." - } -} - -variable "vcs_connectors" { - description = "Keyed VCS connector configuration. Credentials are sensitive and persist in Terraform state." - sensitive = true - type = map(object({ - kind = string - name = string - github = optional(object({ - githubCreds = string - github_com_url = optional(string) - github_http_url = optional(string) - })) - gitlab = optional(object({ - gitlabCreds = string - gitlabHttpUrl = optional(string) - gitlabApiUrl = optional(string) - })) - bitbucket = optional(object({ - bitbucket_creds = string - })) - })) - default = {} - - validation { - condition = alltrue([for name, connector in var.vcs_connectors : can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", name)) && connector.name == name]) - error_message = "vcs_connectors keys must be resource names and match the name required by the unchanged VCS connector module." - } - - validation { - condition = alltrue([for connector in values(var.vcs_connectors) : - (connector.kind == "GITHUB_COM" && connector.github != null && connector.gitlab == null && connector.bitbucket == null && length(trimspace(connector.github.githubCreds)) > 0) || - (connector.kind == "GITLAB_COM" && connector.gitlab != null && connector.github == null && connector.bitbucket == null && length(trimspace(connector.gitlab.gitlabCreds)) > 0) || - (connector.kind == "BITBUCKET_ORG" && connector.bitbucket != null && connector.github == null && connector.gitlab == null && length(trimspace(connector.bitbucket.bitbucket_creds)) > 0) - ]) - error_message = "Each VCS connector must have exactly one matching GitHub, GitLab, or Bitbucket credential object with non-empty credentials." - } -} - -variable "roles" { - description = "Keyed StackGuardian roles and their resource scopes." - type = map(object({ - workflow_groups = optional(list(string), []) - cloud_connectors = optional(list(string), []) - vcs_connectors = optional(list(string), []) - template_list = optional(list(string), []) - })) - default = {} - - validation { - condition = alltrue([for name, role in var.roles : - can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", name)) && - length(role.workflow_groups) == length(toset(role.workflow_groups)) && - length(role.cloud_connectors) == length(toset(role.cloud_connectors)) && - length(role.vcs_connectors) == length(toset(role.vcs_connectors)) && - length(role.template_list) == length(toset(role.template_list)) && - alltrue([for reference in concat(role.workflow_groups, role.cloud_connectors, role.vcs_connectors, role.template_list) : length(trimspace(reference)) > 0]) && - length(concat(role.workflow_groups, role.cloud_connectors, role.vcs_connectors, role.template_list)) > 0 - ]) - error_message = "Each role key must be a resource name and each role must have at least one unique, non-empty workflow group, connector, or template reference." - } -} - -variable "subjects" { - description = "Keyed local-email, qualified SSO-email, or SSO-group subjects and their assigned roles." +variable "policy_assignments" { type = map(object({ - entity_type = optional(string, "EMAIL") - roles = list(string) + name = string + scope = string + policy_definition_id = string + description = optional(string) + display_name = optional(string) })) default = {} - - validation { - condition = alltrue([for subject, assignment in var.subjects : - can(regex("^([^/@\\s]+/[^/\\s]+|[^@\\s]+@[^@\\s]+\\.[^@\\s]+)$", subject)) && - contains(["EMAIL", "GROUP"], assignment.entity_type) && - length(assignment.roles) > 0 && length(assignment.roles) == length(toset(assignment.roles)) && - alltrue([for role in assignment.roles : can(regex("^[A-Za-z0-9][A-Za-z0-9 _.-]{0,99}$", role))]) - ]) - error_message = "Each subject must be a local email or qualified SSO subject with a valid entity_type and a non-empty, duplicate-free role list." - } } diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..c099a4a --- /dev/null +++ b/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 2.99" + } + } +}