| 1 | import { randomUUID } from "crypto"; |
| 2 | import { and, eq |
| 3 | import { z } |
| 4 | import { ORPCError } |
| 5 | import { db } |
| 6 | import { databaseEngineTypes, databaseHosts |
| 7 | import { encrypt, decrypt |
| 8 | import { dbEngines } |
| 9 | import { recordActivity } |
| 10 | import { protectedProcedure } |
| 11 | |
| 12 | const usernameMaxLength: Record |
| 13 | mysql: 32, |
| 14 | mariadb: 32, |
| 15 | postgresql: 63, |
| 16 | mongodb: 63, |
| 17 | redis: 63, |
| 18 | }; |
| 19 | |
| 20 | const databaseNameMaxLength: Record |
| 21 | mysql: 64, |
| 22 | mariadb: 64, |
| 23 | postgresql: 63, |
| 24 | mongodb: 64, |
| 25 | redis: 64, |
| 26 | }; |
| 27 | |
| 28 | function randomIndex(length: |
| 29 | const arr = |
| 30 | crypto.getRandomValues(arr |
| 31 | return arr[0] |
| 32 | } |
| 33 | |
| 34 | function generatePassword(length |
| 35 | const chars = |
| 36 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"; |
| 37 | const bytes = |
| 38 | crypto.getRandomValues(bytes |
| 39 | return Array.from |
| 40 | .map((b) |
| 41 | .join(""); |
| 42 | } |
| 43 | |
| 44 | export const databasesRouter = |
| 45 | list: protectedProcedure |
| 46 | .input(z.object |
| 47 | .handler(async ( |
| 48 | const server = |
| 49 | where: eq(servers |
| 50 | }); |
| 51 | if (!server) |
| 52 | if (server.userId |
| 53 | throw new ORPCError( |
| 54 | } |
| 55 | |
| 56 | const rows = |
| 57 | where: eq(serverDatabases |
| 58 | with: { host: true |
| 59 | }); |
| 60 | |
| 61 | return rows.map |
| 62 | ...r, |
| 63 | password: decrypt(r |
| 64 | host: { ...r |
| 65 | })); |
| 66 | }), |
| 67 | |
| 68 | create: protectedProcedure |
| 69 | .input( |
| 70 | z.object({ |
| 71 | serverId: z.string |
| 72 | type: z.enum |
| 73 | database: z.string |
| 74 | remote: z.string |
| 75 | }), |
| 76 | ) |
| 77 | .handler(async ( |
| 78 | const server = |
| 79 | where: eq(servers |
| 80 | with: { egg: true |
| 81 | }); |
| 82 | if (!server) |
| 83 | if (server.userId |
| 84 | throw new ORPCError( |
| 85 | } |
| 86 | |
| 87 | const rawAllowed = |
| 88 | const allowed = |
| 89 | if (allowed && |
| 90 | throw new ORPCError( |
| 91 | } |
| 92 | |
| 93 | const candidates = |
| 94 | where: eq(databaseHosts |
| 95 | }); |
| 96 | const eligible = |
| 97 | const nodeIds = |
| 98 | return !nodeIds || |
| 99 | }); |
| 100 | if (eligible.length |
| 101 | throw new ORPCError( |
| 102 | } |
| 103 | const host = |
| 104 | |
| 105 | const remote = |
| 106 | const dbName = |
| 107 | const username = |
| 108 | const password = |
| 109 | |
| 110 | await dbEngines[host |
| 111 | { host: host. |
| 112 | { database: dbName, |
| 113 | ); |
| 114 | |
| 115 | const id = |
| 116 | const uuid = |
| 117 | await db.insert |
| 118 | id, |
| 119 | uuid, |
| 120 | serverId: input.serverId |
| 121 | hostId: host.id |
| 122 | database: dbName, |
| 123 | username, |
| 124 | remote, |
| 125 | password: encrypt(password |
| 126 | }); |
| 127 | |
| 128 | recordActivity({ |
| 129 | eventType: "server:database.create", |
| 130 | userId: context.session |
| 131 | serverId: input.serverId |
| 132 | ip: context.ip |
| 133 | properties: { database: dbName |
| 134 | }); |
| 135 | |
| 136 | return db.query |
| 137 | where: eq(serverDatabases |
| 138 | with: { host: true |
| 139 | }); |
| 140 | }), |
| 141 | |
| 142 | rotatePassword: protectedProcedure |
| 143 | .input(z.object |
| 144 | .handler(async ( |
| 145 | const server = |
| 146 | where: eq(servers |
| 147 | }); |
| 148 | if (!server) |
| 149 | if (server.userId |
| 150 | throw new ORPCError( |
| 151 | } |
| 152 | |
| 153 | const dbRecord = |
| 154 | where: and( |
| 155 | eq(serverDatabases.id |
| 156 | eq(serverDatabases.serverId |
| 157 | ), |
| 158 | with: { host: true |
| 159 | }); |
| 160 | if (!dbRecord) |
| 161 | |
| 162 | const newPassword = |
| 163 | await dbEngines[dbRecord |
| 164 | { |
| 165 | host: dbRecord.host |
| 166 | port: dbRecord.host |
| 167 | username: dbRecord.host |
| 168 | password: decrypt(dbRecord |
| 169 | ssl: dbRecord.host |
| 170 | }, |
| 171 | { username: dbRecord. |
| 172 | ); |
| 173 | |
| 174 | await db |
| 175 | .update(serverDatabases) |
| 176 | .set({ password: |
| 177 | .where(eq(serverDatabases |
| 178 | |
| 179 | recordActivity({ |
| 180 | eventType: "server:database.rotate-password", |
| 181 | userId: context.session |
| 182 | serverId: input.serverId |
| 183 | ip: context.ip |
| 184 | properties: { database: dbRecord |
| 185 | }); |
| 186 | |
| 187 | return { password: |
| 188 | }), |
| 189 | |
| 190 | delete: protectedProcedure |
| 191 | .input(z.object |
| 192 | .handler(async ( |
| 193 | const server = |
| 194 | where: eq(servers |
| 195 | }); |
| 196 | if (!server) |
| 197 | if (server.userId |
| 198 | throw new ORPCError( |
| 199 | } |
| 200 | |
| 201 | const dbRecord = |
| 202 | where: and( |
| 203 | eq(serverDatabases.id |
| 204 | eq(serverDatabases.serverId |
| 205 | ), |
| 206 | with: { host: true |
| 207 | }); |
| 208 | if (!dbRecord) |
| 209 | |
| 210 | await dbEngines[dbRecord |
| 211 | { |
| 212 | host: dbRecord.host |
| 213 | port: dbRecord.host |
| 214 | username: dbRecord.host |
| 215 | password: decrypt(dbRecord |
| 216 | ssl: dbRecord.host |
| 217 | }, |
| 218 | { database: dbRecord. |
| 219 | ); |
| 220 | |
| 221 | await db |
| 222 | .delete(serverDatabases) |
| 223 | .where(eq(serverDatabases |
| 224 | |
| 225 | recordActivity({ |
| 226 | eventType: "server:database.delete", |
| 227 | userId: context.session |
| 228 | serverId: input.serverId |
| 229 | ip: context.ip |
| 230 | properties: { database: dbRecord |
| 231 | }); |
| 232 | }), |
| 233 | }; |