Skip to content

Commit 3766e34

Browse files
mmrqsMélanie Marquesremyleone
authored
fix(key_manager): fix the base64 encoding and decoding (#3884)
Co-authored-by: Mélanie Marques <[email protected]> Co-authored-by: Rémy Léone <[email protected]>
1 parent 325f5a1 commit 3766e34

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

cmd/scw/testdata/test-all-usage-keymanager-key-decrypt-usage.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ USAGE:
77

88
ARGS:
99
key-id ID of the key to decrypt
10-
[ciphertext] Ciphertext data to decrypt
10+
ciphertext Base64 Ciphertext data to decrypt (Support file loading with @/path/to/file)
1111
[associated-data] (Optional) Additional authenticated data
1212
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
1313

cmd/scw/testdata/test-all-usage-keymanager-key-encrypt-usage.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ USAGE:
77

88
ARGS:
99
key-id ID of the key to encrypt
10-
[plaintext] Plaintext data to encrypt
10+
plaintext Base64 Plaintext data to encrypt (Support file loading with @/path/to/file)
1111
[associated-data] (Optional) Additional authenticated data
1212
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
1313

docs/commands/keymanager.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ scw keymanager key decrypt [arg=value ...]
6666
| Name | | Description |
6767
|------|---|-------------|
6868
| key-id | Required | ID of the key to decrypt |
69-
| ciphertext | | Ciphertext data to decrypt |
69+
| ciphertext | Required | Base64 Ciphertext data to decrypt |
7070
| associated-data | | (Optional) Additional authenticated data |
7171
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |
7272

@@ -148,7 +148,7 @@ scw keymanager key encrypt [arg=value ...]
148148
| Name | | Description |
149149
|------|---|-------------|
150150
| key-id | Required | ID of the key to encrypt |
151-
| plaintext | | Plaintext data to encrypt |
151+
| plaintext | Required | Base64 Plaintext data to encrypt |
152152
| associated-data | | (Optional) Additional authenticated data |
153153
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |
154154

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
11
package key_manager
22

3-
import "github.com/scaleway/scaleway-cli/v2/internal/core"
3+
import (
4+
"context"
5+
"encoding/base64"
6+
7+
"github.com/scaleway/scaleway-cli/v2/internal/core"
8+
key_manager "github.com/scaleway/scaleway-sdk-go/api/key_manager/v1alpha1"
9+
)
410

511
func GetCommands() *core.Commands {
6-
return GetGeneratedCommands()
12+
cmds := GetGeneratedCommands()
13+
14+
cmds.MustFind("keymanager", "key", "decrypt").Override(cipherDecrypt)
15+
cmds.MustFind("keymanager", "key", "encrypt").Override(plaintextEncrypt)
16+
17+
return cmds
18+
}
19+
20+
func plaintextEncrypt(c *core.Command) *core.Command {
21+
*c.ArgSpecs.GetByName("plaintext") = core.ArgSpec{
22+
Name: "plaintext",
23+
Short: "Base64 Plaintext data to encrypt",
24+
Required: true,
25+
CanLoadFile: true,
26+
}
27+
28+
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
29+
args := argsI.(*key_manager.EncryptRequest)
30+
31+
p, err := base64.StdEncoding.DecodeString(string(args.Plaintext))
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
args.Plaintext = p
37+
38+
return runner(ctx, args)
39+
}
40+
41+
return c
42+
}
43+
44+
func cipherDecrypt(c *core.Command) *core.Command {
45+
*c.ArgSpecs.GetByName("ciphertext") = core.ArgSpec{
46+
Name: "ciphertext",
47+
Short: "Base64 Ciphertext data to decrypt",
48+
Required: true,
49+
CanLoadFile: true,
50+
}
51+
52+
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
53+
args := argsI.(*key_manager.DecryptRequest)
54+
55+
c, err := base64.StdEncoding.DecodeString(string(args.Ciphertext))
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
args.Ciphertext = c
61+
62+
return runner(ctx, args)
63+
}
64+
65+
return c
766
}

internal/namespaces/key_manager/v1alpha1/key_manager_cli.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ func keymanagerKeyEncrypt() *core.Command {
540540
},
541541
{
542542
Name: "plaintext",
543-
Short: `Plaintext data to encrypt`,
543+
Short: `Base64 Plaintext data to encrypt`,
544544
Required: false,
545545
Deprecated: false,
546546
Positional: false,
@@ -584,7 +584,7 @@ func keymanagerKeyDecrypt() *core.Command {
584584
},
585585
{
586586
Name: "ciphertext",
587-
Short: `Ciphertext data to decrypt`,
587+
Short: `Base64 Ciphertext data to decrypt`,
588588
Required: false,
589589
Deprecated: false,
590590
Positional: false,

0 commit comments

Comments
 (0)