Skip to content

Commit e62cc03

Browse files
authored
[Fix-17969][API] No tenant validation for workflow (#17970)
1 parent 5e6d41f commit e62cc03

8 files changed

Lines changed: 81 additions & 32 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public class ExecutorServiceImpl extends BaseServiceImpl implements ExecutorServ
163163
@Transactional
164164
public Integer triggerWorkflowDefinition(final WorkflowTriggerRequest triggerRequest) {
165165
final TriggerWorkflowDTO triggerWorkflowDTO = triggerWorkflowRequestTransformer.transform(triggerRequest);
166+
// todo: use validator chain
166167
triggerWorkflowDTOValidator.validate(triggerWorkflowDTO);
167168
return executorClient.triggerWorkflowDefinition().execute(triggerWorkflowDTO);
168169
}

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.dolphinscheduler.api.service.SchedulerService;
3232
import org.apache.dolphinscheduler.api.utils.PageInfo;
3333
import org.apache.dolphinscheduler.api.utils.Result;
34+
import org.apache.dolphinscheduler.api.validator.TenantExistValidator;
3435
import org.apache.dolphinscheduler.api.vo.ScheduleVO;
3536
import org.apache.dolphinscheduler.common.constants.Constants;
3637
import org.apache.dolphinscheduler.common.enums.FailureStrategy;
@@ -43,13 +44,11 @@
4344
import org.apache.dolphinscheduler.dao.entity.Environment;
4445
import org.apache.dolphinscheduler.dao.entity.Project;
4546
import org.apache.dolphinscheduler.dao.entity.Schedule;
46-
import org.apache.dolphinscheduler.dao.entity.Tenant;
4747
import org.apache.dolphinscheduler.dao.entity.User;
4848
import org.apache.dolphinscheduler.dao.entity.WorkflowDefinition;
4949
import org.apache.dolphinscheduler.dao.mapper.EnvironmentMapper;
5050
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
5151
import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper;
52-
import org.apache.dolphinscheduler.dao.mapper.TenantMapper;
5352
import org.apache.dolphinscheduler.dao.mapper.WorkflowDefinitionMapper;
5453
import org.apache.dolphinscheduler.scheduler.api.SchedulerApi;
5554
import org.apache.dolphinscheduler.service.cron.CronUtils;
@@ -107,7 +106,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
107106
private EnvironmentMapper environmentMapper;
108107

109108
@Autowired
110-
private TenantMapper tenantMapper;
109+
private TenantExistValidator tenantExistValidator;
111110

112111
/**
113112
* save schedule
@@ -166,7 +165,7 @@ public Map<String, Object> insertSchedule(User loginUser,
166165
Schedule scheduleObj = new Schedule();
167166
Date now = new Date();
168167

169-
checkValidTenant(tenantCode);
168+
tenantExistValidator.validate(tenantCode);
170169

171170
scheduleObj.setTenantCode(tenantCode);
172171
scheduleObj.setProjectName(project.getName());
@@ -276,7 +275,7 @@ public Schedule createSchedulesV2(User loginUser,
276275
scheduleExists.getId());
277276
}
278277

279-
checkValidTenant(scheduleCreateRequest.getTenantCode());
278+
tenantExistValidator.validate(scheduleCreateRequest.getTenantCode());
280279

281280
Schedule schedule = scheduleCreateRequest.convert2Schedule();
282281
Environment environment = environmentMapper.queryByEnvironmentCode(schedule.getEnvironmentCode());
@@ -759,7 +758,7 @@ private void updateSchedule(Map<String, Object> result, Schedule schedule, Workf
759758

760759
Date now = new Date();
761760

762-
checkValidTenant(tenantCode);
761+
tenantExistValidator.validate(tenantCode);
763762
schedule.setTenantCode(tenantCode);
764763

765764
// updateWorkflowInstance param
@@ -818,17 +817,4 @@ private void updateSchedule(Map<String, Object> result, Schedule schedule, Workf
818817
putMsg(result, Status.SUCCESS);
819818
}
820819

821-
/**
822-
* check valid tenant
823-
*
824-
* @param tenantCode
825-
*/
826-
private void checkValidTenant(String tenantCode) {
827-
if (!Constants.DEFAULT.equals(tenantCode)) {
828-
Tenant tenant = tenantMapper.queryByTenantCode(tenantCode);
829-
if (tenant == null) {
830-
throw new ServiceException(Status.TENANT_NOT_EXIST, tenantCode);
831-
}
832-
}
833-
}
834820
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.api.validator;
19+
20+
import org.apache.dolphinscheduler.dao.repository.TenantDao;
21+
22+
import lombok.extern.slf4j.Slf4j;
23+
24+
import org.springframework.stereotype.Component;
25+
26+
/**
27+
* This validator is used to validate whether the tenant exists.
28+
* <p> If the tenant does not exist, an {@link IllegalArgumentException} will be thrown. </p>
29+
*/
30+
@Slf4j
31+
@Component
32+
public class TenantExistValidator implements IValidator<String> {
33+
34+
private final TenantDao tenantDao;
35+
36+
public TenantExistValidator(TenantDao tenantDao) {
37+
this.tenantDao = tenantDao;
38+
}
39+
40+
@Override
41+
public void validate(String tenantCode) {
42+
if (!tenantDao.queryByCode(tenantCode).isPresent()) {
43+
throw new IllegalArgumentException(String.format("Tenant: [%s] not exists", tenantCode));
44+
}
45+
}
46+
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/BackfillWorkflowDTOValidator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.dolphinscheduler.api.validator.workflow;
1919

2020
import org.apache.dolphinscheduler.api.validator.IValidator;
21+
import org.apache.dolphinscheduler.api.validator.TenantExistValidator;
2122
import org.apache.dolphinscheduler.common.enums.CommandType;
2223
import org.apache.dolphinscheduler.common.enums.ReleaseState;
2324

@@ -31,6 +32,12 @@
3132
@Component
3233
public class BackfillWorkflowDTOValidator implements IValidator<BackfillWorkflowDTO> {
3334

35+
private final TenantExistValidator tenantExistValidator;
36+
37+
public BackfillWorkflowDTOValidator(TenantExistValidator tenantExistValidator) {
38+
this.tenantExistValidator = tenantExistValidator;
39+
}
40+
3441
@Override
3542
public void validate(final BackfillWorkflowDTO backfillWorkflowDTO) {
3643
final BackfillWorkflowDTO.BackfillParamsDTO backfillParams = backfillWorkflowDTO.getBackfillParams();
@@ -52,5 +59,6 @@ public void validate(final BackfillWorkflowDTO backfillWorkflowDTO) {
5259
if (backfillWorkflowDTO.getWorkflowDefinition().getReleaseState() != ReleaseState.ONLINE) {
5360
throw new IllegalStateException("The workflowDefinition should be online");
5461
}
62+
tenantExistValidator.validate(backfillWorkflowDTO.getTenantCode());
5563
}
5664
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/TriggerWorkflowDTOValidator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.dolphinscheduler.api.validator.workflow;
1919

2020
import org.apache.dolphinscheduler.api.validator.IValidator;
21+
import org.apache.dolphinscheduler.api.validator.TenantExistValidator;
2122
import org.apache.dolphinscheduler.common.enums.CommandType;
2223
import org.apache.dolphinscheduler.common.enums.ReleaseState;
2324

@@ -29,6 +30,12 @@
2930
@Component
3031
public class TriggerWorkflowDTOValidator implements IValidator<TriggerWorkflowDTO> {
3132

33+
private final TenantExistValidator tenantExistValidator;
34+
35+
public TriggerWorkflowDTOValidator(TenantExistValidator tenantExistValidator) {
36+
this.tenantExistValidator = tenantExistValidator;
37+
}
38+
3239
@Override
3340
public void validate(final TriggerWorkflowDTO triggerWorkflowDTO) {
3441
if (triggerWorkflowDTO.getExecType() != CommandType.START_PROCESS) {
@@ -40,5 +47,6 @@ public void validate(final TriggerWorkflowDTO triggerWorkflowDTO) {
4047
if (triggerWorkflowDTO.getWorkflowDefinition().getReleaseState() != ReleaseState.ONLINE) {
4148
throw new IllegalStateException("The workflowDefinition should be online");
4249
}
50+
tenantExistValidator.validate(triggerWorkflowDTO.getTenantCode());
4351
}
4452
}

dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.dolphinscheduler.api.enums.Status;
2727
import org.apache.dolphinscheduler.api.exceptions.ServiceException;
2828
import org.apache.dolphinscheduler.api.service.impl.SchedulerServiceImpl;
29+
import org.apache.dolphinscheduler.api.validator.TenantExistValidator;
2930
import org.apache.dolphinscheduler.common.constants.Constants;
3031
import org.apache.dolphinscheduler.common.enums.ReleaseState;
3132
import org.apache.dolphinscheduler.dao.entity.Environment;
@@ -36,11 +37,8 @@
3637
import org.apache.dolphinscheduler.dao.mapper.EnvironmentMapper;
3738
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
3839
import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper;
39-
import org.apache.dolphinscheduler.dao.mapper.TenantMapper;
4040
import org.apache.dolphinscheduler.dao.mapper.WorkflowDefinitionMapper;
41-
import org.apache.dolphinscheduler.dao.mapper.WorkflowTaskRelationMapper;
4241
import org.apache.dolphinscheduler.scheduler.api.SchedulerApi;
43-
import org.apache.dolphinscheduler.service.process.ProcessService;
4442

4543
import org.junit.jupiter.api.Assertions;
4644
import org.junit.jupiter.api.BeforeEach;
@@ -63,15 +61,6 @@ public class SchedulerServiceTest extends BaseServiceTestTool {
6361
@InjectMocks
6462
private SchedulerServiceImpl schedulerService;
6563

66-
@Mock
67-
private WorkflowTaskRelationMapper workflowTaskRelationMapper;
68-
69-
@Mock
70-
private MonitorService monitorService;
71-
72-
@Mock
73-
private ProcessService processService;
74-
7564
@Mock
7665
private ScheduleMapper scheduleMapper;
7766

@@ -94,7 +83,7 @@ public class SchedulerServiceTest extends BaseServiceTestTool {
9483
private EnvironmentMapper environmentMapper;
9584

9685
@Mock
97-
private TenantMapper tenantMapper;
86+
private TenantExistValidator tenantExistValidator;
9887

9988
protected static User user;
10089
protected Exception exception;
@@ -128,6 +117,7 @@ public void testCreateSchedulesV2() {
128117
scheduleCreateRequest.setWorkflowDefinitionCode(processDefinitionCode);
129118
scheduleCreateRequest.setEnvironmentCode(environmentCode);
130119
scheduleCreateRequest.setTenantCode(Constants.DEFAULT);
120+
scheduleCreateRequest.setStartTime(startTime);
131121

132122
// error process definition not exists
133123
exception = Assertions.assertThrows(ServiceException.class,

dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/TenantDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
import org.apache.dolphinscheduler.dao.entity.Tenant;
2121

22+
import java.util.Optional;
23+
2224
public interface TenantDao extends IDao<Tenant> {
2325

26+
Optional<Tenant> queryByCode(String tenantCode);
27+
2428
}

dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/impl/TenantDaoImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.apache.dolphinscheduler.dao.repository.BaseDao;
2323
import org.apache.dolphinscheduler.dao.repository.TenantDao;
2424

25+
import java.util.Optional;
26+
2527
import lombok.NonNull;
2628

2729
import org.springframework.stereotype.Repository;
@@ -33,4 +35,8 @@ public TenantDaoImpl(@NonNull TenantMapper tenantMapper) {
3335
super(tenantMapper);
3436
}
3537

38+
@Override
39+
public Optional<Tenant> queryByCode(String tenantCode) {
40+
return Optional.ofNullable(mybatisMapper.queryByTenantCode(tenantCode));
41+
}
3642
}

0 commit comments

Comments
 (0)