Skip to content

Commit

Permalink
fix: suffix subkey consts for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
gak committed Aug 11, 2024
1 parent 4c77884 commit 67576e6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions internal/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func decryptBytesForStreaming(streamingPrimitive tink.StreamingAEAD, encrypted [
type SubKey string

const (
Logs SubKey = "logs"
Async SubKey = "async"
LogsSubKey SubKey = "logs"
AsyncSubKey SubKey = "async"
)

type EncryptorNext interface {
Expand Down
16 changes: 8 additions & 8 deletions internal/encryption/encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,29 @@ func TestPlaintextEncryptor(t *testing.T) {
encryptor, err := NewPlaintextEncryptor(key)
assert.NoError(t, err)

encrypted, err := encryptor.Encrypt(Logs, []byte("hunter2"))
encrypted, err := encryptor.Encrypt(LogsSubKey, []byte("hunter2"))
assert.NoError(t, err)
fmt.Printf("Encrypted: %s\n", encrypted)

decrypted, err := encryptor.Decrypt(Logs, encrypted)
decrypted, err := encryptor.Decrypt(LogsSubKey, encrypted)
assert.NoError(t, err)
fmt.Printf("Decrypted: %s\n", decrypted)

assert.Equal(t, "hunter2", string(decrypted))

// Should fail to decrypt with the wrong subkey
_, err = encryptor.Decrypt(Async, encrypted)
_, err = encryptor.Decrypt(AsyncSubKey, encrypted)
assert.Error(t, err)

}

func TestNoOpEncryptor(t *testing.T) {
encryptor := NoOpEncryptorNext{}

encrypted, err := encryptor.Encrypt(Logs, []byte("hunter2"))
encrypted, err := encryptor.Encrypt(LogsSubKey, []byte("hunter2"))
assert.NoError(t, err)

decrypted, err := encryptor.Decrypt(Logs, encrypted)
decrypted, err := encryptor.Decrypt(LogsSubKey, encrypted)
assert.NoError(t, err)

assert.Equal(t, "hunter2", string(decrypted))
Expand All @@ -86,14 +86,14 @@ func TestKMSEncryptorFakeKMS(t *testing.T) {
encryptor, err := NewKMSEncryptorGenerateKey(uri, nil)
assert.NoError(t, err)

encrypted, err := encryptor.Encrypt(Logs, []byte("hunter2"))
encrypted, err := encryptor.Encrypt(LogsSubKey, []byte("hunter2"))
assert.NoError(t, err)

decrypted, err := encryptor.Decrypt(Logs, encrypted)
decrypted, err := encryptor.Decrypt(LogsSubKey, encrypted)
assert.NoError(t, err)
assert.Equal(t, "hunter2", string(decrypted))

// Should fail to decrypt with the wrong subkey
_, err = encryptor.Decrypt(Async, encrypted)
_, err = encryptor.Decrypt(AsyncSubKey, encrypted)
assert.Error(t, err)
}
6 changes: 3 additions & 3 deletions internal/encryption/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ func TestKMSEncryptorLocalstack(t *testing.T) {
encryptor, err := NewKMSEncryptorGenerateKey(uri, v1client)
assert.NoError(t, err)

encrypted, err := encryptor.Encrypt(Logs, []byte("hunter2"))
encrypted, err := encryptor.Encrypt(LogsSubKey, []byte("hunter2"))
assert.NoError(t, err)

decrypted, err := encryptor.Decrypt(Logs, encrypted)
decrypted, err := encryptor.Decrypt(LogsSubKey, encrypted)
assert.NoError(t, err)
assert.Equal(t, "hunter2", string(decrypted))

// Should fail to decrypt with the wrong subkey
_, err = encryptor.Decrypt(Async, encrypted)
_, err = encryptor.Decrypt(AsyncSubKey, encrypted)
assert.Error(t, err)
}

0 comments on commit 67576e6

Please sign in to comment.