|
| 1 | +# Stack role attachments |
| 2 | + |
| 3 | +Stacks can receive role bindings to perform operations with elevated permissions, similar to how users, API keys, and IdP groups receive permissions through [Spacelift's RBAC system](rbac-system.md). |
| 4 | + |
| 5 | +Stack role attachments replace the deprecated [Administrative](../stack/stack-settings.md#administrative) flag, providing a more flexible, auditable, and powerful approach to granting stacks elevated permissions. |
| 6 | + |
| 7 | +## Why use stack role attachments |
| 8 | + |
| 9 | +Stack role attachments offer significant advantages over the deprecated administrative flag: |
| 10 | + |
| 11 | +### Cross-space access |
| 12 | + |
| 13 | +**Administrative flag limitation**: Can only create resources in the stack's own space and subspaces. |
| 14 | + |
| 15 | +**Role attachments advantage**: Can attach roles to sibling spaces, enabling horizontal access across the space tree. In the example below, a stack in `ChildSpace1` can be granted access to `ChildSpace2`: |
| 16 | + |
| 17 | +```mermaid |
| 18 | +graph TD |
| 19 | + A[root] --> B[ChildSpace1] |
| 20 | + A --> C[ChildSpace2] |
| 21 | +``` |
| 22 | + |
| 23 | +### Fine-grained access control |
| 24 | + |
| 25 | +**Administrative flag limitation**: All-or-nothing approach - grants full Space Admin permission with every available permission. |
| 26 | + |
| 27 | +**Role attachments advantage**: Use custom roles with specific permissions (for example, only `context:create` and `workerpool:create`). |
| 28 | + |
| 29 | +This means a stack can create contexts and worker pools, but cannot manage any other resources, such as policies or webhooks. |
| 30 | + |
| 31 | +### Enhanced audit trail |
| 32 | + |
| 33 | +**Administrative flag**: Basic audit trail with stack actor information. |
| 34 | + |
| 35 | +**Role attachments advantage**: Audit trail webhooks include role information in the `ActorRoles` field (array of role slugs). |
| 36 | + |
| 37 | +This provides better visibility into what permissions the stack was using when performing actions. See the [audit trail documentation](../../integrations/audit-trail.md) for details. |
| 38 | + |
| 39 | +### Modern RBAC consistency |
| 40 | + |
| 41 | +Role attachments align stacks with the broader role-based access model already used by [users](./assigning-roles-users.md), [IdP groups](./assigning-roles-groups.md), and [API keys](./assigning-roles-api-keys.md), providing a consistent permission management experience across all actors. |
| 42 | + |
| 43 | +## Assign roles to stacks |
| 44 | + |
| 45 | +### Prerequisites |
| 46 | + |
| 47 | +To attach a role to a stack, you need: |
| 48 | + |
| 49 | +- `StackManage` permission (or `SpaceAdmin` permission as fallback) to the stack's space |
| 50 | +- `SpaceAdmin` permission to the binding space (the space where the role will be effective) |
| 51 | + |
| 52 | +!!! info "Why both permissions are required" |
| 53 | + Creating a role binding that grants permissions to a space effectively allows the stack to act in that space. To prevent privilege escalation, you must have admin access to both spaces: the space where the stack resides and the space where the role will be effective. |
| 54 | + |
| 55 | +### Using the Web UI |
| 56 | + |
| 57 | +1. Navigate to the stack's **Settings** page, then choose **Roles** on the left |
| 58 | +2. Click **Manage Roles** on the top right |
| 59 | +3. In the sidebar, select the desired role and the target space |
| 60 | +4. Click **Add** |
| 61 | + |
| 62 | +### Using the Terraform provider |
| 63 | + |
| 64 | +Use the `spacelift_role_attachment` resource: |
| 65 | + |
| 66 | +```hcl |
| 67 | +resource "spacelift_stack" "admin" { |
| 68 | + name = "Admin stack" |
| 69 | + repository = "spacelift-resources" |
| 70 | + description = "Only has permissions to create another stacks in the dev space" |
| 71 | + branch = "main" |
| 72 | +} |
| 73 | +
|
| 74 | +resource "spacelift_role" "stack_creator" { |
| 75 | + name = "Stack creator" |
| 76 | + description = "A role solely for creating stacks" |
| 77 | + actions = ["STACK_MANAGE"] |
| 78 | +} |
| 79 | +
|
| 80 | +resource "spacelift_space" "dev" { |
| 81 | + name = "dev" |
| 82 | + parent_space_id = "root" |
| 83 | +} |
| 84 | +
|
| 85 | +resource "spacelift_role_attachment" "stack_creator_to_admin_stack" { |
| 86 | + stack_id = spacelift_stack.admin.id |
| 87 | + role_id = spacelift_role.stack_creator.id |
| 88 | + space_id = spacelift_space.dev.id |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +For more information, see the [Spacelift Terraform provider documentation](https://search.opentofu.org/provider/spacelift-io/spacelift/latest/docs/resources/role_attachment){: rel="nofollow"}. |
| 93 | + |
| 94 | +## Permission cascading |
| 95 | + |
| 96 | +Role attachments cascade down to child spaces, similar to how the administrative flag worked: |
| 97 | + |
| 98 | +```text |
| 99 | +parentSpace |
| 100 | +├── childSpace1 |
| 101 | +└── childSpace2 |
| 102 | + └── grandchildSpace |
| 103 | +``` |
| 104 | + |
| 105 | +If a role is attached to `parentSpace`, the same role will be effective in `childSpace1`, `childSpace2`, and `grandchildSpace`. |
| 106 | + |
| 107 | +!!! warning "Root space caution" |
| 108 | + Since the `root` space is the parent of all spaces, attaching roles to it affects **all spaces** in your account. Use this with extreme caution and only when necessary. |
| 109 | + |
| 110 | +## Administrative flag precedence |
| 111 | + |
| 112 | +The administrative flag takes precedence over role attachments: |
| 113 | + |
| 114 | +- If `administrative = true`, any attached roles will be **completely ineffective** |
| 115 | +- You **must** set `administrative = false` for role attachments to take effect |
| 116 | +- This is critical for migration - disable the administrative flag after creating role attachments |
| 117 | + |
| 118 | +## Policy integration |
| 119 | + |
| 120 | +Policies can react to stack role attachments through the `stack.roles` field in [policy inputs](../policy/approval-policy.md#data-input-schema). This enables policy-based logic based on what roles a stack has attached. |
| 121 | + |
| 122 | +### Example: Reject Space Admin role usage |
| 123 | + |
| 124 | +```rego |
| 125 | +package spacelift |
| 126 | +
|
| 127 | +reject_with_note["Don't use the Space Admin role!"] { |
| 128 | + role := input.stack.roles[_] |
| 129 | + role.id == "space-admin" # Role slug. Use the "Copy Slug" button in the UI to get it. |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## Multiple roles |
| 134 | + |
| 135 | +Stacks can have multiple role bindings: |
| 136 | + |
| 137 | +- Different roles in different spaces for varied access levels |
| 138 | +- Multiple roles in the same space (permissions are additive) |
| 139 | +- Combinations of Space Admin in own space and Reader in other spaces |
| 140 | + |
| 141 | +### Example: Multiple role attachments |
| 142 | + |
| 143 | +```hcl |
| 144 | +# Admin access to development space |
| 145 | +resource "spacelift_role_attachment" "dev_admin" { |
| 146 | + stack_id = spacelift_stack.platform.id |
| 147 | + role_id = "space-admin" |
| 148 | + space_id = "development-space-id" |
| 149 | +} |
| 150 | +
|
| 151 | +# Read access to production space |
| 152 | +resource "spacelift_role_attachment" "prod_reader" { |
| 153 | + stack_id = spacelift_stack.platform.id |
| 154 | + role_id = "space-reader" |
| 155 | + space_id = "production-space-id" |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +## External state access |
| 160 | + |
| 161 | +External state access allows you to read the state of the stack from outside authorized runs and [tasks](../../concepts/run/task.md). See the documentation [here](../../vendors/terraform/external-state-access.md) for further details. |
| 162 | + |
| 163 | +In order for your stack to access another stack's OpenTofu/Terraform state, the stack needs to have **Space writer** role to the target stack's space. This can be achieved by attaching the **Space writer** role to the stack for the target stack's space. |
| 164 | + |
| 165 | +Example: |
| 166 | + |
| 167 | +```hcl |
| 168 | +data "spacelift_role" "space_writer" { |
| 169 | + slug = "space-writer" |
| 170 | +} |
| 171 | +
|
| 172 | +resource "spacelift_role_attachment" "space_writer" { |
| 173 | + stack_id = spacelift_stack.consumer.id |
| 174 | + role_id = data.spacelift_role.space_writer.id |
| 175 | + space_id = spacelift_stack.provider.space_id |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +!!! note |
| 180 | + The **Space admin** role also includes **Space writer** permissions. |
| 181 | + |
| 182 | +## Migration from administrative flag |
| 183 | + |
| 184 | +!!! warning "Automatic Migration on June 1st, 2026" |
| 185 | + On June 1st, 2026, Spacelift will automatically disable all administrative flags and attach the Space Admin role to each stack's own space. This automatic migration is **100% backward compatible** and ensures no functionality loss. |
| 186 | + |
| 187 | + However, **manual migration is strongly recommended** to take advantage of advanced features like cross-space access and fine-grained permissions that are not available with automatic migration. |
| 188 | + |
| 189 | +### What happens on June 1st, 2026 |
| 190 | + |
| 191 | +On June 1st, 2026, Spacelift will automatically: |
| 192 | + |
| 193 | +- Disable all administrative flags |
| 194 | +- Attach the Space Admin role to each stack's own space (100% backward compatible) |
| 195 | + - Note: if you move the stack to a different space later, the role attachment remains unchanged and will not follow the stack's new space |
| 196 | +- Remove the administrative flag from the UI |
| 197 | +- Make the flag ineffective in the GraphQL API (even if set to `true`, it will behave as `false`) |
| 198 | + |
| 199 | +### Step-by-step migration guide |
| 200 | + |
| 201 | +#### 1. Identify stacks with administrative flag |
| 202 | + |
| 203 | +List all stacks with `administrative = true` in your account. You can do this through the Spacelift UI (by filtering on the stacks page) or by searching your Terraform code for the `administrative` attribute. |
| 204 | + |
| 205 | +#### 2. For each stack, create role attachment |
| 206 | + |
| 207 | +Attach the Space Admin role to the stack using the stack's own space as the binding space: |
| 208 | + |
| 209 | +**Using the Terraform provider:** |
| 210 | + |
| 211 | +```hcl |
| 212 | +data "spacelift_role" "admin_role" { |
| 213 | + slug = "space-admin" |
| 214 | +} |
| 215 | + |
| 216 | +resource "spacelift_role_attachment" "stack_admin" { |
| 217 | + stack_id = spacelift_stack.management.id |
| 218 | + space_id = spacelift_stack.management.space_id |
| 219 | + role_id = data.spacelift_role.admin_role.id |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +**Using the Web UI:** |
| 224 | + |
| 225 | +1. Navigate to the stack's **Settings** page, then choose **Roles** |
| 226 | +2. Click **Manage Roles** on the top right |
| 227 | +3. In the drawer, select the **Space admin** role and the stack's own Space as the target |
| 228 | +4. Click **Add** |
| 229 | + |
| 230 | +Assuming your stack is in the `dev` [Space](../spaces/README.md), the role attachment will grant **Space admin** permissions in the `dev` space: |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | +#### 3. Disable the administrative flag |
| 235 | + |
| 236 | +Once you've verified the roles have been attached, disable the administrative flag: |
| 237 | + |
| 238 | +```hcl |
| 239 | +resource "spacelift_stack" "management" { |
| 240 | + # [... other stack settings ...] |
| 241 | + administrative = false |
| 242 | +} |
| 243 | +``` |
| 244 | + |
| 245 | +!!! important |
| 246 | + The administrative flag takes precedence over role attachments. If `administrative = true`, any attached roles will be ignored. You must set `administrative = false` for role attachments to take effect. |
| 247 | + |
| 248 | +#### 4. Verify the role attachment |
| 249 | + |
| 250 | +After creating the role attachment, verify that the stack can perform the same operations as before. Trigger a tracked run and ensure it succeeds. |
| 251 | + |
| 252 | +#### 5. Adjust policies if necessary |
| 253 | + |
| 254 | +If any of your policies reference the `stack.administrative` field, update them to use the `stack.roles` field instead. For example: |
| 255 | + |
| 256 | +```rego |
| 257 | +# Old policy: |
| 258 | +deny_with_note["Administrative stacks are not allowed"] { |
| 259 | + stack := input.stack |
| 260 | + stack.administrative == true |
| 261 | +} |
| 262 | +
|
| 263 | +# Would become: |
| 264 | +deny_with_note["Administrative stacks are not allowed"] { |
| 265 | + role := input.stack.roles[_] |
| 266 | + role.id == "space-admin" # Role slug. Use the "Copy Slug" button in the UI to get it. |
| 267 | +} |
| 268 | +``` |
| 269 | + |
| 270 | +### Rollback procedure |
| 271 | + |
| 272 | +If you need to roll back during migration: |
| 273 | + |
| 274 | +1. Set `administrative = true` again |
| 275 | +2. The administrative flag takes precedence, so role attachments will be ignored |
| 276 | +3. Test that everything works as before |
| 277 | +4. You can leave the role attachments in place and try migration again later |
| 278 | + |
| 279 | +## Edge cases |
| 280 | + |
| 281 | +### Stack moving between spaces |
| 282 | + |
| 283 | +When a stack moves to a different space, existing role bindings remain unchanged. This is intentional and important for Terraform provider stability. |
| 284 | + |
| 285 | +If you want to update role bindings after moving a stack, you need to explicitly modify the role attachments. |
0 commit comments