-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support decrypting API keys encrypted with an encryption context (#77)
- Loading branch information
Showing
27 changed files
with
141 additions
and
34 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Datadog datadog-lambda-go | ||
Copyright 2019 Datadog, Inc. | ||
Copyright 2021 Datadog, Inc. | ||
|
||
This product includes software developed at Datadog (https://www.datadoghq.com/). |
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
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
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,76 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package metrics | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/kms" | ||
"github.com/aws/aws-sdk-go/service/kms/kmsiface" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// mockEncryptedAPIKeyBase64 represents an API key encrypted with KMS and encoded as a base64 string | ||
const mockEncryptedAPIKeyBase64 = "MjIyMjIyMjIyMjIyMjIyMg==" | ||
|
||
// mockDecodedEncryptedAPIKey represents the encrypted API key after it has been decoded from base64 | ||
const mockDecodedEncryptedAPIKey = "2222222222222222" | ||
|
||
// expectedDecryptedAPIKey represents the true value of the API key after decryption by KMS | ||
const expectedDecryptedAPIKey = "1111111111111111" | ||
|
||
// mockFunctionName represents the name of the current function | ||
var mockFunctionName = "my-Function" | ||
|
||
type mockKMSClientWithEncryptionContext struct { | ||
kmsiface.KMSAPI | ||
} | ||
|
||
func (mockKMSClientWithEncryptionContext) Decrypt(params *kms.DecryptInput) (*kms.DecryptOutput, error) { | ||
if *params.EncryptionContext[encryptionContextKey] != mockFunctionName { | ||
return nil, errors.New("InvalidCiphertextExeption") | ||
} | ||
if bytes.Equal(params.CiphertextBlob, []byte(mockDecodedEncryptedAPIKey)) { | ||
return &kms.DecryptOutput{ | ||
Plaintext: []byte(expectedDecryptedAPIKey), | ||
}, nil | ||
} | ||
return nil, errors.New("KMS error") | ||
} | ||
|
||
type mockKMSClientNoEncryptionContext struct { | ||
kmsiface.KMSAPI | ||
} | ||
|
||
func (mockKMSClientNoEncryptionContext) Decrypt(params *kms.DecryptInput) (*kms.DecryptOutput, error) { | ||
if params.EncryptionContext[encryptionContextKey] != nil { | ||
return nil, errors.New("InvalidCiphertextExeption") | ||
} | ||
if bytes.Equal(params.CiphertextBlob, []byte(mockDecodedEncryptedAPIKey)) { | ||
return &kms.DecryptOutput{ | ||
Plaintext: []byte(expectedDecryptedAPIKey), | ||
}, nil | ||
} | ||
return nil, errors.New("KMS error") | ||
} | ||
|
||
func TestDecryptKMSWithEncryptionContext(t *testing.T) { | ||
os.Setenv(functionNameEnvVar, mockFunctionName) | ||
defer os.Setenv(functionNameEnvVar, "") | ||
|
||
client := mockKMSClientWithEncryptionContext{} | ||
result, _ := decryptKMS(client, mockEncryptedAPIKeyBase64) | ||
assert.Equal(t, expectedDecryptedAPIKey, result) | ||
} | ||
|
||
func TestDecryptKMSNoEncryptionContext(t *testing.T) { | ||
client := mockKMSClientNoEncryptionContext{} | ||
result, _ := decryptKMS(client, mockEncryptedAPIKeyBase64) | ||
assert.Equal(t, expectedDecryptedAPIKey, result) | ||
} |
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
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
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