client.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright © The ESO Authors
  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 implements a provider for Delinea DevOps Secrets Vault.
  14. // It provides functionality to interact with secrets stored in Delinea DSV,
  15. // supporting operations like fetching secrets and managing secret lifecycles.
  16. package delinea
  17. import (
  18. "context"
  19. "encoding/json"
  20. "errors"
  21. "github.com/DelineaXPM/dsv-sdk-go/v2/vault"
  22. "github.com/tidwall/gjson"
  23. corev1 "k8s.io/api/core/v1"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. "github.com/external-secrets/external-secrets/runtime/esutils"
  26. )
  27. type client struct {
  28. api secretAPI
  29. }
  30. var _ esv1.SecretsClient = &client{}
  31. // GetSecret supports two types:
  32. // 1. get the full secret as json-encoded value
  33. // by leaving the ref.Property empty.
  34. // 2. get a key from the secret.
  35. // Nested values are supported by specifying a gjson expression
  36. func (c *client) GetSecret(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  37. secret, err := c.getSecret(ctx, ref)
  38. if err != nil {
  39. return nil, err
  40. }
  41. // Return nil if secret value is null
  42. if secret.Data == nil {
  43. return nil, nil
  44. }
  45. jsonStr, err := json.Marshal(secret.Data)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // return raw json if no property is defined
  50. if ref.Property == "" {
  51. return jsonStr, nil
  52. }
  53. // extract key from secret using gjson
  54. val := gjson.Get(string(jsonStr), ref.Property)
  55. if !val.Exists() {
  56. return nil, esv1.NoSecretError{}
  57. }
  58. return []byte(val.String()), nil
  59. }
  60. func (c *client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  61. return errors.New("pushing secrets is not supported by Delinea DevOps Secrets Vault")
  62. }
  63. func (c *client) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  64. return errors.New("deleting secrets is not supported by Delinea DevOps Secrets Vault")
  65. }
  66. func (c *client) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  67. return false, errors.New("not implemented")
  68. }
  69. func (c *client) Validate() (esv1.ValidationResult, error) {
  70. return esv1.ValidationResultReady, nil
  71. }
  72. // GetSecretMap retrieves all key-value pairs from the secret referenced by ref.
  73. func (c *client) GetSecretMap(ctx context.Context, ref esv1.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 = esutils.GetByteValueFromMap(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, _ esv1.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 esv1.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. }