auth.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "github.com/pkg/errors"
  17. "github.com/rook/rook/pkg/clusterd"
  18. )
  19. // AuthGetOrCreate will either get or create a user with the given capabilities. The keyring for the
  20. // user will be written to the given keyring path.
  21. func AuthGetOrCreate(context *clusterd.Context, clusterInfo *ClusterInfo, name, keyringPath string, caps []string) error {
  22. logger.Infof("getting or creating ceph auth %q", name)
  23. args := append([]string{"auth", "get-or-create", name, "-o", keyringPath}, caps...)
  24. cmd := NewCephCommand(context, clusterInfo, args)
  25. cmd.JsonOutput = false
  26. _, err := cmd.Run()
  27. if err != nil {
  28. return errors.Wrapf(err, "failed to auth get-or-create for %s", name)
  29. }
  30. return nil
  31. }
  32. // AuthGetKey gets the key for the given user.
  33. func AuthGetKey(context *clusterd.Context, clusterInfo *ClusterInfo, name string) (string, error) {
  34. logger.Infof("getting ceph auth key %q", name)
  35. args := []string{"auth", "get-key", name}
  36. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  37. if err != nil {
  38. return "", errors.Wrapf(err, "failed to get key for %s", name)
  39. }
  40. return parseAuthKey(buf)
  41. }
  42. // AuthGetOrCreateKey gets or creates the key for the given user.
  43. func AuthGetOrCreateKey(context *clusterd.Context, clusterInfo *ClusterInfo, name string, caps []string) (string, error) {
  44. logger.Infof("getting or creating ceph auth key %q", name)
  45. args := append([]string{"auth", "get-or-create-key", name}, caps...)
  46. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  47. if err != nil {
  48. return "", errors.Wrapf(err, "failed get-or-create-key %s", name)
  49. }
  50. return parseAuthKey(buf)
  51. }
  52. // AuthUpdateCaps updates the capabilities for the given user.
  53. func AuthUpdateCaps(context *clusterd.Context, clusterInfo *ClusterInfo, name string, caps []string) error {
  54. logger.Infof("updating ceph auth caps %q to %v", name, caps)
  55. args := append([]string{"auth", "caps", name}, caps...)
  56. _, err := NewCephCommand(context, clusterInfo, args).Run()
  57. if err != nil {
  58. return errors.Wrapf(err, "failed to update caps for %s", name)
  59. }
  60. return err
  61. }
  62. // AuthGetCaps gets the capabilities for the given user.
  63. func AuthGetCaps(context *clusterd.Context, clusterInfo *ClusterInfo, name string) (caps map[string]string, error error) {
  64. logger.Infof("getting ceph auth caps for %q", name)
  65. args := []string{"auth", "get", name}
  66. output, err := NewCephCommand(context, clusterInfo, args).Run()
  67. if err != nil {
  68. return nil, errors.Wrapf(err, "failed to get caps for %q", name)
  69. }
  70. var data []map[string]interface{}
  71. err = json.Unmarshal(output, &data)
  72. if err != nil {
  73. return nil, errors.Wrap(err, "failed to unmarshal auth get response")
  74. }
  75. caps = make(map[string]string)
  76. if data[0]["caps"].(map[string]interface{})["mon"] != nil {
  77. caps["mon"] = data[0]["caps"].(map[string]interface{})["mon"].(string)
  78. }
  79. if data[0]["caps"].(map[string]interface{})["mds"] != nil {
  80. caps["mds"] = data[0]["caps"].(map[string]interface{})["mds"].(string)
  81. }
  82. if data[0]["caps"].(map[string]interface{})["mgr"] != nil {
  83. caps["mgr"] = data[0]["caps"].(map[string]interface{})["mgr"].(string)
  84. }
  85. if data[0]["caps"].(map[string]interface{})["osd"] != nil {
  86. caps["osd"] = data[0]["caps"].(map[string]interface{})["osd"].(string)
  87. }
  88. return caps, err
  89. }
  90. // AuthDelete will delete the given user.
  91. func AuthDelete(context *clusterd.Context, clusterInfo *ClusterInfo, name string) error {
  92. logger.Infof("deleting ceph auth %q", name)
  93. args := []string{"auth", "del", name}
  94. _, err := NewCephCommand(context, clusterInfo, args).Run()
  95. if err != nil {
  96. return errors.Wrapf(err, "failed to delete auth for %s", name)
  97. }
  98. return nil
  99. }
  100. func parseAuthKey(buf []byte) (string, error) {
  101. var resp map[string]interface{}
  102. if err := json.Unmarshal(buf, &resp); err != nil {
  103. return "", errors.Wrap(err, "failed to unmarshal get/create key response")
  104. }
  105. return resp["key"].(string), nil
  106. }