provider.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/aws"
  18. "github.com/aws/aws-sdk-go/aws/credentials"
  19. "github.com/aws/aws-sdk-go/aws/session"
  20. "github.com/aws/aws-sdk-go/service/ssm"
  21. //nolint
  22. . "github.com/onsi/ginkgo/v2"
  23. // nolint
  24. . "github.com/onsi/gomega"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  27. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  28. esmetav1 "github.com/external-secrets/external-secrets/apis/meta/v1"
  29. "github.com/external-secrets/external-secrets/e2e/framework"
  30. "github.com/external-secrets/external-secrets/e2e/framework/log"
  31. common "github.com/external-secrets/external-secrets/e2e/suite/aws"
  32. )
  33. type Provider struct {
  34. ServiceAccountName string
  35. ServiceAccountNamespace string
  36. region string
  37. client *ssm.SSM
  38. framework *framework.Framework
  39. }
  40. func NewProvider(f *framework.Framework, kid, sak, region, saName, saNamespace string) *Provider {
  41. sess, err := session.NewSessionWithOptions(session.Options{
  42. Config: aws.Config{
  43. Credentials: credentials.NewStaticCredentials(kid, sak, ""),
  44. Region: aws.String(region),
  45. },
  46. })
  47. if err != nil {
  48. Fail(err.Error())
  49. }
  50. sm := ssm.New(sess)
  51. prov := &Provider{
  52. ServiceAccountName: saName,
  53. ServiceAccountNamespace: saNamespace,
  54. region: region,
  55. client: sm,
  56. framework: f,
  57. }
  58. BeforeEach(func() {
  59. common.SetupStaticStore(f, kid, sak, region, esv1beta1.AWSServiceParameterStore)
  60. prov.SetupReferencedIRSAStore()
  61. prov.SetupMountedIRSAStore()
  62. })
  63. AfterEach(func() {
  64. // Cleanup ClusterSecretStore
  65. err := prov.framework.CRClient.Delete(context.Background(), &esv1beta1.ClusterSecretStore{
  66. ObjectMeta: metav1.ObjectMeta{
  67. Name: common.ReferencedIRSAStoreName(f),
  68. },
  69. })
  70. Expect(err).ToNot(HaveOccurred())
  71. })
  72. return prov
  73. }
  74. func NewFromEnv(f *framework.Framework) *Provider {
  75. kid := os.Getenv("AWS_ACCESS_KEY_ID")
  76. sak := os.Getenv("AWS_SECRET_ACCESS_KEY")
  77. region := "eu-west-1"
  78. saName := os.Getenv("AWS_SA_NAME")
  79. saNamespace := os.Getenv("AWS_SA_NAMESPACE")
  80. return NewProvider(f, kid, sak, region, saName, saNamespace)
  81. }
  82. // CreateSecret creates a secret at the provider.
  83. func (s *Provider) CreateSecret(key string, val framework.SecretEntry) {
  84. pmTags := make([]*ssm.Tag, 0)
  85. for k, v := range val.Tags {
  86. pmTags = append(pmTags, &ssm.Tag{
  87. Key: aws.String(k),
  88. Value: aws.String(v),
  89. })
  90. }
  91. _, err := s.client.PutParameter(&ssm.PutParameterInput{
  92. Name: aws.String(key),
  93. Value: aws.String(val.Value),
  94. DataType: aws.String("text"),
  95. Type: aws.String("String"),
  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(&ssm.DeleteParameterInput{
  103. Name: aws.String(key),
  104. })
  105. var nf *ssm.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 := &esv1beta1.SecretStore{
  115. ObjectMeta: metav1.ObjectMeta{
  116. Name: common.MountedIRSAStoreName(s.framework),
  117. Namespace: s.framework.Namespace.Name,
  118. },
  119. Spec: esv1beta1.SecretStoreSpec{
  120. Provider: &esv1beta1.SecretStoreProvider{
  121. AWS: &esv1beta1.AWSProvider{
  122. Service: esv1beta1.AWSServiceParameterStore,
  123. Region: s.region,
  124. Auth: esv1beta1.AWSAuth{},
  125. },
  126. },
  127. },
  128. }
  129. err := s.framework.CRClient.Create(context.Background(), secretStore)
  130. Expect(err).ToNot(HaveOccurred())
  131. }
  132. // ReferncedIRSAStore is a ClusterStore
  133. // that references a (IRSA-) ServiceAccount in the default namespace.
  134. func (s *Provider) SetupReferencedIRSAStore() {
  135. log.Logf("creating IRSA ClusterSecretStore %s", s.framework.Namespace.Name)
  136. secretStore := &esv1beta1.ClusterSecretStore{
  137. ObjectMeta: metav1.ObjectMeta{
  138. Name: common.ReferencedIRSAStoreName(s.framework),
  139. },
  140. }
  141. _, err := controllerutil.CreateOrUpdate(context.Background(), s.framework.CRClient, secretStore, func() error {
  142. secretStore.Spec.Provider = &esv1beta1.SecretStoreProvider{
  143. AWS: &esv1beta1.AWSProvider{
  144. Service: esv1beta1.AWSServiceParameterStore,
  145. Region: s.region,
  146. Auth: esv1beta1.AWSAuth{
  147. JWTAuth: &esv1beta1.AWSJWTAuth{
  148. ServiceAccountRef: &esmetav1.ServiceAccountSelector{
  149. Name: s.ServiceAccountName,
  150. Namespace: &s.ServiceAccountNamespace,
  151. },
  152. },
  153. },
  154. },
  155. }
  156. return nil
  157. })
  158. Expect(err).ToNot(HaveOccurred())
  159. }