client.go 5.0 KB

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