-
Notifications
You must be signed in to change notification settings - Fork 48
feat(kms): add key resource and datasource #1055
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
feat(kms): add key resource and datasource
relates to STACKITTPR-411
- Loading branch information
commit 720a183e07f24f9bae3b5b6ecb2da1670ab15f72
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,45 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_kms_key Data Source - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| KMS Key datasource schema. Uses the default_region specified in the provider configuration as a fallback in case no region is defined on datasource level. | ||
| --- | ||
|
|
||
| # stackit_kms_key (Data Source) | ||
|
|
||
| KMS Key datasource schema. Uses the `default_region` specified in the provider configuration as a fallback in case no `region` is defined on datasource level. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| data "stackit_kms_key" "key" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| key_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `key_id` (String) The ID of the key | ||
| - `keyring_id` (String) The ID of the associated key ring | ||
| - `project_id` (String) STACKIT project ID to which the key is associated. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `region` (String) The resource region. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `access_scope` (String) The access scope of the key. Default is `PUBLIC`. Possible values are: `PUBLIC`, `SNA`. | ||
| - `algorithm` (String) The encryption algorithm that the key will use to encrypt data. Possible values are: `aes_256_gcm`, `rsa_2048_oaep_sha256`, `rsa_3072_oaep_sha256`, `rsa_4096_oaep_sha256`, `rsa_4096_oaep_sha512`, `hmac_sha256`, `hmac_sha384`, `hmac_sha512`, `ecdsa_p256_sha256`, `ecdsa_p384_sha384`, `ecdsa_p521_sha512`. | ||
| - `description` (String) A user chosen description to distinguish multiple keys | ||
| - `display_name` (String) The display name to distinguish multiple keys | ||
| - `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`keyring_id,`key_id`". | ||
| - `import_only` (Boolean) States whether versions can be created or only imported. | ||
| - `protection` (String) The purpose for which the key will be used | ||
| - `purpose` (String) The purpose for which the key will be used. Possible values are: `symmetric_encrypt_decrypt`, `asymmetric_encrypt_decrypt`, `message_authentication_code`, `asymmetric_sign_verify`. | ||
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,51 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_kms_key Resource - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| KMS Key resource schema. Uses the default_region specified in the provider configuration as a fallback in case no region is defined on resource level. | ||
| ~> Keys will not be instantly destroyed by terraform during a terraform destroy. They will just be scheduled for deletion via the API and thrown out of the Terraform state afterwards. This way we can ensure no key setups are deleted by accident and it gives you the option to recover your keys within the grace period. | ||
| --- | ||
|
|
||
| # stackit_kms_key (Resource) | ||
|
|
||
| KMS Key resource schema. Uses the `default_region` specified in the provider configuration as a fallback in case no `region` is defined on resource level. | ||
|
|
||
| ~> Keys will **not** be instantly destroyed by terraform during a `terraform destroy`. They will just be scheduled for deletion via the API and thrown out of the Terraform state afterwards. **This way we can ensure no key setups are deleted by accident and it gives you the option to recover your keys within the grace period.** | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| resource "stackit_kms_key" "key" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| display_name = "key-01" | ||
| protection = "software" | ||
| algorithm = "aes_256_gcm" | ||
| purpose = "symmetric_encrypt_decrypt" | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `algorithm` (String) The encryption algorithm that the key will use to encrypt data. Possible values are: `aes_256_gcm`, `rsa_2048_oaep_sha256`, `rsa_3072_oaep_sha256`, `rsa_4096_oaep_sha256`, `rsa_4096_oaep_sha512`, `hmac_sha256`, `hmac_sha384`, `hmac_sha512`, `ecdsa_p256_sha256`, `ecdsa_p384_sha384`, `ecdsa_p521_sha512`. | ||
| - `display_name` (String) The display name to distinguish multiple keys | ||
| - `keyring_id` (String) The ID of the associated keyring | ||
| - `project_id` (String) STACKIT project ID to which the key is associated. | ||
| - `protection` (String) The underlying system that is responsible for protecting the key material. Currently only software is accepted. | ||
rubenhoenle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - `purpose` (String) The purpose for which the key will be used. Possible values are: `symmetric_encrypt_decrypt`, `asymmetric_encrypt_decrypt`, `message_authentication_code`, `asymmetric_sign_verify`. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `access_scope` (String) The access scope of the key. Default is `PUBLIC`. Possible values are: `PUBLIC`, `SNA`. | ||
| - `description` (String) A user chosen description to distinguish multiple keys | ||
| - `import_only` (Boolean) States whether versions can be created or only imported. | ||
| - `region` (String) The resource region. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`keyring_id`,`key_id`". | ||
| - `key_id` (String) The ID of the key | ||
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,5 @@ | ||
| data "stackit_kms_key" "key" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| key_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } |
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,8 @@ | ||
| resource "stackit_kms_key" "key" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| display_name = "key-01" | ||
| protection = "software" | ||
| algorithm = "aes_256_gcm" | ||
| purpose = "symmetric_encrypt_decrypt" | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| package kms | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/kms" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||
| kmsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/kms/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||
| ) | ||
|
|
||
| var ( | ||
| _ datasource.DataSource = &keyDataSource{} | ||
| ) | ||
|
|
||
| func NewKeyDataSource() datasource.DataSource { | ||
| return &keyDataSource{} | ||
| } | ||
|
|
||
| type keyDataSource struct { | ||
| client *kms.APIClient | ||
| providerData core.ProviderData | ||
| } | ||
|
|
||
| func (k *keyDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| resp.TypeName = req.ProviderTypeName + "_kms_key" | ||
| } | ||
|
|
||
| func (k *keyDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
| var ok bool | ||
| k.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| k.client = kmsUtils.ConfigureClient(ctx, &k.providerData, &resp.Diagnostics) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| tflog.Info(ctx, "KMS client configured") | ||
| } | ||
|
|
||
| func (k *keyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Description: fmt.Sprintf("KMS Key datasource schema. %s", core.DatasourceRegionFallbackDocstring), | ||
| Attributes: map[string]schema.Attribute{ | ||
| "access_scope": schema.StringAttribute{ | ||
| Description: fmt.Sprintf("The access scope of the key. Default is `%s`. %s", string(kms.ACCESSSCOPE_PUBLIC), utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedAccessScopeEnumValues)...)), | ||
| Computed: true, | ||
| Validators: []validator.String{ | ||
| stringvalidator.LengthAtLeast(1), | ||
| }, | ||
| }, | ||
| "algorithm": schema.StringAttribute{ | ||
| Description: fmt.Sprintf("The encryption algorithm that the key will use to encrypt data. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedAlgorithmEnumValues)...)), | ||
| Computed: true, | ||
| Validators: []validator.String{ | ||
| stringvalidator.LengthAtLeast(1), | ||
| }, | ||
| }, | ||
| "description": schema.StringAttribute{ | ||
| Description: "A user chosen description to distinguish multiple keys", | ||
| Computed: true, | ||
| Validators: []validator.String{ | ||
| stringvalidator.LengthAtLeast(1), | ||
| }, | ||
| }, | ||
| "display_name": schema.StringAttribute{ | ||
| Description: "The display name to distinguish multiple keys", | ||
| Computed: true, | ||
| Validators: []validator.String{ | ||
| stringvalidator.LengthAtLeast(1), | ||
| }, | ||
| }, | ||
| "id": schema.StringAttribute{ | ||
| Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`keyring_id,`key_id`\".", | ||
| Computed: true, | ||
| }, | ||
| "import_only": schema.BoolAttribute{ | ||
| Description: "States whether versions can be created or only imported.", | ||
| Computed: true, | ||
| }, | ||
| "key_id": schema.StringAttribute{ | ||
| Description: "The ID of the key", | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "keyring_id": schema.StringAttribute{ | ||
| Description: "The ID of the associated key ring", | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "protection": schema.StringAttribute{ | ||
| Description: "The purpose for which the key will be used", | ||
| Computed: true, | ||
| Validators: []validator.String{ | ||
| stringvalidator.LengthAtLeast(1), | ||
| }, | ||
| }, | ||
| "purpose": schema.StringAttribute{ | ||
| Description: fmt.Sprintf("The purpose for which the key will be used. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedPurposeEnumValues)...)), | ||
| Computed: true, | ||
| Validators: []validator.String{ | ||
| stringvalidator.LengthAtLeast(1), | ||
| }, | ||
| }, | ||
| "project_id": schema.StringAttribute{ | ||
| Description: "STACKIT project ID to which the key is associated.", | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "region": schema.StringAttribute{ | ||
| Optional: true, | ||
| // must be computed to allow for storing the override value from the provider | ||
| Computed: true, | ||
| Description: "The resource region. If not defined, the provider region is used.", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (k *keyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform | ||
| var model Model | ||
| diags := req.Config.Get(ctx, &model) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| projectId := model.ProjectId.ValueString() | ||
| keyRingId := model.KeyRingId.ValueString() | ||
| region := k.providerData.GetRegionWithOverride(model.Region) | ||
| keyId := model.KeyId.ValueString() | ||
|
|
||
| ctx = tflog.SetField(ctx, "keyring_id", keyRingId) | ||
| ctx = tflog.SetField(ctx, "project_id", projectId) | ||
| ctx = tflog.SetField(ctx, "region", region) | ||
| ctx = tflog.SetField(ctx, "key_id", keyId) | ||
|
|
||
| keyResponse, err := k.client.GetKey(ctx, projectId, region, keyRingId, keyId).Execute() | ||
| if err != nil { | ||
| utils.LogError( | ||
| ctx, | ||
| &resp.Diagnostics, | ||
| err, | ||
| "Reading key", | ||
| fmt.Sprintf("Key with ID %q does not exist in project %q.", keyId, projectId), | ||
| map[int]string{ | ||
| http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId), | ||
| }, | ||
| ) | ||
| resp.State.RemoveResource(ctx) | ||
| return | ||
| } | ||
|
|
||
| err = mapFields(keyResponse, &model, region) | ||
| if err != nil { | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading key", fmt.Sprintf("Processing API payload: %v", err)) | ||
| return | ||
| } | ||
| diags = resp.State.Set(ctx, model) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| tflog.Info(ctx, "Key read") | ||
| } |
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.