Skip to content

Commit 2b8815b

Browse files
zhangwenchao-123my-ship-it
authored andcommitted
Fix drop directory privilege check.
Now, when we drop directory table we will check whether current user has tablespace's privilege which is not reasonable. In this commit, we will check directory table's privilege in drop directory table.
1 parent d32c144 commit 2b8815b

File tree

5 files changed

+161
-5
lines changed

5 files changed

+161
-5
lines changed

src/backend/catalog/storage_directory_table.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ DirectoryTableDropStorage(Relation rel)
9494
tablespaceoid = spcform->oid;
9595
tablespace_name = pstrdup(NameStr(((Form_pg_tablespace) GETSTRUCT(tuple))->spcname));
9696

97-
/* Must be tablespace owner */
98-
if (!pg_tablespace_ownercheck(tablespaceoid, GetUserId()))
99-
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_TABLESPACE,
100-
tablespace_name);
101-
10297
table_endscan(scandesc);
10398
table_close(spcrel, RowExclusiveLock);
10499

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generated subdirectories
2+
/tmp_check/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# src/test/modules/directory_table/Makefile
2+
3+
MODULE = directory_table
4+
PGFILEDESC = "directory_table - Test DDL of manipulating directory table"
5+
6+
TAP_TESTS = 1
7+
8+
ifdef USE_PGXS
9+
PG_CONFIG = pg_config
10+
PGXS := $(shell $(PG_CONFIG) --pgxs)
11+
include $(PGXS)
12+
else
13+
subdir = src/test/modules/directory_table
14+
top_builddir = ../../../..
15+
include $(top_builddir)/src/Makefile.global
16+
include $(top_srcdir)/contrib/contrib-global.mk
17+
endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test DDL of manipulating directory table
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
# Copyright (c) 2024, Cloudberry Database, HashData Technology Limited.
3+
4+
# Test drop directory table.
5+
#
6+
# This test can only run with Unix-domain sockets.
7+
8+
use strict;
9+
use warnings;
10+
use PostgresNode;
11+
use TestLib;
12+
use Test::More;
13+
if (!$use_unix_sockets)
14+
{
15+
plan skip_all =>
16+
"drop directory table tests cannot run without Unix-domain sockets";
17+
}
18+
else
19+
{
20+
plan tests => 10;
21+
}
22+
23+
# Delete pg_hba.conf from the given node, add a new entry to it
24+
# and then execute a reload to refresh it.
25+
sub reset_pg_hba
26+
{
27+
my $node = shift;
28+
my $hba_method = shift;
29+
30+
unlink($node->data_dir . '/pg_hba.conf');
31+
$node->append_conf('pg_hba.conf', "local all all $hba_method");
32+
$node->reload;
33+
return;
34+
}
35+
36+
# Initialize primary node. Force UTF-8 encoding, so that we can use non-ASCII
37+
# characters in the passwords below.
38+
my $node = get_new_node('primary');
39+
my ($ret, $stdout, $stderr);
40+
$node->init(extra => [ '--locale=C', '--encoding=UTF8' ]);
41+
$node->append_conf('postgresql.conf', "log_connections = on\n");
42+
$node->start;
43+
44+
# Create test directory table.
45+
$node->safe_psql(
46+
'postgres',
47+
"CREATE DIRECTORY TABLE test_dir1;
48+
CREATE DIRECTORY TABLE test_dir2;
49+
");
50+
51+
# Create test roles.
52+
$node->safe_psql(
53+
'postgres',
54+
"SET client_encoding='utf8';
55+
CREATE USER test_user1;
56+
CREATE USER test_user2 SUPERUSER;
57+
");
58+
59+
# Test CREATE DIRECTORY TABLE
60+
($ret, $stdout, $stderr) =
61+
$node->role_psql(
62+
'test_user1',
63+
'postgres',
64+
"CREATE DIRECTORY TABLE test_dir3;"
65+
);
66+
is($ret, 0, 'create directory table succeed');
67+
68+
($ret, $stdout, $stderr) =
69+
$node->role_psql(
70+
'test_user2',
71+
'postgres',
72+
"CREATE DIRECTORY TABLE test_dir4;"
73+
);
74+
is($ret, 0, 'create directory table succeed');
75+
76+
# Test DROP DIRECTORY TABLE
77+
($ret, $stdout, $stderr) =
78+
$node->role_psql(
79+
'test_user1',
80+
'postgres',
81+
"DROP DIRECTORY TABLE test_dir1;"
82+
);
83+
is($ret, 3, 'user has no privileges to drop directory table');
84+
like(
85+
$stderr,
86+
qr/must be owner of directory table test_dir1/,
87+
'expected error from user can not drop directory table'
88+
);
89+
90+
($ret, $stdout, $stderr) =
91+
$node->role_psql(
92+
'test_user2',
93+
'postgres',
94+
"DROP DIRECTORY TABLE test_dir2;"
95+
);
96+
is($ret, 0, 'drop directory table succeed');
97+
98+
($ret, $stdout, $stderr) =
99+
$node->role_psql(
100+
'test_user1',
101+
'postgres',
102+
"DROP DIRECTORY TABLE test_dir3;"
103+
);
104+
is($ret, 0, 'drop directory table succeed');
105+
106+
($ret, $stdout, $stderr) =
107+
$node->role_psql(
108+
'test_user1',
109+
'postgres',
110+
"DROP DIRECTORY TABLE test_dir4;"
111+
);
112+
is($ret, 3, 'user has no privileges to drop directory table');
113+
like(
114+
$stderr,
115+
qr/must be owner of directory table test_dir4/,
116+
'expected error from user can not drop directory table'
117+
);
118+
119+
($ret, $stdout, $stderr) =
120+
$node->role_psql(
121+
'test_user2',
122+
'postgres',
123+
"DROP DIRECTORY TABLE test_dir4;"
124+
);
125+
is($ret, 0, 'drop directory table succeed');
126+
127+
($ret, $stdout, $stderr) =
128+
$node->role_psql(
129+
'test_user2',
130+
'postgres',
131+
"DROP DIRECTORY TABLE test_dir1;"
132+
);
133+
is($ret, 0, 'drop directory table succeed');
134+
135+
# cleanup
136+
reset_pg_hba($node, 'trust');
137+
$node->safe_psql(
138+
'postgres',
139+
"DROP USER test_user1;
140+
DROP USER test_user2;
141+
");

0 commit comments

Comments
 (0)