From 895b0fa9b8811d2907e70f153d077b083a7fd161 Mon Sep 17 00:00:00 2001 From: ouyangyewei Date: Wed, 15 Dec 2021 10:29:41 +0800 Subject: [PATCH] [Cherry-pick-2.0.2][Feature-7408][dolphinscheduler-api] Built GenerateToken into the UpdateToken API (#7412) * to feature 7408 * trigger e2e test Co-authored-by: ouyangyewei --- .../api/controller/AccessTokenController.java | 12 +++++++--- .../api/service/AccessTokenService.java | 2 +- .../service/impl/AccessTokenServiceImpl.java | 13 +++++++++- .../main/resources/i18n/messages.properties | 4 +++- .../resources/i18n/messages_en_US.properties | 4 +++- .../resources/i18n/messages_zh_CN.properties | 2 ++ .../controller/AccessTokenControllerTest.java | 24 ++++++++++++++++++- .../api/service/AccessTokenServiceTest.java | 11 ++++++--- .../api/service/UsersServiceTest.java | 2 +- 9 files changed, 62 insertions(+), 12 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java index 22c09dc0f4..e724b09571 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java @@ -193,10 +193,16 @@ public class AccessTokenController extends BaseController { * @param id token id * @param userId token for user * @param expireTime token expire time - * @param token token string + * @param token token string (if it is absent, it will be automatically generated) * @return update result code */ - @ApiIgnore + @ApiOperation(value = "updateToken", notes = "UPDATE_TOKEN_NOTES") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "TOKEN_ID", required = true, dataType = "Int"), + @ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int"), + @ApiImplicitParam(name = "expireTime", value = "EXPIRE_TIME", required = true, dataType = "String", example = "2021-12-31 00:00:00"), + @ApiImplicitParam(name = "token", value = "TOKEN", required = false, dataType = "String", example = "xxxx") + }) @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_ACCESS_TOKEN_ERROR) @@ -205,7 +211,7 @@ public class AccessTokenController extends BaseController { @PathVariable(value = "id") int id, @RequestParam(value = "userId") int userId, @RequestParam(value = "expireTime") String expireTime, - @RequestParam(value = "token") String token) { + @RequestParam(value = "token", required = false) String token) { Map result = accessTokenService.updateToken(loginUser, id, userId, expireTime, token); return returnDataList(result); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java index ca3c4a89fe..81fb5e8d5b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java @@ -82,7 +82,7 @@ public interface AccessTokenService { * @param id token id * @param userId token for user * @param expireTime token expire time - * @param token token string + * @param token token string (if it is absent, it will be automatically generated) * @return update result code */ Map updateToken(User loginUser, int id, int userId, String expireTime, String token); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java index 6cf4937ab7..04572ff096 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java @@ -207,22 +207,33 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok * @param id token id * @param userId token for user * @param expireTime token expire time - * @param token token string + * @param token token string (if it is absent, it will be automatically generated) * @return update result code */ @Override public Map updateToken(User loginUser, int id, int userId, String expireTime, String token) { Map result = new HashMap<>(); + + // 1. check permission if (!hasPerm(loginUser,userId)) { putMsg(result, Status.USER_NO_OPERATION_PERM); return result; } + + // 2. check if token is existed AccessToken accessToken = accessTokenMapper.selectById(id); if (accessToken == null) { logger.error("access token not exist, access token id {}", id); putMsg(result, Status.ACCESS_TOKEN_NOT_EXIST); return result; } + + // 3. generate access token if absent + if (StringUtils.isBlank(token)) { + token = EncryptionUtils.getMd5(userId + expireTime + System.currentTimeMillis()); + } + + // 4. persist to the database accessToken.setUserId(userId); accessToken.setExpireTime(DateUtils.stringToDate(expireTime)); accessToken.setToken(token); diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages.properties b/dolphinscheduler-api/src/main/resources/i18n/messages.properties index 3aaa3f33eb..7a4282a63c 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages.properties @@ -144,8 +144,10 @@ QUERY_AUTHORIZED_USER_NOTES=query authorized user TASK_RECORD_TAG=task record related operation QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging CREATE_TOKEN_NOTES=create access token for specified user +UPDATE_TOKEN_NOTES=update access token for specified user TOKEN=access token string, it will be automatically generated when it absent EXPIRE_TIME=expire time for the token +TOKEN_ID=access token id QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user SCHEDULE=schedule @@ -227,7 +229,7 @@ GRANT_PROJECT_NOTES=GRANT PROJECT PROJECT_IDS=project ids(string format, multiple projects separated by ",") GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER -PROJECT_CODE=project codes +PROJECT_CODE=project code GRANT_RESOURCE_NOTES=grant resource file RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") GET_USER_INFO_NOTES=get user info diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties index cf5df23639..19dc343c59 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties @@ -160,8 +160,10 @@ QUERY_AUTHORIZED_USER_NOTES=query authorized user TASK_RECORD_TAG=task record related operation QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging CREATE_TOKEN_NOTES=create access token for specified user +UPDATE_TOKEN_NOTES=update access token for specified user TOKEN=access token string, it will be automatically generated when it absent EXPIRE_TIME=expire time for the token +TOKEN_ID=access token id QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user SCHEDULE=schedule @@ -274,7 +276,7 @@ GRANT_PROJECT_NOTES=GRANT PROJECT PROJECT_IDS=project ids(string format, multiple projects separated by ",") GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER -PROJECT_CODE=project codes +PROJECT_CODE=project code GRANT_RESOURCE_NOTES=grant resource file RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") GET_USER_INFO_NOTES=get user info diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties index 8398ff2ca9..a0f1b8d47a 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties @@ -149,8 +149,10 @@ QUERY_AUTHORIZED_USER_NOTES=查询拥有项目授权的用户 TASK_RECORD_TAG=任务记录相关操作 QUERY_TASK_RECORD_LIST_PAGING_NOTES=分页查询任务记录列表 CREATE_TOKEN_NOTES=为指定用户创建安全令牌 +UPDATE_TOKEN_NOTES=更新指定用户的安全令牌 TOKEN=安全令牌字符串,若未显式指定将会自动生成 EXPIRE_TIME=安全令牌的过期时间 +TOKEN_ID=安全令牌的ID QUERY_ACCESS_TOKEN_LIST_NOTES=分页查询access token列表 QUERY_ACCESS_TOKEN_BY_USER_NOTES=查询指定用户的access token SCHEDULE=定时 diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java index abba7e09ef..1891f573e8 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java @@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.api.controller; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -60,7 +61,7 @@ public class AccessTokenControllerTest extends AbstractControllerTest { } @Test - public void testCreateTokenWhenTokenAbsent() throws Exception { + public void testCreateTokenIfAbsent() throws Exception { MultiValueMap paramsMap = new LinkedMultiValueMap<>(); paramsMap.add("userId", "4"); paramsMap.add("expireTime", "2019-12-18 00:00:00"); @@ -175,4 +176,25 @@ public class AccessTokenControllerTest extends AbstractControllerTest { logger.info(mvcResult.getResponse().getContentAsString()); } + @Test + public void testUpdateTokenIfAbsent() throws Exception { + this.testCreateTokenIfAbsent(); + + MultiValueMap paramsMap = new LinkedMultiValueMap<>(); + paramsMap.add("userId", "4"); + paramsMap.add("expireTime", "2019-12-20 00:00:00"); + paramsMap.add("token", null); + + MvcResult mvcResult = this.mockMvc + .perform(put("/access-tokens/2") + .header("sessionId", this.sessionId) + .params(paramsMap)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + + Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); + Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + logger.info(mvcResult.getResponse().getContentAsString()); + } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java index eb002f408f..009fb9af81 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java @@ -147,16 +147,21 @@ public class AccessTokenServiceTest { @Test public void testUpdateToken() { - + // Given Token when(accessTokenMapper.selectById(1)).thenReturn(getEntity()); Map result = accessTokenService.updateToken(getLoginUser(), 1,Integer.MAX_VALUE,getDate(),"token"); logger.info(result.toString()); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - // not exist + + // Token is absent + result = accessTokenService.updateToken(getLoginUser(), 1, Integer.MAX_VALUE,getDate(),null); + logger.info(result.toString()); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + + // ACCESS_TOKEN_NOT_EXIST result = accessTokenService.updateToken(getLoginUser(), 2,Integer.MAX_VALUE,getDate(),"token"); logger.info(result.toString()); Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST, result.get(Constants.STATUS)); - } private User getLoginUser() { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java index 85daa7eae8..5e0eb6b9c1 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java @@ -70,7 +70,7 @@ import com.google.common.collect.Lists; /** * users service test */ -@RunWith(MockitoJUnitRunner.class) +@RunWith(MockitoJUnitRunner.Silent.class) public class UsersServiceTest { private static final Logger logger = LoggerFactory.getLogger(UsersServiceTest.class);