You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by lm...@apache.org on 2017/02/05 21:37:20 UTC
[2/7] knox git commit: checkpoint
checkpoint
Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/b5bf07d7
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/b5bf07d7
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/b5bf07d7
Branch: refs/heads/master
Commit: b5bf07d7d8ebee2861054a516d21c32912cd7f82
Parents: d6fda9c
Author: Larry McCay <lm...@hortonworks.com>
Authored: Sun Jan 29 08:49:34 2017 -0500
Committer: Larry McCay <lm...@hortonworks.com>
Committed: Sun Jan 29 08:49:34 2017 -0500
----------------------------------------------------------------------
gateway-shell/pom.xml | 4 +
.../org/apache/hadoop/gateway/shell/Hadoop.java | 110 ++-------
.../org/apache/hadoop/gateway/shell/KnoxSh.java | 246 +++++++++++++++++++
pom.xml | 5 +
4 files changed, 274 insertions(+), 91 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/knox/blob/b5bf07d7/gateway-shell/pom.xml
----------------------------------------------------------------------
diff --git a/gateway-shell/pom.xml b/gateway-shell/pom.xml
index a6f1ea2..f17cfd8 100644
--- a/gateway-shell/pom.xml
+++ b/gateway-shell/pom.xml
@@ -65,6 +65,10 @@
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpcore</artifactId>
+ </dependency>
+ <dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
http://git-wip-us.apache.org/repos/asf/knox/blob/b5bf07d7/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
index b2e186f..6bb6ba2 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
@@ -91,7 +91,7 @@ public class Hadoop implements Closeable {
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
-
+
public static Hadoop login( String url, String username, String password ) throws URISyntaxException {
return new Hadoop(ClientContext.with(username, password, url));
}
@@ -101,29 +101,11 @@ public class Hadoop implements Closeable {
.connection().secure(false).end());
}
-<<<<<<< Updated upstream
public Hadoop( ClientContext clientContext) throws HadoopException, URISyntaxException {
-=======
- public static Hadoop login(String url, Map<String, String> headers) throws URISyntaxException {
- return new Hadoop( url, headers, true );
- }
-
- private Hadoop( String url, Map<String, String> headers ) throws HadoopException, URISyntaxException {
- this(url, null, null, false);
- this.headers = headers;
- }
-
- private Hadoop( String url, String username, String password ) throws HadoopException, URISyntaxException {
- this(url, username, password, false);
- }
-
- private Hadoop( String url, String username, String password, boolean secure ) throws HadoopException, URISyntaxException {
->>>>>>> Stashed changes
this.executor = Executors.newCachedThreadPool();
this.base = clientContext.url();
try {
-<<<<<<< Updated upstream
client = createClient(clientContext);
} catch (GeneralSecurityException e) {
throw new HadoopException("Failed to create HTTP client.", e);
@@ -147,63 +129,6 @@ public class Hadoop implements Closeable {
+ "*******************************************");
}
-=======
- if (!secure) {
- client = createInsecureClient();
- }
- else {
- client = createClient();
- }
- if (username != null && password != null) {
- client.getCredentialsProvider().setCredentials(
- new AuthScope( host.getHostName(), host.getPort() ),
- new UsernamePasswordCredentials( username, password ) );
- AuthCache authCache = new BasicAuthCache();
- BasicScheme authScheme = new BasicScheme();
- authCache.put( host, authScheme );
- context = new BasicHttpContext();
- context.setAttribute( ClientContext.AUTH_CACHE, authCache );
- }
- } catch( GeneralSecurityException e ) {
- throw new HadoopException( "Failed to create HTTP client.", e );
- }
- }
-
- private Hadoop(String url, Map<String,String> headers, boolean secure)
- throws HadoopException, URISyntaxException {
- this.executor = Executors.newCachedThreadPool();
- this.base = url;
- this.headers = headers;
-
- URI uri = new URI( url );
- host = new HttpHost( uri.getHost(), uri.getPort(), uri.getScheme() );
-
- try {
- if (!secure) {
- client = createInsecureClient();
- }
- else {
- client = createClient();
- }
- if (username != null && password != null) {
- client.getCredentialsProvider().setCredentials(
- new AuthScope( host.getHostName(), host.getPort() ),
- new UsernamePasswordCredentials( username, password ) );
- AuthCache authCache = new BasicAuthCache();
- BasicScheme authScheme = new BasicScheme();
- authCache.put( host, authScheme );
- context = new BasicHttpContext();
- context.setAttribute( ClientContext.AUTH_CACHE, authCache );
- }
- } catch( GeneralSecurityException e ) {
- throw new HadoopException( "Failed to create HTTP client.", e );
- }
- }
-
-
- private static DefaultHttpClient createClient() throws GeneralSecurityException {
- SchemeRegistry registry = new SchemeRegistry();
->>>>>>> Stashed changes
KeyStore trustStore = getTrustStore();
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStore, trustStrategy).build();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
@@ -232,22 +157,25 @@ public class Hadoop implements Closeable {
// Auth
URI uri = URI.create(clientContext.url());
host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
-
- CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(
- new AuthScope(host.getHostName(), host.getPort()),
- new UsernamePasswordCredentials(clientContext.username(), clientContext.password()));
-
- AuthCache authCache = new BasicAuthCache();
- BasicScheme authScheme = new BasicScheme();
- authCache.put(host, authScheme);
- context = new BasicHttpContext();
- context.setAttribute(org.apache.http.client.protocol.HttpClientContext.AUTH_CACHE, authCache);
-
+
+ CredentialsProvider credentialsProvider = null;
+ if (clientContext.username() != null && clientContext.password() != null) {
+ credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(
+ new AuthScope(host.getHostName(), host.getPort()),
+ new UsernamePasswordCredentials(clientContext.username(), clientContext.password()));
+
+ AuthCache authCache = new BasicAuthCache();
+ BasicScheme authScheme = new BasicScheme();
+ authCache.put(host, authScheme);
+ context = new BasicHttpContext();
+ context.setAttribute(org.apache.http.client.protocol.HttpClientContext.AUTH_CACHE, authCache);
+ }
return HttpClients.custom()
- .setConnectionManager(connectionManager)
- .setDefaultCredentialsProvider(credentialsProvider)
- .build();
+ .setConnectionManager(connectionManager)
+ .setDefaultCredentialsProvider(credentialsProvider)
+ .build();
+
}
private static KeyStore getTrustStore() throws GeneralSecurityException {
http://git-wip-us.apache.org/repos/asf/knox/blob/b5bf07d7/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
new file mode 100644
index 0000000..088bd6c
--- /dev/null
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
@@ -0,0 +1,246 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway.shell;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.attribute.PosixFilePermission;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.apache.hadoop.gateway.shell.knox.token.Token;
+import org.apache.hadoop.gateway.util.JsonUtils;
+
+/**
+ *
+ */
+public class KnoxSh {
+
+ private static final String USAGE_PREFIX = "KnoxCLI {cmd} [options]";
+ final static private String COMMANDS =
+ " [--help]\n" +
+ " [" + KnoxInit.USAGE + "]\n" +
+ " [" + KnoxDestroy.USAGE + "]\n" +
+ " [" + KnoxList.USAGE + "]\n";
+
+ /** allows stdout to be captured if necessary */
+ public PrintStream out = System.out;
+ /** allows stderr to be captured if necessary */
+ public PrintStream err = System.err;
+
+ private Command command;
+ private String gateway = null;
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.util.Tool#run(java.lang.String[])
+ */
+ public int run(String[] args) throws Exception {
+ int exitCode = 0;
+ try {
+ exitCode = init(args);
+ if (exitCode != 0) {
+ return exitCode;
+ }
+ if (command != null && command.validate()) {
+ command.execute();
+ } else {
+ out.println("ERROR: Invalid Command" + "\n" + "Unrecognized option:" +
+ args[0] + "\n" +
+ "A fatal exception has occurred. Program will exit.");
+ exitCode = -2;
+ }
+ } catch (Exception e) {
+ e.printStackTrace( err );
+ err.flush();
+ return -3;
+ }
+ return exitCode;
+ }
+
+ /**
+ * Parse the command line arguments and initialize the data
+ * <pre>
+ * % knoxcli version
+ * % knoxcli service-test [--u user] [--p password] [--cluster clustername] [--hostname name] [--port port]
+ *
+ * </pre>
+ * @param args
+ * @return
+ * @throws IOException
+ */
+ private int init(String[] args) throws IOException {
+ if (args.length == 0) {
+ printKnoxShellUsage();
+ return -1;
+ }
+ for (int i = 0; i < args.length; i++) { // parse command line
+ if ( args[i].equals("destroy") ) {
+ command = new KnoxDestroy();
+ } else if ( args[i].equals("init") ) {
+ command = new KnoxInit();
+ } else if ( args[i].equals("list") ) {
+ command = new KnoxList();
+ } else if (args[i].equals("--gateway")) {
+ if( i+1 >= args.length || args[i+1].startsWith( "-" ) ) {
+ printKnoxShellUsage();
+ return -1;
+ }
+ this.gateway = args[++i];
+ } else if (args[i].equals("--help")) {
+ printKnoxShellUsage();
+ return -1;
+ } else {
+ printKnoxShellUsage();
+ //ToolRunner.printGenericCommandUsage(System.err);
+ return -1;
+ }
+ }
+ return 0;
+ }
+
+ private void printKnoxShellUsage() {
+ out.println( USAGE_PREFIX + "\n" + COMMANDS );
+ if ( command != null ) {
+ out.println(command.getUsage());
+ } else {
+ char[] chars = new char[79];
+ Arrays.fill( chars, '=' );
+ String div = new String( chars );
+
+ out.println( div );
+ out.println( KnoxInit.USAGE + "\n\n" + KnoxInit.DESC );
+ out.println();
+ out.println( div );
+ out.println(KnoxDestroy.USAGE + "\n\n" + KnoxDestroy.DESC);
+ out.println();
+ out.println( div );
+ out.println(KnoxList.USAGE + "\n\n" + KnoxList.DESC);
+ out.println();
+ out.println( div );
+ }
+ }
+
+ private abstract class Command {
+ public boolean validate() {
+ return true;
+ }
+
+ public abstract void execute() throws Exception;
+
+ public abstract String getUsage();
+ }
+
+ private class KnoxInit extends Command {
+
+ public static final String USAGE = "init";
+ public static final String DESC = "Initializes a Knox token session.";
+
+ @Override
+ public void execute() throws Exception {
+ Credentials credentials = new Credentials();
+ credentials.add("ClearInput", "Enter username: ", "user")
+ .add("HiddenInput", "Enter pas" + "sword: ", "pass");
+ credentials.collect();
+
+ String username = credentials.get("user").string();
+ String pass = credentials.get("pass").string();
+
+ if (gateway == null) {
+ gateway = System.getenv("GATEWAY_HOME");
+ }
+
+ Hadoop session = Hadoop.login(gateway, username, pass);
+
+ String text = Token.get( session ).now().toString();
+ Map<String, String> json = JsonUtils.getMapFromJsonString(text);
+
+ //println "Access Token: " + json.access_token
+ System.out.println("knoxinit successful!");
+ System.out.println("Token Type: " + json.get("token_type"));
+ System.out.println("Expires On: " + new Date(json.get("expires_in")));
+
+ File tokenfile = new File(System.getProperty("user.home"), ".knoxtokencache");
+ FileOutputStream fos = new FileOutputStream(tokenfile);
+ fos.write(text.getBytes("UTF-8"));
+
+ Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
+ fos.close();
+
+ //add owners permission only
+ perms.add(PosixFilePermission.OWNER_READ);
+ perms.add(PosixFilePermission.OWNER_WRITE);
+
+ Files.setPosixFilePermissions(Paths.get(System.getProperty("user.home") + "/.knoxtokencache"), perms);
+
+ session.shutdown();
+ }
+
+ @Override
+ public String getUsage() {
+ return USAGE + ":\n\n" + DESC;
+ }
+
+ }
+
+ private class KnoxDestroy extends Command {
+
+ public static final String USAGE = "version";
+ public static final String DESC = "Displays Knox version information.";
+
+ @Override
+ public void execute() throws Exception {
+ }
+
+ @Override
+ public String getUsage() {
+ return USAGE + ":\n\n" + DESC;
+ }
+
+ }
+
+ private class KnoxList extends Command {
+
+ public static final String USAGE = "version";
+ public static final String DESC = "Displays Knox version information.";
+
+ @Override
+ public void execute() throws Exception {
+ }
+
+ @Override
+ public String getUsage() {
+ return USAGE + ":\n\n" + DESC;
+ }
+
+ }
+
+ /**
+ * @param args
+ * @throws Exception
+ */
+ public static void main(String[] args) throws Exception {
+ KnoxSh sh = new KnoxSh();
+ int res = sh.run(args);
+ System.exit(res);
+ }
+}
http://git-wip-us.apache.org/repos/asf/knox/blob/b5bf07d7/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index c317165..ed49cd8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -790,6 +790,11 @@
<version>4.5.1</version>
</dependency>
<dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpcore</artifactId>
+ <version>4.4.6</version>
+ </dependency>
+ <dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>