Skip to content

Commit 340499b

Browse files
committed
Correctly fetch client certificate
Signed-off-by: worksofliam <[email protected]>
1 parent 555f58d commit 340499b

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/api/debug/certificates.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,6 @@ export async function remoteServerCertExists(connection: IBMi, legacy = false) {
8585
return list.includes(pfxPath);
8686
}
8787

88-
export async function remoteClientCertExists(connection: IBMi) {
89-
const crtPath = getRemoteClientCertPath(connection);
90-
91-
const dirList = await connection.sendCommand({
92-
command: `ls -p ${crtPath}`
93-
});
94-
95-
const list = dirList.stdout.split(`\n`);
96-
97-
return list.includes(crtPath);
98-
}
99-
10088
/**
10189
* Generate all certifcates on the server
10290
*/
@@ -115,9 +103,9 @@ export async function setup(connection: IBMi) {
115103
const commands = [
116104
`openssl genrsa -out debug_service.key 2048`,
117105
`openssl req -new -key debug_service.key -out debug_service.csr -subj '/CN=${host}'`,
118-
`openssl x509 -req -in debug_service.csr -signkey debug_service.key -out debug_service.crt -days 1095 -sha256 -sha256 -req -extfile <(printf "${extFileContent}")`,
106+
`openssl x509 -req -in debug_service.csr -signkey debug_service.key -out debug_service.crt -days 1095 -sha256 -req -extfile <(printf "${extFileContent}")`,
119107
`openssl pkcs12 -export -out debug_service.pfx -inkey debug_service.key -in debug_service.crt -password pass:${host}`,
120-
`rm debug_service.key debug_service.csr`,
108+
`rm debug_service.key debug_service.csr debug_service.crt`,
121109
`chmod 444 debug_service.pfx`
122110
];
123111

@@ -141,11 +129,19 @@ export async function setup(connection: IBMi) {
141129
}
142130
}
143131

144-
export function downloadClientCert(connection: IBMi) {
145-
const remotePath = getRemoteClientCertPath(connection);
132+
export async function downloadClientCert(connection: IBMi) {
146133
const localPath = getLocalCertPath(connection);
147134

148-
return connection.downloadFile(localPath, remotePath);
135+
const result = await connection.sendCommand({
136+
command: `openssl s_client -connect localhost:${connection.config?.debugPort} -showcerts < /dev/null 2> /dev/null | openssl x509 -outform PEM`,
137+
directory: getRemoteCertDirectory(connection)
138+
});
139+
140+
if (result.code && result.code > 0) {
141+
throw new Error(`Failed to download client certificate.`);
142+
}
143+
144+
await fs.writeFile(localPath, result.stdout, {encoding: `utf8`});
149145
}
150146

151147
export function getLocalCertPath(connection: IBMi) {

src/api/debug/index.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -274,18 +274,27 @@ export async function initialize(context: ExtensionContext) {
274274
if (connection.config!.debugIsSecure) {
275275

276276
try {
277-
const remoteClientCertExists = await certificates.remoteClientCertExists(connection);
277+
const existingDebugService = await server.getRunningJob(connection.config?.debugPort || "8005", instance.getContent()!);
278+
const remoteCertExists = await certificates.remoteServerCertExists(connection);
278279

279280
// If the client certificate exists on the server, download it
280-
if (remoteClientCertExists) {
281-
await certificates.downloadClientCert(connection);
282-
localCertsOk = true;
283-
vscode.window.showInformationMessage(`Debug certificate downloaded from the server.`);
281+
if (remoteCertExists) {
282+
if (existingDebugService) {
283+
await certificates.downloadClientCert(connection);
284+
localCertsOk = true;
285+
vscode.window.showInformationMessage(`Debug certificate downloaded from the server.`);
286+
} else {
287+
vscode.window.showInformationMessage(`Cannot fetch client certificate because the Debug Service is not running.`, `Startup Service`).then(result => {
288+
if (result === `Startup Service`) {
289+
vscode.commands.executeCommand(`code-for-ibmi.debug.start`);
290+
}
291+
});
292+
}
284293

285294
} else {
286295
const doImport = await vscode.window.showInformationMessage(`Debug setup`, {
287296
modal: true,
288-
detail: `The client certificate is not setup on the server. Would you like to import a certificate from your device?`
297+
detail: `The server certificate is not setup on the server. Would you like to import a certificate from your device?`
289298
}, `Yes`, `No`);
290299

291300
if (doImport === `Yes`) {
@@ -414,7 +423,7 @@ export async function initialize(context: ExtensionContext) {
414423
if (remoteCertsExist) {
415424
vscode.commands.executeCommand(`setContext`, remoteCertContext, true);
416425

417-
if (isSecure) {
426+
if (isSecure && existingDebugService) {
418427
const localCertsExists = await certificates.localClientCertExists(connection);
419428

420429
if (localCertsExists) {

0 commit comments

Comments
 (0)