-
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.
105400: pgrepl: allow logging in with `replication` parameter r=rafiss a=otan This commit adds the `replication=database` connection parameter, which enables certain replication commands to be run. See: https://www.postgresql.org/docs/current/protocol-replication.html We do this by making it a session variable instead of a parameter on the connection like PG does. This adds a hidden `replication` connection parameter to accomplish this. Informs: #105130 Release note: None Co-authored-by: Oliver Tan <[email protected]>
- Loading branch information
Showing
9 changed files
with
202 additions
and
0 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
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,26 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_test") | ||
|
||
go_test( | ||
name = "pgrepl_test", | ||
srcs = [ | ||
"connect_test.go", | ||
"pgrepl_test.go", | ||
], | ||
args = ["-test.timeout=295s"], | ||
deps = [ | ||
"//pkg/security/securityassets", | ||
"//pkg/security/securitytest", | ||
"//pkg/security/username", | ||
"//pkg/server", | ||
"//pkg/sql/pgwire/pgcode", | ||
"//pkg/sql/tests", | ||
"//pkg/testutils/serverutils", | ||
"//pkg/testutils/sqlutils", | ||
"//pkg/util/leaktest", | ||
"//pkg/util/log", | ||
"@com_github_cockroachdb_errors//:errors", | ||
"@com_github_jackc_pgx_v5//:pgx", | ||
"@com_github_jackc_pgx_v5//pgconn", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
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,81 @@ | ||
// Copyright 2023 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 pgrepl_test | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/security/username" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/sql/tests" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/errors" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/jackc/pgx/v5/pgconn" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConnect(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
params, _ := tests.CreateTestServerParams() | ||
|
||
s, db, _ := serverutils.StartServer(t, params) | ||
defer s.Stopper().Stop(context.Background()) | ||
|
||
sqlDB := sqlutils.MakeSQLRunner(db) | ||
sqlDB.Exec(t, `CREATE USER testuser`) | ||
|
||
for _, tc := range []struct { | ||
replicationMode string | ||
expectedSessionVar string | ||
expectError bool | ||
}{ | ||
{replicationMode: "", expectedSessionVar: "off"}, | ||
{replicationMode: "0", expectedSessionVar: "off"}, | ||
{replicationMode: "1", expectedSessionVar: "on"}, | ||
{replicationMode: "false", expectedSessionVar: "off"}, | ||
{replicationMode: "true", expectedSessionVar: "on"}, | ||
{replicationMode: "database", expectedSessionVar: "database"}, | ||
{replicationMode: "asdf", expectError: true}, | ||
} { | ||
t.Run(tc.replicationMode, func(t *testing.T) { | ||
pgURL, cleanup := sqlutils.PGUrl(t, s.ServingSQLAddr(), "pgrepl_conn_test", url.User(username.TestUser)) | ||
defer cleanup() | ||
|
||
cfg, err := pgx.ParseConfig(pgURL.String()) | ||
require.NoError(t, err) | ||
if tc.replicationMode != "" { | ||
cfg.RuntimeParams["replication"] = tc.replicationMode | ||
} | ||
|
||
ctx := context.Background() | ||
conn, err := pgx.ConnectConfig(ctx, cfg) | ||
if tc.expectError { | ||
require.Error(t, err) | ||
var pgErr *pgconn.PgError | ||
require.True(t, errors.As(err, &pgErr)) | ||
require.Equal(t, pgcode.InvalidParameterValue.String(), pgErr.Code) | ||
return | ||
} | ||
require.NoError(t, err) | ||
var val string | ||
require.NoError(t, conn.QueryRow(ctx, "SELECT current_setting('replication')").Scan(&val)) | ||
require.Equal(t, tc.expectedSessionVar, val) | ||
}) | ||
} | ||
} |
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,29 @@ | ||
// Copyright 2023 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 pgrepl_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"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" | ||
) | ||
|
||
//go:generate ../../util/leaktest/add-leaktest.sh *_test.go | ||
|
||
func TestMain(m *testing.M) { | ||
securityassets.SetLoader(securitytest.EmbeddedAssets) | ||
serverutils.InitTestServerFactory(server.TestServerFactory) | ||
os.Exit(m.Run()) | ||
} |
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