Skip to content

Commit 8367ab3

Browse files
author
Chris Wiechmann
committed
#44 Added support to filter applications based on their credentials
1 parent 9f7320b commit 8367ab3

8 files changed

Lines changed: 162 additions & 37 deletions

File tree

modules/apim-adapter/src/main/java/com/axway/apim/adapter/clientApps/APIMgrAppsAdapter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.UnsupportedEncodingException;
55
import java.net.URI;
66
import java.net.URISyntaxException;
7+
import java.util.ArrayList;
78
import java.util.HashMap;
89
import java.util.List;
910
import java.util.Map;
@@ -124,9 +125,9 @@ URI getApplicationsUri(ClientAppFilter filter) throws URISyntaxException {
124125

125126
public List<ClientApplication> getApplications(ClientAppFilter filter, boolean logProgress) throws AppException {
126127
readApplicationsFromAPIManager(filter);
127-
List<ClientApplication> apps;
128+
List<ClientApplication> filteredApps = new ArrayList<ClientApplication>();
128129
try {
129-
apps = mapper.readValue(this.apiManagerResponse.get(filter), new TypeReference<List<ClientApplication>>(){});
130+
List<ClientApplication> apps = mapper.readValue(this.apiManagerResponse.get(filter), new TypeReference<List<ClientApplication>>(){});
130131
LOG.debug("Found: "+apps.size() + " applications");
131132
for(int i=0; i<apps.size();i++) {
132133
ClientApplication app = apps.get(i);
@@ -136,14 +137,16 @@ public List<ClientApplication> getApplications(ClientAppFilter filter, boolean l
136137
}
137138
addApplicationCredentials(app, filter.isIncludeCredentials());
138139
addAPIAccess(app, filter.isIncludeAPIAccess());
140+
if(!filter.filter(app)) continue;
141+
filteredApps.add(app);
139142
if(logProgress && apps.size()>5) Utils.progressPercentage(i, apps.size(), "Laoding "+apps.size()+" Applications");
140143
}
141144
if(logProgress && apps.size()>5) System.out.print("\n");
142145
} catch (Exception e) {
143146
throw new AppException("Can't initialize API-Manager API-Representation.", ErrorCode.API_MANAGER_COMMUNICATION, e);
144147
}
145148

146-
return apps;
149+
return filteredApps;
147150
}
148151

149152
public List<ClientApplication> getAllApplications(boolean logProgress) throws AppException {

modules/apim-adapter/src/main/java/com/axway/apim/adapter/clientApps/ClientAppFilter.java

Lines changed: 117 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
57

68
import org.apache.commons.lang.StringUtils;
79
import org.apache.http.NameValuePair;
@@ -11,6 +13,12 @@
1113

1214
import com.axway.apim.adapter.APIManagerAdapter;
1315
import com.axway.apim.api.model.Organization;
16+
import com.axway.apim.api.model.User;
17+
import com.axway.apim.api.model.apps.APIKey;
18+
import com.axway.apim.api.model.apps.ClientAppCredential;
19+
import com.axway.apim.api.model.apps.ClientApplication;
20+
import com.axway.apim.api.model.apps.ExtClients;
21+
import com.axway.apim.api.model.apps.OAuth;
1422
import com.axway.apim.lib.errorHandling.AppException;
1523
import com.axway.apim.lib.errorHandling.ErrorCode;
1624
import com.axway.apim.lib.errorHandling.ErrorState;
@@ -27,13 +35,17 @@ public class ClientAppFilter {
2735

2836
boolean includeImage;
2937

30-
String applicationName;
38+
private String credential;
3139

32-
String state;
40+
private String redirectUrl;
3341

34-
Organization organization;
42+
private String applicationName;
3543

36-
String applicationId;
44+
private String state;
45+
46+
private Organization organization;
47+
48+
private String applicationId;
3749

3850
List<NameValuePair> filters = new ArrayList<NameValuePair>();
3951

@@ -55,6 +67,22 @@ public boolean isIncludeAPIAccess() {
5567
return includeAPIAccess;
5668
}
5769

70+
public void setIncludeQuota(boolean includeQuota) {
71+
this.includeQuota = includeQuota;
72+
}
73+
74+
public void setIncludeCredentials(boolean includeCredentials) {
75+
this.includeCredentials = includeCredentials;
76+
}
77+
78+
public void setIncludeAPIAccess(boolean includeAPIAccess) {
79+
this.includeAPIAccess = includeAPIAccess;
80+
}
81+
82+
public void setIncludeImage(boolean includeImage) {
83+
this.includeImage = includeImage;
84+
}
85+
5886
public String getApplicationId() {
5987
return applicationId;
6088
}
@@ -98,7 +126,21 @@ public void setApplicationId(String applicationId) {
98126
this.applicationId = applicationId;
99127
}
100128

129+
public String getCredential() {
130+
return credential;
131+
}
132+
133+
public void setCredential(String credential) {
134+
this.credential = credential;
135+
}
136+
137+
public String getRedirectUrl() {
138+
return redirectUrl;
139+
}
101140

141+
public void setRedirectUrl(String redirectUrl) {
142+
this.redirectUrl = redirectUrl;
143+
}
102144

103145
public void setState(String state) {
104146
if(state==null) return;
@@ -151,29 +193,76 @@ public String toString() {
151193
}
152194
}
153195

196+
public boolean filter(ClientApplication app) throws AppException {
197+
if(this.getCredential()==null && this.getRedirectUrl()==null) { // Nothing given to filter out.
198+
return true;
199+
}
200+
boolean match = false;
201+
for(ClientAppCredential cred : app.getCredentials()) {
202+
switch(cred.getCredentialType()) {
203+
case "oauth":
204+
match = filterCredential(((OAuth)cred).getClientId(), ((OAuth)cred).getRedirectUrls());
205+
break;
206+
case "extclients":
207+
match = filterCredential(((ExtClients)cred).getClientId(), null);
208+
break;
209+
case "apikeys":
210+
match = filterCredential(((APIKey)cred).getApiKey(), null);
211+
break;
212+
}
213+
if(match) break;
214+
}
215+
return match;
216+
}
217+
218+
private boolean filterCredential(String appCredential, String[] appRedirectUrls) {
219+
if(this.credential!=null) {
220+
Pattern pattern = Pattern.compile(this.credential.replace("*", ".*"));
221+
Matcher matcher = pattern.matcher(appCredential);
222+
if(!matcher.matches()) return false;
223+
}
224+
if(this.redirectUrl!=null) {
225+
if(appRedirectUrls==null || appRedirectUrls.length==0) return false;
226+
Pattern pattern = Pattern.compile(this.redirectUrl.replace("*", ".*"));
227+
boolean redirectUrlMatch = false;
228+
for(String appRedirectUrl : appRedirectUrls) {
229+
Matcher matcher = pattern.matcher(appRedirectUrl);
230+
if(matcher.matches()) {
231+
redirectUrlMatch = true;
232+
break;
233+
}
234+
}
235+
if(!redirectUrlMatch) return false;
236+
}
237+
return true;
238+
}
154239

155240

156241
/**
157242
* Build an applicationAdapter based on the given configuration
158243
*/
159244
public static class Builder {
160245

161-
boolean includeQuota;
246+
private boolean includeQuota;
162247

163-
boolean includeCredentials;
248+
private boolean includeCredentials;
164249

165-
boolean includeImage;
250+
private boolean includeImage;
166251

167-
boolean includeAPIAccess;
252+
private boolean includeAPIAccess;
168253

169-
Organization organization;
254+
private Organization organization;
170255

171256
/** The name of the application */
172-
String applicationName;
257+
private String applicationName;
173258

174-
String applicationId;
259+
private String applicationId;
175260

176-
String state;
261+
private String state;
262+
263+
private String credential;
264+
265+
private String redirectUrl;
177266

178267
public Builder() {
179268
super();
@@ -189,10 +278,12 @@ public ClientAppFilter build() {
189278
filter.setApplicationName(this.applicationName);
190279
filter.setOrganization(this.organization);
191280
filter.setState(this.state);
192-
filter.includeQuota = this.includeQuota;
193-
filter.includeCredentials = this.includeCredentials;
194-
filter.includeImage = this.includeImage;
195-
filter.includeAPIAccess = this.includeAPIAccess;
281+
filter.setIncludeQuota(this.includeQuota);
282+
filter.setIncludeCredentials(this.includeCredentials);
283+
filter.setIncludeImage(this.includeImage);
284+
filter.setIncludeAPIAccess(this.includeAPIAccess);
285+
filter.setCredential(this.credential);
286+
filter.setRedirectUrl(this.redirectUrl);
196287
return filter;
197288
}
198289

@@ -240,6 +331,16 @@ public Builder hasState(String state) {
240331
return this;
241332
}
242333

334+
public Builder hasCredential(String credential) {
335+
this.credential = credential;
336+
return this;
337+
}
338+
339+
public Builder hasRedirectUrl(String redirectUrl) {
340+
this.redirectUrl = redirectUrl;
341+
return this;
342+
}
343+
243344
public Builder includeQuotas(boolean includeQuota) {
244345
this.includeQuota = includeQuota;
245346
return this;

modules/apim-adapter/src/main/java/com/axway/apim/adapter/user/UserFilter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.axway.apim.adapter.user;
22

33
import java.util.ArrayList;
4-
import java.util.Iterator;
54
import java.util.List;
65
import java.util.regex.Matcher;
76
import java.util.regex.Pattern;
@@ -10,8 +9,6 @@
109
import org.apache.http.NameValuePair;
1110
import org.apache.http.message.BasicNameValuePair;
1211

13-
import com.axway.apim.api.model.OutboundProfile;
14-
import com.axway.apim.api.model.Policy;
1512
import com.axway.apim.api.model.User;
1613
import com.axway.apim.lib.errorHandling.AppException;
1714

@@ -188,7 +185,6 @@ public boolean filter(User user) throws AppException {
188185
if(this.getType()==null && this.getOrganizationName()==null) { // Nothing given to filter out.
189186
return true;
190187
}
191-
// Before 7.7, we have to filter out APIs manually!
192188
if(this.getType()!=null && !this.getType().equals(user.getType())) {
193189
return false;
194190
}

modules/app-export/src/main/java/com/axway/apim/appexport/impl/ApplicationExporter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.slf4j.LoggerFactory;
88

99
import com.axway.apim.adapter.clientApps.ClientAppFilter;
10+
import com.axway.apim.adapter.clientApps.ClientAppFilter.Builder;
1011
import com.axway.apim.api.model.apps.ClientApplication;
1112
import com.axway.apim.appexport.lib.AppExportParams;
1213
import com.axway.apim.lib.errorHandling.AppException;
@@ -59,4 +60,17 @@ public boolean hasError() {
5960
}
6061

6162
public abstract ClientAppFilter getFilter() throws AppException;
63+
64+
protected Builder getBaseFilterBuilder() throws AppException {
65+
Builder builder = new ClientAppFilter.Builder()
66+
.hasState(params.getAppState())
67+
.hasName(params.getAppName())
68+
.hasId(params.getAppId())
69+
.hasCredential(params.getCredential())
70+
.hasRedirectUrl(params.getRedirectUrl())
71+
.hasOrganizationName(params.getOrgName())
72+
.includeImage(false);
73+
if(params.getCredential()!=null || params.getRedirectUrl()!=null) builder.includeCredentials(true);
74+
return builder;
75+
}
6276
}

modules/app-export/src/main/java/com/axway/apim/appexport/impl/ConsoleAppExporter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,18 @@ private boolean hasAppQuota(ClientApplication app) {
6363

6464
@Override
6565
public ClientAppFilter getFilter() throws AppException {
66-
Builder builder = new ClientAppFilter.Builder()
67-
.hasState(AppExportParams.getInstance().getAppState())
68-
.hasName(AppExportParams.getInstance().getAppName())
69-
.hasId(AppExportParams.getInstance().getAppId())
70-
.hasOrganizationName(AppExportParams.getInstance().getOrgName())
71-
.includeImage(false);
66+
Builder builder = getBaseFilterBuilder();
7267

7368
switch(params.getWide()) {
7469
case standard:
7570
builder.includeQuotas(false);
76-
builder.includeCredentials(false);
71+
if(params.getCredential()==null && params.getRedirectUrl()==null) builder.includeCredentials(false);
7772
builder.includeAPIAccess(false);
7873
break;
7974
case wide:
8075
case ultra:
8176
builder.includeQuotas(true);
82-
builder.includeCredentials(true);
77+
if(params.getCredential()==null && params.getRedirectUrl()==null) builder.includeCredentials(true);
8378
builder.includeAPIAccess(true);
8479
break;
8580
}

modules/app-export/src/main/java/com/axway/apim/appexport/impl/JsonApplicationExporter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ protected void removeApplicationDefaultQuota(ClientApplication app) {
118118

119119
@Override
120120
public ClientAppFilter getFilter() throws AppException {
121-
return new ClientAppFilter.Builder()
122-
.hasState(AppExportParams.getInstance().getAppState())
123-
.hasName(AppExportParams.getInstance().getAppName())
124-
.hasId(AppExportParams.getInstance().getAppId())
125-
.hasOrganizationName(AppExportParams.getInstance().getOrgName())
121+
return getBaseFilterBuilder()
126122
.includeQuotas(true)
127123
.includeCredentials(true)
128124
.includeAPIAccess(true)

modules/app-export/src/main/java/com/axway/apim/appexport/lib/AppExportCLIOptions.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@ public AppExportCLIOptions(String[] args) throws ParseException {
3030

3131
option = new Option("orgName", true, "Filter applications to this organization");
3232
option.setRequired(false);
33-
option.setArgName("Partners");
33+
option.setArgName("*Partners*");
34+
options.addOption(option);
35+
36+
option = new Option("credential", true, "Filter applications having this credential information. Client-ID and API-Key is considered here.");
37+
option.setRequired(false);
38+
option.setArgName("*9877979779*");
39+
options.addOption(option);
40+
41+
option = new Option("redirectUrl", true, "Filter applications having this Redirect-URL. Only OAuth-Credentials are considered.");
42+
option.setRequired(false);
43+
option.setArgName("*localhost*");
3444
options.addOption(option);
3545
}
3646

@@ -49,6 +59,8 @@ public void printUsage(String message, String[] args) {
4959
System.out.println(getBinaryName()+" api get -s api-env -n \"Client App\" -o json");
5060
System.out.println(getBinaryName()+" app get -s api-env -n \"Client App\" -t /tmp/exported_apps -o json -deleteTarget ");
5161
System.out.println(getBinaryName()+" app get -s api-env -n \"App 123\" -t /tmp/exported_apps -o json -deleteTarget");
62+
System.out.println(getBinaryName()+" app get -s api-env -redirectUrl \"localhost\"");
63+
System.out.println(getBinaryName()+" app get -s api-env -credential 16378192");
5264
System.out.println();
5365
System.out.println("For more information and advanced examples please visit:");
5466
System.out.println("https://github.com/Axway-API-Management-Plus/apim-cli/wiki");

modules/app-export/src/main/java/com/axway/apim/appexport/lib/AppExportParams.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@ public String getAppId() {
3232
public String getOrgName() {
3333
return getValue("orgName");
3434
}
35+
36+
public String getCredential() {
37+
return getValue("credential");
38+
}
39+
40+
public String getRedirectUrl() {
41+
return getValue("redirectUrl");
42+
}
3543
}

0 commit comments

Comments
 (0)