provider.go 5.7 KB

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