parameterstore.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/session"
  19. "github.com/aws/aws-sdk-go/service/ssm"
  20. "github.com/tidwall/gjson"
  21. ctrl "sigs.k8s.io/controller-runtime"
  22. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  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. sess *session.Session
  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 *session.Session) (*ParameterStore, error) {
  38. return &ParameterStore{
  39. sess: sess,
  40. client: ssm.New(sess),
  41. }, nil
  42. }
  43. // Empty GetAllSecrets.
  44. func (pm *ParameterStore) GetAllSecrets(ctx context.Context, ref esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  45. // TO be implemented
  46. return nil, fmt.Errorf("GetAllSecrets not implemented")
  47. }
  48. // GetSecret returns a single secret from the provider.
  49. func (pm *ParameterStore) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  50. log.Info("fetching secret value", "key", ref.Key)
  51. out, err := pm.client.GetParameter(&ssm.GetParameterInput{
  52. Name: &ref.Key,
  53. WithDecryption: aws.Bool(true),
  54. })
  55. if err != nil {
  56. return nil, util.SanitizeErr(err)
  57. }
  58. if ref.Property == "" {
  59. if out.Parameter.Value != nil {
  60. return []byte(*out.Parameter.Value), nil
  61. }
  62. return nil, fmt.Errorf("invalid secret received. parameter value is nil for key: %s", ref.Key)
  63. }
  64. val := gjson.Get(*out.Parameter.Value, ref.Property)
  65. if !val.Exists() {
  66. return nil, fmt.Errorf("key %s does not exist in secret %s", ref.Property, ref.Key)
  67. }
  68. return []byte(val.String()), nil
  69. }
  70. // GetSecretMap returns multiple k/v pairs from the provider.
  71. func (pm *ParameterStore) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  72. log.Info("fetching secret map", "key", ref.Key)
  73. data, err := pm.GetSecret(ctx, ref)
  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.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. }
  91. func (pm *ParameterStore) Validate() error {
  92. _, err := pm.sess.Config.Credentials.Get()
  93. return err
  94. }