-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
externalconn: register azure-storage as a supported External Connection
`azure-storage` URIs can now be represented using External Connections. Release note (sql change): Users can now `CREATE EXTERNAL CONNECTION` to represent an `azure-storage` URI.
- Loading branch information
1 parent
7c38417
commit 8c7cf17
Showing
15 changed files
with
326 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_test") | ||
|
||
go_test( | ||
name = "azure_test", | ||
srcs = [ | ||
"azure_connection_test.go", | ||
"main_test.go", | ||
], | ||
deps = [ | ||
"//pkg/base", | ||
"//pkg/ccl", | ||
"//pkg/ccl/kvccl/kvtenantccl", | ||
"//pkg/ccl/utilccl", | ||
"//pkg/cloud/azure", | ||
"//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_azure_go_autorest_autorest//azure", | ||
], | ||
) | ||
|
||
get_x_data(name = "get_x_data") |
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,103 @@ | ||
// 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 azure | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"testing" | ||
|
||
az "github.com/Azure/go-autorest/autorest/azure" | ||
"github.com/cockroachdb/cockroach/pkg/base" | ||
_ "github.com/cockroachdb/cockroach/pkg/ccl" | ||
"github.com/cockroachdb/cockroach/pkg/cloud/azure" | ||
_ "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 (a azureConfig) URI(file string) string { | ||
return fmt.Sprintf("azure-storage://%s/%s?%s=%s&%s=%s&%s=%s", | ||
a.bucket, file, | ||
azure.AzureAccountKeyParam, url.QueryEscape(a.key), | ||
azure.AzureAccountNameParam, url.QueryEscape(a.account), | ||
azure.AzureEnvironmentKeyParam, url.QueryEscape(a.environment)) | ||
} | ||
|
||
type azureConfig struct { | ||
account, key, bucket, environment string | ||
} | ||
|
||
func getAzureConfig() (azureConfig, error) { | ||
cfg := azureConfig{ | ||
account: os.Getenv("AZURE_ACCOUNT_NAME"), | ||
key: os.Getenv("AZURE_ACCOUNT_KEY"), | ||
bucket: os.Getenv("AZURE_CONTAINER"), | ||
environment: az.PublicCloud.Name, | ||
} | ||
if cfg.account == "" || cfg.key == "" || cfg.bucket == "" { | ||
return azureConfig{}, errors.New("AZURE_ACCOUNT_NAME, AZURE_ACCOUNT_KEY, AZURE_CONTAINER must all be set") | ||
} | ||
if v, ok := os.LookupEnv(azure.AzureEnvironmentKeyParam); ok { | ||
cfg.environment = v | ||
} | ||
return cfg, nil | ||
} | ||
|
||
func TestExternalConnections(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`) | ||
} | ||
|
||
cfg, err := getAzureConfig() | ||
if err != nil { | ||
skip.IgnoreLint(t, "TestExternalConnections not configured for Azure") | ||
return | ||
} | ||
|
||
ecName := "azure-ec" | ||
createExternalConnection(ecName, cfg.URI("backup-ec")) | ||
backupAndRestoreFromExternalConnection(ecName) | ||
} |
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,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 azure_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 |
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,47 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package azure | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/cloud/externalconn" | ||
"github.com/cockroachdb/cockroach/pkg/cloud/externalconn/connectionpb" | ||
"github.com/cockroachdb/cockroach/pkg/cloud/externalconn/utils" | ||
"github.com/cockroachdb/cockroach/pkg/security/username" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
func parseAndValidateAzureConnectionURI( | ||
ctx context.Context, execCfg interface{}, user username.SQLUsername, uri *url.URL, | ||
) (externalconn.ExternalConnection, error) { | ||
if err := utils.CheckExternalStorageConnection(ctx, execCfg, user, uri.String()); err != nil { | ||
return nil, errors.Wrap(err, "failed to create azure external connection") | ||
} | ||
|
||
connDetails := connectionpb.ConnectionDetails{ | ||
Provider: connectionpb.ConnectionProvider_azure_storage, | ||
Details: &connectionpb.ConnectionDetails_SimpleURI{ | ||
SimpleURI: &connectionpb.SimpleURI{ | ||
URI: uri.String(), | ||
}, | ||
}, | ||
} | ||
return externalconn.NewExternalConnection(connDetails), nil | ||
} | ||
|
||
func init() { | ||
externalconn.RegisterConnectionDetailsFromURIFactory( | ||
externalConnectionScheme, | ||
parseAndValidateAzureConnectionURI, | ||
) | ||
} |
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
Oops, something went wrong.