Skip to content

Commit

Permalink
✨ Add support to secret env
Browse files Browse the repository at this point in the history
  • Loading branch information
vinicius73 committed Jun 1, 2023
1 parent fdd740d commit a1ffbe9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
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

0 comments on commit a1ffbe9

Please sign in to comment.