client.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.TUFClient = 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 fs_1 = __importDefault(require("fs"));
  20. const path_1 = __importDefault(require("path"));
  21. const tuf_js_1 = require("tuf-js");
  22. const _1 = require(".");
  23. const target_1 = require("./target");
  24. const TARGETS_DIR_NAME = 'targets';
  25. class TUFClient {
  26. constructor(options) {
  27. const url = new URL(options.mirrorURL);
  28. const repoName = encodeURIComponent(url.host + url.pathname.replace(/\/$/, ''));
  29. const cachePath = path_1.default.join(options.cachePath, repoName);
  30. initTufCache(cachePath);
  31. seedCache({
  32. cachePath,
  33. mirrorURL: options.mirrorURL,
  34. tufRootPath: options.rootPath,
  35. forceInit: options.forceInit,
  36. });
  37. this.updater = initClient({
  38. mirrorURL: options.mirrorURL,
  39. cachePath,
  40. forceCache: options.forceCache,
  41. retry: options.retry,
  42. timeout: options.timeout,
  43. });
  44. }
  45. async refresh() {
  46. return this.updater.refresh();
  47. }
  48. getTarget(targetName) {
  49. return (0, target_1.readTarget)(this.updater, targetName);
  50. }
  51. }
  52. exports.TUFClient = TUFClient;
  53. // Initializes the TUF cache directory structure including the initial
  54. // root.json file. If the cache directory does not exist, it will be
  55. // created. If the targets directory does not exist, it will be created.
  56. // If the root.json file does not exist, it will be copied from the
  57. // rootPath argument.
  58. function initTufCache(cachePath) {
  59. const targetsPath = path_1.default.join(cachePath, TARGETS_DIR_NAME);
  60. if (!fs_1.default.existsSync(cachePath)) {
  61. fs_1.default.mkdirSync(cachePath, { recursive: true });
  62. }
  63. if (!fs_1.default.existsSync(targetsPath)) {
  64. fs_1.default.mkdirSync(targetsPath);
  65. }
  66. }
  67. // Populates the TUF cache with the initial root.json file. If the root.json
  68. // file does not exist (or we're forcing re-initialization), copy it from either
  69. // the rootPath argument or from one of the repo seeds.
  70. function seedCache({ cachePath, mirrorURL, tufRootPath, forceInit, }) {
  71. const cachedRootPath = path_1.default.join(cachePath, 'root.json');
  72. // If the root.json file does not exist (or we're forcing re-initialization),
  73. // populate it either from the supplied rootPath or from one of the repo seeds.
  74. if (!fs_1.default.existsSync(cachedRootPath) || forceInit) {
  75. if (tufRootPath) {
  76. fs_1.default.copyFileSync(tufRootPath, cachedRootPath);
  77. }
  78. else {
  79. const seeds = require('../seeds.json');
  80. const repoSeed = seeds[mirrorURL];
  81. if (!repoSeed) {
  82. throw new _1.TUFError({
  83. code: 'TUF_INIT_CACHE_ERROR',
  84. message: `No root.json found for mirror: ${mirrorURL}`,
  85. });
  86. }
  87. fs_1.default.writeFileSync(cachedRootPath, Buffer.from(repoSeed['root.json'], 'base64'));
  88. // Copy any seed targets into the cache
  89. Object.entries(repoSeed.targets).forEach(([targetName, target]) => {
  90. fs_1.default.writeFileSync(path_1.default.join(cachePath, TARGETS_DIR_NAME, targetName), Buffer.from(target, 'base64'));
  91. });
  92. }
  93. }
  94. }
  95. function initClient(options) {
  96. const config = {
  97. fetchTimeout: options.timeout,
  98. fetchRetry: options.retry,
  99. };
  100. return new tuf_js_1.Updater({
  101. metadataBaseUrl: options.mirrorURL,
  102. targetBaseUrl: `${options.mirrorURL}/targets`,
  103. metadataDir: options.cachePath,
  104. targetDir: path_1.default.join(options.cachePath, TARGETS_DIR_NAME),
  105. forceCache: options.forceCache,
  106. config,
  107. });
  108. }