provider.go 5.8 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. "time"
  18. "github.com/aws/aws-sdk-go/aws"
  19. "github.com/aws/aws-sdk-go/aws/credentials"
  20. "github.com/aws/aws-sdk-go/aws/session"
  21. "github.com/aws/aws-sdk-go/service/secretsmanager"
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  29. esmetav1 "github.com/external-secrets/external-secrets/apis/meta/v1"
  30. "github.com/external-secrets/external-secrets/e2e/framework"
  31. "github.com/external-secrets/external-secrets/e2e/framework/log"
  32. common "github.com/external-secrets/external-secrets/e2e/suite/aws"
  33. )
  34. type Provider struct {
  35. ServiceAccountName string
  36. ServiceAccountNamespace string
  37. region string
  38. client *secretsmanager.SecretsManager
  39. framework *framework.Framework
  40. }
  41. func NewProvider(f *framework.Framework, kid, sak, region, saName, saNamespace string) *Provider {
  42. sess, err := session.NewSessionWithOptions(session.Options{
  43. Config: aws.Config{
  44. Credentials: credentials.NewStaticCredentials(kid, sak, ""),
  45. Region: aws.String(region),
  46. },
  47. })
  48. if err != nil {
  49. Fail(err.Error())
  50. }
  51. sm := secretsmanager.New(sess)
  52. prov := &Provider{
  53. ServiceAccountName: saName,
  54. ServiceAccountNamespace: saNamespace,
  55. region: region,
  56. client: sm,
  57. framework: f,
  58. }
  59. BeforeEach(func() {
  60. common.SetupStaticStore(f, kid, sak, region, esv1beta1.AWSServiceSecretsManager)
  61. prov.SetupReferencedIRSAStore()
  62. prov.SetupMountedIRSAStore()
  63. })
  64. AfterEach(func() {
  65. // Cleanup ClusterSecretStore
  66. err := prov.framework.CRClient.Delete(context.Background(), &esv1beta1.ClusterSecretStore{
  67. ObjectMeta: metav1.ObjectMeta{
  68. Name: common.ReferencedIRSAStoreName(f),
  69. },
  70. })
  71. Expect(err).ToNot(HaveOccurred())
  72. })
  73. return prov
  74. }
  75. func NewFromEnv(f *framework.Framework) *Provider {
  76. kid := os.Getenv("AWS_ACCESS_KEY_ID")
  77. sak := os.Getenv("AWS_SECRET_ACCESS_KEY")
  78. region := "eu-west-1"
  79. saName := os.Getenv("AWS_SA_NAME")
  80. saNamespace := os.Getenv("AWS_SA_NAMESPACE")
  81. return NewProvider(f, kid, sak, region, saName, saNamespace)
  82. }
  83. // CreateSecret creates a secret at the provider.
  84. func (s *Provider) CreateSecret(key string, val framework.SecretEntry) {
  85. smTags := make([]*secretsmanager.Tag, 0)
  86. for k, v := range val.Tags {
  87. smTags = append(smTags, &secretsmanager.Tag{
  88. Key: aws.String(k),
  89. Value: aws.String(v),
  90. })
  91. }
  92. // we re-use some secret names throughout our test suite
  93. // due to the fact that there is a short delay before the secret is actually deleted
  94. // we have to retry creating the secret
  95. attempts := 20
  96. for {
  97. log.Logf("creating secret %s / attempts left: %d", key, attempts)
  98. _, err := s.client.CreateSecret(&secretsmanager.CreateSecretInput{
  99. Name: aws.String(key),
  100. SecretString: aws.String(val.Value),
  101. Tags: smTags,
  102. })
  103. if err == nil {
  104. return
  105. }
  106. attempts--
  107. if attempts < 0 {
  108. Fail("unable to create secret: " + err.Error())
  109. }
  110. <-time.After(time.Second * 5)
  111. }
  112. }
  113. // DeleteSecret deletes a secret at the provider.
  114. // There may be a short delay between calling this function
  115. // and the removal of the secret on the provider side.
  116. func (s *Provider) DeleteSecret(key string) {
  117. log.Logf("deleting secret %s", key)
  118. _, err := s.client.DeleteSecret(&secretsmanager.DeleteSecretInput{
  119. SecretId: aws.String(key),
  120. ForceDeleteWithoutRecovery: aws.Bool(true),
  121. })
  122. var nf *secretsmanager.ResourceNotFoundException
  123. if errors.As(err, &nf) {
  124. return
  125. }
  126. Expect(err).ToNot(HaveOccurred())
  127. }
  128. // MountedIRSAStore is a SecretStore without auth config
  129. // ESO relies on the pod-mounted ServiceAccount when using this store.
  130. func (s *Provider) SetupMountedIRSAStore() {
  131. secretStore := &esv1beta1.SecretStore{
  132. ObjectMeta: metav1.ObjectMeta{
  133. Name: common.MountedIRSAStoreName(s.framework),
  134. Namespace: s.framework.Namespace.Name,
  135. },
  136. Spec: esv1beta1.SecretStoreSpec{
  137. Provider: &esv1beta1.SecretStoreProvider{
  138. AWS: &esv1beta1.AWSProvider{
  139. Service: esv1beta1.AWSServiceSecretsManager,
  140. Region: s.region,
  141. Auth: esv1beta1.AWSAuth{},
  142. },
  143. },
  144. },
  145. }
  146. err := s.framework.CRClient.Create(context.Background(), secretStore)
  147. Expect(err).ToNot(HaveOccurred())
  148. }
  149. // ReferncedIRSAStore is a ClusterStore
  150. // that references a (IRSA-) ServiceAccount in the default namespace.
  151. func (s *Provider) SetupReferencedIRSAStore() {
  152. log.Logf("creating IRSA ClusterSecretStore %s", s.framework.Namespace.Name)
  153. secretStore := &esv1beta1.ClusterSecretStore{
  154. ObjectMeta: metav1.ObjectMeta{
  155. Name: common.ReferencedIRSAStoreName(s.framework),
  156. },
  157. }
  158. _, err := controllerutil.CreateOrUpdate(context.Background(), s.framework.CRClient, secretStore, func() error {
  159. secretStore.Spec.Provider = &esv1beta1.SecretStoreProvider{
  160. AWS: &esv1beta1.AWSProvider{
  161. Service: esv1beta1.AWSServiceSecretsManager,
  162. Region: s.region,
  163. Auth: esv1beta1.AWSAuth{
  164. JWTAuth: &esv1beta1.AWSJWTAuth{
  165. ServiceAccountRef: &esmetav1.ServiceAccountSelector{
  166. Name: s.ServiceAccountName,
  167. Namespace: &s.ServiceAccountNamespace,
  168. },
  169. },
  170. },
  171. },
  172. }
  173. return nil
  174. })
  175. Expect(err).ToNot(HaveOccurred())
  176. }