auth.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // */
  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/pkg/utils/resolvers"
  22. )
  23. func (g *Client) AuthWithPrivateKey(ctx context.Context) (*github.Client, error) {
  24. privateKey, err := resolvers.SecretKeyRef(ctx, g.crClient, g.storeKind, g.namespace, &g.provider.Auth.PrivateKey)
  25. if err != nil {
  26. return nil, fmt.Errorf("couldn't get private key from secret: resolvers.SecretKeyRef failed with error %w", err)
  27. }
  28. itr, err := ghinstallation.New(http.DefaultTransport, g.provider.AppID, g.provider.InstallationID, []byte(privateKey))
  29. if err != nil {
  30. return nil, fmt.Errorf("could not instantiate new installation transport: %w", err)
  31. }
  32. client := github.NewClient(&http.Client{Transport: itr})
  33. if (g.provider.URL != "") && (g.provider.URL != "https://github.com/") {
  34. uploadURL := g.provider.UploadURL
  35. if uploadURL == "" {
  36. uploadURL = g.provider.URL
  37. }
  38. return client.WithEnterpriseURLs(g.provider.URL, uploadURL)
  39. }
  40. return client, nil
  41. }