auth.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // /*
  14. // Licensed under the Apache License, Version 2.0 (the "License");
  15. // you may not use this file except in compliance with the License.
  16. // You may obtain a copy of the License at
  17. //
  18. // https://www.apache.org/licenses/LICENSE-2.0
  19. //
  20. // Unless required by applicable law or agreed to in writing, software
  21. // distributed under the License is distributed on an "AS IS" BASIS,
  22. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. // See the License for the specific language governing permissions and
  24. // limitations under the License.
  25. // */
  26. package github
  27. import (
  28. "context"
  29. "fmt"
  30. "net/http"
  31. "github.com/bradleyfalzon/ghinstallation/v2"
  32. github "github.com/google/go-github/v56/github"
  33. "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
  34. )
  35. func (g *Client) AuthWithPrivateKey(ctx context.Context) (*github.Client, error) {
  36. privateKey, err := resolvers.SecretKeyRef(ctx, g.crClient, g.storeKind, g.namespace, &g.provider.Auth.PrivateKey)
  37. if err != nil {
  38. return nil, fmt.Errorf("couldn't get private key from secret: resolvers.SecretKeyRef failed with error %w", err)
  39. }
  40. itr, err := ghinstallation.New(http.DefaultTransport, g.provider.AppID, g.provider.InstallationID, []byte(privateKey))
  41. if err != nil {
  42. return nil, fmt.Errorf("could not instantiate new installation transport: %w", err)
  43. }
  44. client := github.NewClient(&http.Client{Transport: itr})
  45. if (g.provider.URL != "") && (g.provider.URL != "https://github.com/") {
  46. uploadURL := g.provider.UploadURL
  47. if uploadURL == "" {
  48. uploadURL = g.provider.URL
  49. }
  50. return client.WithEnterpriseURLs(g.provider.URL, uploadURL)
  51. }
  52. return client, nil
  53. }