Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] Fix submit and run error #10792

Merged
merged 15 commits into from
Jul 8, 2022
Merged

Conversation

zhongjiajie
Copy link
Member

@zhongjiajie zhongjiajie commented Jul 5, 2022

because we add permission, so it failed out
createQueue method. this patch fix it and
do some refactor of our tenant and queue
validator code

close: #10794

because we add permission, so it failed out
createQueue method. this patch fix it and
do some refactor of our tenant and queue
validator code
@zhongjiajie zhongjiajie self-assigned this Jul 5, 2022
@zhongjiajie zhongjiajie added bug Something isn't working Python labels Jul 5, 2022
@zhongjiajie zhongjiajie marked this pull request as ready for review July 5, 2022 08:24
@zhongjiajie zhongjiajie added this to the 3.1.0-alpha milestone Jul 5, 2022
@zhongjiajie
Copy link
Member Author

I changed the function name according to your suggestion @SbloodyS

@zhongjiajie
Copy link
Member Author

I find out I forget add tests on it, will add it right now

tenant.setUpdateTime(now);

// save
tenantMapper.insert(tenant);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the original method to insert tenant, then it can keep the unified logic, such as add permission check, see #10718 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should ignore the permission check currently for pydolphinscheudler, if we have to consider the permission, we have to create a role and connect the role for the current user who creates the workflow from pydolphinscheudler to define code.
But as we know, pydolphinscheudler is a batch operator API instead of an interactive API, which mean that it will batch create or update all related object when is submit. If users change the definition in

with ProcessDefinition(
name="tutorial",
schedule="0 0 0 * * ? *",
start_time="2021-01-01",
tenant="tenant_exists",
) as pd:
like change users or tenant, it have to reflash all the user and role relation to make the change work.
The most important thing is, that it should work finally, otherwise, users will think there is a bug here, IMO is another method to ignore the permission. If we instead to add RBAC to pydolphinscheudler, will make dolphinscheduler have mulitple dummy role and the relation between roles and users

Copy link
Member Author

@zhongjiajie zhongjiajie Jul 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can indeed possible to abstract the insert part like below to a new private function:

        Tenant tenant = new Tenant();
        Date now = new Date();
        tenant.setTenantCode(tenantCode);
        tenant.setQueueId(newQueue.getId());
        tenant.setDescription(desc);
        tenant.setCreateTime(now);
        tenant.setUpdateTime(now);

        // save
        tenantMapper.insert(tenant);

like

private Tenant createRecordToDB(String tenantCode, int queueId) {
        Tenant tenant = new Tenant();
        Date now = new Date();
        tenant.setTenantCode(tenantCode);
        tenant.setQueueId(queueID);
        tenant.setDescription(desc);
        tenant.setCreateTime(now);
        tenant.setUpdateTime(now);

        // save
        tenantMapper.insert(tenant);
        return tenant;
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can indeed possible to abstract the insert part like below to a new private function:

        Tenant tenant = new Tenant();
        Date now = new Date();
        tenant.setTenantCode(tenantCode);
        tenant.setQueueId(newQueue.getId());
        tenant.setDescription(desc);
        tenant.setCreateTime(now);
        tenant.setUpdateTime(now);

        // save
        tenantMapper.insert(tenant);

like

private Tenant createRecordToDB(String tenantCode, int queueId) {
        Tenant tenant = new Tenant();
        Date now = new Date();
        tenant.setTenantCode(tenantCode);
        tenant.setQueueId(queueID);
        tenant.setDescription(desc);
        tenant.setCreateTime(now);
        tenant.setUpdateTime(now);

        // save
        tenantMapper.insert(tenant);
        return tenant;
};

Yes, permission check is just a case, and maybe we will add other logic when create tenant or queue. If we use a new method, when someone modify the insert logic, he should modify two places, otherwise there may be problems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But currently, python api will create or update objects with admin permission

@zhongjiajie
Copy link
Member Author

zhongjiajie commented Jul 5, 2022

I find out I forget add tests on it, will add it right now

Add in edc1ff8

@codecov-commenter
Copy link

codecov-commenter commented Jul 5, 2022

Codecov Report

Merging #10792 (05e65e0) into dev (d2fe16d) will increase coverage by 0.06%.
The diff coverage is 33.67%.

❗ Current head 05e65e0 differs from pull request most recent head 09cb86b. Consider uploading reports for the commit 09cb86b to get more accurate results

@@             Coverage Diff              @@
##                dev   #10792      +/-   ##
============================================
+ Coverage     40.61%   40.67%   +0.06%     
- Complexity     4817     4826       +9     
============================================
  Files           902      913      +11     
  Lines         36221    36306      +85     
  Branches       3999     3989      -10     
============================================
+ Hits          14711    14768      +57     
- Misses        20045    20065      +20     
- Partials       1465     1473       +8     
Impacted Files Coverage Δ
...che/dolphinscheduler/api/k8s/K8sClientService.java 4.25% <0.00%> (-2.13%) ⬇️
...che/dolphinscheduler/api/python/PythonGateway.java 21.24% <0.00%> (+1.81%) ⬆️
...duler/api/service/impl/AccessTokenServiceImpl.java 83.72% <ø> (ø)
...eduler/api/service/impl/AlertGroupServiceImpl.java 82.52% <ø> (+1.57%) ⬆️
...eduler/api/service/impl/DataSourceServiceImpl.java 29.49% <ø> (+0.19%) ⬆️
...duler/api/service/impl/EnvironmentServiceImpl.java 80.51% <ø> (ø)
...api/service/impl/ProcessDefinitionServiceImpl.java 31.37% <ø> (ø)
...i/service/impl/ProcessTaskRelationServiceImpl.java 21.03% <ø> (ø)
...scheduler/api/service/impl/ProjectServiceImpl.java 57.92% <ø> (ø)
...heduler/api/service/impl/ResourcesServiceImpl.java 48.56% <ø> (ø)
... and 71 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d2fe16d...09cb86b. Read the comment docs.

@zhongjiajie
Copy link
Member Author

I add new commit f4fc406 to fix this #10792 (comment)

@zhongjiajie
Copy link
Member Author

Maybe we should re-design the permission in pydolphinscheduler after we add role in our codebase

@@ -194,7 +194,7 @@ public enum Status {
QUERY_WORKFLOW_LINEAGE_ERROR(10161, "query workflow lineage error", "查询血缘失败"),
QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR(10162, "query authorized and user created project error error", "查询授权的和用户创建的项目错误"),
DELETE_PROCESS_DEFINITION_BY_CODE_FAIL(10163, "delete process definition by code fail, for there are {0} process instances in executing using it", "删除工作流定义失败,有[{0}]个运行中的工作流实例正在使用"),
CHECK_OS_TENANT_CODE_ERROR(10164, "Please enter the English os tenant code", "请输入英文操作系统租户"),
CHECK_OS_TENANT_CODE_ERROR(10164, "Tenant code invalid, should follow linux's users naming conventions", "非法的租户名,需要遵守 Linux 用户命名规范"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the reviewers, I change this message due to I think the pervious message is wrong

// not exist
Map<String, Object> result = queueService.updateQueue(getLoginUser(), 0, "queue", queueName);
logger.info(result.toString());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I delete all the logger is our tests, because I think is redundance, correct me if I am wrong

@zhongjiajie
Copy link
Member Author

I suggest python API is always created or updated in Admin role currently, and all our validate expect permission should including in valid function, which will call in both createTenant(use in web ui and open api) and createTenantIfNotExists(use in python api) to keep the same logic

Comment on lines +39 to +43
@ExceptionHandler(ServiceException.class)
public Result exceptionHandler(ServiceException e, HandlerMethod hm) {
logger.error("ServiceException: ", e);
return new Result(e.getCode(), e.getMessage());
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add a new handler to handle Service Exception throw from service side, according to @caishunfeng suggetion. So we can throw exception in service side, and keep our code clear

@sonarcloud
Copy link

sonarcloud bot commented Jul 7, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 18 Code Smells

79.6% 79.6% Coverage
0.0% 0.0% Duplication

@zhongjiajie
Copy link
Member Author

zhongjiajie commented Jul 7, 2022

Finally, the CI pass 🤣

Copy link
Member

@SbloodyS SbloodyS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@zhongjiajie zhongjiajie merged commit ae6aa53 into apache:dev Jul 8, 2022
@zhongjiajie zhongjiajie deleted the f-py-api-submit branch July 8, 2022 02:54
@zhongjiajie
Copy link
Member Author

Thanks all

zhongjiajie added a commit to zhongjiajie/dolphinscheduler that referenced this pull request Jul 10, 2022
because we add permission, so it failed out
createQueue method. this patch fix it and
do some refactor of our tenant and queue
validator code
zhongjiajie added a commit that referenced this pull request Jul 28, 2022
because we add permission, so it failed out
createQueue method. this patch fix it and
do some refactor of our tenant and queue
validator code

(cherry picked from commit ae6aa53)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] [python] Can not run python api definition in dev branch
4 participants