provider.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "os"
  16. "time"
  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/secretsmanager"
  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. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  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 *secretsmanager.SecretsManager
  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 := secretsmanager.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, esv1alpha1.AWSServiceSecretsManager)
  60. prov.SetupReferencedIRSAStore()
  61. prov.SetupMountedIRSAStore()
  62. })
  63. AfterEach(func() {
  64. // Cleanup ClusterSecretStore
  65. err := prov.framework.CRClient.Delete(context.Background(), &esv1alpha1.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, val string) {
  84. // we re-use some secret names throughout our test suite
  85. // due to the fact that there is a short delay before the secret is actually deleted
  86. // we have to retry creating the secret
  87. attempts := 20
  88. for {
  89. log.Logf("creating secret %s / attempts left: %d", key, attempts)
  90. _, err := s.client.CreateSecret(&secretsmanager.CreateSecretInput{
  91. Name: aws.String(key),
  92. SecretString: aws.String(val),
  93. })
  94. if err == nil {
  95. return
  96. }
  97. attempts--
  98. if attempts < 0 {
  99. Fail("unable to create secret: " + err.Error())
  100. }
  101. <-time.After(time.Second * 5)
  102. }
  103. }
  104. // DeleteSecret deletes a secret at the provider.
  105. // There may be a short delay between calling this function
  106. // and the removal of the secret on the provider side.
  107. func (s *Provider) DeleteSecret(key string) {
  108. log.Logf("deleting secret %s", key)
  109. _, err := s.client.DeleteSecret(&secretsmanager.DeleteSecretInput{
  110. SecretId: aws.String(key),
  111. ForceDeleteWithoutRecovery: aws.Bool(true),
  112. })
  113. Expect(err).ToNot(HaveOccurred())
  114. }
  115. // MountedIRSAStore is a SecretStore without auth config
  116. // ESO relies on the pod-mounted ServiceAccount when using this store.
  117. func (s *Provider) SetupMountedIRSAStore() {
  118. secretStore := &esv1alpha1.SecretStore{
  119. ObjectMeta: metav1.ObjectMeta{
  120. Name: common.MountedIRSAStoreName(s.framework),
  121. Namespace: s.framework.Namespace.Name,
  122. },
  123. Spec: esv1alpha1.SecretStoreSpec{
  124. Provider: &esv1alpha1.SecretStoreProvider{
  125. AWS: &esv1alpha1.AWSProvider{
  126. Service: esv1alpha1.AWSServiceSecretsManager,
  127. Region: s.region,
  128. Auth: esv1alpha1.AWSAuth{},
  129. },
  130. },
  131. },
  132. }
  133. err := s.framework.CRClient.Create(context.Background(), secretStore)
  134. Expect(err).ToNot(HaveOccurred())
  135. }
  136. // ReferncedIRSAStore is a ClusterStore
  137. // that references a (IRSA-) ServiceAccount in the default namespace.
  138. func (s *Provider) SetupReferencedIRSAStore() {
  139. log.Logf("creating IRSA ClusterSecretStore %s", s.framework.Namespace.Name)
  140. secretStore := &esv1alpha1.ClusterSecretStore{
  141. ObjectMeta: metav1.ObjectMeta{
  142. Name: common.ReferencedIRSAStoreName(s.framework),
  143. },
  144. }
  145. _, err := controllerutil.CreateOrUpdate(context.Background(), s.framework.CRClient, secretStore, func() error {
  146. secretStore.Spec.Provider = &esv1alpha1.SecretStoreProvider{
  147. AWS: &esv1alpha1.AWSProvider{
  148. Service: esv1alpha1.AWSServiceSecretsManager,
  149. Region: s.region,
  150. Auth: esv1alpha1.AWSAuth{
  151. JWTAuth: &esv1alpha1.AWSJWTAuth{
  152. ServiceAccountRef: &esmetav1.ServiceAccountSelector{
  153. Name: s.ServiceAccountName,
  154. Namespace: &s.ServiceAccountNamespace,
  155. },
  156. },
  157. },
  158. },
  159. }
  160. return nil
  161. })
  162. Expect(err).ToNot(HaveOccurred())
  163. }