Skip to content

Commit 350dd50

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 947aab2 commit 350dd50

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
@@ -18133,6 +18133,120 @@ CreateDynamicTable(
1813318133
)
1813418134

1813518135

18136+
---------- Input ----------
18137+
GRANT CREATE TASK ON *.* TO role role1
18138+
---------- Output ---------
18139+
GRANT CREATE DATABASE ON *.* TO ROLE 'role1'
18140+
---------- AST ------------
18141+
Grant(
18142+
GrantStmt {
18143+
source: Privs {
18144+
privileges: [
18145+
CreateDatabase,
18146+
],
18147+
level: Global,
18148+
},
18149+
principal: Role(
18150+
"role1",
18151+
),
18152+
},
18153+
)
18154+
18155+
18156+
---------- Input ----------
18157+
GRANT ownership ON task MyTask1 TO role role1
18158+
---------- Output ---------
18159+
GRANT OWNERSHIP ON TASK MyTask1 TO ROLE 'role1'
18160+
---------- AST ------------
18161+
Grant(
18162+
GrantStmt {
18163+
source: Privs {
18164+
privileges: [
18165+
Ownership,
18166+
],
18167+
level: Task(
18168+
"MyTask1",
18169+
),
18170+
},
18171+
principal: Role(
18172+
"role1",
18173+
),
18174+
},
18175+
)
18176+
18177+
18178+
---------- Input ----------
18179+
GRANT ownership ON task MyTask1 TO role role2
18180+
---------- Output ---------
18181+
GRANT OWNERSHIP ON TASK MyTask1 TO ROLE 'role2'
18182+
---------- AST ------------
18183+
Grant(
18184+
GrantStmt {
18185+
source: Privs {
18186+
privileges: [
18187+
Ownership,
18188+
],
18189+
level: Task(
18190+
"MyTask1",
18191+
),
18192+
},
18193+
principal: Role(
18194+
"role2",
18195+
),
18196+
},
18197+
)
18198+
18199+
18200+
---------- Input ----------
18201+
GRANT drop ON task MyTask1 TO u2
18202+
---------- Output ---------
18203+
GRANT DROP ON TASK MyTask1 TO USER 'u2'@'%'
18204+
---------- AST ------------
18205+
Grant(
18206+
GrantStmt {
18207+
source: Privs {
18208+
privileges: [
18209+
Drop,
18210+
],
18211+
level: Task(
18212+
"MyTask1",
18213+
),
18214+
},
18215+
principal: User(
18216+
UserIdentity {
18217+
username: "u2",
18218+
hostname: "%",
18219+
},
18220+
),
18221+
},
18222+
)
18223+
18224+
18225+
---------- Input ----------
18226+
GRANT alter ON task MyTask1 TO u1
18227+
---------- Output ---------
18228+
GRANT ALTER ON TASK MyTask1 TO USER 'u1'@'%'
18229+
---------- AST ------------
18230+
Grant(
18231+
GrantStmt {
18232+
source: Privs {
18233+
privileges: [
18234+
Alter,
18235+
],
18236+
level: Task(
18237+
"MyTask1",
18238+
),
18239+
},
18240+
principal: User(
18241+
UserIdentity {
18242+
username: "u1",
18243+
hostname: "%",
18244+
},
18245+
),
18246+
},
18247+
)
18248+
18249+
1813618250
---------- Input ----------
1813718251
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
1813818252
---------- 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)