Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private Component createSoaComponent() {
inlineTypes = new HashMap<>();

buildSoaSecuritySchemes();

buildSoaExposedTypes();

buildSoaServices();
Expand Down Expand Up @@ -821,6 +821,16 @@ private void buildSoaOperations() {
}
}

/**
* Create soa operation from swagger service
*
* @param path
* @param swgVerb
* @param swgOperation
* @param debugPath
* @param globalSecuritySchemes - global security scheme apply on the whole swagger
* @return the created operation
*/
private org.obeonetwork.dsl.soa.Operation createSoaOperation(String path, HttpMethod swgVerb, Operation swgOperation, List<String> debugPath) {
Service soaService = getSoaServiceFromPath(path);
Interface soaInterface = getOrCreateInterface(soaService);
Expand Down Expand Up @@ -884,27 +894,35 @@ private org.obeonetwork.dsl.soa.Operation createSoaOperation(String path, HttpMe
}
}

if (swgOperation.getSecurity() != null) {
for (SecurityRequirement swgSecurityRequirement : swgOperation.getSecurity()) {
final List<SecurityRequirement> securitySchemeForOp = swgOperation.getSecurity();
final List<SecurityRequirement> globalDeclaredSecurity = openApi.getSecurity();
// check if security behavior has been added to the operation in the swagger
Set<SecurityRequirement> allSecurityRequirement = new HashSet<>();
if (securitySchemeForOp != null) {
allSecurityRequirement.addAll(securitySchemeForOp);
}
if (globalDeclaredSecurity != null) {
allSecurityRequirement.addAll(globalDeclaredSecurity);
}
for (SecurityRequirement swgSecurityRequirement : allSecurityRequirement) {

if (!swgSecurityRequirement.keySet().isEmpty()) {
String ssKey = swgSecurityRequirement.keySet().iterator().next();
if (!swgSecurityRequirement.keySet().isEmpty()) {
String ssKey = swgSecurityRequirement.keySet().iterator().next();

for (org.obeonetwork.dsl.soa.SecurityScheme securityScheme : soaComponent.getSecuritySchemes().stream()//
.filter(ss -> ssKey.equals(ss.getName()))//
.collect(toList())) {
SecurityApplication soaSecurityApplication = SoaFactory.eINSTANCE.createSecurityApplication();
soaSecurityApplication.setSecurityScheme(securityScheme);
soaOperation.getSecurityApplications().add(soaSecurityApplication);
for (org.obeonetwork.dsl.soa.SecurityScheme securityScheme : soaComponent.getSecuritySchemes().stream()//
.filter(ss -> ssKey.equals(ss.getName()))//
.toList()) {
SecurityApplication soaSecurityApplication = SoaFactory.eINSTANCE.createSecurityApplication();
soaSecurityApplication.setSecurityScheme(securityScheme);
soaOperation.getSecurityApplications().add(soaSecurityApplication);

List<String> scopeNames = swgSecurityRequirement.get(ssKey);
if (scopeNames != null) {
for (String scopeName : scopeNames) {
List<Scope> soaScopes = securityScheme.getFlows().stream()//
.flatMap(f -> f.getScopes().stream()).filter(s -> s.getName().equals(scopeName))//
.collect(toList());
soaSecurityApplication.getScopes().addAll(soaScopes);
}
List<String> scopeNames = swgSecurityRequirement.get(ssKey);
if (scopeNames != null) {
for (String scopeName : scopeNames) {
List<Scope> soaScopes = securityScheme.getFlows().stream()//
.flatMap(f -> f.getScopes().stream()).filter(s -> s.getName().equals(scopeName))//
.collect(toList());
soaSecurityApplication.getScopes().addAll(soaScopes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.EList;
Expand All @@ -67,6 +68,7 @@
import org.obeonetwork.dsl.soa.Information;
import org.obeonetwork.dsl.soa.ParameterPassingMode;
import org.obeonetwork.dsl.soa.Scope;
import org.obeonetwork.dsl.soa.SecurityApplication;
import org.obeonetwork.dsl.soa.SecuritySchemeType;
import org.obeonetwork.dsl.soa.Service;
import org.obeonetwork.dsl.soa.gen.swagger.utils.ComponentGenUtil;
Expand Down Expand Up @@ -166,6 +168,7 @@ private OpenAPI createOpenAPI() {
buildTags();
buildSecuritySchemes();
buildSchemas();
buildGlobalSecurity();
buildPaths();

return openApi;
Expand Down Expand Up @@ -207,6 +210,29 @@ private Tag createTag(Service soaService) {

return tag;
}

/**
* fill the global security rule if a rule is apply on all operations
*/
private void buildGlobalSecurity() {
List<org.obeonetwork.dsl.soa.Operation> restOperations = soaComponent.getProvidedServices().stream()
.map(soaService -> soaService.getOwnedInterface()).filter(itf -> itf != null)
.flatMap(itf -> itf.getOwnedOperations().stream()).filter(o -> o.getExposition() == ExpositionKind.REST)
.toList();
if (restOperations != null && !restOperations.isEmpty()) {
// we use the first operation as a lighter list of cases to analyze
for (SecurityApplication securityApplication : restOperations.get(0).getAllSecurityApplications()) {
if (restOperations.stream().allMatch(
ope -> ope.getAllSecurityApplications().stream().anyMatch(securityApp -> securityApplication
.getSecurityScheme().getName().equals(securityApp.getSecurityScheme().getName())))) {
SecurityRequirement swgSecurityRequirement = new SecurityRequirement();
swgSecurityRequirement.addList(securityApplication.getSecurityScheme().getName(),
securityApplication.getScopes().stream().map(Scope::getName).toList());
openApi.addSecurityItem(swgSecurityRequirement);
}
}
}
}

private void buildSecuritySchemes() {
soaComponent.getSecuritySchemes().forEach(soaSecurityScheme -> {
Expand Down Expand Up @@ -910,10 +936,13 @@ private Operation createOperation(org.obeonetwork.dsl.soa.Operation soaOperation
for (org.obeonetwork.dsl.soa.SecurityApplication soaSecurityApplication : soaOperation
.getAllSecurityApplications()) {
org.obeonetwork.dsl.soa.SecurityScheme soaSecurityScheme = soaSecurityApplication.getSecurityScheme();
SecurityRequirement swgSecurityRequirement = new SecurityRequirement();
swgSecurityRequirement.addList(soaSecurityScheme.getName(),
soaSecurityApplication.getScopes().stream().map(Scope::getName).collect(toList()));
swgOperation.addSecurityItem(swgSecurityRequirement);
// security item is create for the operation if it doesn't already exist in global security
if (!openApi.getSecurity().stream().anyMatch(globalSecurity -> globalSecurity.containsKey(soaSecurityScheme.getName()))) {
SecurityRequirement swgSecurityRequirement = new SecurityRequirement();
swgSecurityRequirement.addList(soaSecurityScheme.getName(),
soaSecurityApplication.getScopes().stream().map(Scope::getName).collect(toList()));
swgOperation.addSecurityItem(swgSecurityRequirement);
}
}

addPropertiesExtensionsFromSoaToSwg(soaOperation, swgOperation);
Expand Down