mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-27 07:16:40 +00:00
backend setup to store and get DNS Records
This commit is contained in:
@@ -82,6 +82,7 @@ export enum ActionsEnum {
|
|||||||
getClient = "getClient",
|
getClient = "getClient",
|
||||||
listOrgDomains = "listOrgDomains",
|
listOrgDomains = "listOrgDomains",
|
||||||
getDomain = "getDomain",
|
getDomain = "getDomain",
|
||||||
|
getDNSRecords = "getDNSRecords",
|
||||||
createNewt = "createNewt",
|
createNewt = "createNewt",
|
||||||
createIdp = "createIdp",
|
createIdp = "createIdp",
|
||||||
updateIdp = "updateIdp",
|
updateIdp = "updateIdp",
|
||||||
|
|||||||
@@ -16,6 +16,18 @@ export const domains = sqliteTable("domains", {
|
|||||||
preferWildcardCert: integer("preferWildcardCert", { mode: "boolean" })
|
preferWildcardCert: integer("preferWildcardCert", { mode: "boolean" })
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const dnsRecords = sqliteTable("dnsRecords", {
|
||||||
|
id: text("id").primaryKey(),
|
||||||
|
domainId: text("domainId")
|
||||||
|
.notNull()
|
||||||
|
.references(() => domains.domainId, { onDelete: "cascade" }),
|
||||||
|
|
||||||
|
recordType: text("recordType").notNull(), // "NS" | "CNAME" | "A" | "TXT"
|
||||||
|
baseDomain: text("baseDomain"),
|
||||||
|
value: text("value").notNull(),
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
export const orgs = sqliteTable("orgs", {
|
export const orgs = sqliteTable("orgs", {
|
||||||
orgId: text("orgId").primaryKey(),
|
orgId: text("orgId").primaryKey(),
|
||||||
name: text("name").notNull(),
|
name: text("name").notNull(),
|
||||||
@@ -748,6 +760,7 @@ export type ResourceWhitelist = InferSelectModel<typeof resourceWhitelist>;
|
|||||||
export type VersionMigration = InferSelectModel<typeof versionMigrations>;
|
export type VersionMigration = InferSelectModel<typeof versionMigrations>;
|
||||||
export type ResourceRule = InferSelectModel<typeof resourceRules>;
|
export type ResourceRule = InferSelectModel<typeof resourceRules>;
|
||||||
export type Domain = InferSelectModel<typeof domains>;
|
export type Domain = InferSelectModel<typeof domains>;
|
||||||
|
export type DnsRecord = InferSelectModel<typeof dnsRecords>;
|
||||||
export type Client = InferSelectModel<typeof clients>;
|
export type Client = InferSelectModel<typeof clients>;
|
||||||
export type ClientSite = InferSelectModel<typeof clientSites>;
|
export type ClientSite = InferSelectModel<typeof clientSites>;
|
||||||
export type RoleClient = InferSelectModel<typeof roleClients>;
|
export type RoleClient = InferSelectModel<typeof roleClients>;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { db, Domain, domains, OrgDomains, orgDomains } from "@server/db";
|
import { db, Domain, domains, OrgDomains, orgDomains, dnsRecords } from "@server/db";
|
||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
@@ -276,9 +276,23 @@ export async function createOrgDomain(
|
|||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
|
// Prepare DNS records to insert
|
||||||
|
const recordsToInsert = [];
|
||||||
|
|
||||||
// TODO: This needs to be cross region and not hardcoded
|
// TODO: This needs to be cross region and not hardcoded
|
||||||
if (type === "ns") {
|
if (type === "ns") {
|
||||||
nsRecords = config.getRawConfig().dns.nameservers as string[];
|
nsRecords = config.getRawConfig().dns.nameservers as string[];
|
||||||
|
|
||||||
|
// Save NS records to database
|
||||||
|
for (const nsValue of nsRecords) {
|
||||||
|
recordsToInsert.push({
|
||||||
|
id: generateId(15),
|
||||||
|
domainId,
|
||||||
|
recordType: "NS",
|
||||||
|
baseDomain: baseDomain,
|
||||||
|
value: nsValue
|
||||||
|
});
|
||||||
|
}
|
||||||
} else if (type === "cname") {
|
} else if (type === "cname") {
|
||||||
cnameRecords = [
|
cnameRecords = [
|
||||||
{
|
{
|
||||||
@@ -290,6 +304,17 @@ export async function createOrgDomain(
|
|||||||
baseDomain: `_acme-challenge.${baseDomain}`
|
baseDomain: `_acme-challenge.${baseDomain}`
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Save CNAME records to database
|
||||||
|
for (const cnameRecord of cnameRecords) {
|
||||||
|
recordsToInsert.push({
|
||||||
|
id: generateId(15),
|
||||||
|
domainId,
|
||||||
|
recordType: "CNAME",
|
||||||
|
baseDomain: cnameRecord.baseDomain,
|
||||||
|
value: cnameRecord.value
|
||||||
|
});
|
||||||
|
}
|
||||||
} else if (type === "wildcard") {
|
} else if (type === "wildcard") {
|
||||||
aRecords = [
|
aRecords = [
|
||||||
{
|
{
|
||||||
@@ -301,6 +326,22 @@ export async function createOrgDomain(
|
|||||||
baseDomain: `${baseDomain}`
|
baseDomain: `${baseDomain}`
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Save A records to database
|
||||||
|
for (const aRecord of aRecords) {
|
||||||
|
recordsToInsert.push({
|
||||||
|
id: generateId(15),
|
||||||
|
domainId,
|
||||||
|
recordType: "A",
|
||||||
|
baseDomain: aRecord.baseDomain,
|
||||||
|
value: aRecord.value
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert all DNS records in batch
|
||||||
|
if (recordsToInsert.length > 0) {
|
||||||
|
await trx.insert(dnsRecords).values(recordsToInsert);
|
||||||
}
|
}
|
||||||
|
|
||||||
numOrgDomains = await trx
|
numOrgDomains = await trx
|
||||||
|
|||||||
86
server/routers/domain/getDNSRecords.ts
Normal file
86
server/routers/domain/getDNSRecords.ts
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db, dnsRecords } from "@server/db";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
|
||||||
|
const getDNSRecordsSchema = z
|
||||||
|
.object({
|
||||||
|
domainId: z.string(),
|
||||||
|
orgId: z.string()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
async function query(domainId: string) {
|
||||||
|
const records = await db
|
||||||
|
.select()
|
||||||
|
.from(dnsRecords)
|
||||||
|
.where(eq(dnsRecords.domainId, domainId));
|
||||||
|
|
||||||
|
return records;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type GetDNSRecordsResponse = Awaited<ReturnType<typeof query>>;
|
||||||
|
|
||||||
|
registry.registerPath({
|
||||||
|
method: "get",
|
||||||
|
path: "/org/{orgId}/domain/{domainId}/dns-records",
|
||||||
|
description: "Get all DNS records for a domain by domainId.",
|
||||||
|
tags: [OpenAPITags.Domain],
|
||||||
|
request: {
|
||||||
|
params: z.object({
|
||||||
|
domainId: z.string(),
|
||||||
|
orgId: z.string()
|
||||||
|
})
|
||||||
|
},
|
||||||
|
responses: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function getDNSRecords(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedParams = getDNSRecordsSchema.safeParse(req.params);
|
||||||
|
if (!parsedParams.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedParams.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { domainId } = parsedParams.data;
|
||||||
|
|
||||||
|
const records = await query(domainId);
|
||||||
|
|
||||||
|
if (!records || records.length === 0) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
"No DNS records found for this domain"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response<GetDNSRecordsResponse>(res, {
|
||||||
|
data: records,
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "DNS records retrieved successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,3 +3,4 @@ export * from "./createOrgDomain";
|
|||||||
export * from "./deleteOrgDomain";
|
export * from "./deleteOrgDomain";
|
||||||
export * from "./restartOrgDomain";
|
export * from "./restartOrgDomain";
|
||||||
export * from "./getDomain";
|
export * from "./getDomain";
|
||||||
|
export * from "./getDNSRecords";
|
||||||
@@ -309,6 +309,13 @@ authenticated.get(
|
|||||||
domain.getDomain
|
domain.getDomain
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.get(
|
||||||
|
"/org/:orgId/domain/:domainId/dns-records",
|
||||||
|
verifyOrgAccess,
|
||||||
|
verifyUserHasAction(ActionsEnum.getDNSRecords),
|
||||||
|
domain.getDNSRecords
|
||||||
|
);
|
||||||
|
|
||||||
authenticated.get(
|
authenticated.get(
|
||||||
"/org/:orgId/invitations",
|
"/org/:orgId/invitations",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
|
|||||||
Reference in New Issue
Block a user