Skip to content

Commit 8d1f751

Browse files
Merge pull request #65 from fortanix/EXTREQ-1005
Support for rotation policy:
2 parents a156159 + be412ab commit 8d1f751

File tree

7 files changed

+162
-1
lines changed

7 files changed

+162
-1
lines changed

docs/resources/dsm_aws_sobject.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ resource "dsm_aws_sobject" "sobject" {
2424
custom_metadata = {
2525
aws-aliases = <alias-to-use>
2626
}
27+
rotation_policy = {
28+
interval_days = <number of days>
29+
effective_at = "<yyyymmddThhmmssZ>"
30+
}
2731
}
2832
```
2933

@@ -50,6 +54,21 @@ The following arguments are supported in the `dsm_aws_sobject` resource block:
5054
* _**custom\_metadata (optional)**_: Contains metadata about an AWS KMS key
5155
* **aws-aliases** – The display name for AWS KMS key used to identify the key.
5256
* **aws-policy** - JSON format of AWS policy that should be enforced for the key.
57+
* * _**rotation_policy(optional)**_ = Policy to rotate a Security Object, configure the below parameters.
58+
* * _**interval_days**_ = Rotate the key for every given number of days
59+
* * _**interval_weeks**_ = Rotate the key for every given number of weeks
60+
* * _**interval_months**_ = Rotate the key for every given number of months
61+
* * _**interval_years**_ = Rotate the key for every given number of years
62+
* * _**effective_at**_ = Start of the rotation policy time
63+
64+
## Note on rotational_policy
65+
66+
Only one of the following attributes should be used while configuring the interval in rotational_policy
67+
1. interval_days
68+
2. interval_weeks
69+
3. interval_months
70+
4. interval_years
71+
5372

5473
## Attribute Reference
5574

@@ -76,3 +95,4 @@ The following attributes are stored in the `dsm_aws_sobject` resource block:
7695
* **key\_size**: The size of the security object
7796
* **description**: The security object description
7897
* **expiry\_date**: The security object expiry date in RFC format from Fortanix DSM
98+
* * _**rotation_\_policy**_ = Policy to rotate a Security Object

docs/resources/dsm_azure_sobject.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ resource "dsm_azure_sobject" "sobject" {
2323
azure_key_state = <azure_key_state>
2424
azure-key-name = <azure_key_name>
2525
}
26+
rotation_policy = {
27+
interval_days = <number of days>
28+
effective_at = "<yyyymmddThhmmssZ>"
29+
deactivate_rotated_key = <true/false>
30+
}
2631
}
2732
```
2833

@@ -43,6 +48,21 @@ The following arguments are supported in the `dsm_azure_sobject` resource block:
4348
* _**custom\_metadata (optional)**_: Azure CMK level metadata information
4449
* **azure-key-state** – Key state within Azure KV
4550
* **azure-key-name** - Key name within Azure KV
51+
* _**rotation_policy(optional)**_ = Policy to rotate a Security Object, configure the below parameters.
52+
* * _**interval_days**_ = Rotate the key for every given number of days
53+
* * _**interval_weeks**_ = Rotate the key for every given number of weeks
54+
* * _**interval_months**_ = Rotate the key for every given number of months
55+
* * _**interval_years**_ = Rotate the key for every given number of years
56+
* * _**effective_at**_ = Start of the rotation policy time
57+
* * _**deactivate_rotated_key**_ = Deactivate original key after rotation (true/false)
58+
59+
## Note on rotational_policy
60+
61+
Only one of the following attributes should be used while configuring the interval in rotational_policy
62+
1. interval_days
63+
2. interval_weeks
64+
3. interval_months
65+
4. interval_years
4666

4767
## Attribute Reference
4868

@@ -66,3 +86,4 @@ The following attributes are stored in the `dsm_azure_sobject` resource block:
6686
* _**custom\_metadata (optional)**_: Azure CMK level metadata information
6787
* **azure-key-state** – Key state within Azure KV
6888
* **azure-key-name** - Key name within Azure KV
89+
* _**rotation\_policy**_ = Policy to rotate a Security Object

docs/resources/dsm_sobject.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ resource "dsm_sobject" "sobject" {
3030
value = <imported sobject content>
3131
hash_alg = <HashAlgorithm>
3232
subgroup_size = <subgroup_size_value>
33+
rotation_policy = {
34+
interval_days = <number of days>
35+
effective_at = "<yyyymmddThhmmssZ>"
36+
deactivate_rotated_key = <true/false>
37+
rotate_copied_keys = "all_external"
38+
}
3339
}
3440
```
3541

@@ -58,6 +64,24 @@ The following arguments are supported in the `dsm_sobject` resource block:
5864
* _**allowed\_missing\_justifications (optional)**_: The security object allows missing justifications even if not provided.
5965
* _**hash\_alg**_ = Hashing Algorithm for KCDSA and ECKCDSA
6066
* _**subgroup\_size**_ = Subgroup Size for DSA and ECKCDSA
67+
* _**rotation_policy(optional)**_ = Policy to rotate a Security Object, configure the below parameters.
68+
* * _**interval_days**_ = Rotate the key for every given number of days
69+
* * _**interval_weeks**_ = Rotate the key for every given number of weeks
70+
* * _**interval_months**_ = Rotate the key for every given number of months
71+
* * _**interval_years**_ = Rotate the key for every given number of years
72+
* * _**effective_at**_ = Start of the rotation policy time
73+
* * _**deactivate_rotated_key**_ = Deactivate original key after rotation (true/false)
74+
* * _**rotate_copied_keys**_ = Enable key rotation for copied keys
75+
76+
## Note on rotational_policy
77+
78+
Only one of the following attributes should be used while configuring the interval in rotational_policy
79+
1. interval_days
80+
2. interval_weeks
81+
3. interval_months
82+
4. interval_years
83+
84+
6185

6286
## Attribute Reference
6387

@@ -89,4 +113,5 @@ The following attributes are stored in the `dsm_sobject` resource block:
89113
* _**allowed\_missing\_justifications (optional)**_: Boolean value which allows missing justifications even if not provided to the security object. The values are `True` / `False`.
90114

91115
* _**hash\_alg**_ = Hashing Algorithm for KCDSA and ECKCDSA. The allowed Hashing Algorithms are `SHA1`,`SHA224`, `SHA256`, `SHA384`, `SHA521`.
92-
* _**subgroup\_size**_ = Subgroup Size for DSA and ECKCDSA. The allowed Subgroup Sizes are `224` and `256`
116+
* _**subgroup\_size**_ = Subgroup Size for DSA and ECKCDSA. The allowed Subgroup Sizes are `224` and `256`
117+
* _**rotation\_policy**_ = Policy to rotate a security object

dsm/common.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"encoding/json"
1616
//"encoding/pem"
1717
"fmt"
18+
"strconv"
1819

1920
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
2021
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -152,3 +153,44 @@ func substr(input string, start int, length int) string {
152153

153154
return string(asRunes[start : start+length])
154155
}
156+
157+
// To write/update the security object rotation_policy
158+
func sobj_rotation_policy_write(rp map[string]interface{}) map[string]interface{} {
159+
rotation_policy := make(map[string]interface{})
160+
for k, v := range rp{
161+
/* while sending the request, interval_days should be assigned as an integer.
162+
Hence it is converted to integer from the string.
163+
*/
164+
if k == "interval_days" {
165+
val, _ := strconv.Atoi(v.(string))
166+
rotation_policy[k] = val
167+
} else if k == "deactivate_rotated_key" {
168+
/* while sending the request, deactivate_rotated_key should be assigned as a boolean..
169+
Hence it is converted to boolean from the string.
170+
*/
171+
val, _ := strconv.ParseBool(v.(string))
172+
rotation_policy[k] = val
173+
} else {
174+
rotation_policy[k] = v
175+
}
176+
}
177+
return rotation_policy
178+
}
179+
180+
// To read the security object rotation_policy
181+
func sobj_rotation_policy_read(rp map[string]interface{}) map[string]interface{} {
182+
rotation_policy := make(map[string]interface{})
183+
for k, v := range rp{
184+
/* while reading the rotation_policy from terraform the interval_days attribute is assigned as float64 datatype.
185+
Hence it will be converted to string from float object.
186+
*/
187+
if k == "interval_days" {
188+
rotation_policy[k] = strconv.FormatFloat(v.(float64), 'f', -1, 64)
189+
} else if k == "deactivate_rotated_key" {
190+
rotation_policy[k] = strconv.FormatBool(v.(bool))
191+
} else {
192+
rotation_policy[k] = v
193+
}
194+
}
195+
return rotation_policy
196+
}

dsm/resource_aws_sobject.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ func resourceAWSSobject() *schema.Resource {
9090
Type: schema.TypeString,
9191
},
9292
},
93+
"rotation_policy": {
94+
Type: schema.TypeMap,
95+
Optional: true,
96+
Elem: &schema.Schema{
97+
Type: schema.TypeString,
98+
},
99+
},
93100
"custom_metadata": {
94101
Type: schema.TypeMap,
95102
Optional: true,
@@ -203,6 +210,9 @@ func resourceCreateAWSSobject(ctx context.Context, d *schema.ResourceData, m int
203210
if err := d.Get("custom_metadata").(map[string]interface{}); len(err) > 0 {
204211
security_object["custom_metadata"] = d.Get("custom_metadata")
205212
}
213+
if rotation_policy := d.Get("rotation_policy").(map[string]interface{}); len(rotation_policy) > 0 {
214+
security_object["rotation_policy"] = sobj_rotation_policy_write(rotation_policy)
215+
}
206216

207217
// FYOO: Get tags
208218
if err := d.Get("aws_tags").(map[string]interface{}); len(err) > 0 {
@@ -355,6 +365,12 @@ func resourceReadAWSSobject(ctx context.Context, d *schema.ResourceData, m inter
355365
return diag.FromErr(newerr)
356366
}
357367
}
368+
if _, ok := req["rotation_policy"]; ok {
369+
rotation_policy := sobj_rotation_policy_read(req["rotation_policy"].(map[string]interface{}))
370+
if err := d.Set("rotation_policy", rotation_policy); err != nil {
371+
return diag.FromErr(err)
372+
}
373+
}
358374
// FYOO: clear values that are irrelevant
359375
d.Set("rotate", "")
360376
d.Set("rotate_from", "")

dsm/resource_azure_sobject.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ func resourceAzureSobject() *schema.Resource {
6363
Type: schema.TypeString,
6464
},
6565
},
66+
"rotation_policy": {
67+
Type: schema.TypeMap,
68+
Optional: true,
69+
Elem: &schema.Schema{
70+
Type: schema.TypeString,
71+
},
72+
},
6673
"custom_metadata": {
6774
Type: schema.TypeMap,
6875
Optional: true,
@@ -140,6 +147,9 @@ func resourceCreateAzureSobject(ctx context.Context, d *schema.ResourceData, m i
140147
if err := d.Get("custom_metadata").(map[string]interface{}); len(err) > 0 {
141148
security_object["custom_metadata"] = d.Get("custom_metadata")
142149
}
150+
if rotation_policy := d.Get("rotation_policy").(map[string]interface{}); len(rotation_policy) > 0 {
151+
security_object["rotation_policy"] = sobj_rotation_policy_write(rotation_policy)
152+
}
143153

144154
req, err := m.(*api_client).APICallBody("POST", "crypto/v1/keys/copy", security_object)
145155
if err != nil {
@@ -254,6 +264,12 @@ func resourceReadAzureSobject(ctx context.Context, d *schema.ResourceData, m int
254264
return diag.FromErr(newerr)
255265
}
256266
}
267+
if _, ok := req["rotation_policy"]; ok {
268+
rotation_policy := sobj_rotation_policy_read(req["rotation_policy"].(map[string]interface{}))
269+
if err := d.Set("rotation_policy", rotation_policy); err != nil {
270+
return diag.FromErr(err)
271+
}
272+
}
257273
}
258274

259275
return nil

dsm/resource_sobject.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ func resourceSobject() *schema.Resource {
6969
Type: schema.TypeString,
7070
},
7171
},
72+
"rotation_policy": {
73+
Type: schema.TypeMap,
74+
Optional: true,
75+
Elem: &schema.Schema{
76+
Type: schema.TypeString,
77+
},
78+
},
7279
// Unable to define links
7380
//"links": {
7481
// Type: schema.TypeMap,
@@ -305,6 +312,9 @@ func createSO(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
305312
if err := d.Get("custom_metadata").(map[string]interface{}); len(err) > 0 {
306313
security_object["custom_metadata"] = err
307314
}
315+
if rotation_policy := d.Get("rotation_policy").(map[string]interface{}); len(rotation_policy) > 0 {
316+
security_object["rotation_policy"] = sobj_rotation_policy_write(rotation_policy)
317+
}
308318

309319
if len(hash_alg) > 0 && obj_type == "KCDSA" {
310320
kcdsa := make(map[string]interface{})
@@ -556,6 +566,12 @@ func resourceReadSobject(ctx context.Context, d *schema.ResourceData, m interfac
556566
}
557567
}
558568
}
569+
if _, ok := req["rotation_policy"]; ok {
570+
rotation_policy := sobj_rotation_policy_read(req["rotation_policy"].(map[string]interface{}))
571+
if err := d.Set("rotation_policy", rotation_policy); err != nil {
572+
return diag.FromErr(err)
573+
}
574+
}
559575

560576
// FYOO: clear values that are irrelevant
561577
d.Set("rotate", "")
@@ -619,6 +635,11 @@ func resourceUpdateSobject(ctx context.Context, d *schema.ResourceData, m interf
619635
security_object["custom_metadata"] = d.Get("custom_metadata").(map[string]interface{})
620636
has_changed = true
621637
}
638+
if d.HasChange("rotation_policy") {
639+
rotation_policy := d.Get("rotation_policy").(map[string]interface{})
640+
security_object["rotation_policy"] = sobj_rotation_policy_write(rotation_policy)
641+
has_changed = true
642+
}
622643
if d.HasChange("hash_alg") {
623644
old_ha, new_ha := d.GetChange("hash_alg")
624645
d.Set("hash_alg", old_ha)

0 commit comments

Comments
 (0)