1"use strict"; 2Object.defineProperty(exports, "__esModule", { value: true }); 3exports.TLogClient = void 0; 4/* 5Copyright 2022 The Sigstore Authors. 6 7Licensed under the Apache License, Version 2.0 (the "License"); 8you may not use this file except in compliance with the License. 9You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13Unless required by applicable law or agreed to in writing, software 14distributed under the License is distributed on an "AS IS" BASIS, 15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16See the License for the specific language governing permissions and 17limitations under the License. 18*/ 19const error_1 = require("../error"); 20const external_1 = require("../external"); 21const format_1 = require("./format"); 22class TLogClient { 23 constructor(options) { 24 this.rekor = new external_1.Rekor({ 25 baseURL: options.rekorBaseURL, 26 retry: options.retry, 27 timeout: options.timeout, 28 }); 29 } 30 async createMessageSignatureEntry(digest, sigMaterial, options = {}) { 31 const proposedEntry = (0, format_1.toProposedHashedRekordEntry)(digest, sigMaterial); 32 return this.createEntry(proposedEntry, options.fetchOnConflict); 33 } 34 async createDSSEEntry(envelope, sigMaterial, options = {}) { 35 const proposedEntry = (0, format_1.toProposedIntotoEntry)(envelope, sigMaterial); 36 return this.createEntry(proposedEntry, options.fetchOnConflict); 37 } 38 async createEntry(proposedEntry, fetchOnConflict = false) { 39 let entry; 40 try { 41 entry = await this.rekor.createEntry(proposedEntry); 42 } 43 catch (err) { 44 // If the entry already exists, fetch it (if enabled) 45 if (entryExistsError(err) && fetchOnConflict) { 46 // Grab the UUID of the existing entry from the location header 47 const uuid = err.location.split('/').pop() || ''; 48 try { 49 entry = await this.rekor.getEntry(uuid); 50 } 51 catch (err) { 52 throw new error_1.InternalError({ 53 code: 'TLOG_FETCH_ENTRY_ERROR', 54 message: 'error fetching tlog entry', 55 cause: err, 56 }); 57 } 58 } 59 else { 60 throw new error_1.InternalError({ 61 code: 'TLOG_CREATE_ENTRY_ERROR', 62 message: 'error creating tlog entry', 63 cause: err, 64 }); 65 } 66 } 67 return entry; 68 } 69} 70exports.TLogClient = TLogClient; 71function entryExistsError(value) { 72 return (value instanceof external_1.HTTPError && 73 value.statusCode === 409 && 74 value.location !== undefined); 75} 76