Skip to content

Instantly share code, notes, and snippets.

@ror6ax
Created August 24, 2017 20:43
Show Gist options
  • Save ror6ax/65f15c4382ecd1edc43db2d646c917ef to your computer and use it in GitHub Desktop.
Save ror6ax/65f15c4382ecd1edc43db2d646c917ef to your computer and use it in GitHub Desktop.
package keystoneauth
import (
"fmt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathListUsers(b *backend) *framework.Path {
return &framework.Path{
Pattern: "users/?$",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathUserList,
},
}
}
func pathUsers(b *backend) *framework.Path {
return &framework.Path{
Pattern: "users/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "User name",
},
"default_project_id": &framework.FieldSchema{
Type: framework.TypeString,
Description: "default_project_id",
Default: "optional",
},
"domain_id": &framework.FieldSchema{
Type: framework.TypeString,
Description: "default_domain_id",
Default: "optional",
},
"enabled": &framework.FieldSchema{
Type: framework.TypeBool,
Description: "default_enabled",
Default: true,
},
"password": &framework.FieldSchema{
Type: framework.TypeString,
Description: "default_passwords",
Default: "optional",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathUserWrite,
//logical.ReadOperation: b.pathUserRead,
},
}
}
func (b *backend) User(s logical.Storage, n string) (*userEntry, error) {
entry, err := s.Get("user/" + n)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result userEntry
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
return &result, nil
}
func (b *backend) pathUserRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
user, err := b.User(req.Storage, name)
if err != nil {
return nil, err
}
if user == nil {
return logical.ErrorResponse(fmt.Sprintf("unknown user: %s", name)), nil
}
//CreateUser()
return &logical.Response{
Data: map[string]interface{}{
"name": user.User_name,
"default_project_id": user.User_default_project_id,
"domain_id": user.User_domain_id,
"enabled": user.User_enabled,
"password": user.User_password,
},
}, nil
}
func (b *backend) pathUserList(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List("user/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
func (b *backend) pathUserWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
fmt.Println("started")
name := data.Get("name").(string)
default_project_id := data.Get("admin_auth_token").(string)
domain_id := data.Get("domain_id").(string)
enabled := data.Get("enabled").(bool)
password := data.Get("password").(string)
fmt.Println(name)
fmt.Println(default_project_id)
fmt.Println(domain_id)
fmt.Println(password)
// Store it
entry, err := logical.StorageEntryJSON("user/"+name, &userEntry{
User_name: name,
User_default_project_id: default_project_id,
User_domain_id: domain_id,
User_enabled: enabled,
User_password: password,
})
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
if err := req.Storage.Put(entry); err != nil {
return nil, err
}
return nil, nil
}
type userEntry struct {
User_name string `json:"name" structs:"name" mapstructure:"name"`
User_default_project_id string `json:"default_project_id" structs:"default_project_id" mapstructure:"default_project_id"`
User_domain_id string `json:"domain_id" structs:"domain_id" mapstructure:"domain_id"`
User_enabled bool `json:"enabled" structs:"enabled" mapstructure:"enabled"`
User_password string `json:"password" structs:"password" mapstructure:"password"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment