client.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package delinea
  14. import (
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "github.com/DelineaXPM/dsv-sdk-go/v2/vault"
  19. "github.com/tidwall/gjson"
  20. corev1 "k8s.io/api/core/v1"
  21. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  22. "github.com/external-secrets/external-secrets/pkg/utils"
  23. )
  24. type client struct {
  25. api secretAPI
  26. }
  27. var _ esv1.SecretsClient = &client{}
  28. // GetSecret supports two types:
  29. // 1. get the full secret as json-encoded value
  30. // by leaving the ref.Property empty.
  31. // 2. get a key from the secret.
  32. // Nested values are supported by specifying a gjson expression
  33. func (c *client) GetSecret(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  34. secret, err := c.getSecret(ctx, ref)
  35. if err != nil {
  36. return nil, err
  37. }
  38. // Return nil if secret value is null
  39. if secret.Data == nil {
  40. return nil, nil
  41. }
  42. jsonStr, err := json.Marshal(secret.Data)
  43. if err != nil {
  44. return nil, err
  45. }
  46. // return raw json if no property is defined
  47. if ref.Property == "" {
  48. return jsonStr, nil
  49. }
  50. // extract key from secret using gjson
  51. val := gjson.Get(string(jsonStr), ref.Property)
  52. if !val.Exists() {
  53. return nil, esv1.NoSecretError{}
  54. }
  55. return []byte(val.String()), nil
  56. }
  57. func (c *client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  58. return errors.New("pushing secrets is not supported by Delinea DevOps Secrets Vault")
  59. }
  60. func (c *client) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  61. return errors.New("deleting secrets is not supported by Delinea DevOps Secrets Vault")
  62. }
  63. func (c *client) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  64. return false, errors.New("not implemented")
  65. }
  66. func (c *client) Validate() (esv1.ValidationResult, error) {
  67. return esv1.ValidationResultReady, nil
  68. }
  69. // GetSecret gets the full secret as json-encoded value.
  70. func (c *client) GetSecretMap(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  71. secret, err := c.getSecret(ctx, ref)
  72. if err != nil {
  73. return nil, err
  74. }
  75. byteMap := make(map[string][]byte, len(secret.Data))
  76. for k := range secret.Data {
  77. byteMap[k], err = utils.GetByteValueFromMap(secret.Data, k)
  78. if err != nil {
  79. return nil, err
  80. }
  81. }
  82. return byteMap, nil
  83. }
  84. // GetAllSecrets lists secrets matching the given criteria and return their latest versions.
  85. func (c *client) GetAllSecrets(_ context.Context, _ esv1.ExternalSecretFind) (map[string][]byte, error) {
  86. return nil, errors.New("getting all secrets is not supported by Delinea DevOps Secrets Vault")
  87. }
  88. func (c *client) Close(context.Context) error {
  89. return nil
  90. }
  91. // getSecret retrieves the secret referenced by ref from the Vault API.
  92. func (c *client) getSecret(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) (*vault.Secret, error) {
  93. if ref.Version != "" {
  94. return nil, errors.New("specifying a version is not yet supported")
  95. }
  96. return c.api.Secret(ref.Key)
  97. }