Skip to content

Commit 4be8058

Browse files
author
Chris Wiechmann
committed
#61 Now exporting InboundSecurityPolicy instead of AuthPolicy
into security column for Console and CSV-Export
1 parent 75c557c commit 4be8058

5 files changed

Lines changed: 113 additions & 23 deletions

File tree

modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIFilter.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
import com.axway.apim.adapter.apis.APIFilter.Builder.APIType;
1818
import com.axway.apim.adapter.clientApps.ClientAppFilter;
1919
import com.axway.apim.api.API;
20+
import com.axway.apim.api.model.DeviceType;
21+
import com.axway.apim.api.model.InboundProfile;
2022
import com.axway.apim.api.model.OutboundProfile;
2123
import com.axway.apim.api.model.Policy;
24+
import com.axway.apim.api.model.SecurityDevice;
25+
import com.axway.apim.api.model.SecurityProfile;
2226
import com.axway.apim.lib.errorHandling.AppException;
27+
import com.axway.apim.lib.utils.Utils;
2328

2429
public class APIFilter {
2530

@@ -435,20 +440,43 @@ public boolean filter(API api) throws AppException {
435440
}
436441
}
437442
if(this.getPolicyName()!=null) {
438-
Pattern pattern = Pattern.compile(this.getPolicyName().replace("*", ".*"));
439-
Iterator<OutboundProfile> it = api.getOutboundProfiles().values().iterator();
440443
boolean requestedPolicyUsed = false;
441-
while(it.hasNext()) {
442-
OutboundProfile profile = it.next();
443-
for(Policy policy : profile.getAllPolices()) {
444-
if(policy.getName()==null) {
445-
LOG.warn("Cannot check policy: "+policy+" as policy name is empty.");
446-
continue;
444+
Pattern pattern = Pattern.compile(this.getPolicyName().replace("*", ".*"));
445+
if(api.getOutboundProfiles()!=null) {
446+
Iterator<OutboundProfile> it = api.getOutboundProfiles().values().iterator();
447+
while(it.hasNext()) {
448+
OutboundProfile profile = it.next();
449+
for(Policy policy : profile.getAllPolices()) {
450+
if(policy.getName()==null) {
451+
LOG.warn("Cannot check policy: "+policy+" as policy name is empty.");
452+
continue;
453+
}
454+
Matcher matcher = pattern.matcher(policy.getName());
455+
if(matcher.matches()) {
456+
requestedPolicyUsed = true;
457+
break;
458+
}
447459
}
448-
Matcher matcher = pattern.matcher(policy.getName());
449-
if(matcher.matches()) {
450-
requestedPolicyUsed = true;
451-
break;
460+
}
461+
}
462+
if(api.getInboundProfiles()!=null) {
463+
Iterator<InboundProfile> it = api.getInboundProfiles().values().iterator();
464+
while(it.hasNext()) {
465+
InboundProfile profile = it.next();
466+
if(profile.getSecurityProfile()!=null) {
467+
for(SecurityProfile securityProfile : api.getSecurityProfiles()) {
468+
if(securityProfile.getName().equals(profile.getSecurityProfile())) {
469+
for(SecurityDevice device : securityProfile.getDevices()) {
470+
if(device.getType()!=DeviceType.authPolicy) continue;
471+
String securityPolicy = device.getProperties().get("authenticationPolicy");
472+
Matcher matcher = pattern.matcher(Utils.getExternalPolicyName(securityPolicy));
473+
if(matcher.matches()) {
474+
requestedPolicyUsed = true;
475+
break;
476+
}
477+
}
478+
}
479+
}
452480
}
453481
}
454482
}

modules/apim-adapter/src/main/java/com/axway/apim/lib/utils/Utils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,12 @@ public static boolean askYesNo(String question, String positive, String negative
6363
// Assess if we match a positive response
6464
return answer.matches(positive);
6565
}
66+
67+
public static String getExternalPolicyName(String policy) {
68+
if(policy.startsWith("<key")) {
69+
policy = policy.substring(policy.indexOf("<key type='FilterCircuit'>"));
70+
policy = policy.substring(policy.indexOf("value='")+7, policy.lastIndexOf("'/></key>"));
71+
}
72+
return policy;
73+
}
6674
}

modules/apim-adapter/src/test/java/com/axway/apim/adapter/apis/APIFilterTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import static org.testng.Assert.assertTrue;
55

66
import java.io.IOException;
7+
import java.util.ArrayList;
78
import java.util.HashMap;
9+
import java.util.List;
810
import java.util.Map;
911

1012
import org.testng.Assert;
@@ -16,8 +18,12 @@
1618
import com.axway.apim.adapter.apis.APIFilter.Builder.APIType;
1719
import com.axway.apim.adapter.apis.APIManagerPoliciesAdapter.PolicyType;
1820
import com.axway.apim.api.API;
21+
import com.axway.apim.api.model.DeviceType;
22+
import com.axway.apim.api.model.InboundProfile;
1923
import com.axway.apim.api.model.OutboundProfile;
2024
import com.axway.apim.api.model.Policy;
25+
import com.axway.apim.api.model.SecurityDevice;
26+
import com.axway.apim.api.model.SecurityProfile;
2127
import com.axway.apim.api.model.ServiceProfile;
2228
import com.axway.apim.lib.errorHandling.AppException;
2329
import com.axway.apim.lib.utils.TestIndicator;
@@ -238,6 +244,17 @@ public void testPolicyFilter() throws AppException {
238244
assertFalse(filter.filter(testAPI), "API must NOT match to pattern 'Not used policy' as it is using 'Response Policy 1'");
239245
}
240246

247+
@Test
248+
public void testInboundSecurityPolicyFilter() throws AppException {
249+
API testAPI = new API();
250+
addInboundSecurityPolicy(testAPI, "Inbound Security Policy 1");
251+
252+
APIFilter filter = new APIFilter.Builder()
253+
.hasPolicyName("Inbound Security*")
254+
.build();
255+
assertTrue(filter.filter(testAPI), "API must match to pattern 'Inbound Security*'");
256+
}
257+
241258
private API getAPIWithBackendBasepath(String basePath) {
242259
API api = new API();
243260
ServiceProfile serviceProfile = new ServiceProfile();
@@ -272,5 +289,42 @@ private API addPolicy(API api, String policyName, PolicyType type) throws AppExc
272289
api.setOutboundProfiles(outboundProfiles);
273290
return api;
274291
}
292+
293+
private API addInboundSecurityPolicy(API api, String policyName) throws AppException {
294+
Map<String, String> properties = new HashMap<String, String>();
295+
properties.put("authenticationPolicy", "<key type='CircuitContainer'><id field='name' value='API Keys'/><key type='FilterCircuit'><id field='name' value='"+policyName+"'/></key></key>");
296+
SecurityDevice securityDevice = new SecurityDevice();
297+
securityDevice.setType(DeviceType.authPolicy);
298+
securityDevice.setConvertPolicies(false);
299+
securityDevice.setProperties(properties);
300+
List<SecurityDevice> devices = new ArrayList<SecurityDevice>();
301+
devices.add(securityDevice);
302+
SecurityProfile securityProfile = new SecurityProfile();
303+
securityProfile.setName("_default");
304+
securityProfile.setDevices(devices);
305+
List<SecurityProfile> securityProfiles = new ArrayList<SecurityProfile>();
306+
securityProfiles.add(securityProfile);
307+
api.setSecurityProfiles(securityProfiles);
308+
309+
InboundProfile inboundProfile = new InboundProfile();
310+
inboundProfile.setSecurityProfile("_default");
311+
Map<String, InboundProfile> inboundProfiles = new HashMap<String, InboundProfile>();
312+
inboundProfiles.put("_default", inboundProfile);
313+
api.setInboundProfiles(inboundProfiles);
314+
return api;
315+
}
275316
}
276317

318+
/**
319+
* "inboundProfiles":{
320+
"findPetsByStatus":{
321+
"securityProfile":"Yet another API-Key profile"
322+
},
323+
"_default":{
324+
"securityProfile":"_default"
325+
}
326+
},
327+
328+
329+
*/
330+

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.axway.apim.api.model.TagMap;
2626
import com.axway.apim.api.model.apps.ClientApplication;
2727
import com.axway.apim.lib.errorHandling.AppException;
28+
import com.axway.apim.lib.utils.Utils;
2829
import com.fasterxml.jackson.annotation.JsonIgnore;
2930
import com.fasterxml.jackson.annotation.JsonProperty;
3031
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@@ -75,14 +76,6 @@ public Map<String, OutboundProfile> getOutboundProfiles() throws AppException {
7576
}
7677
return this.actualAPIProxy.getOutboundProfiles();
7778
}
78-
79-
private static String getExternalPolicyName(String policy) {
80-
if(policy.startsWith("<key")) {
81-
policy = policy.substring(policy.indexOf("<key type='FilterCircuit'>"));
82-
policy = policy.substring(policy.indexOf("value='")+7, policy.lastIndexOf("'/></key>"));
83-
}
84-
return policy;
85-
}
8679

8780

8881
public List<SecurityProfile> getSecurityProfiles() throws AppException {
@@ -97,12 +90,12 @@ public List<SecurityProfile> getSecurityProfiles() throws AppException {
9790
if(device.getType().equals(DeviceType.oauthExternal)) {
9891
String tokenStore = device.getProperties().get("tokenStore");
9992
if(tokenStore!=null) {
100-
device.getProperties().put("tokenStore", getExternalPolicyName(tokenStore));
93+
device.getProperties().put("tokenStore", Utils.getExternalPolicyName(tokenStore));
10194
}
10295
} else if(device.getType().equals(DeviceType.authPolicy)) {
10396
String authenticationPolicy = device.getProperties().get("authenticationPolicy");
10497
if(authenticationPolicy!=null) {
105-
device.getProperties().put("authenticationPolicy", getExternalPolicyName(authenticationPolicy));
98+
device.getProperties().put("authenticationPolicy", Utils.getExternalPolicyName(authenticationPolicy));
10699
}
107100
}
108101
device.setConvertPolicies(false);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
import com.axway.apim.api.API;
2323
import com.axway.apim.api.export.ExportAPI;
2424
import com.axway.apim.api.export.lib.APIExportParams;
25+
import com.axway.apim.api.model.DeviceType;
2526
import com.axway.apim.api.model.InboundProfile;
2627
import com.axway.apim.api.model.Organization;
2728
import com.axway.apim.api.model.OutboundProfile;
2829
import com.axway.apim.api.model.SecurityDevice;
2930
import com.axway.apim.api.model.SecurityProfile;
3031
import com.axway.apim.lib.errorHandling.AppException;
3132
import com.axway.apim.lib.errorHandling.ErrorCode;
33+
import com.axway.apim.lib.utils.Utils;
3234

3335
public abstract class APIResultHandler {
3436

@@ -120,7 +122,12 @@ protected static String getUsedSecurity(API api) {
120122
InboundProfile profile = it.next();
121123
SecurityProfile usedSecProfile = secProfilesMappedByName.get(profile.getSecurityProfile());
122124
for(SecurityDevice device : usedSecProfile.getDevices()) {
123-
usedSecurity.add(""+device.getType());
125+
if(device.getType()==DeviceType.authPolicy) {
126+
String authenticationPolicy = device.getProperties().get("authenticationPolicy");
127+
usedSecurity.add(Utils.getExternalPolicyName(authenticationPolicy));
128+
} else {
129+
usedSecurity.add(""+device.getType());
130+
}
124131
}
125132
}
126133
String result = usedSecurity.toString().replace("[", "").replace("]", "");

0 commit comments

Comments
 (0)