client.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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
  14. import (
  15. "context"
  16. crypto_rand "crypto/rand"
  17. "encoding/base64"
  18. "encoding/json"
  19. "fmt"
  20. "time"
  21. github "github.com/google/go-github/v56/github"
  22. "golang.org/x/crypto/nacl/box"
  23. corev1 "k8s.io/api/core/v1"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  26. )
  27. // https://github.com/external-secrets/external-secrets/issues/644
  28. var _ esv1.SecretsClient = &Client{}
  29. type ActionsServiceClient interface {
  30. CreateOrUpdateOrgSecret(ctx context.Context, org string, eSecret *github.EncryptedSecret) (response *github.Response, err error)
  31. GetOrgSecret(ctx context.Context, org string, name string) (*github.Secret, *github.Response, error)
  32. ListOrgSecrets(ctx context.Context, org string, opts *github.ListOptions) (*github.Secrets, *github.Response, error)
  33. }
  34. type Client struct {
  35. crClient client.Client
  36. store esv1.GenericStore
  37. provider *esv1.GithubProvider
  38. baseClient github.ActionsService
  39. namespace string
  40. storeKind string
  41. repoID int64
  42. getSecretFn func(ctx context.Context, ref esv1.PushSecretRemoteRef) (*github.Secret, *github.Response, error)
  43. getPublicKeyFn func(ctx context.Context) (*github.PublicKey, *github.Response, error)
  44. createOrUpdateFn func(ctx context.Context, eSecret *github.EncryptedSecret) (*github.Response, error)
  45. listSecretsFn func(ctx context.Context) (*github.Secrets, *github.Response, error)
  46. deleteSecretFn func(ctx context.Context, ref esv1.PushSecretRemoteRef) (*github.Response, error)
  47. }
  48. func (g *Client) DeleteSecret(ctx context.Context, remoteRef esv1.PushSecretRemoteRef) error {
  49. _, err := g.deleteSecretFn(ctx, remoteRef)
  50. if err != nil {
  51. return fmt.Errorf("failed to delete secret: %w", err)
  52. }
  53. return nil
  54. }
  55. func (g *Client) SecretExists(ctx context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  56. githubSecret, _, err := g.getSecretFn(ctx, ref)
  57. if err != nil {
  58. return false, fmt.Errorf("error fetching secret: %w", err)
  59. }
  60. if githubSecret != nil {
  61. return true, nil
  62. }
  63. return false, nil
  64. }
  65. func (g *Client) PushSecret(ctx context.Context, secret *corev1.Secret, remoteRef esv1.PushSecretData) error {
  66. githubSecret, response, err := g.getSecretFn(ctx, remoteRef)
  67. if err != nil && (response == nil || response.StatusCode != 404) {
  68. return fmt.Errorf("error fetching secret: %w", err)
  69. }
  70. // First at all, we need the organization public key to encrypt the secret.
  71. publicKey, _, err := g.getPublicKeyFn(ctx)
  72. if err != nil {
  73. return fmt.Errorf("error fetching public key: %w", err)
  74. }
  75. decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKey.GetKey())
  76. if err != nil {
  77. return fmt.Errorf("unable to decode public key: %w", err)
  78. }
  79. var boxKey [32]byte
  80. copy(boxKey[:], decodedPublicKey)
  81. var ok bool
  82. // default to full secret.
  83. value, err := json.Marshal(secret.Data)
  84. if err != nil {
  85. return fmt.Errorf("json.Marshal failed with error %w", err)
  86. }
  87. // if key is specified, overwrite to key only
  88. if remoteRef.GetSecretKey() != "" {
  89. value, ok = secret.Data[remoteRef.GetSecretKey()]
  90. if !ok {
  91. return fmt.Errorf("key %s not found in secret", remoteRef.GetSecretKey())
  92. }
  93. }
  94. encryptedBytes, err := box.SealAnonymous([]byte{}, value, &boxKey, crypto_rand.Reader)
  95. if err != nil {
  96. return fmt.Errorf("box.SealAnonymous failed with error %w", err)
  97. }
  98. name := remoteRef.GetRemoteKey()
  99. visibility := "all"
  100. if githubSecret != nil {
  101. name = githubSecret.Name
  102. visibility = githubSecret.Visibility
  103. }
  104. encryptedString := base64.StdEncoding.EncodeToString(encryptedBytes)
  105. keyID := publicKey.GetKeyID()
  106. encryptedSecret := &github.EncryptedSecret{
  107. Name: name,
  108. KeyID: keyID,
  109. EncryptedValue: encryptedString,
  110. Visibility: visibility,
  111. }
  112. if _, err := g.createOrUpdateFn(ctx, encryptedSecret); err != nil {
  113. return fmt.Errorf("failed to create secret: %w", err)
  114. }
  115. return nil
  116. }
  117. func (g *Client) GetAllSecrets(ctx context.Context, ref esv1.ExternalSecretFind) (map[string][]byte, error) {
  118. return nil, fmt.Errorf("not implemented - this provider supports write-only operations")
  119. }
  120. func (g *Client) GetSecret(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  121. return nil, fmt.Errorf("not implemented - this provider supports write-only operations")
  122. }
  123. func (g *Client) GetSecretMap(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  124. return nil, fmt.Errorf("not implemented - this provider supports write-only operations")
  125. }
  126. func (g *Client) Close(_ context.Context) error {
  127. return nil
  128. }
  129. func (g *Client) Validate() (esv1.ValidationResult, error) {
  130. if g.store.GetKind() == esv1.ClusterSecretStoreKind {
  131. return esv1.ValidationResultUnknown, nil
  132. }
  133. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  134. defer cancel()
  135. _, _, err := g.listSecretsFn(ctx)
  136. if err != nil {
  137. return esv1.ValidationResultError, fmt.Errorf("store is not allowed to list secrets: %w", err)
  138. }
  139. return esv1.ValidationResultReady, nil
  140. }