-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mostly just duplicate audit logs and other clickhouse stuff (#2134
- Loading branch information
1 parent
a1528d1
commit 99cd54d
Showing
124 changed files
with
3,818 additions
and
3,852 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
apps/agent/pkg/clickhouse/schema/003_create_raw_telemetry_sdks_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- +goose up | ||
CREATE TABLE default.raw_telemetry_sdks_v1( | ||
-- the api request id, so we can correlate the telemetry with traces and logs | ||
request_id String, | ||
|
||
-- unix milli | ||
time Int64, | ||
|
||
-- ie: node@20 | ||
runtime String, | ||
-- ie: vercel | ||
platform String, | ||
|
||
-- ie: [ "@unkey/api@1.2.3", "@unkey/ratelimit@4.5.6" ] | ||
versions Array(String) | ||
) | ||
ENGINE = MergeTree() | ||
ORDER BY (request_id, time) | ||
; |
14 changes: 14 additions & 0 deletions
14
apps/agent/pkg/clickhouse/schema/004_create_key_verifications_per_day.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- +goose up | ||
CREATE TABLE default.key_verifications_per_day_v1 | ||
( | ||
time DateTime, | ||
workspace_id String, | ||
key_space_id String, | ||
identity_id String, | ||
key_id String, | ||
outcome LowCardinality(String), | ||
count AggregateFunction(count, UInt64) | ||
) | ||
ENGINE = AggregatingMergeTree() | ||
ORDER BY (workspace_id, key_space_id, time, identity_id, key_id) | ||
; |
19 changes: 19 additions & 0 deletions
19
apps/agent/pkg/clickhouse/schema/005_create_mv_key_verifications_per_day.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- +goose up | ||
CREATE MATERIALIZED VIEW default.mv_key_verifications_per_day_v1 TO default.key_verifications_per_day_v1 AS | ||
SELECT | ||
workspace_id, | ||
key_space_id, | ||
identity_id, | ||
key_id, | ||
outcome, | ||
countState() as count, | ||
toStartOfDay(fromUnixTimestamp64Milli(time)) AS time | ||
FROM default.raw_key_verifications_v1 | ||
GROUP BY | ||
workspace_id, | ||
key_space_id, | ||
identity_id, | ||
key_id, | ||
outcome, | ||
time | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import type { Context } from "@/pkg/hono/app"; | ||
import { auditLogSchemaV1, unkeyAuditLogEvents } from "@unkey/schema/src/auditlog"; | ||
import { type Transaction, schema } from "./db"; | ||
|
||
import { newId } from "@unkey/id"; | ||
import { z } from "zod"; | ||
import type { UnkeyAuditLog } from "./analytics"; | ||
|
||
export async function insertUnkeyAuditLog( | ||
c: Context, | ||
tx: Transaction | undefined, | ||
auditLogs: UnkeyAuditLog | Array<UnkeyAuditLog>, | ||
): Promise<void> { | ||
const schema = auditLogSchemaV1.merge( | ||
z.object({ | ||
event: unkeyAuditLogEvents, | ||
auditLogId: z.string().default(newId("auditLog")), | ||
bucket: z.string().default("unkey_mutations"), | ||
time: z.number().default(Date.now()), | ||
}), | ||
); | ||
|
||
const arr = Array.isArray(auditLogs) ? auditLogs : [auditLogs]; | ||
return insertGenericAuditLogs( | ||
c, | ||
tx, | ||
arr.map((l) => schema.parse(l)), | ||
); | ||
} | ||
|
||
export async function insertGenericAuditLogs( | ||
c: Context, | ||
tx: Transaction | undefined, | ||
auditLogs: z.infer<typeof auditLogSchemaV1> | z.infer<typeof auditLogSchemaV1>[], | ||
): Promise<void> { | ||
const arr = Array.isArray(auditLogs) ? auditLogs : [auditLogs]; | ||
|
||
if (arr.length === 0) { | ||
return; | ||
} | ||
|
||
const { cache, logger, db } = c.get("services"); | ||
|
||
for (const log of arr) { | ||
const { val: bucket, err } = await cache.auditLogBucketByWorkspaceIdAndName.swr( | ||
[log.workspaceId, log.bucket].join(":"), | ||
async () => { | ||
const bucket = await (tx ?? db.primary).query.auditLogBucket.findFirst({ | ||
where: (table, { eq, and }) => | ||
and(eq(table.workspaceId, log.workspaceId), eq(table.name, log.bucket)), | ||
}); | ||
if (!bucket) { | ||
return undefined; | ||
} | ||
return { | ||
id: bucket.id, | ||
}; | ||
}, | ||
); | ||
if (err) { | ||
logger.error("Could not find audit log bucket for workspace", { | ||
workspaceId: log.workspaceId, | ||
error: err.message, | ||
}); | ||
continue; | ||
} | ||
|
||
if (!bucket) { | ||
logger.error("Could not find audit log bucket for workspace", { | ||
workspaceId: log.workspaceId, | ||
}); | ||
continue; | ||
} | ||
|
||
const auditLogId = newId("auditLog"); | ||
await (tx ?? db.primary).insert(schema.auditLog).values({ | ||
id: auditLogId, | ||
workspaceId: log.workspaceId, | ||
bucketId: bucket.id, | ||
event: log.event, | ||
time: log.time, | ||
|
||
display: log.description ?? "", | ||
|
||
remoteIp: log.context?.location, | ||
|
||
userAgent: log.context?.userAgent, | ||
actorType: log.actor.type, | ||
actorId: log.actor.id, | ||
actorName: log.actor.name, | ||
actorMeta: log.actor.meta, | ||
}); | ||
await (tx ?? db.primary).insert(schema.auditLogTarget).values( | ||
log.resources.map((r) => ({ | ||
workspaceId: log.workspaceId, | ||
bucketId: bucket.id, | ||
auditLogId, | ||
displayName: r.name ?? "", | ||
type: r.type, | ||
id: r.id, | ||
name: r.name, | ||
meta: r.meta, | ||
})), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.