provider.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "errors"
  16. "fmt"
  17. "net/http"
  18. gitlab "gitlab.com/gitlab-org/api/client-go"
  19. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  20. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  21. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  22. "github.com/external-secrets/external-secrets/pkg/constants"
  23. "github.com/external-secrets/external-secrets/pkg/metrics"
  24. "github.com/external-secrets/external-secrets/pkg/utils"
  25. )
  26. // Provider satisfies the provider interface.
  27. type Provider struct{}
  28. // gitlabBase satisfies the provider.SecretsClient interface.
  29. type gitlabBase struct {
  30. kube kclient.Client
  31. store *esv1.GitlabProvider
  32. storeKind string
  33. namespace string
  34. projectsClient ProjectsClient
  35. projectVariablesClient ProjectVariablesClient
  36. groupVariablesClient GroupVariablesClient
  37. }
  38. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  39. func (g *Provider) Capabilities() esv1.SecretStoreCapabilities {
  40. return esv1.SecretStoreReadOnly
  41. }
  42. // Method on GitLab Provider to set up projectVariablesClient with credentials, populate projectID and environment.
  43. func (g *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  44. storeSpec := store.GetSpec()
  45. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Gitlab == nil {
  46. return nil, errors.New("no store type or wrong store type")
  47. }
  48. storeSpecGitlab := storeSpec.Provider.Gitlab
  49. gl := &gitlabBase{
  50. kube: kube,
  51. store: storeSpecGitlab,
  52. namespace: namespace,
  53. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  54. }
  55. client, err := gl.getClient(ctx, storeSpecGitlab)
  56. if err != nil {
  57. return nil, err
  58. }
  59. gl.projectsClient = client.Projects
  60. gl.projectVariablesClient = client.ProjectVariables
  61. gl.groupVariablesClient = client.GroupVariables
  62. return gl, nil
  63. }
  64. func (g *gitlabBase) getClient(ctx context.Context, provider *esv1.GitlabProvider) (*gitlab.Client, error) {
  65. credentials, err := g.getAuth(ctx)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // Create projectVariablesClient options
  70. var opts []gitlab.ClientOptionFunc
  71. if provider.URL != "" {
  72. opts = append(opts, gitlab.WithBaseURL(provider.URL))
  73. }
  74. // ClientOptionFunc from the gitlab package can be mapped with the CRD
  75. // in a similar way to extend functionality of the provider
  76. // Create a new GitLab Client using credentials and options
  77. client, err := gitlab.NewClient(credentials, opts...)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return client, nil
  82. }
  83. func (g *gitlabBase) getVariables(ref esv1.ExternalSecretDataRemoteRef, vopts *gitlab.GetProjectVariableOptions) (*gitlab.ProjectVariable, *gitlab.Response, error) {
  84. data, resp, err := g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts)
  85. metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err)
  86. if err != nil {
  87. if resp != nil && resp.StatusCode == http.StatusNotFound && !isEmptyOrWildcard(g.store.Environment) {
  88. vopts.Filter.EnvironmentScope = "*"
  89. data, resp, err = g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts)
  90. metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err)
  91. if err != nil || resp == nil {
  92. return nil, resp, fmt.Errorf("error getting variable %s from GitLab: %w", ref.Key, err)
  93. }
  94. } else {
  95. return nil, resp, err
  96. }
  97. }
  98. return data, resp, nil
  99. }
  100. func (g *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  101. storeSpec := store.GetSpec()
  102. gitlabSpec := storeSpec.Provider.Gitlab
  103. accessToken := gitlabSpec.Auth.SecretRef.AccessToken
  104. err := utils.ValidateSecretSelector(store, accessToken)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if gitlabSpec.ProjectID == "" && len(gitlabSpec.GroupIDs) == 0 {
  109. return nil, errors.New("projectID and groupIDs must not both be empty")
  110. }
  111. if gitlabSpec.InheritFromGroups && len(gitlabSpec.GroupIDs) > 0 {
  112. return nil, errors.New("defining groupIDs and inheritFromGroups = true is not allowed")
  113. }
  114. if accessToken.Key == "" {
  115. return nil, errors.New("accessToken.key cannot be empty")
  116. }
  117. if accessToken.Name == "" {
  118. return nil, errors.New("accessToken.name cannot be empty")
  119. }
  120. return nil, nil
  121. }
  122. func init() {
  123. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  124. Gitlab: &esv1.GitlabProvider{},
  125. }, esv1.MaintenanceStatusMaintained)
  126. }