client.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // extract first "field" i.e. Items.0.ItemValue, data from secret using gjson
  56. val := gjson.Get(string(jsonStr), "Items.0.ItemValue")
  57. if !val.Exists() {
  58. return nil, esv1beta1.NoSecretError{}
  59. }
  60. // extract specific value from data directly above using gjson
  61. out := gjson.Get(val.String(), ref.Property)
  62. if !out.Exists() {
  63. return nil, esv1beta1.NoSecretError{}
  64. }
  65. return []byte(out.String()), nil
  66. }
  67. // Not supported at this time.
  68. func (c *client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  69. return errors.New("pushing secrets is not supported by Secret Server at this time")
  70. }
  71. // Not supported at this time.
  72. func (c *client) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  73. return errors.New("deleting secrets is not supported by Secret Server at this time")
  74. }
  75. // Not supported at this time.
  76. func (c *client) SecretExists(_ context.Context, _ esv1beta1.PushSecretRemoteRef) (bool, error) {
  77. return false, errors.New("not implemented")
  78. }
  79. // Not supported at this time.
  80. func (c *client) Validate() (esv1beta1.ValidationResult, error) {
  81. return esv1beta1.ValidationResultReady, nil
  82. }
  83. func (c *client) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  84. secret, err := c.getSecret(ctx, ref)
  85. if err != nil {
  86. return nil, err
  87. }
  88. secretData := make(map[string]any)
  89. err = json.Unmarshal([]byte(secret.Fields[0].ItemValue), &secretData)
  90. if err != nil {
  91. return nil, err
  92. }
  93. data := make(map[string][]byte)
  94. for k, v := range secretData {
  95. data[k], err = utils.GetByteValue(v)
  96. if err != nil {
  97. return nil, err
  98. }
  99. }
  100. return data, nil
  101. }
  102. // Not supported at this time.
  103. func (c *client) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  104. return nil, errors.New("getting all secrets is not supported by Delinea Secret Server at this time")
  105. }
  106. func (c *client) Close(context.Context) error {
  107. return nil
  108. }
  109. // getSecret retrieves the secret referenced by ref from the Vault API.
  110. func (c *client) getSecret(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (*server.Secret, error) {
  111. if ref.Version != "" {
  112. return nil, errors.New("specifying a version is not supported")
  113. }
  114. id, err := strconv.Atoi(ref.Key)
  115. if err != nil {
  116. s, err := c.api.Secrets(ref.Key, "Name")
  117. if err != nil {
  118. return nil, err
  119. }
  120. if len(s) == 0 {
  121. return nil, errors.New("unable to retrieve secret at this time")
  122. }
  123. return &s[0], nil
  124. }
  125. return c.api.Secret(id)
  126. }