Skip to content

Commit 5204627

Browse files
committed
Task privilege Part2
1. add function: list_task_ownerships, only list task prefix. 2. add visibility in task/task_history system table. 3. refactor showtasks, directly use select * from system.task; 4. fix ut err
1 parent 88bef1f commit 5204627

File tree

22 files changed

+830
-310
lines changed

22 files changed

+830
-310
lines changed

src/common/cloud_control/proto/task.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ message ShowTasksRequest {// every owner has a roles list like ["role1", "role2"
119119
int32 result_limit = 4;
120120
repeated string owners = 5; // all available roles under current client
121121
repeated string task_ids = 6; // all task ids which permit to access for given user
122+
repeated string task_names = 7; // all task names which permit to access for given user
122123
}
123124

124125
message ShowTasksResponse {
@@ -170,6 +171,7 @@ message ShowTaskRunsRequest {
170171
repeated string owners = 6;
171172
repeated string task_ids = 7;
172173
string task_name = 8;
174+
repeated string task_names = 9;
173175

174176
optional int32 page_size = 90; // 100 by default
175177
optional int64 next_page_token = 91;

src/meta/app/src/principal/tenant_ownership_object_ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ mod tests {
253253
assert_eq!(role_grantee, parsed);
254254
}
255255

256-
// udf
256+
// task
257257
{
258258
let role_grantee = TenantOwnershipObjectIdent::new_unchecked(
259259
Tenant::new_literal("test"),

src/query/ast/tests/it/testdata/statement-error.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,3 +916,48 @@ error:
916916
| while parsing `DROP TABLE [IF EXISTS] [<database>.]<table>`
917917

918918

919+
---------- Input ----------
920+
GRANT CREATE TASK ON task mytask1 TO role role1
921+
---------- Output ---------
922+
error:
923+
--> SQL:1:22
924+
|
925+
1 | GRANT CREATE TASK ON task mytask1 TO role role1
926+
| ----- ------ ^^^^ unexpected `task`, expecting `FALSE`, <QuotedString>, <LiteralInteger>, `TRUE`, `IDENTIFIER`, <PGLiteralHex>, <MySQLLiteralHex>, `*`, or <Ident>
927+
| | |
928+
| | while parsing <privileges> ON <privileges_level>
929+
| while parsing `GRANT { ROLE <role_name> | schemaObjectPrivileges | ALL [ PRIVILEGES ] ON <privileges_level> } TO { [ROLE <role_name>] | [USER] <user> }`
930+
931+
932+
---------- Input ----------
933+
GRANT ownership ON task MyTask1 TO u1
934+
---------- Output ---------
935+
error:
936+
--> SQL:1:36
937+
|
938+
1 | GRANT ownership ON task MyTask1 TO u1
939+
| ----- ^^ unexpected `u1`, expecting `ROLE`
940+
| |
941+
| while parsing GRANT OWNERSHIP ON <privileges_level> TO ROLE <role_name>
942+
943+
944+
---------- Input ----------
945+
GRANT select ON task MyTask1 TO u2
946+
---------- Output ---------
947+
error:
948+
--> SQL:1:17
949+
|
950+
1 | GRANT select ON task MyTask1 TO u2
951+
| ^^^^ unexpected `task`, expecting `FALSE`, `TABLE`, <QuotedString>, <LiteralInteger>, `TRUE`, `IDENTIFIER`, <PGLiteralHex>, <MySQLLiteralHex>, `DATABASE`, `*`, or <Ident>
952+
953+
954+
---------- Input ----------
955+
GRANT usage ON task MyTask1 TO u1
956+
---------- Output ---------
957+
error:
958+
--> SQL:1:16
959+
|
960+
1 | GRANT usage ON task MyTask1 TO u1
961+
| ^^^^ unexpected `task`, expecting `FALSE`, `TABLE`, <QuotedString>, <LiteralInteger>, `TRUE`, `IDENTIFIER`, <PGLiteralHex>, <MySQLLiteralHex>, `DATABASE`, `UDF`, `*`, or <Ident>
962+
963+

src/query/ast/tests/it/testdata/statement.txt

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18002,6 +18002,120 @@ CreateDynamicTable(
1800218002
)
1800318003

1800418004

18005+
---------- Input ----------
18006+
GRANT CREATE TASK ON *.* TO role role1
18007+
---------- Output ---------
18008+
GRANT CREATE DATABASE ON *.* TO ROLE 'role1'
18009+
---------- AST ------------
18010+
Grant(
18011+
GrantStmt {
18012+
source: Privs {
18013+
privileges: [
18014+
CreateDatabase,
18015+
],
18016+
level: Global,
18017+
},
18018+
principal: Role(
18019+
"role1",
18020+
),
18021+
},
18022+
)
18023+
18024+
18025+
---------- Input ----------
18026+
GRANT ownership ON task MyTask1 TO role role1
18027+
---------- Output ---------
18028+
GRANT OWNERSHIP ON TASK MyTask1 TO ROLE 'role1'
18029+
---------- AST ------------
18030+
Grant(
18031+
GrantStmt {
18032+
source: Privs {
18033+
privileges: [
18034+
Ownership,
18035+
],
18036+
level: Task(
18037+
"MyTask1",
18038+
),
18039+
},
18040+
principal: Role(
18041+
"role1",
18042+
),
18043+
},
18044+
)
18045+
18046+
18047+
---------- Input ----------
18048+
GRANT ownership ON task MyTask1 TO role role2
18049+
---------- Output ---------
18050+
GRANT OWNERSHIP ON TASK MyTask1 TO ROLE 'role2'
18051+
---------- AST ------------
18052+
Grant(
18053+
GrantStmt {
18054+
source: Privs {
18055+
privileges: [
18056+
Ownership,
18057+
],
18058+
level: Task(
18059+
"MyTask1",
18060+
),
18061+
},
18062+
principal: Role(
18063+
"role2",
18064+
),
18065+
},
18066+
)
18067+
18068+
18069+
---------- Input ----------
18070+
GRANT drop ON task MyTask1 TO u2
18071+
---------- Output ---------
18072+
GRANT DROP ON TASK MyTask1 TO USER 'u2'@'%'
18073+
---------- AST ------------
18074+
Grant(
18075+
GrantStmt {
18076+
source: Privs {
18077+
privileges: [
18078+
Drop,
18079+
],
18080+
level: Task(
18081+
"MyTask1",
18082+
),
18083+
},
18084+
principal: User(
18085+
UserIdentity {
18086+
username: "u2",
18087+
hostname: "%",
18088+
},
18089+
),
18090+
},
18091+
)
18092+
18093+
18094+
---------- Input ----------
18095+
GRANT alter ON task MyTask1 TO u1
18096+
---------- Output ---------
18097+
GRANT ALTER ON TASK MyTask1 TO USER 'u1'@'%'
18098+
---------- AST ------------
18099+
Grant(
18100+
GrantStmt {
18101+
source: Privs {
18102+
privileges: [
18103+
Alter,
18104+
],
18105+
level: Task(
18106+
"MyTask1",
18107+
),
18108+
},
18109+
principal: User(
18110+
UserIdentity {
18111+
username: "u1",
18112+
hostname: "%",
18113+
},
18114+
),
18115+
},
18116+
)
18117+
18118+
1800518119
---------- Input ----------
1800618120
CREATE TASK IF NOT EXISTS MyTask1 WAREHOUSE = 'MyWarehouse' SCHEDULE = 15 MINUTE SUSPEND_TASK_AFTER_NUM_FAILURES = 3 ERROR_INTEGRATION = 'notification_name' COMMENT = 'This is test task 1' DATABASE = 'target', TIMEZONE = 'America/Los Angeles' AS SELECT * FROM MyTable1
1800718121
---------- Output ---------

src/query/management/src/role/role_api.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use databend_common_exception::ErrorCode;
1516
use databend_common_exception::Result;
1617
use databend_common_meta_app::principal::OwnershipInfo;
1718
use databend_common_meta_app::principal::OwnershipObject;
@@ -30,6 +31,10 @@ pub trait RoleApi: Sync + Send {
3031

3132
async fn get_ownerships(&self) -> Result<Vec<SeqV<OwnershipInfo>>>;
3233

34+
async fn list_tasks_ownerships(
35+
&self,
36+
) -> std::result::Result<Vec<SeqV<OwnershipInfo>>, ErrorCode>;
37+
3338
/// General role update.
3439
///
3540
/// It fetches the role that matches the specified seq number, update it in place, then write it back with the seq it sees.

src/query/management/src/role/role_mgr.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,28 @@ impl RoleApi for RoleMgr {
217217
Ok(r)
218218
}
219219

220+
#[async_backtrace::framed]
221+
#[minitrace::trace]
222+
async fn list_tasks_ownerships(&self) -> Result<Vec<SeqV<OwnershipInfo>>, ErrorCode> {
223+
let mut task_object_owner_prefix = self.ownership_object_prefix();
224+
task_object_owner_prefix.push_str("task-by-name/");
225+
let values = self
226+
.kv_api
227+
.prefix_list_kv(task_object_owner_prefix.as_str())
228+
.await?;
229+
230+
let mut r = vec![];
231+
232+
let mut quota = Quota::new(func_name!());
233+
234+
for (key, val) in values {
235+
let u = check_and_upgrade_to_pb(&mut quota, key, &val, self.kv_api.as_ref()).await?;
236+
r.push(u);
237+
}
238+
239+
Ok(r)
240+
}
241+
220242
/// General role update.
221243
///
222244
/// It fetch the role that matches the specified seq number, update it in place, then write it back with the seq it sees.

0 commit comments

Comments
 (0)