provider.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. v1 "k8s.io/api/core/v1"
  26. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  28. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  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. )
  33. type SMProvider struct {
  34. ServiceAccountName string
  35. ServiceAccountNamespace string
  36. kid string
  37. sak string
  38. region string
  39. client *secretsmanager.SecretsManager
  40. framework *framework.Framework
  41. }
  42. const (
  43. staticCredentialsSecretName = "provider-secret"
  44. )
  45. func NewSMProvider(f *framework.Framework, kid, sak, region, saName, saNamespace string) *SMProvider {
  46. sess, err := session.NewSessionWithOptions(session.Options{
  47. Config: aws.Config{
  48. Credentials: credentials.NewStaticCredentials(kid, sak, ""),
  49. Region: aws.String(region),
  50. },
  51. })
  52. if err != nil {
  53. Fail(err.Error())
  54. }
  55. sm := secretsmanager.New(sess)
  56. prov := &SMProvider{
  57. ServiceAccountName: saName,
  58. ServiceAccountNamespace: saNamespace,
  59. kid: kid,
  60. sak: sak,
  61. region: region,
  62. client: sm,
  63. framework: f,
  64. }
  65. BeforeEach(func() {
  66. prov.SetupStaticStore()
  67. prov.SetupReferencedIRSAStore()
  68. prov.SetupMountedIRSAStore()
  69. })
  70. AfterEach(func() {
  71. // Cleanup ClusterSecretStore
  72. err := prov.framework.CRClient.Delete(context.Background(), &esv1alpha1.ClusterSecretStore{
  73. ObjectMeta: metav1.ObjectMeta{
  74. Name: prov.ReferencedIRSAStoreName(),
  75. },
  76. })
  77. Expect(err).ToNot(HaveOccurred())
  78. })
  79. return prov
  80. }
  81. func NewFromEnv(f *framework.Framework) *SMProvider {
  82. kid := os.Getenv("AWS_ACCESS_KEY_ID")
  83. sak := os.Getenv("AWS_SECRET_ACCESS_KEY")
  84. region := "eu-west-1"
  85. saName := os.Getenv("AWS_SA_NAME")
  86. saNamespace := os.Getenv("AWS_SA_NAMESPACE")
  87. return NewSMProvider(f, kid, sak, region, saName, saNamespace)
  88. }
  89. // CreateSecret creates a secret at the provider.
  90. func (s *SMProvider) CreateSecret(key, val string) {
  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),
  100. })
  101. if err == nil {
  102. return
  103. }
  104. attempts--
  105. if attempts < 0 {
  106. Fail("unable to create secret: " + err.Error())
  107. }
  108. <-time.After(time.Second * 5)
  109. }
  110. }
  111. // DeleteSecret deletes a secret at the provider.
  112. // There may be a short delay between calling this function
  113. // and the removal of the secret on the provider side.
  114. func (s *SMProvider) DeleteSecret(key string) {
  115. log.Logf("deleting secret %s", key)
  116. _, err := s.client.DeleteSecret(&secretsmanager.DeleteSecretInput{
  117. SecretId: aws.String(key),
  118. ForceDeleteWithoutRecovery: aws.Bool(true),
  119. })
  120. Expect(err).ToNot(HaveOccurred())
  121. }
  122. // MountedIRSAStore is a SecretStore without auth config
  123. // ESO relies on the pod-mounted ServiceAccount when using this store.
  124. func (s *SMProvider) SetupMountedIRSAStore() {
  125. secretStore := &esv1alpha1.SecretStore{
  126. ObjectMeta: metav1.ObjectMeta{
  127. Name: s.MountedIRSAStoreName(),
  128. Namespace: s.framework.Namespace.Name,
  129. },
  130. Spec: esv1alpha1.SecretStoreSpec{
  131. Provider: &esv1alpha1.SecretStoreProvider{
  132. AWS: &esv1alpha1.AWSProvider{
  133. Service: esv1alpha1.AWSServiceSecretsManager,
  134. Region: s.region,
  135. Auth: esv1alpha1.AWSAuth{},
  136. },
  137. },
  138. },
  139. }
  140. err := s.framework.CRClient.Create(context.Background(), secretStore)
  141. Expect(err).ToNot(HaveOccurred())
  142. }
  143. func (s *SMProvider) MountedIRSAStoreName() string {
  144. return "irsa-mounted-" + s.framework.Namespace.Name
  145. }
  146. // ReferncedIRSAStore is a ClusterStore
  147. // that references a (IRSA-) ServiceAccount in the default namespace.
  148. func (s *SMProvider) SetupReferencedIRSAStore() {
  149. log.Logf("creating IRSA ClusterSecretStore %s", s.framework.Namespace.Name)
  150. secretStore := &esv1alpha1.ClusterSecretStore{
  151. ObjectMeta: metav1.ObjectMeta{
  152. Name: s.ReferencedIRSAStoreName(),
  153. },
  154. }
  155. _, err := controllerutil.CreateOrUpdate(context.Background(), s.framework.CRClient, secretStore, func() error {
  156. secretStore.Spec.Provider = &esv1alpha1.SecretStoreProvider{
  157. AWS: &esv1alpha1.AWSProvider{
  158. Service: esv1alpha1.AWSServiceSecretsManager,
  159. Region: s.region,
  160. Auth: esv1alpha1.AWSAuth{
  161. JWTAuth: &esv1alpha1.AWSJWTAuth{
  162. ServiceAccountRef: &esmetav1.ServiceAccountSelector{
  163. Name: s.ServiceAccountName,
  164. Namespace: &s.ServiceAccountNamespace,
  165. },
  166. },
  167. },
  168. },
  169. }
  170. return nil
  171. })
  172. Expect(err).ToNot(HaveOccurred())
  173. }
  174. func (s *SMProvider) ReferencedIRSAStoreName() string {
  175. return "irsa-ref-" + s.framework.Namespace.Name
  176. }
  177. // StaticStore is namespaced and references
  178. // static credentials from a secret.
  179. func (s *SMProvider) SetupStaticStore() {
  180. awsCreds := &v1.Secret{
  181. ObjectMeta: metav1.ObjectMeta{
  182. Name: staticCredentialsSecretName,
  183. Namespace: s.framework.Namespace.Name,
  184. },
  185. StringData: map[string]string{
  186. "kid": s.kid,
  187. "sak": s.sak,
  188. },
  189. }
  190. err := s.framework.CRClient.Create(context.Background(), awsCreds)
  191. Expect(err).ToNot(HaveOccurred())
  192. secretStore := &esv1alpha1.SecretStore{
  193. ObjectMeta: metav1.ObjectMeta{
  194. Name: s.framework.Namespace.Name,
  195. Namespace: s.framework.Namespace.Name,
  196. },
  197. Spec: esv1alpha1.SecretStoreSpec{
  198. Provider: &esv1alpha1.SecretStoreProvider{
  199. AWS: &esv1alpha1.AWSProvider{
  200. Service: esv1alpha1.AWSServiceSecretsManager,
  201. Region: s.region,
  202. Auth: esv1alpha1.AWSAuth{
  203. SecretRef: &esv1alpha1.AWSAuthSecretRef{
  204. AccessKeyID: esmetav1.SecretKeySelector{
  205. Name: staticCredentialsSecretName,
  206. Key: "kid",
  207. },
  208. SecretAccessKey: esmetav1.SecretKeySelector{
  209. Name: staticCredentialsSecretName,
  210. Key: "sak",
  211. },
  212. },
  213. },
  214. },
  215. },
  216. },
  217. }
  218. err = s.framework.CRClient.Create(context.Background(), secretStore)
  219. Expect(err).ToNot(HaveOccurred())
  220. }