Skip to content
This repository has been archived by the owner on Jun 12, 2020. It is now read-only.

Commit

Permalink
add support for authentication in PoolOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscm committed Dec 23, 2012
1 parent f42a150 commit bcaa181
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
39 changes: 29 additions & 10 deletions src/gossie/connection.go
Expand Up @@ -47,14 +47,15 @@ type ConnectionPool interface {

// PoolOptions stores the options for the creation of a ConnectionPool
type PoolOptions struct {
Size int // keep up to Size connections open and ready
ReadConsistency int // default read consistency
WriteConsistency int // default write consistency
Timeout int // socket timeout in ms
Recycle int // close connections after Recycle seconds
RecycleJitter int // max jitter to add to Recycle so not all connections close at the same time
Grace int // if a node is blacklisted try to contact it again after Grace seconds
Retries int // retry queries for Retries times before raising an error
Size int // keep up to Size connections open and ready
ReadConsistency int // default read consistency
WriteConsistency int // default write consistency
Timeout int // socket timeout in ms
Recycle int // close connections after Recycle seconds
RecycleJitter int // max jitter to add to Recycle so not all connections close at the same time
Grace int // if a node is blacklisted try to contact it again after Grace seconds
Retries int // retry queries for Retries times before raising an error
Authentication map[string]string // if one or more keys are present, login() is called with the values from Authentication
}

const (
Expand Down Expand Up @@ -280,7 +281,7 @@ func (cp *connectionPool) acquire() (*connection, error) {
cp.releaseEmpty()
return nil, err
}
c, err = newConnection(node, cp.keyspace, cp.options.Timeout)
c, err = newConnection(node, cp.keyspace, cp.options.Timeout, cp.options.Authentication)
if err == ErrorConnectionTimeout {
cp.blacklist(node)
return nil, err
Expand Down Expand Up @@ -351,7 +352,7 @@ type connection struct {
keyspace string
}

func newConnection(node, keyspace string, timeout int) (*connection, error) {
func newConnection(node, keyspace string, timeout int, authentication map[string]string) (*connection, error) {

addr, err := net.ResolveTCPAddr("tcp", node)
if err != nil {
Expand Down Expand Up @@ -410,6 +411,24 @@ func newConnection(node, keyspace string, timeout int) (*connection, error) {
", server reports ", majorVersion))
}

if len(authentication) > 0 {
ar := cassandra.NewAuthenticationRequest()
ar.Credentials = thrift.NewTMap(thrift.STRING, thrift.STRING, 1)
for k, v := range authentication {
ar.Credentials.Set(k, v)
}
autE, auzE, err := c.client.Login(ar)
if autE != nil {
return nil, errors.New("Login error: cannot authenticate with the given credentials")
}
if auzE != nil {
return nil, errors.New("Login error: the given credentials are not authorized to access the server")
}
if err != nil {
return nil, err
}
}

ire, err := c.client.SetKeyspace(keyspace)
if err != nil {
c.close()
Expand Down
44 changes: 42 additions & 2 deletions src/gossie/connection_test.go
Expand Up @@ -16,12 +16,12 @@ func TestConnection(t *testing.T) {
}
*/

c, err := newConnection(localEndpoint, "NotExists", shortTimeout)
c, err := newConnection(localEndpoint, "NotExists", shortTimeout, map[string]string{})
if err == nil {
t.Fatal("Invalid keyspace did not return error")
}

c, err = newConnection(localEndpoint, keyspace, shortTimeout)
c, err = newConnection(localEndpoint, keyspace, shortTimeout, map[string]string{})
if err != nil {
t.Fatal("Error connecting to Cassandra:", err)
}
Expand Down Expand Up @@ -66,6 +66,46 @@ func TestNewConnectionPool(t *testing.T) {
cp.Close()
}

// possible test for users of SimpleAuthenticator
/*
func TestNewConnectionPoolWithAuth(t *testing.T) {
poolOptionsAuth := poolOptions
poolOptionsAuth.Authentication = map[string]string{
"keyspace": "invalid",
"username": "invalid",
"password": "invalid",
}
cp, err := NewConnectionPool(localEndpointPool, "TestGossie", poolOptionsAuth)
if err == nil {
t.Fatal("Invalid keyspace did not return error")
}
poolOptionsAuth.Authentication["keyspace"] = "TestGossie"
cp, err = NewConnectionPool(localEndpointPool, "TestGossie", poolOptionsAuth)
if err == nil {
t.Fatal("Invalid username did not return error")
}
poolOptionsAuth.Authentication["username"] = "test"
cp, err = NewConnectionPool(localEndpointPool, "TestGossie", poolOptionsAuth)
if err == nil {
t.Fatal("Invalid password did not return error")
}
poolOptionsAuth.Authentication["password"] = "testpw"
cp, err = NewConnectionPool(localEndpointPool, "TestGossie", poolOptionsAuth)
if err != nil {
t.Fatal("Correct credetinals did not allow login")
}
if cp.Keyspace() != keyspace {
t.Fatal("Invalid keyspace")
}
cp.Close()
}
*/

func TestAcquireRelease(t *testing.T) {
var err error
var c *connection
Expand Down
2 changes: 1 addition & 1 deletion src/gossie/schema_test.go
Expand Up @@ -6,7 +6,7 @@ import (

func TestSchema(t *testing.T) {

c, err := newConnection(localEndpoint, keyspace, standardTimeout)
c, err := newConnection(localEndpoint, keyspace, standardTimeout, map[string]string{})
if err != nil {
t.Fatal("Error connecting to Cassandra:", err)
}
Expand Down

0 comments on commit bcaa181

Please sign in to comment.