client_get_secret_map.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 ovh
  14. import (
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. "github.com/external-secrets/external-secrets/runtime/esutils"
  21. )
  22. const retrieveSecretError = "failed to retrieve secret at path"
  23. // GetSecretMap retrieves a single secret from the provider.
  24. // The created secret will have the same keys as the Secret Manager secret.
  25. // You can specify a key, a property, and a version.
  26. // If a property is provided, it should reference only nested values.
  27. func (cl *ovhClient) GetSecretMap(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  28. // Retrieve secret from KMS.
  29. secretDataBytes, _, err := cl.getSecretWithOvhSDK(ctx, cl.okmsID, ref)
  30. if err != nil {
  31. if errors.Is(err, esv1.NoSecretErr) {
  32. return map[string][]byte{}, err
  33. }
  34. return map[string][]byte{}, wrapRetrieveSecretError(ref.Key, err)
  35. }
  36. if len(secretDataBytes) == 0 {
  37. return map[string][]byte{}, nil
  38. }
  39. // Unmarshal the secret value into a map[string]any
  40. // so it can be passed to esutils.GetByteValueFromMap.
  41. var rawSecretDataMap map[string]any
  42. err = json.Unmarshal(secretDataBytes, &rawSecretDataMap)
  43. if err != nil {
  44. return map[string][]byte{}, wrapRetrieveSecretError(ref.Key, err)
  45. }
  46. // Convert the map[string]any into map[string][]byte.
  47. secretDataMap := make(map[string][]byte, len(rawSecretDataMap))
  48. for key := range rawSecretDataMap {
  49. secretDataMap[key], err = esutils.GetByteValueFromMap(rawSecretDataMap, key)
  50. if err != nil {
  51. return map[string][]byte{}, wrapRetrieveSecretError(ref.Key, err)
  52. }
  53. }
  54. return secretDataMap, nil
  55. }
  56. func wrapRetrieveSecretError(key string, err error) error {
  57. return fmt.Errorf("%s %q: %w", retrieveSecretError, key, err)
  58. }