Skip to content
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

cloud/amazon: test kms against mock server #131172

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/cloud/amazon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ go_test(
"//pkg/cloud/cloudpb",
"//pkg/cloud/cloudtestutils",
"//pkg/security/username",
"//pkg/settings",
"//pkg/settings/cluster",
"//pkg/testutils",
"//pkg/testutils/skip",
Expand Down
63 changes: 63 additions & 0 deletions pkg/cloud/amazon/aws_kms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ package amazon

import (
"context"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/cloud"
"github.com/cockroachdb/cockroach/pkg/cloud/cloudtestutils"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
Expand Down Expand Up @@ -231,6 +237,63 @@ func TestEncryptDecryptAWSAssumeRole(t *testing.T) {
})
}

func TestKMSAgainstMockAWS(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()

// Setup a bogus credentials file so it doesn't try to use a metadata server.
dt marked this conversation as resolved.
Show resolved Hide resolved
tempDir, cleanup := testutils.TempDir(t)
defer cleanup()
credFile := filepath.Join(tempDir, "credentials")
require.NoError(t, os.Setenv("AWS_SHARED_CREDENTIALS_FILE", credFile))
defer func() {
require.NoError(t, os.Unsetenv("AWS_SHARED_CREDENTIALS_FILE"))
}()
require.NoError(t, os.WriteFile(credFile, []byte(`[default]
aws_access_key_id = abc
aws_secret_access_key = xyz
`), 0644))

srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
defer r.Body.Close()
// Default to replying with static placeholder "ciphertext", unless req is
// a decrypt req, in which case send static "decrypted" plaintext resp.
resp := `{"CiphertextBlob": "dW51c2Vk"}` // "unused".
if strings.Contains(string(body), "Ciphertext") {
dt marked this conversation as resolved.
Show resolved Hide resolved
resp = `{"Plaintext": "aGVsbG8gd29ybGQ="}` // base64 for 'hello world'
}
_, err = w.Write([]byte(resp))
require.NoError(t, err)
}))
defer srv.Close()

tEnv := &cloud.TestKMSEnv{Settings: cluster.MakeTestingClusterSettings(), ExternalIOConfig: &base.ExternalIODirConfig{}}

// Set the custom CA so testserver is trusted, and defer reset of it.
u := tEnv.Settings.MakeUpdater()
require.NoError(t, u.Set(ctx, "cloudstorage.http.custom_ca", settings.EncodedValue{
Type: "s", Value: string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: srv.Certificate().Raw})),
dt marked this conversation as resolved.
Show resolved Hide resolved
}))

t.Run("implicit", func(t *testing.T) {
q := url.Values{
KMSRegionParam: []string{"r"}, AWSEndpointParam: []string{srv.URL},
cloud.AuthParam: []string{"implicit"},
}
cloud.KMSEncryptDecrypt(t, fmt.Sprintf("aws:///arn?%s", q.Encode()), tEnv)
})

t.Run("specified", func(t *testing.T) {
q := url.Values{
KMSRegionParam: []string{"r"}, AWSEndpointParam: []string{srv.URL},
AWSAccessKeyParam: []string{"k"}, AWSSecretParam: []string{"s"},
}
cloud.KMSEncryptDecrypt(t, fmt.Sprintf("aws:///arn?%s", q.Encode()), tEnv)
})
}

func TestPutAWSKMSEndpoint(t *testing.T) {
dt marked this conversation as resolved.
Show resolved Hide resolved
defer leaktest.AfterTest(t)()
ctx := context.Background()
Expand Down
Loading