Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package keystoneauth
  2.  
  3. import (
  4. "fmt"
  5. "github.com/hashicorp/vault/logical"
  6. "github.com/hashicorp/vault/logical/framework"
  7. )
  8.  
  9. func pathListUsers(b *backend) *framework.Path {
  10. return &framework.Path{
  11. Pattern: "users/?$",
  12.  
  13. Callbacks: map[logical.Operation]framework.OperationFunc{
  14. logical.ListOperation: b.pathUserList,
  15. },
  16. }
  17. }
  18.  
  19. func pathUsers(b *backend) *framework.Path {
  20. return &framework.Path{
  21. Pattern: "users/" + framework.GenericNameRegex("name"),
  22. Fields: map[string]*framework.FieldSchema{
  23. "name": &framework.FieldSchema{
  24. Type: framework.TypeString,
  25. Description: "User name",
  26. },
  27. "default_project_id": &framework.FieldSchema{
  28. Type: framework.TypeString,
  29. Description: "default_project_id",
  30. Default: "optional",
  31. },
  32. "domain_id": &framework.FieldSchema{
  33. Type: framework.TypeString,
  34. Description: "default_domain_id",
  35. Default: "optional",
  36. },
  37. "enabled": &framework.FieldSchema{
  38. Type: framework.TypeBool,
  39. Description: "default_enabled",
  40. Default: true,
  41. },
  42. "password": &framework.FieldSchema{
  43. Type: framework.TypeString,
  44. Description: "default_passwords",
  45. Default: "optional",
  46. },
  47. },
  48. Callbacks: map[logical.Operation]framework.OperationFunc{
  49. logical.UpdateOperation: b.pathUserWrite,
  50. //logical.ReadOperation: b.pathUserRead,
  51. },
  52. }
  53. }
  54.  
  55. func (b *backend) User(s logical.Storage, n string) (*userEntry, error) {
  56. entry, err := s.Get("user/" + n)
  57. if err != nil {
  58. return nil, err
  59. }
  60. if entry == nil {
  61. return nil, nil
  62. }
  63.  
  64. var result userEntry
  65.  
  66. if err := entry.DecodeJSON(&result); err != nil {
  67. return nil, err
  68. }
  69.  
  70. return &result, nil
  71. }
  72.  
  73. func (b *backend) pathUserRead(
  74. req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
  75.  
  76. name := data.Get("name").(string)
  77.  
  78. user, err := b.User(req.Storage, name)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if user == nil {
  83. return logical.ErrorResponse(fmt.Sprintf("unknown user: %s", name)), nil
  84. }
  85.  
  86. //CreateUser()
  87.  
  88. return &logical.Response{
  89. Data: map[string]interface{}{
  90. "name": user.User_name,
  91. "default_project_id": user.User_default_project_id,
  92. "domain_id": user.User_domain_id,
  93. "enabled": user.User_enabled,
  94. "password": user.User_password,
  95. },
  96. }, nil
  97. }
  98.  
  99. func (b *backend) pathUserList(
  100. req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
  101. entries, err := req.Storage.List("user/")
  102. if err != nil {
  103. return nil, err
  104. }
  105.  
  106. return logical.ListResponse(entries), nil
  107. }
  108.  
  109. func (b *backend) pathUserWrite(
  110. req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
  111.  
  112. fmt.Println("started")
  113.  
  114. name := data.Get("name").(string)
  115. default_project_id := data.Get("admin_auth_token").(string)
  116. domain_id := data.Get("domain_id").(string)
  117. enabled := data.Get("enabled").(bool)
  118. password := data.Get("password").(string)
  119.  
  120. fmt.Println(name)
  121. fmt.Println(default_project_id)
  122. fmt.Println(domain_id)
  123. fmt.Println(password)
  124.  
  125. // Store it
  126. entry, err := logical.StorageEntryJSON("user/"+name, &userEntry{
  127. User_name: name,
  128. User_default_project_id: default_project_id,
  129. User_domain_id: domain_id,
  130. User_enabled: enabled,
  131. User_password: password,
  132. })
  133.  
  134. if err != nil {
  135. return nil, err
  136. }
  137.  
  138. if err != nil {
  139. return nil, err
  140. }
  141. if err := req.Storage.Put(entry); err != nil {
  142. return nil, err
  143. }
  144. return nil, nil
  145. }
  146.  
  147. type userEntry struct {
  148. User_name string `json:"name" structs:"name" mapstructure:"name"`
  149. User_default_project_id string `json:"default_project_id" structs:"default_project_id" mapstructure:"default_project_id"`
  150. User_domain_id string `json:"domain_id" structs:"domain_id" mapstructure:"domain_id"`
  151. User_enabled bool `json:"enabled" structs:"enabled" mapstructure:"enabled"`
  152. User_password string `json:"password" structs:"password" mapstructure:"password"`
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement