Skip to content

Commit 9df89cf

Browse files
authored
Use bootstrap user as the default tenant (#13825)
1 parent 85605bb commit 9df89cf

19 files changed

Lines changed: 171 additions & 257 deletions

File tree

docs/docs/en/guide/start/quick-start.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ The brief is as follows:
4343

4444
We can create a tenant in DolphinScheduler `Security -> Tenant Manage` page.
4545

46+
> NOTE: The user will bind to a default tenant when it is created, if you use the default tenant, the task will be executed by worker's bootstrap user.
47+
4648
![create-tenant](../../../../img/start/create-tenant.gif)
4749

4850
#### Assign Tenant to User

docs/docs/zh/guide/start/quick-start.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ Tenant是使用DolphinScheduler时绕不开的一个概念,所以先简单介
3333

3434
我们可以在 DolphinScheduler `Security -> Tenant Manage` 页面创建租户。
3535

36-
![create-tenant](../../../../img/start/create-tenant.gif)
36+
> ![create-tenant](../../../../img/start/create-tenant.gif)
37+
>
38+
> 注意:如果没有关联租户,则会使用默认租户,默认租户为default,会使用程序启动用户执行任务。
3739
3840
#### 将租户分配给用户
3941

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import org.apache.dolphinscheduler.dao.entity.Schedule;
7171
import org.apache.dolphinscheduler.dao.entity.TaskDefinition;
7272
import org.apache.dolphinscheduler.dao.entity.TaskGroupQueue;
73-
import org.apache.dolphinscheduler.dao.entity.Tenant;
7473
import org.apache.dolphinscheduler.dao.entity.User;
7574
import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper;
7675
import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper;
@@ -241,13 +240,6 @@ public Map<String, Object> execProcessInstance(User loginUser, long projectCode,
241240
processDefinition.getVersion());
242241
// check current version whether include startNodeList
243242
checkStartNodeList(startNodeList, processDefinitionCode, processDefinition.getVersion());
244-
if (!checkTenantSuitable(processDefinition)) {
245-
log.error(
246-
"There is not any valid tenant for the process definition, processDefinitionCode:{}, processDefinitionName:{}.",
247-
processDefinition.getCode(), processDefinition.getName());
248-
putMsg(result, Status.TENANT_NOT_SUITABLE);
249-
return result;
250-
}
251243

252244
checkScheduleTimeNumExceed(commandType, cronTime);
253245
checkMasterExists();
@@ -466,14 +458,6 @@ public WorkflowExecuteResponse executeTask(User loginUser, long projectCode, Int
466458
this.checkProcessDefinitionValid(projectCode, processDefinition, processInstance.getProcessDefinitionCode(),
467459
processInstance.getProcessDefinitionVersion());
468460

469-
if (!checkTenantSuitable(processDefinition)) {
470-
log.error(
471-
"There is not any valid tenant for the process definition, processDefinitionId:{}, processDefinitionCode:{}, ",
472-
processDefinition.getId(), processDefinition.getName());
473-
putMsg(response, Status.TENANT_NOT_SUITABLE);
474-
return response;
475-
}
476-
477461
// get the startParams user specified at the first starting while repeat running is needed
478462

479463
long startNodeListLong;
@@ -554,18 +538,6 @@ public Map<String, Object> forceStartTaskInstance(User loginUser, int queueId) {
554538
return forceStart(processInstance, taskGroupQueue);
555539
}
556540

557-
/**
558-
* check tenant suitable
559-
*
560-
* @param processDefinition process definition
561-
* @return true if tenant suitable, otherwise return false
562-
*/
563-
private boolean checkTenantSuitable(ProcessDefinition processDefinition) {
564-
Tenant tenant =
565-
processService.getTenantForProcess(processDefinition.getTenantId(), processDefinition.getUserId());
566-
return tenant != null;
567-
}
568-
569541
public void checkStartNodeList(String startNodeList, Long processDefinitionCode, int version) {
570542
if (StringUtils.isNotEmpty(startNodeList)) {
571543
List<ProcessTaskRelation> processTaskRelations =

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java

Lines changed: 24 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,7 @@ public Result<Object> createDirectory(User loginUser,
161161
return result;
162162
}
163163

164-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
165-
if (tenant == null) {
166-
log.error("tenant not exists");
167-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
168-
return result;
169-
}
170-
171-
String tenantCode = tenant.getTenantCode();
164+
String tenantCode = getTenantCode(user);
172165

173166
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, "")) {
174167
log.error("current user does not have permission");
@@ -240,14 +233,7 @@ public Result<Object> createResource(User loginUser,
240233
return result;
241234
}
242235

243-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
244-
if (tenant == null) {
245-
log.error("tenant not exists");
246-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
247-
return result;
248-
}
249-
250-
String tenantCode = tenant.getTenantCode();
236+
String tenantCode = getTenantCode(user);
251237

252238
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, "")) {
253239
log.error("current user does not have permission");
@@ -378,14 +364,7 @@ public Result<Object> updateResource(User loginUser,
378364
return result;
379365
}
380366

381-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
382-
if (tenant == null) {
383-
log.error("tenant not exists");
384-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
385-
return result;
386-
}
387-
388-
String tenantCode = tenant.getTenantCode();
367+
String tenantCode = getTenantCode(user);
389368

390369
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
391370
log.error("current user does not have permission");
@@ -594,14 +573,7 @@ public Result<PageInfo<StorageEntity>> queryResourceListPaging(User loginUser, S
594573
return result;
595574
}
596575

597-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
598-
if (tenant == null) {
599-
log.error("tenant not exists");
600-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
601-
return result;
602-
}
603-
604-
String tenantCode = tenant.getTenantCode();
576+
String tenantCode = getTenantCode(user);
605577

606578
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
607579
log.error("current user does not have permission");
@@ -619,7 +591,7 @@ public Result<PageInfo<StorageEntity>> queryResourceListPaging(User loginUser, S
619591
List<User> userList = userMapper.selectList(null);
620592
Set<String> visitedTenantEntityCode = new HashSet<>();
621593
for (User userEntity : userList) {
622-
String tenantEntityCode = tenantMapper.queryById(userEntity.getTenantId()).getTenantCode();
594+
String tenantEntityCode = getTenantCode(userEntity);
623595
if (!visitedTenantEntityCode.contains(tenantEntityCode)) {
624596
defaultPath = storageOperate.getResDir(tenantEntityCode);
625597
if (type.equals(ResourceType.UDF)) {
@@ -723,7 +695,7 @@ private boolean upload(User loginUser, String fullName, MultipartFile file, Reso
723695
return false;
724696
}
725697
// query tenant
726-
String tenantCode = tenantMapper.queryById(loginUser.getTenantId()).getTenantCode();
698+
String tenantCode = getTenantCode(loginUser);
727699
// random file name
728700
String localFilename = FileUtils.getUploadFilename(tenantCode, UUID.randomUUID().toString());
729701

@@ -768,13 +740,7 @@ public Map<String, Object> queryResourceList(User loginUser, ResourceType type,
768740
return null;
769741
}
770742

771-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
772-
if (tenant == null) {
773-
log.error("tenant not exists");
774-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
775-
return null;
776-
}
777-
String tenantCode = tenant.getTenantCode();
743+
String tenantCode = getTenantCode(user);
778744

779745
String defaultPath = "";
780746
List<StorageEntity> resourcesList = new ArrayList<>();
@@ -785,7 +751,7 @@ public Map<String, Object> queryResourceList(User loginUser, ResourceType type,
785751
Set<String> visitedTenantEntityCode = new HashSet<>();
786752
for (User userEntity : userList) {
787753

788-
String tenantEntityCode = tenantMapper.queryById(userEntity.getTenantId()).getTenantCode();
754+
String tenantEntityCode = getTenantCode(userEntity);
789755
if (!visitedTenantEntityCode.contains(tenantEntityCode)) {
790756
defaultPath = storageOperate.getResDir(tenantEntityCode);
791757
if (type.equals(ResourceType.UDF)) {
@@ -911,14 +877,7 @@ public Result<Object> delete(User loginUser, String fullName,
911877
return result;
912878
}
913879

914-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
915-
if (tenant == null) {
916-
log.error("tenant not exists");
917-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
918-
return result;
919-
}
920-
921-
String tenantCode = tenant.getTenantCode();
880+
String tenantCode = getTenantCode(user);
922881

923882
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
924883
log.error("current user does not have permission");
@@ -1042,14 +1001,7 @@ public Result<Object> queryResourceByFileName(User loginUser, String fileName, R
10421001
return result;
10431002
}
10441003

1045-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1046-
if (tenant == null) {
1047-
log.error("tenant not exists");
1048-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
1049-
return result;
1050-
}
1051-
1052-
String tenantCode = tenant.getTenantCode();
1004+
String tenantCode = getTenantCode(user);
10531005

10541006
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
10551007
log.error("current user does not have permission");
@@ -1095,14 +1047,7 @@ public Result<Object> queryResourceByFullName(User loginUser, String fullName, S
10951047
return result;
10961048
}
10971049

1098-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1099-
if (tenant == null) {
1100-
log.error("tenant not exists");
1101-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
1102-
return result;
1103-
}
1104-
1105-
String tenantCode = tenant.getTenantCode();
1050+
String tenantCode = getTenantCode(user);
11061051

11071052
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
11081053
log.error("current user does not have permission");
@@ -1153,14 +1098,7 @@ public Result<Object> readResource(User loginUser, String fullName, String resTe
11531098
return result;
11541099
}
11551100

1156-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1157-
if (tenant == null) {
1158-
log.error("tenant not exists");
1159-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
1160-
return result;
1161-
}
1162-
1163-
String tenantCode = tenant.getTenantCode();
1101+
String tenantCode = getTenantCode(user);
11641102

11651103
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
11661104
log.error("current user does not have permission");
@@ -1236,14 +1174,7 @@ public Result<Object> onlineCreateResource(User loginUser, ResourceType type, St
12361174
return result;
12371175
}
12381176

1239-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1240-
if (tenant == null) {
1241-
log.error("tenant not exists");
1242-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
1243-
return result;
1244-
}
1245-
1246-
String tenantCode = tenant.getTenantCode();
1177+
String tenantCode = getTenantCode(user);
12471178

12481179
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, "")) {
12491180
log.error("current user does not have permission");
@@ -1384,14 +1315,7 @@ public Result<Object> updateResourceContent(User loginUser, String fullName, Str
13841315
return result;
13851316
}
13861317

1387-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1388-
if (tenant == null) {
1389-
log.error("tenant not exists");
1390-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
1391-
return result;
1392-
}
1393-
1394-
String tenantCode = tenant.getTenantCode();
1318+
String tenantCode = getTenantCode(user);
13951319

13961320
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
13971321
log.error("current user does not have permission");
@@ -1510,17 +1434,7 @@ public org.springframework.core.io.Resource downloadResource(User loginUser,
15101434
throw new ServiceException(String.format("Resource owner id %d does not exist", userId));
15111435
}
15121436

1513-
String tenantCode = "";
1514-
1515-
if (user.getTenantId() != 0) {
1516-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1517-
if (tenant == null) {
1518-
log.error("Tenant id {} not exists", user.getTenantId());
1519-
throw new ServiceException(
1520-
String.format("The tenant id %d of resource owner not exist", user.getTenantId()));
1521-
}
1522-
tenantCode = tenant.getTenantCode();
1523-
}
1437+
String tenantCode = getTenantCode(user);
15241438

15251439
String[] aliasArr = fullName.split("/");
15261440
String alias = aliasArr[aliasArr.length - 1];
@@ -1598,14 +1512,7 @@ public DeleteDataTransferResponse deleteDataTransferData(User loginUser, Integer
15981512
return result;
15991513
}
16001514

1601-
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1602-
if (tenant == null) {
1603-
log.error("tenant not exists");
1604-
putMsg(result, Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
1605-
return result;
1606-
}
1607-
1608-
String tenantCode = tenant.getTenantCode();
1515+
String tenantCode = getTenantCode(user);
16091516

16101517
String baseFolder = storageOperate.getResourceFileName(tenantCode, "DATA_TRANSFER");
16111518

@@ -1875,4 +1782,12 @@ private boolean isUserTenantValid(boolean isAdmin, String userTenantCode,
18751782

18761783
return true;
18771784
}
1785+
1786+
private String getTenantCode(User user) {
1787+
Tenant tenant = tenantMapper.queryById(user.getTenantId());
1788+
if (tenant == null) {
1789+
throw new ServiceException(Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST);
1790+
}
1791+
return tenant.getTenantCode();
1792+
}
18781793
}

0 commit comments

Comments
 (0)