Skip to content

Commit 348da41

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 2bc021d commit 348da41

File tree

21 files changed

+828
-309
lines changed

21 files changed

+828
-309
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
@@ -18350,6 +18350,120 @@ CreateDynamicTable(
1835018350
)
1835118351

1835218352

18353+
---------- Input ----------
18354+
GRANT CREATE TASK ON *.* TO role role1
18355+
---------- Output ---------
18356+
GRANT CREATE DATABASE ON *.* TO ROLE 'role1'
18357+
---------- AST ------------
18358+
Grant(
18359+
GrantStmt {
18360+
source: Privs {
18361+
privileges: [
18362+
CreateDatabase,
18363+
],
18364+
level: Global,
18365+
},
18366+
principal: Role(
18367+
"role1",
18368+
),
18369+
},
18370+
)
18371+
18372+
18373+
---------- Input ----------
18374+
GRANT ownership ON task MyTask1 TO role role1
18375+
---------- Output ---------
18376+
GRANT OWNERSHIP ON TASK MyTask1 TO ROLE 'role1'
18377+
---------- AST ------------
18378+
Grant(
18379+
GrantStmt {
18380+
source: Privs {
18381+
privileges: [
18382+
Ownership,
18383+
],
18384+
level: Task(
18385+
"MyTask1",
18386+
),
18387+
},
18388+
principal: Role(
18389+
"role1",
18390+
),
18391+
},
18392+
)
18393+
18394+
18395+
---------- Input ----------
18396+
GRANT ownership ON task MyTask1 TO role role2
18397+
---------- Output ---------
18398+
GRANT OWNERSHIP ON TASK MyTask1 TO ROLE 'role2'
18399+
---------- AST ------------
18400+
Grant(
18401+
GrantStmt {
18402+
source: Privs {
18403+
privileges: [
18404+
Ownership,
18405+
],
18406+
level: Task(
18407+
"MyTask1",
18408+
),
18409+
},
18410+
principal: Role(
18411+
"role2",
18412+
),
18413+
},
18414+
)
18415+
18416+
18417+
---------- Input ----------
18418+
GRANT drop ON task MyTask1 TO u2
18419+
---------- Output ---------
18420+
GRANT DROP ON TASK MyTask1 TO USER 'u2'@'%'
18421+
---------- AST ------------
18422+
Grant(
18423+
GrantStmt {
18424+
source: Privs {
18425+
privileges: [
18426+
Drop,
18427+
],
18428+
level: Task(
18429+
"MyTask1",
18430+
),
18431+
},
18432+
principal: User(
18433+
UserIdentity {
18434+
username: "u2",
18435+
hostname: "%",
18436+
},
18437+
),
18438+
},
18439+
)
18440+
18441+
18442+
---------- Input ----------
18443+
GRANT alter ON task MyTask1 TO u1
18444+
---------- Output ---------
18445+
GRANT ALTER ON TASK MyTask1 TO USER 'u1'@'%'
18446+
---------- AST ------------
18447+
Grant(
18448+
GrantStmt {
18449+
source: Privs {
18450+
privileges: [
18451+
Alter,
18452+
],
18453+
level: Task(
18454+
"MyTask1",
18455+
),
18456+
},
18457+
principal: User(
18458+
UserIdentity {
18459+
username: "u1",
18460+
hostname: "%",
18461+
},
18462+
),
18463+
},
18464+
)
18465+
18466+
1835318467
---------- Input ----------
1835418468
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
1835518469
---------- 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)