Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
GIT_PASSWORD,
GIT_PUSH_RETRIES,
GIT_USER,
TRUST_PROXY,
} from 'src/validators'
import swaggerUi from 'swagger-ui-express'
import giteaCheckLatest from './gitea/connect'
Expand All @@ -41,6 +42,7 @@ const env = cleanEnv({
GIT_PASSWORD,
EXPRESS_PAYLOAD_LIMIT,
GIT_PUSH_RETRIES,
TRUST_PROXY,
})

const debug = Debug('otomi:app')
Expand Down Expand Up @@ -153,6 +155,14 @@ export async function initApp(inOtomiStack?: OtomiStack) {
// Only create lightship in production (not in tests)
const lightship = env.isTest ? null : createLightship()
const app = express()

// Configure trust proxy for rate limiting behind Kubernetes Ingress
// See: https://github.com/express-rate-limit/express-rate-limit/wiki/Troubleshooting-Proxy-Issues
// Set to number of proxies between user and server (typically 1 for Kubernetes Ingress)
if (!env.isTest && env.TRUST_PROXY > 0) {
app.set('trust proxy', env.TRUST_PROXY)
}

const apiRoutesPath = path.resolve(__dirname, 'api')
await loadSpec()
const authz = new Authz(otomiSpec.spec)
Expand Down
5 changes: 5 additions & 0 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ export const RATE_LIMIT_AUTH_MAX_ATTEMPTS = num({
desc: 'Maximum number of failed authentication attempts per IP per time window',
default: 500,
})
export const TRUST_PROXY = num({
desc: 'Number of reverse proxies to trust for client IP detection (0 to disable, 1 for Kubernetes Ingress, 2 for LB + Ingress)',
default: 2,
devDefault: 0,
})
const { env } = process
export function cleanEnv<T>(validators: { [K in keyof T]: ValidatorSpec<T[K]> }, options: CleanOptions<T> = {}) {
if (env.NODE_ENV === 'test') {
Expand Down
Loading