Skip to content

Commit

Permalink
amazon,externalconn: add s3 support to External Connections
Browse files Browse the repository at this point in the history
This change registers s3 as a URI that can be represented as
an External Connection. Most notably we take a page from the
CDC book and switch the s3 parse function to check for invalid
parameters, and configurations. This allows us to catch certain
misconfiguration at the time we create the external connection.

Informs: cockroachdb#84753

Release note (sql change): Users can now `CREATE EXTERNAL CONNECTION`
to represent an s3 URI.
  • Loading branch information
adityamaru committed Aug 10, 2022
1 parent 62b625c commit 49c90b7
Show file tree
Hide file tree
Showing 24 changed files with 477 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ log_into_aws

exit_status=0
$BAZEL_BIN/pkg/cmd/bazci/bazci_/bazci --config=ci \
test //pkg/cloud/gcp:gcp_test //pkg/cloud/amazon:amazon_test //pkg/ccl/cloudccl/gcp:gcp_test -- \
test //pkg/cloud/gcp:gcp_test //pkg/cloud/amazon:amazon_test //pkg/ccl/cloudccl/gcp:gcp_test //pkg/ccl/cloudccl/amazon:amazon_test -- \
--test_env=GO_TEST_WRAP_TESTV=1 \
--test_env=GO_TEST_WRAP=1 \
--test_env=GO_TEST_JSON_OUTPUT_FILE=$GO_TEST_JSON_OUTPUT_FILE \
Expand Down
3 changes: 3 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ALL_TESTS = [
"//pkg/ccl/changefeedccl/schemafeed:schemafeed_test",
"//pkg/ccl/changefeedccl:changefeedccl_test",
"//pkg/ccl/cliccl:cliccl_test",
"//pkg/ccl/cloudccl/amazon:amazon_test",
"//pkg/ccl/cloudccl/externalconn:externalconn_test",
"//pkg/ccl/cloudccl/gcp:gcp_test",
"//pkg/ccl/importerccl:importerccl_test",
Expand Down Expand Up @@ -662,6 +663,7 @@ GO_TARGETS = [
"//pkg/ccl/cliccl/cliflagsccl:cliflagsccl",
"//pkg/ccl/cliccl:cliccl",
"//pkg/ccl/cliccl:cliccl_test",
"//pkg/ccl/cloudccl/amazon:amazon_test",
"//pkg/ccl/cloudccl/externalconn:externalconn_test",
"//pkg/ccl/cloudccl/gcp:gcp_test",
"//pkg/ccl/cmdccl/enc_utils:enc_utils",
Expand Down Expand Up @@ -2080,6 +2082,7 @@ GET_X_DATA_TARGETS = [
"//pkg/ccl/changefeedccl/schemafeed/schematestutils:get_x_data",
"//pkg/ccl/cliccl:get_x_data",
"//pkg/ccl/cliccl/cliflagsccl:get_x_data",
"//pkg/ccl/cloudccl/amazon:get_x_data",
"//pkg/ccl/cloudccl/externalconn:get_x_data",
"//pkg/ccl/cloudccl/gcp:get_x_data",
"//pkg/ccl/cmdccl/enc_utils:get_x_data",
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/changefeedccl/sink_kafka_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func parseAndValidateKafkaSinkURI(
}

connDetails := connectionpb.ConnectionDetails{
Provider: connectionpb.ConnectionProvider_TypeKafka,
Provider: connectionpb.ConnectionProvider_kafka,
Details: &connectionpb.ConnectionDetails_SimpleURI{
SimpleURI: &connectionpb.SimpleURI{
URI: uri.String(),
Expand Down
34 changes: 34 additions & 0 deletions pkg/ccl/cloudccl/amazon/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data")
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "amazon_test",
srcs = [
"main_test.go",
"s3_connection_test.go",
],
deps = [
"//pkg/base",
"//pkg/ccl",
"//pkg/ccl/kvccl/kvtenantccl",
"//pkg/ccl/utilccl",
"//pkg/cloud",
"//pkg/cloud/amazon",
"//pkg/cloud/cloudpb",
"//pkg/cloud/externalconn/providers",
"//pkg/security/securityassets",
"//pkg/security/securitytest",
"//pkg/server",
"//pkg/testutils",
"//pkg/testutils/serverutils",
"//pkg/testutils/skip",
"//pkg/testutils/sqlutils",
"//pkg/testutils/testcluster",
"//pkg/util/leaktest",
"//pkg/util/log",
"//pkg/util/randutil",
"@com_github_aws_aws_sdk_go//aws/credentials",
],
)

get_x_data(name = "get_x_data")
35 changes: 35 additions & 0 deletions pkg/ccl/cloudccl/amazon/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2022 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt

package amazon_test

import (
"os"
"testing"

_ "github.com/cockroachdb/cockroach/pkg/ccl/kvccl/kvtenantccl"
"github.com/cockroachdb/cockroach/pkg/ccl/utilccl"
"github.com/cockroachdb/cockroach/pkg/security/securityassets"
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
)

func TestMain(m *testing.M) {
defer utilccl.TestingEnableEnterprise()()

securityassets.SetLoader(securitytest.EmbeddedAssets)
randutil.SeedForTests()
serverutils.InitTestServerFactory(server.TestServerFactory)
serverutils.InitTestClusterFactory(testcluster.TestClusterFactory)
os.Exit(m.Run())
}

//go:generate ../../../util/leaktest/add-leaktest.sh *_test.go
184 changes: 184 additions & 0 deletions pkg/ccl/cloudccl/amazon/s3_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright 2020 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt

package amazon

import (
"context"
"fmt"
"net/url"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/cockroachdb/cockroach/pkg/base"
_ "github.com/cockroachdb/cockroach/pkg/ccl"
"github.com/cockroachdb/cockroach/pkg/cloud"
"github.com/cockroachdb/cockroach/pkg/cloud/amazon"
"github.com/cockroachdb/cockroach/pkg/cloud/cloudpb"
_ "github.com/cockroachdb/cockroach/pkg/cloud/externalconn/providers" // import External Connection providers.
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
)

func TestS3ExternalConnection(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

dir, dirCleanupFn := testutils.TempDir(t)
defer dirCleanupFn()

params := base.TestClusterArgs{}
params.ServerArgs.ExternalIODir = dir

tc := testcluster.StartTestCluster(t, 1, params)
defer tc.Stopper().Stop(context.Background())

tc.WaitForNodeLiveness(t)
sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0])

// Setup some dummy data.
sqlDB.Exec(t, `CREATE DATABASE foo`)
sqlDB.Exec(t, `USE foo`)
sqlDB.Exec(t, `CREATE TABLE foo (id INT PRIMARY KEY)`)
sqlDB.Exec(t, `INSERT INTO foo VALUES (1), (2), (3)`)

createExternalConnection := func(externalConnectionName, uri string) {
sqlDB.Exec(t, fmt.Sprintf(`CREATE EXTERNAL CONNECTION '%s' AS '%s'`, externalConnectionName, uri))
}
backupAndRestoreFromExternalConnection := func(backupExternalConnectionName string) {
backupURI := fmt.Sprintf("external://%s", backupExternalConnectionName)
sqlDB.Exec(t, fmt.Sprintf(`BACKUP DATABASE foo INTO '%s'`, backupURI))
sqlDB.Exec(t, fmt.Sprintf(`RESTORE DATABASE foo FROM LATEST IN '%s' WITH new_db_name = bar`, backupURI))
sqlDB.CheckQueryResults(t, `SELECT * FROM bar.foo`, [][]string{{"1"}, {"2"}, {"3"}})
sqlDB.CheckQueryResults(t, `SELECT * FROM crdb_internal.invalid_objects`, [][]string{})
sqlDB.Exec(t, `DROP DATABASE bar CASCADE`)
}

// If environment credentials are not present, we want to
// skip all S3 tests, including auth-implicit, even though
// it is not used in auth-implicit.
creds, err := credentials.NewEnvCredentials().Get()
if err != nil {
skip.IgnoreLint(t, "No AWS credentials")
}
bucket := os.Getenv("AWS_S3_BUCKET")
if bucket == "" {
skip.IgnoreLint(t, "AWS_S3_BUCKET env var must be set")
}

t.Run("auth-implicit", func(t *testing.T) {
// You can create an IAM that can access S3
// in the AWS console, then set it up locally.
// https://docs.aws.com/cli/latest/userguide/cli-configure-role.html
// We only run this test if default role exists.
credentialsProvider := credentials.SharedCredentialsProvider{}
_, err := credentialsProvider.Retrieve()
if err != nil {
skip.IgnoreLintf(t, "we only run this test if a default role exists, "+
"refer to https://docs.aws.com/cli/latest/userguide/cli-configure-role.html: %s", err)
}

// Set the AUTH to implicit.
params := make(url.Values)
params.Add(cloud.AuthParam, cloud.AuthParamImplicit)

s3URI := fmt.Sprintf("s3://%s/backup-ec-test-default?%s", bucket, params.Encode())
ecName := "auth-implicit-s3"
createExternalConnection(ecName, s3URI)
backupAndRestoreFromExternalConnection(ecName)
})

t.Run("auth-specified", func(t *testing.T) {
s3URI := amazon.S3URI(bucket, "backup-ec-test-default",
&cloudpb.ExternalStorage_S3{
AccessKey: creds.AccessKeyID,
Secret: creds.SecretAccessKey,
Region: "us-east-1",
Auth: cloud.AuthParamSpecified,
},
)
ecName := "auth-specified-s3"
createExternalConnection(ecName, s3URI)
backupAndRestoreFromExternalConnection(ecName)
})

// Tests that we can put an object with server side encryption specified.
t.Run("server-side-encryption", func(t *testing.T) {
// You can create an IAM that can access S3
// in the AWS console, then set it up locally.
// https://docs.aws.com/cli/latest/userguide/cli-configure-role.html
// We only run this test if default role exists.
credentialsProvider := credentials.SharedCredentialsProvider{}
_, err := credentialsProvider.Retrieve()
if err != nil {
skip.IgnoreLintf(t, "we only run this test if a default role exists, "+
"refer to https://docs.aws.com/cli/latest/userguide/cli-configure-role.html: %s", err)
}

s3URI := amazon.S3URI(bucket, "backup-ec-test-sse-256", &cloudpb.ExternalStorage_S3{
Region: "us-east-1",
Auth: cloud.AuthParamImplicit,
ServerEncMode: "AES256",
})
ecName := "server-side-encryption-s3"
createExternalConnection(ecName, s3URI)
backupAndRestoreFromExternalConnection(ecName)

v := os.Getenv("AWS_KMS_KEY_ARN")
if v == "" {
skip.IgnoreLint(t, "AWS_KMS_KEY_ARN env var must be set")
}
s3KMSURI := amazon.S3URI(bucket, "backup-ec-test-sse-kms", &cloudpb.ExternalStorage_S3{
Region: "us-east-1",
Auth: cloud.AuthParamImplicit,
ServerEncMode: "aws:kms",
ServerKMSID: v,
})
ecName = "server-side-encryption-kms-s3"
createExternalConnection(ecName, s3KMSURI)
backupAndRestoreFromExternalConnection(ecName)
})

t.Run("server-side-encryption-invalid-params", func(t *testing.T) {
// You can create an IAM that can access S3
// in the AWS console, then set it up locally.
// https://docs.aws.com/cli/latest/userguide/cli-configure-role.html
// We only run this test if default role exists.
credentialsProvider := credentials.SharedCredentialsProvider{}
_, err := credentialsProvider.Retrieve()
if err != nil {
skip.IgnoreLintf(t, "we only run this test if a default role exists, "+
"refer to https://docs.aws.com/cli/latest/userguide/cli-configure-role.html: %s", err)
}

// Unsupported server side encryption option.
invalidS3URI := amazon.S3URI(bucket, "backup-ec-test-sse-256", &cloudpb.ExternalStorage_S3{
Region: "us-east-1",
Auth: cloud.AuthParamImplicit,
ServerEncMode: "unsupported-algorithm",
})
sqlDB.ExpectErr(t,
"unsupported server encryption mode unsupported-algorithm. Supported values are `aws:kms` and `AES256",
fmt.Sprintf(`BACKUP DATABASE foo INTO '%s'`, invalidS3URI))

invalidS3URI = amazon.S3URI(bucket, "backup-ec-test-sse-256", &cloudpb.ExternalStorage_S3{
Region: "us-east-1",
Auth: cloud.AuthParamImplicit,
ServerEncMode: "aws:kms",
})

// Specify aws:kms encryption mode but don't specify kms ID.
sqlDB.ExpectErr(t, "AWS_SERVER_KMS_ID param must be set when using aws:kms server side encryption mode.", fmt.Sprintf(`BACKUP DATABASE foo INTO '%s'`,
invalidS3URI))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,36 @@ inspect-system-table

subtest end

subtest basic-s3

exec-sql
CREATE EXTERNAL CONNECTION "foo-s3" AS 's3://foo/bar?AUTH=implicit&AWS_ACCESS_KEY_ID=123&AWS_SECRET_ACCESS_KEY=456&ASSUME_ROLE=ronaldo,rashford,bruno';
----

# Reject invalid S3 URIs.
exec-sql
CREATE EXTERNAL CONNECTION "missing-host-s3" AS 's3:///?AUTH=implicit';
----
pq: failed to construct External Connection details: empty host component; s3 URI must specify a target bucket

exec-sql
CREATE EXTERNAL CONNECTION "invalid-params-s3" AS 's3://foo/bar?AUTH=implicit&INVALIDPARAM=baz';
----
pq: failed to construct External Connection details: unknown S3 query parameters: INVALIDPARAM

inspect-system-table
----
foo-s3 STORAGE {"provider": "s3", "simpleUri": {"uri": "s3://foo/bar?AUTH=implicit&AWS_ACCESS_KEY_ID=123&AWS_SECRET_ACCESS_KEY=456&ASSUME_ROLE=ronaldo,rashford,bruno"}}

exec-sql
DROP EXTERNAL CONNECTION "foo-s3";
----

inspect-system-table
----

subtest end

subtest basic-kafka-sink

exec-sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,36 @@ inspect-system-table

subtest end

subtest basic-s3

exec-sql
CREATE EXTERNAL CONNECTION "foo-s3" AS 's3://foo/bar?AUTH=implicit&AWS_ACCESS_KEY_ID=123&AWS_SECRET_ACCESS_KEY=456&ASSUME_ROLE=ronaldo,rashford,bruno';
----

# Reject invalid S3 URIs.
exec-sql
CREATE EXTERNAL CONNECTION "missing-host-s3" AS 's3:///?AUTH=implicit';
----
pq: failed to construct External Connection details: empty host component; s3 URI must specify a target bucket

exec-sql
CREATE EXTERNAL CONNECTION "invalid-params-s3" AS 's3://foo/bar?AUTH=implicit&INVALIDPARAM=baz';
----
pq: failed to construct External Connection details: unknown S3 query parameters: INVALIDPARAM

inspect-system-table
----
foo-s3 STORAGE {"provider": "s3", "simpleUri": {"uri": "s3://foo/bar?AUTH=implicit&AWS_ACCESS_KEY_ID=123&AWS_SECRET_ACCESS_KEY=456&ASSUME_ROLE=ronaldo,rashford,bruno"}}

exec-sql
DROP EXTERNAL CONNECTION "foo-s3";
----

inspect-system-table
----

subtest end

subtest basic-kafka-sink

exec-sql
Expand Down
1 change: 1 addition & 0 deletions pkg/ccl/cloudccl/gcp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_test(
"//pkg/cloud/cloudtestutils",
"//pkg/cloud/externalconn/providers",
"//pkg/cloud/gcp",
"//pkg/cloud/impl:cloudimpl",
"//pkg/security/securityassets",
"//pkg/security/securitytest",
"//pkg/server",
Expand Down
Loading

0 comments on commit 49c90b7

Please sign in to comment.