Skip to content

Commit d443b54

Browse files
authored
Feat: implement sa tokens (#712)
* feat: implement sa access token resource
1 parent 23e9a25 commit d443b54

File tree

6 files changed

+783
-0
lines changed

6 files changed

+783
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_service_account_access_token Resource - stackit"
4+
subcategory: ""
5+
description: |-
6+
Service account access token schema.
7+
~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources.
8+
Example Usage
9+
Automatically rotate access tokens
10+
11+
resource "stackit_service_account" "sa" {
12+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
13+
name = "sa01"
14+
}
15+
16+
resource "time_rotating" "rotate" {
17+
rotation_days = 80
18+
}
19+
20+
resource "stackit_service_account_access_token" "sa_token" {
21+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
22+
service_account_email = stackit_service_account.sa.email
23+
ttl_days = 180
24+
25+
rotate_when_changed = {
26+
rotation = time_rotating.rotate.id
27+
}
28+
}
29+
---
30+
31+
# stackit_service_account_access_token (Resource)
32+
33+
Service account access token schema.
34+
35+
~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources.
36+
## Example Usage
37+
38+
39+
### Automatically rotate access tokens
40+
```terraform
41+
resource "stackit_service_account" "sa" {
42+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
43+
name = "sa01"
44+
}
45+
46+
resource "time_rotating" "rotate" {
47+
rotation_days = 80
48+
}
49+
50+
resource "stackit_service_account_access_token" "sa_token" {
51+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
52+
service_account_email = stackit_service_account.sa.email
53+
ttl_days = 180
54+
55+
rotate_when_changed = {
56+
rotation = time_rotating.rotate.id
57+
}
58+
}
59+
60+
```
61+
62+
63+
64+
<!-- schema generated by tfplugindocs -->
65+
## Schema
66+
67+
### Required
68+
69+
- `project_id` (String) STACKIT project ID associated with the service account token.
70+
- `service_account_email` (String) Email address linked to the service account.
71+
72+
### Optional
73+
74+
- `rotate_when_changed` (Map of String) A map of arbitrary key/value pairs that will force recreation of the token when they change, enabling token rotation based on external conditions such as a rotating timestamp. Changing this forces a new resource to be created.
75+
- `ttl_days` (Number) Specifies the token's validity duration in days. If unspecified, defaults to 90 days.
76+
77+
### Read-Only
78+
79+
- `access_token_id` (String) Identifier for the access token linked to the service account.
80+
- `active` (Boolean) Indicate whether the token is currently active or inactive
81+
- `created_at` (String) Timestamp indicating when the access token was created.
82+
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`service_account_email`,`access_token_id`".
83+
- `token` (String, Sensitive) JWT access token for API authentication. Prefixed by 'Bearer' and should be stored securely as it is irretrievable once lost.
84+
- `valid_until` (String) Estimated expiration timestamp of the access token. For precise validity, check the JWT details.

stackit/internal/services/serviceaccount/serviceaccount_acc_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func inputServiceAccountResourceConfig(name string) string {
3030
project_id = "%s"
3131
name = "%s"
3232
}
33+
34+
resource "stackit_service_account_access_token" "token" {
35+
project_id = stackit_service_account.sa.project_id
36+
service_account_email = stackit_service_account.sa.email
37+
}
3338
`,
3439
testutil.ServiceAccountProviderConfig(),
3540
serviceAccountResource["project_id"],
@@ -62,6 +67,11 @@ func TestServiceAccount(t *testing.T) {
6267
resource.TestCheckResourceAttr("stackit_service_account.sa", "project_id", serviceAccountResource["project_id"]),
6368
resource.TestCheckResourceAttr("stackit_service_account.sa", "name", serviceAccountResource["name01"]),
6469
resource.TestCheckResourceAttrSet("stackit_service_account.sa", "email"),
70+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "token"),
71+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "created_at"),
72+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "valid_until"),
73+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "service_account_email"),
74+
resource.TestCheckResourceAttrPair("stackit_service_account.sa", "email", "stackit_service_account_access_token.token", "service_account_email"),
6575
),
6676
},
6777
// Update
@@ -71,6 +81,11 @@ func TestServiceAccount(t *testing.T) {
7181
resource.TestCheckResourceAttr("stackit_service_account.sa", "project_id", serviceAccountResource["project_id"]),
7282
resource.TestCheckResourceAttr("stackit_service_account.sa", "name", serviceAccountResource["name02"]),
7383
resource.TestCheckResourceAttrSet("stackit_service_account.sa", "email"),
84+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "token"),
85+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "created_at"),
86+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "valid_until"),
87+
resource.TestCheckResourceAttrSet("stackit_service_account_access_token.token", "service_account_email"),
88+
resource.TestCheckResourceAttrPair("stackit_service_account.sa", "email", "stackit_service_account_access_token.token", "service_account_email"),
7489
),
7590
},
7691
// Data source
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package token
2+
3+
const markdownDescription = `
4+
## Example Usage` + "\n" + `
5+
6+
### Automatically rotate access tokens` + "\n" +
7+
"```terraform" + `
8+
resource "stackit_service_account" "sa" {
9+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
10+
name = "sa01"
11+
}
12+
13+
resource "time_rotating" "rotate" {
14+
rotation_days = 80
15+
}
16+
17+
resource "stackit_service_account_access_token" "sa_token" {
18+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
19+
service_account_email = stackit_service_account.sa.email
20+
ttl_days = 180
21+
22+
rotate_when_changed = {
23+
rotation = time_rotating.rotate.id
24+
}
25+
}
26+
` + "\n```"

0 commit comments

Comments
 (0)