client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "github.com/DelineaXPM/dsv-sdk-go/v2/vault"
  18. "github.com/tidwall/gjson"
  19. corev1 "k8s.io/api/core/v1"
  20. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  21. "github.com/external-secrets/external-secrets/pkg/utils"
  22. )
  23. type client struct {
  24. api secretAPI
  25. }
  26. var _ esv1beta1.SecretsClient = &client{}
  27. // GetSecret supports two types:
  28. // 1. get the full secret as json-encoded value
  29. // by leaving the ref.Property empty.
  30. // 2. get a key from the secret.
  31. // Nested values are supported by specifying a gjson expression
  32. func (c *client) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  33. secret, err := c.getSecret(ctx, ref)
  34. if err != nil {
  35. return nil, err
  36. }
  37. // Return nil if secret value is null
  38. if secret.Data == nil {
  39. return nil, nil
  40. }
  41. jsonStr, err := json.Marshal(secret.Data)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // return raw json if no property is defined
  46. if ref.Property == "" {
  47. return jsonStr, nil
  48. }
  49. // extract key from secret using gjson
  50. val := gjson.Get(string(jsonStr), ref.Property)
  51. if !val.Exists() {
  52. return nil, esv1beta1.NoSecretError{}
  53. }
  54. return []byte(val.String()), nil
  55. }
  56. func (c *client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  57. return errors.New("pushing secrets is not supported by Delinea DevOps Secrets Vault")
  58. }
  59. func (c *client) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  60. return errors.New("deleting secrets is not supported by Delinea DevOps Secrets Vault")
  61. }
  62. func (c *client) Validate() (esv1beta1.ValidationResult, error) {
  63. return esv1beta1.ValidationResultReady, nil
  64. }
  65. // GetSecret gets the full secret as json-encoded value.
  66. func (c *client) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  67. secret, err := c.getSecret(ctx, ref)
  68. if err != nil {
  69. return nil, err
  70. }
  71. byteMap := make(map[string][]byte, len(secret.Data))
  72. for k := range secret.Data {
  73. byteMap[k], err = utils.GetByteValueFromMap(secret.Data, k)
  74. if err != nil {
  75. return nil, err
  76. }
  77. }
  78. return byteMap, nil
  79. }
  80. // GetAllSecrets lists secrets matching the given criteria and return their latest versions.
  81. func (c *client) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  82. return nil, errors.New("getting all secrets is not supported by Delinea DevOps Secrets Vault")
  83. }
  84. func (c *client) Close(context.Context) error {
  85. return nil
  86. }
  87. // getSecret retrieves the secret referenced by ref from the Vault API.
  88. func (c *client) getSecret(_ context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (*vault.Secret, error) {
  89. if ref.Version != "" {
  90. return nil, errors.New("specifying a version is not yet supported")
  91. }
  92. return c.api.Secret(ref.Key)
  93. }