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