Browse Source

[Feature-10290] [API] Refactor org.apache.dolphinscheduler.api.controller.AccessTokenController#createToken api (#10301)

* add feature_10290

* fix dolphinscheduler-ui/pnpm-lock.yaml error

* fix dolphinscheduler-ui/pnpm-lock.yaml error

* fix incorrect dependency

* fix incorrect dependency and unit test

* fix unit test error

* add dolphinscheduler-api/logs to .gitignore

* add AccessTokenV2ControllerTest.java

* fix AccessTokenV2ControllerTest error
3.1.0-release
xiangzihao 3 years ago committed by GitHub
parent
commit
8c72552237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 6
      dolphinscheduler-api/pom.xml
  3. 2
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/AppConfiguration.java
  4. 37
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/SwaggerConfig.java
  5. 3
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java
  6. 76
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2Controller.java
  7. 56
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenRequest.java
  8. 41
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java
  9. 2
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java
  10. 11
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java
  11. 18
      dolphinscheduler-api/src/main/resources/swagger.properties
  12. 4
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java
  13. 101
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java
  14. 7
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java

1
.gitignore vendored

@ -48,6 +48,7 @@ dolphinscheduler-ui/node
dolphinscheduler-common/sql
dolphinscheduler-common/test
dolphinscheduler-worker/logs
dolphinscheduler-api/logs
# ------------------
# pydolphinscheduler

6
dolphinscheduler-api/pom.xml

@ -151,12 +151,6 @@
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<exclusions>
<exclusion>
<artifactId>swagger-annotations</artifactId>
<groupId>io.swagger</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>

2
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/AppConfiguration.java

@ -101,7 +101,7 @@ public class AppConfiguration implements WebMvcConfigurer {
registry.addInterceptor(loginInterceptor())
.addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN)
.excludePathPatterns(LOGIN_PATH_PATTERN, REGISTER_PATH_PATTERN,
"/swagger-resources/**", "/webjars/**", "/v2/**",
"/swagger-resources/**", "/webjars/**", "/api-docs/**",
"/doc.html", "/swagger-ui.html", "*.html", "/ui/**", "/error");
}

37
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/SwaggerConfig.java

@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
@ -39,19 +40,45 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnWebApplication
@PropertySource("classpath:swagger.properties")
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("org.apache.dolphinscheduler.api.controller")).paths(PathSelectors.any())
public Docket createV1RestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("v1(current)")
.apiInfo(apiV1Info())
.select()
.apis(RequestHandlerSelectors.basePackage("org.apache.dolphinscheduler.api.controller"))
.paths(PathSelectors.any())
.paths(PathSelectors.regex("^(?!/v2).*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Dolphin Scheduler Api Docs").description("Dolphin Scheduler Api Docs")
private ApiInfo apiV1Info() {
return new ApiInfoBuilder()
.title("Dolphin Scheduler Api Docs")
.description("Dolphin Scheduler Api Docs")
.version("V1")
.build();
}
@Bean
public Docket createV2RestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("v2")
.apiInfo(apiV2Info())
.select()
.apis(RequestHandlerSelectors.basePackage("org.apache.dolphinscheduler.api.controller"))
.paths(PathSelectors.ant("/v2/**"))
.build();
}
private ApiInfo apiV2Info() {
return new ApiInfoBuilder()
.title("Dolphin Scheduler Api Docs")
.description("Dolphin Scheduler Api Docs")
.version("V2")
.build();
}
}

3
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java

@ -89,8 +89,7 @@ public class AccessTokenController extends BaseController {
@RequestParam(value = "expireTime") String expireTime,
@RequestParam(value = "token", required = false) String token) {
Map<String, Object> result = accessTokenService.createToken(loginUser, userId, expireTime, token);
return returnDataList(result);
return accessTokenService.createToken(loginUser, userId, expireTime, token);
}
/**

76
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2Controller.java

@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.dto.CreateTokenRequest;
import org.apache.dolphinscheduler.api.dto.CreateTokenResponse;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.AccessTokenService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import static org.apache.dolphinscheduler.api.enums.Status.CREATE_ACCESS_TOKEN_ERROR;
/**
* access token controller
*/
@Api(tags = "ACCESS_TOKEN_TAG")
@RestController
@RequestMapping("/v2/access-tokens")
public class AccessTokenV2Controller extends BaseController {
@Autowired
private AccessTokenService accessTokenService;
/**
* create token
*
* @param loginUser login user
* @param createTokenRequest createTokenRequest
* @return CreateTokenResponse CreateTokenResponse
*/
@ApiOperation(value = "createToken", notes = "CREATE_TOKEN_NOTES")
@PostMapping(consumes = {"application/json"})
@ResponseStatus(HttpStatus.CREATED)
@ApiException(CREATE_ACCESS_TOKEN_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public CreateTokenResponse createToken(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestBody CreateTokenRequest createTokenRequest) {
Result result = accessTokenService.createToken(loginUser,
createTokenRequest.getUserId(),
createTokenRequest.getExpireTime(),
createTokenRequest.getToken());
return new CreateTokenResponse(result);
}
}

56
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenRequest.java

@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto;
import io.swagger.annotations.ApiModelProperty;
public class CreateTokenRequest {
@ApiModelProperty(example = "1", required = true)
Integer userId;
@ApiModelProperty(example = "2022-12-31 00:00:00", required = true)
String expireTime;
@ApiModelProperty(example = "abc123xyz")
String token;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getExpireTime() {
return expireTime;
}
public void setExpireTime(String expireTime) {
this.expireTime = expireTime;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}

41
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.AccessToken;
public class CreateTokenResponse extends Result {
private AccessToken data;
public CreateTokenResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData((AccessToken) result.getData());
}
@Override
public AccessToken getData() {
return data;
}
public void setData(AccessToken data) {
this.data = data;
}
}

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

@ -55,7 +55,7 @@ public interface AccessTokenService {
* @param token token string (if it is absent, it will be automatically generated)
* @return create result code
*/
Map<String, Object> createToken(User loginUser, int userId, String expireTime, String token);
Result createToken(User loginUser, int userId, String expireTime, String token);
/**

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

@ -121,8 +121,8 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok
*/
@SuppressWarnings("checkstyle:WhitespaceAround")
@Override
public Map<String, Object> createToken(User loginUser, int userId, String expireTime, String token) {
Map<String, Object> result = new HashMap<>();
public Result createToken(User loginUser, int userId, String expireTime, String token) {
Result result = new Result();
// 1. check permission
if (!(canOperatorPermissions(loginUser,null, AuthorizationType.ACCESS_TOKEN,ACCESS_TOKEN_CREATE) || loginUser.getId() == userId)) {
@ -132,7 +132,10 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok
// 2. check if user is existed
if (userId <= 0) {
throw new IllegalArgumentException("User id should not less than or equals to 0.");
String errorMsg = "User id should not less than or equals to 0.";
logger.error(errorMsg);
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, errorMsg);
return result;
}
// 3. generate access token if absent
@ -151,7 +154,7 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok
int insert = accessTokenMapper.insert(accessToken);
if (insert > 0) {
result.put(Constants.DATA_LIST, accessToken);
result.setData(accessToken);
putMsg(result, Status.SUCCESS);
} else {
putMsg(result, Status.CREATE_ACCESS_TOKEN_ERROR);

18
dolphinscheduler-api/src/main/resources/swagger.properties

@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
springfox.documentation.swagger.v2.path=/api-docs

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

@ -89,11 +89,11 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
MvcResult mvcResult = mockMvc.perform(post("/access-tokens")
.header("sessionId", sessionId)
.params(paramsMap))
.andExpect(status().isOk())
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
Assert.assertEquals(Status.CREATE_ACCESS_TOKEN_ERROR.getCode(), result.getCode().intValue());
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
}

101
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java

@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.controller;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* access token v2 controller test
*/
public class AccessTokenV2ControllerTest extends AbstractControllerTest {
private static final Logger logger = LoggerFactory.getLogger(AccessTokenV2ControllerTest.class);
@Test
public void testCreateToken() throws Exception {
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("userId", 1);
paramsMap.put("expireTime", "2022-12-31 00:00:00");
paramsMap.put("token", "607f5aeaaa2093dbdff5d5522ce00510");
MvcResult mvcResult = mockMvc.perform(post("/v2/access-tokens")
.header("sessionId", sessionId)
.contentType(MediaType.APPLICATION_JSON)
.content(JSONUtils.toJsonString(paramsMap)))
.andExpect(status().isCreated())
.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());
}
@Test
public void testCreateTokenIfAbsent() throws Exception {
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("userId", 1);
paramsMap.put("expireTime", "2022-12-31 00:00:00");
paramsMap.put("token", null);
MvcResult mvcResult = this.mockMvc
.perform(post("/v2/access-tokens")
.header("sessionId", this.sessionId)
.contentType(MediaType.APPLICATION_JSON)
.content(JSONUtils.toJsonString(paramsMap)))
.andExpect(status().isCreated())
.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());
}
@Test
public void testExceptionHandler() throws Exception {
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("userId", -1);
paramsMap.put("expireTime", "2022-12-31 00:00:00");
paramsMap.put("token", "507f5aeaaa2093dbdff5d5522ce00510");
MvcResult mvcResult = mockMvc.perform(post("/v2/access-tokens")
.header("sessionId", sessionId)
.contentType(MediaType.APPLICATION_JSON)
.content(JSONUtils.toJsonString(paramsMap)))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
}
}

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

@ -40,6 +40,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.dolphinscheduler.service.permission.ResourcePermissionCheckService;
import org.assertj.core.util.Lists;
@ -117,14 +118,14 @@ public class AccessTokenServiceTest {
public void testCreateToken() {
// Given Token
when(accessTokenMapper.insert(any(AccessToken.class))).thenReturn(2);
Map<String, Object> result = accessTokenService.createToken(getLoginUser(), 1, getDate(), "AccessTokenServiceTest");
Result result = accessTokenService.createToken(getLoginUser(), 1, getDate(), "AccessTokenServiceTest");
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
// Token is absent
result = this.accessTokenService.createToken(getLoginUser(), 1, getDate(), null);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
}
@Test

Loading…
Cancel
Save