Skip to content

Commit 5e8c7a7

Browse files
authored
feat(kms): add key resource and datasource (#1055)
relates to STACKITTPR-411
1 parent b5f82e7 commit 5e8c7a7

File tree

13 files changed

+1369
-3
lines changed

13 files changed

+1369
-3
lines changed

docs/data-sources/kms_key.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_kms_key Data Source - stackit"
4+
subcategory: ""
5+
description: |-
6+
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.
7+
---
8+
9+
# stackit_kms_key (Data Source)
10+
11+
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.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "stackit_kms_key" "key" {
17+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
18+
keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
19+
key_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
20+
}
21+
```
22+
23+
<!-- schema generated by tfplugindocs -->
24+
## Schema
25+
26+
### Required
27+
28+
- `key_id` (String) The ID of the key
29+
- `keyring_id` (String) The ID of the associated key ring
30+
- `project_id` (String) STACKIT project ID to which the key is associated.
31+
32+
### Optional
33+
34+
- `region` (String) The resource region. If not defined, the provider region is used.
35+
36+
### Read-Only
37+
38+
- `access_scope` (String) The access scope of the key. Default is `PUBLIC`. Possible values are: `PUBLIC`, `SNA`.
39+
- `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`.
40+
- `description` (String) A user chosen description to distinguish multiple keys
41+
- `display_name` (String) The display name to distinguish multiple keys
42+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`keyring_id`,`key_id`".
43+
- `import_only` (Boolean) States whether versions can be created or only imported.
44+
- `protection` (String) The underlying system that is responsible for protecting the key material. Possible values are: `software`.
45+
- `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`.

docs/resources/kms_key.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_kms_key Resource - stackit"
4+
subcategory: ""
5+
description: |-
6+
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.
7+
~> 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.
8+
---
9+
10+
# stackit_kms_key (Resource)
11+
12+
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.
13+
14+
~> 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.**
15+
16+
## Example Usage
17+
18+
```terraform
19+
resource "stackit_kms_key" "key" {
20+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21+
keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
22+
display_name = "key-01"
23+
protection = "software"
24+
algorithm = "aes_256_gcm"
25+
purpose = "symmetric_encrypt_decrypt"
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Required
33+
34+
- `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`.
35+
- `display_name` (String) The display name to distinguish multiple keys
36+
- `keyring_id` (String) The ID of the associated keyring
37+
- `project_id` (String) STACKIT project ID to which the key is associated.
38+
- `protection` (String) The underlying system that is responsible for protecting the key material. Possible values are: `software`.
39+
- `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`.
40+
41+
### Optional
42+
43+
- `access_scope` (String) The access scope of the key. Default is `PUBLIC`. Possible values are: `PUBLIC`, `SNA`.
44+
- `description` (String) A user chosen description to distinguish multiple keys
45+
- `import_only` (Boolean) States whether versions can be created or only imported.
46+
- `region` (String) The resource region. If not defined, the provider region is used.
47+
48+
### Read-Only
49+
50+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`keyring_id`,`key_id`".
51+
- `key_id` (String) The ID of the key
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "stackit_kms_key" "key" {
2+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3+
keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
4+
key_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "stackit_kms_key" "key" {
2+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3+
keyring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
4+
display_name = "key-01"
5+
protection = "software"
6+
algorithm = "aes_256_gcm"
7+
purpose = "symmetric_encrypt_decrypt"
8+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/hashicorp/terraform-plugin-go v0.29.0
1212
github.com/hashicorp/terraform-plugin-log v0.10.0
1313
github.com/hashicorp/terraform-plugin-testing v1.13.3
14-
github.com/stackitcloud/stackit-sdk-go/core v0.17.3
14+
github.com/stackitcloud/stackit-sdk-go/core v0.19.0
1515
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.6.0
1616
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.1
1717
github.com/stackitcloud/stackit-sdk-go/services/git v0.8.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN
152152
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
153153
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
154154
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
155-
github.com/stackitcloud/stackit-sdk-go/core v0.17.3 h1:GsZGmRRc/3GJLmCUnsZswirr5wfLRrwavbnL/renOqg=
156-
github.com/stackitcloud/stackit-sdk-go/core v0.17.3/go.mod h1:HBCXJGPgdRulplDzhrmwC+Dak9B/x0nzNtmOpu+1Ahg=
155+
github.com/stackitcloud/stackit-sdk-go/core v0.19.0 h1:dtJcs6/TTCzzb2RKI7HJugDrbCkaFEDmn1pOeFe8qnI=
156+
github.com/stackitcloud/stackit-sdk-go/core v0.19.0/go.mod h1:fqto7M82ynGhEnpZU6VkQKYWYoFG5goC076JWXTUPRQ=
157157
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.9.0 h1:7ZKd3b+E/R4TEVShLTXxx5FrsuDuJBOyuVOuKTMa4mo=
158158
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.9.0/go.mod h1:/FoXa6hF77Gv8brrvLBCKa5ie1Xy9xn39yfHwaln9Tw=
159159
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.6.0 h1:Q+qIdejeMsYMkbtVoI9BpGlKGdSVFRBhH/zj44SP8TM=
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package kms
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
12+
"github.com/hashicorp/terraform-plugin-log/tflog"
13+
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
14+
"github.com/stackitcloud/stackit-sdk-go/services/kms"
15+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
16+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
17+
kmsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/kms/utils"
18+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
19+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
20+
)
21+
22+
var (
23+
_ datasource.DataSource = &keyDataSource{}
24+
)
25+
26+
func NewKeyDataSource() datasource.DataSource {
27+
return &keyDataSource{}
28+
}
29+
30+
type keyDataSource struct {
31+
client *kms.APIClient
32+
providerData core.ProviderData
33+
}
34+
35+
func (k *keyDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
36+
resp.TypeName = req.ProviderTypeName + "_kms_key"
37+
}
38+
39+
func (k *keyDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
40+
var ok bool
41+
k.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
42+
if !ok {
43+
return
44+
}
45+
46+
k.client = kmsUtils.ConfigureClient(ctx, &k.providerData, &resp.Diagnostics)
47+
if resp.Diagnostics.HasError() {
48+
return
49+
}
50+
51+
tflog.Info(ctx, "KMS client configured")
52+
}
53+
54+
func (k *keyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
55+
resp.Schema = schema.Schema{
56+
Description: fmt.Sprintf("KMS Key datasource schema. %s", core.DatasourceRegionFallbackDocstring),
57+
Attributes: map[string]schema.Attribute{
58+
"access_scope": schema.StringAttribute{
59+
Description: fmt.Sprintf("The access scope of the key. Default is `%s`. %s", string(kms.ACCESSSCOPE_PUBLIC), utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedAccessScopeEnumValues)...)),
60+
Computed: true,
61+
Validators: []validator.String{
62+
stringvalidator.LengthAtLeast(1),
63+
},
64+
},
65+
"algorithm": schema.StringAttribute{
66+
Description: fmt.Sprintf("The encryption algorithm that the key will use to encrypt data. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedAlgorithmEnumValues)...)),
67+
Computed: true,
68+
Validators: []validator.String{
69+
stringvalidator.LengthAtLeast(1),
70+
},
71+
},
72+
"description": schema.StringAttribute{
73+
Description: "A user chosen description to distinguish multiple keys",
74+
Computed: true,
75+
Validators: []validator.String{
76+
stringvalidator.LengthAtLeast(1),
77+
},
78+
},
79+
"display_name": schema.StringAttribute{
80+
Description: "The display name to distinguish multiple keys",
81+
Computed: true,
82+
Validators: []validator.String{
83+
stringvalidator.LengthAtLeast(1),
84+
},
85+
},
86+
"id": schema.StringAttribute{
87+
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`keyring_id`,`key_id`\".",
88+
Computed: true,
89+
},
90+
"import_only": schema.BoolAttribute{
91+
Description: "States whether versions can be created or only imported.",
92+
Computed: true,
93+
},
94+
"key_id": schema.StringAttribute{
95+
Description: "The ID of the key",
96+
Required: true,
97+
Validators: []validator.String{
98+
validate.UUID(),
99+
validate.NoSeparator(),
100+
},
101+
},
102+
"keyring_id": schema.StringAttribute{
103+
Description: "The ID of the associated key ring",
104+
Required: true,
105+
Validators: []validator.String{
106+
validate.UUID(),
107+
validate.NoSeparator(),
108+
},
109+
},
110+
"protection": schema.StringAttribute{
111+
Description: fmt.Sprintf("The underlying system that is responsible for protecting the key material. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedProtectionEnumValues)...)),
112+
Computed: true,
113+
Validators: []validator.String{
114+
stringvalidator.LengthAtLeast(1),
115+
},
116+
},
117+
"purpose": schema.StringAttribute{
118+
Description: fmt.Sprintf("The purpose for which the key will be used. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedPurposeEnumValues)...)),
119+
Computed: true,
120+
Validators: []validator.String{
121+
stringvalidator.LengthAtLeast(1),
122+
},
123+
},
124+
"project_id": schema.StringAttribute{
125+
Description: "STACKIT project ID to which the key is associated.",
126+
Required: true,
127+
Validators: []validator.String{
128+
validate.UUID(),
129+
validate.NoSeparator(),
130+
},
131+
},
132+
"region": schema.StringAttribute{
133+
Optional: true,
134+
// must be computed to allow for storing the override value from the provider
135+
Computed: true,
136+
Description: "The resource region. If not defined, the provider region is used.",
137+
},
138+
},
139+
}
140+
}
141+
142+
func (k *keyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
143+
var model Model
144+
diags := req.Config.Get(ctx, &model)
145+
resp.Diagnostics.Append(diags...)
146+
if resp.Diagnostics.HasError() {
147+
return
148+
}
149+
150+
projectId := model.ProjectId.ValueString()
151+
keyRingId := model.KeyRingId.ValueString()
152+
region := k.providerData.GetRegionWithOverride(model.Region)
153+
keyId := model.KeyId.ValueString()
154+
155+
ctx = tflog.SetField(ctx, "keyring_id", keyRingId)
156+
ctx = tflog.SetField(ctx, "project_id", projectId)
157+
ctx = tflog.SetField(ctx, "region", region)
158+
ctx = tflog.SetField(ctx, "key_id", keyId)
159+
160+
keyResponse, err := k.client.GetKey(ctx, projectId, region, keyRingId, keyId).Execute()
161+
if err != nil {
162+
utils.LogError(
163+
ctx,
164+
&resp.Diagnostics,
165+
err,
166+
"Reading key",
167+
fmt.Sprintf("Key with ID %q does not exist in project %q.", keyId, projectId),
168+
map[int]string{
169+
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
170+
},
171+
)
172+
resp.State.RemoveResource(ctx)
173+
return
174+
}
175+
176+
err = mapFields(keyResponse, &model, region)
177+
if err != nil {
178+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading key", fmt.Sprintf("Processing API payload: %v", err))
179+
return
180+
}
181+
diags = resp.State.Set(ctx, model)
182+
resp.Diagnostics.Append(diags...)
183+
if resp.Diagnostics.HasError() {
184+
return
185+
}
186+
tflog.Info(ctx, "Key read")
187+
}

0 commit comments

Comments
 (0)