Skip to content

Commit b9f9c11

Browse files
committed
feat(keymanager): add algorithm field to allow custom key algorithms
1 parent 791929a commit b9f9c11

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

internal/services/keymanager/helpers.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,54 @@ func NewKeyManagerAPIWithRegionAndID(m any, id string) (*key_manager.API, scw.Re
5555
return client, region, keyID, nil
5656
}
5757

58-
func ExpandKeyUsage(usage string) *key_manager.KeyUsage {
58+
func ExpandKeyUsage(usage string, algorithm string) *key_manager.KeyUsage {
5959
switch usage {
6060
case "symmetric_encryption":
6161
alg := key_manager.KeyAlgorithmSymmetricEncryptionAes256Gcm
62+
if algorithm != "" {
63+
alg = key_manager.KeyAlgorithmSymmetricEncryption(algorithm)
64+
}
6265

6366
return &key_manager.KeyUsage{SymmetricEncryption: &alg}
6467
case "asymmetric_encryption":
6568
alg := key_manager.KeyAlgorithmAsymmetricEncryptionRsaOaep3072Sha256
69+
if algorithm != "" {
70+
alg = key_manager.KeyAlgorithmAsymmetricEncryption(algorithm)
71+
}
6672

6773
return &key_manager.KeyUsage{AsymmetricEncryption: &alg}
6874
case "asymmetric_signing":
6975
alg := key_manager.KeyAlgorithmAsymmetricSigningEcP256Sha256
76+
if algorithm != "" {
77+
alg = key_manager.KeyAlgorithmAsymmetricSigning(algorithm)
78+
}
7079

7180
return &key_manager.KeyUsage{AsymmetricSigning: &alg}
7281
default:
7382
return nil
7483
}
7584
}
7685

86+
func AlgorithmFromKeyUsage(u *key_manager.KeyUsage) string {
87+
if u == nil {
88+
return ""
89+
}
90+
91+
if u.SymmetricEncryption != nil {
92+
return string(*u.SymmetricEncryption)
93+
}
94+
95+
if u.AsymmetricEncryption != nil {
96+
return string(*u.AsymmetricEncryption)
97+
}
98+
99+
if u.AsymmetricSigning != nil {
100+
return string(*u.AsymmetricSigning)
101+
}
102+
103+
return ""
104+
}
105+
77106
func ExpandKeyRotationPolicy(v any) (*key_manager.KeyRotationPolicy, error) {
78107
list, ok := v.([]any)
79108
if !ok || len(list) == 0 {

internal/services/keymanager/key_resource.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ func ResourceKeyManagerKey() *schema.Resource {
3535
}, false),
3636
Description: "Key usage. Keys with a usage set to 'symmetric_encryption' can encrypt and decrypt data using the AES-256-GCM key algorithm. Possible values: symmetric_encryption, asymmetric_encryption, asymmetric_signing.",
3737
},
38+
"algorithm": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Computed: true,
42+
Description: "Algorithm for the key. If not specified, a default algorithm is chosen based on usage. See Key Manager documentation for supported algorithms.",
43+
},
3844
"description": {
3945
Type: schema.TypeString,
4046
Optional: true,
@@ -116,7 +122,8 @@ func resourceKeyManagerKeyCreate(ctx context.Context, d *schema.ResourceData, m
116122
createReq.Origin = key_manager.KeyOrigin(v.(string))
117123
}
118124

119-
createReq.Usage = ExpandKeyUsage(d.Get("usage").(string))
125+
algorithm := d.Get("algorithm").(string)
126+
createReq.Usage = ExpandKeyUsage(d.Get("usage").(string), algorithm)
120127

121128
key, err := api.CreateKey(createReq)
122129
if err != nil {
@@ -146,6 +153,7 @@ func resourceKeyManagerKeyRead(ctx context.Context, d *schema.ResourceData, m an
146153
_ = d.Set("project_id", key.ProjectID)
147154
_ = d.Set("region", key.Region.String())
148155
_ = d.Set("usage", UsageToString(key.Usage))
156+
_ = d.Set("algorithm", AlgorithmFromKeyUsage(key.Usage))
149157
_ = d.Set("description", key.Description)
150158
_ = d.Set("tags", key.Tags)
151159
_ = d.Set("rotation_count", int(key.RotationCount))

0 commit comments

Comments
 (0)