Skip to content

Commit

Permalink
buildx: hasAttestationType and resolveAttestationAttrs funcs
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Mar 26, 2024
1 parent de2888a commit 545f7cd
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
35 changes: 35 additions & 0 deletions __tests__/buildx/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,41 @@ describe('hasDockerExporter', () => {
});
});

describe('hasAttestationType', () => {
// prettier-ignore
test.each([
['type=provenance,mode=min', 'provenance', true],
['type=sbom,true', 'sbom', true],
['type=foo,bar', 'provenance', false],
])('given %p for %p returns %p', async (attrs: string, name: string, expected: boolean) => {
expect(Inputs.hasAttestationType(name, attrs)).toEqual(expected);
});
});

describe('resolveAttestationAttrs', () => {
// prettier-ignore
test.each([
[
'type=provenance,mode=min',
'type=provenance,mode=min'
],
[
'type=provenance,true',
'type=provenance,disabled=false'
],
[
'type=provenance,false',
'type=provenance,disabled=true'
],
[
'',
''
],
])('given %p', async (input: string, expected: string) => {
expect(Inputs.resolveAttestationAttrs(input)).toEqual(expected);
});
});

describe('hasGitAuthTokenSecret', () => {
// prettier-ignore
test.each([
Expand Down
30 changes: 30 additions & 0 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,36 @@ describe('hash', () => {
});
});

// https://github.com/golang/go/blob/f6b93a4c358b28b350dd8fe1780c1f78e520c09c/src/strconv/atob_test.go#L36-L58
describe('parseBool', () => {
[
{input: '', expected: false, throwsError: true},
{input: 'asdf', expected: false, throwsError: true},
{input: '0', expected: false, throwsError: false},
{input: 'f', expected: false, throwsError: false},
{input: 'F', expected: false, throwsError: false},
{input: 'FALSE', expected: false, throwsError: false},
{input: 'false', expected: false, throwsError: false},
{input: 'False', expected: false, throwsError: false},
{input: '1', expected: true, throwsError: false},
{input: 't', expected: true, throwsError: false},
{input: 'T', expected: true, throwsError: false},
{input: 'TRUE', expected: true, throwsError: false},
{input: 'true', expected: true, throwsError: false},
{input: 'True', expected: true, throwsError: false}
].forEach(({input, expected, throwsError}) => {
test(`parseBool("${input}")`, () => {
if (throwsError) {
// eslint-disable-next-line jest/no-conditional-expect
expect(() => Util.parseBool(input)).toThrow();
} else {
// eslint-disable-next-line jest/no-conditional-expect
expect(Util.parseBool(input)).toBe(expected);
}
});
});
});

// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
Expand Down
40 changes: 40 additions & 0 deletions src/buildx/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {parse} from 'csv-parse/sync';

import {Context} from '../context';
import {GitHub} from '../github';
import {Util} from '../util';

const parseKvp = (kvp: string): [string, string] => {
const delimiterIndex = kvp.indexOf('=');
Expand Down Expand Up @@ -176,6 +177,45 @@ export class Inputs {
return false;
}

public static hasAttestationType(name: string, attrs: string): boolean {
const records = parse(attrs, {
delimiter: ',',
trim: true,
columns: false,
relaxColumnCount: true
});
for (const record of records) {
for (const [key, value] of record.map((chunk: string) => chunk.split('=').map(item => item.trim()))) {
if (key == 'type' && value == name) {
return true;
}
}
}
return false;
}

public static resolveAttestationAttrs(attrs: string): string {
const records = parse(attrs, {
delimiter: ',',
trim: true,
columns: false,
relaxColumnCount: true
});
const res: Array<string> = [];
for (const record of records) {
for (const attr of record) {
try {
// https://github.com/docker/buildx/blob/8abef5908705e49f7ba88ef8c957e1127b597a2a/util/buildflags/attests.go#L13-L21
const v = Util.parseBool(attr);
res.push(`disabled=${!v}`);
} catch (err) {
res.push(attr);
}
}
}
return res.join(',');
}

public static hasGitAuthTokenSecret(secrets: string[]): boolean {
for (const secret of secrets) {
if (secret.startsWith('GIT_AUTH_TOKEN=')) {
Expand Down
22 changes: 22 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,26 @@ export class Util {
public static hash(input: string): string {
return crypto.createHash('sha256').update(input).digest('hex');
}

// https://github.com/golang/go/blob/f6b93a4c358b28b350dd8fe1780c1f78e520c09c/src/strconv/atob.go#L7-L18
public static parseBool(str: string): boolean {
switch (str) {
case '1':
case 't':
case 'T':
case 'true':
case 'TRUE':
case 'True':
return true;
case '0':
case 'f':
case 'F':
case 'false':
case 'FALSE':
case 'False':
return false;
default:
throw new Error(`parseBool syntax error: ${str}`);
}
}
}

0 comments on commit 545f7cd

Please sign in to comment.