provider.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "github.com/xanzy/go-gitlab"
  19. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  20. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  23. prov "github.com/external-secrets/external-secrets/apis/providers/v1alpha1"
  24. "github.com/external-secrets/external-secrets/pkg/utils"
  25. )
  26. // Provider satisfies the provider interface.
  27. type Provider struct {
  28. storeKind string
  29. }
  30. // gitlabBase satisfies the provider.SecretsClient interface.
  31. type gitlabBase struct {
  32. kube kclient.Client
  33. store *prov.GitlabSpec
  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() esv1beta1.SecretStoreCapabilities {
  42. return esv1beta1.SecretStoreReadOnly
  43. }
  44. func (g *Provider) ApplyReferent(spec kclient.Object, caller esmeta.ReferentCallOrigin, namespace string) (kclient.Object, error) {
  45. converted, ok := spec.(*prov.Gitlab)
  46. if !ok {
  47. return nil, fmt.Errorf("could not convert source object %v into 'fake' provider type: object from type %T", spec.GetName(), spec)
  48. }
  49. ns := &namespace
  50. out := converted.DeepCopy()
  51. switch caller {
  52. case esmeta.ReferentCallProvider:
  53. case esmeta.ReferentCallSecretStore:
  54. out.Spec.Auth.SecretRef.AccessToken.Namespace = ns
  55. case esmeta.ReferentCallClusterSecretStore:
  56. // compatibility with utils.SecretKeyRef
  57. g.storeKind = esv1beta1.ClusterSecretStoreKind
  58. default:
  59. }
  60. return spec, nil
  61. }
  62. func (g *Provider) Convert(in esv1beta1.GenericStore) (kclient.Object, error) {
  63. out := &prov.Gitlab{}
  64. tmp := map[string]interface{}{
  65. "spec": in.GetSpec().Provider.Gitlab,
  66. }
  67. d, err := json.Marshal(tmp)
  68. if err != nil {
  69. return nil, err
  70. }
  71. err = json.Unmarshal(d, out)
  72. if err != nil {
  73. return nil, fmt.Errorf("could not convert %v in a valid fake provider: %w", in.GetName(), err)
  74. }
  75. return out, nil
  76. }
  77. func (g *Provider) NewClientFromObj(ctx context.Context, obj kclient.Object, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  78. spec, ok := obj.(*prov.Gitlab)
  79. if !ok {
  80. return nil, errors.New("no store type or wrong store type")
  81. }
  82. gl := &gitlabBase{
  83. kube: kube,
  84. store: &spec.Spec,
  85. storeKind: g.storeKind,
  86. namespace: namespace,
  87. }
  88. client, err := gl.getClient(ctx, &spec.Spec)
  89. if err != nil {
  90. return nil, err
  91. }
  92. gl.projectsClient = client.Projects
  93. gl.projectVariablesClient = client.ProjectVariables
  94. gl.groupVariablesClient = client.GroupVariables
  95. return gl, nil
  96. }
  97. // Method on GitLab Provider to set up projectVariablesClient with credentials, populate projectID and environment.
  98. func (g *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  99. return nil, errors.New("method no longer supported")
  100. }
  101. func (g *gitlabBase) getClient(ctx context.Context, provider *prov.GitlabSpec) (*gitlab.Client, error) {
  102. credentials, err := g.getAuth(ctx)
  103. if err != nil {
  104. return nil, err
  105. }
  106. // Create projectVariablesClient options
  107. var opts []gitlab.ClientOptionFunc
  108. if provider.URL != "" {
  109. opts = append(opts, gitlab.WithBaseURL(provider.URL))
  110. }
  111. // ClientOptionFunc from the gitlab package can be mapped with the CRD
  112. // in a similar way to extend functionality of the provider
  113. // Create a new GitLab Client using credentials and options
  114. client, err := gitlab.NewClient(credentials, opts...)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return client, nil
  119. }
  120. func (g *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
  121. storeSpec := store.GetSpec()
  122. gitlabSpec := storeSpec.Provider.Gitlab
  123. accessToken := gitlabSpec.Auth.SecretRef.AccessToken
  124. err := utils.ValidateSecretSelector(store, accessToken)
  125. if err != nil {
  126. return nil, err
  127. }
  128. if gitlabSpec.ProjectID == "" && len(gitlabSpec.GroupIDs) == 0 {
  129. return nil, errors.New("projectID and groupIDs must not both be empty")
  130. }
  131. if gitlabSpec.InheritFromGroups && len(gitlabSpec.GroupIDs) > 0 {
  132. return nil, errors.New("defining groupIDs and inheritFromGroups = true is not allowed")
  133. }
  134. if accessToken.Key == "" {
  135. return nil, errors.New("accessToken.key cannot be empty")
  136. }
  137. if accessToken.Name == "" {
  138. return nil, errors.New("accessToken.name cannot be empty")
  139. }
  140. return nil, nil
  141. }
  142. func init() {
  143. esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
  144. Gitlab: &esv1beta1.GitlabProvider{},
  145. })
  146. esv1beta1.RegisterByName(&Provider{}, prov.GitlabKind)
  147. ref := esmeta.ProviderRef{
  148. APIVersion: prov.Group + "/" + prov.Version,
  149. Kind: prov.GitlabKind,
  150. }
  151. prov.RefRegister(&prov.Gitlab{}, ref)
  152. }