Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added Ratelimit to dashboard #2076

Merged
merged 18 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
12e849d
added ratelimit to dashboard
MichaelUnkey Sep 10, 2024
d07a80a
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 10, 2024
59edb75
consolidate ratelimit for dashboard
MichaelUnkey Sep 16, 2024
2770f8a
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 16, 2024
3885e73
updated import path
MichaelUnkey Sep 16, 2024
f966210
Merge branch 'eng-1314-add-ratelimiting-to-our-trpc-routes' of https:…
MichaelUnkey Sep 16, 2024
bbea079
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 16, 2024
964ce0f
fixed with no root key
MichaelUnkey Sep 17, 2024
98efcc4
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 17, 2024
da21fbc
remove extra data returned
MichaelUnkey Sep 17, 2024
4430f4b
Merge branch 'eng-1314-add-ratelimiting-to-our-trpc-routes' of https:…
MichaelUnkey Sep 17, 2024
4531420
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 17, 2024
90dd410
Merge branch 'main' into eng-1314-add-ratelimiting-to-our-trpc-routes
MichaelUnkey Sep 17, 2024
658dd01
Merge branch 'main' of https://github.com/unkeyed/unkey into eng-1314…
MichaelUnkey Sep 17, 2024
a60e83d
Merge branch 'main' into eng-1314-add-ratelimiting-to-our-trpc-routes
MichaelUnkey Sep 18, 2024
251eb48
Merge branch 'main' of https://github.com/unkeyed/unkey into eng-1314…
MichaelUnkey Sep 18, 2024
2d89ecd
Merge branch 'eng-1314-add-ratelimiting-to-our-trpc-routes' of https:…
MichaelUnkey Sep 18, 2024
c6e26bf
docs: explain permissions and use of key
chronark Sep 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/dashboard/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const env = () =>
AGENT_TOKEN: z.string(),

GITHUB_KEYS_URI: z.string().optional(),

UNKEY_ROOT_KEY: z.string(),
MichaelUnkey marked this conversation as resolved.
Show resolved Hide resolved
})
.parse(process.env);

Expand Down
10 changes: 10 additions & 0 deletions apps/dashboard/lib/ratelimitValues.ts
MichaelUnkey marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Values for route types

export const CREATE_LIMIT = 5;
export const CREATE_LIMIT_DURATION = 3;
MichaelUnkey marked this conversation as resolved.
Show resolved Hide resolved

export const UPDATE_LIMIT = 25;
export const UPDATE_LIMIT_DURATION = 5;

export const DELETE_LIMIT = 5;
export const DELETE_LIMIT_DURATION = 3;
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { db, schema } from "@/lib/db";
import { CREATE_LIMIT, CREATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { newId } from "@unkey/id";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const createApi = t.procedure
.use(auth)
export const createApi = rateLimitedProcedure({
limit: CREATE_LIMIT,
duration: CREATE_LIMIT_DURATION,
})
.input(
z.object({
name: z
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/api/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { db, eq, schema } from "@/lib/db";
import { DELETE_LIMIT, DELETE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const deleteApi = t.procedure
.use(auth)
export const deleteApi = rateLimitedProcedure({
limit: DELETE_LIMIT,
duration: DELETE_LIMIT_DURATION,
})
.input(
z.object({
apiId: z.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateAPIDeleteProtection = t.procedure
.use(auth)
export const updateAPIDeleteProtection = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
apiId: z.string(),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateApiIpWhitelist = t.procedure
.use(auth)
export const updateApiIpWhitelist = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
ipWhitelist: z
Expand Down
10 changes: 6 additions & 4 deletions apps/dashboard/lib/trpc/routers/api/updateName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { th } from "@faker-js/faker";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateApiName = t.procedure
.use(auth)
export const updateApiName = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
name: z.string().min(3, "API names must contain at least 3 characters"),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/gateway/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { db } from "@/lib/db";
import { CREATE_LIMIT, CREATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { TRPCError } from "@trpc/server";
import { newId } from "@unkey/id";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const createGateway = t.procedure
.use(auth)
export const createGateway = rateLimitedProcedure({
limit: CREATE_LIMIT,
duration: CREATE_LIMIT_DURATION,
})
.input(
z.object({
subdomain: z
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { db, schema } from "@/lib/db";
import { CREATE_LIMIT, CREATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { newId } from "@unkey/id";
import { newKey } from "@unkey/keys";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const createKey = t.procedure
.use(auth)
export const createKey = rateLimitedProcedure({
limit: CREATE_LIMIT,
duration: CREATE_LIMIT_DURATION,
})
.input(
z.object({
prefix: z.string().optional(),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/createRootKey.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { type Permission, db, eq, schema } from "@/lib/db";
import { env } from "@/lib/env";
import { CREATE_LIMIT, CREATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { type UnkeyAuditLog, ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { newId } from "@unkey/id";
import { newKey } from "@unkey/keys";
import { unkeyPermissionValidation } from "@unkey/rbac";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";
import { upsertPermissions } from "../rbac";

export const createRootKey = t.procedure
.use(auth)
export const createRootKey = rateLimitedProcedure({
limit: CREATE_LIMIT,
duration: CREATE_LIMIT_DURATION,
})
.input(
z.object({
name: z.string().optional(),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/delete.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { and, db, eq, inArray, schema } from "@/lib/db";
import { DELETE_LIMIT, DELETE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const deleteKeys = t.procedure
.use(auth)
export const deleteKeys = rateLimitedProcedure({
limit: DELETE_LIMIT,
duration: DELETE_LIMIT_DURATION,
})
.input(
z.object({
keyIds: z.array(z.string()),
Expand Down
11 changes: 7 additions & 4 deletions apps/dashboard/lib/trpc/routers/key/deleteRootKey.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { and, db, eq, inArray, isNotNull, schema } from "@/lib/db";
import { db, inArray, schema } from "@/lib/db";
import { env } from "@/lib/env";
import { DELETE_LIMIT, DELETE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const deleteRootKeys = t.procedure
.use(auth)
export const deleteRootKeys = rateLimitedProcedure({
limit: DELETE_LIMIT,
duration: DELETE_LIMIT_DURATION,
})
.input(
z.object({
keyIds: z.array(z.string()),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/updateEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateKeyEnabled = t.procedure
.use(auth)
export const updateKeyEnabled = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
keyId: z.string(),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/updateExpiration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateKeyExpiration = t.procedure
.use(auth)
export const updateKeyExpiration = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
keyId: z.string(),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/updateMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateKeyMetadata = t.procedure
.use(auth)
export const updateKeyMetadata = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
keyId: z.string(),
Expand Down
7 changes: 6 additions & 1 deletion apps/dashboard/lib/trpc/routers/key/updateName.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateKeyName = t.procedure
export const updateKeyName = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.use(auth)
.input(
z.object({
Expand Down
7 changes: 6 additions & 1 deletion apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateKeyOwnerId = t.procedure
export const updateKeyOwnerId = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.use(auth)
.input(
z.object({
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/updateRatelimit.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateKeyRatelimit = t.procedure
.use(auth)
export const updateKeyRatelimit = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
keyId: z.string(),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/key/updateRemaining.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { db, eq, schema } from "@/lib/db";
import { UPDATE_LIMIT, UPDATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const updateKeyRemaining = t.procedure
.use(auth)
export const updateKeyRemaining = rateLimitedProcedure({
limit: UPDATE_LIMIT,
duration: UPDATE_LIMIT_DURATION,
})
.input(
z.object({
keyId: z.string(),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/llmGateway/create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { db, schema } from "@/lib/db";
import { CREATE_LIMIT, CREATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { DatabaseError } from "@planetscale/database";
import { TRPCError } from "@trpc/server";
import { newId } from "@unkey/id";
import { z } from "zod";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const createLlmGateway = t.procedure
.use(auth)
export const createLlmGateway = rateLimitedProcedure({
limit: CREATE_LIMIT,
duration: CREATE_LIMIT_DURATION,
})
.input(
z.object({
subdomain: z.string().min(1).max(50),
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/lib/trpc/routers/llmGateway/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { db, eq, schema } from "@/lib/db";
import { DELETE_LIMIT, DELETE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { auth, t } from "../../trpc";
import { rateLimitedProcedure } from "../../trpc";

export const deleteLlmGateway = t.procedure
.use(auth)
export const deleteLlmGateway = rateLimitedProcedure({
limit: DELETE_LIMIT,
duration: DELETE_LIMIT_DURATION,
})
.input(z.object({ gatewayId: z.string() }))
.mutation(async ({ ctx, input }) => {
const llmGateway = await db.query.llmGateways.findFirst({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { type Webhook, db, schema } from "@/lib/db";
import { CREATE_LIMIT, CREATE_LIMIT_DURATION } from "@/lib/ratelimitValues";
import { ingestAuditLogs } from "@/lib/tinybird";
import { TRPCError, createCallerFactory } from "@trpc/server";
import { newId } from "@unkey/id";
import { z } from "zod";
import { router } from "../..";
import { auth, t } from "../../../trpc";
import { rateLimitedProcedure } from "../../../trpc";

export const createVerificationMonitor = t.procedure
.use(auth)
export const createVerificationMonitor = rateLimitedProcedure({
limit: CREATE_LIMIT,
duration: CREATE_LIMIT_DURATION,
})
.input(
z.object({
interval: z
Expand Down
Loading
Loading