provider.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package github implements a provider for GitHub secrets, allowing
  14. // External Secrets to write secrets to GitHub Actions.
  15. package github
  16. import (
  17. "context"
  18. "errors"
  19. "fmt"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. )
  24. const (
  25. errUnexpectedStoreSpec = "unexpected store spec"
  26. errInvalidStoreSpec = "invalid store spec"
  27. errInvalidStoreProv = "invalid store provider"
  28. errInvalidGithubProv = "invalid github provider"
  29. errInvalidStore = "invalid store"
  30. )
  31. // Provider implements the GitHub provider for managing secrets through GitHub Actions.
  32. type Provider struct {
  33. }
  34. var _ esv1.Provider = &Provider{}
  35. func init() {
  36. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  37. Github: &esv1.GithubProvider{},
  38. }, esv1.MaintenanceStatusMaintained)
  39. }
  40. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  41. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  42. return esv1.SecretStoreWriteOnly
  43. }
  44. // NewClient constructs a new secrets client based on the provided store.
  45. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
  46. return newClient(ctx, store, kube, namespace)
  47. }
  48. func newClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
  49. provider, err := getProvider(store)
  50. if err != nil {
  51. return nil, err
  52. }
  53. g := &Client{
  54. crClient: kube,
  55. store: store,
  56. namespace: namespace,
  57. provider: provider,
  58. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  59. }
  60. g.getSecretFn = g.orgGetSecretFn
  61. g.getPublicKeyFn = g.orgGetPublicKeyFn
  62. g.createOrUpdateFn = g.orgCreateOrUpdateSecret
  63. g.listSecretsFn = g.orgListSecretsFn
  64. g.deleteSecretFn = g.orgDeleteSecretsFn
  65. ghClient, err := g.AuthWithPrivateKey(ctx)
  66. if err != nil {
  67. return nil, fmt.Errorf("could not get private key: %w", err)
  68. }
  69. g.baseClient = *ghClient.Actions
  70. if provider.Repository != "" {
  71. g.getSecretFn = g.repoGetSecretFn
  72. g.getPublicKeyFn = g.repoGetPublicKeyFn
  73. g.createOrUpdateFn = g.repoCreateOrUpdateSecret
  74. g.listSecretsFn = g.repoListSecretsFn
  75. g.deleteSecretFn = g.repoDeleteSecretsFn
  76. if provider.Environment != "" {
  77. // For environment to work, we need the repository ID instead of its name.
  78. repo, _, err := ghClient.Repositories.Get(ctx, g.provider.Organization, g.provider.Repository)
  79. if err != nil {
  80. return nil, fmt.Errorf("error fetching repository: %w", err)
  81. }
  82. g.repoID = repo.GetID()
  83. g.getSecretFn = g.envGetSecretFn
  84. g.getPublicKeyFn = g.envGetPublicKeyFn
  85. g.createOrUpdateFn = g.envCreateOrUpdateSecret
  86. g.listSecretsFn = g.envListSecretsFn
  87. g.deleteSecretFn = g.envDeleteSecretsFn
  88. }
  89. }
  90. return g, nil
  91. }
  92. func getProvider(store esv1.GenericStore) (*esv1.GithubProvider, error) {
  93. spc := store.GetSpec()
  94. if spc == nil || spc.Provider.Github == nil {
  95. return nil, errors.New(errUnexpectedStoreSpec)
  96. }
  97. return spc.Provider.Github, nil
  98. }
  99. // ValidateStore validates the configuration of a GitHub secret store.
  100. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  101. if store == nil {
  102. return nil, errors.New(errInvalidStore)
  103. }
  104. spc := store.GetSpec()
  105. if spc == nil {
  106. return nil, errors.New(errInvalidStoreSpec)
  107. }
  108. if spc.Provider == nil {
  109. return nil, errors.New(errInvalidStoreProv)
  110. }
  111. prov := spc.Provider.Github
  112. if prov == nil {
  113. return nil, errors.New(errInvalidGithubProv)
  114. }
  115. return nil, nil
  116. }