parameterstore.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 parameterstore
  13. import (
  14. "context"
  15. "encoding/json"
  16. "fmt"
  17. "github.com/aws/aws-sdk-go/aws"
  18. "github.com/aws/aws-sdk-go/aws/client"
  19. "github.com/aws/aws-sdk-go/service/ssm"
  20. "github.com/tidwall/gjson"
  21. ctrl "sigs.k8s.io/controller-runtime"
  22. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  23. "github.com/external-secrets/external-secrets/pkg/provider/aws/util"
  24. )
  25. // ParameterStore is a provider for AWS ParameterStore.
  26. type ParameterStore struct {
  27. client PMInterface
  28. }
  29. // PMInterface is a subset of the parameterstore api.
  30. // see: https://docs.aws.amazon.com/sdk-for-go/api/service/ssm/ssmiface/
  31. type PMInterface interface {
  32. GetParameter(*ssm.GetParameterInput) (*ssm.GetParameterOutput, error)
  33. }
  34. var log = ctrl.Log.WithName("provider").WithName("aws").WithName("parameterstore")
  35. // New constructs a ParameterStore Provider that is specific to a store.
  36. func New(sess client.ConfigProvider) (*ParameterStore, error) {
  37. return &ParameterStore{
  38. client: ssm.New(sess),
  39. }, nil
  40. }
  41. // GetSecret returns a single secret from the provider.
  42. func (pm *ParameterStore) GetSecret(ctx context.Context, ref esv1alpha1.ExternalSecretDataRemoteRef) ([]byte, error) {
  43. log.Info("fetching secret value", "key", ref.Key)
  44. out, err := pm.client.GetParameter(&ssm.GetParameterInput{
  45. Name: &ref.Key,
  46. WithDecryption: aws.Bool(true),
  47. })
  48. if err != nil {
  49. return nil, util.SanitizeErr(err)
  50. }
  51. if ref.Property == "" {
  52. if out.Parameter.Value != nil {
  53. return []byte(*out.Parameter.Value), nil
  54. }
  55. return nil, fmt.Errorf("invalid secret received. parameter value is nil for key: %s", ref.Key)
  56. }
  57. val := gjson.Get(*out.Parameter.Value, ref.Property)
  58. if !val.Exists() {
  59. return nil, fmt.Errorf("key %s does not exist in secret %s", ref.Property, ref.Key)
  60. }
  61. return []byte(val.String()), nil
  62. }
  63. // Implements store.Client.GetAllSecrets Interface.
  64. // New version of GetAllSecrets.
  65. func (pm *ParameterStore) GetAllSecrets(ctx context.Context, ref esv1alpha1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  66. // TO be implemented
  67. return map[string][]byte{}, nil
  68. }
  69. // GetSecretMap returns multiple k/v pairs from the provider.
  70. func (pm *ParameterStore) GetSecretMap(ctx context.Context, ref esv1alpha1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  71. log.Info("fetching secret map", "key", ref.Key)
  72. data, err := pm.GetSecret(ctx, ref)
  73. if err != nil {
  74. return nil, err
  75. }
  76. kv := make(map[string]string)
  77. err = json.Unmarshal(data, &kv)
  78. if err != nil {
  79. return nil, fmt.Errorf("unable to unmarshal secret %s: %w", ref.Key, err)
  80. }
  81. secretData := make(map[string][]byte)
  82. for k, v := range kv {
  83. secretData[k] = []byte(v)
  84. }
  85. return secretData, nil
  86. }
  87. func (pm *ParameterStore) Close(ctx context.Context) error {
  88. return nil
  89. }