summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/sigstore/dist/tuf/index.js
blob: 824bce9105ed8eb9c5024825ffca84c88f6f734b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTrustedRoot = void 0;
/*
Copyright 2023 The Sigstore Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const tuf_js_1 = require("tuf-js");
const sigstore = __importStar(require("../types/sigstore"));
const target_1 = require("./target");
const TRUSTED_ROOT_TARGET = 'trusted_root.json';
const DEFAULT_MIRROR_URL = 'https://sigstore-tuf-root.storage.googleapis.com';
const DEFAULT_TUF_ROOT_PATH = '../../store/public-good-instance-root.json';
async function getTrustedRoot(cachePath, options = {}) {
    const tufRootPath = options.rootPath || require.resolve(DEFAULT_TUF_ROOT_PATH);
    const mirrorURL = options.mirrorURL || DEFAULT_MIRROR_URL;
    initTufCache(cachePath, tufRootPath);
    const remote = initRemoteConfig(cachePath, mirrorURL);
    const repoClient = initClient(cachePath, remote);
    const trustedRoot = await (0, target_1.getTarget)(repoClient, TRUSTED_ROOT_TARGET);
    return sigstore.TrustedRoot.fromJSON(JSON.parse(trustedRoot));
}
exports.getTrustedRoot = getTrustedRoot;
// Initializes the TUF cache directory structure including the initial
// root.json file. If the cache directory does not exist, it will be
// created. If the targets directory does not exist, it will be created.
// If the root.json file does not exist, it will be copied from the
// rootPath argument.
function initTufCache(cachePath, tufRootPath) {
    const targetsPath = path_1.default.join(cachePath, 'targets');
    const cachedRootPath = path_1.default.join(cachePath, 'root.json');
    if (!fs_1.default.existsSync(cachePath)) {
        fs_1.default.mkdirSync(cachePath, { recursive: true });
    }
    if (!fs_1.default.existsSync(targetsPath)) {
        fs_1.default.mkdirSync(targetsPath);
    }
    if (!fs_1.default.existsSync(cachedRootPath)) {
        fs_1.default.copyFileSync(tufRootPath, cachedRootPath);
    }
    return cachePath;
}
// Initializes the remote.json file, which contains the URL of the TUF
// repository. If the file does not exist, it will be created. If the file
// exists, it will be parsed and returned.
function initRemoteConfig(rootDir, mirrorURL) {
    let remoteConfig;
    const remoteConfigPath = path_1.default.join(rootDir, 'remote.json');
    if (fs_1.default.existsSync(remoteConfigPath)) {
        const data = fs_1.default.readFileSync(remoteConfigPath, 'utf-8');
        remoteConfig = JSON.parse(data);
    }
    if (!remoteConfig) {
        remoteConfig = { mirror: mirrorURL };
        fs_1.default.writeFileSync(remoteConfigPath, JSON.stringify(remoteConfig));
    }
    return remoteConfig;
}
function initClient(cachePath, remote) {
    const baseURL = remote.mirror;
    return new tuf_js_1.Updater({
        metadataBaseUrl: baseURL,
        targetBaseUrl: `${baseURL}/targets`,
        metadataDir: cachePath,
        targetDir: path_1.default.join(cachePath, 'targets'),
    });
}