/
index.ts
55 lines (43 loc) · 1.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { loggedExec, sshConnect } from "@scow/lib-ssh";
import { FastifyInstance } from "fastify";
import { AuthProvider } from "src/auth/AuthProvider";
import { serveLoginHtml } from "src/auth/loginHtml";
import { registerPostHandler } from "src/auth/ssh/postHandler";
import { authConfig, SshConfigSchema } from "src/config/auth";
import { clusters } from "src/config/clusters";
import { rootKeyPair } from "src/config/env";
import { ensureNotUndefined } from "src/utils/validations";
function checkLoginNode(sshConfig: SshConfigSchema) {
let loginNode = sshConfig.baseNode;
if (!loginNode) {
if (Object.keys(clusters).length === 0) {
throw new Error("No cluster has been set in clusters config");
}
const clusterConfig = Object.values(clusters)[0];
loginNode = clusterConfig.slurm.loginNodes[0];
if (!loginNode) {
throw new Error(`Cluster ${clusterConfig.displayName} has no login node.`);
}
}
return loginNode;
}
export const createSshAuthProvider = (f: FastifyInstance) => {
const { ssh } = ensureNotUndefined(authConfig, ["ssh"]);
const loginNode = checkLoginNode(ssh);
f.log.info("Determined login node %s", loginNode);
registerPostHandler(f, loginNode);
return <AuthProvider>{
serveLoginHtml: (callbackUrl, req, rep) => serveLoginHtml(false, callbackUrl, req, rep),
fetchAuthTokenInfo: async () => undefined,
getUser: async (identityId, req) => {
return await sshConnect(loginNode, "root", rootKeyPair, req.log, async (ssh) => {
return loggedExec(ssh, req.log, true, "id", [identityId])
.then(() => ({ identityId }))
.catch(() => undefined);
});
},
validateName: undefined,
createUser: undefined,
changePassword: undefined,
};
};