mirror of https://github.com/actions/toolkit
add subjects option to attest
Signed-off-by: Adam Nauth <Forrin@users.noreply.github.com>pull/1749/head
parent
c6b487124a
commit
1952a763b3
|
@ -17,7 +17,7 @@ describe('buildIntotoStatement', () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
it('returns an intoto statement', () => {
|
it('returns an intoto statement', () => {
|
||||||
const statement = buildIntotoStatement(subject, predicate)
|
const statement = buildIntotoStatement([subject], predicate)
|
||||||
expect(statement).toMatchSnapshot()
|
expect(statement).toMatchSnapshot()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -5,6 +5,7 @@ import nock from 'nock'
|
||||||
import {MockAgent, setGlobalDispatcher} from 'undici'
|
import {MockAgent, setGlobalDispatcher} from 'undici'
|
||||||
import {SIGSTORE_PUBLIC_GOOD, signingEndpoints} from '../src/endpoints'
|
import {SIGSTORE_PUBLIC_GOOD, signingEndpoints} from '../src/endpoints'
|
||||||
import {attestProvenance, buildSLSAProvenancePredicate} from '../src/provenance'
|
import {attestProvenance, buildSLSAProvenancePredicate} from '../src/provenance'
|
||||||
|
import type {Subject} from '../src/shared.types'
|
||||||
|
|
||||||
describe('provenance functions', () => {
|
describe('provenance functions', () => {
|
||||||
const originalEnv = process.env
|
const originalEnv = process.env
|
||||||
|
@ -79,6 +80,22 @@ describe('provenance functions', () => {
|
||||||
sha256: '7d070f6b64d9bcc530fe99cc21eaaa4b3c364e0b2d367d7735671fa202a03b32'
|
sha256: '7d070f6b64d9bcc530fe99cc21eaaa4b3c364e0b2d367d7735671fa202a03b32'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const subjects: Subject[] = [
|
||||||
|
{
|
||||||
|
name: 'subjective',
|
||||||
|
digest: {
|
||||||
|
sha256:
|
||||||
|
'7d070f6b64d9bcc530fe99cc21eaaa4b3c364e0b2d367d7735671fa202a03b32'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'subject_two',
|
||||||
|
digest: {
|
||||||
|
gitcommit: 'c6b487124a61d7dc6c7bd6ea0208368af3513a6e'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
// Fake an OIDC token
|
// Fake an OIDC token
|
||||||
const oidcPayload = {sub: 'foo@bar.com', iss: ''}
|
const oidcPayload = {sub: 'foo@bar.com', iss: ''}
|
||||||
const oidcToken = `.${Buffer.from(JSON.stringify(oidcPayload)).toString(
|
const oidcToken = `.${Buffer.from(JSON.stringify(oidcPayload)).toString(
|
||||||
|
@ -114,8 +131,7 @@ describe('provenance functions', () => {
|
||||||
describe('when the sigstore instance is explicitly set', () => {
|
describe('when the sigstore instance is explicitly set', () => {
|
||||||
it('attests provenance', async () => {
|
it('attests provenance', async () => {
|
||||||
const attestation = await attestProvenance({
|
const attestation = await attestProvenance({
|
||||||
subjectName,
|
subjects,
|
||||||
subjectDigest,
|
|
||||||
token: 'token',
|
token: 'token',
|
||||||
sigstore: 'github',
|
sigstore: 'github',
|
||||||
issuer
|
issuer
|
||||||
|
|
|
@ -15,10 +15,16 @@ const INTOTO_PAYLOAD_TYPE = 'application/vnd.in-toto+json'
|
||||||
*/
|
*/
|
||||||
export type AttestOptions = {
|
export type AttestOptions = {
|
||||||
// The name of the subject to be attested.
|
// The name of the subject to be attested.
|
||||||
subjectName: string
|
// @deprecated see 'subjects'
|
||||||
|
subjectName?: string
|
||||||
// The digest of the subject to be attested. Should be a map of digest
|
// The digest of the subject to be attested. Should be a map of digest
|
||||||
// algorithms to their hex-encoded values.
|
// algorithms to their hex-encoded values.
|
||||||
subjectDigest: Record<string, string>
|
// @deprecated see 'subjects'
|
||||||
|
subjectDigest?: Record<string, string>
|
||||||
|
// The subjects to be attested
|
||||||
|
// Includes the digest(s) of the subject to be attested. Should be a map of digest
|
||||||
|
// algorithms to their hex-encoded values.
|
||||||
|
subjects?: Subject[]
|
||||||
// Content type of the predicate being attested.
|
// Content type of the predicate being attested.
|
||||||
predicateType: string
|
predicateType: string
|
||||||
// Predicate to be attested.
|
// Predicate to be attested.
|
||||||
|
@ -40,10 +46,18 @@ export type AttestOptions = {
|
||||||
* @returns A promise that resolves to the attestation.
|
* @returns A promise that resolves to the attestation.
|
||||||
*/
|
*/
|
||||||
export async function attest(options: AttestOptions): Promise<Attestation> {
|
export async function attest(options: AttestOptions): Promise<Attestation> {
|
||||||
const subject: Subject = {
|
let subject = [] as Subject[]
|
||||||
name: options.subjectName,
|
if (options.subjects && options.subjects.length > 0) {
|
||||||
digest: options.subjectDigest
|
subject = options.subjects
|
||||||
|
} else if (options.subjectName && options.subjectDigest) {
|
||||||
|
subject = [
|
||||||
|
{
|
||||||
|
name: options.subjectName,
|
||||||
|
digest: options.subjectDigest
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
const predicate: Predicate = {
|
const predicate: Predicate = {
|
||||||
type: options.predicateType,
|
type: options.predicateType,
|
||||||
params: options.predicate
|
params: options.predicate
|
||||||
|
|
|
@ -20,12 +20,12 @@ export type InTotoStatement = {
|
||||||
* @returns The constructed in-toto statement.
|
* @returns The constructed in-toto statement.
|
||||||
*/
|
*/
|
||||||
export const buildIntotoStatement = (
|
export const buildIntotoStatement = (
|
||||||
subject: Subject,
|
subject: Subject[],
|
||||||
predicate: Predicate
|
predicate: Predicate
|
||||||
): InTotoStatement => {
|
): InTotoStatement => {
|
||||||
return {
|
return {
|
||||||
_type: INTOTO_STATEMENT_V1_TYPE,
|
_type: INTOTO_STATEMENT_V1_TYPE,
|
||||||
subject: [subject],
|
subject,
|
||||||
predicateType: predicate.type,
|
predicateType: predicate.type,
|
||||||
predicate: predicate.params
|
predicate: predicate.params
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue