-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
419067f
commit aa22718
Showing
10 changed files
with
171 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { join } from "path"; | ||
import { secrets } from "@cocalc/backend/data"; | ||
import { readFile } from "node:fs/promises"; | ||
import getLogger from "@cocalc/backend/logger"; | ||
import { connect, credsAuthenticator } from "nats"; | ||
|
||
const logger = getLogger("backend:nats"); | ||
|
||
export async function getCreds(): Promise<string | undefined> { | ||
const filename = join(secrets, "nats.creds"); | ||
try { | ||
return (await readFile(filename)).toString().trim(); | ||
} catch { | ||
logger.debug( | ||
`getCreds -- please create ${filename}, which is missing. Nothing will work.`, | ||
); | ||
return undefined; | ||
} | ||
} | ||
|
||
let nc: Awaited<ReturnType<typeof connect>> | null = null; | ||
export async function getConnection() { | ||
logger.debug("connecting to nats"); | ||
|
||
if (nc == null) { | ||
const creds = await getCreds(); | ||
nc = await connect({ | ||
authenticator: credsAuthenticator(new TextEncoder().encode(creds)), | ||
}); | ||
logger.debug(`connected to ${nc.getServer()}`); | ||
} | ||
return nc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
echo "require('@cocalc/database/nats').init()" | COCALC_MODE='single-user' DEBUG_CONSOLE=yes DEBUG=cocalc:* node | ||
*/ | ||
|
||
import getLogger from "@cocalc/backend/logger"; | ||
import { JSONCodec } from "nats"; | ||
import { isValidUUID } from "@cocalc/util/misc"; | ||
import userQuery from "@cocalc/database/user-query"; | ||
import { getConnection } from "@cocalc/backend/nats"; | ||
|
||
const logger = getLogger("database:nats"); | ||
|
||
const jc = JSONCodec(); | ||
|
||
export async function init() { | ||
const subject = "hub.*.*.db"; | ||
logger.debug(`init -- subject='${subject}', options=`, { | ||
queue: "0", | ||
}); | ||
const nc = await getConnection(); | ||
const sub = nc.subscribe(subject, { queue: "0" }); | ||
for await (const mesg of sub) { | ||
handleRequest(mesg); | ||
} | ||
} | ||
|
||
async function handleRequest(mesg) { | ||
console.log({ subject: mesg.subject }); | ||
let resp; | ||
try { | ||
const segments = mesg.subject.split("."); | ||
const uuid = segments[2]; | ||
if (!isValidUUID(uuid)) { | ||
throw Error(`invalid uuid '${uuid}'`); | ||
} | ||
const type = segments[1]; // 'project' or 'account' | ||
let account_id, project_id; | ||
if (type == "project") { | ||
project_id = uuid; | ||
account_id = undefined; | ||
} else if (type == "account") { | ||
project_id = undefined; | ||
account_id = uuid; | ||
} else { | ||
throw Error("must be project or account"); | ||
} | ||
const { name, args } = jc.decode(mesg.data) ?? ({} as any); | ||
if (!name) { | ||
throw Error("api endpoint name must be given in message"); | ||
} | ||
logger.debug("handling hub db request:", { | ||
account_id, | ||
project_id, | ||
name, | ||
args, | ||
}); | ||
resp = await getResponse({ name, args, account_id, project_id }); | ||
} catch (err) { | ||
resp = { error: `${err}` }; | ||
} | ||
mesg.respond(jc.encode(resp)); | ||
} | ||
|
||
async function getResponse({ name, args, account_id, project_id }) { | ||
if (name == "userQuery") { | ||
return await userQuery({ ...args[0], account_id, project_id }); | ||
} else { | ||
throw Error(`name='${name}' not implemented`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,12 @@ | ||
import { connect, credsAuthenticator } from "nats"; | ||
import getLogger from "@cocalc/backend/logger"; | ||
import { initAPI } from "./api"; | ||
import { getConnection } from "@cocalc/backend/nats"; | ||
|
||
const logger = getLogger("server:nats"); | ||
|
||
const creds = `-----BEGIN NATS USER JWT----- | ||
eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJVTDJaWEdFWFFKTzVRNjdLU1hKNDdERFpKSFE3QUFWMjdHWUtBN1ZJVjVaT01DQU1SN1hBIiwiaWF0IjoxNzM3NTY3OTQwLCJpc3MiOiJBRDRHNlI2MkJERFFVU0NKVkxaTkE3RVM3UjNBNkRXWExZVVdHWlY3NEVKMlM2VkJDN0RRVk0zSSIsIm5hbWUiOiJhZG1pbiIsInN1YiI6IlVBV1hZVUpYSEFXQzNPSFFURE1SQVBSWVpNNFQ0RkZDRk1TTVFLNDVCWU1SS0ZSRE5RTjQ0Vk1SIiwibmF0cyI6eyJwdWIiOnsiYWxsb3ciOlsiXHUwMDNlIl19LCJzdWIiOnsiYWxsb3ciOlsiXHUwMDNlIl19LCJzdWJzIjotMSwiZGF0YSI6LTEsInBheWxvYWQiOi0xLCJ0eXBlIjoidXNlciIsInZlcnNpb24iOjJ9fQ.Pv9-T3P7cO1VSFiNocGA0vCGvwQ-UaX3b7OzwMIHdn5hGs4kUv4eLE-Er_6dxrZiPu6PJjBYB7eD2hyb-gxSCQ | ||
------END NATS USER JWT------ | ||
************************* IMPORTANT ************************* | ||
NKEY Seed printed below can be used to sign and prove identity. | ||
NKEYs are sensitive and should be treated as secrets. | ||
-----BEGIN USER NKEY SEED----- | ||
SUAMW6S2OXSKL2ETX5GJE3NDLWGXZFZ4JAP5WHBCK43RMFDPJCCJLPWC5Y | ||
------END USER NKEY SEED------ | ||
************************************************************* | ||
`; | ||
|
||
export default async function initNatsServer() { | ||
logger.debug("initializing nats cocalc hub server"); | ||
const nc = await connect({ | ||
authenticator: credsAuthenticator(new TextEncoder().encode(creds)), | ||
}); | ||
const nc = await getConnection(); | ||
logger.debug(`connected to ${nc.getServer()}`); | ||
initAPI(nc); | ||
} |