client.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // ActionsServiceClient defines the interface for interacting with GitHub Actions secrets.
  30. type ActionsServiceClient interface {
  31. // CreateOrUpdateOrgSecret creates or updates an organization secret.
  32. CreateOrUpdateOrgSecret(ctx context.Context, org string, eSecret *github.EncryptedSecret) (response *github.Response, err error)
  33. // GetOrgSecret retrieves an organization secret.
  34. GetOrgSecret(ctx context.Context, org string, name string) (*github.Secret, *github.Response, error)
  35. // ListOrgSecrets lists all organization secrets.
  36. ListOrgSecrets(ctx context.Context, org string, opts *github.ListOptions) (*github.Secrets, *github.Response, error)
  37. }
  38. // Client implements the External Secrets Kubernetes provider for GitHub Actions secrets.
  39. type Client struct {
  40. crClient client.Client
  41. store esv1.GenericStore
  42. provider *esv1.GithubProvider
  43. baseClient github.ActionsService
  44. namespace string
  45. storeKind string
  46. repoID int64
  47. getSecretFn func(ctx context.Context, ref esv1.PushSecretRemoteRef) (*github.Secret, *github.Response, error)
  48. getPublicKeyFn func(ctx context.Context) (*github.PublicKey, *github.Response, error)
  49. createOrUpdateFn func(ctx context.Context, eSecret *github.EncryptedSecret) (*github.Response, error)
  50. listSecretsFn func(ctx context.Context) (*github.Secrets, *github.Response, error)
  51. deleteSecretFn func(ctx context.Context, ref esv1.PushSecretRemoteRef) (*github.Response, error)
  52. }
  53. // DeleteSecret deletes a secret from GitHub Actions.
  54. func (g *Client) DeleteSecret(ctx context.Context, remoteRef esv1.PushSecretRemoteRef) error {
  55. _, err := g.deleteSecretFn(ctx, remoteRef)
  56. if err != nil {
  57. return fmt.Errorf("failed to delete secret: %w", err)
  58. }
  59. return nil
  60. }
  61. // SecretExists checks if a secret exists in GitHub Actions.
  62. func (g *Client) SecretExists(ctx context.Context, ref esv1.PushSecretRemoteRef) (bool, error) {
  63. githubSecret, _, err := g.getSecretFn(ctx, ref)
  64. if err != nil {
  65. return false, fmt.Errorf("error fetching secret: %w", err)
  66. }
  67. if githubSecret != nil {
  68. return true, nil
  69. }
  70. return false, nil
  71. }
  72. // PushSecret pushes a new secret to GitHub Actions.
  73. func (g *Client) PushSecret(ctx context.Context, secret *corev1.Secret, remoteRef esv1.PushSecretData) error {
  74. githubSecret, response, err := g.getSecretFn(ctx, remoteRef)
  75. if err != nil && (response == nil || response.StatusCode != 404) {
  76. return fmt.Errorf("error fetching secret: %w", err)
  77. }
  78. // First at all, we need the organization public key to encrypt the secret.
  79. publicKey, _, err := g.getPublicKeyFn(ctx)
  80. if err != nil {
  81. return fmt.Errorf("error fetching public key: %w", err)
  82. }
  83. decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKey.GetKey())
  84. if err != nil {
  85. return fmt.Errorf("unable to decode public key: %w", err)
  86. }
  87. var boxKey [32]byte
  88. copy(boxKey[:], decodedPublicKey)
  89. var ok bool
  90. // default to full secret.
  91. value, err := json.Marshal(secret.Data)
  92. if err != nil {
  93. return fmt.Errorf("json.Marshal failed with error %w", err)
  94. }
  95. // if key is specified, overwrite to key only
  96. if remoteRef.GetSecretKey() != "" {
  97. value, ok = secret.Data[remoteRef.GetSecretKey()]
  98. if !ok {
  99. return fmt.Errorf("key %s not found in secret", remoteRef.GetSecretKey())
  100. }
  101. }
  102. encryptedBytes, err := box.SealAnonymous([]byte{}, value, &boxKey, crypto_rand.Reader)
  103. if err != nil {
  104. return fmt.Errorf("box.SealAnonymous failed with error %w", err)
  105. }
  106. name := remoteRef.GetRemoteKey()
  107. visibility := "all"
  108. if githubSecret != nil {
  109. name = githubSecret.Name
  110. visibility = githubSecret.Visibility
  111. }
  112. encryptedString := base64.StdEncoding.EncodeToString(encryptedBytes)
  113. keyID := publicKey.GetKeyID()
  114. encryptedSecret := &github.EncryptedSecret{
  115. Name: name,
  116. KeyID: keyID,
  117. EncryptedValue: encryptedString,
  118. Visibility: visibility,
  119. }
  120. if _, err := g.createOrUpdateFn(ctx, encryptedSecret); err != nil {
  121. return fmt.Errorf("failed to create secret: %w", err)
  122. }
  123. return nil
  124. }
  125. // GetAllSecrets is not implemented as this provider is write-only.
  126. func (g *Client) GetAllSecrets(_ context.Context, _ esv1.ExternalSecretFind) (map[string][]byte, error) {
  127. return nil, fmt.Errorf("not implemented - this provider supports write-only operations")
  128. }
  129. // GetSecret is not implemented as this provider is write-only.
  130. func (g *Client) GetSecret(_ context.Context, _ esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  131. return nil, fmt.Errorf("not implemented - this provider supports write-only operations")
  132. }
  133. // GetSecretMap is not implemented as this provider is write-only.
  134. func (g *Client) GetSecretMap(_ context.Context, _ esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  135. return nil, fmt.Errorf("not implemented - this provider supports write-only operations")
  136. }
  137. // Close cleans up any resources held by the client. No-op for this provider.
  138. func (g *Client) Close(_ context.Context) error {
  139. return nil
  140. }
  141. // Validate checks if the client is properly configured and has access to the GitHub Actions API.
  142. func (g *Client) Validate() (esv1.ValidationResult, error) {
  143. if g.store.GetKind() == esv1.ClusterSecretStoreKind {
  144. return esv1.ValidationResultUnknown, nil
  145. }
  146. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  147. defer cancel()
  148. _, _, err := g.listSecretsFn(ctx)
  149. if err != nil {
  150. return esv1.ValidationResultError, fmt.Errorf("store is not allowed to list secrets: %w", err)
  151. }
  152. return esv1.ValidationResultReady, nil
  153. }