provider.go 5.7 KB

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