Browse Source

[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 <yewei.oyyw@alibaba-inc.com>
2.0.7-release
ouyangyewei 3 years ago committed by GitHub
parent
commit
895b0fa9b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java
  2. 2
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java
  3. 13
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java
  4. 4
      dolphinscheduler-api/src/main/resources/i18n/messages.properties
  5. 4
      dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties
  6. 2
      dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties
  7. 24
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java
  8. 11
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java
  9. 2
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java

12
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 id token id
* @param userId token for user * @param userId token for user
* @param expireTime token expire time * @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 * @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}") @PutMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(UPDATE_ACCESS_TOKEN_ERROR) @ApiException(UPDATE_ACCESS_TOKEN_ERROR)
@ -205,7 +211,7 @@ public class AccessTokenController extends BaseController {
@PathVariable(value = "id") int id, @PathVariable(value = "id") int id,
@RequestParam(value = "userId") int userId, @RequestParam(value = "userId") int userId,
@RequestParam(value = "expireTime") String expireTime, @RequestParam(value = "expireTime") String expireTime,
@RequestParam(value = "token") String token) { @RequestParam(value = "token", required = false) String token) {
Map<String, Object> result = accessTokenService.updateToken(loginUser, id, userId, expireTime, token); Map<String, Object> result = accessTokenService.updateToken(loginUser, id, userId, expireTime, token);
return returnDataList(result); return returnDataList(result);

2
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java

@ -82,7 +82,7 @@ public interface AccessTokenService {
* @param id token id * @param id token id
* @param userId token for user * @param userId token for user
* @param expireTime token expire time * @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 * @return update result code
*/ */
Map<String, Object> updateToken(User loginUser, int id, int userId, String expireTime, String token); Map<String, Object> updateToken(User loginUser, int id, int userId, String expireTime, String token);

13
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 id token id
* @param userId token for user * @param userId token for user
* @param expireTime token expire time * @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 * @return update result code
*/ */
@Override @Override
public Map<String, Object> updateToken(User loginUser, int id, int userId, String expireTime, String token) { public Map<String, Object> updateToken(User loginUser, int id, int userId, String expireTime, String token) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
// 1. check permission
if (!hasPerm(loginUser,userId)) { if (!hasPerm(loginUser,userId)) {
putMsg(result, Status.USER_NO_OPERATION_PERM); putMsg(result, Status.USER_NO_OPERATION_PERM);
return result; return result;
} }
// 2. check if token is existed
AccessToken accessToken = accessTokenMapper.selectById(id); AccessToken accessToken = accessTokenMapper.selectById(id);
if (accessToken == null) { if (accessToken == null) {
logger.error("access token not exist, access token id {}", id); logger.error("access token not exist, access token id {}", id);
putMsg(result, Status.ACCESS_TOKEN_NOT_EXIST); putMsg(result, Status.ACCESS_TOKEN_NOT_EXIST);
return result; 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.setUserId(userId);
accessToken.setExpireTime(DateUtils.stringToDate(expireTime)); accessToken.setExpireTime(DateUtils.stringToDate(expireTime));
accessToken.setToken(token); accessToken.setToken(token);

4
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 TASK_RECORD_TAG=task record related operation
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
CREATE_TOKEN_NOTES=create access token for specified user 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 TOKEN=access token string, it will be automatically generated when it absent
EXPIRE_TIME=expire time for the token 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_LIST_NOTES=query access token list paging
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
SCHEDULE=schedule SCHEDULE=schedule
@ -227,7 +229,7 @@ GRANT_PROJECT_NOTES=GRANT PROJECT
PROJECT_IDS=project ids(string format, multiple projects separated by ",") PROJECT_IDS=project ids(string format, multiple projects separated by ",")
GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE
REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER
PROJECT_CODE=project codes PROJECT_CODE=project code
GRANT_RESOURCE_NOTES=grant resource file GRANT_RESOURCE_NOTES=grant resource file
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
GET_USER_INFO_NOTES=get user info GET_USER_INFO_NOTES=get user info

4
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 TASK_RECORD_TAG=task record related operation
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
CREATE_TOKEN_NOTES=create access token for specified user 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 TOKEN=access token string, it will be automatically generated when it absent
EXPIRE_TIME=expire time for the token 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_LIST_NOTES=query access token list paging
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
SCHEDULE=schedule SCHEDULE=schedule
@ -274,7 +276,7 @@ GRANT_PROJECT_NOTES=GRANT PROJECT
PROJECT_IDS=project ids(string format, multiple projects separated by ",") PROJECT_IDS=project ids(string format, multiple projects separated by ",")
GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE
REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER
PROJECT_CODE=project codes PROJECT_CODE=project code
GRANT_RESOURCE_NOTES=grant resource file GRANT_RESOURCE_NOTES=grant resource file
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
GET_USER_INFO_NOTES=get user info GET_USER_INFO_NOTES=get user info

2
dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties

@ -149,8 +149,10 @@ QUERY_AUTHORIZED_USER_NOTES=查询拥有项目授权的用户
TASK_RECORD_TAG=任务记录相关操作 TASK_RECORD_TAG=任务记录相关操作
QUERY_TASK_RECORD_LIST_PAGING_NOTES=分页查询任务记录列表 QUERY_TASK_RECORD_LIST_PAGING_NOTES=分页查询任务记录列表
CREATE_TOKEN_NOTES=为指定用户创建安全令牌 CREATE_TOKEN_NOTES=为指定用户创建安全令牌
UPDATE_TOKEN_NOTES=更新指定用户的安全令牌
TOKEN=安全令牌字符串,若未显式指定将会自动生成 TOKEN=安全令牌字符串,若未显式指定将会自动生成
EXPIRE_TIME=安全令牌的过期时间 EXPIRE_TIME=安全令牌的过期时间
TOKEN_ID=安全令牌的ID
QUERY_ACCESS_TOKEN_LIST_NOTES=分页查询access token列表 QUERY_ACCESS_TOKEN_LIST_NOTES=分页查询access token列表
QUERY_ACCESS_TOKEN_BY_USER_NOTES=查询指定用户的access token QUERY_ACCESS_TOKEN_BY_USER_NOTES=查询指定用户的access token
SCHEDULE=定时 SCHEDULE=定时

24
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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 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.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -60,7 +61,7 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
} }
@Test @Test
public void testCreateTokenWhenTokenAbsent() throws Exception { public void testCreateTokenIfAbsent() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>(); MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("userId", "4"); paramsMap.add("userId", "4");
paramsMap.add("expireTime", "2019-12-18 00:00:00"); paramsMap.add("expireTime", "2019-12-18 00:00:00");
@ -175,4 +176,25 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
logger.info(mvcResult.getResponse().getContentAsString()); logger.info(mvcResult.getResponse().getContentAsString());
} }
@Test
public void testUpdateTokenIfAbsent() throws Exception {
this.testCreateTokenIfAbsent();
MultiValueMap<String, String> 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());
}
} }

11
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java

@ -147,16 +147,21 @@ public class AccessTokenServiceTest {
@Test @Test
public void testUpdateToken() { public void testUpdateToken() {
// Given Token
when(accessTokenMapper.selectById(1)).thenReturn(getEntity()); when(accessTokenMapper.selectById(1)).thenReturn(getEntity());
Map<String, Object> result = accessTokenService.updateToken(getLoginUser(), 1,Integer.MAX_VALUE,getDate(),"token"); Map<String, Object> result = accessTokenService.updateToken(getLoginUser(), 1,Integer.MAX_VALUE,getDate(),"token");
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); 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"); result = accessTokenService.updateToken(getLoginUser(), 2,Integer.MAX_VALUE,getDate(),"token");
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST, result.get(Constants.STATUS)); Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST, result.get(Constants.STATUS));
} }
private User getLoginUser() { private User getLoginUser() {

2
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 * users service test
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.Silent.class)
public class UsersServiceTest { public class UsersServiceTest {
private static final Logger logger = LoggerFactory.getLogger(UsersServiceTest.class); private static final Logger logger = LoggerFactory.getLogger(UsersServiceTest.class);

Loading…
Cancel
Save