-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ROX-18428: implement KMS encryption and decryption #1178
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b986d0e
add kms cipher implementation
johannes94 78a6b7d
add integration test for kms cipher
johannes94 4303bdd
change test/rds to test/aws to test all AWS integrations of fleetshard
johannes94 ecc0bc6
fix integration tests
johannes94 62176b8
change variable name in kms test
johannes94 d25a2ce
fix accidental change in up.sh
johannes94 a883854
fix skip test message for kms tests
johannes94 bf5612f
implement data key encryption instead of KMS api
johannes94 b056b9f
fix comment
johannes94 07ac394
restructure cipher package
johannes94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cipher | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/kms" | ||
) | ||
|
||
type kmsCipher struct { | ||
keyID string | ||
kms *kms.KMS | ||
} | ||
|
||
// NewKMSCipher return a new Cipher using AWS KMS with the given keyId | ||
func NewKMSCipher(keyID string) (Cipher, error) { | ||
sess, err := session.NewSession() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create session for KMS client %w", err) | ||
} | ||
|
||
return kmsCipher{ | ||
keyID: keyID, | ||
kms: kms.New(sess), | ||
}, nil | ||
} | ||
|
||
func (k kmsCipher) Encrypt(plaintext []byte) ([]byte, error) { | ||
encryptInput := &kms.EncryptInput{ | ||
KeyId: &k.keyID, | ||
Plaintext: plaintext, | ||
} | ||
|
||
encryptOut, err := k.kms.Encrypt(encryptInput) | ||
if err != nil { | ||
return nil, fmt.Errorf("error encrypting data: %w", err) | ||
} | ||
|
||
return encryptOut.CiphertextBlob, nil | ||
} | ||
|
||
func (k kmsCipher) Decrypt(ciphertext []byte) ([]byte, error) { | ||
decryptInput := &kms.DecryptInput{ | ||
KeyId: &k.keyID, | ||
CiphertextBlob: ciphertext, | ||
} | ||
|
||
decryptOut, err := k.kms.Decrypt(decryptInput) | ||
if err != nil { | ||
return nil, fmt.Errorf("error decrypting data: %w", err) | ||
} | ||
return decryptOut.Plaintext, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cipher | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKMSEncryptDecrypt(t *testing.T) { | ||
if os.Getenv("RUN_AWS_INTEGRATION") != "true" { | ||
t.Skip("Skip KMS tests. Set RUN_AWS_INTEGRATION=true env variable to enable KMS tests.") | ||
} | ||
|
||
keyID := os.Getenv("SECRET_ENCRYPTION_KEY_ID") | ||
require.NotEmpty(t, keyID, "SECRET_ENCRYPTION_KEY_ID not set") | ||
|
||
cipher, err := NewKMSCipher(keyID) | ||
require.NoError(t, err, "creating KMS cipher") | ||
|
||
plaintext := "This is example plain text" | ||
plaintextB := []byte(plaintext) | ||
ciphertextB, err := cipher.Encrypt(plaintextB) | ||
require.NoError(t, err, "encrypting plaintext") | ||
|
||
decrypted, err := cipher.Decrypt(ciphertextB) | ||
require.NoError(t, err, "decrypting ciphertext") | ||
|
||
require.NotEqual(t, plaintext, string(ciphertextB)) | ||
require.Equal(t, plaintext, string(decrypted)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need timeout here if it's specified in the actions job?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to also timeout for local executions of this tests.