60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
|
package postgresql
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/hashicorp/vault/logical"
|
||
|
"github.com/hashicorp/vault/logical/framework"
|
||
|
"github.com/lib/pq"
|
||
|
)
|
||
|
|
||
|
const SecretCredsType = "creds"
|
||
|
|
||
|
func secretCreds(b *backend) *framework.Secret {
|
||
|
return &framework.Secret{
|
||
|
Type: SecretCredsType,
|
||
|
Fields: map[string]*framework.FieldSchema{
|
||
|
"username": &framework.FieldSchema{
|
||
|
Type: framework.TypeString,
|
||
|
Description: "Username",
|
||
|
},
|
||
|
|
||
|
"password": &framework.FieldSchema{
|
||
|
Type: framework.TypeString,
|
||
|
Description: "Password",
|
||
|
},
|
||
|
},
|
||
|
|
||
|
Revoke: b.secretCredsRevoke,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (b *backend) secretCredsRevoke(
|
||
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
||
|
// Get the username from the internal data
|
||
|
usernameRaw, ok := req.Secret.InternalData["username"]
|
||
|
if !ok {
|
||
|
return nil, fmt.Errorf("secret is missing username internal data")
|
||
|
}
|
||
|
username, ok := usernameRaw.(string)
|
||
|
|
||
|
// Get our connection
|
||
|
db, err := b.DB(req.Storage)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Drop this user
|
||
|
stmt, err := db.Prepare(fmt.Sprintf(
|
||
|
"DROP ROLE IF EXISTS %s;", pq.QuoteIdentifier(username)))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer stmt.Close()
|
||
|
if _, err := stmt.Exec(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return nil, nil
|
||
|
}
|