Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CAMEL-16606 Allow specifying security requirement(s) applicable to en…
…tire API Calls to RestDefinition.security(String key [, String scopes]) without a verb adds the security scheme specified by key as a security requirement for all rest calls.
- Loading branch information
1 parent
4299e11
commit ad65210
Showing
16 changed files
with
379 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...catalog/src/generated/resources/org/apache/camel/catalog/models/securityRequirements.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"model": { | ||
"kind": "model", | ||
"name": "securityRequirements", | ||
"title": "Security Requirements", | ||
"description": "To configure global rest security requirements.", | ||
"deprecated": false, | ||
"label": "rest,security", | ||
"javaType": "org.apache.camel.model.rest.RestSecuritiesRequirement", | ||
"input": false, | ||
"output": false | ||
}, | ||
"properties": { | ||
"securityRequirements": { "kind": "element", "displayName": "Security Requirements", "required": true, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.SecurityDefinition>", "oneOf": [ "securityRequirement" ], "deprecated": false, "autowired": false, "secret": false } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...a/src/test/java/org/apache/camel/openapi/RestOpenApiModelApiSecurityRequirementsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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.camel.openapi; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import io.apicurio.datamodels.Library; | ||
import io.apicurio.datamodels.openapi.models.OasDocument; | ||
import org.apache.camel.builder.RouteBuilder; | ||
import org.apache.camel.impl.engine.DefaultClassResolver; | ||
import org.apache.camel.test.junit5.CamelTestSupport; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class RestOpenApiModelApiSecurityRequirementsTest extends CamelTestSupport { | ||
|
||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
@Override | ||
protected RouteBuilder createRouteBuilder() throws Exception { | ||
return new RouteBuilder() { | ||
@Override | ||
public void configure() { | ||
rest() | ||
.securityDefinitions() | ||
.oauth2("petstore_auth") | ||
.authorizationUrl("https://petstore.swagger.io/oauth/dialog") | ||
.end() | ||
.apiKey("api_key") | ||
.withHeader("myHeader").end() | ||
.end() | ||
.security("petstore_auth", "read, write") | ||
.security("api_key"); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void testReaderRead() throws Exception { | ||
BeanConfig config = new BeanConfig(); | ||
config.setHost("localhost:8080"); | ||
config.setSchemes(new String[] { "http" }); | ||
config.setBasePath("/api"); | ||
config.setTitle("Camel User store"); | ||
config.setLicense("Apache 2.0"); | ||
config.setLicenseUrl("https://www.apache.org/licenses/LICENSE-2.0.html"); | ||
config.setVersion("2.0"); | ||
RestOpenApiReader reader = new RestOpenApiReader(); | ||
|
||
OasDocument openApi = reader.read(context, context.getRestDefinitions(), null, config, context.getName(), | ||
new DefaultClassResolver()); | ||
assertNotNull(openApi); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.enable(SerializationFeature.INDENT_OUTPUT); | ||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
Object dump = Library.writeNode(openApi); | ||
String json = mapper.writeValueAsString(dump); | ||
|
||
log.info(json); | ||
|
||
assertTrue(json.contains("\"securityDefinitions\" : {")); | ||
assertTrue(json.contains("\"type\" : \"oauth2\"")); | ||
assertTrue(json.contains("\"authorizationUrl\" : \"https://petstore.swagger.io/oauth/dialog\"")); | ||
assertTrue(json.contains("\"flow\" : \"implicit\"")); | ||
assertTrue(json.contains("\"type\" : \"apiKey\",")); | ||
assertTrue(json.contains("\"security\" : [ {")); | ||
assertTrue(json.contains("\"petstore_auth\" : [ \"read\", \"write\" ]")); | ||
assertTrue(json.contains("\"api_key\" : [ ]")); | ||
} | ||
|
||
@Test | ||
public void testReaderReadV3() throws Exception { | ||
BeanConfig config = new BeanConfig(); | ||
config.setHost("localhost:8080"); | ||
config.setSchemes(new String[] { "http" }); | ||
config.setBasePath("/api"); | ||
config.setTitle("Camel User store"); | ||
config.setLicense("Apache 2.0"); | ||
config.setLicenseUrl("https://www.apache.org/licenses/LICENSE-2.0.html"); | ||
RestOpenApiReader reader = new RestOpenApiReader(); | ||
|
||
OasDocument openApi = reader.read(context, context.getRestDefinitions(), null, config, context.getName(), | ||
new DefaultClassResolver()); | ||
assertNotNull(openApi); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.enable(SerializationFeature.INDENT_OUTPUT); | ||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
Object dump = Library.writeNode(openApi); | ||
String json = mapper.writeValueAsString(dump); | ||
|
||
log.info(json); | ||
|
||
assertTrue(json.contains("securitySchemes")); | ||
assertTrue(json.contains("\"type\" : \"oauth2\"")); | ||
assertTrue(json.contains("\"authorizationUrl\" : \"https://petstore.swagger.io/oauth/dialog\"")); | ||
assertTrue(json.contains("\"flows\" : {")); | ||
assertTrue(json.contains("\"implicit\"")); | ||
assertTrue(json.contains("\"security\" : [ {")); | ||
assertTrue(json.contains("\"petstore_auth\" : [ \"read\", \"write\" ]")); | ||
assertTrue(json.contains("\"api_key\" : [ ]")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...-core-model/src/generated/resources/org/apache/camel/model/rest/securityRequirements.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"model": { | ||
"kind": "model", | ||
"name": "securityRequirements", | ||
"title": "Security Requirements", | ||
"description": "To configure global rest security requirements.", | ||
"deprecated": false, | ||
"label": "rest,security", | ||
"javaType": "org.apache.camel.model.rest.RestSecuritiesRequirement", | ||
"input": false, | ||
"output": false | ||
}, | ||
"properties": { | ||
"securityRequirements": { "kind": "element", "displayName": "Security Requirements", "required": true, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.SecurityDefinition>", "oneOf": [ "securityRequirement" ], "deprecated": false, "autowired": false, "secret": false } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.