client.go 5.5 KB

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