|
| 1 | +/* |
| 2 | + Copyright (c) 2025 Red Hat, Inc. |
| 3 | + This program and the accompanying materials are made |
| 4 | + available under the terms of the Eclipse Public License 2.0 |
| 5 | + which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | +
|
| 7 | + SPDX-License-Identifier: EPL-2.0 |
| 8 | +*/ |
| 9 | + |
| 10 | +const http = require('http'); |
| 11 | +const fs = require('fs'); |
| 12 | +const os = require('os'); |
| 13 | + |
| 14 | +const hostname = '127.0.0.1'; |
| 15 | +const port = 3400; |
| 16 | + |
| 17 | +const server = http.createServer((req, res) => { |
| 18 | + res.statusCode = 200; |
| 19 | + res.setHeader('Content-Type', 'text/html'); |
| 20 | + |
| 21 | + let hasUserPrefSSHKey = fs.existsSync('/etc/ssh/dwo_ssh_key.pub'); |
| 22 | + |
| 23 | + let pubKey = "PUBLIC KEY COULD NOT BE DISPLAYED"; |
| 24 | + try { |
| 25 | + pubKey = fs.readFileSync('/etc/ssh/dwo_ssh_key.pub', 'utf8'); |
| 26 | + } catch (err) { |
| 27 | + // continue |
| 28 | + } |
| 29 | + |
| 30 | + let genKey = "PRIVATE KEY NOT FOUND"; |
| 31 | + try { |
| 32 | + genKey = fs.readFileSync('/opt/ssh/ssh_client_ed25519_key', 'utf8'); |
| 33 | + } catch (err) { |
| 34 | + // continue |
| 35 | + } |
| 36 | + |
| 37 | + let keyMessage = ` |
| 38 | + <pre>${hasUserPrefSSHKey ? pubKey : genKey}</pre>`; |
| 39 | + |
| 40 | + res.end(` |
| 41 | +<!DOCTYPE html> |
| 42 | +<html> |
| 43 | + <head> |
| 44 | + <title>${process.env["DEVWORKSPACE_NAME"]}</title> |
| 45 | + </head> |
| 46 | + <body> |
| 47 | + <h1>Workspace ${process.env["DEVWORKSPACE_NAME"]} is running</h1> |
| 48 | + <div class="border"> |
| 49 | + <ol> |
| 50 | + <li>Make sure your local oc client is logged in to your OpenShift cluster</li> |
| 51 | + <li><p class="center">Run <code><b>oc port-forward ${process.env["HOSTNAME"]} 2022:2022</b></code>. This establishes a connection to the workspace.</p></li> |
| 52 | + <li> |
| 53 | + <p>In your local VS Code, connect to <code>localhost</code> on port <code>2022</code> with user <code>${os.userInfo().username}</code> ${hasUserPrefSSHKey ? `. The SSH key, corresponding to the following public key, configured in the "SSH Keys" tab of "User Preferences" has been authorized to connect :` : `and the following identity file :`} ${keyMessage}</p> |
| 54 | + <p> |
| 55 | + This can also be configured locally in <code>$HOME/.ssh/config</code> with the following : |
| 56 | + <pre><b> |
| 57 | +Host localhost |
| 58 | + HostName 127.0.0.1 |
| 59 | + User ${os.userInfo().username} |
| 60 | + Port 2022 |
| 61 | + IdentityFile /path/to/the/ssh_client_ed25519_key |
| 62 | + UserKnownHostsFile /dev/null |
| 63 | + </b></pre> |
| 64 | + </p> |
| 65 | + </li> |
| 66 | + </ol> |
| 67 | + <h3>Troubleshooting</h3> |
| 68 | + <p>If the connection fails with "<code>WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED</code>", it may be necessary to remove the <code>localhost</code> or <code>127.0.0.1</code> entries from <code>$HOME/.ssh/known_hosts</code>. This is because the SSHD service container (to which <code>oc port-forward</code> is forwarding) may change. This can be bypassed by setting <code>UserKnownHostsFile /dev/null</code></p> |
| 69 | + <p>Please ensure the permissions on the private key used are restricted to allow only the file owner to read/write. The SSH service may fail to correctly authenticate otherwise.</p> |
| 70 | + <p>The most common setup is to connect to the workspace using the "Remote - SSH Connection" from the corresponding editor's extension marketplace. If this setup fails to connect to the workspace, consider disabling the setting <code><b>remote.SSH.useExecServer</b></code> (set to false)</p> |
| 71 | + <p>For any other issues, relating to the use of a VS Code-based editor and the "Remote - SSH connection", the "Remote - SSH" logs from the "Output" view are very helpful in diagnosing the issue.</p> |
| 72 | + </div> |
| 73 | + </body> |
| 74 | +</html> |
| 75 | + `); |
| 76 | +}); |
| 77 | + |
| 78 | +server.listen(port, hostname, () => { |
| 79 | + console.log(`Server running at http://${hostname}:${port}/`); |
| 80 | +}); |
0 commit comments