auth.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 provides a client for GitHub API interactions.
  14. package github
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "github.com/bradleyfalzon/ghinstallation/v2"
  20. github "github.com/google/go-github/v56/github"
  21. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  22. )
  23. // AuthWithPrivateKey creates a new GitHub client authenticated using a private key.
  24. // It retrieves the private key from the secret referenced in the provider configuration
  25. // and sets up GitHub App authentication.
  26. func (g *Client) AuthWithPrivateKey(ctx context.Context) (*github.Client, error) {
  27. privateKey, err := resolvers.SecretKeyRef(ctx, g.crClient, g.storeKind, g.namespace, &g.provider.Auth.PrivateKey)
  28. if err != nil {
  29. return nil, fmt.Errorf("couldn't get private key from secret: resolvers.SecretKeyRef failed with error %w", err)
  30. }
  31. itr, err := ghinstallation.New(http.DefaultTransport, g.provider.AppID, g.provider.InstallationID, []byte(privateKey))
  32. if err != nil {
  33. return nil, fmt.Errorf("could not instantiate new installation transport: %w", err)
  34. }
  35. client := github.NewClient(&http.Client{Transport: itr})
  36. if (g.provider.URL != "") && (g.provider.URL != "https://github.com/") {
  37. uploadURL := g.provider.UploadURL
  38. if uploadURL == "" {
  39. uploadURL = g.provider.URL
  40. }
  41. return client.WithEnterpriseURLs(g.provider.URL, uploadURL)
  42. }
  43. return client, nil
  44. }