You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/06/18 08:31:20 UTC
[camel] 02/04: CAMEL-9751: Allow to configure swagger security
requirements in generated swagger api docs in rest-dsl.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 30533e069680fea971cd9c83ab2bca885fa8d307
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Jun 16 14:42:24 2018 +0200
CAMEL-9751: Allow to configure swagger security requirements in generated swagger api docs in rest-dsl.
---
.../apache/camel/model/rest/RestDefinition.java | 15 ++++---
.../camel/model/rest/RestSecuritiesDefinition.java | 52 ++++++++++++++++++++++
.../camel/model/rest/RestSecurityApiKey.java | 25 +++++++++++
.../camel/model/rest/RestSecurityBasicAuth.java | 6 +++
.../camel/model/rest/RestSecurityDefinition.java | 10 ++++-
.../camel/model/rest/RestSecurityOAuth2.java | 42 +++++++++++++++++
.../apache/camel/swagger/RestSwaggerReader.java | 41 +++++++++++++++++
.../RestSwaggerReaderModelApiSecurityTest.java | 17 ++++---
8 files changed, 195 insertions(+), 13 deletions(-)
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
index 36db164..5583def 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
@@ -425,6 +425,16 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
return this;
}
+ /**
+ * To configure security definitions.
+ */
+ public RestSecuritiesDefinition securityDefinitions() {
+ if (securityDefinitions == null) {
+ securityDefinitions = new RestSecuritiesDefinition(this);
+ }
+ return securityDefinitions;
+ }
+
public RestDefinition produces(String mediaType) {
if (getVerbs().isEmpty()) {
this.produces = mediaType;
@@ -618,11 +628,6 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
return route;
}
- public RestSecurityDefinition securityDefinition(String id) {
- //return new RestSecurityDefinition(this, id);
- return null;
- }
-
// Implementation
//-------------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecuritiesDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecuritiesDefinition.java
index bf87079..2e15a18 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecuritiesDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecuritiesDefinition.java
@@ -23,6 +23,7 @@ import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
import org.apache.camel.spi.Metadata;
@@ -34,6 +35,9 @@ import org.apache.camel.spi.Metadata;
@XmlAccessorType(XmlAccessType.FIELD)
public class RestSecuritiesDefinition {
+ @XmlTransient
+ private RestDefinition rest;
+
@XmlElements({
@XmlElement(name = "apiKey", type = RestSecurityApiKey.class),
@XmlElement(name = "basicAuth", type = RestSecurityBasicAuth.class),
@@ -41,6 +45,13 @@ public class RestSecuritiesDefinition {
})
private List<RestSecurityDefinition> securityDefinitions = new ArrayList<>();
+ public RestSecuritiesDefinition() {
+ }
+
+ public RestSecuritiesDefinition(RestDefinition rest) {
+ this.rest = rest;
+ }
+
public List<RestSecurityDefinition> getSecurityDefinitions() {
return securityDefinitions;
}
@@ -48,4 +59,45 @@ public class RestSecuritiesDefinition {
public void setSecurityDefinitions(List<RestSecurityDefinition> securityDefinitions) {
this.securityDefinitions = securityDefinitions;
}
+
+ public RestSecurityApiKey apiKey(String key) {
+ return apiKey(key, null);
+ }
+
+ public RestSecurityApiKey apiKey(String key, String description) {
+ RestSecurityApiKey auth = new RestSecurityApiKey(rest);
+ auth.setKey(key);
+ auth.setDescription(description);
+ securityDefinitions.add(auth);
+ return auth;
+ }
+
+ public RestSecuritiesDefinition basicAuth(String key) {
+ return basicAuth(key, null);
+ }
+
+ public RestSecuritiesDefinition basicAuth(String key, String description) {
+ RestSecurityBasicAuth auth = new RestSecurityBasicAuth(rest);
+ securityDefinitions.add(auth);
+ auth.setKey(key);
+ auth.setDescription(description);
+ return this;
+ }
+
+ public RestSecurityOAuth2 oauth2(String key) {
+ return oauth2(key, null);
+ }
+
+ public RestSecurityOAuth2 oauth2(String key, String description) {
+ RestSecurityOAuth2 auth = new RestSecurityOAuth2(rest);
+ auth.setKey(key);
+ auth.setDescription(description);
+ securityDefinitions.add(auth);
+ return auth;
+ }
+
+ public RestDefinition end() {
+ return rest;
+ }
+
}
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java
index c9a74f4..3e48330 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java
@@ -44,6 +44,13 @@ public class RestSecurityApiKey extends RestSecurityDefinition {
return name;
}
+ public RestSecurityApiKey() {
+ }
+
+ public RestSecurityApiKey(RestDefinition rest) {
+ super(rest);
+ }
+
/**
* The name of the header or query parameter to be used.
*/
@@ -72,4 +79,22 @@ public class RestSecurityApiKey extends RestSecurityDefinition {
public void setInQuery(Boolean inQuery) {
this.inQuery = inQuery;
}
+
+ public RestSecurityApiKey withHeader(String name) {
+ setName(name);
+ setInHeader(true);
+ setInQuery(false);
+ return this;
+ }
+
+ public RestSecurityApiKey withQuery(String name) {
+ setName(name);
+ setInQuery(true);
+ setInHeader(false);
+ return this;
+ }
+
+ public RestSecuritiesDefinition end() {
+ return rest.getSecurityDefinitions();
+ }
}
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityBasicAuth.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityBasicAuth.java
index 6e4e80c..d61b423 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityBasicAuth.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityBasicAuth.java
@@ -31,4 +31,10 @@ import org.apache.camel.spi.Metadata;
@XmlAccessorType(XmlAccessType.FIELD)
public class RestSecurityBasicAuth extends RestSecurityDefinition {
+ public RestSecurityBasicAuth() {
+ }
+
+ public RestSecurityBasicAuth(RestDefinition rest) {
+ super(rest);
+ }
}
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityDefinition.java
index fa132f8..77e55e8 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityDefinition.java
@@ -30,7 +30,7 @@ import org.apache.camel.spi.Metadata;
public abstract class RestSecurityDefinition {
@XmlTransient
- private RestDefinition rest;
+ RestDefinition rest;
@XmlAttribute(required = true) @Metadata(required = "true")
private String key;
@@ -38,6 +38,13 @@ public abstract class RestSecurityDefinition {
@XmlAttribute
private String description;
+ public RestSecurityDefinition() {
+ }
+
+ public RestSecurityDefinition(RestDefinition rest) {
+ this.rest = rest;
+ }
+
/**
* Ends the configuration of this security
*/
@@ -67,4 +74,5 @@ public abstract class RestSecurityDefinition {
public void setDescription(String description) {
this.description = description;
}
+
}
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityOAuth2.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityOAuth2.java
index efafc2e..edb9787 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityOAuth2.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityOAuth2.java
@@ -46,6 +46,13 @@ public class RestSecurityOAuth2 extends RestSecurityDefinition {
@XmlElement(name = "scopes")
private List<RestPropertyDefinition> scopes = new ArrayList<>();
+ public RestSecurityOAuth2() {
+ }
+
+ public RestSecurityOAuth2(RestDefinition rest) {
+ super(rest);
+ }
+
public String getAuthorizationUrl() {
return authorizationUrl;
}
@@ -92,4 +99,39 @@ public class RestSecurityOAuth2 extends RestSecurityDefinition {
public void setScopes(List<RestPropertyDefinition> scopes) {
this.scopes = scopes;
}
+
+ public RestSecurityOAuth2 authorizationUrl(String authorizationUrl) {
+ setAuthorizationUrl(authorizationUrl);
+ setFlow("implicit");
+ return this;
+ }
+
+ public RestSecurityOAuth2 password(String tokenUrl) {
+ setTokenUrl(tokenUrl);
+ setFlow("password");
+ return this;
+ }
+
+ public RestSecurityOAuth2 application(String tokenUrl) {
+ setTokenUrl(tokenUrl);
+ setFlow("application");
+ return this;
+ }
+
+ public RestSecurityOAuth2 accessCode(String authorizationUrl, String tokenUrl) {
+ setAuthorizationUrl(authorizationUrl);
+ setTokenUrl(tokenUrl);
+ setFlow("accessCode");
+ return this;
+ }
+
+ public RestSecurityOAuth2 withScope(String key, String description) {
+ scopes.add(new RestPropertyDefinition(key, description));
+ return this;
+ }
+
+ public RestSecuritiesDefinition end() {
+ return rest.getSecurityDefinitions();
+ }
+
}
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
index b7aa89e..c100ae8 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
@@ -41,6 +41,10 @@ import io.swagger.models.RefModel;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.Tag;
+import io.swagger.models.auth.ApiKeyAuthDefinition;
+import io.swagger.models.auth.BasicAuthDefinition;
+import io.swagger.models.auth.In;
+import io.swagger.models.auth.OAuth2Definition;
import io.swagger.models.parameters.AbstractSerializableParameter;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.FormParameter;
@@ -65,6 +69,11 @@ import org.apache.camel.model.rest.RestOperationResponseHeaderDefinition;
import org.apache.camel.model.rest.RestOperationResponseMsgDefinition;
import org.apache.camel.model.rest.RestParamType;
import org.apache.camel.model.rest.RestPropertyDefinition;
+import org.apache.camel.model.rest.RestSecuritiesDefinition;
+import org.apache.camel.model.rest.RestSecurityApiKey;
+import org.apache.camel.model.rest.RestSecurityBasicAuth;
+import org.apache.camel.model.rest.RestSecurityDefinition;
+import org.apache.camel.model.rest.RestSecurityOAuth2;
import org.apache.camel.model.rest.VerbDefinition;
import org.apache.camel.spi.ClassResolver;
import org.apache.camel.util.FileUtil;
@@ -123,6 +132,38 @@ public class RestSwaggerReader {
swagger.addTag(tag);
}
+ // setup security definitions
+ RestSecuritiesDefinition sd = rest.getSecurityDefinitions();
+ for (RestSecurityDefinition def : sd.getSecurityDefinitions()) {
+ if (def instanceof RestSecurityBasicAuth) {
+ BasicAuthDefinition auth = new BasicAuthDefinition();
+ auth.setDescription(def.getDescription());
+ swagger.addSecurityDefinition(def.getKey(), auth);
+ } else if (def instanceof RestSecurityApiKey) {
+ RestSecurityApiKey rs = (RestSecurityApiKey) def;
+ ApiKeyAuthDefinition auth = new ApiKeyAuthDefinition();
+ auth.setDescription(rs.getDescription());
+ auth.setName(rs.getName());
+ if (rs.getInHeader() != null && rs.getInHeader()) {
+ auth.setIn(In.HEADER);
+ } else {
+ auth.setIn(In.QUERY);
+ }
+ swagger.addSecurityDefinition(def.getKey(), auth);
+ } else if (def instanceof RestSecurityOAuth2) {
+ RestSecurityOAuth2 rs = (RestSecurityOAuth2) def;
+ OAuth2Definition auth = new OAuth2Definition();
+ auth.setDescription(rs.getDescription());
+ auth.setFlow(rs.getFlow());
+ auth.setAuthorizationUrl(rs.getAuthorizationUrl());
+ auth.setTokenUrl(rs.getTokenUrl());
+ for (RestPropertyDefinition scope : rs.getScopes()) {
+ auth.addScope(scope.getKey(), scope.getValue());
+ }
+ swagger.addSecurityDefinition(def.getKey(), auth);
+ }
+ }
+
// gather all types in use
Set<String> types = new LinkedHashSet<>();
for (VerbDefinition verb : verbs) {
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
index 680243f..dcebc21 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
@@ -42,13 +42,12 @@ public class RestSwaggerReaderModelApiSecurityTest extends CamelTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
-// restConfiguration()
-// .apiSecurityProperty("petstore_auth", "type", "oauth2")
-// .apiSecurityProperty("petstore_auth", "authorizationUrl", "http://petstore.swagger.io/oauth/dialog")
-// .apiSecurityProperty("petstore_auth", "flow", "implicit");
-
- // this user REST service is json only
rest("/user").tag("dude").description("User rest service")
+ // setup security definitions
+ .securityDefinitions()
+ .oauth2("petstore_auth").authorizationUrl("http://petstore.swagger.io/oauth/dialog").end()
+ .apiKey("api_key").withHeader("myHeader").end()
+ .end()
.consumes("application/json").produces("application/json")
.get("/{id}/{date}").description("Find user by id and date").outType(User.class)
@@ -88,8 +87,12 @@ public class RestSwaggerReaderModelApiSecurityTest extends CamelTestSupport {
String json = mapper.writeValueAsString(swagger);
log.info(json);
-System.out.println(json);
+ assertTrue(json.contains("\"securityDefinitions\" : {"));
+ assertTrue(json.contains("\"type\" : \"oauth2\","));
+ assertTrue(json.contains("\"authorizationUrl\" : \"http://petstore.swagger.io/oauth/dialog\","));
+ assertTrue(json.contains("\"type\" : \"apiKey\","));
+ assertTrue(json.contains("\"in\" : \"header\""));
assertTrue(json.contains("\"host\" : \"localhost:8080\""));
assertTrue(json.contains("\"description\" : \"The user returned\""));
assertTrue(json.contains("\"$ref\" : \"#/definitions/User\""));
--
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.