client.go 6.3 KB

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