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"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
_ "github.com/SAP/go-hdb/driver"
|
2020-10-21 22:55:56 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/connutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/credsutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/dbtxn"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
2017-07-07 20:11:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-21 22:55:56 +00:00
|
|
|
hanaTypeName = "hdb"
|
|
|
|
maxIdentifierLength = 127
|
2017-07-07 20:11:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
var _ dbplugin.Database = (*HANA)(nil)
|
2017-12-14 22:03:11 +00:00
|
|
|
|
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
|
2020-10-21 22:55:56 +00:00
|
|
|
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.secretValues)
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
return dbType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func new() *HANA {
|
2017-07-07 20:11:23 +00:00
|
|
|
connProducer := &connutil.SQLConnectionProducer{}
|
|
|
|
connProducer.Type = hanaTypeName
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return &HANA{
|
|
|
|
SQLConnectionProducer: connProducer,
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
func (h *HANA) secretValues() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
h.Password: "[password]",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HANA) Initialize(ctx context.Context, req dbplugin.InitializeRequest) (dbplugin.InitializeResponse, error) {
|
|
|
|
conf, err := h.Init(ctx, req.Config, req.VerifyConnection)
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("error initializing db: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbplugin.InitializeResponse{
|
|
|
|
Config: conf,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
// NewUser generates the username/password on the underlying HANA secret backend
|
2017-07-07 20:11:23 +00:00
|
|
|
// as instructed by the CreationStatement provided.
|
2020-10-21 22:55:56 +00:00
|
|
|
func (h *HANA) NewUser(ctx context.Context, req dbplugin.NewUserRequest) (response dbplugin.NewUserResponse, err error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
// Grab the lock
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
// 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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
if len(req.Statements.Commands) == 0 {
|
|
|
|
return dbplugin.NewUserResponse{}, dbutil.ErrEmptyCreationStatement
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate username
|
2020-10-21 22:55:56 +00:00
|
|
|
username, err := credsutil.GenerateUsername(
|
|
|
|
credsutil.DisplayName(req.UsernameConfig.DisplayName, 32),
|
|
|
|
credsutil.RoleName(req.UsernameConfig.RoleName, 20),
|
|
|
|
credsutil.MaxLength(maxIdentifierLength),
|
|
|
|
credsutil.Separator("_"),
|
|
|
|
credsutil.ToUpper(),
|
|
|
|
)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HANA does not allow hyphens in usernames, and highly prefers capital letters
|
|
|
|
username = strings.Replace(username, "-", "_", -1)
|
|
|
|
username = strings.ToUpper(username)
|
|
|
|
|
|
|
|
// 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
|
2020-10-21 22:55:56 +00:00
|
|
|
expirationStr := req.Expiration.UTC().Format("2006-01-02 15:04:05")
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
// 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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Execute each query
|
2020-10-21 22:55:56 +00:00
|
|
|
for _, stmt := range req.Statements.Commands {
|
2018-03-21 19:05:56 +00:00
|
|
|
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,
|
2020-10-21 22:55:56 +00:00
|
|
|
"password": req.Password,
|
2018-03-21 19:05:56 +00:00
|
|
|
"expiration": expirationStr,
|
|
|
|
}
|
2020-10-21 22:55:56 +00:00
|
|
|
|
2018-04-17 23:31:09 +00:00
|
|
|
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := dbplugin.NewUserResponse{
|
|
|
|
Username: username,
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
return resp, nil
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
// UpdateUser allows for updating the expiration or password of the user mentioned in
|
|
|
|
// the UpdateUserRequest
|
|
|
|
func (h *HANA) UpdateUser(ctx context.Context, req dbplugin.UpdateUserRequest) (dbplugin.UpdateUserResponse, error) {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
// No change requested
|
|
|
|
if req.Password == nil && req.Expiration == nil {
|
|
|
|
return dbplugin.UpdateUserResponse{}, nil
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
if req.Password != nil {
|
|
|
|
err = h.updateUserPassword(ctx, tx, req.Username, req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.UpdateUserResponse{}, err
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
if req.Expiration != nil {
|
|
|
|
err = h.updateUserExpiration(ctx, tx, req.Username, req.Expiration)
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.UpdateUserResponse{}, err
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbplugin.UpdateUserResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HANA) updateUserPassword(ctx context.Context, tx *sql.Tx, username string, req *dbplugin.ChangePassword) error {
|
|
|
|
password := req.NewPassword
|
|
|
|
|
|
|
|
if username == "" || password == "" {
|
|
|
|
return fmt.Errorf("must provide both username and password")
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts := req.Statements.Commands
|
|
|
|
if len(stmts) == 0 {
|
|
|
|
stmts = []string{"ALTER USER {{username}} PASSWORD \"{{password}}\""}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stmt := range stmts {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]string{
|
|
|
|
"name": username,
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute query: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HANA) updateUserExpiration(ctx context.Context, tx *sql.Tx, username string, req *dbplugin.ChangeExpiration) error {
|
|
|
|
// 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 := req.NewExpiration.String()
|
|
|
|
|
|
|
|
if username == "" || expirationStr == "" {
|
|
|
|
return fmt.Errorf("must provide both username and expiration")
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts := req.Statements.Commands
|
|
|
|
if len(stmts) == 0 {
|
|
|
|
stmts = []string{"ALTER USER {{username}} VALID UNTIL '{{expiration}}'"}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stmt := range stmts {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]string{
|
|
|
|
"name": username,
|
|
|
|
"username": username,
|
|
|
|
"expiration": expirationStr,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute query: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoking hana user will deactivate user and try to perform a soft drop
|
2020-10-21 22:55:56 +00:00
|
|
|
func (h *HANA) DeleteUser(ctx context.Context, req dbplugin.DeleteUserRequest) (dbplugin.DeleteUserResponse, error) {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
// default revoke will be a soft drop on user
|
2020-10-21 22:55:56 +00:00
|
|
|
if len(req.Statements.Commands) == 0 {
|
|
|
|
return h.revokeUserDefault(ctx, req)
|
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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Execute each query
|
2020-10-21 22:55:56 +00:00
|
|
|
for _, stmt := range req.Statements.Commands {
|
2018-03-21 19:05:56 +00:00
|
|
|
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{
|
2020-10-21 22:55:56 +00:00
|
|
|
"name": req.Username,
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
2018-04-17 23:31:09 +00:00
|
|
|
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, tx.Commit()
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
func (h *HANA) revokeUserDefault(ctx context.Context, req dbplugin.DeleteUserRequest) (dbplugin.DeleteUserResponse, 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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Disable server login for user
|
2020-10-21 22:55:56 +00:00
|
|
|
disableStmt, err := tx.PrepareContext(ctx, fmt.Sprintf("ALTER USER %s DEACTIVATE USER NOW", req.Username))
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
defer disableStmt.Close()
|
2017-12-15 18:26:17 +00:00
|
|
|
if _, err := disableStmt.ExecContext(ctx); err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidates current sessions and performs soft drop (drop if no dependencies)
|
|
|
|
// if hard drop is desired, custom revoke statements should be written for role
|
2020-10-21 22:55:56 +00:00
|
|
|
dropStmt, err := tx.PrepareContext(ctx, fmt.Sprintf("DROP USER %s RESTRICT", req.Username))
|
2017-07-07 20:11:23 +00:00
|
|
|
if err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
defer dropStmt.Close()
|
2017-12-15 18:26:17 +00:00
|
|
|
if _, err := dropStmt.ExecContext(ctx); err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:55:56 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, nil
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|