2015-04-19 01:09:33 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathRoles(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "roles/(?P<name>\\w+)",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the role.",
|
|
|
|
},
|
|
|
|
|
|
|
|
"sql": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "SQL string to create a user. See help for more info.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2015-04-19 04:59:59 +00:00
|
|
|
logical.ReadOperation: b.pathRoleRead,
|
|
|
|
logical.WriteOperation: b.pathRoleCreate,
|
|
|
|
logical.DeleteOperation: b.pathRoleDelete,
|
2015-04-19 01:09:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRoleHelpSyn,
|
|
|
|
HelpDescription: pathRoleHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 04:59:59 +00:00
|
|
|
func (b *backend) Role(s logical.Storage, n string) (*roleEntry, error) {
|
|
|
|
entry, err := s.Get("role/" + n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result roleEntry
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathRoleDelete(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
err := req.Storage.Delete("role/" + data.Get("name").(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathRoleRead(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
role, err := b.Role(req.Storage, data.Get("name").(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"sql": role.SQL,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 01:09:33 +00:00
|
|
|
func (b *backend) pathRoleCreate(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
name := data.Get("name").(string)
|
|
|
|
sql := data.Get("sql").(string)
|
|
|
|
|
|
|
|
// Get our connection
|
|
|
|
db, err := b.DB(req.Storage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the query by trying to prepare it
|
2015-04-27 18:31:27 +00:00
|
|
|
for _, query := range SplitSQL(sql) {
|
|
|
|
stmt, err := db.Prepare(Query(query, map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
"password": "bar",
|
|
|
|
"expiration": "",
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Error testing query: %s", err)), nil
|
|
|
|
}
|
|
|
|
stmt.Close()
|
2015-04-19 01:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
2015-04-19 04:59:59 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("role/"+name, &roleEntry{
|
|
|
|
SQL: sql,
|
2015-04-19 01:09:33 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 04:59:59 +00:00
|
|
|
type roleEntry struct {
|
|
|
|
SQL string `json:"sql"`
|
|
|
|
}
|
|
|
|
|
2015-04-19 01:09:33 +00:00
|
|
|
const pathRoleHelpSyn = `
|
|
|
|
Manage the roles that can be created with this backend.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathRoleHelpDesc = `
|
|
|
|
This path lets you manage the roles that can be created with this backend.
|
|
|
|
|
|
|
|
The "sql" parameter customizes the SQL string used to create the role.
|
2015-04-27 18:31:27 +00:00
|
|
|
This can be a sequence of SQL queries. Some substitution will be done to the
|
2015-04-19 01:09:33 +00:00
|
|
|
SQL string for certain keys. The names of the variables must be surrounded
|
|
|
|
by "{{" and "}}" to be replaced.
|
|
|
|
|
|
|
|
* "name" - The random username generated for the DB user.
|
|
|
|
|
|
|
|
* "password" - The random password generated for the DB user.
|
|
|
|
|
|
|
|
* "expiration" - The timestamp when this user will expire.
|
|
|
|
|
|
|
|
Example of a decent SQL query to use:
|
|
|
|
|
2015-04-27 18:31:27 +00:00
|
|
|
CREATE ROLE '{{name}}' WITH
|
2015-04-19 01:09:33 +00:00
|
|
|
LOGIN
|
|
|
|
PASSWORD '{{password}}'
|
|
|
|
VALID UNTIL '{{expiration}}';
|
2015-04-27 18:31:27 +00:00
|
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA db1 TO '{{name}}';
|
2015-04-19 01:09:33 +00:00
|
|
|
|
2015-04-27 18:31:27 +00:00
|
|
|
Note the above user would be able to access everything. In schema dc1.
|
|
|
|
For more complex GRANT clauses, see the PostgreSQL manuel.
|
2015-04-19 01:09:33 +00:00
|
|
|
`
|