provider.go 3.5 KB

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