client.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. "github.com/external-secrets/external-secrets/pkg/utils"
  23. )
  24. type client struct {
  25. api secretAPI
  26. }
  27. var _ esv1beta1.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 esv1beta1.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. // If no property is defined return the full secret as raw json
  52. if ref.Property == "" {
  53. return jsonStr, nil
  54. }
  55. // Keep original behavior of decoding first Item into gjson
  56. if len(secret.Fields) == 1 {
  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() {
  60. return nil, esv1beta1.NoSecretError{}
  61. }
  62. // extract specific value from data directly above using gjson
  63. out := gjson.Get(val.String(), ref.Property)
  64. if !out.Exists() {
  65. return nil, esv1beta1.NoSecretError{}
  66. }
  67. return []byte(out.String()), nil
  68. } else {
  69. // More general case Fields is an array in DelineaXPM/tss-sdk-go/v2/server
  70. // https://github.com/DelineaXPM/tss-sdk-go/blob/571e5674a8103031ad6f873453db27959ec1ca67/server/secret.go#L23
  71. secretMap := make(map[string]string)
  72. for index := range secret.Fields {
  73. secretMap[secret.Fields[index].FieldName] = secret.Fields[index].ItemValue
  74. secretMap[secret.Fields[index].Slug] = secret.Fields[index].ItemValue
  75. }
  76. out, ok := secretMap[ref.Property]
  77. if !ok {
  78. return nil, esv1beta1.NoSecretError{}
  79. }
  80. return []byte(out), nil
  81. }
  82. }
  83. // Not supported at this time.
  84. func (c *client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  85. return errors.New("pushing secrets is not supported by Secret Server at this time")
  86. }
  87. // Not supported at this time.
  88. func (c *client) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  89. return errors.New("deleting secrets is not supported by Secret Server at this time")
  90. }
  91. // Not supported at this time.
  92. func (c *client) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  93. return false, errors.New("not implemented")
  94. }
  95. // Not supported at this time.
  96. func (c *client) Validate() (esv1beta1.ValidationResult, error) {
  97. return esv1beta1.ValidationResultReady, nil
  98. }
  99. func (c *client) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  100. secret, err := c.getSecret(ctx, ref)
  101. if err != nil {
  102. return nil, err
  103. }
  104. secretData := make(map[string]any)
  105. err = json.Unmarshal([]byte(secret.Fields[0].ItemValue), &secretData)
  106. if err != nil {
  107. return nil, err
  108. }
  109. data := make(map[string][]byte)
  110. for k, v := range secretData {
  111. data[k], err = utils.GetByteValue(v)
  112. if err != nil {
  113. return nil, err
  114. }
  115. }
  116. return data, nil
  117. }
  118. // Not supported at this time.
  119. func (c *client) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  120. return nil, errors.New("getting all secrets is not supported by Delinea Secret Server at this time")
  121. }
  122. func (c *client) Close(context.Context) error {
  123. return nil
  124. }
  125. // getSecret retrieves the secret referenced by ref from the Vault API.
  126. func (c *client) getSecret(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (*server.Secret, error) {
  127. if ref.Version != "" {
  128. return nil, errors.New("specifying a version is not supported")
  129. }
  130. id, err := strconv.Atoi(ref.Key)
  131. if err != nil {
  132. s, err := c.api.Secrets(ref.Key, "Name")
  133. if err != nil {
  134. return nil, err
  135. }
  136. if len(s) == 0 {
  137. return nil, errors.New("unable to retrieve secret at this time")
  138. }
  139. return &s[0], nil
  140. }
  141. return c.api.Secret(id)
  142. }