provider.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 gitlab
  13. import (
  14. "context"
  15. "os"
  16. "strings"
  17. // nolint
  18. . "github.com/onsi/ginkgo/v2"
  19. // nolint
  20. . "github.com/onsi/gomega"
  21. gitlab "github.com/xanzy/go-gitlab"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  25. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  26. "github.com/external-secrets/external-secrets/e2e/framework"
  27. )
  28. type gitlabProvider struct {
  29. credentials string
  30. projectID string
  31. framework *framework.Framework
  32. }
  33. func newGitlabProvider(f *framework.Framework, credentials, projectID string) *gitlabProvider {
  34. prov := &gitlabProvider{
  35. credentials: credentials,
  36. projectID: projectID,
  37. framework: f,
  38. }
  39. BeforeEach(prov.BeforeEach)
  40. return prov
  41. }
  42. func newFromEnv(f *framework.Framework) *gitlabProvider {
  43. credentials := os.Getenv("GITLAB_TOKEN")
  44. projectID := os.Getenv("GITLAB_PROJECT_ID")
  45. return newGitlabProvider(f, credentials, projectID)
  46. }
  47. func (s *gitlabProvider) CreateSecret(key string, val framework.SecretEntry) {
  48. // **Open the client
  49. client, err := gitlab.NewClient(s.credentials)
  50. Expect(err).ToNot(HaveOccurred())
  51. // Open the client**
  52. // Set variable options
  53. variableKey := strings.ReplaceAll(key, "-", "_")
  54. variableValue := val
  55. opt := gitlab.CreateProjectVariableOptions{
  56. Key: &variableKey,
  57. Value: &variableValue.Value,
  58. VariableType: nil,
  59. Protected: nil,
  60. Masked: nil,
  61. EnvironmentScope: nil,
  62. }
  63. // Create a variable
  64. _, _, err = client.ProjectVariables.CreateVariable(s.projectID, &opt)
  65. Expect(err).ToNot(HaveOccurred())
  66. // Versions aren't supported by Gitlab, but we could add
  67. // more parameters to test
  68. }
  69. func (s *gitlabProvider) DeleteSecret(key string) {
  70. // **Open a client
  71. client, err := gitlab.NewClient(s.credentials)
  72. Expect(err).ToNot(HaveOccurred())
  73. // Open a client**
  74. // Delete the secret
  75. _, err = client.ProjectVariables.RemoveVariable(s.projectID, strings.ReplaceAll(key, "-", "_"), &gitlab.RemoveProjectVariableOptions{})
  76. Expect(err).ToNot(HaveOccurred())
  77. }
  78. func (s *gitlabProvider) BeforeEach() {
  79. By("creating a gitlab variable")
  80. gitlabCreds := &v1.Secret{
  81. ObjectMeta: metav1.ObjectMeta{
  82. Name: "provider-secret",
  83. Namespace: s.framework.Namespace.Name,
  84. },
  85. // Puts access token into StringData
  86. StringData: map[string]string{
  87. "token": s.credentials,
  88. "projectID": s.projectID,
  89. },
  90. }
  91. err := s.framework.CRClient.Create(context.Background(), gitlabCreds)
  92. Expect(err).ToNot(HaveOccurred())
  93. // Create a secret store - change these values to match YAML
  94. By("creating a secret store for credentials")
  95. secretStore := &esv1beta1.SecretStore{
  96. ObjectMeta: metav1.ObjectMeta{
  97. Name: s.framework.Namespace.Name,
  98. Namespace: s.framework.Namespace.Name,
  99. },
  100. Spec: esv1beta1.SecretStoreSpec{
  101. Provider: &esv1beta1.SecretStoreProvider{
  102. Gitlab: &esv1beta1.GitlabProvider{
  103. ProjectID: s.projectID,
  104. Auth: esv1beta1.GitlabAuth{
  105. SecretRef: esv1beta1.GitlabSecretRef{
  106. AccessToken: esmeta.SecretKeySelector{
  107. Name: "provider-secret",
  108. Key: "token",
  109. },
  110. },
  111. },
  112. },
  113. },
  114. },
  115. }
  116. err = s.framework.CRClient.Create(context.Background(), secretStore)
  117. Expect(err).ToNot(HaveOccurred())
  118. }