Skip to content

Commit

Permalink
sigstore type refactoring (#550)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <bdehamer@github.com>
  • Loading branch information
bdehamer committed Jun 12, 2023
1 parent 24abc28 commit bd1e1e1
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 131 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-mangos-hug.md
@@ -0,0 +1,5 @@
---
'sigstore': patch
---

Internal refactoring of Typescript types
5 changes: 3 additions & 2 deletions packages/client/src/__tests__/__fixtures__/trust.ts
Expand Up @@ -13,7 +13,8 @@ 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.
*/
import * as sigstore from '../../types/sigstore';
import { TrustedRoot } from '@sigstore/protobuf-specs';

const trustedRootJSON = {
mediaType: 'application/vnd.dev.sigstore.trustedroot+json;version=0.1',
tlogs: [
Expand Down Expand Up @@ -103,4 +104,4 @@ const trustedRootJSON = {
timestampAuthorities: [],
};

export const trustedRoot = sigstore.TrustedRoot.fromJSON(trustedRootJSON);
export const trustedRoot = TrustedRoot.fromJSON(trustedRootJSON);
2 changes: 1 addition & 1 deletion packages/client/src/__tests__/ca/verify/index.test.ts
Expand Up @@ -21,7 +21,7 @@ import { trustedRoot } from '../../__fixtures__/trust';
describe('verifySigningCertificate', () => {
// Temporary until we reconsole bundle formats
const bundleJSON = bundles.dsse.valid.withSigningCert;
const bundle = sigstore.Bundle.fromJSON(
const bundle = sigstore.bundleFromJSON(
bundleJSON
) as sigstore.BundleWithCertificateChain;

Expand Down
12 changes: 6 additions & 6 deletions packages/client/src/__tests__/sigstore.test.ts
Expand Up @@ -13,19 +13,19 @@ 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.
*/
import { TUFError } from '@sigstore/tuf';
import mocktuf, { Target } from '@tufjs/repo-mock';
import { PolicyError, VerificationError } from '../error';
import { Signer } from '../sign';
import { attest, sign, tuf, verify } from '../sigstore';
import {
Bundle,
HashAlgorithm,
TimestampVerificationData,
TransparencyLogEntry,
TrustedRoot,
X509CertificateChain,
} from '../types/sigstore';
} from '@sigstore/protobuf-specs';
import { TUFError } from '@sigstore/tuf';
import mocktuf, { Target } from '@tufjs/repo-mock';
import { PolicyError, VerificationError } from '../error';
import { Signer } from '../sign';
import { attest, sign, tuf, verify } from '../sigstore';
import bundles from './__fixtures__/bundles';
import { trustedRoot } from './__fixtures__/trust';

Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/__tests__/tlog/verify/index.test.ts
Expand Up @@ -22,7 +22,7 @@ import { trustedRoot } from '../../__fixtures__/trust';
describe('verifyTLogEntries', () => {
const bundle = sigstore.bundleFromJSON(
bundles.signature.valid.withSigningCert
) as sigstore.BundleWithVerificationMaterial;
) as sigstore.Bundle;

const options: sigstore.ArtifactVerificationOptions_TlogOptions = {
disable: false,
Expand All @@ -42,7 +42,7 @@ describe('verifyTLogEntries', () => {
describe('when the bundle does NOT have a signing certificate', () => {
const bundle = sigstore.bundleFromJSON(
bundles.signature.valid.withPublicKey
) as sigstore.BundleWithVerificationMaterial;
) as sigstore.Bundle;

it('does NOT throw an error', () => {
expect(() =>
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('verifyTLogEntries', () => {
describe('when tlog entries are missing data necessary for verification', () => {
const bundle = sigstore.bundleFromJSON(
bundles.dsse.invalid.tlogKindVersionMissing
) as sigstore.BundleWithVerificationMaterial;
) as sigstore.Bundle;

it('throws an error', () => {
expect(() => verifyTLogEntries(bundle, trustedRoot, options)).toThrow(
Expand Down
39 changes: 3 additions & 36 deletions packages/client/src/__tests__/types/sigstore/index.test.ts
Expand Up @@ -13,49 +13,16 @@ 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.
*/
import type { Entry } from '../../../external/rekor';
import { SignatureMaterial } from '../../../types/signature';
import * as sigstore from '../../../types/sigstore';
import { encoding as enc, pem } from '../../../util';
import bundles from '../../__fixtures__/bundles/';

import type { Entry } from '../../../external/rekor';

describe('isBundleWithVerificationMaterial', () => {
describe('when the bundle contains verification material', () => {
const json = bundles.dsse.valid.withSigningCert;
const bundle = sigstore.Bundle.fromJSON(json);

it('returns true', () => {
expect(sigstore.isBundleWithVerificationMaterial(bundle)).toBe(true);
});
});

describe('when the bundle does NOT contain verification material', () => {
const bundle: sigstore.Bundle = {
mediaType: 'application/vnd.dev.cosign.simplesigning.v1+json',
verificationMaterial: undefined,
content: {
$case: 'messageSignature',
messageSignature: {
messageDigest: {
algorithm: sigstore.HashAlgorithm.SHA2_256,
digest: Buffer.from(''),
},
signature: Buffer.from(''),
},
},
};

it('returns false', () => {
expect(sigstore.isBundleWithVerificationMaterial(bundle)).toBe(false);
});
});
});

describe('isBundleWithCertificateChain', () => {
describe('when the bundle contains a certificate chain', () => {
const json = bundles.dsse.valid.withSigningCert;
const bundle = sigstore.Bundle.fromJSON(json);
const bundle = sigstore.bundleFromJSON(json);

it('returns true', () => {
expect(sigstore.isBundleWithCertificateChain(bundle)).toBe(true);
Expand All @@ -64,7 +31,7 @@ describe('isBundleWithCertificateChain', () => {

describe('when the bundle does NOT contain a certificate chain', () => {
const json = bundles.dsse.valid.withPublicKey;
const bundle = sigstore.Bundle.fromJSON(json);
const bundle = sigstore.bundleFromJSON(json);

it('returns false', () => {
expect(sigstore.isBundleWithCertificateChain(bundle)).toBe(false);
Expand Down
Expand Up @@ -13,11 +13,10 @@ 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.
*/
import {
Bundle,
import { Bundle, hashAlgorithmToJSON } from '@sigstore/protobuf-specs';
import type {
Envelope,
HashAlgorithm,
hashAlgorithmToJSON,
MessageSignature,
PublicKeyIdentifier,
SerializedBundle,
Expand Down
7 changes: 4 additions & 3 deletions packages/client/src/__tests__/types/sigstore/validate.test.ts
Expand Up @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { ValidationError } from '../../../error';
import {
assertValidBundle,
import { assertValidBundle } from '../../../types/sigstore/validate';

import type {
Bundle,
Signature,
X509Certificate,
} from '../../../types/sigstore';
} from '@sigstore/protobuf-specs';

describe('assertValidBundle', () => {
describe('when the bundle is completely empty', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/__tests__/x509/cert.test.ts
Expand Up @@ -13,7 +13,7 @@ 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.
*/
import * as sigstore from '../../types/sigstore';
import { TransparencyLogInstance } from '@sigstore/protobuf-specs';
import { pem } from '../../util';
import { x509Certificate } from '../../x509/cert';
import { certificates } from '../__fixtures__/certs';
Expand Down Expand Up @@ -290,8 +290,8 @@ describe('x509Certificate', () => {
logId: { keyId: 'CGCS8ChS/2hF0dFrJ4ScRWcYrBY9wzjSbea8IgY2b3I=' },
};

const logs: sigstore.TransparencyLogInstance[] = [
sigstore.TransparencyLogInstance.fromJSON(ctl),
const logs: TransparencyLogInstance[] = [
TransparencyLogInstance.fromJSON(ctl),
];

describe('when the certificate does NOT have an SCT extension', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/__tests__/x509/sct.test.ts
Expand Up @@ -13,7 +13,7 @@ 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.
*/
import * as sigstore from '../../types/sigstore';
import { TransparencyLogInstance } from '@sigstore/protobuf-specs';
import { SignedCertificateTimestamp } from '../../x509/sct';

describe('SignedCertificateTimestamp', () => {
Expand Down Expand Up @@ -130,8 +130,8 @@ describe('SignedCertificateTimestamp', () => {
logId: { keyId: Buffer.from(logID, 'hex') },
};

const logs: sigstore.TransparencyLogInstance[] = [
sigstore.TransparencyLogInstance.fromJSON(ctl),
const logs: TransparencyLogInstance[] = [
TransparencyLogInstance.fromJSON(ctl),
];

describe('when the signature is valid', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/sigstore-utils.ts
Expand Up @@ -65,5 +65,5 @@ export async function createRekorEntry(
signature: sigMaterial,
tlogEntry: entry,
});
return sigstore.Bundle.toJSON(bundle) as sigstore.SerializedBundle;
return sigstore.bundleToJSON(bundle) as sigstore.SerializedBundle;
}
4 changes: 2 additions & 2 deletions packages/client/src/sigstore.ts
Expand Up @@ -36,7 +36,7 @@ export async function sign(
});

const bundle = await signer.signBlob(payload);
return sigstore.Bundle.toJSON(bundle) as sigstore.SerializedBundle;
return sigstore.bundleToJSON(bundle) as sigstore.SerializedBundle;
}

export async function attest(
Expand All @@ -59,7 +59,7 @@ export async function attest(
});

const bundle = await signer.signAttestation(payload, payloadType);
return sigstore.Bundle.toJSON(bundle) as sigstore.SerializedBundle;
return sigstore.bundleToJSON(bundle) as sigstore.SerializedBundle;
}

export async function verify(
Expand Down
16 changes: 14 additions & 2 deletions packages/client/src/tlog/verify/index.ts
Expand Up @@ -22,7 +22,7 @@ import { verifyTLogSET } from './set';
// Verifies that the number of tlog entries that pass offline verification
// is greater than or equal to the threshold specified in the options.
export function verifyTLogEntries(
bundle: sigstore.BundleWithVerificationMaterial,
bundle: sigstore.Bundle,
trustedRoot: sigstore.TrustedRoot,
options: sigstore.ArtifactVerificationOptions_TlogOptions
): void {
Expand All @@ -31,7 +31,7 @@ export function verifyTLogEntries(
}

// Extract the signing cert, if available
const signingCert = sigstore.signingCertificate(bundle);
const signingCert = signingCertificate(bundle);

// Iterate over the tlog entries and verify each one
const verifiedEntries = bundle.verificationMaterial.tlogEntries.filter(
Expand Down Expand Up @@ -74,3 +74,15 @@ function verifyTLogEntryOffline(
verifyTLogIntegrationTime()
);
}

function signingCertificate(
bundle: sigstore.Bundle
): x509Certificate | undefined {
if (!sigstore.isBundleWithCertificateChain(bundle)) {
return undefined;
}

const signingCert =
bundle.verificationMaterial.content.x509CertificateChain.certificates[0];
return x509Certificate.parse(signingCert.rawBytes);
}

0 comments on commit bd1e1e1

Please sign in to comment.