provider.go 3.8 KB

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