/vendor/github.com/hashicorp/vault/builtin/logical/mysql/path_roles.go

https://github.com/Caiyeon/goldfish · Go · 233 lines · 198 code · 31 blank · 4 comment · 25 complexity · 945fa01f016011fa90ad418303a04ae7 MD5 · raw file

  1. package mysql
  2. import (
  3. "fmt"
  4. "strings"
  5. _ "github.com/go-sql-driver/mysql"
  6. "github.com/hashicorp/vault/helper/strutil"
  7. "github.com/hashicorp/vault/logical"
  8. "github.com/hashicorp/vault/logical/framework"
  9. )
  10. func pathListRoles(b *backend) *framework.Path {
  11. return &framework.Path{
  12. Pattern: "roles/?$",
  13. Callbacks: map[logical.Operation]framework.OperationFunc{
  14. logical.ListOperation: b.pathRoleList,
  15. },
  16. HelpSynopsis: pathRoleHelpSyn,
  17. HelpDescription: pathRoleHelpDesc,
  18. }
  19. }
  20. func pathRoles(b *backend) *framework.Path {
  21. return &framework.Path{
  22. Pattern: "roles/" + framework.GenericNameRegex("name"),
  23. Fields: map[string]*framework.FieldSchema{
  24. "name": {
  25. Type: framework.TypeString,
  26. Description: "Name of the role.",
  27. },
  28. "sql": {
  29. Type: framework.TypeString,
  30. Description: "SQL string to create a user. See help for more info.",
  31. },
  32. "revocation_sql": {
  33. Type: framework.TypeString,
  34. Description: "SQL string to revoke a user. See help for more info.",
  35. },
  36. "username_length": {
  37. Type: framework.TypeInt,
  38. Description: "number of characters to truncate generated mysql usernames to (default 16)",
  39. Default: 16,
  40. },
  41. "rolename_length": {
  42. Type: framework.TypeInt,
  43. Description: "number of characters to truncate the rolename portion of generated mysql usernames to (default 4)",
  44. Default: 4,
  45. },
  46. "displayname_length": {
  47. Type: framework.TypeInt,
  48. Description: "number of characters to truncate the displayname portion of generated mysql usernames to (default 4)",
  49. Default: 4,
  50. },
  51. },
  52. Callbacks: map[logical.Operation]framework.OperationFunc{
  53. logical.ReadOperation: b.pathRoleRead,
  54. logical.UpdateOperation: b.pathRoleCreate,
  55. logical.DeleteOperation: b.pathRoleDelete,
  56. },
  57. HelpSynopsis: pathRoleHelpSyn,
  58. HelpDescription: pathRoleHelpDesc,
  59. }
  60. }
  61. func (b *backend) Role(s logical.Storage, n string) (*roleEntry, error) {
  62. entry, err := s.Get("role/" + n)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if entry == nil {
  67. return nil, nil
  68. }
  69. // Set defaults to handle upgrade cases
  70. result := roleEntry{
  71. UsernameLength: 16,
  72. RolenameLength: 4,
  73. DisplaynameLength: 4,
  74. }
  75. if err := entry.DecodeJSON(&result); err != nil {
  76. return nil, err
  77. }
  78. return &result, nil
  79. }
  80. func (b *backend) pathRoleDelete(
  81. req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
  82. err := req.Storage.Delete("role/" + data.Get("name").(string))
  83. if err != nil {
  84. return nil, err
  85. }
  86. return nil, nil
  87. }
  88. func (b *backend) pathRoleRead(
  89. req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
  90. role, err := b.Role(req.Storage, data.Get("name").(string))
  91. if err != nil {
  92. return nil, err
  93. }
  94. if role == nil {
  95. return nil, nil
  96. }
  97. return &logical.Response{
  98. Data: map[string]interface{}{
  99. "sql": role.SQL,
  100. "revocation_sql": role.RevocationSQL,
  101. },
  102. }, nil
  103. }
  104. func (b *backend) pathRoleList(
  105. req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
  106. entries, err := req.Storage.List("role/")
  107. if err != nil {
  108. return nil, err
  109. }
  110. return logical.ListResponse(entries), nil
  111. }
  112. func (b *backend) pathRoleCreate(
  113. req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
  114. name := data.Get("name").(string)
  115. // Get our connection
  116. db, err := b.DB(req.Storage)
  117. if err != nil {
  118. return nil, err
  119. }
  120. // Test the query by trying to prepare it
  121. sql := data.Get("sql").(string)
  122. for _, query := range strutil.ParseArbitraryStringSlice(sql, ";") {
  123. query = strings.TrimSpace(query)
  124. if len(query) == 0 {
  125. continue
  126. }
  127. stmt, err := db.Prepare(Query(query, map[string]string{
  128. "name": "foo",
  129. "password": "bar",
  130. }))
  131. if err != nil {
  132. return logical.ErrorResponse(fmt.Sprintf(
  133. "Error testing query: %s", err)), nil
  134. }
  135. stmt.Close()
  136. }
  137. // Store it
  138. entry, err := logical.StorageEntryJSON("role/"+name, &roleEntry{
  139. SQL: sql,
  140. RevocationSQL: data.Get("revocation_sql").(string),
  141. UsernameLength: data.Get("username_length").(int),
  142. DisplaynameLength: data.Get("displayname_length").(int),
  143. RolenameLength: data.Get("rolename_length").(int),
  144. })
  145. if err != nil {
  146. return nil, err
  147. }
  148. if err := req.Storage.Put(entry); err != nil {
  149. return nil, err
  150. }
  151. return nil, nil
  152. }
  153. type roleEntry struct {
  154. SQL string `json:"sql" mapstructure:"sql" structs:"sql"`
  155. RevocationSQL string `json:"revocation_sql" mapstructure:"revocation_sql" structs:"revocation_sql"`
  156. UsernameLength int `json:"username_length" mapstructure:"username_length" structs:"username_length"`
  157. DisplaynameLength int `json:"displayname_length" mapstructure:"displayname_length" structs:"displayname_length"`
  158. RolenameLength int `json:"rolename_length" mapstructure:"rolename_length" structs:"rolename_length"`
  159. }
  160. const pathRoleHelpSyn = `
  161. Manage the roles that can be created with this backend.
  162. `
  163. const pathRoleHelpDesc = `
  164. This path lets you manage the roles that can be created with this backend.
  165. The "sql" parameter customizes the SQL string used to create the role.
  166. This can be a sequence of SQL queries, each semi-colon seperated. Some
  167. substitution will be done to the SQL string for certain keys.
  168. The names of the variables must be surrounded by "{{" and "}}" to be replaced.
  169. * "name" - The random username generated for the DB user.
  170. * "password" - The random password generated for the DB user.
  171. Example of a decent SQL query to use:
  172. CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
  173. GRANT ALL ON db1.* TO '{{name}}'@'%';
  174. Note the above user would be able to access anything in db1. Please see the MySQL
  175. manual on the GRANT command to learn how to do more fine grained access.
  176. The "rolename_length" parameter determines how many characters of the role name
  177. will be used in creating the generated mysql username; the default is 4.
  178. The "displayname_length" parameter determines how many characters of the token
  179. display name will be used in creating the generated mysql username; the default
  180. is 4.
  181. The "username_length" parameter determines how many total characters the
  182. generated username (including the role name, token display name and the uuid
  183. portion) will be truncated to. Versions of MySQL prior to 5.7.8 are limited to
  184. 16 characters total (see
  185. http://dev.mysql.com/doc/refman/5.7/en/user-names.html) so that is the default;
  186. for versions >=5.7.8 it is safe to increase this to 32.
  187. For best readability in MySQL process lists, we recommend using MySQL 5.7.8 or
  188. later, setting "username_length" to 32 and setting both "rolename_length" and
  189. "displayname_length" to 8. However due the the prevalence of older versions of
  190. MySQL in general deployment, the defaults are currently tuned for a
  191. username_length of 16.
  192. `