ci.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.CIContextProvider = void 0;
  7. /*
  8. Copyright 2023 The Sigstore Authors.
  9. Licensed under the Apache License, Version 2.0 (the "License");
  10. you may not use this file except in compliance with the License.
  11. You may obtain a copy of the License at
  12. http://www.apache.org/licenses/LICENSE-2.0
  13. Unless required by applicable law or agreed to in writing, software
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. */
  19. const make_fetch_happen_1 = __importDefault(require("make-fetch-happen"));
  20. // Collection of all the CI-specific providers we have implemented
  21. const providers = [getGHAToken, getEnv];
  22. /**
  23. * CIContextProvider is a composite identity provider which will iterate
  24. * over all of the CI-specific providers and return the token from the first
  25. * one that resolves.
  26. */
  27. class CIContextProvider {
  28. /* istanbul ignore next */
  29. constructor(audience = 'sigstore') {
  30. this.audience = audience;
  31. }
  32. // Invoke all registered ProviderFuncs and return the value of whichever one
  33. // resolves first.
  34. async getToken() {
  35. return Promise.any(providers.map((getToken) => getToken(this.audience))).catch(() => Promise.reject('CI: no tokens available'));
  36. }
  37. }
  38. exports.CIContextProvider = CIContextProvider;
  39. /**
  40. * getGHAToken can retrieve an OIDC token when running in a GitHub Actions
  41. * workflow
  42. */
  43. async function getGHAToken(audience) {
  44. // Check to see if we're running in GitHub Actions
  45. if (!process.env.ACTIONS_ID_TOKEN_REQUEST_URL ||
  46. !process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN) {
  47. return Promise.reject('no token available');
  48. }
  49. // Construct URL to request token w/ appropriate audience
  50. const url = new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL);
  51. url.searchParams.append('audience', audience);
  52. const response = await (0, make_fetch_happen_1.default)(url.href, {
  53. retry: 2,
  54. headers: {
  55. Accept: 'application/json',
  56. Authorization: `Bearer ${process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}`,
  57. },
  58. });
  59. return response.json().then((data) => data.value);
  60. }
  61. /**
  62. * getEnv can retrieve an OIDC token from an environment variable.
  63. * This matches the behavior of https://github.com/sigstore/cosign/tree/main/pkg/providers/envvar
  64. */
  65. async function getEnv() {
  66. if (!process.env.SIGSTORE_ID_TOKEN) {
  67. return Promise.reject('no token available');
  68. }
  69. return process.env.SIGSTORE_ID_TOKEN;
  70. }