Skip to content

Commit 2c415f2

Browse files
author
rathnapandi
committed
Fix issue #601
1 parent 3911e41 commit 2c415f2

7 files changed

Lines changed: 135 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
# [1.14.17] In progress
88

99
### Added
10+
- New option `-credentialType` for `app get` to export only applications having at least one credential of the given type
11+
([#601](https://github.com/Axway-API-Management-Plus/apim-cli/issues/601)). Supported types are `oauth`, `apikeys` and `extclients`; several
12+
types can be given comma separated, e.g. `apim app get -s api-env -credentialType oauth -o json -t /tmp/exported_apps`. The option can be combined
13+
with `-credential` and `-redirectUrl`, in which case the matching credential must be of the given type.
1014
- `APIImportApp` returns the details of an error ([#594](https://github.com/Axway-API-Management-Plus/apim-cli/issues/594)). Applications, which use
1115
the class to import APIs (e.g. in a service used by a CI/CD pipeline), get the error code, the name of the error, the message and the details of the
1216
underlying causes instead of only the return code: `APIImportApp.importAPIwithDetails(params)` returns a one-line JSON, e.g.
@@ -16,6 +20,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1620
variable is not set.
1721

1822
### Fixed
23+
- Applications with the same name (in the same or in different organizations) were not all exported by `app get`: the second application was
24+
skipped, or with `-deleteTarget` replaced the export of the first one, as the export folder was derived from the application name only
25+
([#600](https://github.com/Axway-API-Management-Plus/apim-cli/issues/600)). Each application is now exported into its own folder; a repeated
26+
name gets a numeric suffix (`My-App`, `My-App_1`, `My-App_2`). This applies to the JSON and YAML export.
1927
- Updating the quota of an API corrupted restrictions that use the same method, e.g. a `throttle` and a `throttlemb` restriction for `*`
2028
([#592](https://github.com/Axway-API-Management-Plus/apim-cli/issues/592)). Restrictions were matched by method only, which turned
2129
one restriction into the other. They are now identified by method, type, period and per. Restrictions of the same type and method with a different

‎modules/apim-adapter/src/main/java/com/axway/apim/adapter/client/apps/ClientAppFilter.java‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.axway.apim.adapter.client.apps;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.LinkedHashSet;
47
import java.util.List;
8+
import java.util.Set;
59
import java.util.regex.Matcher;
610
import java.util.regex.Pattern;
711

@@ -30,6 +34,7 @@ public class ClientAppFilter implements CustomPropertiesFilter {
3034
private static final Logger LOG = LoggerFactory.getLogger(ClientAppFilter.class);
3135
public static final String FIELD = "field";
3236
public static final String VALUE = "value";
37+
public static final List<String> SUPPORTED_CREDENTIAL_TYPES = Collections.unmodifiableList(Arrays.asList("oauth", "apikeys", "extclients"));
3338

3439
boolean includeQuota;
3540

@@ -45,6 +50,11 @@ public class ClientAppFilter implements CustomPropertiesFilter {
4550

4651
private String redirectUrl;
4752

53+
/**
54+
* Credential types (oauth, apikeys, extclients), of which an application must have at least one. Null or empty means no restriction.
55+
*/
56+
private Set<String> credentialTypes;
57+
4858
private String applicationName;
4959

5060
private String state;
@@ -189,6 +199,18 @@ public void setCredential(String credential) {
189199
this.credential = credential;
190200
}
191201

202+
public Set<String> getCredentialTypes() {
203+
return credentialTypes;
204+
}
205+
206+
public void setCredentialTypes(Set<String> credentialTypes) {
207+
this.credentialTypes = credentialTypes;
208+
}
209+
210+
private boolean hasCredentialTypeFilter() {
211+
return credentialTypes != null && !credentialTypes.isEmpty();
212+
}
213+
192214
public String getRedirectUrl() {
193215
return redirectUrl;
194216
}
@@ -249,14 +271,16 @@ public String toString() {
249271
}
250272

251273
public boolean filter(ClientApplication app) {
252-
if (this.getCredential() == null && this.getRedirectUrl() == null && this.getApiName() == null) { // Nothing given to filter out.
274+
if (this.getCredential() == null && this.getRedirectUrl() == null && this.getApiName() == null && !hasCredentialTypeFilter()) { // Nothing given to filter out.
253275
return false;
254276
}
255-
if (this.getCredential() != null || this.getRedirectUrl() != null) {
277+
if (this.getCredential() != null || this.getRedirectUrl() != null || hasCredentialTypeFilter()) {
256278
// Filter this app, as it doesn't have any credentials
257279
if (app.getCredentials() == null) return true;
258280
boolean match = false;
259281
for (ClientAppCredential cred : app.getCredentials()) {
282+
// Ignore credentials of a type, which is not requested
283+
if (hasCredentialTypeFilter() && !credentialTypes.contains(cred.getCredentialType())) continue;
260284
switch (cred.getCredentialType()) {
261285
case "oauth":
262286
match = filterCredential(((OAuth) cred).getClientId(), ((OAuth) cred).getRedirectUrls(), app.getName());
@@ -353,6 +377,8 @@ public static class Builder {
353377

354378
private String redirectUrl;
355379

380+
private Set<String> credentialTypes;
381+
356382
private boolean includeOauthResources;
357383

358384
private String apiName;
@@ -382,6 +408,7 @@ public ClientAppFilter build() {
382408
filter.setIncludeAPIAccess(this.includeAPIAccess);
383409
filter.setCredential(this.credential);
384410
filter.setRedirectUrl(this.redirectUrl);
411+
filter.setCredentialTypes(this.credentialTypes);
385412
filter.setIncludeOauthResources(this.includeOauthResources);
386413
filter.setCustomProperties(this.customProperties);
387414
filter.setApiName(this.apiName);
@@ -455,6 +482,28 @@ public Builder hasRedirectUrl(String redirectUrl) {
455482
return this;
456483
}
457484

485+
/**
486+
* Restricts the applications to those having at least one credential of the given type(s).
487+
*
488+
* @param credentialTypes comma separated list of credential types: oauth, apikeys, extclients (case-insensitive). Null or empty means no restriction.
489+
* @return the builder
490+
* @throws AppException if an unsupported credential type is given
491+
*/
492+
public Builder hasCredentialType(String credentialTypes) throws AppException {
493+
if (StringUtils.isBlank(credentialTypes)) return this;
494+
Set<String> types = new LinkedHashSet<>();
495+
for (String type : credentialTypes.split(",")) {
496+
String normalized = type.trim().toLowerCase();
497+
if (normalized.isEmpty()) continue;
498+
if (!SUPPORTED_CREDENTIAL_TYPES.contains(normalized)) {
499+
throw new AppException("Unsupported credential type: '" + type.trim() + "'. Supported types are: " + String.join(", ", SUPPORTED_CREDENTIAL_TYPES), ErrorCode.INVALID_PARAMETER);
500+
}
501+
types.add(normalized);
502+
}
503+
this.credentialTypes = types.isEmpty() ? null : types;
504+
return this;
505+
}
506+
458507
public Builder includeQuotas(boolean includeQuota) {
459508
this.includeQuota = includeQuota;
460509
return this;

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,61 @@ public void appWithoutCredentialTest() throws IOException {
107107
assertTrue(filter.filter(testApp), "App SHOULD NOT match as there are no credentials");
108108
}
109109

110+
@Test
111+
public void credentialTypeFilterTest() throws IOException, AppException {
112+
ClientApplication mixedApp = getTestApp("client-app-with-two-redirectUrls.json"); // oauth + apikeys
113+
ClientApplication apiKeyOnlyApp = getTestApp("client-app-with-two-api-key-only.json");
114+
ClientApplication noCredentialApp = getTestApp("client-app-with-two-redirectUrls.json");
115+
noCredentialApp.setCredentials(null);
116+
117+
ClientAppFilter oauth = new ClientAppFilter.Builder().hasCredentialType("oauth").build();
118+
assertFalse(oauth.filter(mixedApp), "App with an OAuth credential SHOULD match");
119+
assertTrue(oauth.filter(apiKeyOnlyApp), "App with API-Keys only SHOULD NOT match oauth");
120+
assertTrue(oauth.filter(noCredentialApp), "App without credentials SHOULD NOT match");
121+
122+
ClientAppFilter apiKeys = new ClientAppFilter.Builder().hasCredentialType("apikeys").build();
123+
assertFalse(apiKeys.filter(mixedApp));
124+
assertFalse(apiKeys.filter(apiKeyOnlyApp));
125+
126+
ClientAppFilter extClients = new ClientAppFilter.Builder().hasCredentialType("extclients").build();
127+
assertTrue(extClients.filter(mixedApp), "App without external client credential SHOULD NOT match");
128+
129+
ClientAppFilter multiple = new ClientAppFilter.Builder().hasCredentialType(" OAuth , extclients ").build();
130+
assertFalse(multiple.filter(mixedApp), "Types are case-insensitive and trimmed");
131+
assertTrue(multiple.filter(apiKeyOnlyApp));
132+
133+
ClientAppFilter none = new ClientAppFilter.Builder().hasCredentialType(null).build();
134+
assertFalse(none.filter(apiKeyOnlyApp), "No credential type given means no restriction");
135+
}
136+
137+
@Test
138+
public void credentialTypeCombinedWithCredentialValueTest() throws IOException, AppException {
139+
ClientApplication mixedApp = getTestApp("client-app-with-two-redirectUrls.json");
140+
// The API-Key value belongs to the apikeys credential, not to the OAuth one
141+
ClientAppFilter filter = new ClientAppFilter.Builder()
142+
.hasCredentialType("oauth")
143+
.hasCredential("6cd55c27-675a-444a-9bc7-ae9a7869184d")
144+
.build();
145+
assertTrue(filter.filter(mixedApp), "The matching credential must be of the requested type");
146+
147+
filter = new ClientAppFilter.Builder()
148+
.hasCredentialType("apikeys")
149+
.hasCredential("6cd55c27-675a-444a-9bc7-ae9a7869184d")
150+
.build();
151+
assertFalse(filter.filter(mixedApp));
152+
153+
filter = new ClientAppFilter.Builder()
154+
.hasCredentialType("oauth")
155+
.hasRedirectUrl("*oauthclient:8088*")
156+
.build();
157+
assertFalse(filter.filter(mixedApp));
158+
}
159+
160+
@Test(expectedExceptions = AppException.class)
161+
public void unsupportedCredentialTypeTest() throws AppException {
162+
new ClientAppFilter.Builder().hasCredentialType("oauth,unknown");
163+
}
164+
110165
@Test
111166
public void testAppHavingAPIKeyButNoClientID() throws IOException {
112167
ClientApplication testApp = getTestApp("client-app-with-two-api-key-only.json");

‎modules/apps/src/main/java/com/axway/apim/appexport/impl/ApplicationExporter.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ protected Builder getBaseFilterBuilder() throws AppException {
7575
.hasId(params.getId())
7676
.hasCredential(params.getCredential())
7777
.hasRedirectUrl(params.getRedirectUrl())
78+
.hasCredentialType(params.getCredentialType())
7879
.hasOrganizationName(params.getOrgName())
7980
.hasCreatedByLoginName(params.getCreatedBy())
8081
.includeCustomProperties(getCustomProperties())
8182
.includeAppPermissions(false)
8283
.includeOauthResources(false)
8384
.hasApiName(params.getApiName())
8485
.includeImage(false);
85-
if (params.getCredential() != null || params.getRedirectUrl() != null) builder.includeCredentials(true);
86+
if (params.getCredential() != null || params.getRedirectUrl() != null || params.getCredentialType() != null) builder.includeCredentials(true);
8687
if (params.getApiName() != null) builder.includeAPIAccess(true);
8788
return builder;
8889
}

‎modules/apps/src/main/java/com/axway/apim/appexport/lib/AppExportCLIOptions.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public void addOptions() {
6565
option.setRequired(false);
6666
option.setArgName("*localhost*");
6767
addOption(option);
68+
69+
option = new Option("credentialType", true, "Filter applications having at least one credential of this type. Supported types: oauth, apikeys, extclients. Multiple types can be given comma separated.");
70+
option.setRequired(false);
71+
option.setArgName("oauth");
72+
addOption(option);
6873
}
6974

7075
@Override
@@ -85,6 +90,8 @@ public void printUsage(String message, String[] args) {
8590
Console.println(getBinaryName()+" app get -s api-env -n \"App 123\" -t /tmp/exported_apps -o json -deleteTarget");
8691
Console.println(getBinaryName()+" app get -s api-env -redirectUrl \"localhost\"");
8792
Console.println(getBinaryName()+" app get -s api-env -credential 16378192");
93+
Console.println(getBinaryName()+" app get -s api-env -credentialType oauth -t /tmp/exported_apps -o json -deleteTarget");
94+
Console.println(getBinaryName()+" app get -s api-env -credentialType oauth,apikeys");
8895
Console.println();
8996
Console.println("For more information and advanced examples please visit:");
9097
Console.println("https://github.com/Axway-API-Management-Plus/apim-cli/wiki");
@@ -105,6 +112,7 @@ public Parameters getParams() {
105112
params.setCreatedBy(getValue("createdBy"));
106113
params.setCredential(getValue("credential"));
107114
params.setRedirectUrl(getValue("redirectUrl"));
115+
params.setCredentialType(getValue("credentialType"));
108116
params.setApiName(getValue("api"));
109117
return params;
110118
}

‎modules/apps/src/main/java/com/axway/apim/appexport/lib/AppExportParams.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class AppExportParams extends StandardExportParams implements Parameters
1313
private String createdBy;
1414
private String credential;
1515
private String redirectUrl;
16+
private String credentialType;
1617
private String apiName;
1718

1819
public static synchronized AppExportParams getInstance() {
@@ -67,6 +68,14 @@ public void setCredential(String credential) {
6768
this.credential = credential;
6869
}
6970

71+
public String getCredentialType() {
72+
return credentialType;
73+
}
74+
75+
public void setCredentialType(String credentialType) {
76+
this.credentialType = credentialType;
77+
}
78+
7079
public String getRedirectUrl() {
7180
return redirectUrl;
7281
}

‎modules/apps/src/test/java/com/axway/apim/cli/AppCLIOptionsTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testAppImportParameters() throws AppException {
5454

5555
@Test
5656
public void testExportApplicationParameters() throws AppException {
57-
String[] args = {"-s", "prod", "-n", "*My Great App*", "-id", "UUID-ID-OF-THE-APP", "-state", "pending", "-orgName", "*Partners*", "-createdBy", "Tom", "-credential", "*9877979779*", "-redirectUrl", "*localhost*", "-o", "json", "-wide", "-apimCLIHome", apimCliHome};
57+
String[] args = {"-s", "prod", "-n", "*My Great App*", "-id", "UUID-ID-OF-THE-APP", "-state", "pending", "-orgName", "*Partners*", "-createdBy", "Tom", "-credential", "*9877979779*", "-redirectUrl", "*localhost*", "-credentialType", "oauth,apikeys", "-o", "json", "-wide", "-apimCLIHome", apimCliHome};
5858
CLIOptions options = AppExportCLIOptions.create(args);
5959
AppExportParams params = (AppExportParams) options.getParams();
6060
// Validate core parameters are included
@@ -74,6 +74,7 @@ public void testExportApplicationParameters() throws AppException {
7474
Assert.assertEquals(params.getCreatedBy(), "Tom");
7575
Assert.assertEquals(params.getCredential(), "*9877979779*");
7676
Assert.assertEquals(params.getRedirectUrl(), "*localhost*");
77+
Assert.assertEquals(params.getCredentialType(), "oauth,apikeys");
7778
Assert.assertNotNull(params.getProperties(), "Properties should never be null. They must be created as a base or per stage.");
7879
}
7980

0 commit comments

Comments
 (0)