Skip to content

Commit ceb2412

Browse files
lucy-fanBrian Strauchtadsul
authored
[CLI-1430] Weak encryption implementation (#1170)
* Update to AES GCM 256 and crypto/rand for encryption * Change back to string iv * Add backwards compatability * Code review Co-authored-by: Brian Strauch <bstrauch@confluent.io> * Add iv length constant * Code review * Fix tests * Code review * Remove padding from GCM mode * Add support for feature based cipher mode (#1196) * Add support for feature based cipher mode * Apply suggestions from code review Co-authored-by: Brian Strauch <bstrauch@confluent.io> * Code review; add default to Cipher struct * Update Cipher constructor Co-authored-by: Brian Strauch <bstrauch@confluent.io> Co-authored-by: Brian Strauch <bstrauch@confluent.io> Co-authored-by: tadsul <43974298+tadsul@users.noreply.github.com>
1 parent a6f885c commit ceb2412

15 files changed

+399
-240
lines changed

internal/cmd/secret/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
type command struct {
11-
*pcmd.CLICommand
11+
*pcmd.AuthenticatedStateFlagCommand
1212
flagResolver pcmd.FlagResolver
1313
plugin secret.PasswordProtection
1414
}
@@ -21,7 +21,7 @@ func New(prerunner pcmd.PreRunner, flagResolver pcmd.FlagResolver, plugin secret
2121
}
2222

2323
c := &command{
24-
CLICommand: pcmd.NewAnonymousCLICommand(cmd, prerunner),
24+
AuthenticatedStateFlagCommand: pcmd.NewAuthenticatedWithMDSStateFlagCommand(cmd, prerunner),
2525
flagResolver: flagResolver,
2626
plugin: plugin,
2727
}

internal/cmd/secret/command_file.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package secret
22

33
import (
4+
"context"
5+
"github.com/confluentinc/cli/internal/pkg/secret"
46
"github.com/spf13/cobra"
7+
"net/http"
8+
"os"
59

610
pcmd "github.com/confluentinc/cli/internal/pkg/cmd"
11+
mds "github.com/confluentinc/mds-sdk-go/mdsv1"
712
"github.com/confluentinc/cli/internal/pkg/errors"
813
)
914

@@ -55,3 +60,21 @@ func (c *command) getConfigs(configSource string, inputType string, prompt strin
5560
}
5661
return newConfigs, nil
5762
}
63+
64+
func (c *command) getCipherMode() string {
65+
if os.Getenv("XX_SECRETS_GCM_MODE") != "" {
66+
return secret.AES_GCM
67+
}
68+
69+
ctx := context.WithValue(context.Background(), mds.ContextAccessToken, c.State.AuthToken)
70+
featureInfo, response, err := c.MDSClient.MetadataServiceOperationsApi.Features(ctx)
71+
72+
if err != nil || response.StatusCode == http.StatusNotFound {
73+
return secret.AES_CBC
74+
}
75+
76+
if _, ok := featureInfo.Features[secret.MdsFeatureCipherFlag]; ok {
77+
return secret.AES_GCM
78+
}
79+
return secret.AES_CBC
80+
}

internal/cmd/secret/command_file_add.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ func (c *command) add(cmd *cobra.Command, _ []string) error {
4444
return err
4545
}
4646

47+
cipherMode := c.getCipherMode()
48+
c.plugin.SetCipherMode(cipherMode)
4749
return c.plugin.AddEncryptedPasswords(configPath, localSecretsPath, remoteSecretsPath, newConfigs)
4850
}

internal/cmd/secret/command_file_decrypt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,8 @@ func (c *command) decrypt(cmd *cobra.Command, _ []string) error {
4848
return err
4949
}
5050

51+
cipherMode := c.getCipherMode()
52+
c.plugin.SetCipherMode(cipherMode)
53+
5154
return c.plugin.DecryptConfigFileSecrets(configPath, localSecretsPath, outputPath, configs)
5255
}

internal/cmd/secret/command_file_encrypt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@ func (c *command) encrypt(cmd *cobra.Command, _ []string) error {
3838
return err
3939
}
4040

41+
cipherMode := c.getCipherMode()
42+
c.plugin.SetCipherMode(cipherMode)
43+
4144
return c.plugin.EncryptConfigFileSecrets(configPath, localSecretsPath, remoteSecretsPath, configs)
4245
}

internal/cmd/secret/command_file_remove.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func (c *command) remove(cmd *cobra.Command, _ []string) error {
4747
return err
4848
}
4949

50+
cipherMode := c.getCipherMode()
51+
c.plugin.SetCipherMode(cipherMode)
52+
5053
if err := c.plugin.RemoveEncryptedPasswords(configPath, localSecretsPath, removeConfigs); err != nil {
5154
return err
5255
}

internal/cmd/secret/command_file_rotate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ func (c *command) rotate(cmd *cobra.Command, _ []string) error {
4141
return err
4242
}
4343

44+
cipherMode := c.getCipherMode()
45+
c.plugin.SetCipherMode(cipherMode)
46+
4447
if rotateMEK {
4548
oldPassphraseSource, err := cmd.Flags().GetString("passphrase")
4649
if err != nil {

internal/cmd/secret/command_file_update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func (c *command) update(cmd *cobra.Command, _ []string) error {
4646
return err
4747
}
4848

49+
cipherMode := c.getCipherMode()
50+
c.plugin.SetCipherMode(cipherMode)
51+
4952
if err := c.plugin.UpdateEncryptedPasswords(configPath, localSecretsPath, remoteSecretsPath, newConfigs); err != nil {
5053
return err
5154
}

internal/cmd/secret/command_master_key_generate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func (c *command) generate(cmd *cobra.Command, _ []string) error {
5151
return err
5252
}
5353

54+
cipherMode := c.getCipherMode()
55+
c.plugin.SetCipherMode(cipherMode)
56+
5457
masterKey, err := c.plugin.CreateMasterKey(passphrase, localSecretsPath)
5558
if err != nil {
5659
return err

internal/pkg/errors/error_message.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ const (
303303
InvalidJSONFileFormatErrorMsg = "invalid json file format"
304304
InvalidFilePathErrorMsg = "invalid file path \"%s\""
305305
UnsupportedFileFormatErrorMsg = "unsupported file format for file \"%s\""
306+
InvalidAlgorithmErrorMsg = "invalid algorithm \"%s\""
306307

307308
// sso package
308309
StartHTTPServerErrorMsg = "unable to start HTTP server"

0 commit comments

Comments
 (0)