|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package sql |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 13 | + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestAdminShowExternalConnection(t *testing.T) { |
| 21 | + defer leaktest.AfterTest(t)() |
| 22 | + defer log.Scope(t).Close(t) |
| 23 | + |
| 24 | + ctx := context.Background() |
| 25 | + |
| 26 | + s, db, _ := serverutils.StartServer(t, base.TestServerArgs{}) |
| 27 | + defer s.Stopper().Stop(ctx) |
| 28 | + srv := s.ApplicationLayer() |
| 29 | + |
| 30 | + adminDB := sqlutils.MakeSQLRunner(db) |
| 31 | + const password = "correcthorsebatterystaple" |
| 32 | + adminDB.Exec(t, "CREATE USER testuser1 WITH PASSWORD $1", password) |
| 33 | + adminDB.Exec(t, "GRANT SYSTEM EXTERNALCONNECTION TO testuser1") |
| 34 | + |
| 35 | + nonAdminDB := sqlutils.MakeSQLRunner(srv.SQLConn( |
| 36 | + t, serverutils.UserPassword("testuser1", password), serverutils.ClientCerts(false), |
| 37 | + )) |
| 38 | + nonAdminDB.Exec(t, "CREATE EXTERNAL CONNECTION foo AS 'userfile:///'") |
| 39 | + |
| 40 | + t.Run("non-admin user cannot see other external connections", func(t *testing.T) { |
| 41 | + _, err := db.Exec("CREATE USER testuser2 WITH PASSWORD $1", password) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + nonAdminDB2 := sqlutils.MakeSQLRunner(srv.SQLConn( |
| 45 | + t, serverutils.UserPassword("testuser2", password), serverutils.ClientCerts(false), |
| 46 | + )) |
| 47 | + |
| 48 | + rows := nonAdminDB2.QueryStr(t, "SHOW EXTERNAL CONNECTIONS") |
| 49 | + require.Empty(t, rows, "expected no rows for non-admin user without privileges") |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("admin user can see all external connections", func(t *testing.T) { |
| 53 | + rows := adminDB.QueryStr(t, "SHOW EXTERNAL CONNECTIONS") |
| 54 | + require.Len(t, rows, 1, "expected one row for admin user") |
| 55 | + }) |
| 56 | +} |
0 commit comments