Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add support to secret env #110

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions __tests__/buildx/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ describe('resolveBuildSecret', () => {
expect(e.message).toEqual(error?.message);
}
});

test.each([
['FOO=bar', 'FOO', 'bar', null],
['FOO=', 'FOO', '', new Error('FOO= is not a valid secret')],
['=bar', '', '', new Error('=bar is not a valid secret')],
['FOO=bar=baz', 'FOO', 'bar=baz', null]
])('given %p key and %p env', async (kvp: string, exKey: string, exValue: string, error: Error | null) => {
try {
const secret = Inputs.resolveBuildSecretEnv(kvp);
expect(secret).toEqual(`id=${exKey},env="${exValue}"`);
} catch (e) {
// eslint-disable-next-line jest/no-conditional-expect
expect(e.message).toEqual(error?.message);
}
});
});

describe('hasLocalExporter', () => {
Expand Down
28 changes: 22 additions & 6 deletions src/buildx/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ import {parse} from 'csv-parse/sync';

import {Context} from '../context';

const parseKvp = (kvp: string): [string, string] => {
const delimiterIndex = kvp.indexOf('=');
const key = kvp.substring(0, delimiterIndex);
const value = kvp.substring(delimiterIndex + 1);

if (key.length == 0 || value.length == 0) {
throw new Error(`${kvp} is not a valid secret`);
}

return [key, value];
};

export class Inputs {
public static getBuildImageIDFilePath(): string {
return path.join(Context.tmpDir(), 'iidfile');
Expand Down Expand Up @@ -70,13 +82,17 @@ export class Inputs {
return Inputs.resolveBuildSecret(kvp, true);
}

public static resolveBuildSecretEnv(kvp: string): string {
const [key, value] = parseKvp(kvp);

return `id=${key},env="${value}"`;
}

public static resolveBuildSecret(kvp: string, file: boolean): string {
const delimiterIndex = kvp.indexOf('=');
const key = kvp.substring(0, delimiterIndex);
let value = kvp.substring(delimiterIndex + 1);
if (key.length == 0 || value.length == 0) {
throw new Error(`${kvp} is not a valid secret`);
}
const [key, _value] = parseKvp(kvp);

let value = _value;

if (file) {
if (!fs.existsSync(value)) {
throw new Error(`secret file ${value} not found`);
Expand Down