mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-16 03:27:01 +02:00
* Add priority queue to sdk * fix issue of storing pointers and now copy * update to use copy structure * Remove file, put Item struct def. into other file * add link * clean up docs * refactor internal data structure to hide heap method implementations. Other cleanup after feedback * rename PushItem and PopItem to just Push/Pop, after encapsulating the heap methods * updates after feedback * refactoring/renaming * guard against pushing a nil item * minor updates after feedback * Add SetCredentials, GenerateCredentials gRPC methods to combined database backend gPRC * Initial Combined database backend implementation of static accounts and automatic rotation * vendor updates * initial implementation of static accounts with Combined database backend, starting with PostgreSQL implementation * add lock and setup of rotation queue * vendor the queue * rebase on new method signature of queue * remove mongo tests for now * update default role sql * gofmt after rebase * cleanup after rebasing to remove checks for ErrNotFound error * rebase cdcr-priority-queue * vendor dependencies with 'go mod vendor' * website database docs for Static Role support * document the rotate-role API endpoint * postgres specific static role docs * use constants for paths * updates from review * remove dead code * combine and clarify error message for older plugins * Update builtin/logical/database/backend.go Co-Authored-By: Jim Kalafut <jim@kalafut.net> * cleanups from feedback * code and comment cleanups * move db.RLock higher to protect db.GenerateCredentials call * Return output with WALID if we failed to delete the WAL * Update builtin/logical/database/path_creds_create.go Co-Authored-By: Jim Kalafut <jim@kalafut.net> * updates after running 'make fmt' * update after running 'make proto' * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * update comment and remove and rearrange some dead code * Update website/source/api/secret/databases/index.html.md Co-Authored-By: Jim Kalafut <jim@kalafut.net> * cleanups after review * Update sdk/database/dbplugin/grpc_transport.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * code cleanup after feedback * remove PasswordLastSet; it's not used * document GenerateCredentials and SetCredentials * Update builtin/logical/database/path_rotate_credentials.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * wrap pop and popbykey in backend methods to protect against nil cred rotation queue * use strings.HasPrefix instead of direct equality check for path * Forgot to commit this * updates after feedback * re-purpose an outdated test to now check that static and dynamic roles cannot share a name * check for unique name across dynamic and static roles * refactor loadStaticWALs to return a map of name/setCredentialsWAL struct to consolidate where we're calling set credentials * remove commented out code * refactor to have loadstaticwals filter out wals for roles that no longer exist * return error if nil input given * add nil check for input into setStaticAccount * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * add constant for queue tick time in seconds, used for comparrison in updates * Update builtin/logical/database/path_roles.go Co-Authored-By: Jim Kalafut <jim@kalafut.net> * code cleanup after review * remove misplaced code comment * remove commented out code * create a queue in the Factory method, even if it's never used * update path_roles to use a common set of fields, with specific overrides for dynamic/static roles by type * document new method * move rotation things into a specific file * rename test file and consolidate some static account tests * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * update code comments, method names, and move more methods into rotation.go * update comments to be capitalized * remove the item from the queue before we try to destroy it * findStaticWAL returns an error * use lowercase keys when encoding WAL entries * small cleanups * remove vestigial static account check * remove redundant DeleteWAL call in populate queue * if we error on loading role, push back to queue with 10 second backoff * poll in initqueue to make sure the backend is setup and can write/delete data * add revoke_user_on_delete flag to allow users to opt-in to revoking the static database user on delete of the Vault role. Default false * add code comments on read-only loop * code comment updates * re-push if error returned from find static wal * add locksutil and acquire locks when pop'ing from the queue * grab exclusive locks for updating static roles * Add SetCredentials and GenerateCredentials stubs to mockPlugin * add a switch in initQueue to listen for cancelation * remove guard on zero time, it should have no affect * create a new context in Factory to pass on and use for closing the backend queue * restore master copy of vendor dir
254 lines
6.4 KiB
Go
254 lines
6.4 KiB
Go
package influxdb
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
"github.com/hashicorp/vault/api"
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin"
|
|
"github.com/hashicorp/vault/sdk/database/helper/credsutil"
|
|
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
|
influx "github.com/influxdata/influxdb/client/v2"
|
|
)
|
|
|
|
const (
|
|
defaultUserCreationIFQL = `CREATE USER "{{username}}" WITH PASSWORD '{{password}}';`
|
|
defaultUserDeletionIFQL = `DROP USER "{{username}}";`
|
|
defaultRootCredentialRotationIFQL = `SET PASSWORD FOR "{{username}}" = '{{password}}';`
|
|
influxdbTypeName = "influxdb"
|
|
)
|
|
|
|
var _ dbplugin.Database = &Influxdb{}
|
|
|
|
// Influxdb is an implementation of Database interface
|
|
type Influxdb struct {
|
|
*influxdbConnectionProducer
|
|
credsutil.CredentialsProducer
|
|
}
|
|
|
|
// New returns a new Cassandra instance
|
|
func New() (interface{}, error) {
|
|
db := new()
|
|
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.secretValues)
|
|
|
|
return dbType, nil
|
|
}
|
|
|
|
func new() *Influxdb {
|
|
connProducer := &influxdbConnectionProducer{}
|
|
connProducer.Type = influxdbTypeName
|
|
|
|
credsProducer := &credsutil.SQLCredentialsProducer{
|
|
DisplayNameLen: 15,
|
|
RoleNameLen: 15,
|
|
UsernameLen: 100,
|
|
Separator: "_",
|
|
}
|
|
|
|
return &Influxdb{
|
|
influxdbConnectionProducer: connProducer,
|
|
CredentialsProducer: credsProducer,
|
|
}
|
|
}
|
|
|
|
// Run instantiates a Influxdb object, and runs the RPC server for the plugin
|
|
func Run(apiTLSConfig *api.TLSConfig) error {
|
|
dbType, err := New()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dbplugin.Serve(dbType.(dbplugin.Database), api.VaultPluginTLSProvider(apiTLSConfig))
|
|
|
|
return nil
|
|
}
|
|
|
|
// Type returns the TypeName for this backend
|
|
func (i *Influxdb) Type() (string, error) {
|
|
return influxdbTypeName, nil
|
|
}
|
|
|
|
func (i *Influxdb) getConnection(ctx context.Context) (influx.Client, error) {
|
|
cli, err := i.Connection(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cli.(influx.Client), nil
|
|
}
|
|
|
|
// CreateUser generates the username/password on the underlying Influxdb secret backend as instructed by
|
|
// the CreationStatement provided.
|
|
func (i *Influxdb) CreateUser(ctx context.Context, statements dbplugin.Statements, usernameConfig dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
|
|
// Grab the lock
|
|
i.Lock()
|
|
defer i.Unlock()
|
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
// Get the connection
|
|
cli, err := i.getConnection(ctx)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
creationIFQL := statements.Creation
|
|
if len(creationIFQL) == 0 {
|
|
creationIFQL = []string{defaultUserCreationIFQL}
|
|
}
|
|
|
|
rollbackIFQL := statements.Rollback
|
|
if len(rollbackIFQL) == 0 {
|
|
rollbackIFQL = []string{defaultUserDeletionIFQL}
|
|
}
|
|
|
|
username, err = i.GenerateUsername(usernameConfig)
|
|
username = strings.Replace(username, "-", "_", -1)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
username = strings.ToLower(username)
|
|
password, err = i.GeneratePassword()
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
// Execute each query
|
|
for _, stmt := range creationIFQL {
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
query = strings.TrimSpace(query)
|
|
if len(query) == 0 {
|
|
continue
|
|
}
|
|
|
|
q := influx.NewQuery(dbutil.QueryHelper(query, map[string]string{
|
|
"username": username,
|
|
"password": password,
|
|
}), "", "")
|
|
response, err := cli.Query(q)
|
|
if err != nil && response.Error() != nil {
|
|
for _, stmt := range rollbackIFQL {
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
continue
|
|
}
|
|
q := influx.NewQuery(dbutil.QueryHelper(query, map[string]string{
|
|
"username": username,
|
|
}), "", "")
|
|
|
|
response, err := cli.Query(q)
|
|
if err != nil && response.Error() != nil {
|
|
return "", "", err
|
|
}
|
|
}
|
|
}
|
|
return "", "", err
|
|
}
|
|
}
|
|
}
|
|
return username, password, nil
|
|
}
|
|
|
|
// RenewUser is not supported on Influxdb, so this is a no-op.
|
|
func (i *Influxdb) RenewUser(ctx context.Context, statements dbplugin.Statements, username string, expiration time.Time) error {
|
|
// NOOP
|
|
return nil
|
|
}
|
|
|
|
// RevokeUser attempts to drop the specified user.
|
|
func (i *Influxdb) RevokeUser(ctx context.Context, statements dbplugin.Statements, username string) error {
|
|
// Grab the lock
|
|
i.Lock()
|
|
defer i.Unlock()
|
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
cli, err := i.getConnection(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
revocationIFQL := statements.Revocation
|
|
if len(revocationIFQL) == 0 {
|
|
revocationIFQL = []string{defaultUserDeletionIFQL}
|
|
}
|
|
|
|
var result *multierror.Error
|
|
for _, stmt := range revocationIFQL {
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
query = strings.TrimSpace(query)
|
|
if len(query) == 0 {
|
|
continue
|
|
}
|
|
q := influx.NewQuery(dbutil.QueryHelper(query, map[string]string{
|
|
"username": username,
|
|
}), "", "")
|
|
response, err := cli.Query(q)
|
|
result = multierror.Append(result, err)
|
|
result = multierror.Append(result, response.Error())
|
|
}
|
|
}
|
|
return result.ErrorOrNil()
|
|
}
|
|
|
|
// RotateRootCredentials is useful when we try to change root credential
|
|
func (i *Influxdb) RotateRootCredentials(ctx context.Context, statements []string) (map[string]interface{}, error) {
|
|
// Grab the lock
|
|
i.Lock()
|
|
defer i.Unlock()
|
|
|
|
cli, err := i.getConnection(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rotateIFQL := statements
|
|
if len(rotateIFQL) == 0 {
|
|
rotateIFQL = []string{defaultRootCredentialRotationIFQL}
|
|
}
|
|
|
|
password, err := i.GeneratePassword()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result *multierror.Error
|
|
for _, stmt := range rotateIFQL {
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
query = strings.TrimSpace(query)
|
|
if len(query) == 0 {
|
|
continue
|
|
}
|
|
q := influx.NewQuery(dbutil.QueryHelper(query, map[string]string{
|
|
"username": i.Username,
|
|
"password": password,
|
|
}), "", "")
|
|
response, err := cli.Query(q)
|
|
result = multierror.Append(result, err)
|
|
result = multierror.Append(result, response.Error())
|
|
}
|
|
}
|
|
|
|
err = result.ErrorOrNil()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
i.rawConfig["password"] = password
|
|
return i.rawConfig, nil
|
|
}
|
|
|
|
// GenerateCredentials returns a generated password
|
|
func (i *Influxdb) GenerateCredentials(ctx context.Context) (string, error) {
|
|
password, err := i.GeneratePassword()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return password, nil
|
|
}
|