client.go 4.3 KB

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