provider.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "github.com/aws/aws-sdk-go/aws"
  17. "github.com/aws/aws-sdk-go/aws/credentials"
  18. "github.com/aws/aws-sdk-go/aws/session"
  19. "github.com/aws/aws-sdk-go/service/ssm"
  20. //nolint
  21. . "github.com/onsi/ginkgo/v2"
  22. // nolint
  23. . "github.com/onsi/gomega"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  26. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  27. esmetav1 "github.com/external-secrets/external-secrets/apis/meta/v1"
  28. "github.com/external-secrets/external-secrets/e2e/framework"
  29. "github.com/external-secrets/external-secrets/e2e/framework/log"
  30. common "github.com/external-secrets/external-secrets/e2e/suite/aws"
  31. )
  32. type Provider struct {
  33. ServiceAccountName string
  34. ServiceAccountNamespace string
  35. region string
  36. client *ssm.SSM
  37. framework *framework.Framework
  38. }
  39. func NewProvider(f *framework.Framework, kid, sak, region, saName, saNamespace string) *Provider {
  40. sess, err := session.NewSessionWithOptions(session.Options{
  41. Config: aws.Config{
  42. Credentials: credentials.NewStaticCredentials(kid, sak, ""),
  43. Region: aws.String(region),
  44. },
  45. })
  46. if err != nil {
  47. Fail(err.Error())
  48. }
  49. sm := ssm.New(sess)
  50. prov := &Provider{
  51. ServiceAccountName: saName,
  52. ServiceAccountNamespace: saNamespace,
  53. region: region,
  54. client: sm,
  55. framework: f,
  56. }
  57. BeforeEach(func() {
  58. common.SetupStaticStore(f, kid, sak, region, esv1beta1.AWSServiceParameterStore)
  59. prov.SetupReferencedIRSAStore()
  60. prov.SetupMountedIRSAStore()
  61. })
  62. AfterEach(func() {
  63. // Cleanup ClusterSecretStore
  64. err := prov.framework.CRClient.Delete(context.Background(), &esv1beta1.ClusterSecretStore{
  65. ObjectMeta: metav1.ObjectMeta{
  66. Name: common.ReferencedIRSAStoreName(f),
  67. },
  68. })
  69. Expect(err).ToNot(HaveOccurred())
  70. })
  71. return prov
  72. }
  73. func NewFromEnv(f *framework.Framework) *Provider {
  74. kid := os.Getenv("AWS_ACCESS_KEY_ID")
  75. sak := os.Getenv("AWS_SECRET_ACCESS_KEY")
  76. region := "eu-west-1"
  77. saName := os.Getenv("AWS_SA_NAME")
  78. saNamespace := os.Getenv("AWS_SA_NAMESPACE")
  79. return NewProvider(f, kid, sak, region, saName, saNamespace)
  80. }
  81. // CreateSecret creates a secret at the provider.
  82. func (s *Provider) CreateSecret(key, val string) {
  83. _, err := s.client.PutParameter(&ssm.PutParameterInput{
  84. Name: aws.String(key),
  85. Value: aws.String(val),
  86. DataType: aws.String("text"),
  87. Type: aws.String("String"),
  88. })
  89. Expect(err).ToNot(HaveOccurred())
  90. }
  91. // DeleteSecret deletes a secret at the provider.
  92. func (s *Provider) DeleteSecret(key string) {
  93. _, err := s.client.DeleteParameter(&ssm.DeleteParameterInput{
  94. Name: aws.String(key),
  95. })
  96. Expect(err).ToNot(HaveOccurred())
  97. }
  98. // MountedIRSAStore is a SecretStore without auth config
  99. // ESO relies on the pod-mounted ServiceAccount when using this store.
  100. func (s *Provider) SetupMountedIRSAStore() {
  101. secretStore := &esv1beta1.SecretStore{
  102. ObjectMeta: metav1.ObjectMeta{
  103. Name: common.MountedIRSAStoreName(s.framework),
  104. Namespace: s.framework.Namespace.Name,
  105. },
  106. Spec: esv1beta1.SecretStoreSpec{
  107. Provider: &esv1beta1.SecretStoreProvider{
  108. AWS: &esv1beta1.AWSProvider{
  109. Service: esv1beta1.AWSServiceParameterStore,
  110. Region: s.region,
  111. Auth: esv1beta1.AWSAuth{},
  112. },
  113. },
  114. },
  115. }
  116. err := s.framework.CRClient.Create(context.Background(), secretStore)
  117. Expect(err).ToNot(HaveOccurred())
  118. }
  119. // ReferncedIRSAStore is a ClusterStore
  120. // that references a (IRSA-) ServiceAccount in the default namespace.
  121. func (s *Provider) SetupReferencedIRSAStore() {
  122. log.Logf("creating IRSA ClusterSecretStore %s", s.framework.Namespace.Name)
  123. secretStore := &esv1beta1.ClusterSecretStore{
  124. ObjectMeta: metav1.ObjectMeta{
  125. Name: common.ReferencedIRSAStoreName(s.framework),
  126. },
  127. }
  128. _, err := controllerutil.CreateOrUpdate(context.Background(), s.framework.CRClient, secretStore, func() error {
  129. secretStore.Spec.Provider = &esv1beta1.SecretStoreProvider{
  130. AWS: &esv1beta1.AWSProvider{
  131. Service: esv1beta1.AWSServiceParameterStore,
  132. Region: s.region,
  133. Auth: esv1beta1.AWSAuth{
  134. JWTAuth: &esv1beta1.AWSJWTAuth{
  135. ServiceAccountRef: &esmetav1.ServiceAccountSelector{
  136. Name: s.ServiceAccountName,
  137. Namespace: &s.ServiceAccountNamespace,
  138. },
  139. },
  140. },
  141. },
  142. }
  143. return nil
  144. })
  145. Expect(err).ToNot(HaveOccurred())
  146. }