2017-07-07 20:11:23 +00:00
|
|
|
package hana
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-07-07 20:11:23 +00:00
|
|
|
"database/sql"
|
2018-03-21 19:05:56 +00:00
|
|
|
"errors"
|
2017-07-07 20:11:23 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/SAP/go-hdb/driver"
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
|
2018-04-17 23:31:09 +00:00
|
|
|
"github.com/hashicorp/vault/helper/dbtxn"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
2017-07-07 20:11:23 +00:00
|
|
|
"github.com/hashicorp/vault/plugins"
|
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/connutil"
|
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/credsutil"
|
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/dbutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hanaTypeName = "hdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HANA is an implementation of Database interface
|
|
|
|
type HANA struct {
|
2018-03-21 19:05:56 +00:00
|
|
|
*connutil.SQLConnectionProducer
|
2017-07-07 20:11:23 +00:00
|
|
|
credsutil.CredentialsProducer
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
var _ dbplugin.Database = &HANA{}
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
// New implements builtinplugins.BuiltinFactory
|
|
|
|
func New() (interface{}, error) {
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
// Wrap the plugin with middleware to sanitize errors
|
|
|
|
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.SecretValues)
|
|
|
|
|
|
|
|
return dbType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func new() *HANA {
|
2017-07-07 20:11:23 +00:00
|
|
|
connProducer := &connutil.SQLConnectionProducer{}
|
|
|
|
connProducer.Type = hanaTypeName
|
|
|
|
|
|
|
|
credsProducer := &credsutil.SQLCredentialsProducer{
|
|
|
|
DisplayNameLen: 32,
|
|
|
|
RoleNameLen: 20,
|
|
|
|
UsernameLen: 128,
|
|
|
|
Separator: "_",
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return &HANA{
|
|
|
|
SQLConnectionProducer: connProducer,
|
|
|
|
CredentialsProducer: credsProducer,
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run instantiates a HANA object, and runs the RPC server for the plugin
|
|
|
|
func Run(apiTLSConfig *api.TLSConfig) error {
|
|
|
|
dbType, err := New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
plugins.Serve(dbType.(dbplugin.Database), apiTLSConfig)
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the TypeName for this backend
|
|
|
|
func (h *HANA) Type() (string, error) {
|
|
|
|
return hanaTypeName, nil
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (h *HANA) getConnection(ctx context.Context) (*sql.DB, error) {
|
|
|
|
db, err := h.Connection(ctx)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.(*sql.DB), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser generates the username/password on the underlying HANA secret backend
|
|
|
|
// as instructed by the CreationStatement provided.
|
2017-12-14 22:03:11 +00:00
|
|
|
func (h *HANA) CreateUser(ctx context.Context, statements dbplugin.Statements, usernameConfig dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
// Grab the lock
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
// Get the connection
|
2017-12-14 22:03:11 +00:00
|
|
|
db, err := h.getConnection(ctx)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if len(statements.Creation) == 0 {
|
2017-07-07 20:11:23 +00:00
|
|
|
return "", "", dbutil.ErrEmptyCreationStatement
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate username
|
|
|
|
username, err = h.GenerateUsername(usernameConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// HANA does not allow hyphens in usernames, and highly prefers capital letters
|
|
|
|
username = strings.Replace(username, "-", "_", -1)
|
|
|
|
username = strings.ToUpper(username)
|
|
|
|
|
|
|
|
// Generate password
|
|
|
|
password, err = h.GeneratePassword()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
// Most HANA configurations have password constraints
|
|
|
|
// Prefix with A1a to satisfy these constraints. User will be forced to change upon login
|
|
|
|
password = strings.Replace(password, "-", "_", -1)
|
|
|
|
password = "A1a" + password
|
|
|
|
|
|
|
|
// If expiration is in the role SQL, HANA will deactivate the user when time is up,
|
|
|
|
// regardless of whether vault is alive to revoke lease
|
|
|
|
expirationStr, err := h.GenerateExpiration(expiration)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
2017-12-15 18:26:17 +00:00
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Execute each query
|
2018-03-21 19:05:56 +00:00
|
|
|
for _, stmt := range statements.Creation {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:31:09 +00:00
|
|
|
m := map[string]string{
|
2018-03-21 19:05:56 +00:00
|
|
|
"name": username,
|
|
|
|
"password": password,
|
|
|
|
"expiration": expirationStr,
|
|
|
|
}
|
2018-04-17 23:31:09 +00:00
|
|
|
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return username, password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renewing hana user just means altering user's valid until property
|
2017-12-14 22:03:11 +00:00
|
|
|
func (h *HANA) RenewUser(ctx context.Context, statements dbplugin.Statements, username string, expiration time.Time) error {
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
// Get connection
|
2017-12-14 22:03:11 +00:00
|
|
|
db, err := h.getConnection(ctx)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
2017-12-15 18:26:17 +00:00
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// If expiration is in the role SQL, HANA will deactivate the user when time is up,
|
|
|
|
// regardless of whether vault is alive to revoke lease
|
|
|
|
expirationStr, err := h.GenerateExpiration(expiration)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renew user's valid until property field
|
2017-12-15 18:26:17 +00:00
|
|
|
stmt, err := tx.PrepareContext(ctx, "ALTER USER "+username+" VALID UNTIL "+"'"+expirationStr+"'")
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2017-12-15 18:26:17 +00:00
|
|
|
if _, err := stmt.ExecContext(ctx); err != nil {
|
2017-07-07 20:11:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoking hana user will deactivate user and try to perform a soft drop
|
2017-12-14 22:03:11 +00:00
|
|
|
func (h *HANA) RevokeUser(ctx context.Context, statements dbplugin.Statements, username string) error {
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
// default revoke will be a soft drop on user
|
2018-03-21 19:05:56 +00:00
|
|
|
if len(statements.Revocation) == 0 {
|
2017-12-14 22:03:11 +00:00
|
|
|
return h.revokeUserDefault(ctx, username)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get connection
|
2017-12-14 22:03:11 +00:00
|
|
|
db, err := h.getConnection(ctx)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
2017-12-15 18:26:17 +00:00
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Execute each query
|
2018-03-21 19:05:56 +00:00
|
|
|
for _, stmt := range statements.Revocation {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:31:09 +00:00
|
|
|
m := map[string]string{
|
2018-03-21 19:05:56 +00:00
|
|
|
"name": username,
|
|
|
|
}
|
2018-04-17 23:31:09 +00:00
|
|
|
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return tx.Commit()
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (h *HANA) revokeUserDefault(ctx context.Context, username string) error {
|
2017-07-07 20:11:23 +00:00
|
|
|
// Get connection
|
2017-12-14 22:03:11 +00:00
|
|
|
db, err := h.getConnection(ctx)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
2017-12-15 18:26:17 +00:00
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Disable server login for user
|
2017-12-15 18:26:17 +00:00
|
|
|
disableStmt, err := tx.PrepareContext(ctx, fmt.Sprintf("ALTER USER %s DEACTIVATE USER NOW", username))
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer disableStmt.Close()
|
2017-12-15 18:26:17 +00:00
|
|
|
if _, err := disableStmt.ExecContext(ctx); err != nil {
|
2017-07-07 20:11:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidates current sessions and performs soft drop (drop if no dependencies)
|
|
|
|
// if hard drop is desired, custom revoke statements should be written for role
|
2017-12-15 18:26:17 +00:00
|
|
|
dropStmt, err := tx.PrepareContext(ctx, fmt.Sprintf("DROP USER %s RESTRICT", username))
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dropStmt.Close()
|
2017-12-15 18:26:17 +00:00
|
|
|
if _, err := dropStmt.ExecContext(ctx); err != nil {
|
2017-07-07 20:11:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
// RotateRootCredentials is not currently supported on HANA
|
|
|
|
func (h *HANA) RotateRootCredentials(ctx context.Context, statements []string) (map[string]interface{}, error) {
|
|
|
|
return nil, errors.New("root credentaion rotation is not currently implemented in this database secrets engine")
|
|
|
|
}
|