provider.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 scaleway
  14. import (
  15. "github.com/external-secrets/external-secrets-e2e/framework"
  16. "github.com/onsi/gomega"
  17. smapi "github.com/scaleway/scaleway-sdk-go/api/secret/v1beta1"
  18. "github.com/scaleway/scaleway-sdk-go/scw"
  19. )
  20. const remoteRefPrefix = "name:"
  21. const cleanupTag = "eso-e2e" // tag for easy cleanup
  22. type secretStoreProvider struct {
  23. api *smapi.API
  24. cfg *config
  25. }
  26. func (p *secretStoreProvider) init(cfg *config) {
  27. p.cfg = cfg
  28. options := []scw.ClientOption{
  29. scw.WithDefaultRegion(scw.Region(cfg.region)),
  30. scw.WithDefaultProjectID(cfg.projectId),
  31. scw.WithAuth(cfg.accessKey, cfg.secretKey),
  32. }
  33. if cfg.apiUrl != nil {
  34. options = append(options, scw.WithAPIURL(*cfg.apiUrl))
  35. }
  36. scwClient, err := scw.NewClient(options...)
  37. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  38. p.api = smapi.NewAPI(scwClient)
  39. }
  40. // cleanup prevents accumulation of secrets after aborted runs.
  41. func (p *secretStoreProvider) cleanup() {
  42. for {
  43. listResp, err := p.api.ListSecrets(&smapi.ListSecretsRequest{
  44. ProjectID: &p.cfg.projectId,
  45. Tags: []string{cleanupTag},
  46. })
  47. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  48. for _, secret := range listResp.Secrets {
  49. err := p.api.DeleteSecret(&smapi.DeleteSecretRequest{
  50. SecretID: secret.ID,
  51. })
  52. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  53. }
  54. if uint64(len(listResp.Secrets)) == listResp.TotalCount {
  55. break
  56. }
  57. }
  58. }
  59. func (p *secretStoreProvider) CreateSecret(key string, val framework.SecretEntry) {
  60. gomega.Expect(key).To(gomega.HavePrefix(remoteRefPrefix))
  61. secretName := key[len(remoteRefPrefix):]
  62. var tags []string
  63. for tag := range val.Tags {
  64. tags = append(tags, tag)
  65. }
  66. tags = append(tags, cleanupTag)
  67. secret, err := p.api.CreateSecret(&smapi.CreateSecretRequest{
  68. Name: secretName,
  69. Tags: tags,
  70. })
  71. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  72. _, err = p.api.CreateSecretVersion(&smapi.CreateSecretVersionRequest{
  73. SecretID: secret.ID,
  74. Data: []byte(val.Value),
  75. })
  76. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  77. }
  78. func (p *secretStoreProvider) DeleteSecret(key string) {
  79. gomega.Expect(key).To(gomega.HavePrefix(remoteRefPrefix))
  80. secretName := key[len(remoteRefPrefix):]
  81. p.api.GetSecret(&smapi.GetSecretRequest{
  82. Region: "",
  83. SecretID: "",
  84. })
  85. res, err := p.api.ListSecrets(&smapi.ListSecretsRequest{
  86. Name: &secretName,
  87. })
  88. if _, isErrNotFound := err.(*scw.ResourceNotFoundError); isErrNotFound {
  89. return
  90. }
  91. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  92. for _, secret := range res.Secrets {
  93. err = p.api.DeleteSecret(&smapi.DeleteSecretRequest{
  94. SecretID: secret.ID,
  95. })
  96. if _, isErrNotFound := err.(*scw.ResourceNotFoundError); isErrNotFound {
  97. return
  98. }
  99. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  100. }
  101. }