Skip to content

Commit 9ddf1cb

Browse files
authored
Merge pull request #25 from thc202/rm-api-key-methods
Do not require to set the API key in each call
2 parents de21b05 + 635eac5 commit 9ddf1cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+4873
-1601
lines changed

subprojects/zap-clientapi-ant/src/main/java/org/zaproxy/clientapi/ant/ActiveScanSubtreeTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ActiveScanSubtreeTask extends ZapTask {
2828
@Override
2929
public void execute() throws BuildException {
3030
try {
31-
this.getClientApi().ascan.scan(null, url, "true", "false", "", "", "");
31+
this.getClientApi().ascan.scan(url, "true", "false", "", "", "");
3232

3333
} catch (Exception e) {
3434
throw new BuildException(e);

subprojects/zap-clientapi-ant/src/main/java/org/zaproxy/clientapi/ant/ActiveScanUrlTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ActiveScanUrlTask extends ZapTask {
2828
@Override
2929
public void execute() throws BuildException {
3030
try {
31-
this.getClientApi().ascan.scan(null, url, "false", "false", "", "", "");
31+
this.getClientApi().ascan.scan(url, "false", "false", "", "", "");
3232

3333
} catch (Exception e) {
3434
throw new BuildException(e);

subprojects/zap-clientapi-ant/src/main/java/org/zaproxy/clientapi/ant/LoadSessionTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class LoadSessionTask extends ZapTask {
2828
@Override
2929
public void execute() throws BuildException {
3030
try {
31-
this.getClientApi().core.loadSession(null, name);
31+
this.getClientApi().core.loadSession(name);
3232

3333
} catch (Exception e) {
3434
throw new BuildException(e);

subprojects/zap-clientapi-ant/src/main/java/org/zaproxy/clientapi/ant/NewSessionTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class NewSessionTask extends ZapTask {
2828
@Override
2929
public void execute() throws BuildException {
3030
try {
31-
this.getClientApi().core.newSession(null, name, "true");
31+
this.getClientApi().core.newSession(name, "true");
3232

3333
} catch (Exception e) {
3434
throw new BuildException(e);

subprojects/zap-clientapi-ant/src/main/java/org/zaproxy/clientapi/ant/SaveSessionTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class SaveSessionTask extends ZapTask {
2828
@Override
2929
public void execute() throws BuildException {
3030
try {
31-
this.getClientApi().core.saveSession(null, name, "true");
31+
this.getClientApi().core.saveSession(name, "true");
3232

3333
} catch (Exception e) {
3434
throw new BuildException(e);

subprojects/zap-clientapi-ant/src/main/java/org/zaproxy/clientapi/ant/SpiderUrlTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class SpiderUrlTask extends ZapTask {
2828
@Override
2929
public void execute() throws BuildException {
3030
try {
31-
this.getClientApi().spider.scan(null, url, "", "", null, null);
31+
this.getClientApi().spider.scan(url, "", "", null, null);
3232

3333
} catch (Exception e) {
3434
throw new BuildException(e);

subprojects/zap-clientapi-ant/src/main/java/org/zaproxy/clientapi/ant/StopZapTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class StopZapTask extends ZapTask {
2626
@Override
2727
public void execute() throws BuildException {
2828
try {
29-
this.getClientApi().core.shutdown(null);
29+
this.getClientApi().core.shutdown();
3030
} catch (Exception e) {
3131
throw new BuildException(e);
3232
}

subprojects/zap-clientapi/src/examples/java/org/zaproxy/clientapi/examples/SimpleExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static void main(String[] args) {
4343
// Start spidering the target
4444
System.out.println("Spider : " + TARGET);
4545
// It's not necessary to pass the ZAP API key again, already set when creating the ClientApi.
46-
ApiResponse resp = api.spider.scan(null, TARGET, null, null, null, null);
46+
ApiResponse resp = api.spider.scan(TARGET, null, null, null, null);
4747
String scanid;
4848
int progress;
4949

@@ -65,7 +65,7 @@ public static void main(String[] args) {
6565
Thread.sleep(2000);
6666

6767
System.out.println("Active scan : " + TARGET);
68-
resp = api.ascan.scan(null, TARGET, "True", "False", null, null, null);
68+
resp = api.ascan.scan(TARGET, "True", "False", null, null, null);
6969

7070
// The scan now returns a scan id to support concurrent scanning
7171
scanid = ((ApiResponseElement) resp).getValue();
@@ -82,7 +82,7 @@ public static void main(String[] args) {
8282
System.out.println("Active Scan complete");
8383

8484
System.out.println("Alerts:");
85-
System.out.println(new String(api.core.xmlreport(null)));
85+
System.out.println(new String(api.core.xmlreport()));
8686

8787
} catch (Exception e) {
8888
System.out.println("Exception : " + e.getMessage());

subprojects/zap-clientapi/src/examples/java/org/zaproxy/clientapi/examples/authentication/FormBasedAuthentication.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static void setLoggedInIndicator(ClientApi clientApi) throws ClientApiEx
108108
String contextId = "1";
109109

110110
// Actually set the logged in indicator
111-
clientApi.authentication.setLoggedInIndicator(ZAP_API_KEY, contextId, java.util.regex.Pattern.quote(loggedInIndicator));
111+
clientApi.authentication.setLoggedInIndicator(contextId, java.util.regex.Pattern.quote(loggedInIndicator));
112112

113113
// Check out the logged in indicator that is set
114114
System.out.println("Configured logged in indicator regex: "
@@ -130,7 +130,7 @@ private static void setFormBasedAuthenticationForBodgeit(ClientApi clientApi) th
130130

131131
System.out.println("Setting form based authentication configuration as: "
132132
+ formBasedConfig.toString());
133-
clientApi.authentication.setAuthenticationMethod(ZAP_API_KEY, contextId, "formBasedAuthentication",
133+
clientApi.authentication.setAuthenticationMethod(contextId, "formBasedAuthentication",
134134
formBasedConfig.toString());
135135

136136
// Check if everything is set up ok
@@ -146,7 +146,7 @@ private static void setUserAuthConfigForBodgeit(ClientApi clientApi) throws Clie
146146
String password = "weakPassword";
147147

148148
// Make sure we have at least one user
149-
String userId = extractUserId(clientApi.users.newUser(ZAP_API_KEY, contextId, user));
149+
String userId = extractUserId(clientApi.users.newUser(contextId, user));
150150

151151
// Prepare the configuration in a format similar to how URL parameters are formed. This
152152
// means that any value we add for the configuration values has to be URL encoded.
@@ -155,7 +155,7 @@ private static void setUserAuthConfigForBodgeit(ClientApi clientApi) throws Clie
155155
userAuthConfig.append("&password=").append(URLEncoder.encode(password, "UTF-8"));
156156

157157
System.out.println("Setting user authentication configuration as: " + userAuthConfig.toString());
158-
clientApi.users.setAuthenticationCredentials(ZAP_API_KEY, contextId, userId, userAuthConfig.toString());
158+
clientApi.users.setAuthenticationCredentials(contextId, userId, userAuthConfig.toString());
159159

160160
// Check if everything is set up ok
161161
System.out.println("Authentication config: " + clientApi.users.getUserById(contextId, userId).toString(0));
@@ -172,7 +172,7 @@ private static String extractUserId(ApiResponse response) {
172172
* @throws Exception if an error occurred while accessing the API
173173
*/
174174
public static void main(String[] args) throws Exception {
175-
ClientApi clientApi = new ClientApi(ZAP_ADDRESS, ZAP_PORT);
175+
ClientApi clientApi = new ClientApi(ZAP_ADDRESS, ZAP_PORT, ZAP_API_KEY);
176176

177177
listAuthInformation(clientApi);
178178
System.out.println("-------------");

subprojects/zap-clientapi/src/main/java/org/zaproxy/clientapi/core/ClientApi.java

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,48 @@ private static String encodeQueryParam(String param) {
416416
return param;
417417
}
418418

419+
/**
420+
* Adds the given regular expression to the exclusion list of the given context.
421+
*
422+
* @param apikey the API key, might be {@code null}.
423+
* @param contextName the name of the context.
424+
* @param regex the regular expression to add.
425+
* @throws Exception if an error occurred while calling the API.
426+
* @deprecated (TODO add version) Use {@link Context#excludeFromContext(String, String)} instead.
427+
* @see #context
428+
*/
429+
@Deprecated
419430
public void addExcludeFromContext(String apikey, String contextName, String regex) throws Exception {
420431
context.excludeFromContext(apikey, contextName, regex);
421432
}
422433

434+
/**
435+
* Adds the given regular expression to the inclusion list of the given context.
436+
*
437+
* @param apikey the API key, might be {@code null}.
438+
* @param contextName the name of the context.
439+
* @param regex the regular expression to add.
440+
* @throws Exception if an error occurred while calling the API.
441+
* @deprecated (TODO add version) Use {@link Context#includeInContext(String, String)} instead.
442+
* @see #context
443+
*/
444+
@Deprecated
423445
public void addIncludeInContext(String apikey, String contextName, String regex) throws Exception {
424446
context.includeInContext(apikey, contextName, regex);
425447
}
426448

449+
/**
450+
* Includes just one of the nodes that match the given regular expression in the context with the given name.
451+
* <p>
452+
* Nodes that do not match the regular expression are excluded.
453+
*
454+
* @param apikey the API key, might be {@code null}.
455+
* @param contextName the name of the context.
456+
* @param regex the regular expression to match the node/URL.
457+
* @throws Exception if an error occurred while calling the API.
458+
* @deprecated (TODO add version) Use {@link #includeOneMatchingNodeInContext(String, String)} instead.
459+
*/
460+
@Deprecated
427461
public void includeOneMatchingNodeInContext(String apikey, String contextName, String regex) throws Exception {
428462
List<String> sessionUrls = getSessionUrls();
429463
boolean foundOneMatch = false;
@@ -442,6 +476,32 @@ public void includeOneMatchingNodeInContext(String apikey, String contextName, S
442476

443477
}
444478

479+
/**
480+
* Includes just one of the nodes that match the given regular expression in the context with the given name.
481+
* <p>
482+
* Nodes that do not match the regular expression are excluded.
483+
*
484+
* @param contextName the name of the context.
485+
* @param regex the regular expression to match the node/URL.
486+
* @throws Exception if an error occurred while calling the API.
487+
*/
488+
public void includeOneMatchingNodeInContext(String contextName, String regex) throws Exception {
489+
List<String> sessionUrls = getSessionUrls();
490+
boolean foundOneMatch = false;
491+
for (String sessionUrl : sessionUrls) {
492+
if (sessionUrl.matches(regex)) {
493+
if (foundOneMatch) {
494+
context.excludeFromContext(contextName, regex);
495+
} else {
496+
foundOneMatch = true;
497+
}
498+
}
499+
}
500+
if (!foundOneMatch) {
501+
throw new Exception("Unexpected result: No url found in site tree matching regex " + regex);
502+
}
503+
}
504+
445505
private List<String> getSessionUrls() throws Exception {
446506
List<String> sessionUrls = new ArrayList<>();
447507
ApiResponse response = core.urls();
@@ -456,15 +516,45 @@ private List<String> getSessionUrls() throws Exception {
456516
return sessionUrls;
457517
}
458518

519+
/**
520+
* Active scans the given site, that's in scope.
521+
* <p>
522+
* The method returns only after the scan has finished.
523+
*
524+
* @param apikey the API key, might be {@code null}.
525+
* @param url the site to scan
526+
* @throws Exception if an error occurred while calling the API.
527+
* @deprecated (TODO add version) Use {@link #activeScanSiteInScope(String)} instead, the API key should be set using one of
528+
* the {@code ClientApi} constructors.
529+
*/
530+
@Deprecated
459531
public void activeScanSiteInScope(String apikey, String url) throws Exception {
460532
ascan.scan(apikey, url, "true", "true", "", "", "");
533+
waitForAScanToFinish(url);
534+
}
535+
536+
/**
537+
* Active scans the given site, that's in scope.
538+
* <p>
539+
* The method returns only after the scan has finished.
540+
*
541+
* @param url the site to scan
542+
* @throws Exception if an error occurred while calling the API.
543+
* @since TODO add version
544+
*/
545+
public void activeScanSiteInScope(String url) throws Exception {
546+
ascan.scan(url, "true", "true", "", "", "");
547+
waitForAScanToFinish(url);
548+
}
549+
550+
private void waitForAScanToFinish(String targetUrl) throws ClientApiException {
461551
// Poll until spider finished
462552
int status = 0;
463553
while ( status < 100) {
464554
status = statusToInt(ascan.status(""));
465555
if(debug){
466556
String format = "Scanning %s Progress: %d%%";
467-
System.out.println(String.format(format, url, status));
557+
System.out.println(String.format(format, targetUrl, status));
468558
}try {
469559
Thread.sleep(1000);
470560
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)