keyring.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2018 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/base64"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "github.com/pkg/errors"
  20. "github.com/rook/rook/pkg/clusterd"
  21. )
  22. const (
  23. // AdminKeyringTemplate is a string template of Ceph keyring settings which allow connection
  24. // as admin. The key value must be filled in by the admin auth key for the cluster.
  25. AdminKeyringTemplate = `
  26. [client.admin]
  27. key = %s
  28. caps mds = "allow *"
  29. caps mon = "allow *"
  30. caps osd = "allow *"
  31. caps mgr = "allow *"
  32. `
  33. // UserKeyringTemplate is a string template of Ceph keyring settings which allow connection.
  34. UserKeyringTemplate = `
  35. [%s]
  36. key = %s
  37. `
  38. )
  39. // CephKeyring returns the filled-out user keyring
  40. func CephKeyring(cred CephCred) string {
  41. if cred.Username == AdminUsername {
  42. return fmt.Sprintf(AdminKeyringTemplate, cred.Secret)
  43. }
  44. return fmt.Sprintf(UserKeyringTemplate, cred.Username, cred.Secret)
  45. }
  46. // WriteKeyring calls the generate contents function with auth key as an argument then saves the
  47. // output of the generateContents function to disk at the keyring path
  48. // TODO: Kludgey; can keyring files be generated w/ go-ini package or using the '-o' option to
  49. // 'ceph auth get-or-create ...'?
  50. func WriteKeyring(keyringPath, authKey string, generateContents func(string) string) error {
  51. contents := generateContents(authKey)
  52. return writeKeyring(contents, keyringPath)
  53. }
  54. // CreateKeyring creates a keyring for access to the cluster with the desired set of privileges
  55. // and writes it to disk at the keyring path
  56. func CreateKeyring(context *clusterd.Context, clusterInfo *ClusterInfo, username, keyringPath string, access []string, generateContents func(string) string) error {
  57. _, err := os.Stat(keyringPath)
  58. if err == nil {
  59. // no error, the file exists, bail out with no error
  60. logger.Debugf("keyring already exists at %s", keyringPath)
  61. return nil
  62. } else if !os.IsNotExist(err) {
  63. // some other error besides "does not exist", bail out with error
  64. return errors.Wrapf(err, "failed to stat %s", keyringPath)
  65. }
  66. // get-or-create-key for the user account
  67. key, err := AuthGetOrCreateKey(context, clusterInfo, username, access)
  68. if err != nil {
  69. return errors.Wrapf(err, "failed to get or create auth key for %s", username)
  70. }
  71. return WriteKeyring(keyringPath, key, generateContents)
  72. }
  73. // writes the keyring to disk
  74. // TODO: Write keyring only to the default ceph config location since we are in a container
  75. func writeKeyring(keyring, keyringPath string) error {
  76. // save the keyring to the given path
  77. if err := os.MkdirAll(filepath.Dir(keyringPath), 0700); err != nil {
  78. return errors.Wrapf(err, "failed to create keyring directory for %s", keyringPath)
  79. }
  80. if err := os.WriteFile(keyringPath, []byte(keyring), 0600); err != nil {
  81. return errors.Wrapf(err, "failed to write monitor keyring to %s", keyringPath)
  82. }
  83. return nil
  84. }
  85. // IsKeyringBase64Encoded returns whether the keyring is valid
  86. func IsKeyringBase64Encoded(keyring string) bool {
  87. // If the keyring is not base64 we fail
  88. _, err := base64.StdEncoding.DecodeString(keyring)
  89. if err != nil {
  90. logger.Errorf("key is not base64 encoded. %v", err)
  91. return false
  92. }
  93. return true
  94. }