mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-11-04 04:28:08 +00:00 
			
		
		
		
	1) Use the new LeaseExtend 2) Use default values controlled by mount tuning/system defaults instead of a random hard coded value 3) Remove grace periods
		
			
				
	
	
		
			133 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package postgresql
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"github.com/hashicorp/go-uuid"
 | 
						|
	"github.com/hashicorp/vault/logical"
 | 
						|
	"github.com/hashicorp/vault/logical/framework"
 | 
						|
	_ "github.com/lib/pq"
 | 
						|
)
 | 
						|
 | 
						|
func pathRoleCreate(b *backend) *framework.Path {
 | 
						|
	return &framework.Path{
 | 
						|
		Pattern: "creds/" + framework.GenericNameRegex("name"),
 | 
						|
		Fields: map[string]*framework.FieldSchema{
 | 
						|
			"name": &framework.FieldSchema{
 | 
						|
				Type:        framework.TypeString,
 | 
						|
				Description: "Name of the role.",
 | 
						|
			},
 | 
						|
		},
 | 
						|
 | 
						|
		Callbacks: map[logical.Operation]framework.OperationFunc{
 | 
						|
			logical.ReadOperation: b.pathRoleCreateRead,
 | 
						|
		},
 | 
						|
 | 
						|
		HelpSynopsis:    pathRoleCreateReadHelpSyn,
 | 
						|
		HelpDescription: pathRoleCreateReadHelpDesc,
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func (b *backend) pathRoleCreateRead(
 | 
						|
	req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
 | 
						|
	name := data.Get("name").(string)
 | 
						|
 | 
						|
	// Get the role
 | 
						|
	role, err := b.Role(req.Storage, name)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	if role == nil {
 | 
						|
		return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", name)), nil
 | 
						|
	}
 | 
						|
 | 
						|
	// Determine if we have a lease
 | 
						|
	lease, err := b.Lease(req.Storage)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	// Unlike some other backends we need a lease here (can't leave as 0 and
 | 
						|
	// let core fill it in) because Postgres also expires users as a safety
 | 
						|
	// measure, so cannot be zero
 | 
						|
	if lease == nil {
 | 
						|
		lease = &configLease{
 | 
						|
			Lease: b.System().DefaultLeaseTTL(),
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	// Generate the username, password and expiration. PG limits user to 63 characters
 | 
						|
	displayName := req.DisplayName
 | 
						|
	if len(displayName) > 26 {
 | 
						|
		displayName = displayName[:26]
 | 
						|
	}
 | 
						|
	userUUID, err := uuid.GenerateUUID()
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	username := fmt.Sprintf("%s-%s", displayName, userUUID)
 | 
						|
	if len(username) > 63 {
 | 
						|
		username = username[:63]
 | 
						|
	}
 | 
						|
	password, err := uuid.GenerateUUID()
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	expiration := time.Now().UTC().
 | 
						|
		Add(lease.Lease).
 | 
						|
		Format("2006-01-02 15:04:05-0700")
 | 
						|
 | 
						|
	// Get our connection
 | 
						|
	db, err := b.DB(req.Storage)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	// Start a transaction
 | 
						|
	tx, err := db.Begin()
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	defer tx.Rollback()
 | 
						|
 | 
						|
	// Execute each query
 | 
						|
	for _, query := range SplitSQL(role.SQL) {
 | 
						|
		stmt, err := db.Prepare(Query(query, map[string]string{
 | 
						|
			"name":       username,
 | 
						|
			"password":   password,
 | 
						|
			"expiration": expiration,
 | 
						|
		}))
 | 
						|
		if err != nil {
 | 
						|
			return nil, err
 | 
						|
		}
 | 
						|
		if _, err := stmt.Exec(); err != nil {
 | 
						|
			return nil, err
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	// Commit the transaction
 | 
						|
	if err := tx.Commit(); err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	// Return the secret
 | 
						|
	resp := b.Secret(SecretCredsType).Response(map[string]interface{}{
 | 
						|
		"username": username,
 | 
						|
		"password": password,
 | 
						|
	}, map[string]interface{}{
 | 
						|
		"username": username,
 | 
						|
	})
 | 
						|
	resp.Secret.TTL = lease.Lease
 | 
						|
	return resp, nil
 | 
						|
}
 | 
						|
 | 
						|
const pathRoleCreateReadHelpSyn = `
 | 
						|
Request database credentials for a certain role.
 | 
						|
`
 | 
						|
 | 
						|
const pathRoleCreateReadHelpDesc = `
 | 
						|
This path reads database credentials for a certain role. The
 | 
						|
database credentials will be generated on demand and will be automatically
 | 
						|
revoked when the lease is up.
 | 
						|
`
 |