client.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 delinea
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "github.com/DelineaXPM/dsv-sdk-go/v2/vault"
  22. "github.com/tidwall/gjson"
  23. corev1 "k8s.io/api/core/v1"
  24. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  25. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  26. )
  27. const (
  28. errSecretKeyFmt = "cannot find secret data for key: %q"
  29. errUnexpectedKey = "unexpected key in data: %s"
  30. errSecretFormat = "secret data for property %s not in expected format: %s"
  31. )
  32. type client struct {
  33. api secretAPI
  34. }
  35. var _ esv1beta1.SecretsClient = &client{}
  36. // GetSecret supports two types:
  37. // 1. get the full secret as json-encoded value
  38. // by leaving the ref.Property empty.
  39. // 2. get a key from the secret.
  40. // Nested values are supported by specifying a gjson expression
  41. func (c *client) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  42. secret, err := c.getSecret(ctx, ref)
  43. if err != nil {
  44. return nil, err
  45. }
  46. // Return nil if secret value is null
  47. if secret.Data == nil {
  48. return nil, nil
  49. }
  50. jsonStr, err := json.Marshal(secret.Data)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // return raw json if no property is defined
  55. if ref.Property == "" {
  56. return jsonStr, nil
  57. }
  58. // extract key from secret using gjson
  59. val := gjson.Get(string(jsonStr), ref.Property)
  60. if !val.Exists() {
  61. return nil, esv1beta1.NoSecretError{}
  62. }
  63. return []byte(val.String()), nil
  64. }
  65. func (c *client) PushSecret(_ context.Context, _ []byte, _ corev1.SecretType, _ *apiextensionsv1.JSON, _ esv1beta1.PushRemoteRef) error {
  66. return errors.New("pushing secrets is not supported by Delinea DevOps Secrets Vault")
  67. }
  68. func (c *client) DeleteSecret(_ context.Context, _ esv1beta1.PushRemoteRef) error {
  69. return errors.New("deleting secrets is not supported by Delinea DevOps Secrets Vault")
  70. }
  71. func (c *client) Validate() (esv1beta1.ValidationResult, error) {
  72. return esv1beta1.ValidationResultReady, nil
  73. }
  74. // GetSecret gets the full secret as json-encoded value.
  75. func (c *client) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  76. secret, err := c.getSecret(ctx, ref)
  77. if err != nil {
  78. return nil, err
  79. }
  80. byteMap := make(map[string][]byte, len(secret.Data))
  81. for k := range secret.Data {
  82. byteMap[k], err = getTypedKey(secret.Data, k)
  83. if err != nil {
  84. return nil, err
  85. }
  86. }
  87. return byteMap, nil
  88. }
  89. // GetAllSecrets lists secrets matching the given criteria and return their latest versions.
  90. func (c *client) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  91. return nil, errors.New("getting all secrets is not supported by Delinea DevOps Secrets Vault")
  92. }
  93. func (c *client) Close(context.Context) error {
  94. return nil
  95. }
  96. // getSecret retrieves the secret referenced by ref from the Vault API.
  97. func (c *client) getSecret(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (*vault.Secret, error) {
  98. if ref.Version != "" {
  99. return nil, errors.New("specifying a version is not yet supported")
  100. }
  101. return c.api.Secret(ref.Key)
  102. }
  103. // getTypedKey is copied from pkg/provider/vault/vault.go.
  104. func getTypedKey(data map[string]interface{}, key string) ([]byte, error) {
  105. v, ok := data[key]
  106. if !ok {
  107. return nil, fmt.Errorf(errUnexpectedKey, key)
  108. }
  109. switch t := v.(type) {
  110. case string:
  111. return []byte(t), nil
  112. case map[string]interface{}:
  113. return json.Marshal(t)
  114. case []string:
  115. return []byte(strings.Join(t, "\n")), nil
  116. case []byte:
  117. return t, nil
  118. // also covers int and float32 due to json.Marshal
  119. case float64:
  120. return []byte(strconv.FormatFloat(t, 'f', -1, 64)), nil
  121. case json.Number:
  122. return []byte(t.String()), nil
  123. case []interface{}:
  124. return json.Marshal(t)
  125. case bool:
  126. return []byte(strconv.FormatBool(t)), nil
  127. case nil:
  128. return []byte(nil), nil
  129. default:
  130. return nil, fmt.Errorf(errSecretFormat, key, reflect.TypeOf(t))
  131. }
  132. }