provider.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package aws
  14. import (
  15. //nolint
  16. . "github.com/onsi/ginkgo/v2"
  17. // nolint
  18. . "github.com/onsi/gomega"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  21. "github.com/external-secrets/external-secrets-e2e/framework"
  22. "github.com/external-secrets/external-secrets-e2e/framework/log"
  23. awscommon "github.com/external-secrets/external-secrets-e2e/suites/provider/cases/aws"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. esmetav1 "github.com/external-secrets/external-secrets/apis/meta/v1"
  26. )
  27. type Provider struct {
  28. ServiceAccountName string
  29. ServiceAccountNamespace string
  30. backend *secretsManagerBackend
  31. region string
  32. framework *framework.Framework
  33. }
  34. func NewProvider(f *framework.Framework, kid, sak, st, region, saName, saNamespace string) *Provider {
  35. access := awsAccessConfig{
  36. KID: kid,
  37. SAK: sak,
  38. ST: st,
  39. Region: region,
  40. SAName: saName,
  41. SANamespace: saNamespace,
  42. }
  43. prov := &Provider{
  44. ServiceAccountName: saName,
  45. ServiceAccountNamespace: saNamespace,
  46. backend: newSecretsManagerBackend(f, access),
  47. region: region,
  48. framework: f,
  49. }
  50. BeforeEach(func() {
  51. skipIfAWSStaticCredentialsMissing(access)
  52. awscommon.SetupStaticStore(f, awscommon.AccessOpts{KID: kid, SAK: sak, ST: st, Region: region}, esv1.AWSServiceSecretsManager)
  53. awscommon.SetupExternalIDStore(
  54. f,
  55. awscommon.AccessOpts{KID: kid, SAK: sak, ST: st, Region: region, Role: awscommon.IAMRoleExternalID},
  56. awscommon.IAMTrustedExternalID,
  57. nil,
  58. esv1.AWSServiceSecretsManager,
  59. )
  60. awscommon.SetupSessionTagsStore(
  61. f,
  62. awscommon.AccessOpts{KID: kid, SAK: sak, ST: st, Region: region, Role: awscommon.IAMRoleSessionTags},
  63. nil,
  64. esv1.AWSServiceSecretsManager,
  65. )
  66. awscommon.CreateReferentStaticStore(f, awscommon.AccessOpts{KID: kid, SAK: sak, ST: st, Region: region}, esv1.AWSServiceSecretsManager)
  67. prov.SetupReferencedIRSAStore()
  68. prov.SetupMountedIRSAStore()
  69. })
  70. AfterEach(func() {
  71. prov.TeardownReferencedIRSAStore()
  72. prov.TeardownMountedIRSAStore()
  73. })
  74. return prov
  75. }
  76. func NewFromEnv(f *framework.Framework) *Provider {
  77. access := loadAWSAccessConfigFromEnv()
  78. return NewProvider(f, access.KID, access.SAK, access.ST, access.Region, access.SAName, access.SANamespace)
  79. }
  80. // CreateSecret creates a secret at the provider.
  81. func (s *Provider) CreateSecret(key string, val framework.SecretEntry) {
  82. s.backend.CreateSecret(key, val)
  83. }
  84. // DeleteSecret deletes a secret at the provider.
  85. // There may be a short delay between calling this function
  86. // and the removal of the secret on the provider side.
  87. func (s *Provider) DeleteSecret(key string) {
  88. s.backend.DeleteSecret(key)
  89. }
  90. // MountedIRSAStore is a SecretStore without auth config
  91. // ESO relies on the pod-mounted ServiceAccount when using this store.
  92. func (s *Provider) SetupMountedIRSAStore() {
  93. secretStore := &esv1.SecretStore{
  94. ObjectMeta: metav1.ObjectMeta{
  95. Name: awscommon.MountedIRSAStoreName(s.framework),
  96. Namespace: s.framework.Namespace.Name,
  97. },
  98. Spec: esv1.SecretStoreSpec{
  99. Provider: &esv1.SecretStoreProvider{
  100. AWS: &esv1.AWSProvider{
  101. Service: esv1.AWSServiceSecretsManager,
  102. Region: s.region,
  103. Auth: esv1.AWSAuth{},
  104. },
  105. },
  106. },
  107. }
  108. err := s.framework.CRClient.Create(GinkgoT().Context(), secretStore)
  109. Expect(err).ToNot(HaveOccurred())
  110. }
  111. func (s *Provider) TeardownMountedIRSAStore() {
  112. s.framework.CRClient.Delete(GinkgoT().Context(), &esv1.ClusterSecretStore{
  113. ObjectMeta: metav1.ObjectMeta{
  114. Name: awscommon.MountedIRSAStoreName(s.framework),
  115. },
  116. })
  117. }
  118. // ReferncedIRSAStore is a ClusterStore
  119. // that references a (IRSA-) ServiceAccount in the default namespace.
  120. func (s *Provider) SetupReferencedIRSAStore() {
  121. log.Logf("creating IRSA ClusterSecretStore %s", s.framework.Namespace.Name)
  122. secretStore := &esv1.ClusterSecretStore{
  123. ObjectMeta: metav1.ObjectMeta{
  124. Name: awscommon.ReferencedIRSAStoreName(s.framework),
  125. },
  126. }
  127. _, err := controllerutil.CreateOrUpdate(GinkgoT().Context(), s.framework.CRClient, secretStore, func() error {
  128. secretStore.Spec.Provider = &esv1.SecretStoreProvider{
  129. AWS: &esv1.AWSProvider{
  130. Service: esv1.AWSServiceSecretsManager,
  131. Region: s.region,
  132. Auth: esv1.AWSAuth{
  133. JWTAuth: &esv1.AWSJWTAuth{
  134. ServiceAccountRef: &esmetav1.ServiceAccountSelector{
  135. Name: s.ServiceAccountName,
  136. Namespace: &s.ServiceAccountNamespace,
  137. },
  138. },
  139. },
  140. },
  141. }
  142. return nil
  143. })
  144. Expect(err).ToNot(HaveOccurred())
  145. }
  146. func (s *Provider) TeardownReferencedIRSAStore() {
  147. s.framework.CRClient.Delete(GinkgoT().Context(), &esv1.ClusterSecretStore{
  148. ObjectMeta: metav1.ObjectMeta{
  149. Name: awscommon.ReferencedIRSAStoreName(s.framework),
  150. },
  151. })
  152. }