Skip to content

Commit

Permalink
Added stderr to certificate generation notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Seb Julliand <[email protected]>
  • Loading branch information
sebjulliand committed Mar 25, 2024
1 parent 5c9bc4e commit 5a62fcc
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/api/debug/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function setup(connection: IBMi) {
`openssl req -new -key debug_service.key -out debug_service.csr -subj '/CN=${host}'`,
`openssl x509 -req -in debug_service.csr -signkey debug_service.key -out debug_service.crt -days 1095 -sha256 -req -extfile <(printf "${extFileContent}")`,
`openssl pkcs12 -export -out debug_service.pfx -inkey debug_service.key -in debug_service.crt -password pass:${host}`,
`rm debug_service.key debug_service.csr debug_service.crt`,
`rm debug_service.key debug_service.csr debug_service.crt`,
`chmod 444 debug_service.pfx`
];

Expand All @@ -112,7 +112,7 @@ export async function setup(connection: IBMi) {
});

if (mkdirResult.code && mkdirResult.code > 0) {
throw new Error(`Failed to create server certificate directory: ${directory}`);
throw new Error(`Failed to create server certificate directory ${directory}: ${mkdirResult.stderr}`);
}

const creationResults = await connection.sendCommand({
Expand All @@ -121,23 +121,26 @@ export async function setup(connection: IBMi) {
});

if (creationResults.code && creationResults.code > 0) {
throw new Error(`Failed to create server certificate.`);
throw new Error(`Failed to create server certificate: ${creationResults.stderr}`);
}
}

export async function downloadClientCert(connection: IBMi) {
const localPath = getLocalCertPath(connection);
await fs.writeFile(localPath, await readRemoteCertificate(connection), { encoding: `utf8` });
}

export async function readRemoteCertificate(connection: IBMi) {
const result = await connection.sendCommand({
command: `openssl s_client -connect localhost:${connection.config?.debugPort} -showcerts < /dev/null 2> /dev/null | openssl x509 -outform PEM`,
directory: getRemoteCertificateDirectory(connection)
});

if (result.code && result.code > 0) {
throw new Error(`Failed to download client certificate.`);
throw new Error(`Failed to download client certificate: ${result.stderr}`);
}

await fs.writeFile(localPath, result.stdout, {encoding: `utf8`});
return result.stdout;
}

export function getLocalCertPath(connection: IBMi) {
Expand All @@ -161,7 +164,7 @@ export async function legacyCertificateChecks(connection: IBMi, serviceIsRunning
const usingLegacyCertPath = (getRemoteCertificateDirectory(connection) === LEGACY_CERT_DIRECTORY);
const certsExistAtConfig = await remoteServerCertificateExists(connection);

let changeCertDirConfig: string|undefined;
let changeCertDirConfig: string | undefined;

if (usingLegacyCertPath) {
if (serviceIsRunning) {
Expand Down

0 comments on commit 5a62fcc

Please sign in to comment.