Make org subnet optional

This commit is contained in:
Owen 2025-07-16 11:35:38 -07:00
parent 1dff9baa61
commit 021bc073a2
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
6 changed files with 31 additions and 4 deletions

View file

@ -22,7 +22,7 @@ export const domains = pgTable("domains", {
export const orgs = pgTable("orgs", {
orgId: varchar("orgId").primaryKey(),
name: varchar("name").notNull(),
subnet: varchar("subnet").notNull()
subnet: varchar("subnet")
});
export const orgDomains = pgTable("orgDomains", {

View file

@ -16,7 +16,7 @@ export const domains = sqliteTable("domains", {
export const orgs = sqliteTable("orgs", {
orgId: text("orgId").primaryKey(),
name: text("name").notNull(),
subnet: text("subnet").notNull(),
subnet: text("subnet")
});
export const userDomains = sqliteTable("userDomains", {

View file

@ -240,6 +240,14 @@ export async function getNextAvailableClientSubnet(
): Promise<string> {
const [org] = await db.select().from(orgs).where(eq(orgs.orgId, orgId));
if (!org) {
throw new Error(`Organization with ID ${orgId} not found`);
}
if (!org.subnet) {
throw new Error(`Organization with ID ${orgId} has no subnet defined`);
}
const existingAddressesSites = await db
.select({
address: sites.address
@ -279,7 +287,7 @@ export async function getNextAvailableOrgSubnet(): Promise<string> {
.from(orgs)
.where(isNotNull(orgs.subnet));
const addresses = existingAddresses.map((org) => org.subnet);
const addresses = existingAddresses.map((org) => org.subnet!);
let subnet = findNextAvailableCidr(
addresses,

View file

@ -120,6 +120,15 @@ export async function createClient(
);
}
if (!org.subnet) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
`Organization with ID ${orgId} has no subnet defined`
)
);
}
if (!isIpInCidr(subnet, org.subnet)) {
return next(
createHttpError(

View file

@ -129,6 +129,15 @@ export async function createSite(
);
}
if (!org.subnet) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
`Organization with ID ${orgId} has no subnet defined`
)
);
}
let updatedAddress = null;
if (address) {
if (!isValidIP(address)) {

View file

@ -86,7 +86,7 @@ export default async function migration() {
ALTER TABLE "domains" ADD COLUMN "tries" integer DEFAULT 0 NOT NULL;
ALTER TABLE "exitNodes" ADD COLUMN "maxConnections" integer;
ALTER TABLE "newt" ADD COLUMN "version" varchar;
ALTER TABLE "orgs" ADD COLUMN "subnet" varchar NOT NULL;
ALTER TABLE "orgs" ADD COLUMN "subnet" varchar;
ALTER TABLE "sites" ADD COLUMN "address" varchar;
ALTER TABLE "sites" ADD COLUMN "endpoint" varchar;
ALTER TABLE "sites" ADD COLUMN "publicKey" varchar;
@ -112,6 +112,7 @@ export default async function migration() {
} catch (e) {
console.log("Unable to migrate database schema");
console.log(e);
throw e;
}
console.log(`${version} migration complete`);