Fix parsing of secrets containing '=' character

Signed-off-by: Mathieu Bergeron <mathieu.bergeron@nuecho.com>
This commit is contained in:
Mathieu Bergeron 2020-10-23 12:21:44 -04:00
parent c58c6870a2
commit fc7e9a2b38
4 changed files with 154 additions and 3185 deletions

View File

@ -2,9 +2,9 @@ import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import * as buildx from '../src/buildx'; import * as buildx from '../src/buildx';
import * as exec from '@actions/exec';
import * as context from '../src/context'; import * as context from '../src/context';
const tmpNameSync = path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep);
const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9'; const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
jest.spyOn(context, 'tmpDir').mockImplementation((): string => { jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
@ -16,7 +16,7 @@ jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
}); });
jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => { jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
return path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep); return tmpNameSync;
}); });
describe('getImageID', () => { describe('getImageID', () => {
@ -107,3 +107,16 @@ describe('parseVersion', () => {
expect(await buildx.parseVersion(stdout)).toEqual(expected); expect(await buildx.parseVersion(stdout)).toEqual(expected);
}); });
}); });
describe('getSecret', () => {
it('writes correct secret content', async () => {
const key = 'MY_KEY';
const secret = 'c3RyaW5nLXdpdGgtZXF1YWxzCg==';
const secretArgs = await buildx.getSecret(`${key}=${secret}`);
console.log(`secretArgs: ${secretArgs}`);
expect(secretArgs).toEqual(`id=${key},src=${tmpNameSync}`);
const secretContent = await fs.readFileSync(tmpNameSync, 'utf-8');
console.log(`secretValue: ${secretContent}`);
expect(secretContent).toEqual(secret);
});
});

View File

@ -1,6 +1,5 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as buildx from '../src/buildx';
import * as context from '../src/context'; import * as context from '../src/context';
jest.spyOn(context, 'defaultContext').mockImplementation((): string => { jest.spyOn(context, 'defaultContext').mockImplementation((): string => {
@ -107,7 +106,7 @@ describe('getArgs', () => {
'0.4.2', '0.4.2',
new Map<string, string>([ new Map<string, string>([
['context', '.'], ['context', '.'],
['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno0123456789'], ['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'],
]), ]),
[ [
'buildx', 'buildx',
@ -139,7 +138,7 @@ describe('getArgs', () => {
['context', 'https://github.com/docker/build-push-action.git#heads/master'], ['context', 'https://github.com/docker/build-push-action.git#heads/master'],
['tag', 'localhost:5000/name/app:latest'], ['tag', 'localhost:5000/name/app:latest'],
['platforms', 'linux/amd64,linux/arm64'], ['platforms', 'linux/amd64,linux/arm64'],
['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno0123456789'], ['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'],
['file', './test/Dockerfile'], ['file', './test/Dockerfile'],
['builder', 'builder-git-context-2'], ['builder', 'builder-git-context-2'],
['push', 'true'] ['push', 'true']

3309
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

View File

@ -18,7 +18,9 @@ export async function getImageID(): Promise<string | undefined> {
} }
export async function getSecret(kvp: string): Promise<string> { export async function getSecret(kvp: string): Promise<string> {
const [key, value] = kvp.split('='); const sepIndex = kvp.indexOf('=');
const key = kvp.substr(0, sepIndex);
const value = kvp.substr(sepIndex + 1);
const secretFile = context.tmpNameSync({ const secretFile = context.tmpNameSync({
tmpdir: context.tmpDir() tmpdir: context.tmpDir()
}); });