Browse Source

[Feature-7408][dolphinscheduler-api] Built GenerateToken into the UpdateToken API (#7411)

* to feature 7408

* trigger e2e test

Co-authored-by: ouyangyewei <yewei.oyyw@alibaba-inc.com>
3.0.0/version-upgrade
ouyangyewei 3 years ago committed by GitHub
parent
commit
6b5c393449
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. 23
      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

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 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<String, Object> result = accessTokenService.updateToken(loginUser, id, userId, expireTime, token);
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 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<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 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<String, Object> updateToken(User loginUser, int id, int userId, String expireTime, String token) {
Map<String, Object> 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);

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
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

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
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

2
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=定时

23
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java

@ -61,7 +61,7 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
}
@Test
public void testCreateTokenWhenTokenAbsent() throws Exception {
public void testCreateTokenIfAbsent() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("userId", "4");
paramsMap.add("expireTime", "2019-12-18 00:00:00");
@ -174,4 +174,25 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
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
public void testUpdateToken() {
// Given Token
when(accessTokenMapper.selectById(1)).thenReturn(getEntity());
Map<String, Object> 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() {

Loading…
Cancel
Save