client.go 5.4 KB

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