Handle support/permissions errors in info call (#635)
* Handle support/permissions errors in info call * Fix linting
This commit is contained in:
parent
96bd15b3e2
commit
b2b0094b5e
@ -206,7 +206,15 @@ func (client *Client) TestWithTimeout(timeout time.Duration) (result error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) Info() (*Result, error) {
|
func (client *Client) Info() (*Result, error) {
|
||||||
return client.query(statements.Info)
|
result, err := client.query(statements.Info)
|
||||||
|
if err != nil {
|
||||||
|
msg := err.Error()
|
||||||
|
if strings.Contains(msg, "inet_") && (strings.Contains(msg, "not supported") || strings.Contains(msg, "permission denied")) {
|
||||||
|
// Fetch client information without inet_ function calls
|
||||||
|
result, err = client.query(statements.InfoSimple)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) Databases() ([]string, error) {
|
func (client *Client) Databases() ([]string, error) {
|
||||||
|
@ -203,21 +203,57 @@ func testTest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testInfo(t *testing.T) {
|
func testInfo(t *testing.T) {
|
||||||
expected := []string{
|
t.Run("normal", func(t *testing.T) {
|
||||||
"session_user",
|
expected := []string{
|
||||||
"current_user",
|
"session_user",
|
||||||
"current_database",
|
"current_user",
|
||||||
"current_schemas",
|
"current_database",
|
||||||
"inet_client_addr",
|
"current_schemas",
|
||||||
"inet_client_port",
|
"inet_client_addr",
|
||||||
"inet_server_addr",
|
"inet_client_port",
|
||||||
"inet_server_port",
|
"inet_server_addr",
|
||||||
"version",
|
"inet_server_port",
|
||||||
}
|
"version",
|
||||||
|
}
|
||||||
|
|
||||||
res, err := testClient.Info()
|
res, err := testClient.Info()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, expected, res.Columns)
|
assert.Equal(t, expected, res.Columns)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("with restrictions", func(t *testing.T) {
|
||||||
|
expected := []string{
|
||||||
|
"session_user",
|
||||||
|
"current_user",
|
||||||
|
"current_database",
|
||||||
|
"current_schemas",
|
||||||
|
"version",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare a new user and database
|
||||||
|
testClient.db.MustExec("DROP DATABASE IF EXISTS testdb")
|
||||||
|
testClient.db.Exec("DROP OWNED BY IF EXISTS testuser") //nolint:all
|
||||||
|
testClient.db.MustExec("DROP ROLE IF EXISTS testuser")
|
||||||
|
testClient.db.MustExec("CREATE ROLE testuser WITH PASSWORD 'secret' LOGIN NOSUPERUSER NOINHERIT")
|
||||||
|
testClient.db.MustExec("CREATE DATABASE testdb OWNER testuser")
|
||||||
|
|
||||||
|
// Disable access to inet_ calls for new user
|
||||||
|
url := fmt.Sprintf("postgres://%s:@%s:%s/testdb?sslmode=disable", serverUser, serverHost, serverPort)
|
||||||
|
client, err := NewFromUrl(url, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
client.db.MustExec("REVOKE EXECUTE ON FUNCTION inet_client_addr() FROM PUBLIC")
|
||||||
|
assert.NoError(t, client.Close())
|
||||||
|
|
||||||
|
// Connect using new user
|
||||||
|
url = fmt.Sprintf("postgres://testuser:secret@%s:%s/testdb?sslmode=disable", serverHost, serverPort)
|
||||||
|
client, err = NewFromUrl(url, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
res, err := client.Info()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, res.Columns)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testActivity(t *testing.T) {
|
func testActivity(t *testing.T) {
|
||||||
|
@ -14,6 +14,9 @@ var (
|
|||||||
//go:embed sql/info.sql
|
//go:embed sql/info.sql
|
||||||
Info string
|
Info string
|
||||||
|
|
||||||
|
//go:embed sql/info_simple.sql
|
||||||
|
InfoSimple string
|
||||||
|
|
||||||
//go:embed sql/estimated_row_count.sql
|
//go:embed sql/estimated_row_count.sql
|
||||||
EstimatedTableRowCount string
|
EstimatedTableRowCount string
|
||||||
|
|
||||||
|
6
pkg/statements/sql/info_simple.sql
Normal file
6
pkg/statements/sql/info_simple.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
SELECT
|
||||||
|
session_user,
|
||||||
|
current_user,
|
||||||
|
current_database(),
|
||||||
|
current_schemas(false),
|
||||||
|
version()
|
Loading…
x
Reference in New Issue
Block a user