Skip to content

Commit aca751a

Browse files
guanzhenxingAias00
andauthored
[type:feature] Add Swagger Import Functionality to ShenYu Admin (#6050)
* feature (admin) : add swagger import functionality with support for swagger 2.0 and openapi 3.0. (cherry picked from commit 6cb0507) * refactor(admin): Refactor Swagger-related code - Add SwaggerVersion enum import in SwaggerDocParser - Update test case descriptions in SwaggerImportServiceTest - Move SwaggerVersion enum from the admin module to the common module * refactor(admin): Optimize Swagger document parsing and import logic - Refactor the property handling logic in SwaggerDocParser to improve code readability and efficiency - Optimize HTTP request handling in SwaggerImportServiceImpl to enhance code flexibility - Remove unnecessary static HttpUtils instances to reduce resource consumption - Adjust code formatting and indentation to improve code cleanliness * refactor(admin): Optimize Swagger import-related code - Use Objects.isNull() instead of direct equality checks to enhance code readability and safety - Improve the toString method of the SwaggerImportRequest class - Remove unused imports and optimize parts of the code structure * refactor(admin): Refactor Swagger document import functionality - Extract base path method to support Swagger 2.0 and OpenAPI 3.0 - Optimize HTTP request handling, use Spring Bean to manage HttpUtils - Improve log output, add document MD5 information - Refactor code structure to enhance maintainability and testability * feat(admin): Add URL security checks to prevent SSRF attacks - Added UrlSecurityUtils utility class for URL security validation - Integrated URL security checks into the Swagger import feature - Implemented comprehensive validation for URL format, protocol, host, IP address, and port - Effectively prevents SSRF (Server-Side Request Forgery) and other URL-based attacks * feat(build): Update static resource version - Update CSS file references in index.html - Update JavaScript file references in index.html * refactor(admin): Refactor Swagger import functionality and URL security utility class - Optimized the code structure of the SwaggerImportServiceImpl class to improve code readability - Refactored the UrlSecurityUtils class to enhance URL security check functionality - Adjusted the exception handling method to make error messages clearer - Removed unused import statements to streamline the code --------- Co-authored-by: aias00 <[email protected]>
1 parent 2627a12 commit aca751a

File tree

13 files changed

+907
-41
lines changed

13 files changed

+907
-41
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.shenyu.admin.config;
19+
20+
import org.apache.shenyu.admin.utils.HttpUtils;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
/**
26+
* HTTP utilities configuration.
27+
*/
28+
@Configuration
29+
public class HttpUtilsConfiguration {
30+
31+
/**
32+
* Configure HttpUtils as a Spring Bean.
33+
*
34+
* @return HttpUtils instance
35+
*/
36+
@Bean
37+
@ConditionalOnMissingBean
38+
public HttpUtils httpUtils() {
39+
return new HttpUtils();
40+
}
41+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.shenyu.admin.controller;
19+
20+
import jakarta.validation.Valid;
21+
import org.apache.shenyu.admin.model.result.ShenyuAdminResult;
22+
import org.apache.shenyu.admin.model.dto.SwaggerImportRequest;
23+
import org.apache.shenyu.admin.service.SwaggerImportService;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.validation.annotation.Validated;
27+
import org.springframework.web.bind.annotation.PostMapping;
28+
import org.springframework.web.bind.annotation.RequestBody;
29+
import org.springframework.web.bind.annotation.RequestMapping;
30+
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.RestController;
32+
33+
/**
34+
* Swagger Import Controller.
35+
*/
36+
@RestController
37+
@RequestMapping("/swagger")
38+
@Validated
39+
public class SwaggerImportController {
40+
41+
private static final Logger LOG = LoggerFactory.getLogger(SwaggerImportController.class);
42+
43+
private final SwaggerImportService swaggerImportService;
44+
45+
public SwaggerImportController(final SwaggerImportService swaggerImportService) {
46+
this.swaggerImportService = swaggerImportService;
47+
}
48+
49+
/**
50+
* Import swagger documentation.
51+
*
52+
* @param request the swagger import request
53+
* @return the result of swagger import
54+
*/
55+
@PostMapping("/import")
56+
public ShenyuAdminResult importSwagger(@Valid @RequestBody final SwaggerImportRequest request) {
57+
LOG.info("Received Swagger import request: {}", request);
58+
59+
try {
60+
String result = swaggerImportService.importSwagger(request);
61+
return ShenyuAdminResult.success(result);
62+
63+
} catch (Exception e) {
64+
LOG.error("Failed to import swagger document", e);
65+
66+
return ShenyuAdminResult.error("Import failed: " + e.getMessage());
67+
}
68+
}
69+
70+
/**
71+
* Test connection to swagger URL.
72+
*
73+
* @param swaggerUrl the swagger URL to test
74+
* @return the result of connection test
75+
*/
76+
@PostMapping("/test-connection")
77+
public ShenyuAdminResult testConnection(@RequestParam final String swaggerUrl) {
78+
LOG.info("Testing Swagger URL connection: {}", swaggerUrl);
79+
80+
try {
81+
boolean isConnected = swaggerImportService.testConnection(swaggerUrl);
82+
83+
return ShenyuAdminResult.success(isConnected ? "Connection successful" : "Connection failed");
84+
85+
} catch (Exception e) {
86+
LOG.error("Failed to test connection", e);
87+
88+
return ShenyuAdminResult.error("Connection failed: " + e.getMessage());
89+
}
90+
}
91+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.shenyu.admin.model.dto;
19+
20+
21+
import jakarta.validation.constraints.NotBlank;
22+
import jakarta.validation.constraints.Pattern;
23+
24+
/**
25+
* Swagger Import Request.
26+
*/
27+
public class SwaggerImportRequest {
28+
29+
@NotBlank(message = "swagger URL cannot be empty")
30+
@Pattern(regexp = "^https?://.*", message = "swagger URL must be a valid HTTP/HTTPS address")
31+
private String swaggerUrl;
32+
33+
@NotBlank(message = "project name cannot be empty")
34+
private String projectName;
35+
36+
private String projectDescription;
37+
38+
public String getSwaggerUrl() {
39+
return swaggerUrl;
40+
}
41+
42+
public void setSwaggerUrl(final String swaggerUrl) {
43+
this.swaggerUrl = swaggerUrl;
44+
}
45+
46+
public String getProjectName() {
47+
return projectName;
48+
}
49+
50+
public void setProjectName(final String projectName) {
51+
this.projectName = projectName;
52+
}
53+
54+
public String getProjectDescription() {
55+
return projectDescription;
56+
}
57+
58+
public void setProjectDescription(final String projectDescription) {
59+
this.projectDescription = projectDescription;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "SwaggerImportRequest{"
65+
+ "swaggerUrl='" + swaggerUrl + '\''
66+
+ ", projectName='" + projectName + '\''
67+
+ ", projectDescription='" + projectDescription + '\''
68+
+ '}';
69+
}
70+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.shenyu.admin.service;
19+
20+
import org.apache.shenyu.admin.model.dto.SwaggerImportRequest;
21+
22+
/**
23+
* Swagger Import Service.
24+
*/
25+
public interface SwaggerImportService {
26+
27+
/**
28+
* Import swagger documentation.
29+
*
30+
* @param request swagger import request
31+
* @return import result message
32+
*/
33+
String importSwagger(SwaggerImportRequest request);
34+
35+
/**
36+
* Test connection to swagger URL.
37+
*
38+
* @param swaggerUrl swagger URL to test
39+
* @return true if connection is successful, false otherwise
40+
*/
41+
boolean testConnection(String swaggerUrl);
42+
}

0 commit comments

Comments
 (0)