cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject [1/2] cassandra git commit: Add auth support to cassandra-stress
Date Mon, 15 Dec 2014 15:51:46 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 3a609c20c -> 088ffb2b5


Add auth support to cassandra-stress

Path by Mike Adamson; reviewed by tjake for CASSANDRA-7985


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/a78451e5
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/a78451e5
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/a78451e5

Branch: refs/heads/trunk
Commit: a78451e5c6c92c815fb5472956b3165965e5d420
Parents: 254d6f7
Author: T Jake Luciani <jake@apache.org>
Authored: Mon Dec 15 10:39:55 2014 -0500
Committer: T Jake Luciani <jake@apache.org>
Committed: Mon Dec 15 10:39:55 2014 -0500

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../cassandra/stress/settings/SettingsMode.java | 63 ++++++++++++++++++--
 .../stress/settings/StressSettings.java         |  6 ++
 .../cassandra/stress/util/JavaDriverClient.java | 17 ++++++
 4 files changed, 83 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/a78451e5/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 07d526c..142d5aa 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.1.3
+ * Add auth support to cassandra-stress (CASSANDRA-7985)
  * Fix ArrayIndexOutOfBoundsException when generating error message
    for some CQL syntax errors (CASSANDRA-8455)
  * Scale memtable slab allocation logarithmically (CASSANDRA-7882)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/a78451e5/tools/stress/src/org/apache/cassandra/stress/settings/SettingsMode.java
----------------------------------------------------------------------
diff --git a/tools/stress/src/org/apache/cassandra/stress/settings/SettingsMode.java b/tools/stress/src/org/apache/cassandra/stress/settings/SettingsMode.java
index 1aa745c..c95ed80 100644
--- a/tools/stress/src/org/apache/cassandra/stress/settings/SettingsMode.java
+++ b/tools/stress/src/org/apache/cassandra/stress/settings/SettingsMode.java
@@ -26,6 +26,8 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
+import com.datastax.driver.core.AuthProvider;
+import com.datastax.driver.core.PlainTextAuthProvider;
 import com.datastax.driver.core.ProtocolOptions;
 
 public class SettingsMode implements Serializable
@@ -34,6 +36,12 @@ public class SettingsMode implements Serializable
     public final ConnectionAPI api;
     public final ConnectionStyle style;
     public final CqlVersion cqlVersion;
+
+    public final String username;
+    public final String password;
+    public final String authProviderClassname;
+    public final AuthProvider authProvider;
+
     private final String compression;
 
     public SettingsMode(GroupedOptions options)
@@ -43,8 +51,37 @@ public class SettingsMode implements Serializable
             cqlVersion = CqlVersion.CQL3;
             Cql3Options opts = (Cql3Options) options;
             api = opts.mode().displayPrefix.equals("native") ? ConnectionAPI.JAVA_DRIVER_NATIVE
: ConnectionAPI.THRIFT;
-            style = opts.usePrepared.setByUser() ? ConnectionStyle.CQL_PREPARED : ConnectionStyle.CQL;
+            style = opts.useUnPrepared.setByUser() ? ConnectionStyle.CQL :  ConnectionStyle.CQL_PREPARED;
             compression = ProtocolOptions.Compression.valueOf(opts.useCompression.value().toUpperCase()).name();
+            username = opts.user.value();
+            password = opts.password.value();
+            authProviderClassname = opts.authProvider.value();
+            if (authProviderClassname != null)
+            {
+                try
+                {
+                    Class<?> clazz = Class.forName(authProviderClassname);
+                    if (!AuthProvider.class.isAssignableFrom(clazz))
+                        throw new IllegalArgumentException(clazz + " is not a valid auth
provider");
+                    // check we can instantiate it
+                    if (PlainTextAuthProvider.class.equals(clazz))
+                    {
+                        authProvider = (AuthProvider) clazz.getConstructor(String.class,
String.class)
+                            .newInstance(username, password);
+                    } else
+                    {
+                        authProvider = (AuthProvider) clazz.newInstance();
+                    }
+                }
+                catch (Exception e)
+                {
+                    throw new IllegalArgumentException("Invalid auth provider class: " +
opts.authProvider.value(), e);
+                }
+            }
+            else
+            {
+                authProvider = null;
+            }
         }
         else if (options instanceof Cql3SimpleNativeOptions)
         {
@@ -53,6 +90,10 @@ public class SettingsMode implements Serializable
             api = ConnectionAPI.SIMPLE_NATIVE;
             style = opts.usePrepared.setByUser() ? ConnectionStyle.CQL_PREPARED : ConnectionStyle.CQL;
             compression = ProtocolOptions.Compression.NONE.name();
+            username = null;
+            password = null;
+            authProvider = null;
+            authProviderClassname = null;
         }
         else if (options instanceof Cql2ThriftOptions)
         {
@@ -61,6 +102,10 @@ public class SettingsMode implements Serializable
             Cql2ThriftOptions opts = (Cql2ThriftOptions) options;
             style = opts.usePrepared.setByUser() ? ConnectionStyle.CQL_PREPARED : ConnectionStyle.CQL;
             compression = ProtocolOptions.Compression.NONE.name();
+            username = null;
+            password = null;
+            authProvider = null;
+            authProviderClassname = null;
         }
         else if (options instanceof ThriftOptions)
         {
@@ -69,6 +114,10 @@ public class SettingsMode implements Serializable
             api = opts.smart.setByUser() ? ConnectionAPI.THRIFT_SMART : ConnectionAPI.THRIFT;
             style = ConnectionStyle.THRIFT;
             compression = ProtocolOptions.Compression.NONE.name();
+            username = opts.user.value();
+            password = opts.password.value();
+            authProviderClassname = null;
+            authProvider = null;
         }
         else
             throw new IllegalStateException();
@@ -102,15 +151,18 @@ public class SettingsMode implements Serializable
     private static abstract class Cql3Options extends GroupedOptions
     {
         final OptionSimple api = new OptionSimple("cql3", "", null, "", true);
-        final OptionSimple usePrepared = new OptionSimple("prepared", "", null, "", false);
+        final OptionSimple useUnPrepared = new OptionSimple("unprepared", "", null, "force
use of unprepared statements", false);
         final OptionSimple useCompression = new OptionSimple("compression=", "none|lz4|snappy",
"none", "", false);
         final OptionSimple port = new OptionSimple("port=", "[0-9]+", "9046", "", false);
+        final OptionSimple user = new OptionSimple("user=", ".+", null, "username", false);
+        final OptionSimple password = new OptionSimple("password=", ".+", null, "password",
false);
+        final OptionSimple authProvider = new OptionSimple("auth-provider=", ".*", null,
"Fully qualified implementation of com.datastax.driver.core.AuthProvider", false);
 
         abstract OptionSimple mode();
         @Override
         public List<? extends Option> options()
         {
-            return Arrays.asList(mode(), usePrepared, api, useCompression, port);
+            return Arrays.asList(mode(), useUnPrepared, api, useCompression, port, user,
password, authProvider);
         }
     }
 
@@ -146,11 +198,14 @@ public class SettingsMode implements Serializable
     {
         final OptionSimple api = new OptionSimple("thrift", "", null, "", true);
         final OptionSimple smart = new OptionSimple("smart", "", null, "", false);
+        final OptionSimple user = new OptionSimple("user=", ".+", null, "username", false);
+        final OptionSimple password = new OptionSimple("password=", ".+", null, "password",
false);
+
 
         @Override
         public List<? extends Option> options()
         {
-            return Arrays.asList(api, smart);
+            return Arrays.asList(api, smart, user, password);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/a78451e5/tools/stress/src/org/apache/cassandra/stress/settings/StressSettings.java
----------------------------------------------------------------------
diff --git a/tools/stress/src/org/apache/cassandra/stress/settings/StressSettings.java b/tools/stress/src/org/apache/cassandra/stress/settings/StressSettings.java
index 219e7cd..6d2f7ea 100644
--- a/tools/stress/src/org/apache/cassandra/stress/settings/StressSettings.java
+++ b/tools/stress/src/org/apache/cassandra/stress/settings/StressSettings.java
@@ -25,11 +25,13 @@ import java.io.Serializable;
 import java.util.*;
 
 import com.datastax.driver.core.Metadata;
+import com.google.common.collect.ImmutableMap;
 import org.apache.cassandra.config.EncryptionOptions;
 import org.apache.cassandra.stress.util.JavaDriverClient;
 import org.apache.cassandra.stress.util.SimpleThriftClient;
 import org.apache.cassandra.stress.util.SmartThriftClient;
 import org.apache.cassandra.stress.util.ThriftClient;
+import org.apache.cassandra.thrift.AuthenticationRequest;
 import org.apache.cassandra.thrift.Cassandra;
 import org.apache.cassandra.thrift.InvalidRequestException;
 import org.apache.cassandra.transport.SimpleClient;
@@ -128,6 +130,10 @@ public class StressSettings implements Serializable
 
             if (setKeyspace)
                 client.set_keyspace(schema.keyspace);
+
+            if (mode.username != null)
+                client.login(new AuthenticationRequest(ImmutableMap.of("username", mode.username,
"password", mode.password)));
+
         }
         catch (InvalidRequestException e)
         {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/a78451e5/tools/stress/src/org/apache/cassandra/stress/util/JavaDriverClient.java
----------------------------------------------------------------------
diff --git a/tools/stress/src/org/apache/cassandra/stress/util/JavaDriverClient.java b/tools/stress/src/org/apache/cassandra/stress/util/JavaDriverClient.java
index 295ed25..7aa7257 100644
--- a/tools/stress/src/org/apache/cassandra/stress/util/JavaDriverClient.java
+++ b/tools/stress/src/org/apache/cassandra/stress/util/JavaDriverClient.java
@@ -41,6 +41,10 @@ public class JavaDriverClient
 
     public final String host;
     public final int port;
+    public final String username;
+    public final String password;
+    public final AuthProvider authProvider;
+
     private final EncryptionOptions.ClientEncryptionOptions encryptionOptions;
     private Cluster cluster;
     private Session session;
@@ -57,6 +61,9 @@ public class JavaDriverClient
     {
         this.host = host;
         this.port = port;
+        this.username = settings.mode.username;
+        this.password = settings.mode.password;
+        this.authProvider = settings.mode.authProvider;
         this.encryptionOptions = encryptionOptions;
         if (settings.node.isWhiteList)
             whitelist = new WhiteListPolicy(new DCAwareRoundRobinPolicy(), settings.node.resolveAll(settings.port.nativePort));
@@ -96,6 +103,16 @@ public class JavaDriverClient
             SSLOptions sslOptions = new SSLOptions(sslContext, encryptionOptions.cipher_suites);
             clusterBuilder.withSSL(sslOptions);
         }
+
+        if (authProvider != null)
+        {
+            clusterBuilder.withAuthProvider(authProvider);
+        }
+        else if (username != null)
+        {
+            clusterBuilder.withCredentials(username, password);
+        }
+
         cluster = clusterBuilder.build();
         Metadata metadata = cluster.getMetadata();
         System.out.printf("Connected to cluster: %s%n",


Mime
View raw message