| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.TLogV2Client = exports.TLogClient = void 0; |
| 4 |
/* |
| 5 |
Copyright 2025 The Sigstore Authors. |
| 6 |
|
| 7 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
you may not use this file except in compliance with the License. |
| 9 |
You may obtain a copy of the License at |
| 10 |
|
| 11 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
|
| 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 error_1 = require("../../error"); |
| 20 |
const error_2 = require("../../external/error"); |
| 21 |
const rekor_1 = require("../../external/rekor"); |
| 22 |
const rekor_v2_1 = require("../../external/rekor-v2"); |
| 23 |
class TLogClient { |
| 24 |
rekor; |
| 25 |
fetchOnConflict; |
| 26 |
constructor(options) { |
| 27 |
this.fetchOnConflict = options.fetchOnConflict ?? false; |
| 28 |
this.rekor = new rekor_1.Rekor({ |
| 29 |
baseURL: options.rekorBaseURL, |
| 30 |
retry: options.retry, |
| 31 |
timeout: options.timeout, |
| 32 |
}); |
| 33 |
} |
| 34 |
async createEntry(proposedEntry) { |
| 35 |
let entry; |
| 36 |
try { |
| 37 |
entry = await this.rekor.createEntry(proposedEntry); |
| 38 |
} |
| 39 |
catch (err) { |
| 40 |
// If the entry already exists, fetch it (if enabled) |
| 41 |
if (entryExistsError(err) && this.fetchOnConflict) { |
| 42 |
// Grab the UUID of the existing entry from the location header |
| 43 |
/* istanbul ignore next */ |
| 44 |
const uuid = err.location.split('/').pop() || ''; |
| 45 |
try { |
| 46 |
entry = await this.rekor.getEntry(uuid); |
| 47 |
} |
| 48 |
catch (err) { |
| 49 |
(0, error_1.internalError)(err, 'TLOG_FETCH_ENTRY_ERROR', 'error fetching tlog entry'); |
| 50 |
} |
| 51 |
} |
| 52 |
else { |
| 53 |
(0, error_1.internalError)(err, 'TLOG_CREATE_ENTRY_ERROR', 'error creating tlog entry'); |
| 54 |
} |
| 55 |
} |
| 56 |
return entry; |
| 57 |
} |
| 58 |
} |
| 59 |
exports.TLogClient = TLogClient; |
| 60 |
function entryExistsError(value) { |
| 61 |
return (value instanceof error_2.HTTPError && |
| 62 |
value.statusCode === 409 && |
| 63 |
value.location !== undefined); |
| 64 |
} |
| 65 |
class TLogV2Client { |
| 66 |
rekor; |
| 67 |
constructor(options) { |
| 68 |
this.rekor = new rekor_v2_1.RekorV2({ |
| 69 |
baseURL: options.rekorBaseURL, |
| 70 |
retry: options.retry, |
| 71 |
timeout: options.timeout, |
| 72 |
}); |
| 73 |
} |
| 74 |
async createEntry(createEntryRequest) { |
| 75 |
let entry; |
| 76 |
try { |
| 77 |
entry = await this.rekor.createEntry(createEntryRequest); |
| 78 |
} |
| 79 |
catch (err) { |
| 80 |
(0, error_1.internalError)(err, 'TLOG_CREATE_ENTRY_ERROR', 'error creating tlog entry'); |
| 81 |
} |
| 82 |
if (entry.logId === undefined || entry.kindVersion === undefined) { |
| 83 |
(0, error_1.internalError)(new Error('invalid tlog entry'), 'TLOG_CREATE_ENTRY_ERROR', 'error creating tlog entry'); |
| 84 |
} |
| 85 |
return { |
| 86 |
...entry, |
| 87 |
logId: entry.logId, |
| 88 |
kindVersion: entry.kindVersion, |
| 89 |
}; |
| 90 |
} |
| 91 |
} |
| 92 |
exports.TLogV2Client = TLogV2Client; |