-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(api): separate rate limit budget for deployment endpoints #4565
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: improvement | ||
| --- | ||
|
|
||
| Deployment-related API endpoints now draw from their own generous rate limit budget, configurable via the `DEPLOYMENT_RATE_LIMIT_*` environment variables, so runtime API traffic no longer competes with deployments for the same per-environment budget. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| // Deploy-flow endpoints, rate limited by deploymentRateLimiter with a separate | ||
| // budget instead of the general per-environment buckets. | ||
| export const deploymentApiPaths: (RegExp | string)[] = [ | ||
| // /current is runtime SDK surface, kept out of the deploy budget | ||
| /^\/api\/v\d+\/deployments(?!\/current$)(\/|$)/, | ||
| /^\/api\/v1\/projects\/[^/]+\/(dev|staging|prod|preview)$/, | ||
| /^\/api\/v1\/projects\/[^/]+\/envvars$/, | ||
| /^\/api\/v1\/projects\/[^/]+\/envvars\/[^/]+\/import$/, | ||
| /^\/api\/v1\/projects\/[^/]+\/branches$/, | ||
| /^\/api\/v1\/projects\/[^/]+\/branches\/archive$/, | ||
| "/api/v1/remote-build-provider-status", | ||
| "/api/v1/artifacts", | ||
| ]; | ||
|
myftija marked this conversation as resolved.
|
||
This file contains hidden or 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,53 @@ | ||
| import { env } from "~/env.server"; | ||
| import { resolvePrivateApiKeyRateLimitScope } from "~/models/runtimeEnvironment.server"; | ||
| import { authorizationRateLimitMiddleware } from "./authorizationRateLimitMiddleware.server"; | ||
| import { deploymentApiPaths } from "./deploymentApiPaths.server"; | ||
| import type { Duration } from "./rateLimiter.server"; | ||
|
|
||
| export const deploymentRateLimiter = authorizationRateLimitMiddleware({ | ||
| redis: { | ||
| port: env.RATE_LIMIT_REDIS_PORT, | ||
| host: env.RATE_LIMIT_REDIS_HOST, | ||
| username: env.RATE_LIMIT_REDIS_USERNAME, | ||
| password: env.RATE_LIMIT_REDIS_PASSWORD, | ||
| tlsDisabled: env.RATE_LIMIT_REDIS_TLS_DISABLED === "true", | ||
| clusterMode: env.RATE_LIMIT_REDIS_CLUSTER_MODE_ENABLED === "1", | ||
| }, | ||
| keyPrefix: "deployment", | ||
| defaultLimiter: { | ||
| type: "tokenBucket", | ||
| refillRate: env.DEPLOYMENT_RATE_LIMIT_REFILL_RATE, | ||
| interval: env.DEPLOYMENT_RATE_LIMIT_REFILL_INTERVAL as Duration, | ||
| maxTokens: env.DEPLOYMENT_RATE_LIMIT_MAX, | ||
| }, | ||
| limiterCache: { | ||
| fresh: 60_000 * 10, | ||
| stale: 60_000 * 20, | ||
| maxItems: 1000, | ||
| }, | ||
| limiterConfigOverride: async (authorizationValue) => { | ||
| const rawApiKey = authorizationValue.replace(/^Bearer /, ""); | ||
|
|
||
| if (!rawApiKey.startsWith("tr_")) { | ||
| return; | ||
| } | ||
|
|
||
| const scope = await resolvePrivateApiKeyRateLimitScope(rawApiKey); | ||
|
|
||
| if (!scope) { | ||
| return; | ||
| } | ||
|
|
||
| // Identifier only: the org's apiRateLimiterConfig governs the general API | ||
| // limiter, not the deploy budget. | ||
| return { | ||
| identifier: scope.environmentId, | ||
| }; | ||
| }, | ||
| pathMatchers: deploymentApiPaths, | ||
| log: { | ||
| rejections: env.DEPLOYMENT_RATE_LIMIT_REJECTION_LOGS_ENABLED === "1", | ||
| requests: env.DEPLOYMENT_RATE_LIMIT_REQUEST_LOGS_ENABLED === "1", | ||
| limiter: env.DEPLOYMENT_RATE_LIMIT_LIMITER_LOGS_ENABLED === "1", | ||
| }, | ||
| }); |
This file contains hidden or 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 hidden or 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,72 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { deploymentApiPaths } from "../app/services/deploymentApiPaths.server.js"; | ||
|
|
||
| // Same matching semantics as authorizationRateLimitMiddleware's pathMatchers/pathWhiteList | ||
| function matchesAnyPath(path: string, matchers: (RegExp | string)[]): boolean { | ||
| return matchers.some((matcher) => | ||
| matcher instanceof RegExp ? matcher.test(path) : path === matcher | ||
| ); | ||
| } | ||
|
|
||
| describe("deploymentApiPaths", () => { | ||
| it("matches every endpoint the deploy flow calls", () => { | ||
| const deployFlowPaths = [ | ||
| "/api/v1/deployments", | ||
| "/api/v1/deployments/latest", | ||
| "/api/v1/deployments/deployment_123", | ||
| "/api/v1/deployments/deployment_123/progress", | ||
| "/api/v1/deployments/deployment_123/fail", | ||
| "/api/v1/deployments/deployment_123/cancel", | ||
| "/api/v1/deployments/deployment_123/background-workers", | ||
| "/api/v1/deployments/deployment_123/generate-registry-credentials", | ||
| "/api/v1/deployments/20260811.1/promote", | ||
| "/api/v3/deployments/deployment_123/finalize", | ||
| "/api/v1/projects/proj_abc123/dev", | ||
| "/api/v1/projects/proj_abc123/staging", | ||
| "/api/v1/projects/proj_abc123/prod", | ||
| "/api/v1/projects/proj_abc123/preview", | ||
| "/api/v1/projects/proj_abc123/envvars", | ||
| "/api/v1/projects/proj_abc123/envvars/prod/import", | ||
| "/api/v1/projects/proj_abc123/branches", | ||
| "/api/v1/projects/proj_abc123/branches/archive", | ||
| "/api/v1/remote-build-provider-status", | ||
| "/api/v1/artifacts", | ||
| ]; | ||
|
|
||
| for (const path of deployFlowPaths) { | ||
| expect( | ||
| matchesAnyPath(path, deploymentApiPaths), | ||
| `expected ${path} to be a deployment API path` | ||
| ).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| it("does not match runtime API surface", () => { | ||
| const runtimePaths = [ | ||
| "/api/v1/deployments/current", | ||
| "/api/v1/deploymentsfoo", | ||
| "/api/v1/whoami", | ||
| "/api/v2/whoami", | ||
| "/api/v1/tasks/my-task/trigger", | ||
| "/api/v1/tasks/batch", | ||
| "/api/v2/runs/run_123", | ||
| "/api/v1/runs/run_123/replay", | ||
| "/api/v3/runs/run_123/trace", | ||
| "/api/v1/projects", | ||
| "/api/v1/projects/proj_abc123", | ||
| "/api/v1/projects/proj_abc123/dev-status", | ||
| "/api/v1/projects/proj_abc123/prod/jwt", | ||
| "/api/v1/projects/proj_abc123/envvars/prod", | ||
| "/api/v1/projects/proj_abc123/envvars/prod/MY_VAR", | ||
| "/api/v1/schedules", | ||
| "/api/v1/queues/queue_123", | ||
| ]; | ||
|
|
||
| for (const path of runtimePaths) { | ||
| expect( | ||
| matchesAnyPath(path, deploymentApiPaths), | ||
| `expected ${path} not to be a deployment API path` | ||
| ).toBe(false); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.