Skip to content

Commit 201e2de

Browse files
author
rathnapandi
committed
Fix junit and write open api format based on file type.
1 parent 96007df commit 201e2de

File tree

4 files changed

+148
-64
lines changed

4 files changed

+148
-64
lines changed

modules/apim-adapter/src/main/java/com/axway/apim/api/specification/APISpecification.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,8 @@ public APISpecification setFilterConfig(APISpecificationFilter filterConfig) {
182182
this.filterConfig = filterConfig;
183183
return this;
184184
}
185+
186+
public ObjectMapper getMapper() {
187+
return mapper;
188+
}
185189
}

modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void saveAPILocally(ObjectMapper mapper, ExportAPI exportAPI, String conf
6262
throw new AppException("Backend API Definition is not available for the API : " + exportAPI.getName() + ", hence use the option -useFEAPIDefinition to export API", ErrorCode.BACKEND_API_DEF_NA);
6363
return;
6464
}
65-
writeSpec(mapper, apiDef, exportAPI, localFolder);
65+
writeSpec(apiDef, exportAPI, localFolder);
6666
Image image = exportAPI.getAPIImage();
6767
if (image != null && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) {
6868
writeBytesToFile(image.getImageContent(), localFolder + File.separator + image.getBaseFilename());
@@ -143,22 +143,23 @@ private void storePrivateCerts(File localFolder, List<AuthenticationProfile> aut
143143
}
144144
}
145145

146-
public void writeSpec(ObjectMapper mapper, APISpecification apiDef, ExportAPI exportAPI, File localFolder) throws AppException {
146+
public void writeSpec(APISpecification apiDef, ExportAPI exportAPI, File localFolder) throws AppException {
147147
String targetFile = null;
148148
try {
149149
if (!(apiDef instanceof WSDLSpecification && EnvironmentProperties.RETAIN_BACKEND_URL) && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) {
150150
String fileName = Utils.replaceSpecialChars(exportAPI.getName());
151151
String fileExtension = apiDef.getAPIDefinitionType().getFileExtension();
152-
if(apiDef instanceof Swagger2xSpecification || apiDef instanceof OAS3xSpecification){
152+
if (apiDef instanceof Swagger2xSpecification || apiDef instanceof OAS3xSpecification) {
153+
ObjectMapper mapper = apiDef.getMapper();
153154
if (mapper.getFactory() instanceof YAMLFactory) {
154-
fileExtension = APISpecification.APISpecType.SWAGGER_API_20_YAML.getFileExtension();
155-
}else {
156-
fileExtension = APISpecification.APISpecType.SWAGGER_API_20.getFileExtension();
155+
fileExtension = APISpecification.APISpecType.SWAGGER_API_20_YAML.getFileExtension();
156+
} else {
157+
fileExtension = APISpecification.APISpecType.SWAGGER_API_20.getFileExtension();
157158
}
158159
targetFile = localFolder.getCanonicalPath() + "/" + fileName + fileExtension;
159160
Object spec = mapper.readValue(apiDef.getApiSpecificationContent(), Object.class);
160161
mapper.writerWithDefaultPrettyPrinter().writeValue(new File(targetFile), spec);
161-
}else {
162+
} else {
162163
targetFile = localFolder.getCanonicalPath() + "/" + fileName + fileExtension;
163164
writeBytesToFile(apiDef.getApiSpecificationContent(), targetFile);
164165
}

modules/spectoconfig/src/main/java/com/axway/apim/config/GenerateTemplate.java

Lines changed: 127 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
import java.security.cert.X509Certificate;
5151
import java.util.*;
5252

53+
import static io.swagger.v3.oas.models.security.SecurityScheme.In.QUERY;
54+
import static io.swagger.v3.oas.models.security.SecurityScheme.In.HEADER;
55+
56+
5357
public class GenerateTemplate implements APIMCLIServiceProvider {
5458

5559

@@ -59,6 +63,11 @@ public class GenerateTemplate implements APIMCLIServiceProvider {
5963
public static final String PASS_THROUGH = "Pass Through";
6064
public static final String REMOVE_CREDENTIALS_ON_SUCCESS = "removeCredentialsOnSuccess";
6165
public static final String TOKEN_STORE = "tokenStore";
66+
public static final String TAKE_FROM = "takeFrom";
67+
public static final String OAUTH_TOKEN_CLIENT_ID = "${oauth.token.client_id}";
68+
public static final String USE_CLIENT_REGISTRY = "useClientRegistry";
69+
public static final String SUBJECT_SELECTOR = "subjectSelector";
70+
public static final String HEADER_STR = "HEADER";
6271

6372
@Override
6473
public String getName() {
@@ -292,42 +301,71 @@ public Map<String, InboundProfile> addInboundPerMethodOverride(OpenAPI openAPI,
292301
operationId = httpMethod.name() + " " + key;
293302
}
294303
List<SecurityRequirement> securityRequirements = operation.getSecurity();
295-
if (securityRequirements == null) {
296-
SecurityProfile passThroughProfile = createPassThroughSecurityProfile();
297-
inboundProfile.setSecurityProfile(passThroughProfile.getName());
298-
inboundProfiles.put(operationId, inboundProfile);
299-
securityProfiles.add(passThroughProfile);
300-
} else {
301-
302-
for (SecurityRequirement securityRequirement : securityRequirements) {
303-
Set<String> keys = securityRequirement.keySet();
304-
for (String securityKey : keys) {
305-
SecurityScheme securityScheme = openAPI.getComponents().getSecuritySchemes().get(securityKey);
306-
SecurityScheme.Type type = securityScheme.getType();
307-
308-
if (type == SecurityScheme.Type.OAUTH2) {
309-
List<String> scopes = securityRequirement.get(securityKey);
310-
SecurityProfile oauth2SecurityProfile = createOauthSecurityProfile(operationId, scopes);
311-
inboundProfile.setSecurityProfile(oauth2SecurityProfile.getName());
312-
inboundProfiles.put(operationId, inboundProfile);
313-
securityProfiles.add(oauth2SecurityProfile);
314-
} else if (type == SecurityScheme.Type.APIKEY) {
315-
LOG.warn("API key is not handled");
316-
} else if (type == SecurityScheme.Type.MUTUALTLS) {
317-
LOG.warn("Mutual auth is not handled");
318-
}
319-
}
320-
}
321-
}
304+
handleSecurity(openAPI, inboundProfiles, securityRequirements, securityProfiles, inboundProfile, operationId);
322305
}
323306
}
324307
api.setSecurityProfiles(securityProfiles);
325308
return inboundProfiles;
326309
}
327310

328-
public SecurityProfile createPassThroughSecurityProfile() {
311+
312+
public void handleSecurity(OpenAPI openAPI, Map<String, InboundProfile> inboundProfiles, List<SecurityRequirement> securityRequirements, List<SecurityProfile> securityProfiles, InboundProfile inboundProfile, String operationId) {
313+
if (securityRequirements == null || securityRequirements.isEmpty()) {
314+
SecurityProfile passThroughProfile = createPassThroughSecurityProfile(operationId);
315+
inboundProfile.setSecurityProfile(passThroughProfile.getName());
316+
inboundProfiles.put(operationId, inboundProfile);
317+
securityProfiles.add(passThroughProfile);
318+
} else {
319+
for (SecurityRequirement securityRequirement : securityRequirements) {
320+
Set<String> keys = securityRequirement.keySet();
321+
for (String securityKey : keys) {
322+
SecurityScheme securityScheme = openAPI.getComponents().getSecuritySchemes().get(securityKey);
323+
mapAPIMSecurity(securityRequirement, securityScheme, inboundProfiles, inboundProfile, securityProfiles, operationId, securityKey);
324+
}
325+
}
326+
}
327+
}
328+
329+
public void mapAPIMSecurity(SecurityRequirement securityRequirement, SecurityScheme securityScheme, Map<String, InboundProfile> inboundProfiles, InboundProfile inboundProfile, List<SecurityProfile> securityProfiles, String operationId, String securityKey) {
330+
SecurityScheme.Type type = securityScheme.getType();
331+
if (type == SecurityScheme.Type.OAUTH2) {
332+
LOG.info("mapping oauth2 profile");
333+
List<String> scopes = securityRequirement.get(securityKey);
334+
SecurityProfile oauth2SecurityProfile = createOauthSecurityProfile(operationId, scopes);
335+
inboundProfile.setSecurityProfile(oauth2SecurityProfile.getName());
336+
inboundProfiles.put(operationId, inboundProfile);
337+
securityProfiles.add(oauth2SecurityProfile);
338+
} else if (type == SecurityScheme.Type.APIKEY) {
339+
LOG.info("mapping API key profile");
340+
List<String> scopes = securityRequirement.get(securityKey);
341+
SecurityScheme.In in = securityScheme.getIn();
342+
if (in == SecurityScheme.In.COOKIE) {
343+
LOG.warn("API key in cookie not supported");
344+
return;
345+
}
346+
String apikeyLocation = in.name();
347+
String fieldName = securityScheme.getName();
348+
SecurityProfile apiKeySecurityProfile = createApiKeySecurityProfile(operationId, apikeyLocation, fieldName, scopes);
349+
inboundProfile.setSecurityProfile(apiKeySecurityProfile.getName());
350+
inboundProfiles.put(operationId, inboundProfile);
351+
securityProfiles.add(apiKeySecurityProfile);
352+
} else if (type == SecurityScheme.Type.MUTUALTLS) {
353+
LOG.warn("Mutual auth is not handled");
354+
} else if (type == SecurityScheme.Type.OPENIDCONNECT || type == SecurityScheme.Type.HTTP && securityScheme.getScheme().equalsIgnoreCase("bearer")) {
355+
LOG.info("External auth / openid connect is not handled");
356+
List<String> scopes = securityRequirement.get(securityKey);
357+
SecurityProfile oauth2ExternalSecurityProfile = createOauthExternalSecurityProfile(operationId, scopes);
358+
inboundProfile.setSecurityProfile(oauth2ExternalSecurityProfile.getName());
359+
inboundProfiles.put(operationId, inboundProfile);
360+
securityProfiles.add(oauth2ExternalSecurityProfile);
361+
} else if (type == SecurityScheme.Type.HTTP && securityScheme.getScheme().equalsIgnoreCase("basic")) {
362+
LOG.warn("Basic Auth is not handled");
363+
}
364+
}
365+
366+
public SecurityProfile createPassThroughSecurityProfile(String operationId) {
329367
SecurityProfile profile = new SecurityProfile();
330-
profile.setName(PASS_THROUGH);
368+
profile.setName(PASS_THROUGH + " " + operationId);
331369
profile.setIsDefault(false);
332370
SecurityDevice securityDevice = new SecurityDevice();
333371
securityDevice.setName(PASS_THROUGH);
@@ -343,6 +381,60 @@ public SecurityProfile createPassThroughSecurityProfile() {
343381
return profile;
344382
}
345383

384+
public SecurityProfile createApiKeySecurityProfile(String operationId, String apikeyLocation, String fieldName, List<String> scopes) {
385+
SecurityProfile profile = new SecurityProfile();
386+
profile.setName("apikey " + operationId);
387+
profile.setIsDefault(false);
388+
SecurityDevice securityDevice = new SecurityDevice();
389+
securityDevice.setName("API Key");
390+
securityDevice.setType(DeviceType.apiKey);
391+
securityDevice.setOrder(0);
392+
Map<String, String> properties = new HashMap<>();
393+
properties.put(REMOVE_CREDENTIALS_ON_SUCCESS, "true");
394+
if (apikeyLocation.equals(HEADER.name())) {
395+
properties.put(TAKE_FROM, HEADER_STR);
396+
} else if (apikeyLocation.equals(QUERY.name())) {
397+
properties.put(TAKE_FROM, "QUERY");
398+
}
399+
properties.put("apiKeyFieldName", fieldName);
400+
if (scopes != null && !scopes.isEmpty()) {
401+
String scope = String.join(" ", scopes);
402+
properties.put("scopes", scope);
403+
properties.put("scopesMustMatch", "All");
404+
}
405+
securityDevice.setProperties(properties);
406+
List<SecurityDevice> securityDevices = new ArrayList<>();
407+
securityDevices.add(securityDevice);
408+
profile.setDevices(securityDevices);
409+
return profile;
410+
}
411+
412+
413+
public SecurityProfile createOauthExternalSecurityProfile(String operationId, List<String> scopes) {
414+
SecurityProfile profile = new SecurityProfile();
415+
profile.setName("External Oauth2 " + operationId);
416+
profile.setIsDefault(false);
417+
SecurityDevice securityDevice = new SecurityDevice();
418+
securityDevice.setName("OAuth (External)");
419+
securityDevice.setType(DeviceType.oauthExternal);
420+
securityDevice.setOrder(0);
421+
Map<String, String> properties = new HashMap<>();
422+
properties.put(TOKEN_STORE, "Tokeninfo policy 1");
423+
properties.put(USE_CLIENT_REGISTRY, "true");
424+
properties.put(SUBJECT_SELECTOR, OAUTH_TOKEN_CLIENT_ID);
425+
properties.put("oauth.token.client_id", OAUTH_TOKEN_CLIENT_ID);
426+
properties.put("oauth.token.scopes", "${oauth.token.scopes}");
427+
properties.put("oauth.token.valid", "${oauth.token.valid}");
428+
String scope = String.join(" ", scopes);
429+
setupOauthProperties(properties, scope);
430+
securityDevice.setProperties(properties);
431+
List<SecurityDevice> securityDevices = new ArrayList<>();
432+
securityDevices.add(securityDevice);
433+
profile.setDevices(securityDevices);
434+
return profile;
435+
}
436+
437+
346438
public SecurityProfile createOauthSecurityProfile(String operationId, List<String> scopes) {
347439
SecurityProfile profile = new SecurityProfile();
348440
profile.setName("Oauth2 " + operationId);
@@ -490,20 +582,20 @@ private List<SecurityProfile> addInboundSecurityToAPI(String frontendAuthType) t
490582
Map<String, String> properties = new HashMap<>();
491583
if (deviceType.equals(DeviceType.apiKey)) {
492584
properties.put("apiKeyFieldName", "KeyId");
493-
properties.put("takeFrom", "HEADER");
585+
properties.put(TAKE_FROM, HEADER_STR);
494586
properties.put(REMOVE_CREDENTIALS_ON_SUCCESS, "true");
495587
} else if (deviceType.equals(DeviceType.oauth)) {
496588
properties.put(TOKEN_STORE, "OAuth Access Token Store");
497589
setupOauthProperties(properties, "resource.WRITE, resource.READ");
498590
} else if (deviceType.equals(DeviceType.oauthExternal)) {
499591
properties.put(TOKEN_STORE, "Tokeninfo policy 1");
500-
properties.put("useClientRegistry", "true");
501-
properties.put("subjectSelector", "${oauth.token.client_id}");
592+
properties.put(USE_CLIENT_REGISTRY, "true");
593+
properties.put(SUBJECT_SELECTOR, OAUTH_TOKEN_CLIENT_ID);
502594
setupOauthProperties(properties, "resource.WRITE, resource.READ");
503595
} else if (deviceType.equals(DeviceType.authPolicy)) {
504596
properties.put("authenticationPolicy", "Custom authentication policy");
505-
properties.put("useClientRegistry", "true");
506-
properties.put("subjectSelector", "authentication.subject.id");
597+
properties.put(USE_CLIENT_REGISTRY, "true");
598+
properties.put(SUBJECT_SELECTOR, "authentication.subject.id");
507599
properties.put("descriptionType", ORIGINAL);
508600
properties.put("descriptionUrl", "");
509601
properties.put("descriptionMarkdown", "");
@@ -521,7 +613,7 @@ private List<SecurityProfile> addInboundSecurityToAPI(String frontendAuthType) t
521613
}
522614

523615
private void setupOauthProperties(Map<String, String> properties, String scopes) {
524-
properties.put("accessTokenLocation", "HEADER");
616+
properties.put("accessTokenLocation", HEADER_STR);
525617
properties.put("authorizationHeaderPrefix", "Bearer");
526618
properties.put("accessTokenLocationQueryString", "");
527619
properties.put("scopesMustMatch", "All");

modules/spectoconfig/src/test/java/com/axway/apim/config/GenerateTemplateTest.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,41 +198,28 @@ public void testWithFrontendAuthAlternateName() throws IOException {
198198
}
199199

200200
@Test
201-
public void generateApiMethods() throws IOException {
202-
203-
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/methods.yaml");
201+
public void generateApiMethods() {
202+
OpenAPI openAPI = new OpenAPIV3Parser().read("methods.yaml");
204203
GenerateTemplate generateTemplate = new GenerateTemplate();
205204
List<APIMethod> apiMethods = generateTemplate.addMethods(openAPI);
206-
ObjectMapper objectMapper = new ObjectMapper();
207-
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
208-
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(apiMethods));
209-
// System.out.println(openAPI);
210-
205+
Assert.assertEquals(3, apiMethods.size());
206+
Assert.assertNotNull(apiMethods.get(0).getTags().get("public"));
211207
}
212208

213209
@Test
214-
public void includeInboundPerMethodOverride() throws IOException {
215-
216-
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/methods.yaml");
210+
public void includeInboundPerMethodOverride() {
211+
OpenAPI openAPI = new OpenAPIV3Parser().read("methods.yaml");
217212
GenerateTemplate generateTemplate = new GenerateTemplate();
218213
List<SecurityProfile> securityProfiles = new ArrayList<>();
219214
API api = new API();
220-
generateTemplate.addInboundPerMethodOverride(openAPI, api, securityProfiles);
221-
ObjectMapper objectMapper = new ObjectMapper();
222-
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
223-
FilterProvider filters = new SimpleFilterProvider()
224-
225-
.addFilter("ProfileFilter",
226-
SimpleBeanPropertyFilter.serializeAllExcept("apiMethodId"))
227-
.setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept());
228-
objectMapper.setFilterProvider(filters);
229-
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(api.getInboundProfiles()));
215+
Map<String, InboundProfile> inboundProfileMap = generateTemplate.addInboundPerMethodOverride(openAPI, api, securityProfiles);
216+
Assert.assertNotNull(inboundProfileMap);
230217
}
231218

232219

233220
@Test
234221
public void testInboundOverride() throws IOException {
235-
String[] args = {"template", "generate", "-c", "api-config.yaml", "-a", "src/test/resources/methods.yaml", "-frontendAuthType", "apiKey", "-inboundPerMethodOverride", "-o", "yaml"};
222+
String[] args = {"template", "generate", "-c", "api-config.json", "-a", "src/test/resources/methods.yaml", "-frontendAuthType", "apiKey", "-inboundPerMethodOverride", "-o", "json"};
236223
GenerateTemplate.generate(args);
237224
// DocumentContext documentContext = JsonPath.parse(Files.newInputStream(Paths.get("api-config.json")));
238225
// Assert.assertEquals("Swagger Petstore - OpenAPI 3.0", documentContext.read("$.name"));

0 commit comments

Comments
 (0)