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

Provider exec error reporting #43

Merged
merged 1 commit into from
Jan 25, 2021
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
20 changes: 16 additions & 4 deletions provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const bytesToString = (byteArray: Uint8Array): string => {
};

const execPromise = async (args: Array<string>, input: string): Promise<string> => {
return new Promise((res: (result: string) => void, rej: (error: childProcess.ExecException) => void): void => {
return new Promise((res: (result: string) => void, rej: (error: Error) => void): void => {
const proc = childProcess.spawn('sh', ['-c', 'cat', '-', '|', ...args], { stdio: 'pipe', shell: true });
(proc.stdin as Writable).end(input);

Expand All @@ -137,21 +137,33 @@ const execPromise = async (args: Array<string>, input: string): Promise<string>

proc.on('close', (code: number) => {
if (code > 0) {
rej({
name: `Exited with code ${code}`,
message: stderr,
log(`Exec exited with code ${code}`, {
stdout,
stderr,
});
rej(new Error(`Exec exited with code ${code}`));
} else {
if (stderr) {
log(`Exec exited cleanly, but stderr was not empty`, {
stderr,
});
} else {
log('Exec exited cleanly');
}
res(stdout);
}
});
});
};

const sopsDecode = async (fileContent: string, dataType: string, kmsKeyArn: string | undefined): Promise<unknown> => {
log('Running sops command');
const sopsArgs = ['-d', '--input-type', dataType, '--output-type', 'json', ...(kmsKeyArn ? ['--kms', kmsKeyArn] : []), '/dev/stdin'];
log('Sops command args', { sopsArgs });
const result = await execPromise([path.join(__dirname, 'sops'), ...sopsArgs], fileContent);
log('Sops command result', { result });
const parsed = JSON.parse(result);
log('Sops command result', { parsed });
return Promise.resolve(parsed);
};

Expand Down
2 changes: 1 addition & 1 deletion provider/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
statements: 86,
branches: 65,
functions: 90,
lines: 86,
lines: 85,
},
},
};