mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-26 12:36:41 +00:00
Compare commits
11 Commits
logging-pr
...
1.16.2-s.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2a65b4b74 | ||
|
|
1f01108b62 | ||
|
|
62c63ddcaa | ||
|
|
dfd604c781 | ||
|
|
c96c5e8ae8 | ||
|
|
6f71e9f0f2 | ||
|
|
d17ec6dc1f | ||
|
|
c36a019f5d | ||
|
|
cf2dfdea5b | ||
|
|
985e1bb9ab | ||
|
|
85335bfecc |
115
license.py
115
license.py
@@ -1,115 +0,0 @@
|
|||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
# --- Configuration ---
|
|
||||||
# The header text to be added to the files.
|
|
||||||
HEADER_TEXT = """/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
"""
|
|
||||||
|
|
||||||
def should_add_header(file_path):
|
|
||||||
"""
|
|
||||||
Checks if a file should receive the commercial license header.
|
|
||||||
Returns True if 'private' is in the path or file content.
|
|
||||||
"""
|
|
||||||
# Check if 'private' is in the file path (case-insensitive)
|
|
||||||
if 'server/private' in file_path.lower():
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Check if 'private' is in the file content (case-insensitive)
|
|
||||||
# try:
|
|
||||||
# with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
|
||||||
# content = f.read()
|
|
||||||
# if 'private' in content.lower():
|
|
||||||
# return True
|
|
||||||
# except Exception as e:
|
|
||||||
# print(f"Could not read file {file_path}: {e}")
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def process_directory(root_dir):
|
|
||||||
"""
|
|
||||||
Recursively scans a directory and adds headers to qualifying .ts or .tsx files,
|
|
||||||
skipping any 'node_modules' directories.
|
|
||||||
"""
|
|
||||||
print(f"Scanning directory: {root_dir}")
|
|
||||||
files_processed = 0
|
|
||||||
headers_added = 0
|
|
||||||
|
|
||||||
for root, dirs, files in os.walk(root_dir):
|
|
||||||
# --- MODIFICATION ---
|
|
||||||
# Exclude 'node_modules' directories from the scan to improve performance.
|
|
||||||
if 'node_modules' in dirs:
|
|
||||||
dirs.remove('node_modules')
|
|
||||||
|
|
||||||
for file in files:
|
|
||||||
if file.endswith('.ts') or file.endswith('.tsx'):
|
|
||||||
file_path = os.path.join(root, file)
|
|
||||||
files_processed += 1
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(file_path, 'r+', encoding='utf-8') as f:
|
|
||||||
original_content = f.read()
|
|
||||||
has_header = original_content.startswith(HEADER_TEXT.strip())
|
|
||||||
|
|
||||||
if should_add_header(file_path):
|
|
||||||
# Add header only if it's not already there
|
|
||||||
if not has_header:
|
|
||||||
f.seek(0, 0) # Go to the beginning of the file
|
|
||||||
f.write(HEADER_TEXT.strip() + '\n\n' + original_content)
|
|
||||||
print(f"Added header to: {file_path}")
|
|
||||||
headers_added += 1
|
|
||||||
else:
|
|
||||||
print(f"Header already exists in: {file_path}")
|
|
||||||
else:
|
|
||||||
# Remove header if it exists but shouldn't be there
|
|
||||||
if has_header:
|
|
||||||
# Find the end of the header and remove it (including following newlines)
|
|
||||||
header_with_newlines = HEADER_TEXT.strip() + '\n\n'
|
|
||||||
if original_content.startswith(header_with_newlines):
|
|
||||||
content_without_header = original_content[len(header_with_newlines):]
|
|
||||||
else:
|
|
||||||
# Handle case where there might be different newline patterns
|
|
||||||
header_end = len(HEADER_TEXT.strip())
|
|
||||||
# Skip any newlines after the header
|
|
||||||
while header_end < len(original_content) and original_content[header_end] in '\n\r':
|
|
||||||
header_end += 1
|
|
||||||
content_without_header = original_content[header_end:]
|
|
||||||
|
|
||||||
f.seek(0)
|
|
||||||
f.write(content_without_header)
|
|
||||||
f.truncate()
|
|
||||||
print(f"Removed header from: {file_path}")
|
|
||||||
headers_added += 1 # Reusing counter for modifications
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error processing file {file_path}: {e}")
|
|
||||||
|
|
||||||
print("\n--- Scan Complete ---")
|
|
||||||
print(f"Total .ts or .tsx files found: {files_processed}")
|
|
||||||
print(f"Files modified (headers added/removed): {headers_added}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# Get the target directory from the command line arguments.
|
|
||||||
# If no directory is provided, it uses the current directory ('.').
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
target_directory = sys.argv[1]
|
|
||||||
else:
|
|
||||||
target_directory = '.' # Default to current directory
|
|
||||||
|
|
||||||
if not os.path.isdir(target_directory):
|
|
||||||
print(f"Error: Directory '{target_directory}' not found.")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
process_directory(os.path.abspath(target_directory))
|
|
||||||
@@ -323,41 +323,6 @@
|
|||||||
"apiKeysDelete": "Delete API Key",
|
"apiKeysDelete": "Delete API Key",
|
||||||
"apiKeysManage": "Manage API Keys",
|
"apiKeysManage": "Manage API Keys",
|
||||||
"apiKeysDescription": "API keys are used to authenticate with the integration API",
|
"apiKeysDescription": "API keys are used to authenticate with the integration API",
|
||||||
"provisioningKeysTitle": "Provisioning Key",
|
|
||||||
"provisioningKeysManage": "Manage Provisioning Keys",
|
|
||||||
"provisioningKeysDescription": "Provisioning keys are used to authenticate automated site provisioning for your organization.",
|
|
||||||
"provisioningKeys": "Provisioning Keys",
|
|
||||||
"searchProvisioningKeys": "Search provisioning keys...",
|
|
||||||
"provisioningKeysAdd": "Generate Provisioning Key",
|
|
||||||
"provisioningKeysErrorDelete": "Error deleting provisioning key",
|
|
||||||
"provisioningKeysErrorDeleteMessage": "Error deleting provisioning key",
|
|
||||||
"provisioningKeysQuestionRemove": "Are you sure you want to remove this provisioning key from the organization?",
|
|
||||||
"provisioningKeysMessageRemove": "Once removed, the key can no longer be used for site provisioning.",
|
|
||||||
"provisioningKeysDeleteConfirm": "Confirm Delete Provisioning Key",
|
|
||||||
"provisioningKeysDelete": "Delete Provisioning key",
|
|
||||||
"provisioningKeysCreate": "Generate Provisioning Key",
|
|
||||||
"provisioningKeysCreateDescription": "Generate a new provisioning key for the organization",
|
|
||||||
"provisioningKeysSeeAll": "See all provisioning keys",
|
|
||||||
"provisioningKeysSave": "Save the provisioning key",
|
|
||||||
"provisioningKeysSaveDescription": "You will only be able to see this once. Copy it to a secure place.",
|
|
||||||
"provisioningKeysErrorCreate": "Error creating provisioning key",
|
|
||||||
"provisioningKeysList": "New provisioning key",
|
|
||||||
"provisioningKeysMaxBatchSize": "Max batch size",
|
|
||||||
"provisioningKeysUnlimitedBatchSize": "Unlimited batch size (no limit)",
|
|
||||||
"provisioningKeysMaxBatchUnlimited": "Unlimited",
|
|
||||||
"provisioningKeysMaxBatchSizeInvalid": "Enter a valid max batch size (1–1,000,000).",
|
|
||||||
"provisioningKeysValidUntil": "Valid until",
|
|
||||||
"provisioningKeysValidUntilHint": "Leave empty for no expiration.",
|
|
||||||
"provisioningKeysValidUntilInvalid": "Enter a valid date and time.",
|
|
||||||
"provisioningKeysNumUsed": "Times used",
|
|
||||||
"provisioningKeysLastUsed": "Last used",
|
|
||||||
"provisioningKeysNoExpiry": "No expiration",
|
|
||||||
"provisioningKeysNeverUsed": "Never",
|
|
||||||
"provisioningKeysEdit": "Edit Provisioning Key",
|
|
||||||
"provisioningKeysEditDescription": "Update the max batch size and expiration time for this key.",
|
|
||||||
"provisioningKeysUpdateError": "Error updating provisioning key",
|
|
||||||
"provisioningKeysUpdated": "Provisioning key updated",
|
|
||||||
"provisioningKeysUpdatedDescription": "Your changes have been saved.",
|
|
||||||
"apiKeysSettings": "{apiKeyName} Settings",
|
"apiKeysSettings": "{apiKeyName} Settings",
|
||||||
"userTitle": "Manage All Users",
|
"userTitle": "Manage All Users",
|
||||||
"userDescription": "View and manage all users in the system",
|
"userDescription": "View and manage all users in the system",
|
||||||
@@ -1301,7 +1266,6 @@
|
|||||||
"sidebarRoles": "Roles",
|
"sidebarRoles": "Roles",
|
||||||
"sidebarShareableLinks": "Links",
|
"sidebarShareableLinks": "Links",
|
||||||
"sidebarApiKeys": "API Keys",
|
"sidebarApiKeys": "API Keys",
|
||||||
"sidebarProvisioning": "Provisioning",
|
|
||||||
"sidebarSettings": "Settings",
|
"sidebarSettings": "Settings",
|
||||||
"sidebarAllUsers": "All Users",
|
"sidebarAllUsers": "All Users",
|
||||||
"sidebarIdentityProviders": "Identity Providers",
|
"sidebarIdentityProviders": "Identity Providers",
|
||||||
@@ -2381,12 +2345,6 @@
|
|||||||
"logRetentionEndOfFollowingYear": "End of following year",
|
"logRetentionEndOfFollowingYear": "End of following year",
|
||||||
"actionLogsDescription": "View a history of actions performed in this organization",
|
"actionLogsDescription": "View a history of actions performed in this organization",
|
||||||
"accessLogsDescription": "View access auth requests for resources in this organization",
|
"accessLogsDescription": "View access auth requests for resources in this organization",
|
||||||
"connectionLogs": "Connection Logs",
|
|
||||||
"connectionLogsDescription": "View connection logs for tunnels in this organization",
|
|
||||||
"sidebarLogsConnection": "Connection Logs",
|
|
||||||
"sourceAddress": "Source Address",
|
|
||||||
"destinationAddress": "Destination Address",
|
|
||||||
"duration": "Duration",
|
|
||||||
"licenseRequiredToUse": "An <enterpriseLicenseLink>Enterprise Edition</enterpriseLicenseLink> license or <pangolinCloudLink>Pangolin Cloud</pangolinCloudLink> is required to use this feature. <bookADemoLink>Book a demo or POC trial</bookADemoLink>.",
|
"licenseRequiredToUse": "An <enterpriseLicenseLink>Enterprise Edition</enterpriseLicenseLink> license or <pangolinCloudLink>Pangolin Cloud</pangolinCloudLink> is required to use this feature. <bookADemoLink>Book a demo or POC trial</bookADemoLink>.",
|
||||||
"ossEnterpriseEditionRequired": "The <enterpriseEditionLink>Enterprise Edition</enterpriseEditionLink> is required to use this feature. This feature is also available in <pangolinCloudLink>Pangolin Cloud</pangolinCloudLink>. <bookADemoLink>Book a demo or POC trial</bookADemoLink>.",
|
"ossEnterpriseEditionRequired": "The <enterpriseEditionLink>Enterprise Edition</enterpriseEditionLink> is required to use this feature. This feature is also available in <pangolinCloudLink>Pangolin Cloud</pangolinCloudLink>. <bookADemoLink>Book a demo or POC trial</bookADemoLink>.",
|
||||||
"certResolver": "Certificate Resolver",
|
"certResolver": "Certificate Resolver",
|
||||||
|
|||||||
@@ -109,10 +109,6 @@ export enum ActionsEnum {
|
|||||||
listApiKeyActions = "listApiKeyActions",
|
listApiKeyActions = "listApiKeyActions",
|
||||||
listApiKeys = "listApiKeys",
|
listApiKeys = "listApiKeys",
|
||||||
getApiKey = "getApiKey",
|
getApiKey = "getApiKey",
|
||||||
createSiteProvisioningKey = "createSiteProvisioningKey",
|
|
||||||
listSiteProvisioningKeys = "listSiteProvisioningKeys",
|
|
||||||
updateSiteProvisioningKey = "updateSiteProvisioningKey",
|
|
||||||
deleteSiteProvisioningKey = "deleteSiteProvisioningKey",
|
|
||||||
getCertificate = "getCertificate",
|
getCertificate = "getCertificate",
|
||||||
restartCertificate = "restartCertificate",
|
restartCertificate = "restartCertificate",
|
||||||
billing = "billing",
|
billing = "billing",
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { flushBandwidthToDb } from "@server/routers/newt/handleReceiveBandwidthMessage";
|
import { flushBandwidthToDb } from "@server/routers/newt/handleReceiveBandwidthMessage";
|
||||||
import { flushConnectionLogToDb } from "#dynamic/routers/newt";
|
|
||||||
import { flushSiteBandwidthToDb } from "@server/routers/gerbil/receiveBandwidth";
|
import { flushSiteBandwidthToDb } from "@server/routers/gerbil/receiveBandwidth";
|
||||||
import { stopPingAccumulator } from "@server/routers/newt/pingAccumulator";
|
import { stopPingAccumulator } from "@server/routers/newt/pingAccumulator";
|
||||||
import { cleanup as wsCleanup } from "#dynamic/routers/ws";
|
import { cleanup as wsCleanup } from "#dynamic/routers/ws";
|
||||||
@@ -7,7 +6,6 @@ import { cleanup as wsCleanup } from "#dynamic/routers/ws";
|
|||||||
async function cleanup() {
|
async function cleanup() {
|
||||||
await stopPingAccumulator();
|
await stopPingAccumulator();
|
||||||
await flushBandwidthToDb();
|
await flushBandwidthToDb();
|
||||||
await flushConnectionLogToDb();
|
|
||||||
await flushSiteBandwidthToDb();
|
await flushSiteBandwidthToDb();
|
||||||
await wsCleanup();
|
await wsCleanup();
|
||||||
|
|
||||||
@@ -18,4 +16,4 @@ export async function initCleanup() {
|
|||||||
// Handle process termination
|
// Handle process termination
|
||||||
process.on("SIGTERM", () => cleanup());
|
process.on("SIGTERM", () => cleanup());
|
||||||
process.on("SIGINT", () => cleanup());
|
process.on("SIGINT", () => cleanup());
|
||||||
}
|
}
|
||||||
@@ -7,8 +7,7 @@ import {
|
|||||||
bigint,
|
bigint,
|
||||||
real,
|
real,
|
||||||
text,
|
text,
|
||||||
index,
|
index
|
||||||
primaryKey
|
|
||||||
} from "drizzle-orm/pg-core";
|
} from "drizzle-orm/pg-core";
|
||||||
import { InferSelectModel } from "drizzle-orm";
|
import { InferSelectModel } from "drizzle-orm";
|
||||||
import {
|
import {
|
||||||
@@ -18,9 +17,7 @@ import {
|
|||||||
users,
|
users,
|
||||||
exitNodes,
|
exitNodes,
|
||||||
sessions,
|
sessions,
|
||||||
clients,
|
clients
|
||||||
siteResources,
|
|
||||||
sites
|
|
||||||
} from "./schema";
|
} from "./schema";
|
||||||
|
|
||||||
export const certificates = pgTable("certificates", {
|
export const certificates = pgTable("certificates", {
|
||||||
@@ -92,9 +89,7 @@ export const subscriptions = pgTable("subscriptions", {
|
|||||||
|
|
||||||
export const subscriptionItems = pgTable("subscriptionItems", {
|
export const subscriptionItems = pgTable("subscriptionItems", {
|
||||||
subscriptionItemId: serial("subscriptionItemId").primaryKey(),
|
subscriptionItemId: serial("subscriptionItemId").primaryKey(),
|
||||||
stripeSubscriptionItemId: varchar("stripeSubscriptionItemId", {
|
stripeSubscriptionItemId: varchar("stripeSubscriptionItemId", { length: 255 }),
|
||||||
length: 255
|
|
||||||
}),
|
|
||||||
subscriptionId: varchar("subscriptionId", { length: 255 })
|
subscriptionId: varchar("subscriptionId", { length: 255 })
|
||||||
.notNull()
|
.notNull()
|
||||||
.references(() => subscriptions.subscriptionId, {
|
.references(() => subscriptions.subscriptionId, {
|
||||||
@@ -307,45 +302,6 @@ export const accessAuditLog = pgTable(
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
export const connectionAuditLog = pgTable(
|
|
||||||
"connectionAuditLog",
|
|
||||||
{
|
|
||||||
id: serial("id").primaryKey(),
|
|
||||||
sessionId: text("sessionId").notNull(),
|
|
||||||
siteResourceId: integer("siteResourceId").references(
|
|
||||||
() => siteResources.siteResourceId,
|
|
||||||
{ onDelete: "cascade" }
|
|
||||||
),
|
|
||||||
orgId: text("orgId").references(() => orgs.orgId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
siteId: integer("siteId").references(() => sites.siteId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
clientId: integer("clientId").references(() => clients.clientId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
userId: text("userId").references(() => users.userId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
sourceAddr: text("sourceAddr").notNull(),
|
|
||||||
destAddr: text("destAddr").notNull(),
|
|
||||||
protocol: text("protocol").notNull(),
|
|
||||||
startedAt: integer("startedAt").notNull(),
|
|
||||||
endedAt: integer("endedAt"),
|
|
||||||
bytesTx: integer("bytesTx"),
|
|
||||||
bytesRx: integer("bytesRx")
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
index("idx_accessAuditLog_startedAt").on(table.startedAt),
|
|
||||||
index("idx_accessAuditLog_org_startedAt").on(
|
|
||||||
table.orgId,
|
|
||||||
table.startedAt
|
|
||||||
),
|
|
||||||
index("idx_accessAuditLog_siteResourceId").on(table.siteResourceId)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
export const approvals = pgTable("approvals", {
|
export const approvals = pgTable("approvals", {
|
||||||
approvalId: serial("approvalId").primaryKey(),
|
approvalId: serial("approvalId").primaryKey(),
|
||||||
timestamp: integer("timestamp").notNull(), // this is EPOCH time in seconds
|
timestamp: integer("timestamp").notNull(), // this is EPOCH time in seconds
|
||||||
@@ -373,48 +329,13 @@ export const approvals = pgTable("approvals", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const bannedEmails = pgTable("bannedEmails", {
|
export const bannedEmails = pgTable("bannedEmails", {
|
||||||
email: varchar("email", { length: 255 }).primaryKey()
|
email: varchar("email", { length: 255 }).primaryKey(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const bannedIps = pgTable("bannedIps", {
|
export const bannedIps = pgTable("bannedIps", {
|
||||||
ip: varchar("ip", { length: 255 }).primaryKey()
|
ip: varchar("ip", { length: 255 }).primaryKey(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const siteProvisioningKeys = pgTable("siteProvisioningKeys", {
|
|
||||||
siteProvisioningKeyId: varchar("siteProvisioningKeyId", {
|
|
||||||
length: 255
|
|
||||||
}).primaryKey(),
|
|
||||||
name: varchar("name", { length: 255 }).notNull(),
|
|
||||||
siteProvisioningKeyHash: text("siteProvisioningKeyHash").notNull(),
|
|
||||||
lastChars: varchar("lastChars", { length: 4 }).notNull(),
|
|
||||||
createdAt: varchar("dateCreated", { length: 255 }).notNull(),
|
|
||||||
lastUsed: varchar("lastUsed", { length: 255 }),
|
|
||||||
maxBatchSize: integer("maxBatchSize"), // null = no limit
|
|
||||||
numUsed: integer("numUsed").notNull().default(0),
|
|
||||||
validUntil: varchar("validUntil", { length: 255 })
|
|
||||||
});
|
|
||||||
|
|
||||||
export const siteProvisioningKeyOrg = pgTable(
|
|
||||||
"siteProvisioningKeyOrg",
|
|
||||||
{
|
|
||||||
siteProvisioningKeyId: varchar("siteProvisioningKeyId", {
|
|
||||||
length: 255
|
|
||||||
})
|
|
||||||
.notNull()
|
|
||||||
.references(() => siteProvisioningKeys.siteProvisioningKeyId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
orgId: varchar("orgId", { length: 255 })
|
|
||||||
.notNull()
|
|
||||||
.references(() => orgs.orgId, { onDelete: "cascade" })
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
primaryKey({
|
|
||||||
columns: [table.siteProvisioningKeyId, table.orgId]
|
|
||||||
})
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
export type Approval = InferSelectModel<typeof approvals>;
|
export type Approval = InferSelectModel<typeof approvals>;
|
||||||
export type Limit = InferSelectModel<typeof limits>;
|
export type Limit = InferSelectModel<typeof limits>;
|
||||||
export type Account = InferSelectModel<typeof account>;
|
export type Account = InferSelectModel<typeof account>;
|
||||||
@@ -436,4 +357,3 @@ export type LoginPage = InferSelectModel<typeof loginPage>;
|
|||||||
export type LoginPageBranding = InferSelectModel<typeof loginPageBranding>;
|
export type LoginPageBranding = InferSelectModel<typeof loginPageBranding>;
|
||||||
export type ActionAuditLog = InferSelectModel<typeof actionAuditLog>;
|
export type ActionAuditLog = InferSelectModel<typeof actionAuditLog>;
|
||||||
export type AccessAuditLog = InferSelectModel<typeof accessAuditLog>;
|
export type AccessAuditLog = InferSelectModel<typeof accessAuditLog>;
|
||||||
export type ConnectionAuditLog = InferSelectModel<typeof connectionAuditLog>;
|
|
||||||
|
|||||||
@@ -55,9 +55,6 @@ export const orgs = pgTable("orgs", {
|
|||||||
settingsLogRetentionDaysAction: integer("settingsLogRetentionDaysAction") // where 0 = dont keep logs and -1 = keep forever and 9001 = end of the following year
|
settingsLogRetentionDaysAction: integer("settingsLogRetentionDaysAction") // where 0 = dont keep logs and -1 = keep forever and 9001 = end of the following year
|
||||||
.notNull()
|
.notNull()
|
||||||
.default(0),
|
.default(0),
|
||||||
settingsLogRetentionDaysConnection: integer("settingsLogRetentionDaysConnection") // where 0 = dont keep logs and -1 = keep forever and 9001 = end of the following year
|
|
||||||
.notNull()
|
|
||||||
.default(0),
|
|
||||||
sshCaPrivateKey: text("sshCaPrivateKey"), // Encrypted SSH CA private key (PEM format)
|
sshCaPrivateKey: text("sshCaPrivateKey"), // Encrypted SSH CA private key (PEM format)
|
||||||
sshCaPublicKey: text("sshCaPublicKey"), // SSH CA public key (OpenSSH format)
|
sshCaPublicKey: text("sshCaPublicKey"), // SSH CA public key (OpenSSH format)
|
||||||
isBillingOrg: boolean("isBillingOrg"),
|
isBillingOrg: boolean("isBillingOrg"),
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ import { InferSelectModel } from "drizzle-orm";
|
|||||||
import {
|
import {
|
||||||
index,
|
index,
|
||||||
integer,
|
integer,
|
||||||
primaryKey,
|
|
||||||
real,
|
real,
|
||||||
sqliteTable,
|
sqliteTable,
|
||||||
text
|
text
|
||||||
} from "drizzle-orm/sqlite-core";
|
} from "drizzle-orm/sqlite-core";
|
||||||
import { clients, domains, exitNodes, orgs, sessions, siteResources, sites, users } from "./schema";
|
import { clients, domains, exitNodes, orgs, sessions, users } from "./schema";
|
||||||
|
|
||||||
export const certificates = sqliteTable("certificates", {
|
export const certificates = sqliteTable("certificates", {
|
||||||
certId: integer("certId").primaryKey({ autoIncrement: true }),
|
certId: integer("certId").primaryKey({ autoIncrement: true }),
|
||||||
@@ -295,45 +294,6 @@ export const accessAuditLog = sqliteTable(
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
export const connectionAuditLog = sqliteTable(
|
|
||||||
"connectionAuditLog",
|
|
||||||
{
|
|
||||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
||||||
sessionId: text("sessionId").notNull(),
|
|
||||||
siteResourceId: integer("siteResourceId").references(
|
|
||||||
() => siteResources.siteResourceId,
|
|
||||||
{ onDelete: "cascade" }
|
|
||||||
),
|
|
||||||
orgId: text("orgId").references(() => orgs.orgId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
siteId: integer("siteId").references(() => sites.siteId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
clientId: integer("clientId").references(() => clients.clientId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
userId: text("userId").references(() => users.userId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
sourceAddr: text("sourceAddr").notNull(),
|
|
||||||
destAddr: text("destAddr").notNull(),
|
|
||||||
protocol: text("protocol").notNull(),
|
|
||||||
startedAt: integer("startedAt").notNull(),
|
|
||||||
endedAt: integer("endedAt"),
|
|
||||||
bytesTx: integer("bytesTx"),
|
|
||||||
bytesRx: integer("bytesRx")
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
index("idx_accessAuditLog_startedAt").on(table.startedAt),
|
|
||||||
index("idx_accessAuditLog_org_startedAt").on(
|
|
||||||
table.orgId,
|
|
||||||
table.startedAt
|
|
||||||
),
|
|
||||||
index("idx_accessAuditLog_siteResourceId").on(table.siteResourceId)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
export const approvals = sqliteTable("approvals", {
|
export const approvals = sqliteTable("approvals", {
|
||||||
approvalId: integer("approvalId").primaryKey({ autoIncrement: true }),
|
approvalId: integer("approvalId").primaryKey({ autoIncrement: true }),
|
||||||
timestamp: integer("timestamp").notNull(), // this is EPOCH time in seconds
|
timestamp: integer("timestamp").notNull(), // this is EPOCH time in seconds
|
||||||
@@ -358,6 +318,7 @@ export const approvals = sqliteTable("approvals", {
|
|||||||
.notNull()
|
.notNull()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
export const bannedEmails = sqliteTable("bannedEmails", {
|
export const bannedEmails = sqliteTable("bannedEmails", {
|
||||||
email: text("email").primaryKey()
|
email: text("email").primaryKey()
|
||||||
});
|
});
|
||||||
@@ -366,37 +327,6 @@ export const bannedIps = sqliteTable("bannedIps", {
|
|||||||
ip: text("ip").primaryKey()
|
ip: text("ip").primaryKey()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const siteProvisioningKeys = sqliteTable("siteProvisioningKeys", {
|
|
||||||
siteProvisioningKeyId: text("siteProvisioningKeyId").primaryKey(),
|
|
||||||
name: text("name").notNull(),
|
|
||||||
siteProvisioningKeyHash: text("siteProvisioningKeyHash").notNull(),
|
|
||||||
lastChars: text("lastChars").notNull(),
|
|
||||||
createdAt: text("dateCreated").notNull(),
|
|
||||||
lastUsed: text("lastUsed"),
|
|
||||||
maxBatchSize: integer("maxBatchSize"), // null = no limit
|
|
||||||
numUsed: integer("numUsed").notNull().default(0),
|
|
||||||
validUntil: text("validUntil")
|
|
||||||
});
|
|
||||||
|
|
||||||
export const siteProvisioningKeyOrg = sqliteTable(
|
|
||||||
"siteProvisioningKeyOrg",
|
|
||||||
{
|
|
||||||
siteProvisioningKeyId: text("siteProvisioningKeyId")
|
|
||||||
.notNull()
|
|
||||||
.references(() => siteProvisioningKeys.siteProvisioningKeyId, {
|
|
||||||
onDelete: "cascade"
|
|
||||||
}),
|
|
||||||
orgId: text("orgId")
|
|
||||||
.notNull()
|
|
||||||
.references(() => orgs.orgId, { onDelete: "cascade" })
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
primaryKey({
|
|
||||||
columns: [table.siteProvisioningKeyId, table.orgId]
|
|
||||||
})
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
export type Approval = InferSelectModel<typeof approvals>;
|
export type Approval = InferSelectModel<typeof approvals>;
|
||||||
export type Limit = InferSelectModel<typeof limits>;
|
export type Limit = InferSelectModel<typeof limits>;
|
||||||
export type Account = InferSelectModel<typeof account>;
|
export type Account = InferSelectModel<typeof account>;
|
||||||
@@ -418,4 +348,3 @@ export type LoginPage = InferSelectModel<typeof loginPage>;
|
|||||||
export type LoginPageBranding = InferSelectModel<typeof loginPageBranding>;
|
export type LoginPageBranding = InferSelectModel<typeof loginPageBranding>;
|
||||||
export type ActionAuditLog = InferSelectModel<typeof actionAuditLog>;
|
export type ActionAuditLog = InferSelectModel<typeof actionAuditLog>;
|
||||||
export type AccessAuditLog = InferSelectModel<typeof accessAuditLog>;
|
export type AccessAuditLog = InferSelectModel<typeof accessAuditLog>;
|
||||||
export type ConnectionAuditLog = InferSelectModel<typeof connectionAuditLog>;
|
|
||||||
|
|||||||
@@ -47,9 +47,6 @@ export const orgs = sqliteTable("orgs", {
|
|||||||
settingsLogRetentionDaysAction: integer("settingsLogRetentionDaysAction") // where 0 = dont keep logs and -1 = keep forever and 9001 = end of the following year
|
settingsLogRetentionDaysAction: integer("settingsLogRetentionDaysAction") // where 0 = dont keep logs and -1 = keep forever and 9001 = end of the following year
|
||||||
.notNull()
|
.notNull()
|
||||||
.default(0),
|
.default(0),
|
||||||
settingsLogRetentionDaysConnection: integer("settingsLogRetentionDaysConnection") // where 0 = dont keep logs and -1 = keep forever and 9001 = end of the following year
|
|
||||||
.notNull()
|
|
||||||
.default(0),
|
|
||||||
sshCaPrivateKey: text("sshCaPrivateKey"), // Encrypted SSH CA private key (PEM format)
|
sshCaPrivateKey: text("sshCaPrivateKey"), // Encrypted SSH CA private key (PEM format)
|
||||||
sshCaPublicKey: text("sshCaPublicKey"), // SSH CA public key (OpenSSH format)
|
sshCaPublicKey: text("sshCaPublicKey"), // SSH CA public key (OpenSSH format)
|
||||||
isBillingOrg: integer("isBillingOrg", { mode: "boolean" }),
|
isBillingOrg: integer("isBillingOrg", { mode: "boolean" }),
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ export enum TierFeature {
|
|||||||
LogExport = "logExport",
|
LogExport = "logExport",
|
||||||
AccessLogs = "accessLogs", // set the retention period to none on downgrade
|
AccessLogs = "accessLogs", // set the retention period to none on downgrade
|
||||||
ActionLogs = "actionLogs", // set the retention period to none on downgrade
|
ActionLogs = "actionLogs", // set the retention period to none on downgrade
|
||||||
ConnectionLogs = "connectionLogs",
|
|
||||||
RotateCredentials = "rotateCredentials",
|
RotateCredentials = "rotateCredentials",
|
||||||
MaintencePage = "maintencePage", // handle downgrade
|
MaintencePage = "maintencePage", // handle downgrade
|
||||||
DevicePosture = "devicePosture",
|
DevicePosture = "devicePosture",
|
||||||
@@ -16,8 +15,7 @@ export enum TierFeature {
|
|||||||
SessionDurationPolicies = "sessionDurationPolicies", // handle downgrade by setting to default duration
|
SessionDurationPolicies = "sessionDurationPolicies", // handle downgrade by setting to default duration
|
||||||
PasswordExpirationPolicies = "passwordExpirationPolicies", // handle downgrade by setting to default duration
|
PasswordExpirationPolicies = "passwordExpirationPolicies", // handle downgrade by setting to default duration
|
||||||
AutoProvisioning = "autoProvisioning", // handle downgrade by disabling auto provisioning
|
AutoProvisioning = "autoProvisioning", // handle downgrade by disabling auto provisioning
|
||||||
SshPam = "sshPam",
|
SshPam = "sshPam"
|
||||||
SiteProvisioningKeys = "siteProvisioningKeys" // handle downgrade by revoking keys if needed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const tierMatrix: Record<TierFeature, Tier[]> = {
|
export const tierMatrix: Record<TierFeature, Tier[]> = {
|
||||||
@@ -28,7 +26,6 @@ export const tierMatrix: Record<TierFeature, Tier[]> = {
|
|||||||
[TierFeature.LogExport]: ["tier3", "enterprise"],
|
[TierFeature.LogExport]: ["tier3", "enterprise"],
|
||||||
[TierFeature.AccessLogs]: ["tier2", "tier3", "enterprise"],
|
[TierFeature.AccessLogs]: ["tier2", "tier3", "enterprise"],
|
||||||
[TierFeature.ActionLogs]: ["tier2", "tier3", "enterprise"],
|
[TierFeature.ActionLogs]: ["tier2", "tier3", "enterprise"],
|
||||||
[TierFeature.ConnectionLogs]: ["tier2", "tier3", "enterprise"],
|
|
||||||
[TierFeature.RotateCredentials]: ["tier1", "tier2", "tier3", "enterprise"],
|
[TierFeature.RotateCredentials]: ["tier1", "tier2", "tier3", "enterprise"],
|
||||||
[TierFeature.MaintencePage]: ["tier1", "tier2", "tier3", "enterprise"],
|
[TierFeature.MaintencePage]: ["tier1", "tier2", "tier3", "enterprise"],
|
||||||
[TierFeature.DevicePosture]: ["tier2", "tier3", "enterprise"],
|
[TierFeature.DevicePosture]: ["tier2", "tier3", "enterprise"],
|
||||||
@@ -51,6 +48,5 @@ export const tierMatrix: Record<TierFeature, Tier[]> = {
|
|||||||
"enterprise"
|
"enterprise"
|
||||||
],
|
],
|
||||||
[TierFeature.AutoProvisioning]: ["tier1", "tier3", "enterprise"],
|
[TierFeature.AutoProvisioning]: ["tier1", "tier3", "enterprise"],
|
||||||
[TierFeature.SshPam]: ["tier1", "tier3", "enterprise"],
|
[TierFeature.SshPam]: ["tier1", "tier3", "enterprise"]
|
||||||
[TierFeature.SiteProvisioningKeys]: ["enterprise"]
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { db, orgs } from "@server/db";
|
|||||||
import { cleanUpOldLogs as cleanUpOldAccessLogs } from "#dynamic/lib/logAccessAudit";
|
import { cleanUpOldLogs as cleanUpOldAccessLogs } from "#dynamic/lib/logAccessAudit";
|
||||||
import { cleanUpOldLogs as cleanUpOldActionLogs } from "#dynamic/middlewares/logActionAudit";
|
import { cleanUpOldLogs as cleanUpOldActionLogs } from "#dynamic/middlewares/logActionAudit";
|
||||||
import { cleanUpOldLogs as cleanUpOldRequestLogs } from "@server/routers/badger/logRequestAudit";
|
import { cleanUpOldLogs as cleanUpOldRequestLogs } from "@server/routers/badger/logRequestAudit";
|
||||||
import { cleanUpOldLogs as cleanUpOldConnectionLogs } from "#dynamic/routers/newt";
|
|
||||||
import { gt, or } from "drizzle-orm";
|
import { gt, or } from "drizzle-orm";
|
||||||
import { cleanUpOldFingerprintSnapshots } from "@server/routers/olm/fingerprintingUtils";
|
import { cleanUpOldFingerprintSnapshots } from "@server/routers/olm/fingerprintingUtils";
|
||||||
import { build } from "@server/build";
|
import { build } from "@server/build";
|
||||||
@@ -21,17 +20,14 @@ export function initLogCleanupInterval() {
|
|||||||
settingsLogRetentionDaysAccess:
|
settingsLogRetentionDaysAccess:
|
||||||
orgs.settingsLogRetentionDaysAccess,
|
orgs.settingsLogRetentionDaysAccess,
|
||||||
settingsLogRetentionDaysRequest:
|
settingsLogRetentionDaysRequest:
|
||||||
orgs.settingsLogRetentionDaysRequest,
|
orgs.settingsLogRetentionDaysRequest
|
||||||
settingsLogRetentionDaysConnection:
|
|
||||||
orgs.settingsLogRetentionDaysConnection
|
|
||||||
})
|
})
|
||||||
.from(orgs)
|
.from(orgs)
|
||||||
.where(
|
.where(
|
||||||
or(
|
or(
|
||||||
gt(orgs.settingsLogRetentionDaysAction, 0),
|
gt(orgs.settingsLogRetentionDaysAction, 0),
|
||||||
gt(orgs.settingsLogRetentionDaysAccess, 0),
|
gt(orgs.settingsLogRetentionDaysAccess, 0),
|
||||||
gt(orgs.settingsLogRetentionDaysRequest, 0),
|
gt(orgs.settingsLogRetentionDaysRequest, 0)
|
||||||
gt(orgs.settingsLogRetentionDaysConnection, 0)
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -41,8 +37,7 @@ export function initLogCleanupInterval() {
|
|||||||
orgId,
|
orgId,
|
||||||
settingsLogRetentionDaysAction,
|
settingsLogRetentionDaysAction,
|
||||||
settingsLogRetentionDaysAccess,
|
settingsLogRetentionDaysAccess,
|
||||||
settingsLogRetentionDaysRequest,
|
settingsLogRetentionDaysRequest
|
||||||
settingsLogRetentionDaysConnection
|
|
||||||
} = org;
|
} = org;
|
||||||
|
|
||||||
if (settingsLogRetentionDaysAction > 0) {
|
if (settingsLogRetentionDaysAction > 0) {
|
||||||
@@ -65,13 +60,6 @@ export function initLogCleanupInterval() {
|
|||||||
settingsLogRetentionDaysRequest
|
settingsLogRetentionDaysRequest
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settingsLogRetentionDaysConnection > 0) {
|
|
||||||
await cleanUpOldConnectionLogs(
|
|
||||||
orgId,
|
|
||||||
settingsLogRetentionDaysConnection
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await cleanUpOldFingerprintSnapshots(365);
|
await cleanUpOldFingerprintSnapshots(365);
|
||||||
|
|||||||
127
server/lib/ip.ts
127
server/lib/ip.ts
@@ -571,133 +571,6 @@ export function generateSubnetProxyTargets(
|
|||||||
return targets;
|
return targets;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SubnetProxyTargetV2 = {
|
|
||||||
sourcePrefixes: string[]; // must be cidrs
|
|
||||||
destPrefix: string; // must be a cidr
|
|
||||||
disableIcmp?: boolean;
|
|
||||||
rewriteTo?: string; // must be a cidr
|
|
||||||
portRange?: {
|
|
||||||
min: number;
|
|
||||||
max: number;
|
|
||||||
protocol: "tcp" | "udp";
|
|
||||||
}[];
|
|
||||||
resourceId?: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function generateSubnetProxyTargetV2(
|
|
||||||
siteResource: SiteResource,
|
|
||||||
clients: {
|
|
||||||
clientId: number;
|
|
||||||
pubKey: string | null;
|
|
||||||
subnet: string | null;
|
|
||||||
}[]
|
|
||||||
): SubnetProxyTargetV2 | undefined {
|
|
||||||
if (clients.length === 0) {
|
|
||||||
logger.debug(
|
|
||||||
`No clients have access to site resource ${siteResource.siteResourceId}, skipping target generation.`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let target: SubnetProxyTargetV2 | null = null;
|
|
||||||
|
|
||||||
const portRange = [
|
|
||||||
...parsePortRangeString(siteResource.tcpPortRangeString, "tcp"),
|
|
||||||
...parsePortRangeString(siteResource.udpPortRangeString, "udp")
|
|
||||||
];
|
|
||||||
const disableIcmp = siteResource.disableIcmp ?? false;
|
|
||||||
|
|
||||||
if (siteResource.mode == "host") {
|
|
||||||
let destination = siteResource.destination;
|
|
||||||
// check if this is a valid ip
|
|
||||||
const ipSchema = z.union([z.ipv4(), z.ipv6()]);
|
|
||||||
if (ipSchema.safeParse(destination).success) {
|
|
||||||
destination = `${destination}/32`;
|
|
||||||
|
|
||||||
target = {
|
|
||||||
sourcePrefixes: [],
|
|
||||||
destPrefix: destination,
|
|
||||||
portRange,
|
|
||||||
disableIcmp,
|
|
||||||
resourceId: siteResource.siteResourceId,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (siteResource.alias && siteResource.aliasAddress) {
|
|
||||||
// also push a match for the alias address
|
|
||||||
target = {
|
|
||||||
sourcePrefixes: [],
|
|
||||||
destPrefix: `${siteResource.aliasAddress}/32`,
|
|
||||||
rewriteTo: destination,
|
|
||||||
portRange,
|
|
||||||
disableIcmp,
|
|
||||||
resourceId: siteResource.siteResourceId,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} else if (siteResource.mode == "cidr") {
|
|
||||||
target = {
|
|
||||||
sourcePrefixes: [],
|
|
||||||
destPrefix: siteResource.destination,
|
|
||||||
portRange,
|
|
||||||
disableIcmp,
|
|
||||||
resourceId: siteResource.siteResourceId,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!target) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const clientSite of clients) {
|
|
||||||
if (!clientSite.subnet) {
|
|
||||||
logger.debug(
|
|
||||||
`Client ${clientSite.clientId} has no subnet, skipping for site resource ${siteResource.siteResourceId}.`
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const clientPrefix = `${clientSite.subnet.split("/")[0]}/32`;
|
|
||||||
|
|
||||||
// add client prefix to source prefixes
|
|
||||||
target.sourcePrefixes.push(clientPrefix);
|
|
||||||
}
|
|
||||||
|
|
||||||
// print a nice representation of the targets
|
|
||||||
// logger.debug(
|
|
||||||
// `Generated subnet proxy targets for: ${JSON.stringify(targets, null, 2)}`
|
|
||||||
// );
|
|
||||||
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts a SubnetProxyTargetV2 to an array of SubnetProxyTarget (v1)
|
|
||||||
* by expanding each source prefix into its own target entry.
|
|
||||||
* @param targetV2 - The v2 target to convert
|
|
||||||
* @returns Array of v1 SubnetProxyTarget objects
|
|
||||||
*/
|
|
||||||
export function convertSubnetProxyTargetsV2ToV1(
|
|
||||||
targetsV2: SubnetProxyTargetV2[]
|
|
||||||
): SubnetProxyTarget[] {
|
|
||||||
return targetsV2.flatMap((targetV2) =>
|
|
||||||
targetV2.sourcePrefixes.map((sourcePrefix) => ({
|
|
||||||
sourcePrefix,
|
|
||||||
destPrefix: targetV2.destPrefix,
|
|
||||||
...(targetV2.disableIcmp !== undefined && {
|
|
||||||
disableIcmp: targetV2.disableIcmp
|
|
||||||
}),
|
|
||||||
...(targetV2.rewriteTo !== undefined && {
|
|
||||||
rewriteTo: targetV2.rewriteTo
|
|
||||||
}),
|
|
||||||
...(targetV2.portRange !== undefined && {
|
|
||||||
portRange: targetV2.portRange
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Custom schema for validating port range strings
|
// Custom schema for validating port range strings
|
||||||
// Format: "80,443,8000-9000" or "*" for all ports, or empty string
|
// Format: "80,443,8000-9000" or "*" for all ports, or empty string
|
||||||
export const portRangeStringSchema = z
|
export const portRangeStringSchema = z
|
||||||
|
|||||||
@@ -302,8 +302,8 @@ export const configSchema = z
|
|||||||
.optional()
|
.optional()
|
||||||
.default({
|
.default({
|
||||||
block_size: 24,
|
block_size: 24,
|
||||||
subnet_group: "100.90.128.0/20",
|
subnet_group: "100.90.128.0/24",
|
||||||
utility_subnet_group: "100.96.128.0/20"
|
utility_subnet_group: "100.96.128.0/24"
|
||||||
}),
|
}),
|
||||||
rate_limits: z
|
rate_limits: z
|
||||||
.object({
|
.object({
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import logger from "@server/logger";
|
|||||||
import {
|
import {
|
||||||
generateAliasConfig,
|
generateAliasConfig,
|
||||||
generateRemoteSubnets,
|
generateRemoteSubnets,
|
||||||
generateSubnetProxyTargetV2,
|
generateSubnetProxyTargets,
|
||||||
parseEndpoint,
|
parseEndpoint,
|
||||||
formatEndpoint
|
formatEndpoint
|
||||||
} from "@server/lib/ip";
|
} from "@server/lib/ip";
|
||||||
@@ -660,16 +660,19 @@ async function handleSubnetProxyTargetUpdates(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (addedClients.length > 0) {
|
if (addedClients.length > 0) {
|
||||||
const targetToAdd = generateSubnetProxyTargetV2(
|
const targetsToAdd = generateSubnetProxyTargets(
|
||||||
siteResource,
|
siteResource,
|
||||||
addedClients
|
addedClients
|
||||||
);
|
);
|
||||||
|
|
||||||
if (targetToAdd) {
|
if (targetsToAdd.length > 0) {
|
||||||
|
logger.info(
|
||||||
|
`Adding ${targetsToAdd.length} subnet proxy targets for siteResource ${siteResource.siteResourceId}`
|
||||||
|
);
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
addSubnetProxyTargets(
|
addSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
[targetToAdd],
|
targetsToAdd,
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -697,16 +700,19 @@ async function handleSubnetProxyTargetUpdates(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (removedClients.length > 0) {
|
if (removedClients.length > 0) {
|
||||||
const targetToRemove = generateSubnetProxyTargetV2(
|
const targetsToRemove = generateSubnetProxyTargets(
|
||||||
siteResource,
|
siteResource,
|
||||||
removedClients
|
removedClients
|
||||||
);
|
);
|
||||||
|
|
||||||
if (targetToRemove) {
|
if (targetsToRemove.length > 0) {
|
||||||
|
logger.info(
|
||||||
|
`Removing ${targetsToRemove.length} subnet proxy targets for siteResource ${siteResource.siteResourceId}`
|
||||||
|
);
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
removeSubnetProxyTargets(
|
removeSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
[targetToRemove],
|
targetsToRemove,
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -1163,7 +1169,7 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const resource of resources) {
|
for (const resource of resources) {
|
||||||
const target = generateSubnetProxyTargetV2(resource, [
|
const targets = generateSubnetProxyTargets(resource, [
|
||||||
{
|
{
|
||||||
clientId: client.clientId,
|
clientId: client.clientId,
|
||||||
pubKey: client.pubKey,
|
pubKey: client.pubKey,
|
||||||
@@ -1171,11 +1177,11 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (target) {
|
if (targets.length > 0) {
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
addSubnetProxyTargets(
|
addSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
[target],
|
targets,
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -1240,7 +1246,7 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const resource of resources) {
|
for (const resource of resources) {
|
||||||
const target = generateSubnetProxyTargetV2(resource, [
|
const targets = generateSubnetProxyTargets(resource, [
|
||||||
{
|
{
|
||||||
clientId: client.clientId,
|
clientId: client.clientId,
|
||||||
pubKey: client.pubKey,
|
pubKey: client.pubKey,
|
||||||
@@ -1248,11 +1254,11 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (target) {
|
if (targets.length > 0) {
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
removeSubnetProxyTargets(
|
removeSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
[target],
|
targets,
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ export * from "./verifyClientAccess";
|
|||||||
export * from "./integration";
|
export * from "./integration";
|
||||||
export * from "./verifyUserHasAction";
|
export * from "./verifyUserHasAction";
|
||||||
export * from "./verifyApiKeyAccess";
|
export * from "./verifyApiKeyAccess";
|
||||||
export * from "./verifySiteProvisioningKeyAccess";
|
|
||||||
export * from "./verifyDomainAccess";
|
export * from "./verifyDomainAccess";
|
||||||
export * from "./verifyUserIsOrgOwner";
|
export * from "./verifyUserIsOrgOwner";
|
||||||
export * from "./verifySiteResourceAccess";
|
export * from "./verifySiteResourceAccess";
|
||||||
|
|||||||
@@ -1,131 +0,0 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
import { db, userOrgs, siteProvisioningKeys, siteProvisioningKeyOrg } from "@server/db";
|
|
||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import createHttpError from "http-errors";
|
|
||||||
import HttpCode from "@server/types/HttpCode";
|
|
||||||
import { checkOrgAccessPolicy } from "#dynamic/lib/checkOrgAccessPolicy";
|
|
||||||
|
|
||||||
export async function verifySiteProvisioningKeyAccess(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const userId = req.user!.userId;
|
|
||||||
const siteProvisioningKeyId = req.params.siteProvisioningKeyId;
|
|
||||||
const orgId = req.params.orgId;
|
|
||||||
|
|
||||||
if (!userId) {
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.UNAUTHORIZED, "User not authenticated")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!orgId) {
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.BAD_REQUEST, "Invalid organization ID")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!siteProvisioningKeyId) {
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.BAD_REQUEST, "Invalid key ID")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [row] = await db
|
|
||||||
.select()
|
|
||||||
.from(siteProvisioningKeys)
|
|
||||||
.innerJoin(
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
and(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId
|
|
||||||
),
|
|
||||||
eq(siteProvisioningKeyOrg.orgId, orgId)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!row?.siteProvisioningKeys) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.NOT_FOUND,
|
|
||||||
`Site provisioning key with ID ${siteProvisioningKeyId} not found`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!row.siteProvisioningKeyOrg.orgId) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.INTERNAL_SERVER_ERROR,
|
|
||||||
`Site provisioning key with ID ${siteProvisioningKeyId} does not have an organization ID`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!req.userOrg) {
|
|
||||||
const userOrgRole = await db
|
|
||||||
.select()
|
|
||||||
.from(userOrgs)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(userOrgs.userId, userId),
|
|
||||||
eq(
|
|
||||||
userOrgs.orgId,
|
|
||||||
row.siteProvisioningKeyOrg.orgId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
req.userOrg = userOrgRole[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!req.userOrg) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.FORBIDDEN,
|
|
||||||
"User does not have access to this organization"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.orgPolicyAllowed === undefined && req.userOrg.orgId) {
|
|
||||||
const policyCheck = await checkOrgAccessPolicy({
|
|
||||||
orgId: req.userOrg.orgId,
|
|
||||||
userId,
|
|
||||||
session: req.session
|
|
||||||
});
|
|
||||||
req.orgPolicyAllowed = policyCheck.allowed;
|
|
||||||
if (!policyCheck.allowed || policyCheck.error) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.FORBIDDEN,
|
|
||||||
"Failed organization access policy check: " +
|
|
||||||
(policyCheck.error || "Unknown error")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const userOrgRoleId = req.userOrg.roleId;
|
|
||||||
req.userOrgRoleId = userOrgRoleId;
|
|
||||||
|
|
||||||
return next();
|
|
||||||
} catch (error) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.INTERNAL_SERVER_ERROR,
|
|
||||||
"Error verifying site provisioning key access"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,14 +14,12 @@
|
|||||||
import { rateLimitService } from "#private/lib/rateLimit";
|
import { rateLimitService } from "#private/lib/rateLimit";
|
||||||
import { cleanup as wsCleanup } from "#private/routers/ws";
|
import { cleanup as wsCleanup } from "#private/routers/ws";
|
||||||
import { flushBandwidthToDb } from "@server/routers/newt/handleReceiveBandwidthMessage";
|
import { flushBandwidthToDb } from "@server/routers/newt/handleReceiveBandwidthMessage";
|
||||||
import { flushConnectionLogToDb } from "#dynamic/routers/newt";
|
|
||||||
import { flushSiteBandwidthToDb } from "@server/routers/gerbil/receiveBandwidth";
|
import { flushSiteBandwidthToDb } from "@server/routers/gerbil/receiveBandwidth";
|
||||||
import { stopPingAccumulator } from "@server/routers/newt/pingAccumulator";
|
import { stopPingAccumulator } from "@server/routers/newt/pingAccumulator";
|
||||||
|
|
||||||
async function cleanup() {
|
async function cleanup() {
|
||||||
await stopPingAccumulator();
|
await stopPingAccumulator();
|
||||||
await flushBandwidthToDb();
|
await flushBandwidthToDb();
|
||||||
await flushConnectionLogToDb();
|
|
||||||
await flushSiteBandwidthToDb();
|
await flushSiteBandwidthToDb();
|
||||||
await rateLimitService.cleanup();
|
await rateLimitService.cleanup();
|
||||||
await wsCleanup();
|
await wsCleanup();
|
||||||
@@ -33,4 +31,4 @@ export async function initCleanup() {
|
|||||||
// Handle process termination
|
// Handle process termination
|
||||||
process.on("SIGTERM", () => cleanup());
|
process.on("SIGTERM", () => cleanup());
|
||||||
process.on("SIGINT", () => cleanup());
|
process.on("SIGINT", () => cleanup());
|
||||||
}
|
}
|
||||||
@@ -57,10 +57,7 @@ export const privateConfigSchema = z.object({
|
|||||||
.object({
|
.object({
|
||||||
host: z.string(),
|
host: z.string(),
|
||||||
port: portSchema,
|
port: portSchema,
|
||||||
password: z
|
password: z.string().optional(),
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform(getEnvOrYaml("REDIS_PASSWORD")),
|
|
||||||
db: z.int().nonnegative().optional().default(0),
|
db: z.int().nonnegative().optional().default(0),
|
||||||
replicas: z
|
replicas: z
|
||||||
.array(
|
.array(
|
||||||
|
|||||||
@@ -1,99 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { registry } from "@server/openApi";
|
|
||||||
import { NextFunction } from "express";
|
|
||||||
import { Request, Response } from "express";
|
|
||||||
import { OpenAPITags } from "@server/openApi";
|
|
||||||
import createHttpError from "http-errors";
|
|
||||||
import HttpCode from "@server/types/HttpCode";
|
|
||||||
import { fromError } from "zod-validation-error";
|
|
||||||
import logger from "@server/logger";
|
|
||||||
import {
|
|
||||||
queryConnectionAuditLogsParams,
|
|
||||||
queryConnectionAuditLogsQuery,
|
|
||||||
queryConnection,
|
|
||||||
countConnectionQuery
|
|
||||||
} from "./queryConnectionAuditLog";
|
|
||||||
import { generateCSV } from "@server/routers/auditLogs/generateCSV";
|
|
||||||
import { MAX_EXPORT_LIMIT } from "@server/routers/auditLogs";
|
|
||||||
|
|
||||||
registry.registerPath({
|
|
||||||
method: "get",
|
|
||||||
path: "/org/{orgId}/logs/connection/export",
|
|
||||||
description: "Export the connection audit log for an organization as CSV",
|
|
||||||
tags: [OpenAPITags.Logs],
|
|
||||||
request: {
|
|
||||||
query: queryConnectionAuditLogsQuery,
|
|
||||||
params: queryConnectionAuditLogsParams
|
|
||||||
},
|
|
||||||
responses: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
export async function exportConnectionAuditLogs(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<any> {
|
|
||||||
try {
|
|
||||||
const parsedQuery = queryConnectionAuditLogsQuery.safeParse(req.query);
|
|
||||||
if (!parsedQuery.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedQuery.error)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsedParams = queryConnectionAuditLogsParams.safeParse(req.params);
|
|
||||||
if (!parsedParams.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedParams.error)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = { ...parsedQuery.data, ...parsedParams.data };
|
|
||||||
const [{ count }] = await countConnectionQuery(data);
|
|
||||||
if (count > MAX_EXPORT_LIMIT) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
`Export limit exceeded. Your selection contains ${count} rows, but the maximum is ${MAX_EXPORT_LIMIT} rows. Please select a shorter time range to reduce the data.`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseQuery = queryConnection(data);
|
|
||||||
|
|
||||||
const log = await baseQuery.limit(data.limit).offset(data.offset);
|
|
||||||
|
|
||||||
const csvData = generateCSV(log);
|
|
||||||
|
|
||||||
res.setHeader("Content-Type", "text/csv");
|
|
||||||
res.setHeader(
|
|
||||||
"Content-Disposition",
|
|
||||||
`attachment; filename="connection-audit-logs-${data.orgId}-${Date.now()}.csv"`
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.send(csvData);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error);
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,5 +15,3 @@ export * from "./queryActionAuditLog";
|
|||||||
export * from "./exportActionAuditLog";
|
export * from "./exportActionAuditLog";
|
||||||
export * from "./queryAccessAuditLog";
|
export * from "./queryAccessAuditLog";
|
||||||
export * from "./exportAccessAuditLog";
|
export * from "./exportAccessAuditLog";
|
||||||
export * from "./queryConnectionAuditLog";
|
|
||||||
export * from "./exportConnectionAuditLog";
|
|
||||||
|
|||||||
@@ -1,524 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {
|
|
||||||
connectionAuditLog,
|
|
||||||
logsDb,
|
|
||||||
siteResources,
|
|
||||||
sites,
|
|
||||||
clients,
|
|
||||||
users,
|
|
||||||
primaryDb
|
|
||||||
} from "@server/db";
|
|
||||||
import { registry } from "@server/openApi";
|
|
||||||
import { NextFunction } from "express";
|
|
||||||
import { Request, Response } from "express";
|
|
||||||
import { eq, gt, lt, and, count, desc, inArray } from "drizzle-orm";
|
|
||||||
import { OpenAPITags } from "@server/openApi";
|
|
||||||
import { z } from "zod";
|
|
||||||
import createHttpError from "http-errors";
|
|
||||||
import HttpCode from "@server/types/HttpCode";
|
|
||||||
import { fromError } from "zod-validation-error";
|
|
||||||
import { QueryConnectionAuditLogResponse } from "@server/routers/auditLogs/types";
|
|
||||||
import response from "@server/lib/response";
|
|
||||||
import logger from "@server/logger";
|
|
||||||
import { getSevenDaysAgo } from "@app/lib/getSevenDaysAgo";
|
|
||||||
|
|
||||||
export const queryConnectionAuditLogsQuery = z.object({
|
|
||||||
// iso string just validate its a parseable date
|
|
||||||
timeStart: z
|
|
||||||
.string()
|
|
||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
|
||||||
error: "timeStart must be a valid ISO date string"
|
|
||||||
})
|
|
||||||
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
|
||||||
.prefault(() => getSevenDaysAgo().toISOString())
|
|
||||||
.openapi({
|
|
||||||
type: "string",
|
|
||||||
format: "date-time",
|
|
||||||
description:
|
|
||||||
"Start time as ISO date string (defaults to 7 days ago)"
|
|
||||||
}),
|
|
||||||
timeEnd: z
|
|
||||||
.string()
|
|
||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
|
||||||
error: "timeEnd must be a valid ISO date string"
|
|
||||||
})
|
|
||||||
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
|
||||||
.optional()
|
|
||||||
.prefault(() => new Date().toISOString())
|
|
||||||
.openapi({
|
|
||||||
type: "string",
|
|
||||||
format: "date-time",
|
|
||||||
description:
|
|
||||||
"End time as ISO date string (defaults to current time)"
|
|
||||||
}),
|
|
||||||
protocol: z.string().optional(),
|
|
||||||
sourceAddr: z.string().optional(),
|
|
||||||
destAddr: z.string().optional(),
|
|
||||||
clientId: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform(Number)
|
|
||||||
.pipe(z.int().positive())
|
|
||||||
.optional(),
|
|
||||||
siteId: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform(Number)
|
|
||||||
.pipe(z.int().positive())
|
|
||||||
.optional(),
|
|
||||||
siteResourceId: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform(Number)
|
|
||||||
.pipe(z.int().positive())
|
|
||||||
.optional(),
|
|
||||||
userId: z.string().optional(),
|
|
||||||
limit: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.default("1000")
|
|
||||||
.transform(Number)
|
|
||||||
.pipe(z.int().positive()),
|
|
||||||
offset: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.default("0")
|
|
||||||
.transform(Number)
|
|
||||||
.pipe(z.int().nonnegative())
|
|
||||||
});
|
|
||||||
|
|
||||||
export const queryConnectionAuditLogsParams = z.object({
|
|
||||||
orgId: z.string()
|
|
||||||
});
|
|
||||||
|
|
||||||
export const queryConnectionAuditLogsCombined =
|
|
||||||
queryConnectionAuditLogsQuery.merge(queryConnectionAuditLogsParams);
|
|
||||||
type Q = z.infer<typeof queryConnectionAuditLogsCombined>;
|
|
||||||
|
|
||||||
function getWhere(data: Q) {
|
|
||||||
return and(
|
|
||||||
gt(connectionAuditLog.startedAt, data.timeStart),
|
|
||||||
lt(connectionAuditLog.startedAt, data.timeEnd),
|
|
||||||
eq(connectionAuditLog.orgId, data.orgId),
|
|
||||||
data.protocol
|
|
||||||
? eq(connectionAuditLog.protocol, data.protocol)
|
|
||||||
: undefined,
|
|
||||||
data.sourceAddr
|
|
||||||
? eq(connectionAuditLog.sourceAddr, data.sourceAddr)
|
|
||||||
: undefined,
|
|
||||||
data.destAddr
|
|
||||||
? eq(connectionAuditLog.destAddr, data.destAddr)
|
|
||||||
: undefined,
|
|
||||||
data.clientId
|
|
||||||
? eq(connectionAuditLog.clientId, data.clientId)
|
|
||||||
: undefined,
|
|
||||||
data.siteId
|
|
||||||
? eq(connectionAuditLog.siteId, data.siteId)
|
|
||||||
: undefined,
|
|
||||||
data.siteResourceId
|
|
||||||
? eq(connectionAuditLog.siteResourceId, data.siteResourceId)
|
|
||||||
: undefined,
|
|
||||||
data.userId
|
|
||||||
? eq(connectionAuditLog.userId, data.userId)
|
|
||||||
: undefined
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function queryConnection(data: Q) {
|
|
||||||
return logsDb
|
|
||||||
.select({
|
|
||||||
sessionId: connectionAuditLog.sessionId,
|
|
||||||
siteResourceId: connectionAuditLog.siteResourceId,
|
|
||||||
orgId: connectionAuditLog.orgId,
|
|
||||||
siteId: connectionAuditLog.siteId,
|
|
||||||
clientId: connectionAuditLog.clientId,
|
|
||||||
userId: connectionAuditLog.userId,
|
|
||||||
sourceAddr: connectionAuditLog.sourceAddr,
|
|
||||||
destAddr: connectionAuditLog.destAddr,
|
|
||||||
protocol: connectionAuditLog.protocol,
|
|
||||||
startedAt: connectionAuditLog.startedAt,
|
|
||||||
endedAt: connectionAuditLog.endedAt,
|
|
||||||
bytesTx: connectionAuditLog.bytesTx,
|
|
||||||
bytesRx: connectionAuditLog.bytesRx
|
|
||||||
})
|
|
||||||
.from(connectionAuditLog)
|
|
||||||
.where(getWhere(data))
|
|
||||||
.orderBy(
|
|
||||||
desc(connectionAuditLog.startedAt),
|
|
||||||
desc(connectionAuditLog.id)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function countConnectionQuery(data: Q) {
|
|
||||||
const countQuery = logsDb
|
|
||||||
.select({ count: count() })
|
|
||||||
.from(connectionAuditLog)
|
|
||||||
.where(getWhere(data));
|
|
||||||
return countQuery;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function enrichWithDetails(
|
|
||||||
logs: Awaited<ReturnType<typeof queryConnection>>
|
|
||||||
) {
|
|
||||||
// Collect unique IDs from logs
|
|
||||||
const siteResourceIds = [
|
|
||||||
...new Set(
|
|
||||||
logs
|
|
||||||
.map((log) => log.siteResourceId)
|
|
||||||
.filter((id): id is number => id !== null && id !== undefined)
|
|
||||||
)
|
|
||||||
];
|
|
||||||
const siteIds = [
|
|
||||||
...new Set(
|
|
||||||
logs
|
|
||||||
.map((log) => log.siteId)
|
|
||||||
.filter((id): id is number => id !== null && id !== undefined)
|
|
||||||
)
|
|
||||||
];
|
|
||||||
const clientIds = [
|
|
||||||
...new Set(
|
|
||||||
logs
|
|
||||||
.map((log) => log.clientId)
|
|
||||||
.filter((id): id is number => id !== null && id !== undefined)
|
|
||||||
)
|
|
||||||
];
|
|
||||||
const userIds = [
|
|
||||||
...new Set(
|
|
||||||
logs
|
|
||||||
.map((log) => log.userId)
|
|
||||||
.filter((id): id is string => id !== null && id !== undefined)
|
|
||||||
)
|
|
||||||
];
|
|
||||||
|
|
||||||
// Fetch resource details from main database
|
|
||||||
const resourceMap = new Map<
|
|
||||||
number,
|
|
||||||
{ name: string; niceId: string }
|
|
||||||
>();
|
|
||||||
if (siteResourceIds.length > 0) {
|
|
||||||
const resourceDetails = await primaryDb
|
|
||||||
.select({
|
|
||||||
siteResourceId: siteResources.siteResourceId,
|
|
||||||
name: siteResources.name,
|
|
||||||
niceId: siteResources.niceId
|
|
||||||
})
|
|
||||||
.from(siteResources)
|
|
||||||
.where(inArray(siteResources.siteResourceId, siteResourceIds));
|
|
||||||
|
|
||||||
for (const r of resourceDetails) {
|
|
||||||
resourceMap.set(r.siteResourceId, {
|
|
||||||
name: r.name,
|
|
||||||
niceId: r.niceId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch site details from main database
|
|
||||||
const siteMap = new Map<number, { name: string; niceId: string }>();
|
|
||||||
if (siteIds.length > 0) {
|
|
||||||
const siteDetails = await primaryDb
|
|
||||||
.select({
|
|
||||||
siteId: sites.siteId,
|
|
||||||
name: sites.name,
|
|
||||||
niceId: sites.niceId
|
|
||||||
})
|
|
||||||
.from(sites)
|
|
||||||
.where(inArray(sites.siteId, siteIds));
|
|
||||||
|
|
||||||
for (const s of siteDetails) {
|
|
||||||
siteMap.set(s.siteId, { name: s.name, niceId: s.niceId });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch client details from main database
|
|
||||||
const clientMap = new Map<
|
|
||||||
number,
|
|
||||||
{ name: string; niceId: string; type: string }
|
|
||||||
>();
|
|
||||||
if (clientIds.length > 0) {
|
|
||||||
const clientDetails = await primaryDb
|
|
||||||
.select({
|
|
||||||
clientId: clients.clientId,
|
|
||||||
name: clients.name,
|
|
||||||
niceId: clients.niceId,
|
|
||||||
type: clients.type
|
|
||||||
})
|
|
||||||
.from(clients)
|
|
||||||
.where(inArray(clients.clientId, clientIds));
|
|
||||||
|
|
||||||
for (const c of clientDetails) {
|
|
||||||
clientMap.set(c.clientId, {
|
|
||||||
name: c.name,
|
|
||||||
niceId: c.niceId,
|
|
||||||
type: c.type
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch user details from main database
|
|
||||||
const userMap = new Map<
|
|
||||||
string,
|
|
||||||
{ email: string | null }
|
|
||||||
>();
|
|
||||||
if (userIds.length > 0) {
|
|
||||||
const userDetails = await primaryDb
|
|
||||||
.select({
|
|
||||||
userId: users.userId,
|
|
||||||
email: users.email
|
|
||||||
})
|
|
||||||
.from(users)
|
|
||||||
.where(inArray(users.userId, userIds));
|
|
||||||
|
|
||||||
for (const u of userDetails) {
|
|
||||||
userMap.set(u.userId, { email: u.email });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enrich logs with details
|
|
||||||
return logs.map((log) => ({
|
|
||||||
...log,
|
|
||||||
resourceName: log.siteResourceId
|
|
||||||
? resourceMap.get(log.siteResourceId)?.name ?? null
|
|
||||||
: null,
|
|
||||||
resourceNiceId: log.siteResourceId
|
|
||||||
? resourceMap.get(log.siteResourceId)?.niceId ?? null
|
|
||||||
: null,
|
|
||||||
siteName: log.siteId
|
|
||||||
? siteMap.get(log.siteId)?.name ?? null
|
|
||||||
: null,
|
|
||||||
siteNiceId: log.siteId
|
|
||||||
? siteMap.get(log.siteId)?.niceId ?? null
|
|
||||||
: null,
|
|
||||||
clientName: log.clientId
|
|
||||||
? clientMap.get(log.clientId)?.name ?? null
|
|
||||||
: null,
|
|
||||||
clientNiceId: log.clientId
|
|
||||||
? clientMap.get(log.clientId)?.niceId ?? null
|
|
||||||
: null,
|
|
||||||
clientType: log.clientId
|
|
||||||
? clientMap.get(log.clientId)?.type ?? null
|
|
||||||
: null,
|
|
||||||
userEmail: log.userId
|
|
||||||
? userMap.get(log.userId)?.email ?? null
|
|
||||||
: null
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function queryUniqueFilterAttributes(
|
|
||||||
timeStart: number,
|
|
||||||
timeEnd: number,
|
|
||||||
orgId: string
|
|
||||||
) {
|
|
||||||
const baseConditions = and(
|
|
||||||
gt(connectionAuditLog.startedAt, timeStart),
|
|
||||||
lt(connectionAuditLog.startedAt, timeEnd),
|
|
||||||
eq(connectionAuditLog.orgId, orgId)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Get unique protocols
|
|
||||||
const uniqueProtocols = await logsDb
|
|
||||||
.selectDistinct({
|
|
||||||
protocol: connectionAuditLog.protocol
|
|
||||||
})
|
|
||||||
.from(connectionAuditLog)
|
|
||||||
.where(baseConditions);
|
|
||||||
|
|
||||||
// Get unique destination addresses
|
|
||||||
const uniqueDestAddrs = await logsDb
|
|
||||||
.selectDistinct({
|
|
||||||
destAddr: connectionAuditLog.destAddr
|
|
||||||
})
|
|
||||||
.from(connectionAuditLog)
|
|
||||||
.where(baseConditions);
|
|
||||||
|
|
||||||
// Get unique client IDs
|
|
||||||
const uniqueClients = await logsDb
|
|
||||||
.selectDistinct({
|
|
||||||
clientId: connectionAuditLog.clientId
|
|
||||||
})
|
|
||||||
.from(connectionAuditLog)
|
|
||||||
.where(baseConditions);
|
|
||||||
|
|
||||||
// Get unique resource IDs
|
|
||||||
const uniqueResources = await logsDb
|
|
||||||
.selectDistinct({
|
|
||||||
siteResourceId: connectionAuditLog.siteResourceId
|
|
||||||
})
|
|
||||||
.from(connectionAuditLog)
|
|
||||||
.where(baseConditions);
|
|
||||||
|
|
||||||
// Get unique user IDs
|
|
||||||
const uniqueUsers = await logsDb
|
|
||||||
.selectDistinct({
|
|
||||||
userId: connectionAuditLog.userId
|
|
||||||
})
|
|
||||||
.from(connectionAuditLog)
|
|
||||||
.where(baseConditions);
|
|
||||||
|
|
||||||
// Enrich client IDs with names from main database
|
|
||||||
const clientIds = uniqueClients
|
|
||||||
.map((row) => row.clientId)
|
|
||||||
.filter((id): id is number => id !== null);
|
|
||||||
|
|
||||||
let clientsWithNames: Array<{ id: number; name: string }> = [];
|
|
||||||
if (clientIds.length > 0) {
|
|
||||||
const clientDetails = await primaryDb
|
|
||||||
.select({
|
|
||||||
clientId: clients.clientId,
|
|
||||||
name: clients.name
|
|
||||||
})
|
|
||||||
.from(clients)
|
|
||||||
.where(inArray(clients.clientId, clientIds));
|
|
||||||
|
|
||||||
clientsWithNames = clientDetails.map((c) => ({
|
|
||||||
id: c.clientId,
|
|
||||||
name: c.name
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enrich resource IDs with names from main database
|
|
||||||
const resourceIds = uniqueResources
|
|
||||||
.map((row) => row.siteResourceId)
|
|
||||||
.filter((id): id is number => id !== null);
|
|
||||||
|
|
||||||
let resourcesWithNames: Array<{ id: number; name: string | null }> = [];
|
|
||||||
if (resourceIds.length > 0) {
|
|
||||||
const resourceDetails = await primaryDb
|
|
||||||
.select({
|
|
||||||
siteResourceId: siteResources.siteResourceId,
|
|
||||||
name: siteResources.name
|
|
||||||
})
|
|
||||||
.from(siteResources)
|
|
||||||
.where(inArray(siteResources.siteResourceId, resourceIds));
|
|
||||||
|
|
||||||
resourcesWithNames = resourceDetails.map((r) => ({
|
|
||||||
id: r.siteResourceId,
|
|
||||||
name: r.name
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enrich user IDs with emails from main database
|
|
||||||
const userIdsList = uniqueUsers
|
|
||||||
.map((row) => row.userId)
|
|
||||||
.filter((id): id is string => id !== null);
|
|
||||||
|
|
||||||
let usersWithEmails: Array<{ id: string; email: string | null }> = [];
|
|
||||||
if (userIdsList.length > 0) {
|
|
||||||
const userDetails = await primaryDb
|
|
||||||
.select({
|
|
||||||
userId: users.userId,
|
|
||||||
email: users.email
|
|
||||||
})
|
|
||||||
.from(users)
|
|
||||||
.where(inArray(users.userId, userIdsList));
|
|
||||||
|
|
||||||
usersWithEmails = userDetails.map((u) => ({
|
|
||||||
id: u.userId,
|
|
||||||
email: u.email
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
protocols: uniqueProtocols
|
|
||||||
.map((row) => row.protocol)
|
|
||||||
.filter((protocol): protocol is string => protocol !== null),
|
|
||||||
destAddrs: uniqueDestAddrs
|
|
||||||
.map((row) => row.destAddr)
|
|
||||||
.filter((addr): addr is string => addr !== null),
|
|
||||||
clients: clientsWithNames,
|
|
||||||
resources: resourcesWithNames,
|
|
||||||
users: usersWithEmails
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
registry.registerPath({
|
|
||||||
method: "get",
|
|
||||||
path: "/org/{orgId}/logs/connection",
|
|
||||||
description: "Query the connection audit log for an organization",
|
|
||||||
tags: [OpenAPITags.Logs],
|
|
||||||
request: {
|
|
||||||
query: queryConnectionAuditLogsQuery,
|
|
||||||
params: queryConnectionAuditLogsParams
|
|
||||||
},
|
|
||||||
responses: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
export async function queryConnectionAuditLogs(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<any> {
|
|
||||||
try {
|
|
||||||
const parsedQuery = queryConnectionAuditLogsQuery.safeParse(req.query);
|
|
||||||
if (!parsedQuery.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedQuery.error)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const parsedParams = queryConnectionAuditLogsParams.safeParse(
|
|
||||||
req.params
|
|
||||||
);
|
|
||||||
if (!parsedParams.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedParams.error)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = { ...parsedQuery.data, ...parsedParams.data };
|
|
||||||
|
|
||||||
const baseQuery = queryConnection(data);
|
|
||||||
|
|
||||||
const logsRaw = await baseQuery.limit(data.limit).offset(data.offset);
|
|
||||||
|
|
||||||
// Enrich with resource, site, client, and user details
|
|
||||||
const log = await enrichWithDetails(logsRaw);
|
|
||||||
|
|
||||||
const totalCountResult = await countConnectionQuery(data);
|
|
||||||
const totalCount = totalCountResult[0].count;
|
|
||||||
|
|
||||||
const filterAttributes = await queryUniqueFilterAttributes(
|
|
||||||
data.timeStart,
|
|
||||||
data.timeEnd,
|
|
||||||
data.orgId
|
|
||||||
);
|
|
||||||
|
|
||||||
return response<QueryConnectionAuditLogResponse>(res, {
|
|
||||||
data: {
|
|
||||||
log: log,
|
|
||||||
pagination: {
|
|
||||||
total: totalCount,
|
|
||||||
limit: data.limit,
|
|
||||||
offset: data.offset
|
|
||||||
},
|
|
||||||
filterAttributes
|
|
||||||
},
|
|
||||||
success: true,
|
|
||||||
error: false,
|
|
||||||
message: "Connection audit logs retrieved successfully",
|
|
||||||
status: HttpCode.OK
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error);
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -26,11 +26,9 @@ import {
|
|||||||
orgs,
|
orgs,
|
||||||
resources,
|
resources,
|
||||||
roles,
|
roles,
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
siteProvisioningKeys,
|
|
||||||
siteResources
|
siteResources
|
||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
import { and, eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the maximum allowed retention days for a given tier
|
* Get the maximum allowed retention days for a given tier
|
||||||
@@ -293,10 +291,6 @@ async function disableFeature(
|
|||||||
await disableSshPam(orgId);
|
await disableSshPam(orgId);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TierFeature.SiteProvisioningKeys:
|
|
||||||
await disableSiteProvisioningKeys(orgId);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Unknown feature ${feature} for org ${orgId}, skipping`
|
`Unknown feature ${feature} for org ${orgId}, skipping`
|
||||||
@@ -332,57 +326,6 @@ async function disableSshPam(orgId: string): Promise<void> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function disableSiteProvisioningKeys(orgId: string): Promise<void> {
|
|
||||||
const rows = await db
|
|
||||||
.select({
|
|
||||||
siteProvisioningKeyId:
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId
|
|
||||||
})
|
|
||||||
.from(siteProvisioningKeyOrg)
|
|
||||||
.where(eq(siteProvisioningKeyOrg.orgId, orgId));
|
|
||||||
|
|
||||||
for (const { siteProvisioningKeyId } of rows) {
|
|
||||||
await db.transaction(async (trx) => {
|
|
||||||
await trx
|
|
||||||
.delete(siteProvisioningKeyOrg)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
),
|
|
||||||
eq(siteProvisioningKeyOrg.orgId, orgId)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const remaining = await trx
|
|
||||||
.select()
|
|
||||||
.from(siteProvisioningKeyOrg)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (remaining.length === 0) {
|
|
||||||
await trx
|
|
||||||
.delete(siteProvisioningKeys)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(
|
|
||||||
`Removed site provisioning keys for org ${orgId} after tier downgrade`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function disableLoginPageBranding(orgId: string): Promise<void> {
|
async function disableLoginPageBranding(orgId: string): Promise<void> {
|
||||||
const [existingBranding] = await db
|
const [existingBranding] = await db
|
||||||
.select()
|
.select()
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import * as misc from "#private/routers/misc";
|
|||||||
import * as reKey from "#private/routers/re-key";
|
import * as reKey from "#private/routers/re-key";
|
||||||
import * as approval from "#private/routers/approvals";
|
import * as approval from "#private/routers/approvals";
|
||||||
import * as ssh from "#private/routers/ssh";
|
import * as ssh from "#private/routers/ssh";
|
||||||
import * as siteProvisioning from "#private/routers/siteProvisioning";
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
@@ -34,8 +33,7 @@ import {
|
|||||||
verifyUserIsServerAdmin,
|
verifyUserIsServerAdmin,
|
||||||
verifySiteAccess,
|
verifySiteAccess,
|
||||||
verifyClientAccess,
|
verifyClientAccess,
|
||||||
verifyLimits,
|
verifyLimits
|
||||||
verifySiteProvisioningKeyAccess
|
|
||||||
} from "@server/middlewares";
|
} from "@server/middlewares";
|
||||||
import { ActionsEnum } from "@server/auth/actions";
|
import { ActionsEnum } from "@server/auth/actions";
|
||||||
import {
|
import {
|
||||||
@@ -480,25 +478,6 @@ authenticated.get(
|
|||||||
logs.exportAccessAuditLogs
|
logs.exportAccessAuditLogs
|
||||||
);
|
);
|
||||||
|
|
||||||
authenticated.get(
|
|
||||||
"/org/:orgId/logs/connection",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.connectionLogs),
|
|
||||||
verifyOrgAccess,
|
|
||||||
verifyUserHasAction(ActionsEnum.exportLogs),
|
|
||||||
logs.queryConnectionAuditLogs
|
|
||||||
);
|
|
||||||
|
|
||||||
authenticated.get(
|
|
||||||
"/org/:orgId/logs/connection/export",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.logExport),
|
|
||||||
verifyOrgAccess,
|
|
||||||
verifyUserHasAction(ActionsEnum.exportLogs),
|
|
||||||
logActionAudit(ActionsEnum.exportLogs),
|
|
||||||
logs.exportConnectionAuditLogs
|
|
||||||
);
|
|
||||||
|
|
||||||
authenticated.post(
|
authenticated.post(
|
||||||
"/re-key/:clientId/regenerate-client-secret",
|
"/re-key/:clientId/regenerate-client-secret",
|
||||||
verifyClientAccess, // this is first to set the org id
|
verifyClientAccess, // this is first to set the org id
|
||||||
@@ -539,45 +518,3 @@ authenticated.post(
|
|||||||
// logActionAudit(ActionsEnum.signSshKey), // it is handled inside of the function below so we can include more metadata
|
// logActionAudit(ActionsEnum.signSshKey), // it is handled inside of the function below so we can include more metadata
|
||||||
ssh.signSshKey
|
ssh.signSshKey
|
||||||
);
|
);
|
||||||
|
|
||||||
authenticated.put(
|
|
||||||
"/org/:orgId/site-provisioning-key",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.siteProvisioningKeys),
|
|
||||||
verifyOrgAccess,
|
|
||||||
verifyLimits,
|
|
||||||
verifyUserHasAction(ActionsEnum.createSiteProvisioningKey),
|
|
||||||
logActionAudit(ActionsEnum.createSiteProvisioningKey),
|
|
||||||
siteProvisioning.createSiteProvisioningKey
|
|
||||||
);
|
|
||||||
|
|
||||||
authenticated.get(
|
|
||||||
"/org/:orgId/site-provisioning-keys",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.siteProvisioningKeys),
|
|
||||||
verifyOrgAccess,
|
|
||||||
verifyUserHasAction(ActionsEnum.listSiteProvisioningKeys),
|
|
||||||
siteProvisioning.listSiteProvisioningKeys
|
|
||||||
);
|
|
||||||
|
|
||||||
authenticated.delete(
|
|
||||||
"/org/:orgId/site-provisioning-key/:siteProvisioningKeyId",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.siteProvisioningKeys),
|
|
||||||
verifyOrgAccess,
|
|
||||||
verifySiteProvisioningKeyAccess,
|
|
||||||
verifyUserHasAction(ActionsEnum.deleteSiteProvisioningKey),
|
|
||||||
logActionAudit(ActionsEnum.deleteSiteProvisioningKey),
|
|
||||||
siteProvisioning.deleteSiteProvisioningKey
|
|
||||||
);
|
|
||||||
|
|
||||||
authenticated.patch(
|
|
||||||
"/org/:orgId/site-provisioning-key/:siteProvisioningKeyId",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.siteProvisioningKeys),
|
|
||||||
verifyOrgAccess,
|
|
||||||
verifySiteProvisioningKeyAccess,
|
|
||||||
verifyUserHasAction(ActionsEnum.updateSiteProvisioningKey),
|
|
||||||
logActionAudit(ActionsEnum.updateSiteProvisioningKey),
|
|
||||||
siteProvisioning.updateSiteProvisioningKey
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -91,25 +91,6 @@ authenticated.get(
|
|||||||
logs.exportAccessAuditLogs
|
logs.exportAccessAuditLogs
|
||||||
);
|
);
|
||||||
|
|
||||||
authenticated.get(
|
|
||||||
"/org/:orgId/logs/connection",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.connectionLogs),
|
|
||||||
verifyApiKeyOrgAccess,
|
|
||||||
verifyApiKeyHasAction(ActionsEnum.exportLogs),
|
|
||||||
logs.queryConnectionAuditLogs
|
|
||||||
);
|
|
||||||
|
|
||||||
authenticated.get(
|
|
||||||
"/org/:orgId/logs/connection/export",
|
|
||||||
verifyValidLicense,
|
|
||||||
verifyValidSubscription(tierMatrix.logExport),
|
|
||||||
verifyApiKeyOrgAccess,
|
|
||||||
verifyApiKeyHasAction(ActionsEnum.exportLogs),
|
|
||||||
logActionAudit(ActionsEnum.exportLogs),
|
|
||||||
logs.exportConnectionAuditLogs
|
|
||||||
);
|
|
||||||
|
|
||||||
authenticated.put(
|
authenticated.put(
|
||||||
"/org/:orgId/idp/oidc",
|
"/org/:orgId/idp/oidc",
|
||||||
verifyValidLicense,
|
verifyValidLicense,
|
||||||
|
|||||||
@@ -1,394 +0,0 @@
|
|||||||
import { db, logsDb } from "@server/db";
|
|
||||||
import { MessageHandler } from "@server/routers/ws";
|
|
||||||
import { connectionAuditLog, sites, Newt, clients, orgs } from "@server/db";
|
|
||||||
import { and, eq, lt, inArray } from "drizzle-orm";
|
|
||||||
import logger from "@server/logger";
|
|
||||||
import { inflate } from "zlib";
|
|
||||||
import { promisify } from "util";
|
|
||||||
import { calculateCutoffTimestamp } from "@server/lib/cleanupLogs";
|
|
||||||
|
|
||||||
const zlibInflate = promisify(inflate);
|
|
||||||
|
|
||||||
// Retry configuration for deadlock handling
|
|
||||||
const MAX_RETRIES = 3;
|
|
||||||
const BASE_DELAY_MS = 50;
|
|
||||||
|
|
||||||
// How often to flush accumulated connection log data to the database
|
|
||||||
const FLUSH_INTERVAL_MS = 30_000; // 30 seconds
|
|
||||||
|
|
||||||
// Maximum number of records to buffer before forcing a flush
|
|
||||||
const MAX_BUFFERED_RECORDS = 500;
|
|
||||||
|
|
||||||
// Maximum number of records to insert in a single batch
|
|
||||||
const INSERT_BATCH_SIZE = 100;
|
|
||||||
|
|
||||||
interface ConnectionSessionData {
|
|
||||||
sessionId: string;
|
|
||||||
resourceId: number;
|
|
||||||
sourceAddr: string;
|
|
||||||
destAddr: string;
|
|
||||||
protocol: string;
|
|
||||||
startedAt: string; // ISO 8601 timestamp
|
|
||||||
endedAt?: string; // ISO 8601 timestamp
|
|
||||||
bytesTx?: number;
|
|
||||||
bytesRx?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ConnectionLogRecord {
|
|
||||||
sessionId: string;
|
|
||||||
siteResourceId: number;
|
|
||||||
orgId: string;
|
|
||||||
siteId: number;
|
|
||||||
clientId: number | null;
|
|
||||||
userId: string | null;
|
|
||||||
sourceAddr: string;
|
|
||||||
destAddr: string;
|
|
||||||
protocol: string;
|
|
||||||
startedAt: number; // epoch seconds
|
|
||||||
endedAt: number | null;
|
|
||||||
bytesTx: number | null;
|
|
||||||
bytesRx: number | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// In-memory buffer of records waiting to be flushed
|
|
||||||
let buffer: ConnectionLogRecord[] = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if an error is a deadlock error
|
|
||||||
*/
|
|
||||||
function isDeadlockError(error: any): boolean {
|
|
||||||
return (
|
|
||||||
error?.code === "40P01" ||
|
|
||||||
error?.cause?.code === "40P01" ||
|
|
||||||
(error?.message && error.message.includes("deadlock"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute a function with retry logic for deadlock handling
|
|
||||||
*/
|
|
||||||
async function withDeadlockRetry<T>(
|
|
||||||
operation: () => Promise<T>,
|
|
||||||
context: string
|
|
||||||
): Promise<T> {
|
|
||||||
let attempt = 0;
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
return await operation();
|
|
||||||
} catch (error: any) {
|
|
||||||
if (isDeadlockError(error) && attempt < MAX_RETRIES) {
|
|
||||||
attempt++;
|
|
||||||
const baseDelay = Math.pow(2, attempt - 1) * BASE_DELAY_MS;
|
|
||||||
const jitter = Math.random() * baseDelay;
|
|
||||||
const delay = baseDelay + jitter;
|
|
||||||
logger.warn(
|
|
||||||
`Deadlock detected in ${context}, retrying attempt ${attempt}/${MAX_RETRIES} after ${delay.toFixed(0)}ms`
|
|
||||||
);
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decompress a base64-encoded zlib-compressed string into parsed JSON.
|
|
||||||
*/
|
|
||||||
async function decompressConnectionLog(
|
|
||||||
compressed: string
|
|
||||||
): Promise<ConnectionSessionData[]> {
|
|
||||||
const compressedBuffer = Buffer.from(compressed, "base64");
|
|
||||||
const decompressed = await zlibInflate(compressedBuffer);
|
|
||||||
const jsonString = decompressed.toString("utf-8");
|
|
||||||
const parsed = JSON.parse(jsonString);
|
|
||||||
|
|
||||||
if (!Array.isArray(parsed)) {
|
|
||||||
throw new Error("Decompressed connection log data is not an array");
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert an ISO 8601 timestamp string to epoch seconds.
|
|
||||||
* Returns null if the input is falsy.
|
|
||||||
*/
|
|
||||||
function toEpochSeconds(isoString: string | undefined | null): number | null {
|
|
||||||
if (!isoString) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const ms = new Date(isoString).getTime();
|
|
||||||
if (isNaN(ms)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return Math.floor(ms / 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flush all buffered connection log records to the database.
|
|
||||||
*
|
|
||||||
* Swaps out the buffer before writing so that any records added during the
|
|
||||||
* flush are captured in the new buffer rather than being lost. Entries that
|
|
||||||
* fail to write are re-queued back into the buffer so they will be retried
|
|
||||||
* on the next flush.
|
|
||||||
*
|
|
||||||
* This function is exported so that the application's graceful-shutdown
|
|
||||||
* cleanup handler can call it before the process exits.
|
|
||||||
*/
|
|
||||||
export async function flushConnectionLogToDb(): Promise<void> {
|
|
||||||
if (buffer.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Atomically swap out the buffer so new data keeps flowing in
|
|
||||||
const snapshot = buffer;
|
|
||||||
buffer = [];
|
|
||||||
|
|
||||||
logger.debug(
|
|
||||||
`Flushing ${snapshot.length} connection log record(s) to the database`
|
|
||||||
);
|
|
||||||
|
|
||||||
// Insert in batches to avoid overly large SQL statements
|
|
||||||
for (let i = 0; i < snapshot.length; i += INSERT_BATCH_SIZE) {
|
|
||||||
const batch = snapshot.slice(i, i + INSERT_BATCH_SIZE);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await withDeadlockRetry(async () => {
|
|
||||||
await logsDb.insert(connectionAuditLog).values(batch);
|
|
||||||
}, `flush connection log batch (${batch.length} records)`);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(
|
|
||||||
`Failed to flush connection log batch of ${batch.length} records:`,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
|
|
||||||
// Re-queue the failed batch so it is retried on the next flush
|
|
||||||
buffer = [...batch, ...buffer];
|
|
||||||
|
|
||||||
// Cap buffer to prevent unbounded growth if DB is unreachable
|
|
||||||
if (buffer.length > MAX_BUFFERED_RECORDS * 5) {
|
|
||||||
const dropped = buffer.length - MAX_BUFFERED_RECORDS * 5;
|
|
||||||
buffer = buffer.slice(0, MAX_BUFFERED_RECORDS * 5);
|
|
||||||
logger.warn(
|
|
||||||
`Connection log buffer overflow, dropped ${dropped} oldest records`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop trying further batches from this snapshot — they'll be
|
|
||||||
// picked up by the next flush via the re-queued records above
|
|
||||||
const remaining = snapshot.slice(i + INSERT_BATCH_SIZE);
|
|
||||||
if (remaining.length > 0) {
|
|
||||||
buffer = [...remaining, ...buffer];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const flushTimer = setInterval(async () => {
|
|
||||||
try {
|
|
||||||
await flushConnectionLogToDb();
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(
|
|
||||||
"Unexpected error during periodic connection log flush:",
|
|
||||||
error
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}, FLUSH_INTERVAL_MS);
|
|
||||||
|
|
||||||
// Calling unref() means this timer will not keep the Node.js event loop alive
|
|
||||||
// on its own — the process can still exit normally when there is no other work
|
|
||||||
// left. The graceful-shutdown path will call flushConnectionLogToDb() explicitly
|
|
||||||
// before process.exit(), so no data is lost.
|
|
||||||
flushTimer.unref();
|
|
||||||
|
|
||||||
export async function cleanUpOldLogs(orgId: string, retentionDays: number) {
|
|
||||||
const cutoffTimestamp = calculateCutoffTimestamp(retentionDays);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await logsDb
|
|
||||||
.delete(connectionAuditLog)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
lt(connectionAuditLog.startedAt, cutoffTimestamp),
|
|
||||||
eq(connectionAuditLog.orgId, orgId)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// logger.debug(
|
|
||||||
// `Cleaned up connection audit logs older than ${retentionDays} days`
|
|
||||||
// );
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("Error cleaning up old connection audit logs:", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const handleConnectionLogMessage: MessageHandler = async (context) => {
|
|
||||||
const { message, client } = context;
|
|
||||||
const newt = client as Newt;
|
|
||||||
|
|
||||||
if (!newt) {
|
|
||||||
logger.warn("Connection log received but no newt client in context");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!newt.siteId) {
|
|
||||||
logger.warn("Connection log received but newt has no siteId");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!message.data?.compressed) {
|
|
||||||
logger.warn("Connection log message missing compressed data");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look up the org for this site
|
|
||||||
const [site] = await db
|
|
||||||
.select({ orgId: sites.orgId, orgSubnet: orgs.subnet })
|
|
||||||
.from(sites)
|
|
||||||
.innerJoin(orgs, eq(sites.orgId, orgs.orgId))
|
|
||||||
.where(eq(sites.siteId, newt.siteId));
|
|
||||||
|
|
||||||
if (!site) {
|
|
||||||
logger.warn(
|
|
||||||
`Connection log received but site ${newt.siteId} not found in database`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const orgId = site.orgId;
|
|
||||||
|
|
||||||
// Extract the CIDR suffix (e.g. "/16") from the org subnet so we can
|
|
||||||
// reconstruct the exact subnet string stored on each client record.
|
|
||||||
const cidrSuffix = site.orgSubnet?.includes("/")
|
|
||||||
? site.orgSubnet.substring(site.orgSubnet.indexOf("/"))
|
|
||||||
: null;
|
|
||||||
|
|
||||||
let sessions: ConnectionSessionData[];
|
|
||||||
try {
|
|
||||||
sessions = await decompressConnectionLog(message.data.compressed);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("Failed to decompress connection log data:", error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sessions.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`Sessions: ${JSON.stringify(sessions)}`)
|
|
||||||
|
|
||||||
// Build a map from sourceAddr → { clientId, userId } by querying clients
|
|
||||||
// whose subnet field matches exactly. Client subnets are stored with the
|
|
||||||
// org's CIDR suffix (e.g. "100.90.128.5/16"), so we reconstruct that from
|
|
||||||
// each unique sourceAddr + the org's CIDR suffix and do a targeted IN query.
|
|
||||||
const ipToClient = new Map<string, { clientId: number; userId: string | null }>();
|
|
||||||
|
|
||||||
if (cidrSuffix) {
|
|
||||||
// Collect unique source addresses so we only query for what we need
|
|
||||||
const uniqueSourceAddrs = new Set<string>();
|
|
||||||
for (const session of sessions) {
|
|
||||||
if (session.sourceAddr) {
|
|
||||||
uniqueSourceAddrs.add(session.sourceAddr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uniqueSourceAddrs.size > 0) {
|
|
||||||
// Construct the exact subnet strings as stored in the DB
|
|
||||||
const subnetQueries = Array.from(uniqueSourceAddrs).map(
|
|
||||||
(addr) => {
|
|
||||||
// Strip port if present (e.g. "100.90.128.1:38004" → "100.90.128.1")
|
|
||||||
const ip = addr.includes(":") ? addr.split(":")[0] : addr;
|
|
||||||
return `${ip}${cidrSuffix}`;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
logger.debug(`Subnet queries: ${JSON.stringify(subnetQueries)}`);
|
|
||||||
|
|
||||||
const matchedClients = await db
|
|
||||||
.select({
|
|
||||||
clientId: clients.clientId,
|
|
||||||
userId: clients.userId,
|
|
||||||
subnet: clients.subnet
|
|
||||||
})
|
|
||||||
.from(clients)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(clients.orgId, orgId),
|
|
||||||
inArray(clients.subnet, subnetQueries)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const c of matchedClients) {
|
|
||||||
const ip = c.subnet.split("/")[0];
|
|
||||||
logger.debug(`Client ${c.clientId} subnet ${c.subnet} matches ${ip}`);
|
|
||||||
ipToClient.set(ip, { clientId: c.clientId, userId: c.userId });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to DB records and add to the buffer
|
|
||||||
for (const session of sessions) {
|
|
||||||
// Validate required fields
|
|
||||||
if (
|
|
||||||
!session.sessionId ||
|
|
||||||
!session.resourceId ||
|
|
||||||
!session.sourceAddr ||
|
|
||||||
!session.destAddr ||
|
|
||||||
!session.protocol
|
|
||||||
) {
|
|
||||||
logger.debug(
|
|
||||||
`Skipping connection log session with missing required fields: ${JSON.stringify(session)}`
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const startedAt = toEpochSeconds(session.startedAt);
|
|
||||||
if (startedAt === null) {
|
|
||||||
logger.debug(
|
|
||||||
`Skipping connection log session with invalid startedAt: ${session.startedAt}`
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match the source address to a client. The sourceAddr is the
|
|
||||||
// client's IP on the WireGuard network, which corresponds to the IP
|
|
||||||
// portion of the client's subnet CIDR (e.g. "100.90.128.5/24").
|
|
||||||
// Strip port if present (e.g. "100.90.128.1:38004" → "100.90.128.1")
|
|
||||||
const sourceIp = session.sourceAddr.includes(":") ? session.sourceAddr.split(":")[0] : session.sourceAddr;
|
|
||||||
const clientInfo = ipToClient.get(sourceIp) ?? null;
|
|
||||||
|
|
||||||
|
|
||||||
buffer.push({
|
|
||||||
sessionId: session.sessionId,
|
|
||||||
siteResourceId: session.resourceId,
|
|
||||||
orgId,
|
|
||||||
siteId: newt.siteId,
|
|
||||||
clientId: clientInfo?.clientId ?? null,
|
|
||||||
userId: clientInfo?.userId ?? null,
|
|
||||||
sourceAddr: session.sourceAddr,
|
|
||||||
destAddr: session.destAddr,
|
|
||||||
protocol: session.protocol,
|
|
||||||
startedAt,
|
|
||||||
endedAt: toEpochSeconds(session.endedAt),
|
|
||||||
bytesTx: session.bytesTx ?? null,
|
|
||||||
bytesRx: session.bytesRx ?? null
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(
|
|
||||||
`Buffered ${sessions.length} connection log session(s) from newt ${newt.newtId} (site ${newt.siteId})`
|
|
||||||
);
|
|
||||||
|
|
||||||
// If the buffer has grown large enough, trigger an immediate flush
|
|
||||||
if (buffer.length >= MAX_BUFFERED_RECORDS) {
|
|
||||||
// Fire and forget — errors are handled inside flushConnectionLogToDb
|
|
||||||
flushConnectionLogToDb().catch((error) => {
|
|
||||||
logger.error(
|
|
||||||
"Unexpected error during size-triggered connection log flush:",
|
|
||||||
error
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export * from "./handleConnectionLogMessage";
|
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { NextFunction, Request, Response } from "express";
|
|
||||||
import { db, siteProvisioningKeyOrg, siteProvisioningKeys } from "@server/db";
|
|
||||||
import HttpCode from "@server/types/HttpCode";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { fromError } from "zod-validation-error";
|
|
||||||
import createHttpError from "http-errors";
|
|
||||||
import response from "@server/lib/response";
|
|
||||||
import moment from "moment";
|
|
||||||
import {
|
|
||||||
generateId,
|
|
||||||
generateIdFromEntropySize
|
|
||||||
} from "@server/auth/sessions/app";
|
|
||||||
import logger from "@server/logger";
|
|
||||||
import { hashPassword } from "@server/auth/password";
|
|
||||||
import type { CreateSiteProvisioningKeyResponse } from "@server/routers/siteProvisioning/types";
|
|
||||||
|
|
||||||
const paramsSchema = z.object({
|
|
||||||
orgId: z.string().nonempty()
|
|
||||||
});
|
|
||||||
|
|
||||||
const bodySchema = z
|
|
||||||
.strictObject({
|
|
||||||
name: z.string().min(1).max(255),
|
|
||||||
maxBatchSize: z.union([
|
|
||||||
z.null(),
|
|
||||||
z.coerce.number().int().positive().max(1_000_000)
|
|
||||||
]),
|
|
||||||
validUntil: z.string().max(255).optional()
|
|
||||||
})
|
|
||||||
.superRefine((data, ctx) => {
|
|
||||||
const v = data.validUntil;
|
|
||||||
if (v == null || v.trim() === "") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (Number.isNaN(Date.parse(v))) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
message: "Invalid validUntil",
|
|
||||||
path: ["validUntil"]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export type CreateSiteProvisioningKeyBody = z.infer<typeof bodySchema>;
|
|
||||||
|
|
||||||
export async function createSiteProvisioningKey(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<any> {
|
|
||||||
const parsedParams = paramsSchema.safeParse(req.params);
|
|
||||||
if (!parsedParams.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedParams.error).toString()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsedBody = bodySchema.safeParse(req.body);
|
|
||||||
if (!parsedBody.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedBody.error).toString()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { orgId } = parsedParams.data;
|
|
||||||
const { name, maxBatchSize } = parsedBody.data;
|
|
||||||
const vuRaw = parsedBody.data.validUntil;
|
|
||||||
const validUntil =
|
|
||||||
vuRaw == null || vuRaw.trim() === ""
|
|
||||||
? null
|
|
||||||
: new Date(Date.parse(vuRaw)).toISOString();
|
|
||||||
|
|
||||||
const siteProvisioningKeyId = `spk-${generateId(15)}`;
|
|
||||||
const siteProvisioningKey = generateIdFromEntropySize(25);
|
|
||||||
const siteProvisioningKeyHash = await hashPassword(siteProvisioningKey);
|
|
||||||
const lastChars = siteProvisioningKey.slice(-4);
|
|
||||||
const createdAt = moment().toISOString();
|
|
||||||
const provisioningKey = `${siteProvisioningKeyId}.${siteProvisioningKey}`;
|
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
|
||||||
await trx.insert(siteProvisioningKeys).values({
|
|
||||||
siteProvisioningKeyId,
|
|
||||||
name,
|
|
||||||
siteProvisioningKeyHash,
|
|
||||||
createdAt,
|
|
||||||
lastChars,
|
|
||||||
lastUsed: null,
|
|
||||||
maxBatchSize,
|
|
||||||
numUsed: 0,
|
|
||||||
validUntil
|
|
||||||
});
|
|
||||||
|
|
||||||
await trx.insert(siteProvisioningKeyOrg).values({
|
|
||||||
siteProvisioningKeyId,
|
|
||||||
orgId
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
|
||||||
return response<CreateSiteProvisioningKeyResponse>(res, {
|
|
||||||
data: {
|
|
||||||
siteProvisioningKeyId,
|
|
||||||
orgId,
|
|
||||||
name,
|
|
||||||
siteProvisioningKey: provisioningKey,
|
|
||||||
lastChars,
|
|
||||||
createdAt,
|
|
||||||
lastUsed: null,
|
|
||||||
maxBatchSize,
|
|
||||||
numUsed: 0,
|
|
||||||
validUntil
|
|
||||||
},
|
|
||||||
success: true,
|
|
||||||
error: false,
|
|
||||||
message: "Site provisioning key created",
|
|
||||||
status: HttpCode.CREATED
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
logger.error(e);
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.INTERNAL_SERVER_ERROR,
|
|
||||||
"Failed to create site provisioning key"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,129 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
import { z } from "zod";
|
|
||||||
import {
|
|
||||||
db,
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
siteProvisioningKeys
|
|
||||||
} from "@server/db";
|
|
||||||
import { and, 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";
|
|
||||||
|
|
||||||
const paramsSchema = z.object({
|
|
||||||
siteProvisioningKeyId: z.string().nonempty(),
|
|
||||||
orgId: z.string().nonempty()
|
|
||||||
});
|
|
||||||
|
|
||||||
export async function deleteSiteProvisioningKey(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<any> {
|
|
||||||
try {
|
|
||||||
const parsedParams = paramsSchema.safeParse(req.params);
|
|
||||||
if (!parsedParams.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedParams.error).toString()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { siteProvisioningKeyId, orgId } = parsedParams.data;
|
|
||||||
|
|
||||||
const [row] = await db
|
|
||||||
.select()
|
|
||||||
.from(siteProvisioningKeys)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.innerJoin(
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
and(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId
|
|
||||||
),
|
|
||||||
eq(siteProvisioningKeyOrg.orgId, orgId)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!row) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.NOT_FOUND,
|
|
||||||
`Site provisioning key with ID ${siteProvisioningKeyId} not found`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
|
||||||
await trx
|
|
||||||
.delete(siteProvisioningKeyOrg)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
),
|
|
||||||
eq(siteProvisioningKeyOrg.orgId, orgId)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const siteProvisioningKeyOrgs = await trx
|
|
||||||
.select()
|
|
||||||
.from(siteProvisioningKeyOrg)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (siteProvisioningKeyOrgs.length === 0) {
|
|
||||||
await trx
|
|
||||||
.delete(siteProvisioningKeys)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return response(res, {
|
|
||||||
data: null,
|
|
||||||
success: true,
|
|
||||||
error: false,
|
|
||||||
message: "Site provisioning key deleted successfully",
|
|
||||||
status: HttpCode.OK
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error);
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
|
|
||||||
export * from "./createSiteProvisioningKey";
|
|
||||||
export * from "./listSiteProvisioningKeys";
|
|
||||||
export * from "./deleteSiteProvisioningKey";
|
|
||||||
export * from "./updateSiteProvisioningKey";
|
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {
|
|
||||||
db,
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
siteProvisioningKeys
|
|
||||||
} from "@server/db";
|
|
||||||
import logger from "@server/logger";
|
|
||||||
import HttpCode from "@server/types/HttpCode";
|
|
||||||
import response from "@server/lib/response";
|
|
||||||
import { NextFunction, Request, Response } from "express";
|
|
||||||
import createHttpError from "http-errors";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { fromError } from "zod-validation-error";
|
|
||||||
import { eq } from "drizzle-orm";
|
|
||||||
import type { ListSiteProvisioningKeysResponse } from "@server/routers/siteProvisioning/types";
|
|
||||||
|
|
||||||
const paramsSchema = z.object({
|
|
||||||
orgId: z.string().nonempty()
|
|
||||||
});
|
|
||||||
|
|
||||||
const querySchema = z.object({
|
|
||||||
limit: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.default("1000")
|
|
||||||
.transform(Number)
|
|
||||||
.pipe(z.int().positive()),
|
|
||||||
offset: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.default("0")
|
|
||||||
.transform(Number)
|
|
||||||
.pipe(z.int().nonnegative())
|
|
||||||
});
|
|
||||||
|
|
||||||
function querySiteProvisioningKeys(orgId: string) {
|
|
||||||
return db
|
|
||||||
.select({
|
|
||||||
siteProvisioningKeyId:
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
orgId: siteProvisioningKeyOrg.orgId,
|
|
||||||
lastChars: siteProvisioningKeys.lastChars,
|
|
||||||
createdAt: siteProvisioningKeys.createdAt,
|
|
||||||
name: siteProvisioningKeys.name,
|
|
||||||
lastUsed: siteProvisioningKeys.lastUsed,
|
|
||||||
maxBatchSize: siteProvisioningKeys.maxBatchSize,
|
|
||||||
numUsed: siteProvisioningKeys.numUsed,
|
|
||||||
validUntil: siteProvisioningKeys.validUntil
|
|
||||||
})
|
|
||||||
.from(siteProvisioningKeyOrg)
|
|
||||||
.innerJoin(
|
|
||||||
siteProvisioningKeys,
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.where(eq(siteProvisioningKeyOrg.orgId, orgId));
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function listSiteProvisioningKeys(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<any> {
|
|
||||||
try {
|
|
||||||
const parsedParams = paramsSchema.safeParse(req.params);
|
|
||||||
if (!parsedParams.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedParams.error)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsedQuery = querySchema.safeParse(req.query);
|
|
||||||
if (!parsedQuery.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedQuery.error)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { orgId } = parsedParams.data;
|
|
||||||
const { limit, offset } = parsedQuery.data;
|
|
||||||
|
|
||||||
const siteProvisioningKeysList = await querySiteProvisioningKeys(orgId)
|
|
||||||
.limit(limit)
|
|
||||||
.offset(offset);
|
|
||||||
|
|
||||||
return response<ListSiteProvisioningKeysResponse>(res, {
|
|
||||||
data: {
|
|
||||||
siteProvisioningKeys: siteProvisioningKeysList,
|
|
||||||
pagination: {
|
|
||||||
total: siteProvisioningKeysList.length,
|
|
||||||
limit,
|
|
||||||
offset
|
|
||||||
}
|
|
||||||
},
|
|
||||||
success: true,
|
|
||||||
error: false,
|
|
||||||
message: "Site provisioning keys retrieved successfully",
|
|
||||||
status: HttpCode.OK
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error);
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of a proprietary work.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2025 Fossorial, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is licensed under the Fossorial Commercial License.
|
|
||||||
* You may not use this file except in compliance with the License.
|
|
||||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
|
||||||
*
|
|
||||||
* This file is not licensed under the AGPLv3.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
import { z } from "zod";
|
|
||||||
import {
|
|
||||||
db,
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
siteProvisioningKeys
|
|
||||||
} from "@server/db";
|
|
||||||
import { and, 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 type { UpdateSiteProvisioningKeyResponse } from "@server/routers/siteProvisioning/types";
|
|
||||||
|
|
||||||
const paramsSchema = z.object({
|
|
||||||
siteProvisioningKeyId: z.string().nonempty(),
|
|
||||||
orgId: z.string().nonempty()
|
|
||||||
});
|
|
||||||
|
|
||||||
const bodySchema = z
|
|
||||||
.strictObject({
|
|
||||||
maxBatchSize: z
|
|
||||||
.union([
|
|
||||||
z.null(),
|
|
||||||
z.coerce.number().int().positive().max(1_000_000)
|
|
||||||
])
|
|
||||||
.optional(),
|
|
||||||
validUntil: z.string().max(255).optional()
|
|
||||||
})
|
|
||||||
.superRefine((data, ctx) => {
|
|
||||||
if (
|
|
||||||
data.maxBatchSize === undefined &&
|
|
||||||
data.validUntil === undefined
|
|
||||||
) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
message: "Provide maxBatchSize and/or validUntil",
|
|
||||||
path: ["maxBatchSize"]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const v = data.validUntil;
|
|
||||||
if (v == null || v.trim() === "") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (Number.isNaN(Date.parse(v))) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
message: "Invalid validUntil",
|
|
||||||
path: ["validUntil"]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export type UpdateSiteProvisioningKeyBody = z.infer<typeof bodySchema>;
|
|
||||||
|
|
||||||
export async function updateSiteProvisioningKey(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<any> {
|
|
||||||
try {
|
|
||||||
const parsedParams = paramsSchema.safeParse(req.params);
|
|
||||||
if (!parsedParams.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedParams.error).toString()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsedBody = bodySchema.safeParse(req.body);
|
|
||||||
if (!parsedBody.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedBody.error).toString()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { siteProvisioningKeyId, orgId } = parsedParams.data;
|
|
||||||
const body = parsedBody.data;
|
|
||||||
|
|
||||||
const [row] = await db
|
|
||||||
.select()
|
|
||||||
.from(siteProvisioningKeys)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.innerJoin(
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
and(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId
|
|
||||||
),
|
|
||||||
eq(siteProvisioningKeyOrg.orgId, orgId)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!row) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.NOT_FOUND,
|
|
||||||
`Site provisioning key with ID ${siteProvisioningKeyId} not found`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const setValues: {
|
|
||||||
maxBatchSize?: number | null;
|
|
||||||
validUntil?: string | null;
|
|
||||||
} = {};
|
|
||||||
if (body.maxBatchSize !== undefined) {
|
|
||||||
setValues.maxBatchSize = body.maxBatchSize;
|
|
||||||
}
|
|
||||||
if (body.validUntil !== undefined) {
|
|
||||||
setValues.validUntil =
|
|
||||||
body.validUntil.trim() === ""
|
|
||||||
? null
|
|
||||||
: new Date(Date.parse(body.validUntil)).toISOString();
|
|
||||||
}
|
|
||||||
|
|
||||||
await db
|
|
||||||
.update(siteProvisioningKeys)
|
|
||||||
.set(setValues)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const [updated] = await db
|
|
||||||
.select({
|
|
||||||
siteProvisioningKeyId:
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
name: siteProvisioningKeys.name,
|
|
||||||
lastChars: siteProvisioningKeys.lastChars,
|
|
||||||
createdAt: siteProvisioningKeys.createdAt,
|
|
||||||
lastUsed: siteProvisioningKeys.lastUsed,
|
|
||||||
maxBatchSize: siteProvisioningKeys.maxBatchSize,
|
|
||||||
numUsed: siteProvisioningKeys.numUsed,
|
|
||||||
validUntil: siteProvisioningKeys.validUntil
|
|
||||||
})
|
|
||||||
.from(siteProvisioningKeys)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!updated) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.INTERNAL_SERVER_ERROR,
|
|
||||||
"Failed to load updated site provisioning key"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response<UpdateSiteProvisioningKeyResponse>(res, {
|
|
||||||
data: {
|
|
||||||
...updated,
|
|
||||||
orgId
|
|
||||||
},
|
|
||||||
success: true,
|
|
||||||
error: false,
|
|
||||||
message: "Site provisioning key updated successfully",
|
|
||||||
status: HttpCode.OK
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error);
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -24,7 +24,6 @@ import {
|
|||||||
sites,
|
sites,
|
||||||
userOrgs
|
userOrgs
|
||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
import { logAccessAudit } from "#private/lib/logAccessAudit";
|
|
||||||
import { isLicensedOrSubscribed } from "#private/lib/isLicencedOrSubscribed";
|
import { isLicensedOrSubscribed } from "#private/lib/isLicencedOrSubscribed";
|
||||||
import { tierMatrix } from "@server/lib/billing/tierMatrix";
|
import { tierMatrix } from "@server/lib/billing/tierMatrix";
|
||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
@@ -464,24 +463,6 @@ export async function signSshKey(
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
await logAccessAudit({
|
|
||||||
action: true,
|
|
||||||
type: "ssh",
|
|
||||||
orgId: orgId,
|
|
||||||
resourceId: resource.siteResourceId,
|
|
||||||
user: req.user
|
|
||||||
? { username: req.user.username ?? "", userId: req.user.userId }
|
|
||||||
: undefined,
|
|
||||||
metadata: {
|
|
||||||
resourceName: resource.name,
|
|
||||||
siteId: resource.siteId,
|
|
||||||
sshUsername: usernameToUse,
|
|
||||||
sshHost: sshHost
|
|
||||||
},
|
|
||||||
userAgent: req.headers["user-agent"],
|
|
||||||
requestIp: req.ip
|
|
||||||
});
|
|
||||||
|
|
||||||
return response<SignSshKeyResponse>(res, {
|
return response<SignSshKeyResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
certificate: cert.certificate,
|
certificate: cert.certificate,
|
||||||
|
|||||||
@@ -18,12 +18,10 @@ import {
|
|||||||
} from "#private/routers/remoteExitNode";
|
} from "#private/routers/remoteExitNode";
|
||||||
import { MessageHandler } from "@server/routers/ws";
|
import { MessageHandler } from "@server/routers/ws";
|
||||||
import { build } from "@server/build";
|
import { build } from "@server/build";
|
||||||
import { handleConnectionLogMessage } from "#dynamic/routers/newt";
|
|
||||||
|
|
||||||
export const messageHandlers: Record<string, MessageHandler> = {
|
export const messageHandlers: Record<string, MessageHandler> = {
|
||||||
"remoteExitNode/register": handleRemoteExitNodeRegisterMessage,
|
"remoteExitNode/register": handleRemoteExitNodeRegisterMessage,
|
||||||
"remoteExitNode/ping": handleRemoteExitNodePingMessage,
|
"remoteExitNode/ping": handleRemoteExitNodePingMessage
|
||||||
"newt/access-log": handleConnectionLogMessage,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (build != "saas") {
|
if (build != "saas") {
|
||||||
|
|||||||
@@ -91,50 +91,3 @@ export type QueryAccessAuditLogResponse = {
|
|||||||
locations: string[];
|
locations: string[];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export type QueryConnectionAuditLogResponse = {
|
|
||||||
log: {
|
|
||||||
sessionId: string;
|
|
||||||
siteResourceId: number | null;
|
|
||||||
orgId: string | null;
|
|
||||||
siteId: number | null;
|
|
||||||
clientId: number | null;
|
|
||||||
userId: string | null;
|
|
||||||
sourceAddr: string;
|
|
||||||
destAddr: string;
|
|
||||||
protocol: string;
|
|
||||||
startedAt: number;
|
|
||||||
endedAt: number | null;
|
|
||||||
bytesTx: number | null;
|
|
||||||
bytesRx: number | null;
|
|
||||||
resourceName: string | null;
|
|
||||||
resourceNiceId: string | null;
|
|
||||||
siteName: string | null;
|
|
||||||
siteNiceId: string | null;
|
|
||||||
clientName: string | null;
|
|
||||||
clientNiceId: string | null;
|
|
||||||
clientType: string | null;
|
|
||||||
userEmail: string | null;
|
|
||||||
}[];
|
|
||||||
pagination: {
|
|
||||||
total: number;
|
|
||||||
limit: number;
|
|
||||||
offset: number;
|
|
||||||
};
|
|
||||||
filterAttributes: {
|
|
||||||
protocols: string[];
|
|
||||||
destAddrs: string[];
|
|
||||||
clients: {
|
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
}[];
|
|
||||||
resources: {
|
|
||||||
id: number;
|
|
||||||
name: string | null;
|
|
||||||
}[];
|
|
||||||
users: {
|
|
||||||
id: string;
|
|
||||||
email: string | null;
|
|
||||||
}[];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,54 +1,15 @@
|
|||||||
import { sendToClient } from "#dynamic/routers/ws";
|
import { sendToClient } from "#dynamic/routers/ws";
|
||||||
import { db, newts, olms } from "@server/db";
|
import { db, olms, Transaction } from "@server/db";
|
||||||
import {
|
|
||||||
Alias,
|
|
||||||
convertSubnetProxyTargetsV2ToV1,
|
|
||||||
SubnetProxyTarget,
|
|
||||||
SubnetProxyTargetV2
|
|
||||||
} from "@server/lib/ip";
|
|
||||||
import { canCompress } from "@server/lib/clientVersionChecks";
|
import { canCompress } from "@server/lib/clientVersionChecks";
|
||||||
|
import { Alias, SubnetProxyTarget } from "@server/lib/ip";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import semver from "semver";
|
|
||||||
|
|
||||||
const NEWT_V2_TARGETS_VERSION = ">=1.10.3";
|
|
||||||
|
|
||||||
export async function convertTargetsIfNessicary(
|
|
||||||
newtId: string,
|
|
||||||
targets: SubnetProxyTarget[] | SubnetProxyTargetV2[]
|
|
||||||
) {
|
|
||||||
// get the newt
|
|
||||||
const [newt] = await db
|
|
||||||
.select()
|
|
||||||
.from(newts)
|
|
||||||
.where(eq(newts.newtId, newtId));
|
|
||||||
if (!newt) {
|
|
||||||
throw new Error(`No newt found for id: ${newtId}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check the semver
|
|
||||||
if (
|
|
||||||
newt.version &&
|
|
||||||
!semver.satisfies(newt.version, NEWT_V2_TARGETS_VERSION)
|
|
||||||
) {
|
|
||||||
logger.debug(
|
|
||||||
`addTargets Newt version ${newt.version} does not support targets v2 falling back`
|
|
||||||
);
|
|
||||||
targets = convertSubnetProxyTargetsV2ToV1(
|
|
||||||
targets as SubnetProxyTargetV2[]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return targets;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function addTargets(
|
export async function addTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: SubnetProxyTarget[] | SubnetProxyTargetV2[],
|
targets: SubnetProxyTarget[],
|
||||||
version?: string | null
|
version?: string | null
|
||||||
) {
|
) {
|
||||||
targets = await convertTargetsIfNessicary(newtId, targets);
|
|
||||||
|
|
||||||
await sendToClient(
|
await sendToClient(
|
||||||
newtId,
|
newtId,
|
||||||
{
|
{
|
||||||
@@ -61,11 +22,9 @@ export async function addTargets(
|
|||||||
|
|
||||||
export async function removeTargets(
|
export async function removeTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: SubnetProxyTarget[] | SubnetProxyTargetV2[],
|
targets: SubnetProxyTarget[],
|
||||||
version?: string | null
|
version?: string | null
|
||||||
) {
|
) {
|
||||||
targets = await convertTargetsIfNessicary(newtId, targets);
|
|
||||||
|
|
||||||
await sendToClient(
|
await sendToClient(
|
||||||
newtId,
|
newtId,
|
||||||
{
|
{
|
||||||
@@ -79,39 +38,11 @@ export async function removeTargets(
|
|||||||
export async function updateTargets(
|
export async function updateTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: {
|
targets: {
|
||||||
oldTargets: SubnetProxyTarget[] | SubnetProxyTargetV2[];
|
oldTargets: SubnetProxyTarget[];
|
||||||
newTargets: SubnetProxyTarget[] | SubnetProxyTargetV2[];
|
newTargets: SubnetProxyTarget[];
|
||||||
},
|
},
|
||||||
version?: string | null
|
version?: string | null
|
||||||
) {
|
) {
|
||||||
// get the newt
|
|
||||||
const [newt] = await db
|
|
||||||
.select()
|
|
||||||
.from(newts)
|
|
||||||
.where(eq(newts.newtId, newtId));
|
|
||||||
if (!newt) {
|
|
||||||
logger.error(`addTargetsL No newt found for id: ${newtId}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check the semver
|
|
||||||
if (
|
|
||||||
newt.version &&
|
|
||||||
!semver.satisfies(newt.version, NEWT_V2_TARGETS_VERSION)
|
|
||||||
) {
|
|
||||||
logger.debug(
|
|
||||||
`addTargets Newt version ${newt.version} does not support targets v2 falling back`
|
|
||||||
);
|
|
||||||
targets = {
|
|
||||||
oldTargets: convertSubnetProxyTargetsV2ToV1(
|
|
||||||
targets.oldTargets as SubnetProxyTargetV2[]
|
|
||||||
),
|
|
||||||
newTargets: convertSubnetProxyTargetsV2ToV1(
|
|
||||||
targets.newTargets as SubnetProxyTargetV2[]
|
|
||||||
)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
await sendToClient(
|
await sendToClient(
|
||||||
newtId,
|
newtId,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -102,8 +102,6 @@ authenticated.put(
|
|||||||
logActionAudit(ActionsEnum.createSite),
|
logActionAudit(ActionsEnum.createSite),
|
||||||
site.createSite
|
site.createSite
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
authenticated.get(
|
authenticated.get(
|
||||||
"/org/:orgId/sites",
|
"/org/:orgId/sites",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
@@ -1204,22 +1202,6 @@ authRouter.post(
|
|||||||
}),
|
}),
|
||||||
newt.getNewtToken
|
newt.getNewtToken
|
||||||
);
|
);
|
||||||
|
|
||||||
authRouter.post(
|
|
||||||
"/newt/register",
|
|
||||||
rateLimit({
|
|
||||||
windowMs: 15 * 60 * 1000,
|
|
||||||
max: 30,
|
|
||||||
keyGenerator: (req) =>
|
|
||||||
`newtRegister:${req.body.provisioningKey?.split(".")[0] || ipKeyGenerator(req.ip || "")}`,
|
|
||||||
handler: (req, res, next) => {
|
|
||||||
const message = `You can only register a newt ${30} times every ${15} minutes. Please try again later.`;
|
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
|
||||||
},
|
|
||||||
store: createStore()
|
|
||||||
}),
|
|
||||||
newt.registerNewt
|
|
||||||
);
|
|
||||||
authRouter.post(
|
authRouter.post(
|
||||||
"/olm/get-token",
|
"/olm/get-token",
|
||||||
rateLimit({
|
rateLimit({
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import { eq, and } from "drizzle-orm";
|
|||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import {
|
import {
|
||||||
formatEndpoint,
|
formatEndpoint,
|
||||||
generateSubnetProxyTargetV2,
|
generateSubnetProxyTargets,
|
||||||
SubnetProxyTargetV2
|
SubnetProxyTarget
|
||||||
} from "@server/lib/ip";
|
} from "@server/lib/ip";
|
||||||
|
|
||||||
export async function buildClientConfigurationForNewtClient(
|
export async function buildClientConfigurationForNewtClient(
|
||||||
@@ -143,7 +143,7 @@ export async function buildClientConfigurationForNewtClient(
|
|||||||
.from(siteResources)
|
.from(siteResources)
|
||||||
.where(eq(siteResources.siteId, siteId));
|
.where(eq(siteResources.siteId, siteId));
|
||||||
|
|
||||||
const targetsToSend: SubnetProxyTargetV2[] = [];
|
const targetsToSend: SubnetProxyTarget[] = [];
|
||||||
|
|
||||||
for (const resource of allSiteResources) {
|
for (const resource of allSiteResources) {
|
||||||
// Get clients associated with this specific resource
|
// Get clients associated with this specific resource
|
||||||
@@ -168,14 +168,12 @@ export async function buildClientConfigurationForNewtClient(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
const resourceTarget = generateSubnetProxyTargetV2(
|
const resourceTargets = generateSubnetProxyTargets(
|
||||||
resource,
|
resource,
|
||||||
resourceClients
|
resourceClients
|
||||||
);
|
);
|
||||||
|
|
||||||
if (resourceTarget) {
|
targetsToSend.push(...resourceTargets);
|
||||||
targetsToSend.push(resourceTarget);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
import { MessageHandler } from "@server/routers/ws";
|
|
||||||
|
|
||||||
export async function flushConnectionLogToDb(): Promise<void> {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cleanUpOldLogs(orgId: string, retentionDays: number) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const handleConnectionLogMessage: MessageHandler = async (context) => {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
@@ -6,7 +6,6 @@ import { db, ExitNode, exitNodes, Newt, sites } from "@server/db";
|
|||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { sendToExitNode } from "#dynamic/lib/exitNodes";
|
import { sendToExitNode } from "#dynamic/lib/exitNodes";
|
||||||
import { buildClientConfigurationForNewtClient } from "./buildConfiguration";
|
import { buildClientConfigurationForNewtClient } from "./buildConfiguration";
|
||||||
import { convertTargetsIfNessicary } from "../client/targets";
|
|
||||||
import { canCompress } from "@server/lib/clientVersionChecks";
|
import { canCompress } from "@server/lib/clientVersionChecks";
|
||||||
|
|
||||||
const inputSchema = z.object({
|
const inputSchema = z.object({
|
||||||
@@ -128,15 +127,13 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
|
|||||||
exitNode
|
exitNode
|
||||||
);
|
);
|
||||||
|
|
||||||
const targetsToSend = await convertTargetsIfNessicary(newt.newtId, targets);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
message: {
|
message: {
|
||||||
type: "newt/wg/receive-config",
|
type: "newt/wg/receive-config",
|
||||||
data: {
|
data: {
|
||||||
ipAddress: site.address,
|
ipAddress: site.address,
|
||||||
peers,
|
peers,
|
||||||
targets: targetsToSend
|
targets
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
|
|||||||
@@ -8,5 +8,3 @@ export * from "./handleNewtPingRequestMessage";
|
|||||||
export * from "./handleApplyBlueprintMessage";
|
export * from "./handleApplyBlueprintMessage";
|
||||||
export * from "./handleNewtPingMessage";
|
export * from "./handleNewtPingMessage";
|
||||||
export * from "./handleNewtDisconnectingMessage";
|
export * from "./handleNewtDisconnectingMessage";
|
||||||
export * from "./handleConnectionLogMessage";
|
|
||||||
export * from "./registerNewt";
|
|
||||||
|
|||||||
@@ -1,245 +0,0 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { db } from "@server/db";
|
|
||||||
import {
|
|
||||||
siteProvisioningKeys,
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
newts,
|
|
||||||
orgs,
|
|
||||||
roles,
|
|
||||||
roleSites,
|
|
||||||
sites
|
|
||||||
} from "@server/db";
|
|
||||||
import response from "@server/lib/response";
|
|
||||||
import HttpCode from "@server/types/HttpCode";
|
|
||||||
import createHttpError from "http-errors";
|
|
||||||
import logger from "@server/logger";
|
|
||||||
import { eq, and } from "drizzle-orm";
|
|
||||||
import { fromError } from "zod-validation-error";
|
|
||||||
import { verifyPassword, hashPassword } from "@server/auth/password";
|
|
||||||
import {
|
|
||||||
generateId,
|
|
||||||
generateIdFromEntropySize
|
|
||||||
} from "@server/auth/sessions/app";
|
|
||||||
import { getUniqueSiteName } from "@server/db/names";
|
|
||||||
import moment from "moment";
|
|
||||||
import { build } from "@server/build";
|
|
||||||
import { usageService } from "@server/lib/billing/usageService";
|
|
||||||
import { FeatureId } from "@server/lib/billing";
|
|
||||||
|
|
||||||
const bodySchema = z.object({
|
|
||||||
provisioningKey: z.string().nonempty()
|
|
||||||
});
|
|
||||||
|
|
||||||
export type RegisterNewtBody = z.infer<typeof bodySchema>;
|
|
||||||
|
|
||||||
export type RegisterNewtResponse = {
|
|
||||||
newtId: string;
|
|
||||||
secret: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function registerNewt(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<any> {
|
|
||||||
try {
|
|
||||||
const parsedBody = bodySchema.safeParse(req.body);
|
|
||||||
if (!parsedBody.success) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
fromError(parsedBody.error).toString()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { provisioningKey } = parsedBody.data;
|
|
||||||
|
|
||||||
// Keys are in the format "siteProvisioningKeyId.secret"
|
|
||||||
const dotIndex = provisioningKey.indexOf(".");
|
|
||||||
if (dotIndex === -1) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid provisioning key format"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const provisioningKeyId = provisioningKey.substring(0, dotIndex);
|
|
||||||
const provisioningKeySecret = provisioningKey.substring(dotIndex + 1);
|
|
||||||
|
|
||||||
// Look up the provisioning key by ID, joining to get the orgId
|
|
||||||
const [keyRecord] = await db
|
|
||||||
.select({
|
|
||||||
siteProvisioningKeyId:
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyHash:
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyHash,
|
|
||||||
orgId: siteProvisioningKeyOrg.orgId
|
|
||||||
})
|
|
||||||
.from(siteProvisioningKeys)
|
|
||||||
.innerJoin(
|
|
||||||
siteProvisioningKeyOrg,
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
siteProvisioningKeyOrg.siteProvisioningKeyId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
provisioningKeyId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!keyRecord) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.UNAUTHORIZED,
|
|
||||||
"Invalid provisioning key"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify the secret portion against the stored hash
|
|
||||||
const validSecret = await verifyPassword(
|
|
||||||
provisioningKeySecret,
|
|
||||||
keyRecord.siteProvisioningKeyHash
|
|
||||||
);
|
|
||||||
if (!validSecret) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.UNAUTHORIZED,
|
|
||||||
"Invalid provisioning key"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { orgId } = keyRecord;
|
|
||||||
|
|
||||||
// Verify the org exists
|
|
||||||
const [org] = await db
|
|
||||||
.select()
|
|
||||||
.from(orgs)
|
|
||||||
.where(eq(orgs.orgId, orgId));
|
|
||||||
if (!org) {
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.NOT_FOUND, "Organization not found")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// SaaS billing check
|
|
||||||
if (build == "saas") {
|
|
||||||
const usage = await usageService.getUsage(orgId, FeatureId.SITES);
|
|
||||||
if (!usage) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.NOT_FOUND,
|
|
||||||
"No usage data found for this organization"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const rejectSites = await usageService.checkLimitSet(
|
|
||||||
orgId,
|
|
||||||
FeatureId.SITES,
|
|
||||||
{
|
|
||||||
...usage,
|
|
||||||
instantaneousValue: (usage.instantaneousValue || 0) + 1
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (rejectSites) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.FORBIDDEN,
|
|
||||||
"Site limit exceeded. Please upgrade your plan."
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const niceId = await getUniqueSiteName(orgId);
|
|
||||||
const newtId = generateId(15);
|
|
||||||
const newtSecret = generateIdFromEntropySize(25);
|
|
||||||
const secretHash = await hashPassword(newtSecret);
|
|
||||||
|
|
||||||
let newSiteId: number | undefined;
|
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
|
||||||
// Create the site (type "newt", name = niceId)
|
|
||||||
const [newSite] = await trx
|
|
||||||
.insert(sites)
|
|
||||||
.values({
|
|
||||||
orgId,
|
|
||||||
name: niceId,
|
|
||||||
niceId,
|
|
||||||
type: "newt",
|
|
||||||
dockerSocketEnabled: true
|
|
||||||
})
|
|
||||||
.returning();
|
|
||||||
|
|
||||||
newSiteId = newSite.siteId;
|
|
||||||
|
|
||||||
// Grant admin role access to the new site
|
|
||||||
const [adminRole] = await trx
|
|
||||||
.select()
|
|
||||||
.from(roles)
|
|
||||||
.where(and(eq(roles.isAdmin, true), eq(roles.orgId, orgId)))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!adminRole) {
|
|
||||||
throw new Error(`Admin role not found for org ${orgId}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
await trx.insert(roleSites).values({
|
|
||||||
roleId: adminRole.roleId,
|
|
||||||
siteId: newSite.siteId
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create the newt for this site
|
|
||||||
await trx.insert(newts).values({
|
|
||||||
newtId,
|
|
||||||
secretHash,
|
|
||||||
siteId: newSite.siteId,
|
|
||||||
dateCreated: moment().toISOString()
|
|
||||||
});
|
|
||||||
|
|
||||||
// Consume the provisioning key — cascade removes siteProvisioningKeyOrg
|
|
||||||
await trx
|
|
||||||
.delete(siteProvisioningKeys)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
siteProvisioningKeys.siteProvisioningKeyId,
|
|
||||||
provisioningKeyId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
await usageService.add(orgId, FeatureId.SITES, 1, trx);
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.info(
|
|
||||||
`Provisioned new site (ID: ${newSiteId}) and newt (ID: ${newtId}) for org ${orgId} via provisioning key ${provisioningKeyId}`
|
|
||||||
);
|
|
||||||
|
|
||||||
return response<RegisterNewtResponse>(res, {
|
|
||||||
data: {
|
|
||||||
newtId,
|
|
||||||
secret: newtSecret
|
|
||||||
},
|
|
||||||
success: true,
|
|
||||||
error: false,
|
|
||||||
message: "Newt registered successfully",
|
|
||||||
status: HttpCode.CREATED
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error);
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.INTERNAL_SERVER_ERROR,
|
|
||||||
"An error occurred"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
export type SiteProvisioningKeyListItem = {
|
|
||||||
siteProvisioningKeyId: string;
|
|
||||||
orgId: string;
|
|
||||||
lastChars: string;
|
|
||||||
createdAt: string;
|
|
||||||
name: string;
|
|
||||||
lastUsed: string | null;
|
|
||||||
maxBatchSize: number | null;
|
|
||||||
numUsed: number;
|
|
||||||
validUntil: string | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type ListSiteProvisioningKeysResponse = {
|
|
||||||
siteProvisioningKeys: SiteProvisioningKeyListItem[];
|
|
||||||
pagination: { total: number; limit: number; offset: number };
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CreateSiteProvisioningKeyResponse = {
|
|
||||||
siteProvisioningKeyId: string;
|
|
||||||
orgId: string;
|
|
||||||
name: string;
|
|
||||||
siteProvisioningKey: string;
|
|
||||||
lastChars: string;
|
|
||||||
createdAt: string;
|
|
||||||
lastUsed: string | null;
|
|
||||||
maxBatchSize: number | null;
|
|
||||||
numUsed: number;
|
|
||||||
validUntil: string | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type UpdateSiteProvisioningKeyResponse = {
|
|
||||||
siteProvisioningKeyId: string;
|
|
||||||
orgId: string;
|
|
||||||
name: string;
|
|
||||||
lastChars: string;
|
|
||||||
createdAt: string;
|
|
||||||
lastUsed: string | null;
|
|
||||||
maxBatchSize: number | null;
|
|
||||||
numUsed: number;
|
|
||||||
validUntil: string | null;
|
|
||||||
};
|
|
||||||
@@ -88,7 +88,7 @@ const createSiteResourceSchema = z
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
message:
|
message:
|
||||||
"Destination must be a valid IPV4 address or valid domain AND alias is required"
|
"Destination must be a valid IP address or valid domain AND alias is required"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.refine(
|
.refine(
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import { updatePeerData, updateTargets } from "@server/routers/client/targets";
|
|||||||
import {
|
import {
|
||||||
generateAliasConfig,
|
generateAliasConfig,
|
||||||
generateRemoteSubnets,
|
generateRemoteSubnets,
|
||||||
generateSubnetProxyTargetV2,
|
generateSubnetProxyTargets,
|
||||||
isIpInCidr,
|
isIpInCidr,
|
||||||
portRangeStringSchema
|
portRangeStringSchema
|
||||||
} from "@server/lib/ip";
|
} from "@server/lib/ip";
|
||||||
@@ -608,18 +608,18 @@ export async function handleMessagingForUpdatedSiteResource(
|
|||||||
|
|
||||||
// Only update targets on newt if destination changed
|
// Only update targets on newt if destination changed
|
||||||
if (destinationChanged || portRangesChanged) {
|
if (destinationChanged || portRangesChanged) {
|
||||||
const oldTarget = generateSubnetProxyTargetV2(
|
const oldTargets = generateSubnetProxyTargets(
|
||||||
existingSiteResource,
|
existingSiteResource,
|
||||||
mergedAllClients
|
mergedAllClients
|
||||||
);
|
);
|
||||||
const newTarget = generateSubnetProxyTargetV2(
|
const newTargets = generateSubnetProxyTargets(
|
||||||
updatedSiteResource,
|
updatedSiteResource,
|
||||||
mergedAllClients
|
mergedAllClients
|
||||||
);
|
);
|
||||||
|
|
||||||
await updateTargets(newt.newtId, {
|
await updateTargets(newt.newtId, {
|
||||||
oldTargets: oldTarget ? [oldTarget] : [],
|
oldTargets: oldTargets,
|
||||||
newTargets: newTarget ? [newTarget] : []
|
newTargets: newTargets
|
||||||
}, newt.version);
|
}, newt.version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -493,8 +493,7 @@ export default function GeneralPage() {
|
|||||||
{
|
{
|
||||||
value: "whitelistedEmail",
|
value: "whitelistedEmail",
|
||||||
label: "Whitelisted Email"
|
label: "Whitelisted Email"
|
||||||
},
|
}
|
||||||
{ value: "ssh", label: "SSH" }
|
|
||||||
]}
|
]}
|
||||||
selectedValue={filters.type}
|
selectedValue={filters.type}
|
||||||
onValueChange={(value) =>
|
onValueChange={(value) =>
|
||||||
@@ -508,12 +507,13 @@ export default function GeneralPage() {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const typeLabel =
|
// should be capitalized first letter
|
||||||
row.original.type === "ssh"
|
return (
|
||||||
? "SSH"
|
<span>
|
||||||
: row.original.type.charAt(0).toUpperCase() +
|
{row.original.type.charAt(0).toUpperCase() +
|
||||||
row.original.type.slice(1);
|
row.original.type.slice(1) || "-"}
|
||||||
return <span>{typeLabel || "-"}</span>;
|
</span>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,760 +0,0 @@
|
|||||||
"use client";
|
|
||||||
import { Button } from "@app/components/ui/button";
|
|
||||||
import { ColumnFilter } from "@app/components/ColumnFilter";
|
|
||||||
import { DateTimeValue } from "@app/components/DateTimePicker";
|
|
||||||
import { LogDataTable } from "@app/components/LogDataTable";
|
|
||||||
import { PaidFeaturesAlert } from "@app/components/PaidFeaturesAlert";
|
|
||||||
import SettingsSectionTitle from "@app/components/SettingsSectionTitle";
|
|
||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
|
||||||
import { usePaidStatus } from "@app/hooks/usePaidStatus";
|
|
||||||
import { useStoredPageSize } from "@app/hooks/useStoredPageSize";
|
|
||||||
import { toast } from "@app/hooks/useToast";
|
|
||||||
import { createApiClient } from "@app/lib/api";
|
|
||||||
import { getSevenDaysAgo } from "@app/lib/getSevenDaysAgo";
|
|
||||||
import { build } from "@server/build";
|
|
||||||
import { tierMatrix } from "@server/lib/billing/tierMatrix";
|
|
||||||
import { ColumnDef } from "@tanstack/react-table";
|
|
||||||
import axios from "axios";
|
|
||||||
import { ArrowUpRight, Laptop, User } from "lucide-react";
|
|
||||||
import Link from "next/link";
|
|
||||||
import { useTranslations } from "next-intl";
|
|
||||||
import { useParams, useRouter, useSearchParams } from "next/navigation";
|
|
||||||
import { useEffect, useState, useTransition } from "react";
|
|
||||||
|
|
||||||
function formatBytes(bytes: number | null): string {
|
|
||||||
if (bytes === null || bytes === undefined) return "—";
|
|
||||||
if (bytes === 0) return "0 B";
|
|
||||||
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
||||||
const value = bytes / Math.pow(1024, i);
|
|
||||||
return `${value.toFixed(i === 0 ? 0 : 1)} ${units[i]}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatDuration(startedAt: number, endedAt: number | null): string {
|
|
||||||
if (endedAt === null || endedAt === undefined) return "Active";
|
|
||||||
const durationSec = endedAt - startedAt;
|
|
||||||
if (durationSec < 0) return "—";
|
|
||||||
if (durationSec < 60) return `${durationSec}s`;
|
|
||||||
if (durationSec < 3600) {
|
|
||||||
const m = Math.floor(durationSec / 60);
|
|
||||||
const s = durationSec % 60;
|
|
||||||
return `${m}m ${s}s`;
|
|
||||||
}
|
|
||||||
const h = Math.floor(durationSec / 3600);
|
|
||||||
const m = Math.floor((durationSec % 3600) / 60);
|
|
||||||
return `${h}h ${m}m`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ConnectionLogsPage() {
|
|
||||||
const router = useRouter();
|
|
||||||
const api = createApiClient(useEnvContext());
|
|
||||||
const t = useTranslations();
|
|
||||||
const { orgId } = useParams();
|
|
||||||
const searchParams = useSearchParams();
|
|
||||||
|
|
||||||
const { isPaidUser } = usePaidStatus();
|
|
||||||
|
|
||||||
const [rows, setRows] = useState<any[]>([]);
|
|
||||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
|
||||||
const [isExporting, startTransition] = useTransition();
|
|
||||||
const [filterAttributes, setFilterAttributes] = useState<{
|
|
||||||
protocols: string[];
|
|
||||||
destAddrs: string[];
|
|
||||||
clients: { id: number; name: string }[];
|
|
||||||
resources: { id: number; name: string | null }[];
|
|
||||||
users: { id: string; email: string | null }[];
|
|
||||||
}>({
|
|
||||||
protocols: [],
|
|
||||||
destAddrs: [],
|
|
||||||
clients: [],
|
|
||||||
resources: [],
|
|
||||||
users: []
|
|
||||||
});
|
|
||||||
|
|
||||||
// Filter states - unified object for all filters
|
|
||||||
const [filters, setFilters] = useState<{
|
|
||||||
protocol?: string;
|
|
||||||
destAddr?: string;
|
|
||||||
clientId?: string;
|
|
||||||
siteResourceId?: string;
|
|
||||||
userId?: string;
|
|
||||||
}>({
|
|
||||||
protocol: searchParams.get("protocol") || undefined,
|
|
||||||
destAddr: searchParams.get("destAddr") || undefined,
|
|
||||||
clientId: searchParams.get("clientId") || undefined,
|
|
||||||
siteResourceId: searchParams.get("siteResourceId") || undefined,
|
|
||||||
userId: searchParams.get("userId") || undefined
|
|
||||||
});
|
|
||||||
|
|
||||||
// Pagination state
|
|
||||||
const [totalCount, setTotalCount] = useState<number>(0);
|
|
||||||
const [currentPage, setCurrentPage] = useState<number>(0);
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
|
|
||||||
// Initialize page size from storage or default
|
|
||||||
const [pageSize, setPageSize] = useStoredPageSize(
|
|
||||||
"connection-audit-logs",
|
|
||||||
20
|
|
||||||
);
|
|
||||||
|
|
||||||
// Set default date range to last 7 days
|
|
||||||
const getDefaultDateRange = () => {
|
|
||||||
// if the time is in the url params, use that instead
|
|
||||||
const startParam = searchParams.get("start");
|
|
||||||
const endParam = searchParams.get("end");
|
|
||||||
if (startParam && endParam) {
|
|
||||||
return {
|
|
||||||
startDate: {
|
|
||||||
date: new Date(startParam)
|
|
||||||
},
|
|
||||||
endDate: {
|
|
||||||
date: new Date(endParam)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const now = new Date();
|
|
||||||
const lastWeek = getSevenDaysAgo();
|
|
||||||
|
|
||||||
return {
|
|
||||||
startDate: {
|
|
||||||
date: lastWeek
|
|
||||||
},
|
|
||||||
endDate: {
|
|
||||||
date: now
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const [dateRange, setDateRange] = useState<{
|
|
||||||
startDate: DateTimeValue;
|
|
||||||
endDate: DateTimeValue;
|
|
||||||
}>(getDefaultDateRange());
|
|
||||||
|
|
||||||
// Trigger search with default values on component mount
|
|
||||||
useEffect(() => {
|
|
||||||
if (build === "oss") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const defaultRange = getDefaultDateRange();
|
|
||||||
queryDateTime(
|
|
||||||
defaultRange.startDate,
|
|
||||||
defaultRange.endDate,
|
|
||||||
0,
|
|
||||||
pageSize
|
|
||||||
);
|
|
||||||
}, [orgId]); // Re-run if orgId changes
|
|
||||||
|
|
||||||
const handleDateRangeChange = (
|
|
||||||
startDate: DateTimeValue,
|
|
||||||
endDate: DateTimeValue
|
|
||||||
) => {
|
|
||||||
setDateRange({ startDate, endDate });
|
|
||||||
setCurrentPage(0); // Reset to first page when filtering
|
|
||||||
// put the search params in the url for the time
|
|
||||||
updateUrlParamsForAllFilters({
|
|
||||||
start: startDate.date?.toISOString() || "",
|
|
||||||
end: endDate.date?.toISOString() || ""
|
|
||||||
});
|
|
||||||
|
|
||||||
queryDateTime(startDate, endDate, 0, pageSize);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Handle page changes
|
|
||||||
const handlePageChange = (newPage: number) => {
|
|
||||||
setCurrentPage(newPage);
|
|
||||||
queryDateTime(
|
|
||||||
dateRange.startDate,
|
|
||||||
dateRange.endDate,
|
|
||||||
newPage,
|
|
||||||
pageSize
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Handle page size changes
|
|
||||||
const handlePageSizeChange = (newPageSize: number) => {
|
|
||||||
setPageSize(newPageSize);
|
|
||||||
setCurrentPage(0); // Reset to first page when changing page size
|
|
||||||
queryDateTime(dateRange.startDate, dateRange.endDate, 0, newPageSize);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Handle filter changes generically
|
|
||||||
const handleFilterChange = (
|
|
||||||
filterType: keyof typeof filters,
|
|
||||||
value: string | undefined
|
|
||||||
) => {
|
|
||||||
// Create new filters object with updated value
|
|
||||||
const newFilters = {
|
|
||||||
...filters,
|
|
||||||
[filterType]: value
|
|
||||||
};
|
|
||||||
|
|
||||||
setFilters(newFilters);
|
|
||||||
setCurrentPage(0); // Reset to first page when filtering
|
|
||||||
|
|
||||||
// Update URL params
|
|
||||||
updateUrlParamsForAllFilters(newFilters);
|
|
||||||
|
|
||||||
// Trigger new query with updated filters (pass directly to avoid async state issues)
|
|
||||||
queryDateTime(
|
|
||||||
dateRange.startDate,
|
|
||||||
dateRange.endDate,
|
|
||||||
0,
|
|
||||||
pageSize,
|
|
||||||
newFilters
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateUrlParamsForAllFilters = (
|
|
||||||
newFilters:
|
|
||||||
| typeof filters
|
|
||||||
| {
|
|
||||||
start: string;
|
|
||||||
end: string;
|
|
||||||
}
|
|
||||||
) => {
|
|
||||||
const params = new URLSearchParams(searchParams);
|
|
||||||
Object.entries(newFilters).forEach(([key, value]) => {
|
|
||||||
if (value) {
|
|
||||||
params.set(key, value);
|
|
||||||
} else {
|
|
||||||
params.delete(key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
router.replace(`?${params.toString()}`, { scroll: false });
|
|
||||||
};
|
|
||||||
|
|
||||||
const queryDateTime = async (
|
|
||||||
startDate: DateTimeValue,
|
|
||||||
endDate: DateTimeValue,
|
|
||||||
page: number = currentPage,
|
|
||||||
size: number = pageSize,
|
|
||||||
filtersParam?: typeof filters
|
|
||||||
) => {
|
|
||||||
console.log("Date range changed:", { startDate, endDate, page, size });
|
|
||||||
if (!isPaidUser(tierMatrix.connectionLogs)) {
|
|
||||||
console.log(
|
|
||||||
"Access denied: subscription inactive or license locked"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setIsLoading(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Use the provided filters or fall back to current state
|
|
||||||
const activeFilters = filtersParam || filters;
|
|
||||||
|
|
||||||
// Convert the date/time values to API parameters
|
|
||||||
const params: any = {
|
|
||||||
limit: size,
|
|
||||||
offset: page * size,
|
|
||||||
...activeFilters
|
|
||||||
};
|
|
||||||
|
|
||||||
if (startDate?.date) {
|
|
||||||
const startDateTime = new Date(startDate.date);
|
|
||||||
if (startDate.time) {
|
|
||||||
const [hours, minutes, seconds] = startDate.time
|
|
||||||
.split(":")
|
|
||||||
.map(Number);
|
|
||||||
startDateTime.setHours(hours, minutes, seconds || 0);
|
|
||||||
}
|
|
||||||
params.timeStart = startDateTime.toISOString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (endDate?.date) {
|
|
||||||
const endDateTime = new Date(endDate.date);
|
|
||||||
if (endDate.time) {
|
|
||||||
const [hours, minutes, seconds] = endDate.time
|
|
||||||
.split(":")
|
|
||||||
.map(Number);
|
|
||||||
endDateTime.setHours(hours, minutes, seconds || 0);
|
|
||||||
} else {
|
|
||||||
// If no time is specified, set to NOW
|
|
||||||
const now = new Date();
|
|
||||||
endDateTime.setHours(
|
|
||||||
now.getHours(),
|
|
||||||
now.getMinutes(),
|
|
||||||
now.getSeconds(),
|
|
||||||
now.getMilliseconds()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
params.timeEnd = endDateTime.toISOString();
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await api.get(`/org/${orgId}/logs/connection`, {
|
|
||||||
params
|
|
||||||
});
|
|
||||||
if (res.status === 200) {
|
|
||||||
setRows(res.data.data.log || []);
|
|
||||||
setTotalCount(res.data.data.pagination?.total || 0);
|
|
||||||
setFilterAttributes(res.data.data.filterAttributes);
|
|
||||||
console.log("Fetched connection logs:", res.data);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
toast({
|
|
||||||
title: t("error"),
|
|
||||||
description: t("Failed to filter logs"),
|
|
||||||
variant: "destructive"
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const refreshData = async () => {
|
|
||||||
console.log("Data refreshed");
|
|
||||||
setIsRefreshing(true);
|
|
||||||
try {
|
|
||||||
// Refresh data with current date range and pagination
|
|
||||||
await queryDateTime(
|
|
||||||
dateRange.startDate,
|
|
||||||
dateRange.endDate,
|
|
||||||
currentPage,
|
|
||||||
pageSize
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
toast({
|
|
||||||
title: t("error"),
|
|
||||||
description: t("refreshError"),
|
|
||||||
variant: "destructive"
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
setIsRefreshing(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const exportData = async () => {
|
|
||||||
try {
|
|
||||||
// Prepare query params for export
|
|
||||||
const params: any = {
|
|
||||||
timeStart: dateRange.startDate?.date
|
|
||||||
? new Date(dateRange.startDate.date).toISOString()
|
|
||||||
: undefined,
|
|
||||||
timeEnd: dateRange.endDate?.date
|
|
||||||
? new Date(dateRange.endDate.date).toISOString()
|
|
||||||
: undefined,
|
|
||||||
...filters
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await api.get(
|
|
||||||
`/org/${orgId}/logs/connection/export`,
|
|
||||||
{
|
|
||||||
responseType: "blob",
|
|
||||||
params
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Create a URL for the blob and trigger a download
|
|
||||||
const url = window.URL.createObjectURL(new Blob([response.data]));
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = url;
|
|
||||||
const epoch = Math.floor(Date.now() / 1000);
|
|
||||||
link.setAttribute(
|
|
||||||
"download",
|
|
||||||
`connection-audit-logs-${orgId}-${epoch}.csv`
|
|
||||||
);
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
link.parentNode?.removeChild(link);
|
|
||||||
} catch (error) {
|
|
||||||
let apiErrorMessage: string | null = null;
|
|
||||||
if (axios.isAxiosError(error) && error.response) {
|
|
||||||
const data = error.response.data;
|
|
||||||
|
|
||||||
if (data instanceof Blob && data.type === "application/json") {
|
|
||||||
// Parse the Blob as JSON
|
|
||||||
const text = await data.text();
|
|
||||||
const errorData = JSON.parse(text);
|
|
||||||
apiErrorMessage = errorData.message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
toast({
|
|
||||||
title: t("error"),
|
|
||||||
description: apiErrorMessage ?? t("exportError"),
|
|
||||||
variant: "destructive"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns: ColumnDef<any>[] = [
|
|
||||||
{
|
|
||||||
accessorKey: "startedAt",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return t("timestamp");
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<div className="whitespace-nowrap">
|
|
||||||
{new Date(
|
|
||||||
row.original.startedAt * 1000
|
|
||||||
).toLocaleString()}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "protocol",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span>{t("protocol")}</span>
|
|
||||||
<ColumnFilter
|
|
||||||
options={filterAttributes.protocols.map(
|
|
||||||
(protocol) => ({
|
|
||||||
label: protocol.toUpperCase(),
|
|
||||||
value: protocol
|
|
||||||
})
|
|
||||||
)}
|
|
||||||
selectedValue={filters.protocol}
|
|
||||||
onValueChange={(value) =>
|
|
||||||
handleFilterChange("protocol", value)
|
|
||||||
}
|
|
||||||
searchPlaceholder="Search..."
|
|
||||||
emptyMessage="None found"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<span className="whitespace-nowrap font-mono text-xs">
|
|
||||||
{row.original.protocol?.toUpperCase()}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "resourceName",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span>{t("resource")}</span>
|
|
||||||
<ColumnFilter
|
|
||||||
options={filterAttributes.resources.map((res) => ({
|
|
||||||
value: res.id.toString(),
|
|
||||||
label: res.name || "Unnamed Resource"
|
|
||||||
}))}
|
|
||||||
selectedValue={filters.siteResourceId}
|
|
||||||
onValueChange={(value) =>
|
|
||||||
handleFilterChange("siteResourceId", value)
|
|
||||||
}
|
|
||||||
searchPlaceholder="Search..."
|
|
||||||
emptyMessage="None found"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
if (row.original.resourceName && row.original.resourceNiceId) {
|
|
||||||
return (
|
|
||||||
<Link
|
|
||||||
href={`/${row.original.orgId}/settings/resources/client/?query=${row.original.resourceNiceId}`}
|
|
||||||
>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
className="text-xs h-6"
|
|
||||||
>
|
|
||||||
{row.original.resourceName}
|
|
||||||
<ArrowUpRight className="ml-2 h-3 w-3" />
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
<span className="whitespace-nowrap">
|
|
||||||
{row.original.resourceName ?? "—"}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "clientName",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span>{t("client")}</span>
|
|
||||||
<ColumnFilter
|
|
||||||
options={filterAttributes.clients.map((c) => ({
|
|
||||||
value: c.id.toString(),
|
|
||||||
label: c.name
|
|
||||||
}))}
|
|
||||||
selectedValue={filters.clientId}
|
|
||||||
onValueChange={(value) =>
|
|
||||||
handleFilterChange("clientId", value)
|
|
||||||
}
|
|
||||||
searchPlaceholder="Search..."
|
|
||||||
emptyMessage="None found"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const clientType = row.original.clientType === "olm" ? "machine" : "user";
|
|
||||||
if (row.original.clientName && row.original.clientNiceId) {
|
|
||||||
return (
|
|
||||||
<Link
|
|
||||||
href={`/${row.original.orgId}/settings/clients/${clientType}/${row.original.clientNiceId}`}
|
|
||||||
>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
className="text-xs h-6"
|
|
||||||
>
|
|
||||||
<Laptop className="mr-1 h-3 w-3" />
|
|
||||||
{row.original.clientName}
|
|
||||||
<ArrowUpRight className="ml-2 h-3 w-3" />
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
<span className="whitespace-nowrap">
|
|
||||||
{row.original.clientName ?? "—"}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "userEmail",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span>{t("user")}</span>
|
|
||||||
<ColumnFilter
|
|
||||||
options={filterAttributes.users.map((u) => ({
|
|
||||||
value: u.id,
|
|
||||||
label: u.email || u.id
|
|
||||||
}))}
|
|
||||||
selectedValue={filters.userId}
|
|
||||||
onValueChange={(value) =>
|
|
||||||
handleFilterChange("userId", value)
|
|
||||||
}
|
|
||||||
searchPlaceholder="Search..."
|
|
||||||
emptyMessage="None found"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
if (row.original.userEmail || row.original.userId) {
|
|
||||||
return (
|
|
||||||
<span className="flex items-center gap-1 whitespace-nowrap">
|
|
||||||
<User className="h-4 w-4" />
|
|
||||||
{row.original.userEmail ?? row.original.userId}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return <span>—</span>;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "sourceAddr",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return t("sourceAddress");
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<span className="whitespace-nowrap font-mono text-xs">
|
|
||||||
{row.original.sourceAddr}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "destAddr",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span>{t("destinationAddress")}</span>
|
|
||||||
<ColumnFilter
|
|
||||||
options={filterAttributes.destAddrs.map((addr) => ({
|
|
||||||
value: addr,
|
|
||||||
label: addr
|
|
||||||
}))}
|
|
||||||
selectedValue={filters.destAddr}
|
|
||||||
onValueChange={(value) =>
|
|
||||||
handleFilterChange("destAddr", value)
|
|
||||||
}
|
|
||||||
searchPlaceholder="Search..."
|
|
||||||
emptyMessage="None found"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<span className="whitespace-nowrap font-mono text-xs">
|
|
||||||
{row.original.destAddr}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "duration",
|
|
||||||
header: ({ column }) => {
|
|
||||||
return t("duration");
|
|
||||||
},
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<span className="whitespace-nowrap">
|
|
||||||
{formatDuration(
|
|
||||||
row.original.startedAt,
|
|
||||||
row.original.endedAt
|
|
||||||
)}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const renderExpandedRow = (row: any) => {
|
|
||||||
return (
|
|
||||||
<div className="space-y-4">
|
|
||||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 text-xs">
|
|
||||||
<div className="space-y-2">
|
|
||||||
{/*<div className="flex items-center gap-1 font-semibold text-sm mb-1">
|
|
||||||
Connection Details
|
|
||||||
</div>*/}
|
|
||||||
<div>
|
|
||||||
<strong>Session ID:</strong>{" "}
|
|
||||||
<span className="font-mono">
|
|
||||||
{row.sessionId ?? "—"}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<strong>Protocol:</strong>{" "}
|
|
||||||
{row.protocol?.toUpperCase() ?? "—"}
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<strong>Source:</strong>{" "}
|
|
||||||
<span className="font-mono">
|
|
||||||
{row.sourceAddr ?? "—"}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<strong>Destination:</strong>{" "}
|
|
||||||
<span className="font-mono">
|
|
||||||
{row.destAddr ?? "—"}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="space-y-2">
|
|
||||||
{/*<div className="flex items-center gap-1 font-semibold text-sm mb-1">
|
|
||||||
Resource & Site
|
|
||||||
</div>*/}
|
|
||||||
{/*<div>
|
|
||||||
<strong>Resource:</strong>{" "}
|
|
||||||
{row.resourceName ?? "—"}
|
|
||||||
{row.resourceNiceId && (
|
|
||||||
<span className="text-muted-foreground ml-1">
|
|
||||||
({row.resourceNiceId})
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>*/}
|
|
||||||
<div>
|
|
||||||
<strong>Site:</strong> {row.siteName ?? "—"}
|
|
||||||
{row.siteNiceId && (
|
|
||||||
<span className="text-muted-foreground ml-1">
|
|
||||||
({row.siteNiceId})
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<strong>Site ID:</strong> {row.siteId ?? "—"}
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<strong>Started At:</strong>{" "}
|
|
||||||
{row.startedAt
|
|
||||||
? new Date(
|
|
||||||
row.startedAt * 1000
|
|
||||||
).toLocaleString()
|
|
||||||
: "—"}
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<strong>Ended At:</strong>{" "}
|
|
||||||
{row.endedAt
|
|
||||||
? new Date(
|
|
||||||
row.endedAt * 1000
|
|
||||||
).toLocaleString()
|
|
||||||
: "Active"}
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<strong>Duration:</strong>{" "}
|
|
||||||
{formatDuration(row.startedAt, row.endedAt)}
|
|
||||||
</div>
|
|
||||||
{/*<div>
|
|
||||||
<strong>Resource ID:</strong>{" "}
|
|
||||||
{row.siteResourceId ?? "—"}
|
|
||||||
</div>*/}
|
|
||||||
</div>
|
|
||||||
<div className="space-y-2">
|
|
||||||
{/*<div className="flex items-center gap-1 font-semibold text-sm mb-1">
|
|
||||||
Client & Transfer
|
|
||||||
</div>*/}
|
|
||||||
{/*<div>
|
|
||||||
<strong>Bytes Sent (TX):</strong>{" "}
|
|
||||||
{formatBytes(row.bytesTx)}
|
|
||||||
</div>*/}
|
|
||||||
{/*<div>
|
|
||||||
<strong>Bytes Received (RX):</strong>{" "}
|
|
||||||
{formatBytes(row.bytesRx)}
|
|
||||||
</div>*/}
|
|
||||||
{/*<div>
|
|
||||||
<strong>Total Transfer:</strong>{" "}
|
|
||||||
{formatBytes(
|
|
||||||
(row.bytesTx ?? 0) + (row.bytesRx ?? 0)
|
|
||||||
)}
|
|
||||||
</div>*/}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<SettingsSectionTitle
|
|
||||||
title={t("connectionLogs")}
|
|
||||||
description={t("connectionLogsDescription")}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<PaidFeaturesAlert tiers={tierMatrix.connectionLogs} />
|
|
||||||
|
|
||||||
<LogDataTable
|
|
||||||
columns={columns}
|
|
||||||
data={rows}
|
|
||||||
title={t("connectionLogs")}
|
|
||||||
searchPlaceholder={t("searchLogs")}
|
|
||||||
searchColumn="protocol"
|
|
||||||
onRefresh={refreshData}
|
|
||||||
isRefreshing={isRefreshing}
|
|
||||||
onExport={() => startTransition(exportData)}
|
|
||||||
isExporting={isExporting}
|
|
||||||
onDateRangeChange={handleDateRangeChange}
|
|
||||||
dateRange={{
|
|
||||||
start: dateRange.startDate,
|
|
||||||
end: dateRange.endDate
|
|
||||||
}}
|
|
||||||
defaultSort={{
|
|
||||||
id: "startedAt",
|
|
||||||
desc: true
|
|
||||||
}}
|
|
||||||
// Server-side pagination props
|
|
||||||
totalCount={totalCount}
|
|
||||||
currentPage={currentPage}
|
|
||||||
pageSize={pageSize}
|
|
||||||
onPageChange={handlePageChange}
|
|
||||||
onPageSizeChange={handlePageSizeChange}
|
|
||||||
isLoading={isLoading}
|
|
||||||
// Row expansion props
|
|
||||||
expandable={true}
|
|
||||||
renderExpandedRow={renderExpandedRow}
|
|
||||||
disabled={
|
|
||||||
!isPaidUser(tierMatrix.connectionLogs) || build === "oss"
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
import { internal } from "@app/lib/api";
|
|
||||||
import { authCookieHeader } from "@app/lib/api/cookies";
|
|
||||||
import { AxiosResponse } from "axios";
|
|
||||||
import { PaidFeaturesAlert } from "@app/components/PaidFeaturesAlert";
|
|
||||||
import SettingsSectionTitle from "@app/components/SettingsSectionTitle";
|
|
||||||
import SiteProvisioningKeysTable, {
|
|
||||||
SiteProvisioningKeyRow
|
|
||||||
} from "../../../../components/SiteProvisioningKeysTable";
|
|
||||||
import { ListSiteProvisioningKeysResponse } from "@server/routers/siteProvisioning/types";
|
|
||||||
import { getTranslations } from "next-intl/server";
|
|
||||||
import { TierFeature, tierMatrix } from "@server/lib/billing/tierMatrix";
|
|
||||||
|
|
||||||
type ProvisioningPageProps = {
|
|
||||||
params: Promise<{ orgId: string }>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
export default async function ProvisioningPage(props: ProvisioningPageProps) {
|
|
||||||
const params = await props.params;
|
|
||||||
const t = await getTranslations();
|
|
||||||
|
|
||||||
let siteProvisioningKeys: ListSiteProvisioningKeysResponse["siteProvisioningKeys"] =
|
|
||||||
[];
|
|
||||||
try {
|
|
||||||
const res = await internal.get<
|
|
||||||
AxiosResponse<ListSiteProvisioningKeysResponse>
|
|
||||||
>(
|
|
||||||
`/org/${params.orgId}/site-provisioning-keys`,
|
|
||||||
await authCookieHeader()
|
|
||||||
);
|
|
||||||
siteProvisioningKeys = res.data.data.siteProvisioningKeys;
|
|
||||||
} catch (e) {}
|
|
||||||
|
|
||||||
const rows: SiteProvisioningKeyRow[] = siteProvisioningKeys.map((k) => ({
|
|
||||||
name: k.name,
|
|
||||||
id: k.siteProvisioningKeyId,
|
|
||||||
key: `${k.siteProvisioningKeyId}••••••••••••••••••••${k.lastChars}`,
|
|
||||||
createdAt: k.createdAt,
|
|
||||||
lastUsed: k.lastUsed,
|
|
||||||
maxBatchSize: k.maxBatchSize,
|
|
||||||
numUsed: k.numUsed,
|
|
||||||
validUntil: k.validUntil
|
|
||||||
}));
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<SettingsSectionTitle
|
|
||||||
title={t("provisioningKeysManage")}
|
|
||||||
description={t("provisioningKeysDescription")}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<PaidFeaturesAlert
|
|
||||||
tiers={tierMatrix[TierFeature.SiteProvisioningKeys]}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<SiteProvisioningKeysTable keys={rows} orgId={params.orgId} />
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -2,9 +2,7 @@ import { SidebarNavItem } from "@app/components/SidebarNav";
|
|||||||
import { Env } from "@app/lib/types/env";
|
import { Env } from "@app/lib/types/env";
|
||||||
import { build } from "@server/build";
|
import { build } from "@server/build";
|
||||||
import {
|
import {
|
||||||
Boxes,
|
|
||||||
Building2,
|
Building2,
|
||||||
Cable,
|
|
||||||
ChartLine,
|
ChartLine,
|
||||||
Combine,
|
Combine,
|
||||||
CreditCard,
|
CreditCard,
|
||||||
@@ -191,11 +189,6 @@ export const orgNavSections = (
|
|||||||
title: "sidebarLogsAction",
|
title: "sidebarLogsAction",
|
||||||
href: "/{orgId}/settings/logs/action",
|
href: "/{orgId}/settings/logs/action",
|
||||||
icon: <Logs className="size-4 flex-none" />
|
icon: <Logs className="size-4 flex-none" />
|
||||||
},
|
|
||||||
{
|
|
||||||
title: "sidebarLogsConnection",
|
|
||||||
href: "/{orgId}/settings/logs/connection",
|
|
||||||
icon: <Cable className="size-4 flex-none" />
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
: [])
|
: [])
|
||||||
@@ -210,11 +203,6 @@ export const orgNavSections = (
|
|||||||
href: "/{orgId}/settings/api-keys",
|
href: "/{orgId}/settings/api-keys",
|
||||||
icon: <KeyRound className="size-4 flex-none" />
|
icon: <KeyRound className="size-4 flex-none" />
|
||||||
},
|
},
|
||||||
{
|
|
||||||
title: "sidebarProvisioning",
|
|
||||||
href: "/{orgId}/settings/provisioning",
|
|
||||||
icon: <Boxes className="size-4 flex-none" />
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
title: "sidebarBluePrints",
|
title: "sidebarBluePrints",
|
||||||
href: "/{orgId}/settings/blueprints",
|
href: "/{orgId}/settings/blueprints",
|
||||||
|
|||||||
@@ -1,329 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import {
|
|
||||||
Credenza,
|
|
||||||
CredenzaBody,
|
|
||||||
CredenzaClose,
|
|
||||||
CredenzaContent,
|
|
||||||
CredenzaDescription,
|
|
||||||
CredenzaFooter,
|
|
||||||
CredenzaHeader,
|
|
||||||
CredenzaTitle
|
|
||||||
} from "@app/components/Credenza";
|
|
||||||
import {
|
|
||||||
Form,
|
|
||||||
FormControl,
|
|
||||||
FormDescription,
|
|
||||||
FormField,
|
|
||||||
FormItem,
|
|
||||||
FormLabel,
|
|
||||||
FormMessage
|
|
||||||
} from "@app/components/ui/form";
|
|
||||||
import { Button } from "@app/components/ui/button";
|
|
||||||
import { Checkbox } from "@app/components/ui/checkbox";
|
|
||||||
import { Input } from "@app/components/ui/input";
|
|
||||||
import { Alert, AlertDescription, AlertTitle } from "@app/components/ui/alert";
|
|
||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
|
||||||
import { toast } from "@app/hooks/useToast";
|
|
||||||
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
|
||||||
import { CreateSiteProvisioningKeyResponse } from "@server/routers/siteProvisioning/types";
|
|
||||||
import { AxiosResponse } from "axios";
|
|
||||||
import { InfoIcon } from "lucide-react";
|
|
||||||
import { useTranslations } from "next-intl";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { useForm } from "react-hook-form";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
|
||||||
import CopyTextBox from "@app/components/CopyTextBox";
|
|
||||||
|
|
||||||
const FORM_ID = "create-site-provisioning-key-form";
|
|
||||||
|
|
||||||
type CreateSiteProvisioningKeyCredenzaProps = {
|
|
||||||
open: boolean;
|
|
||||||
setOpen: (open: boolean) => void;
|
|
||||||
orgId: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function CreateSiteProvisioningKeyCredenza({
|
|
||||||
open,
|
|
||||||
setOpen,
|
|
||||||
orgId
|
|
||||||
}: CreateSiteProvisioningKeyCredenzaProps) {
|
|
||||||
const t = useTranslations();
|
|
||||||
const router = useRouter();
|
|
||||||
const api = createApiClient(useEnvContext());
|
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
const [created, setCreated] =
|
|
||||||
useState<CreateSiteProvisioningKeyResponse | null>(null);
|
|
||||||
|
|
||||||
const createFormSchema = z
|
|
||||||
.object({
|
|
||||||
name: z
|
|
||||||
.string()
|
|
||||||
.min(1, {
|
|
||||||
message: t("nameMin", { len: 1 })
|
|
||||||
})
|
|
||||||
.max(255, {
|
|
||||||
message: t("nameMax", { len: 255 })
|
|
||||||
}),
|
|
||||||
unlimitedBatchSize: z.boolean(),
|
|
||||||
maxBatchSize: z
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.min(1, { message: t("provisioningKeysMaxBatchSizeInvalid") })
|
|
||||||
.max(1_000_000, {
|
|
||||||
message: t("provisioningKeysMaxBatchSizeInvalid")
|
|
||||||
}),
|
|
||||||
validUntil: z.string().optional()
|
|
||||||
})
|
|
||||||
.superRefine((data, ctx) => {
|
|
||||||
const v = data.validUntil;
|
|
||||||
if (v == null || v.trim() === "") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (Number.isNaN(Date.parse(v))) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
message: t("provisioningKeysValidUntilInvalid"),
|
|
||||||
path: ["validUntil"]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
type CreateFormValues = z.infer<typeof createFormSchema>;
|
|
||||||
|
|
||||||
const form = useForm<CreateFormValues>({
|
|
||||||
resolver: zodResolver(createFormSchema),
|
|
||||||
defaultValues: {
|
|
||||||
name: "",
|
|
||||||
unlimitedBatchSize: false,
|
|
||||||
maxBatchSize: 100,
|
|
||||||
validUntil: ""
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!open) {
|
|
||||||
setCreated(null);
|
|
||||||
form.reset({
|
|
||||||
name: "",
|
|
||||||
unlimitedBatchSize: false,
|
|
||||||
maxBatchSize: 100,
|
|
||||||
validUntil: ""
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [open, form]);
|
|
||||||
|
|
||||||
async function onSubmit(data: CreateFormValues) {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const res = await api
|
|
||||||
.put<
|
|
||||||
AxiosResponse<CreateSiteProvisioningKeyResponse>
|
|
||||||
>(`/org/${orgId}/site-provisioning-key`, {
|
|
||||||
name: data.name,
|
|
||||||
maxBatchSize: data.unlimitedBatchSize
|
|
||||||
? null
|
|
||||||
: data.maxBatchSize,
|
|
||||||
validUntil:
|
|
||||||
data.validUntil == null || data.validUntil.trim() === ""
|
|
||||||
? undefined
|
|
||||||
: data.validUntil
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
toast({
|
|
||||||
variant: "destructive",
|
|
||||||
title: t("provisioningKeysErrorCreate"),
|
|
||||||
description: formatAxiosError(e)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res && res.status === 201) {
|
|
||||||
setCreated(res.data.data);
|
|
||||||
router.refresh();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const credential =
|
|
||||||
created &&
|
|
||||||
`${created.siteProvisioningKeyId}.${created.siteProvisioningKey}`;
|
|
||||||
|
|
||||||
const unlimitedBatchSize = form.watch("unlimitedBatchSize");
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Credenza open={open} onOpenChange={setOpen}>
|
|
||||||
<CredenzaContent>
|
|
||||||
<CredenzaHeader>
|
|
||||||
<CredenzaTitle>
|
|
||||||
{created
|
|
||||||
? t("provisioningKeysList")
|
|
||||||
: t("provisioningKeysCreate")}
|
|
||||||
</CredenzaTitle>
|
|
||||||
{!created && (
|
|
||||||
<CredenzaDescription>
|
|
||||||
{t("provisioningKeysCreateDescription")}
|
|
||||||
</CredenzaDescription>
|
|
||||||
)}
|
|
||||||
</CredenzaHeader>
|
|
||||||
<CredenzaBody>
|
|
||||||
{!created && (
|
|
||||||
<Form {...form}>
|
|
||||||
<form
|
|
||||||
id={FORM_ID}
|
|
||||||
onSubmit={form.handleSubmit(onSubmit)}
|
|
||||||
className="space-y-4"
|
|
||||||
>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="name"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>{t("name")}</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
autoComplete="off"
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="maxBatchSize"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>
|
|
||||||
{t(
|
|
||||||
"provisioningKeysMaxBatchSize"
|
|
||||||
)}
|
|
||||||
</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="number"
|
|
||||||
min={1}
|
|
||||||
max={1_000_000}
|
|
||||||
autoComplete="off"
|
|
||||||
disabled={
|
|
||||||
unlimitedBatchSize
|
|
||||||
}
|
|
||||||
name={field.name}
|
|
||||||
ref={field.ref}
|
|
||||||
onBlur={field.onBlur}
|
|
||||||
onChange={(e) => {
|
|
||||||
const v =
|
|
||||||
e.target.value;
|
|
||||||
field.onChange(
|
|
||||||
v === ""
|
|
||||||
? 100
|
|
||||||
: Number(v)
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
value={field.value}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="unlimitedBatchSize"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem className="flex flex-row items-center gap-3 space-y-0">
|
|
||||||
<FormControl>
|
|
||||||
<Checkbox
|
|
||||||
id="provisioning-unlimited-batch"
|
|
||||||
checked={field.value}
|
|
||||||
onCheckedChange={(c) =>
|
|
||||||
field.onChange(
|
|
||||||
c === true
|
|
||||||
)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormLabel
|
|
||||||
htmlFor="provisioning-unlimited-batch"
|
|
||||||
className="cursor-pointer font-normal !mt-0"
|
|
||||||
>
|
|
||||||
{t(
|
|
||||||
"provisioningKeysUnlimitedBatchSize"
|
|
||||||
)}
|
|
||||||
</FormLabel>
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="validUntil"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>
|
|
||||||
{t(
|
|
||||||
"provisioningKeysValidUntil"
|
|
||||||
)}
|
|
||||||
</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="datetime-local"
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormDescription>
|
|
||||||
{t(
|
|
||||||
"provisioningKeysValidUntilHint"
|
|
||||||
)}
|
|
||||||
</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{created && credential && (
|
|
||||||
<div className="space-y-4">
|
|
||||||
<Alert variant="neutral">
|
|
||||||
<InfoIcon className="h-4 w-4" />
|
|
||||||
<AlertTitle className="font-semibold">
|
|
||||||
{t("provisioningKeysSave")}
|
|
||||||
</AlertTitle>
|
|
||||||
<AlertDescription>
|
|
||||||
{t("provisioningKeysSaveDescription")}
|
|
||||||
</AlertDescription>
|
|
||||||
</Alert>
|
|
||||||
<CopyTextBox text={credential} />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</CredenzaBody>
|
|
||||||
<CredenzaFooter>
|
|
||||||
{!created ? (
|
|
||||||
<>
|
|
||||||
<CredenzaClose asChild>
|
|
||||||
<Button variant="outline">{t("close")}</Button>
|
|
||||||
</CredenzaClose>
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
form={FORM_ID}
|
|
||||||
loading={loading}
|
|
||||||
disabled={loading}
|
|
||||||
>
|
|
||||||
{t("generate")}
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<CredenzaClose asChild>
|
|
||||||
<Button variant="default">{t("done")}</Button>
|
|
||||||
</CredenzaClose>
|
|
||||||
)}
|
|
||||||
</CredenzaFooter>
|
|
||||||
</CredenzaContent>
|
|
||||||
</Credenza>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,297 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import {
|
|
||||||
Credenza,
|
|
||||||
CredenzaBody,
|
|
||||||
CredenzaClose,
|
|
||||||
CredenzaContent,
|
|
||||||
CredenzaDescription,
|
|
||||||
CredenzaFooter,
|
|
||||||
CredenzaHeader,
|
|
||||||
CredenzaTitle
|
|
||||||
} from "@app/components/Credenza";
|
|
||||||
import {
|
|
||||||
Form,
|
|
||||||
FormControl,
|
|
||||||
FormDescription,
|
|
||||||
FormField,
|
|
||||||
FormItem,
|
|
||||||
FormLabel,
|
|
||||||
FormMessage
|
|
||||||
} from "@app/components/ui/form";
|
|
||||||
import { Button } from "@app/components/ui/button";
|
|
||||||
import { Checkbox } from "@app/components/ui/checkbox";
|
|
||||||
import { Input } from "@app/components/ui/input";
|
|
||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
|
||||||
import { toast } from "@app/hooks/useToast";
|
|
||||||
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
|
||||||
import { UpdateSiteProvisioningKeyResponse } from "@server/routers/siteProvisioning/types";
|
|
||||||
import { AxiosResponse } from "axios";
|
|
||||||
import { useTranslations } from "next-intl";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { useForm } from "react-hook-form";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
|
||||||
import moment from "moment";
|
|
||||||
|
|
||||||
const FORM_ID = "edit-site-provisioning-key-form";
|
|
||||||
|
|
||||||
export type EditableSiteProvisioningKey = {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
maxBatchSize: number | null;
|
|
||||||
validUntil: string | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
type EditSiteProvisioningKeyCredenzaProps = {
|
|
||||||
open: boolean;
|
|
||||||
setOpen: (open: boolean) => void;
|
|
||||||
orgId: string;
|
|
||||||
provisioningKey: EditableSiteProvisioningKey | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function EditSiteProvisioningKeyCredenza({
|
|
||||||
open,
|
|
||||||
setOpen,
|
|
||||||
orgId,
|
|
||||||
provisioningKey
|
|
||||||
}: EditSiteProvisioningKeyCredenzaProps) {
|
|
||||||
const t = useTranslations();
|
|
||||||
const router = useRouter();
|
|
||||||
const api = createApiClient(useEnvContext());
|
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
|
|
||||||
const editFormSchema = z
|
|
||||||
.object({
|
|
||||||
name: z.string(),
|
|
||||||
unlimitedBatchSize: z.boolean(),
|
|
||||||
maxBatchSize: z
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.min(1, { message: t("provisioningKeysMaxBatchSizeInvalid") })
|
|
||||||
.max(1_000_000, {
|
|
||||||
message: t("provisioningKeysMaxBatchSizeInvalid")
|
|
||||||
}),
|
|
||||||
validUntil: z.string().optional()
|
|
||||||
})
|
|
||||||
.superRefine((data, ctx) => {
|
|
||||||
const v = data.validUntil;
|
|
||||||
if (v == null || v.trim() === "") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (Number.isNaN(Date.parse(v))) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: "custom",
|
|
||||||
message: t("provisioningKeysValidUntilInvalid"),
|
|
||||||
path: ["validUntil"]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
type EditFormValues = z.infer<typeof editFormSchema>;
|
|
||||||
|
|
||||||
const form = useForm<EditFormValues>({
|
|
||||||
resolver: zodResolver(editFormSchema),
|
|
||||||
defaultValues: {
|
|
||||||
name: "",
|
|
||||||
unlimitedBatchSize: false,
|
|
||||||
maxBatchSize: 100,
|
|
||||||
validUntil: ""
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!open || !provisioningKey) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
form.reset({
|
|
||||||
name: provisioningKey.name,
|
|
||||||
unlimitedBatchSize: provisioningKey.maxBatchSize == null,
|
|
||||||
maxBatchSize: provisioningKey.maxBatchSize ?? 100,
|
|
||||||
validUntil: provisioningKey.validUntil
|
|
||||||
? moment(provisioningKey.validUntil).format("YYYY-MM-DDTHH:mm")
|
|
||||||
: ""
|
|
||||||
});
|
|
||||||
}, [open, provisioningKey, form]);
|
|
||||||
|
|
||||||
async function onSubmit(data: EditFormValues) {
|
|
||||||
if (!provisioningKey) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const res = await api
|
|
||||||
.patch<
|
|
||||||
AxiosResponse<UpdateSiteProvisioningKeyResponse>
|
|
||||||
>(
|
|
||||||
`/org/${orgId}/site-provisioning-key/${provisioningKey.id}`,
|
|
||||||
{
|
|
||||||
maxBatchSize: data.unlimitedBatchSize
|
|
||||||
? null
|
|
||||||
: data.maxBatchSize,
|
|
||||||
validUntil:
|
|
||||||
data.validUntil == null ||
|
|
||||||
data.validUntil.trim() === ""
|
|
||||||
? ""
|
|
||||||
: data.validUntil
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.catch((e) => {
|
|
||||||
toast({
|
|
||||||
variant: "destructive",
|
|
||||||
title: t("provisioningKeysUpdateError"),
|
|
||||||
description: formatAxiosError(e)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res && res.status === 200) {
|
|
||||||
toast({
|
|
||||||
title: t("provisioningKeysUpdated"),
|
|
||||||
description: t("provisioningKeysUpdatedDescription")
|
|
||||||
});
|
|
||||||
setOpen(false);
|
|
||||||
router.refresh();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const unlimitedBatchSize = form.watch("unlimitedBatchSize");
|
|
||||||
|
|
||||||
if (!provisioningKey) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Credenza open={open} onOpenChange={setOpen}>
|
|
||||||
<CredenzaContent>
|
|
||||||
<CredenzaHeader>
|
|
||||||
<CredenzaTitle>{t("provisioningKeysEdit")}</CredenzaTitle>
|
|
||||||
<CredenzaDescription>
|
|
||||||
{t("provisioningKeysEditDescription")}
|
|
||||||
</CredenzaDescription>
|
|
||||||
</CredenzaHeader>
|
|
||||||
<CredenzaBody>
|
|
||||||
<Form {...form}>
|
|
||||||
<form
|
|
||||||
id={FORM_ID}
|
|
||||||
onSubmit={form.handleSubmit(onSubmit)}
|
|
||||||
className="space-y-4"
|
|
||||||
>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="name"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>{t("name")}</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
autoComplete="off"
|
|
||||||
disabled
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="maxBatchSize"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>
|
|
||||||
{t("provisioningKeysMaxBatchSize")}
|
|
||||||
</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="number"
|
|
||||||
min={1}
|
|
||||||
max={1_000_000}
|
|
||||||
autoComplete="off"
|
|
||||||
disabled={unlimitedBatchSize}
|
|
||||||
name={field.name}
|
|
||||||
ref={field.ref}
|
|
||||||
onBlur={field.onBlur}
|
|
||||||
onChange={(e) => {
|
|
||||||
const v = e.target.value;
|
|
||||||
field.onChange(
|
|
||||||
v === ""
|
|
||||||
? 100
|
|
||||||
: Number(v)
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
value={field.value}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="unlimitedBatchSize"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem className="flex flex-row items-center gap-3 space-y-0">
|
|
||||||
<FormControl>
|
|
||||||
<Checkbox
|
|
||||||
id="provisioning-edit-unlimited-batch"
|
|
||||||
checked={field.value}
|
|
||||||
onCheckedChange={(c) =>
|
|
||||||
field.onChange(c === true)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormLabel
|
|
||||||
htmlFor="provisioning-edit-unlimited-batch"
|
|
||||||
className="cursor-pointer font-normal !mt-0"
|
|
||||||
>
|
|
||||||
{t(
|
|
||||||
"provisioningKeysUnlimitedBatchSize"
|
|
||||||
)}
|
|
||||||
</FormLabel>
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="validUntil"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>
|
|
||||||
{t("provisioningKeysValidUntil")}
|
|
||||||
</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="datetime-local"
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormDescription>
|
|
||||||
{t("provisioningKeysValidUntilHint")}
|
|
||||||
</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
</CredenzaBody>
|
|
||||||
<CredenzaFooter>
|
|
||||||
<CredenzaClose asChild>
|
|
||||||
<Button variant="outline">{t("close")}</Button>
|
|
||||||
</CredenzaClose>
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
form={FORM_ID}
|
|
||||||
loading={loading}
|
|
||||||
disabled={loading}
|
|
||||||
>
|
|
||||||
{t("save")}
|
|
||||||
</Button>
|
|
||||||
</CredenzaFooter>
|
|
||||||
</CredenzaContent>
|
|
||||||
</Credenza>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -93,7 +93,7 @@ export function LayoutMobileMenu({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<span className="flex-shrink-0 w-5 h-5 flex items-center justify-center text-muted-foreground mr-3">
|
<span className="flex-shrink-0 mr-2">
|
||||||
<Server className="h-4 w-4" />
|
<Server className="h-4 w-4" />
|
||||||
</span>
|
</span>
|
||||||
<span className="flex-1">
|
<span className="flex-1">
|
||||||
|
|||||||
@@ -169,8 +169,8 @@ export function LayoutSidebar({
|
|||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex-shrink-0 w-5 h-5 flex items-center justify-center text-muted-foreground",
|
"shrink-0",
|
||||||
!isSidebarCollapsed && "mr-3"
|
!isSidebarCollapsed && "mr-2"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<Server className="h-4 w-4" />
|
<Server className="h-4 w-4" />
|
||||||
@@ -222,34 +222,36 @@ export function LayoutSidebar({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className="pt-1 flex flex-col shrink-0 gap-2 w-full border-t border-border">
|
<div className="w-full border-t border-border mb-3" />
|
||||||
|
|
||||||
|
<div className="p-4 pt-1 flex flex-col shrink-0">
|
||||||
{canShowProductUpdates && (
|
{canShowProductUpdates && (
|
||||||
<div className="px-4">
|
<div className="mb-3 empty:mb-0">
|
||||||
<ProductUpdates isCollapsed={isSidebarCollapsed} />
|
<ProductUpdates isCollapsed={isSidebarCollapsed} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{build === "enterprise" && (
|
{build === "enterprise" && (
|
||||||
<div className="px-4">
|
<div className="mb-3 empty:mb-0">
|
||||||
<SidebarLicenseButton
|
<SidebarLicenseButton
|
||||||
isCollapsed={isSidebarCollapsed}
|
isCollapsed={isSidebarCollapsed}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{build === "oss" && (
|
{build === "oss" && (
|
||||||
<div className="px-4">
|
<div className="mb-3 empty:mb-0">
|
||||||
<SupporterStatus isCollapsed={isSidebarCollapsed} />
|
<SupporterStatus isCollapsed={isSidebarCollapsed} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{build === "saas" && (
|
{build === "saas" && (
|
||||||
<div className="px-4">
|
<div className="mb-3 empty:mb-0">
|
||||||
<SidebarSupportButton
|
<SidebarSupportButton
|
||||||
isCollapsed={isSidebarCollapsed}
|
isCollapsed={isSidebarCollapsed}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{!isSidebarCollapsed && (
|
{!isSidebarCollapsed && (
|
||||||
<div className="px-4 space-y-2 pb-4">
|
<div className="space-y-2">
|
||||||
{loadFooterLinks() ? (
|
{loadFooterLinks() ? (
|
||||||
<>
|
<>
|
||||||
{loadFooterLinks()!.map((link, index) => (
|
{loadFooterLinks()!.map((link, index) => (
|
||||||
|
|||||||
@@ -192,13 +192,13 @@ function ProductUpdatesListPopup({
|
|||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"relative z-1 cursor-pointer block group",
|
"relative z-1 cursor-pointer block group",
|
||||||
"rounded-md border bg-secondary p-2 py-3 w-full flex flex-col gap-2 text-sm",
|
"rounded-md border border-primary/30 bg-linear-to-br dark:from-primary/20 from-primary/20 via-background to-background p-2 py-3 w-full flex flex-col gap-2 text-sm",
|
||||||
"transition duration-300 ease-in-out",
|
"transition duration-300 ease-in-out",
|
||||||
"data-closed:opacity-0 data-closed:translate-y-full"
|
"data-closed:opacity-0 data-closed:translate-y-full"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<BellIcon className="flex-none size-4" />
|
<BellIcon className="flex-none size-4 text-primary" />
|
||||||
<div className="flex justify-between items-center flex-1">
|
<div className="flex justify-between items-center flex-1">
|
||||||
<p className="font-medium text-start">
|
<p className="font-medium text-start">
|
||||||
{t("productUpdateWhatsNew")}
|
{t("productUpdateWhatsNew")}
|
||||||
@@ -346,13 +346,13 @@ function NewVersionAvailable({
|
|||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
className={cn(
|
className={cn(
|
||||||
"relative z-2 group cursor-pointer block",
|
"relative z-2 group cursor-pointer block",
|
||||||
"rounded-md border bg-secondary p-2 py-3 w-full flex flex-col gap-2 text-sm",
|
"rounded-md border border-primary/30 bg-linear-to-br dark:from-primary/20 from-primary/20 via-background to-background p-2 py-3 w-full flex flex-col gap-2 text-sm",
|
||||||
"transition duration-300 ease-in-out",
|
"transition duration-300 ease-in-out",
|
||||||
"data-closed:opacity-0 data-closed:translate-y-full"
|
"data-closed:opacity-0 data-closed:translate-y-full"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<RocketIcon className="flex-none size-4" />
|
<RocketIcon className="flex-none size-4 text-primary" />
|
||||||
<p className="font-medium flex-1">
|
<p className="font-medium flex-1">
|
||||||
{t("pangolinUpdateAvailable")}
|
{t("pangolinUpdateAvailable")}
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -1,320 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import {
|
|
||||||
DataTable,
|
|
||||||
ExtendedColumnDef
|
|
||||||
} from "@app/components/ui/data-table";
|
|
||||||
import {
|
|
||||||
DropdownMenu,
|
|
||||||
DropdownMenuContent,
|
|
||||||
DropdownMenuItem,
|
|
||||||
DropdownMenuTrigger
|
|
||||||
} from "@app/components/ui/dropdown-menu";
|
|
||||||
import { Button } from "@app/components/ui/button";
|
|
||||||
import { ArrowUpDown, MoreHorizontal } from "lucide-react";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import CreateSiteProvisioningKeyCredenza from "@app/components/CreateSiteProvisioningKeyCredenza";
|
|
||||||
import EditSiteProvisioningKeyCredenza from "@app/components/EditSiteProvisioningKeyCredenza";
|
|
||||||
import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog";
|
|
||||||
import { toast } from "@app/hooks/useToast";
|
|
||||||
import { formatAxiosError } from "@app/lib/api";
|
|
||||||
import { createApiClient } from "@app/lib/api";
|
|
||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
|
||||||
import { usePaidStatus } from "@app/hooks/usePaidStatus";
|
|
||||||
import moment from "moment";
|
|
||||||
import { useTranslations } from "next-intl";
|
|
||||||
import { build } from "@server/build";
|
|
||||||
import { TierFeature, tierMatrix } from "@server/lib/billing/tierMatrix";
|
|
||||||
|
|
||||||
export type SiteProvisioningKeyRow = {
|
|
||||||
id: string;
|
|
||||||
key: string;
|
|
||||||
name: string;
|
|
||||||
createdAt: string;
|
|
||||||
lastUsed: string | null;
|
|
||||||
maxBatchSize: number | null;
|
|
||||||
numUsed: number;
|
|
||||||
validUntil: string | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
type SiteProvisioningKeysTableProps = {
|
|
||||||
keys: SiteProvisioningKeyRow[];
|
|
||||||
orgId: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function SiteProvisioningKeysTable({
|
|
||||||
keys,
|
|
||||||
orgId
|
|
||||||
}: SiteProvisioningKeysTableProps) {
|
|
||||||
const router = useRouter();
|
|
||||||
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
|
||||||
const [selected, setSelected] = useState<SiteProvisioningKeyRow | null>(
|
|
||||||
null
|
|
||||||
);
|
|
||||||
const [rows, setRows] = useState<SiteProvisioningKeyRow[]>(keys);
|
|
||||||
const api = createApiClient(useEnvContext());
|
|
||||||
const t = useTranslations();
|
|
||||||
const { isPaidUser } = usePaidStatus();
|
|
||||||
const canUseSiteProvisioning =
|
|
||||||
isPaidUser(tierMatrix[TierFeature.SiteProvisioningKeys]) &&
|
|
||||||
build !== "oss";
|
|
||||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
|
||||||
const [createOpen, setCreateOpen] = useState(false);
|
|
||||||
const [editOpen, setEditOpen] = useState(false);
|
|
||||||
const [editingKey, setEditingKey] =
|
|
||||||
useState<SiteProvisioningKeyRow | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setRows(keys);
|
|
||||||
}, [keys]);
|
|
||||||
|
|
||||||
const refreshData = async () => {
|
|
||||||
setIsRefreshing(true);
|
|
||||||
try {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
||||||
router.refresh();
|
|
||||||
} catch (error) {
|
|
||||||
toast({
|
|
||||||
title: t("error"),
|
|
||||||
description: t("refreshError"),
|
|
||||||
variant: "destructive"
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
setIsRefreshing(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteKey = async (siteProvisioningKeyId: string) => {
|
|
||||||
try {
|
|
||||||
await api.delete(
|
|
||||||
`/org/${orgId}/site-provisioning-key/${siteProvisioningKeyId}`
|
|
||||||
);
|
|
||||||
router.refresh();
|
|
||||||
setIsDeleteModalOpen(false);
|
|
||||||
setSelected(null);
|
|
||||||
setRows((prev) => prev.filter((row) => row.id !== siteProvisioningKeyId));
|
|
||||||
} catch (e) {
|
|
||||||
console.error(t("provisioningKeysErrorDelete"), e);
|
|
||||||
toast({
|
|
||||||
variant: "destructive",
|
|
||||||
title: t("provisioningKeysErrorDelete"),
|
|
||||||
description: formatAxiosError(
|
|
||||||
e,
|
|
||||||
t("provisioningKeysErrorDeleteMessage")
|
|
||||||
)
|
|
||||||
});
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns: ExtendedColumnDef<SiteProvisioningKeyRow>[] = [
|
|
||||||
{
|
|
||||||
accessorKey: "name",
|
|
||||||
enableHiding: false,
|
|
||||||
friendlyName: t("name"),
|
|
||||||
header: ({ column }) => {
|
|
||||||
return (
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
onClick={() =>
|
|
||||||
column.toggleSorting(column.getIsSorted() === "asc")
|
|
||||||
}
|
|
||||||
>
|
|
||||||
{t("name")}
|
|
||||||
<ArrowUpDown className="ml-2 h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "key",
|
|
||||||
friendlyName: t("key"),
|
|
||||||
header: () => <span className="p-3">{t("key")}</span>,
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const r = row.original;
|
|
||||||
return <span className="font-mono">{r.key}</span>;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "maxBatchSize",
|
|
||||||
friendlyName: t("provisioningKeysMaxBatchSize"),
|
|
||||||
header: () => (
|
|
||||||
<span className="p-3">{t("provisioningKeysMaxBatchSize")}</span>
|
|
||||||
),
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const r = row.original;
|
|
||||||
return (
|
|
||||||
<span>
|
|
||||||
{r.maxBatchSize == null
|
|
||||||
? t("provisioningKeysMaxBatchUnlimited")
|
|
||||||
: r.maxBatchSize}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "numUsed",
|
|
||||||
friendlyName: t("provisioningKeysNumUsed"),
|
|
||||||
header: () => (
|
|
||||||
<span className="p-3">{t("provisioningKeysNumUsed")}</span>
|
|
||||||
),
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const r = row.original;
|
|
||||||
return <span>{r.numUsed}</span>;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "validUntil",
|
|
||||||
friendlyName: t("provisioningKeysValidUntil"),
|
|
||||||
header: () => (
|
|
||||||
<span className="p-3">{t("provisioningKeysValidUntil")}</span>
|
|
||||||
),
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const r = row.original;
|
|
||||||
return (
|
|
||||||
<span>
|
|
||||||
{r.validUntil
|
|
||||||
? moment(r.validUntil).format("lll")
|
|
||||||
: t("provisioningKeysNoExpiry")}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "lastUsed",
|
|
||||||
friendlyName: t("provisioningKeysLastUsed"),
|
|
||||||
header: () => (
|
|
||||||
<span className="p-3">{t("provisioningKeysLastUsed")}</span>
|
|
||||||
),
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const r = row.original;
|
|
||||||
return (
|
|
||||||
<span>
|
|
||||||
{r.lastUsed
|
|
||||||
? moment(r.lastUsed).format("lll")
|
|
||||||
: t("provisioningKeysNeverUsed")}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: "createdAt",
|
|
||||||
friendlyName: t("createdAt"),
|
|
||||||
header: () => <span className="p-3">{t("createdAt")}</span>,
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const r = row.original;
|
|
||||||
return <span>{moment(r.createdAt).format("lll")}</span>;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "actions",
|
|
||||||
enableHiding: false,
|
|
||||||
header: () => <span className="p-3"></span>,
|
|
||||||
cell: ({ row }) => {
|
|
||||||
const r = row.original;
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-2 justify-end">
|
|
||||||
<DropdownMenu>
|
|
||||||
<DropdownMenuTrigger asChild>
|
|
||||||
<Button variant="ghost" className="h-8 w-8 p-0">
|
|
||||||
<span className="sr-only">
|
|
||||||
{t("openMenu")}
|
|
||||||
</span>
|
|
||||||
<MoreHorizontal className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</DropdownMenuTrigger>
|
|
||||||
<DropdownMenuContent align="end">
|
|
||||||
<DropdownMenuItem
|
|
||||||
disabled={!canUseSiteProvisioning}
|
|
||||||
onClick={() => {
|
|
||||||
setEditingKey(r);
|
|
||||||
setEditOpen(true);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{t("edit")}
|
|
||||||
</DropdownMenuItem>
|
|
||||||
<DropdownMenuItem
|
|
||||||
disabled={!canUseSiteProvisioning}
|
|
||||||
onClick={() => {
|
|
||||||
setSelected(r);
|
|
||||||
setIsDeleteModalOpen(true);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<span className="text-red-500">
|
|
||||||
{t("delete")}
|
|
||||||
</span>
|
|
||||||
</DropdownMenuItem>
|
|
||||||
</DropdownMenuContent>
|
|
||||||
</DropdownMenu>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<CreateSiteProvisioningKeyCredenza
|
|
||||||
open={createOpen}
|
|
||||||
setOpen={setCreateOpen}
|
|
||||||
orgId={orgId}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<EditSiteProvisioningKeyCredenza
|
|
||||||
open={editOpen}
|
|
||||||
setOpen={(v) => {
|
|
||||||
setEditOpen(v);
|
|
||||||
if (!v) {
|
|
||||||
setEditingKey(null);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
orgId={orgId}
|
|
||||||
provisioningKey={editingKey}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{selected && (
|
|
||||||
<ConfirmDeleteDialog
|
|
||||||
open={isDeleteModalOpen}
|
|
||||||
setOpen={(val) => {
|
|
||||||
setIsDeleteModalOpen(val);
|
|
||||||
if (!val) {
|
|
||||||
setSelected(null);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
dialog={
|
|
||||||
<div className="space-y-2">
|
|
||||||
<p>{t("provisioningKeysQuestionRemove")}</p>
|
|
||||||
<p>{t("provisioningKeysMessageRemove")}</p>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
buttonText={t("provisioningKeysDeleteConfirm")}
|
|
||||||
onConfirm={async () => deleteKey(selected.id)}
|
|
||||||
string={selected.name}
|
|
||||||
title={t("provisioningKeysDelete")}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<DataTable
|
|
||||||
columns={columns}
|
|
||||||
data={rows}
|
|
||||||
persistPageSize="Org-provisioning-keys-table"
|
|
||||||
title={t("provisioningKeys")}
|
|
||||||
searchPlaceholder={t("searchProvisioningKeys")}
|
|
||||||
searchColumn="name"
|
|
||||||
onAdd={() => {
|
|
||||||
if (canUseSiteProvisioning) {
|
|
||||||
setCreateOpen(true);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
addButtonDisabled={!canUseSiteProvisioning}
|
|
||||||
onRefresh={refreshData}
|
|
||||||
isRefreshing={isRefreshing}
|
|
||||||
addButtonText={t("provisioningKeysAdd")}
|
|
||||||
enableColumnVisibility={true}
|
|
||||||
stickyLeftColumn="name"
|
|
||||||
stickyRightColumn="actions"
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -171,7 +171,6 @@ type DataTableProps<TData, TValue> = {
|
|||||||
title?: string;
|
title?: string;
|
||||||
addButtonText?: string;
|
addButtonText?: string;
|
||||||
onAdd?: () => void;
|
onAdd?: () => void;
|
||||||
addButtonDisabled?: boolean;
|
|
||||||
onRefresh?: () => void;
|
onRefresh?: () => void;
|
||||||
isRefreshing?: boolean;
|
isRefreshing?: boolean;
|
||||||
searchPlaceholder?: string;
|
searchPlaceholder?: string;
|
||||||
@@ -204,7 +203,6 @@ export function DataTable<TData, TValue>({
|
|||||||
title,
|
title,
|
||||||
addButtonText,
|
addButtonText,
|
||||||
onAdd,
|
onAdd,
|
||||||
addButtonDisabled = false,
|
|
||||||
onRefresh,
|
onRefresh,
|
||||||
isRefreshing,
|
isRefreshing,
|
||||||
searchPlaceholder = "Search...",
|
searchPlaceholder = "Search...",
|
||||||
@@ -637,7 +635,7 @@ export function DataTable<TData, TValue>({
|
|||||||
)}
|
)}
|
||||||
{onAdd && addButtonText && (
|
{onAdd && addButtonText && (
|
||||||
<div>
|
<div>
|
||||||
<Button onClick={onAdd} disabled={addButtonDisabled}>
|
<Button onClick={onAdd}>
|
||||||
<Plus className="mr-2 h-4 w-4" />
|
<Plus className="mr-2 h-4 w-4" />
|
||||||
{addButtonText}
|
{addButtonText}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user