-
Notifications
You must be signed in to change notification settings - Fork 42
feat(access-token): add ephemeral access-token resource #1068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
h3adex
wants to merge
11
commits into
stackitcloud:main
Choose a base branch
from
h3adex:feat/add-ephemeral-access-token-resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0ed44bd
feat(access-token): add ephemeral access-token resource
h3adex 6926ded
review changes
h3adex a5bc29e
review changes 2
h3adex f177b1f
review changes 3
h3adex 1bab9f1
review changes 4
h3adex 08bdae4
review changes 5 (improve example)
h3adex 6394938
review changes 5 (improve test description)
h3adex 785862b
review changes 5 (improve note docs)
h3adex 8d43a78
review changes 5 (fix typo)
h3adex c45e7c8
review changes 7 (ephemeralProviderData)
h3adex c0ea5a6
review changes 8 (improve logging)
h3adex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_access_token Ephemeral Resource - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| Ephemeral resource that generates a short-lived STACKIT access token (JWT) using a service account key. A new token is generated each time the resource is evaluated, and it remains consistent for the duration of a Terraform operation. If a private key is not explicitly provided, the provider attempts to extract it from the service account key instead. Access tokens generated from service account keys expire after 60 minutes. | ||
| ~> Service account key credentials must be configured either in the STACKIT provider configuration or via environment variables (see example below). If any other authentication method is configured, this ephemeral resource will fail with an error. | ||
| ~> This ephemeral-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. | ||
| --- | ||
|
|
||
| # stackit_access_token (Ephemeral Resource) | ||
|
|
||
| Ephemeral resource that generates a short-lived STACKIT access token (JWT) using a service account key. A new token is generated each time the resource is evaluated, and it remains consistent for the duration of a Terraform operation. If a private key is not explicitly provided, the provider attempts to extract it from the service account key instead. Access tokens generated from service account keys expire after 60 minutes. | ||
|
|
||
| ~> Service account key credentials must be configured either in the STACKIT provider configuration or via environment variables (see example below). If any other authentication method is configured, this ephemeral resource will fail with an error. | ||
|
|
||
| ~> This ephemeral-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. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| provider "stackit" { | ||
| default_region = "eu01" | ||
| service_account_key_path = "/path/to/sa_key.json" | ||
| enable_beta_resources = true | ||
| } | ||
|
|
||
| ephemeral "stackit_access_token" "example" {} | ||
|
|
||
| locals { | ||
| stackit_api_base_url = "https://iaas.api.stackit.cloud" | ||
| public_ip_path = "/v2/projects/${var.project_id}/regions/${var.region}/public-ips" | ||
|
|
||
| public_ip_payload = { | ||
| labels = { | ||
| key = "value" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Docs: https://registry.terraform.io/providers/magodo/restful/latest | ||
| provider "restful" { | ||
| base_url = local.stackit_api_base_url | ||
|
|
||
| security = { | ||
| http = { | ||
| token = { | ||
| token = ephemeral.stackit_access_token.example.access_token | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "restful_resource" "public_ip_restful" { | ||
| path = local.public_ip_path | ||
| body = local.public_ip_payload | ||
|
|
||
| read_path = "$(path)/$(body.id)" | ||
| update_path = "$(path)/$(body.id)" | ||
| update_method = "PATCH" | ||
| delete_path = "$(path)/$(body.id)" | ||
| delete_method = "DELETE" | ||
| } | ||
|
|
||
| # Docs: https://registry.terraform.io/providers/Mastercard/restapi/latest | ||
| provider "restapi" { | ||
| uri = local.stackit_api_base_url | ||
| write_returns_object = true | ||
|
|
||
| headers = { | ||
| Authorization = "Bearer ${ephemeral.stackit_access_token.example.access_token}" | ||
| Content-Type = "application/json" | ||
| } | ||
|
|
||
| create_method = "POST" | ||
| update_method = "PATCH" | ||
| destroy_method = "DELETE" | ||
| } | ||
|
|
||
| resource "restapi_object" "public_ip_restapi" { | ||
| path = local.public_ip_path | ||
| data = jsonencode(local.public_ip_payload) | ||
|
|
||
| id_attribute = "id" | ||
| read_method = "GET" | ||
| create_method = "POST" | ||
| update_method = "PATCH" | ||
| destroy_method = "DELETE" | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `access_token` (String, Sensitive) JWT access token for STACKIT API authentication. | ||
68 changes: 68 additions & 0 deletions
68
examples/ephemeral-resources/stackit_access_token/ephemeral-resource.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| provider "stackit" { | ||
| default_region = "eu01" | ||
| service_account_key_path = "/path/to/sa_key.json" | ||
| enable_beta_resources = true | ||
| } | ||
|
|
||
| ephemeral "stackit_access_token" "example" {} | ||
|
|
||
| locals { | ||
| stackit_api_base_url = "https://iaas.api.stackit.cloud" | ||
| public_ip_path = "/v2/projects/${var.project_id}/regions/${var.region}/public-ips" | ||
|
|
||
| public_ip_payload = { | ||
| labels = { | ||
| key = "value" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Docs: https://registry.terraform.io/providers/magodo/restful/latest | ||
| provider "restful" { | ||
| base_url = local.stackit_api_base_url | ||
|
|
||
| security = { | ||
| http = { | ||
| token = { | ||
| token = ephemeral.stackit_access_token.example.access_token | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "restful_resource" "public_ip_restful" { | ||
| path = local.public_ip_path | ||
| body = local.public_ip_payload | ||
|
|
||
| read_path = "$(path)/$(body.id)" | ||
| update_path = "$(path)/$(body.id)" | ||
| update_method = "PATCH" | ||
| delete_path = "$(path)/$(body.id)" | ||
| delete_method = "DELETE" | ||
| } | ||
|
|
||
| # Docs: https://registry.terraform.io/providers/Mastercard/restapi/latest | ||
| provider "restapi" { | ||
| uri = local.stackit_api_base_url | ||
| write_returns_object = true | ||
|
|
||
| headers = { | ||
| Authorization = "Bearer ${ephemeral.stackit_access_token.example.access_token}" | ||
| Content-Type = "application/json" | ||
| } | ||
|
|
||
| create_method = "POST" | ||
| update_method = "PATCH" | ||
| destroy_method = "DELETE" | ||
| } | ||
|
|
||
| resource "restapi_object" "public_ip_restapi" { | ||
| path = local.public_ip_path | ||
| data = jsonencode(local.public_ip_payload) | ||
|
|
||
| id_attribute = "id" | ||
| read_method = "GET" | ||
| create_method = "POST" | ||
| update_method = "PATCH" | ||
| destroy_method = "DELETE" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
stackit/internal/services/access_token/access_token_acc_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package access_token_test | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "regexp" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/config" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" | ||
| ) | ||
|
|
||
| //go:embed testdata/ephemeral_resource.tf | ||
| var ephemeralResourceConfig string | ||
|
|
||
| var testConfigVars = config.Variables{ | ||
| "default_region": config.StringVariable(testutil.Region), | ||
| } | ||
|
|
||
| func TestAccEphemeralAccessToken(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_10_0), | ||
| }, | ||
| ProtoV6ProviderFactories: testutil.TestEphemeralAccProtoV6ProviderFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: ephemeralResourceConfig, | ||
| ConfigVariables: testConfigVars, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue( | ||
| "echo.example", | ||
| tfjsonpath.New("data").AtMapKey("access_token"), | ||
| knownvalue.NotNull(), | ||
| ), | ||
| // JWT access tokens start with "ey" because the first part is base64-encoded JSON that begins with "{". | ||
| statecheck.ExpectKnownValue( | ||
| "echo.example", | ||
| tfjsonpath.New("data").AtMapKey("access_token"), | ||
| knownvalue.StringRegexp(regexp.MustCompile(`^ey`)), | ||
| ), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.