Browse files

initial comit of login effort. login currently timing out somewhere w…

…ithin cassandra. further investigation is needed
  • Loading branch information...
1 parent 251308f commit 241ea5de7c1b8e54eca13febc76649e19ff4cb99 @shorrockin shorrockin committed Jul 7, 2010
View
16 src/main/scala/com/shorrockin/cascal/session/Session.scala
@@ -3,7 +3,7 @@ package com.shorrockin.cascal.session
import org.apache.thrift.protocol.TBinaryProtocol
import collection.jcl.Buffer
-import org.apache.cassandra.thrift.{Mutation, Cassandra, NotFoundException, ConsistencyLevel}
+import org.apache.cassandra.thrift.{AuthenticationRequest, Mutation, Cassandra, NotFoundException, ConsistencyLevel}
import java.util.{Map => JMap, List => JList, HashMap, ArrayList}
import collection.jcl.Conversions._
@@ -84,6 +84,18 @@ class Session(val host:Host, val defaultConsistency:Consistency, val framedTrans
*/
lazy val keyspaces:Seq[String] = Buffer(client.get_string_list_property("keyspaces"))
+
+ /**
+ * logs into the specified keyspace using this username and password
+ */
+ def login(ks:Keyspace, user:String, pass:String) {
+ val creds:JMap[String, String] = new HashMap();
+ creds.put(user, pass);
+
+ val auth = new AuthenticationRequest(creds);
+ client.login(ks.value, auth);
+ }
+
/**
* returns the column value for the specified column
@@ -320,4 +332,4 @@ class Session(val host:Host, val defaultConsistency:Consistency, val framedTrans
case t:Throwable => lastError = Some(t) ; throw t
}
-}
+}
View
18 src/main/scala/com/shorrockin/cascal/session/SessionPool.scala
@@ -19,7 +19,8 @@ import com.shorrockin.cascal.model._
*
* @author Chris Shorrock
*/
-class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consistency, framedTransport:Boolean) extends SessionTemplate {
+class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consistency, framedTransport:Boolean, val credentials:Option[LoginCredentials]) extends SessionTemplate {
+ def this(hosts:Seq[Host], params:PoolParams, consistency:Consistency, framedTransport:Boolean) = this(hosts, params, consistency, framedTransport, None)
def this(hosts:Seq[Host], params:PoolParams, consistency:Consistency) = this(hosts, params, consistency, false)
def this(hosts:Seq[Host], params:PoolParams) = this(hosts, params, Consistency.One, false)
@@ -132,6 +133,11 @@ class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consis
log.debug("attempting to create connection to: " + host)
val session = new Session(host.address, host.port, host.timeout, consistency, framedTransport)
session.open
+ if (credentials.isDefined) {
+ val c = credentials.get
+ session.login(c.keyspace, c.username, c.password)
+ }
+
CascalStatistics.creation(host)
session
} catch {
@@ -165,6 +171,8 @@ class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consis
def keyspaces:Seq[String] = borrow { _.keyspaces }
+ def login(ks:Keyspace, user:String, pass:String) = throw new IllegalArgumentException("when using a session template through a session pool, you should specify the login credentials as input to the pool")
+
def get[ResultType](col:Gettable[ResultType], consistency:Consistency):Option[ResultType] = borrow { _.get(col, consistency) }
def get[ResultType](col:Gettable[ResultType]):Option[ResultType] = borrow { _.get(col) }
@@ -236,6 +244,12 @@ object ExhaustionPolicy {
/**
+ * an object required to login to the system
+ */
+case class LoginCredentials(keyspace:Keyspace, username:String, password:String)
+
+
+/**
* this class tempts me to upgrade to 2.8. until then it describes, in the
* most verbose fashion possible, all the parameters that can be passed into
* the session pool.
@@ -272,4 +286,4 @@ case class PoolParams(maxActive:Int,
GenericObjectPool.DEFAULT_TEST_WHILE_IDLE,
GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
true)
-}
+}
View
8 src/main/scala/com/shorrockin/cascal/session/SessionTemplate.scala
@@ -36,6 +36,12 @@ trait SessionTemplate {
/**
+ * logs into the specified keyspace using this username and password
+ */
+ def login(ks:Keyspace, user:String, pass:String)
+
+
+ /**
* returns the column value for the specified column
*/
def get[ResultType](col:Gettable[ResultType], consistency:Consistency):Option[ResultType]
@@ -169,4 +175,4 @@ trait SessionTemplate {
* performs the list of operations in batch using the default consistency
*/
def batch(ops:Seq[Operation]):Unit
-}
+}
View
4 src/main/scala/com/shorrockin/cascal/testing/CassandraTestPool.scala
@@ -40,11 +40,15 @@ object EmbeddedTestCassandra extends Logging {
val fileSep = System.getProperty("file.separator")
val storageFile = new File(homeDirectory, "storage-conf.xml")
+ val accessFile = new File(homeDirectory, "access.properties")
+ val passFile = new File(homeDirectory, "passwd.properties")
val logFile = new File(homeDirectory, "log4j.properties")
replace(copy(resource("/storage-conf.xml"), storageFile), ("%temp-dir%" -> (homeDirectory.getCanonicalPath + fileSep)))
copy(resource("/log4j.properties"), logFile)
+ System.setProperty("access.properties", accessFile.getCanonicalPath)
+ System.setProperty("passwd.properties", passFile.getCanonicalPath)
System.setProperty("storage-config", homeDirectory.getCanonicalPath)
log.debug("creating data file and log location directories")
View
1 src/test/resources/access.properties
@@ -0,0 +1 @@
+AuthTest=cshorrock,other
View
4 src/test/resources/log4j.properties
@@ -5,6 +5,6 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d [%c %t] %m%n
# disable cassandra logging for test cases
-log4j.logger.org.apache.cassandra=DEBUG
-log4j.logger.org.apache.cassandra.service=DEBUG
+log4j.logger.org.apache.cassandra=TRACE
+log4j.logger.org.apache.cassandra.service=TRACE
log4j.logger.org.apache.cassandra.thrift=DEBUG
View
2 src/test/resources/passwd.properties
@@ -0,0 +1,2 @@
+cshorrock=thisisnotmyrealpassword
+other=meep
View
11 src/test/resources/storage-conf.xml
@@ -3,6 +3,14 @@
<ClusterName>Test Cluster</ClusterName>
<Keyspaces>
+ <Keyspace Name="AuthTest">
+ <ColumnFamily Name="Standard" CompareWith="BytesType"/>
+ <ReplicaPlacementStrategy>org.apache.cassandra.locator.RackUnawareStrategy</ReplicaPlacementStrategy>
+ <ReplicationFactor>1</ReplicationFactor>
+ <EndPointSnitch>org.apache.cassandra.locator.EndPointSnitch</EndPointSnitch>
+ <Authenticator>org.apache.cassandra.auth.SimpleAuthenticator</Authenticator>
+ </Keyspace>
+
<Keyspace Name="Test">
<ColumnFamily Name="Super"
ColumnType="Super"
@@ -39,8 +47,9 @@
~ and PropertyFileEndPointSnitch is available in contrib/.
-->
<EndPointSnitch>org.apache.cassandra.locator.EndPointSnitch</EndPointSnitch>
-
</Keyspace>
+
+
</Keyspaces>
<!--
View
36 src/test/scala/com/shorrockin/cascal/TestLogin.scala
@@ -0,0 +1,36 @@
+package com.shorrockin.cascal;
+
+import testing._
+import session._
+import utils.{UUID, Conversions}
+import org.junit.{Assert, Test}
+import com.shorrockin.cascal.utils.Logging
+
+class TestLogin extends Logging {
+ import Conversions._
+ import Assert._
+
+ @Test def testValidLogin {
+ val host = Host("localhost", 9160, 10000)
+ val session = new Session(host, Consistency.One, true)
+
+ // as long as we don't throw an exception - all is good
+ try {
+ log.debug("opening connection to cassandra server")
+ session.open()
+
+ log.debug("executing login request")
+ session.login("AuthTest", "cshorrock", "thisisnotmyrealpassword")
+
+ log.debug("executing simple count request")
+ val count = session.count("AuthTest" \ "Standard" \ UUID())
+ assertEquals(0, count)
+ } finally {
+ session.close()
+ }
+ }
+
+ @Test def tetsNoLogin() {
+
+ }
+}

0 comments on commit 241ea5d

Please sign in to comment.