parameterstore.go 3.4 KB

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