1
0
Fork 0
mirror of synced 2024-07-07 15:25:52 +12:00

Merge branch 'master' into features/enterprise-basic-plan

This commit is contained in:
Michael Drury 2023-11-08 18:50:24 +00:00 committed by GitHub
commit 4cc31f0375
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 33 deletions

View file

@ -27,51 +27,59 @@ interface KoaRateLimitOptions {
} }
const PREFIX = "/api/public/v1" const PREFIX = "/api/public/v1"
// allow a lot more requests when in test
const DEFAULT_API_REQ_LIMIT_PER_SEC = env.isTest() ? 100 : 10
function getApiLimitPerSecond(): number { // type can't be known - untyped libraries
if (!env.API_REQ_LIMIT_PER_SEC) { let limiter: any, rateLimitStore: any
return DEFAULT_API_REQ_LIMIT_PER_SEC if (!env.DISABLE_RATE_LIMITING) {
} // allow a lot more requests when in test
return parseInt(env.API_REQ_LIMIT_PER_SEC) const DEFAULT_API_REQ_LIMIT_PER_SEC = env.isTest() ? 100 : 10
}
let rateLimitStore: any = null function getApiLimitPerSecond(): number {
if (!env.isTest()) { if (!env.API_REQ_LIMIT_PER_SEC) {
const { password, host, port } = redis.utils.getRedisConnectionDetails() return DEFAULT_API_REQ_LIMIT_PER_SEC
let options: KoaRateLimitOptions = { }
socket: { return parseInt(env.API_REQ_LIMIT_PER_SEC)
host: host,
port: port,
},
} }
if (password) { if (!env.isTest()) {
options.password = password const { password, host, port } = redis.utils.getRedisConnectionDetails()
} let options: KoaRateLimitOptions = {
socket: {
host: host,
port: port,
},
}
if (!env.REDIS_CLUSTERED) { if (password) {
// Can't set direct redis db in clustered env options.password = password
options.database = SelectableDatabase.RATE_LIMITING }
if (!env.REDIS_CLUSTERED) {
// Can't set direct redis db in clustered env
options.database = SelectableDatabase.RATE_LIMITING
}
rateLimitStore = new Stores.Redis(options)
RateLimit.defaultOptions({
store: rateLimitStore,
})
} }
rateLimitStore = new Stores.Redis(options) // rate limiting, allows for 2 requests per second
RateLimit.defaultOptions({ limiter = RateLimit.middleware({
store: rateLimitStore, interval: { sec: 1 },
// per ip, per interval
max: getApiLimitPerSecond(),
}) })
} else {
console.log("**** PUBLIC API RATE LIMITING DISABLED ****")
} }
// rate limiting, allows for 2 requests per second
const limiter = RateLimit.middleware({
interval: { sec: 1 },
// per ip, per interval
max: getApiLimitPerSecond(),
})
const publicRouter = new Router({ const publicRouter = new Router({
prefix: PREFIX, prefix: PREFIX,
}) })
publicRouter.use(limiter) if (limiter) {
publicRouter.use(limiter)
}
function addMiddleware( function addMiddleware(
endpoints: any, endpoints: any,

View file

@ -61,6 +61,7 @@ const environment = {
ALLOW_DEV_AUTOMATIONS: process.env.ALLOW_DEV_AUTOMATIONS, ALLOW_DEV_AUTOMATIONS: process.env.ALLOW_DEV_AUTOMATIONS,
DISABLE_THREADING: process.env.DISABLE_THREADING, DISABLE_THREADING: process.env.DISABLE_THREADING,
DISABLE_AUTOMATION_LOGS: process.env.DISABLE_AUTOMATION_LOGS, DISABLE_AUTOMATION_LOGS: process.env.DISABLE_AUTOMATION_LOGS,
DISABLE_RATE_LIMITING: process.env.DISABLE_RATE_LIMITING,
MULTI_TENANCY: process.env.MULTI_TENANCY, MULTI_TENANCY: process.env.MULTI_TENANCY,
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS, ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
SELF_HOSTED: process.env.SELF_HOSTED, SELF_HOSTED: process.env.SELF_HOSTED,

View file

@ -20,7 +20,7 @@
"test:self:ci": "yarn run test --testPathIgnorePatterns=\\.integration\\. \\.cloud\\. \\.licensing\\.", "test:self:ci": "yarn run test --testPathIgnorePatterns=\\.integration\\. \\.cloud\\. \\.licensing\\.",
"serve:test:self:ci": "start-server-and-test dev:built http://localhost:4001/health test:self:ci", "serve:test:self:ci": "start-server-and-test dev:built http://localhost:4001/health test:self:ci",
"serve": "start-server-and-test dev:built http://localhost:4001/health", "serve": "start-server-and-test dev:built http://localhost:4001/health",
"dev:built": "cd ../ && yarn dev:built" "dev:built": "cd ../ && DISABLE_RATE_LIMITING=1 yarn dev:built"
}, },
"devDependencies": { "devDependencies": {
"@budibase/types": "^2.3.17", "@budibase/types": "^2.3.17",