2017-04-23 01:02:57 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-04-23 01:02:57 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gocql/gocql"
|
2017-05-01 15:27:35 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2017-05-02 21:40:11 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin"
|
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/credsutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
2017-04-23 01:02:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-03-21 19:05:56 +00:00
|
|
|
defaultUserCreationCQL = `CREATE USER '{{username}}' WITH PASSWORD '{{password}}' NOSUPERUSER;`
|
|
|
|
defaultUserDeletionCQL = `DROP USER '{{username}}';`
|
|
|
|
defaultRootCredentialRotationCQL = `ALTER USER {{username}} WITH PASSWORD '{{password}}';`
|
|
|
|
cassandraTypeName = "cassandra"
|
2017-04-23 01:02:57 +00:00
|
|
|
)
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
var _ dbplugin.Database = &Cassandra{}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
// Cassandra is an implementation of Database interface
|
2017-04-23 01:02:57 +00:00
|
|
|
type Cassandra struct {
|
2018-03-21 19:05:56 +00:00
|
|
|
*cassandraConnectionProducer
|
2017-04-23 01:02:57 +00:00
|
|
|
credsutil.CredentialsProducer
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
// New returns a new Cassandra instance
|
2017-04-27 15:07:52 +00:00
|
|
|
func New() (interface{}, error) {
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.secretValues)
|
|
|
|
|
|
|
|
return dbType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func new() *Cassandra {
|
2017-05-11 21:38:54 +00:00
|
|
|
connProducer := &cassandraConnectionProducer{}
|
2017-04-23 01:02:57 +00:00
|
|
|
connProducer.Type = cassandraTypeName
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
credsProducer := &credsutil.SQLCredentialsProducer{
|
|
|
|
DisplayNameLen: 15,
|
|
|
|
RoleNameLen: 15,
|
|
|
|
UsernameLen: 100,
|
|
|
|
Separator: "_",
|
|
|
|
}
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return &Cassandra{
|
|
|
|
cassandraConnectionProducer: connProducer,
|
|
|
|
CredentialsProducer: credsProducer,
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
// Run instantiates a Cassandra object, and runs the RPC server for the plugin
|
2017-05-02 21:40:11 +00:00
|
|
|
func Run(apiTLSConfig *api.TLSConfig) error {
|
2017-04-27 15:07:52 +00:00
|
|
|
dbType, err := New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2019-04-15 18:10:07 +00:00
|
|
|
dbplugin.Serve(dbType.(dbplugin.Database), api.VaultPluginTLSProvider(apiTLSConfig))
|
2017-04-23 01:02:57 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
// Type returns the TypeName for this backend
|
2017-04-23 01:02:57 +00:00
|
|
|
func (c *Cassandra) Type() (string, error) {
|
|
|
|
return cassandraTypeName, nil
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (c *Cassandra) getConnection(ctx context.Context) (*gocql.Session, error) {
|
|
|
|
session, err := c.Connection(ctx)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.(*gocql.Session), nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
// CreateUser generates the username/password on the underlying Cassandra secret backend as instructed by
|
|
|
|
// the CreationStatement provided.
|
2017-12-14 22:03:11 +00:00
|
|
|
func (c *Cassandra) CreateUser(ctx context.Context, statements dbplugin.Statements, usernameConfig dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
|
2017-04-23 01:02:57 +00:00
|
|
|
// Grab the lock
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-04-23 01:02:57 +00:00
|
|
|
// Get the connection
|
2017-12-14 22:03:11 +00:00
|
|
|
session, err := c.getConnection(ctx)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
creationCQL := statements.Creation
|
|
|
|
if len(creationCQL) == 0 {
|
|
|
|
creationCQL = []string{defaultUserCreationCQL}
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
rollbackCQL := statements.Rollback
|
|
|
|
if len(rollbackCQL) == 0 {
|
|
|
|
rollbackCQL = []string{defaultUserDeletionCQL}
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
username, err = c.GenerateUsername(usernameConfig)
|
2017-08-03 11:34:53 +00:00
|
|
|
username = strings.Replace(username, "-", "_", -1)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2017-06-06 13:49:49 +00:00
|
|
|
// Cassandra doesn't like the uppercase usernames
|
|
|
|
username = strings.ToLower(username)
|
2017-04-23 01:02:57 +00:00
|
|
|
|
|
|
|
password, err = c.GeneratePassword()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute each query
|
2018-03-21 19:05:56 +00:00
|
|
|
for _, stmt := range creationCQL {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
err = session.Query(dbutil.QueryHelper(query, map[string]string{
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
2019-06-21 21:04:50 +00:00
|
|
|
})).WithContext(ctx).Exec()
|
2018-03-21 19:05:56 +00:00
|
|
|
if err != nil {
|
|
|
|
for _, stmt := range rollbackCQL {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
session.Query(dbutil.QueryHelper(query, map[string]string{
|
|
|
|
"username": username,
|
2019-06-21 21:04:50 +00:00
|
|
|
})).WithContext(ctx).Exec()
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
return "", "", err
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return username, password, nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
// RenewUser is not supported on Cassandra, so this is a no-op.
|
2017-12-14 22:03:11 +00:00
|
|
|
func (c *Cassandra) RenewUser(ctx context.Context, statements dbplugin.Statements, username string, expiration time.Time) error {
|
2017-04-23 01:02:57 +00:00
|
|
|
// NOOP
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
// RevokeUser attempts to drop the specified user.
|
2017-12-14 22:03:11 +00:00
|
|
|
func (c *Cassandra) RevokeUser(ctx context.Context, statements dbplugin.Statements, username string) error {
|
2017-04-23 01:02:57 +00:00
|
|
|
// Grab the lock
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
session, err := c.getConnection(ctx)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
revocationCQL := statements.Revocation
|
|
|
|
if len(revocationCQL) == 0 {
|
|
|
|
revocationCQL = []string{defaultUserDeletionCQL}
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:27:35 +00:00
|
|
|
var result *multierror.Error
|
2018-03-21 19:05:56 +00:00
|
|
|
for _, stmt := range revocationCQL {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2017-05-01 15:27:35 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
err := session.Query(dbutil.QueryHelper(query, map[string]string{
|
|
|
|
"username": username,
|
2019-06-21 21:04:50 +00:00
|
|
|
})).WithContext(ctx).Exec()
|
2017-05-01 15:27:35 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
2017-05-01 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.ErrorOrNil()
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
func (c *Cassandra) RotateRootCredentials(ctx context.Context, statements []string) (map[string]interface{}, error) {
|
|
|
|
// Grab the lock
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
session, err := c.getConnection(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rotateCQL := statements
|
|
|
|
if len(rotateCQL) == 0 {
|
|
|
|
rotateCQL = []string{defaultRootCredentialRotationCQL}
|
|
|
|
}
|
|
|
|
|
|
|
|
password, err := c.GeneratePassword()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *multierror.Error
|
|
|
|
for _, stmt := range rotateCQL {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := session.Query(dbutil.QueryHelper(query, map[string]string{
|
|
|
|
"username": c.Username,
|
|
|
|
"password": password,
|
2019-06-21 21:04:50 +00:00
|
|
|
})).WithContext(ctx).Exec()
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
result = multierror.Append(result, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = result.ErrorOrNil()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.rawConfig["password"] = password
|
|
|
|
return c.rawConfig, nil
|
|
|
|
}
|