Repository: nutch Updated Branches: refs/heads/2.x 7fc92a247 -> 699fda4f4
NUTCH-2284 Basic Authentication support for Nutch 2.X REST API. Project: http://git-wip-us.apache.org/repos/asf/nutch/repo Commit: http://git-wip-us.apache.org/repos/asf/nutch/commit/52ffc5a9 Tree: http://git-wip-us.apache.org/repos/asf/nutch/tree/52ffc5a9 Diff: http://git-wip-us.apache.org/repos/asf/nutch/diff/52ffc5a9 Branch: refs/heads/2.x Commit: 52ffc5a983f261570fd25f10f2f8fcf70d543c88 Parents: 72a99cf Author: Furkan KAMACI <[email protected]> Authored: Sun Jun 19 23:27:15 2016 +0300 Committer: Furkan KAMACI <[email protected]> Committed: Sun Jun 19 23:27:55 2016 +0300 ---------------------------------------------------------------------- conf/nutch-default.xml | 26 ++++++++++++++++++++ src/java/org/apache/nutch/api/NutchServer.java | 27 +++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nutch/blob/52ffc5a9/conf/nutch-default.xml ---------------------------------------------------------------------- diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml index 117737b..f5111f7 100644 --- a/conf/nutch-default.xml +++ b/conf/nutch-default.xml @@ -1435,4 +1435,30 @@ </description> </property> +<property> + <name>restapi.auth</name> + <value>false</value> + <description> + Whether to enable HTTP basic authentication for communicating with RESTAPI. + Use the restapi.auth.username and restapi.auth.auth.password properties to configure + your credentials. + </description> +</property> + +<property> + <name>restapi.auth.username</name> + <value>login</value> + <description> + Username for HTTP basic authentication. restapi.auth should be true to use this property. + </description> +</property> + +<property> + <name>restapi.auth.password</name> + <value>secret1</value> + <description> + Password for HTTP basic authentication. restapi.auth should be true to use this property. + </description> +</property> + </configuration> http://git-wip-us.apache.org/repos/asf/nutch/blob/52ffc5a9/src/java/org/apache/nutch/api/NutchServer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/nutch/api/NutchServer.java b/src/java/org/apache/nutch/api/NutchServer.java index ea316a9..3429beb 100644 --- a/src/java/org/apache/nutch/api/NutchServer.java +++ b/src/java/org/apache/nutch/api/NutchServer.java @@ -45,10 +45,13 @@ import org.apache.nutch.api.resources.JobResource; import org.apache.nutch.api.resources.SeedResource; import org.restlet.Component; import org.restlet.Context; +import org.restlet.data.ChallengeScheme; import org.restlet.data.Protocol; import org.restlet.data.Reference; import org.restlet.ext.jaxrs.JaxRsApplication; import org.restlet.resource.ClientResource; +import org.restlet.security.ChallengeAuthenticator; +import org.restlet.security.MapVerifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -108,8 +111,28 @@ public class NutchServer extends Application { application.setStatusService(new ErrorStatusService()); childContext.getAttributes().put(NUTCH_SERVER, this); - // Attach the application. - component.getDefaultHost().attach(application); + boolean isSecure = configManager.get(ConfigResource.DEFAULT).getBoolean("restapi.auth", false); + + if (!isSecure) { + // Attach the application. + component.getDefaultHost().attach(application); + return; + } + + // Guard the restlet with BASIC authentication. + ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "testRealm"); + // Instantiates a Verifier of identifier/secret couples based on a simple Map. + MapVerifier mapVerifier = new MapVerifier(); + + // Load a single static login/secret pair. + String username = configManager.get(ConfigResource.DEFAULT).get("restapi.auth.username", "login"); + String password = configManager.get(ConfigResource.DEFAULT).get("restapi.auth.password", "secret"); + + mapVerifier.getLocalSecrets().put(username, password.toCharArray()); + guard.setVerifier(mapVerifier); + guard.setNext(application); + + component.getDefaultHost().attach(guard); } @Override
