Skip to content

Commit 9c97889

Browse files
committed
add check for audit with public access and entitlement
1 parent 559633c commit 9c97889

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

packages/web/src/app/api/(server)/ee/audit/route.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { isServiceError } from "@/lib/utils";
66
import { serviceErrorResponse } from "@/lib/serviceError";
77
import { StatusCodes } from "http-status-codes";
88
import { ErrorCode } from "@/lib/errorCodes";
9+
import { env } from "@/env.mjs";
10+
import { getEntitlements } from "@/features/entitlements/server";
911

1012
export const GET = async (request: NextRequest) => {
1113
const domain = request.headers.get("X-Org-Domain");
@@ -18,6 +20,23 @@ export const GET = async (request: NextRequest) => {
1820
message: "Missing X-Org-Domain header",
1921
});
2022
}
23+
24+
if (env.SOURCEBOT_EE_AUDIT_LOGGING_ENABLED === 'false') {
25+
return serviceErrorResponse({
26+
statusCode: StatusCodes.FORBIDDEN,
27+
errorCode: ErrorCode.NOT_FOUND,
28+
message: "Audit logging is not enabled",
29+
});
30+
}
31+
32+
const entitlements = getEntitlements();
33+
if (!entitlements.includes('audit')) {
34+
return serviceErrorResponse({
35+
statusCode: StatusCodes.FORBIDDEN,
36+
errorCode: ErrorCode.NOT_FOUND,
37+
message: "Audit logging is not enabled for your license",
38+
});
39+
}
2140

2241
const result = await fetchAuditRecords(domain, apiKey);
2342
if (isServiceError(result)) {
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { IAuditService } from '@/ee/features/audit/types';
22
import { MockAuditService } from '@/ee/features/audit/mockAuditService';
3-
import { hasEntitlement } from '@/features/entitlements/server';
43
import { AuditService } from '@/ee/features/audit/auditService';
4+
import { env } from '@/env.mjs';
55

6-
let enterpriseService: IAuditService | null = null;
6+
let enterpriseService: IAuditService | undefined;
77

88
export function getAuditService(): IAuditService {
9-
if (hasEntitlement('audit')) {
10-
if (!enterpriseService) {
11-
enterpriseService = new AuditService();
12-
}
13-
return enterpriseService!;
14-
}
15-
return new MockAuditService();
9+
enterpriseService = enterpriseService ?? (env.SOURCEBOT_EE_AUDIT_LOGGING_ENABLED === 'true' ? new AuditService() : new MockAuditService());
10+
return enterpriseService;
1611
}

packages/web/src/env.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export const env = createEnv({
8484

8585
// EE License
8686
SOURCEBOT_EE_LICENSE_KEY: z.string().optional(),
87+
SOURCEBOT_EE_AUDIT_LOGGING_ENABLED: booleanSchema.default('false'),
8788

8889
// GitHub app for review agent
8990
GITHUB_APP_ID: z.string().optional(),

packages/web/src/initialize.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
1010
import { indexSchema } from '@sourcebot/schemas/v3/index.schema';
1111
import Ajv from 'ajv';
1212
import { syncSearchContexts } from '@/ee/features/searchContexts/syncSearchContexts';
13-
import { hasEntitlement } from '@/features/entitlements/server';
13+
import { getEntitlements, hasEntitlement } from '@/features/entitlements/server';
1414
import { createGuestUser, setPublicAccessStatus } from '@/ee/features/publicAccess/publicAccess';
1515
import { isServiceError } from './lib/utils';
1616
import { ServiceErrorException } from './lib/serviceError';
@@ -150,6 +150,11 @@ const syncDeclarativeConfig = async (configPath: string) => {
150150
}
151151

152152
if (hasPublicAccessEntitlement) {
153+
if (enablePublicAccess && env.SOURCEBOT_EE_AUDIT_LOGGING_ENABLED === 'true') {
154+
logger.error(`Audit logging is not supported when public access is enabled. Please disable audit logging or disable public access.`);
155+
process.exit(1);
156+
}
157+
153158
logger.info(`Setting public access status to ${!!enablePublicAccess} for org ${SINGLE_TENANT_ORG_DOMAIN}`);
154159
const res = await setPublicAccessStatus(SINGLE_TENANT_ORG_DOMAIN, !!enablePublicAccess);
155160
if (isServiceError(res)) {
@@ -186,6 +191,17 @@ const pruneOldGuestUser = async () => {
186191
}
187192
}
188193

194+
const validateEntitlements = () => {
195+
const entitlements = getEntitlements();
196+
197+
if (env.SOURCEBOT_EE_AUDIT_LOGGING_ENABLED === 'true') {
198+
if (!hasEntitlement('audit')) {
199+
logger.error(`Audit logging is enabled but your license does not include the audit logging entitlement. Please reach out to us to enquire about upgrading your license.`);
200+
process.exit(1);
201+
}
202+
}
203+
}
204+
189205
const initSingleTenancy = async () => {
190206
await prisma.org.upsert({
191207
where: {
@@ -203,6 +219,9 @@ const initSingleTenancy = async () => {
203219
// To keep things simple, we'll just delete the old guest user if it exists in the DB
204220
await pruneOldGuestUser();
205221

222+
// Startup time entitlement/environment variable validation
223+
validateEntitlements();
224+
206225
const hasPublicAccessEntitlement = hasEntitlement("public-access");
207226
if (hasPublicAccessEntitlement) {
208227
const res = await createGuestUser(SINGLE_TENANT_ORG_DOMAIN);

0 commit comments

Comments
 (0)