Skip to content

Commit

Permalink
fixup! Config: Add -edit and encryption upgrade to cmd/config
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 15, 2024
1 parent f447b18 commit 2b8ca24
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
16 changes: 7 additions & 9 deletions config/config_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
var (
errAESBlockSize = errors.New("config file data is too small for the AES required block size")
errNoPrefix = errors.New("data does not start with Encryption Prefix")
errKeyIsEmpty = errors.New("key is empty")

// encryptionPrefix is a prefix to tell us the file is encrypted
encryptionPrefix = []byte("THORS-HAMMER")
Expand Down Expand Up @@ -79,8 +80,7 @@ func PromptForConfigKey(confirmKey bool) ([]byte, error) {
return nil, errors.New("No key entered")
}

// EncryptConfigFile encrypts configuration data that is parsed in with a key
// and returns it as a byte array with an error
// EncryptConfigFile encrypts json config data with a key
func EncryptConfigFile(configData, key []byte) ([]byte, error) {
sessionDK, salt, err := makeNewSessionDK(key)
if err != nil {
Expand All @@ -93,10 +93,10 @@ func EncryptConfigFile(configData, key []byte) ([]byte, error) {
return c.encryptConfigFile(configData)
}

// encryptConfigFile encrypts configuration data that is passed in with a key
// encryptConfigFile encrypts json config data with a key
// The EncryptConfig field is set to config enabled (1)
func (c *Config) encryptConfigFile(configData []byte) ([]byte, error) {
configData, err := jsonparser.Set(configData, []byte("1"), "EncryptConfig")
configData, err := jsonparser.Set(configData, []byte("1"), "encryptConfig")
if err != nil {
return nil, fmt.Errorf("error setting EncryptConfig true during encrypt config file: %w", err)
}
Expand All @@ -120,14 +120,12 @@ func (c *Config) encryptConfigFile(configData []byte) ([]byte, error) {
return appendedFile, nil
}

// DecryptConfigFile decrypts configuration data with the supplied key and
// returns the un-encrypted data as a byte array with an error
// DecryptConfigFile decrypts config data with a key
func DecryptConfigFile(d, key []byte) ([]byte, error) {
return (&Config{}).decryptConfigData(d, key)
}

// decryptConfigData decrypts configuration data with the supplied key and
// returns the un-encrypted data as a byte array with an error
// decryptConfigData decrypts config data with a key
func (c *Config) decryptConfigData(d, key []byte) ([]byte, error) {
if !bytes.HasPrefix(d, encryptionPrefix) {
return d, errNoPrefix
Expand Down Expand Up @@ -193,7 +191,7 @@ func IsFileEncrypted(f string) bool {

func getScryptDK(key, salt []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errors.New("key is empty")
return nil, errKeyIsEmpty
}
return scrypt.Key(key, salt, 32768, 8, 1, 32)
}
Expand Down
36 changes: 11 additions & 25 deletions config/config_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,21 @@ func TestEncryptConfigFile(t *testing.T) {

func TestDecryptConfigFile(t *testing.T) {
t.Parallel()
result, err := EncryptConfigFile([]byte("test"), []byte("key"))
if err != nil {
t.Fatal(err)
}

_, err = DecryptConfigFile(result, nil)
if err == nil {
t.Fatal("Expected error")
}
e, err := EncryptConfigFile([]byte(`{"test":1}`), []byte("key"))
require.NoError(t, err)

_, err = DecryptConfigFile([]byte("test"), nil)
if err == nil {
t.Fatal("Expected error")
}
d, err := DecryptConfigFile(e, []byte("key"))
require.NoError(t, err)
assert.Equal(t, `{"test":1,"encryptConfig":1}`, string(d), "encryptConfig should be set to 1 after first encryption")

_, err = DecryptConfigFile([]byte("test"), []byte("AAAAAAAAAAAAAAAA"))
if err == nil {
t.Fatalf("Expected %s", errAESBlockSize)
}
_, err = DecryptConfigFile(e, nil)
require.ErrorIs(t, err, errKeyIsEmpty)

result, err = EncryptConfigFile([]byte("test"), []byte("key"))
if err != nil {
t.Fatal(err)
}
_, err = DecryptConfigFile([]byte("test"), nil)
require.ErrorIs(t, err, errNoPrefix)

_, err = DecryptConfigFile(result, []byte("key"))
if err != nil {
t.Fatal(err)
}
_, err = DecryptConfigFile(encryptionPrefix, []byte("AAAAAAAAAAAAAAAA"))
require.ErrorIs(t, err, errAESBlockSize)
}

func TestIsEncrypted(t *testing.T) {
Expand Down

0 comments on commit 2b8ca24

Please sign in to comment.