client.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package secretserver
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "strconv"
  18. "strings"
  19. "github.com/DelineaXPM/tss-sdk-go/v3/server"
  20. "github.com/tidwall/gjson"
  21. corev1 "k8s.io/api/core/v1"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. "github.com/external-secrets/external-secrets/pkg/utils"
  24. )
  25. type client struct {
  26. api secretAPI
  27. }
  28. var _ esv1.SecretsClient = &client{}
  29. // GetSecret supports two types:
  30. // 1. Get the secrets using the secret ID in ref.key i.e. key: 53974
  31. // 2. Get the secret using the secret "name" i.e. key: "secretNameHere"
  32. // - Secret names must not contain spaces.
  33. // - If using the secret "name" and multiple secrets are found ...
  34. // the first secret in the array will be the secret returned.
  35. // 3. get the full secret as json-encoded value
  36. // by leaving the ref.Property empty.
  37. // 4. get a specific value by using a key from the json formatted secret in Items.0.ItemValue.
  38. // Nested values are supported by specifying a gjson expression
  39. func (c *client) GetSecret(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  40. secret, err := c.getSecret(ctx, ref)
  41. if err != nil {
  42. return nil, err
  43. }
  44. // Return nil if secret contains no fields
  45. if secret.Fields == nil {
  46. return nil, nil
  47. }
  48. jsonStr, err := json.Marshal(secret)
  49. if err != nil {
  50. return nil, err
  51. }
  52. // Intentionally fetch and return the full secret as raw JSON when no specific property is provided.
  53. // This requires calling the API to retrieve the entire secret object.
  54. if ref.Property == "" {
  55. return jsonStr, nil
  56. }
  57. // extract first "field" i.e. Items.0.ItemValue, data from secret using gjson
  58. val := gjson.Get(string(jsonStr), "Items.0.ItemValue")
  59. if val.Exists() && gjson.Valid(val.String()) {
  60. // extract specific value from data directly above using gjson
  61. out := gjson.Get(val.String(), ref.Property)
  62. if out.Exists() {
  63. return []byte(out.String()), nil
  64. }
  65. }
  66. // More general case Fields is an array in DelineaXPM/tss-sdk-go/v3/server
  67. // https://github.com/DelineaXPM/tss-sdk-go/blob/571e5674a8103031ad6f873453db27959ec1ca67/server/secret.go#L23
  68. secretMap := make(map[string]string)
  69. for index := range secret.Fields {
  70. secretMap[secret.Fields[index].FieldName] = secret.Fields[index].ItemValue
  71. secretMap[secret.Fields[index].Slug] = secret.Fields[index].ItemValue
  72. }
  73. out, ok := secretMap[ref.Property]
  74. if !ok {
  75. return nil, esv1.NoSecretError{}
  76. }
  77. return []byte(out), nil
  78. }
  79. // Not supported at this time.
  80. func (c *client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  81. return errors.New("pushing secrets is not supported by Secret Server at this time")
  82. }
  83. // Not supported at this time.
  84. func (c *client) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  85. return errors.New("deleting secrets is not supported by Secret Server at this time")
  86. }
  87. // Not supported at this time.
  88. func (c *client) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  89. return false, errors.New("not implemented")
  90. }
  91. // Not supported at this time.
  92. func (c *client) Validate() (esv1.ValidationResult, error) {
  93. return esv1.ValidationResultReady, nil
  94. }
  95. func (c *client) GetSecretMap(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  96. secret, err := c.getSecret(ctx, ref)
  97. if err != nil {
  98. return nil, err
  99. }
  100. secretData := make(map[string]any)
  101. err = json.Unmarshal([]byte(secret.Fields[0].ItemValue), &secretData)
  102. if err != nil {
  103. return nil, err
  104. }
  105. data := make(map[string][]byte)
  106. for k, v := range secretData {
  107. data[k], err = utils.GetByteValue(v)
  108. if err != nil {
  109. return nil, err
  110. }
  111. }
  112. return data, nil
  113. }
  114. // Not supported at this time.
  115. func (c *client) GetAllSecrets(_ context.Context, _ esv1.ExternalSecretFind) (map[string][]byte, error) {
  116. return nil, errors.New("getting all secrets is not supported by Delinea Secret Server at this time")
  117. }
  118. func (c *client) Close(context.Context) error {
  119. return nil
  120. }
  121. // getSecret retrieves the secret referenced by ref from the Vault API.
  122. func (c *client) getSecret(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) (*server.Secret, error) {
  123. if ref.Version != "" {
  124. return nil, errors.New("specifying a version is not supported")
  125. }
  126. // If the ref.Key looks like a full path (starts with "/"), fetch by path.
  127. // Example: "/Folder/Subfolder/SecretName"
  128. if strings.HasPrefix(ref.Key, "/") {
  129. s, err := c.api.SecretByPath(ref.Key)
  130. if err != nil {
  131. return nil, err
  132. }
  133. return s, nil
  134. }
  135. // Otherwise try converting it to an ID
  136. id, err := strconv.Atoi(ref.Key)
  137. if err != nil {
  138. s, err := c.api.Secrets(ref.Key, "Name")
  139. if err != nil {
  140. return nil, err
  141. }
  142. if len(s) == 0 {
  143. return nil, errors.New("unable to retrieve secret at this time")
  144. }
  145. return &s[0], nil
  146. }
  147. return c.api.Secret(id)
  148. }