cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eev...@apache.org
Subject svn commit: r920406 - in /incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra: auth/SimpleAuthenticator.java cli/CliMain.java cli/CliOptions.java cli/CliSessionState.java
Date Mon, 08 Mar 2010 17:10:15 GMT
Author: eevans
Date: Mon Mar  8 17:10:15 2010
New Revision: 920406

URL: http://svn.apache.org/viewvc?rev=920406&view=rev
Log:
add login functionality to cassandra-cli

Patch by Roger Schildmeijer; reviewed by eevans for CASSANDRA-852

Modified:
    incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
    incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
    incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
    incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java

Modified: incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java?rev=920406&r1=920405&r2=920406&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
(original)
+++ incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
Mon Mar  8 17:10:15 2010
@@ -26,7 +26,6 @@
 import java.security.NoSuchAlgorithmException;
 import java.util.Properties;
 
-import org.apache.cassandra.service.*;
 import org.apache.cassandra.thrift.AuthenticationException;
 import org.apache.cassandra.thrift.AuthenticationRequest;
 import org.apache.cassandra.thrift.AuthorizationException;
@@ -34,7 +33,7 @@
 public class SimpleAuthenticator implements IAuthenticator
 {
     public final static String PASSWD_FILENAME_PROPERTY        = "passwd.properties";
-    public final static String AUTHORIZATION_FILENAME_PROPERTY = "authorization.properties";
+    public final static String ACCESS_FILENAME_PROPERTY        = "access.properties";
     public final static String PMODE_PROPERTY                  = "passwd.mode";
     public static final String USERNAME_KEY                    = "username";
     public static final String PASSWORD_KEY                    = "password";
@@ -118,7 +117,7 @@
 
         // if we're here, the authentication succeeded. Now let's see if the user is authorized
for this keyspace.
 
-        String afilename = System.getProperty(AUTHORIZATION_FILENAME_PROPERTY);
+        String afilename = System.getProperty(ACCESS_FILENAME_PROPERTY);
         boolean authorized = false;
         try
         {
@@ -140,11 +139,11 @@
         }
         catch (FileNotFoundException e)
         {
-            throw new RuntimeException("Authorization table file given by property " + AUTHORIZATION_FILENAME_PROPERTY
+ " could not be found: " + e.getMessage());
+            throw new RuntimeException("Authorization table file given by property " + ACCESS_FILENAME_PROPERTY
+ " could not be found: " + e.getMessage());
         }
         catch (IOException e)
         {
-            throw new RuntimeException("Authorization table file given by property " + AUTHORIZATION_FILENAME_PROPERTY
+ " could not be opened: " + e.getMessage());
+            throw new RuntimeException("Authorization table file given by property " + ACCESS_FILENAME_PROPERTY
+ " could not be opened: " + e.getMessage());
         }
         catch (Exception e)
         {

Modified: incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java?rev=920406&r1=920405&r2=920406&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
(original)
+++ incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
Mon Mar  8 17:10:15 2010
@@ -20,8 +20,13 @@
 
 import jline.ConsoleReader;
 import jline.History;
+import org.apache.cassandra.auth.SimpleAuthenticator;
+import org.apache.cassandra.thrift.AuthenticationException;
+import org.apache.cassandra.thrift.AuthenticationRequest;
+import org.apache.cassandra.thrift.AuthorizationException;
 import org.apache.cassandra.thrift.Cassandra;
 import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.thrift.TException;
 import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.transport.TFramedTransport;
 import org.apache.thrift.transport.TSocket;
@@ -29,6 +34,8 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.apache.cassandra.db.Table.SYSTEM_TABLE;
 
@@ -89,7 +96,36 @@
 
         thriftClient_ = cassandraClient;
         cliClient_ = new CliClient(css_, thriftClient_);
-
+        
+        // Authenticate
+        Map<String, String> credentials = new HashMap<String, String>();
+        credentials.put(SimpleAuthenticator.USERNAME_KEY, css_.username);
+        credentials.put(SimpleAuthenticator.PASSWORD_KEY, css_.password);
+        AuthenticationRequest authRequest = new AuthenticationRequest(credentials);
+        try 
+        {
+            thriftClient_.login(css_.keyspace, authRequest);
+        } 
+        catch (AuthenticationException e) 
+        {
+            css_.err.println("Exception during authentication to the cassandra node, " +
+            		"verify you are using correct credentials.");
+            return;
+        } 
+        catch (AuthorizationException e) 
+        {
+            css_.err.println("You are not authorized to use keyspace: " + css_.keyspace);
+            return;
+        } 
+        catch (TException e) 
+        {
+            if (css_.debug)
+                e.printStackTrace();
+            
+            css_.err.println("Login failure. Did you specify 'keyspace', 'username' and 'password'?");
+            return;
+        }
+        
         // Lookup the cluster name, this is to make it clear which cluster the user is connected
to
         String clusterName;
 

Modified: incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java?rev=920406&r1=920405&r2=920406&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
(original)
+++ incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
Mon Mar  8 17:10:15 2010
@@ -34,6 +34,9 @@
     private static final String PORT_OPTION = "port";
     private static final String FRAME_OPTION = "framed";
     private static final String DEBUG_OPTION = "debug";
+    private static final String USERNAME_OPTION = "username";
+    private static final String PASSWORD_OPTION = "password";
+    private static final String KEYSPACE_OPTION = "keyspace";
 
     // Default values for optional command line arguments
     private static final int    DEFAULT_THRIFT_PORT = 9160;
@@ -47,12 +50,15 @@
         options.addOption(PORT_OPTION, true, "cassandra server's thrift port");  
         options.addOption(FRAME_OPTION, false, "cassandra server's framed transport");  
         options.addOption(DEBUG_OPTION, false, "display stack traces");  
+        options.addOption(USERNAME_OPTION, true, "username for cassandra authentication");
+        options.addOption(PASSWORD_OPTION, true, "password for cassandra authentication");
+        options.addOption(KEYSPACE_OPTION, true, "cassandra keyspace user is authenticated
against");
     }
 
     private static void printUsage()
     {
         System.err.println("");
-        System.err.println("Usage: cassandra-cli --host hostname [--port <portname>]
[--framed] [--debug]");
+        System.err.println("Usage: cassandra-cli --host hostname [--port <portname>]
[--framed] [--debug] [--username username] [--password password] [--keyspace keyspace]");
         System.err.println("");
     }
 
@@ -108,5 +114,21 @@
         {
             css.thriftPort = DEFAULT_THRIFT_PORT;
         }
+     
+        // Look for authentication credentials (username and password)
+        if (cmd.hasOption(USERNAME_OPTION)) 
+        {
+        	css.username = cmd.getOptionValue(USERNAME_OPTION);
+        }
+        if (cmd.hasOption(PASSWORD_OPTION))
+        {
+        	css.password = cmd.getOptionValue(PASSWORD_OPTION);
+        }
+        
+        // Look for keyspace
+        if (cmd.hasOption(KEYSPACE_OPTION)) 
+        {
+        	css.keyspace = cmd.getOptionValue(KEYSPACE_OPTION);
+        }
     }
 }

Modified: incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java?rev=920406&r1=920405&r2=920406&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java
(original)
+++ incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java
Mon Mar  8 17:10:15 2010
@@ -31,7 +31,9 @@
     public int     thriftPort;     // cassandra server's thrift port
     public boolean framed = false; // cassandra server's framed transport 
     public boolean debug = false;  // print stack traces when errors occur in the CLI
-
+    public String  username;       // cassandra login name (if SimpleAuthenticator is used)
+    public String  password;       // cassandra login password (if SimpleAuthenticator is
used)
+    public String  keyspace;       // cassandra keyspace user is authenticating 			
     /*
      * Streams to read/write from
      */



Mime
View raw message