provider.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 aws
  13. import (
  14. "context"
  15. "errors"
  16. "os"
  17. "github.com/aws/aws-sdk-go-v2/aws"
  18. "github.com/aws/aws-sdk-go-v2/config"
  19. "github.com/aws/aws-sdk-go-v2/credentials"
  20. "github.com/aws/aws-sdk-go-v2/service/ssm"
  21. ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types"
  22. //nolint
  23. . "github.com/onsi/ginkgo/v2"
  24. // nolint
  25. . "github.com/onsi/gomega"
  26. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  28. "github.com/external-secrets/external-secrets-e2e/framework"
  29. "github.com/external-secrets/external-secrets-e2e/framework/log"
  30. awscommon "github.com/external-secrets/external-secrets-e2e/suites/provider/cases/aws"
  31. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  32. esmetav1 "github.com/external-secrets/external-secrets/apis/meta/v1"
  33. )
  34. type Provider struct {
  35. ServiceAccountName string
  36. ServiceAccountNamespace string
  37. region string
  38. client *ssm.Client
  39. framework *framework.Framework
  40. }
  41. func NewProvider(f *framework.Framework, kid, sak, st, region, saName, saNamespace string) *Provider {
  42. config, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(kid, sak, st)))
  43. if err != nil {
  44. Fail(err.Error())
  45. }
  46. sm := ssm.NewFromConfig(config)
  47. prov := &Provider{
  48. ServiceAccountName: saName,
  49. ServiceAccountNamespace: saNamespace,
  50. region: region,
  51. client: sm,
  52. framework: f,
  53. }
  54. BeforeEach(func() {
  55. awscommon.SetupStaticStore(f, awscommon.AccessOpts{KID: kid, SAK: sak, ST: st, Region: region}, esv1.AWSServiceParameterStore)
  56. awscommon.CreateReferentStaticStore(f, awscommon.AccessOpts{KID: kid, SAK: sak, ST: st, Region: region}, esv1.AWSServiceParameterStore)
  57. prov.SetupReferencedIRSAStore()
  58. prov.SetupMountedIRSAStore()
  59. })
  60. AfterEach(func() {
  61. prov.TeardownReferencedIRSAStore()
  62. prov.TeardownMountedIRSAStore()
  63. })
  64. return prov
  65. }
  66. func NewFromEnv(f *framework.Framework) *Provider {
  67. kid := os.Getenv("AWS_ACCESS_KEY_ID")
  68. sak := os.Getenv("AWS_SECRET_ACCESS_KEY")
  69. st := os.Getenv("AWS_SESSION_TOKEN")
  70. region := os.Getenv("AWS_REGION")
  71. saName := os.Getenv("AWS_SA_NAME")
  72. saNamespace := os.Getenv("AWS_SA_NAMESPACE")
  73. return NewProvider(f, kid, sak, st, region, saName, saNamespace)
  74. }
  75. // CreateSecret creates a secret at the provider.
  76. func (s *Provider) CreateSecret(key string, val framework.SecretEntry) {
  77. pmTags := make([]ssmtypes.Tag, 0)
  78. for k, v := range val.Tags {
  79. pmTags = append(pmTags, ssmtypes.Tag{
  80. Key: aws.String(k),
  81. Value: aws.String(v),
  82. })
  83. }
  84. // tags and overwrite can't be used together
  85. // overwrite is needed for the version test
  86. overwrite := false
  87. if len(val.Tags) == 0 {
  88. overwrite = true
  89. }
  90. _, err := s.client.PutParameter(context.Background(), &ssm.PutParameterInput{
  91. Name: aws.String(key),
  92. Value: aws.String(val.Value),
  93. DataType: aws.String("text"),
  94. Type: ssmtypes.ParameterTypeString,
  95. Overwrite: aws.Bool(overwrite),
  96. Tags: pmTags,
  97. })
  98. Expect(err).ToNot(HaveOccurred())
  99. }
  100. // DeleteSecret deletes a secret at the provider.
  101. func (s *Provider) DeleteSecret(key string) {
  102. _, err := s.client.DeleteParameter(context.Background(), &ssm.DeleteParameterInput{
  103. Name: aws.String(key),
  104. })
  105. var nf *ssmtypes.ParameterNotFound
  106. if errors.As(err, &nf) {
  107. return
  108. }
  109. Expect(err).ToNot(HaveOccurred())
  110. }
  111. // MountedIRSAStore is a SecretStore without auth config
  112. // ESO relies on the pod-mounted ServiceAccount when using this store.
  113. func (s *Provider) SetupMountedIRSAStore() {
  114. secretStore := &esv1.SecretStore{
  115. ObjectMeta: metav1.ObjectMeta{
  116. Name: awscommon.MountedIRSAStoreName(s.framework),
  117. Namespace: s.framework.Namespace.Name,
  118. },
  119. Spec: esv1.SecretStoreSpec{
  120. Provider: &esv1.SecretStoreProvider{
  121. AWS: &esv1.AWSProvider{
  122. Service: esv1.AWSServiceParameterStore,
  123. Region: s.region,
  124. Auth: esv1.AWSAuth{},
  125. },
  126. },
  127. },
  128. }
  129. err := s.framework.CRClient.Create(context.Background(), secretStore)
  130. Expect(err).ToNot(HaveOccurred())
  131. }
  132. func (s *Provider) TeardownMountedIRSAStore() {
  133. s.framework.CRClient.Delete(context.Background(), &esv1.ClusterSecretStore{
  134. ObjectMeta: metav1.ObjectMeta{
  135. Name: awscommon.MountedIRSAStoreName(s.framework),
  136. },
  137. })
  138. }
  139. // ReferncedIRSAStore is a ClusterStore
  140. // that references a (IRSA-) ServiceAccount in the default namespace.
  141. func (s *Provider) SetupReferencedIRSAStore() {
  142. log.Logf("creating IRSA ClusterSecretStore %s", s.framework.Namespace.Name)
  143. secretStore := &esv1.ClusterSecretStore{
  144. ObjectMeta: metav1.ObjectMeta{
  145. Name: awscommon.ReferencedIRSAStoreName(s.framework),
  146. },
  147. }
  148. _, err := controllerutil.CreateOrUpdate(context.Background(), s.framework.CRClient, secretStore, func() error {
  149. secretStore.Spec.Provider = &esv1.SecretStoreProvider{
  150. AWS: &esv1.AWSProvider{
  151. Service: esv1.AWSServiceParameterStore,
  152. Region: s.region,
  153. Auth: esv1.AWSAuth{
  154. JWTAuth: &esv1.AWSJWTAuth{
  155. ServiceAccountRef: &esmetav1.ServiceAccountSelector{
  156. Name: s.ServiceAccountName,
  157. Namespace: &s.ServiceAccountNamespace,
  158. },
  159. },
  160. },
  161. },
  162. }
  163. return nil
  164. })
  165. Expect(err).ToNot(HaveOccurred())
  166. }
  167. func (s *Provider) TeardownReferencedIRSAStore() {
  168. s.framework.CRClient.Delete(context.Background(), &esv1.ClusterSecretStore{
  169. ObjectMeta: metav1.ObjectMeta{
  170. Name: awscommon.ReferencedIRSAStoreName(s.framework),
  171. },
  172. })
  173. }