Rejib IP fetching.
This commit is contained in:
parent
20f55e3795
commit
eee2991b09
|
@ -253,6 +253,11 @@ export function getAppId(): string | undefined {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getIP(): string | undefined {
|
||||||
|
const context = Context.get()
|
||||||
|
return context?.ip
|
||||||
|
}
|
||||||
|
|
||||||
export const getProdAppId = () => {
|
export const getProdAppId = () => {
|
||||||
const appId = getAppId()
|
const appId = getAppId()
|
||||||
if (!appId) {
|
if (!appId) {
|
||||||
|
@ -281,6 +286,10 @@ export function doInScimContext(task: any) {
|
||||||
return newContext(updates, task)
|
return newContext(updates, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function doInIPContext(ip: string, task: any) {
|
||||||
|
return newContext({ ip }, task)
|
||||||
|
}
|
||||||
|
|
||||||
export async function ensureSnippetContext(enabled = !env.isTest()) {
|
export async function ensureSnippetContext(enabled = !env.isTest()) {
|
||||||
const ctx = getCurrentContext()
|
const ctx = getCurrentContext()
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ export type ContextMap = {
|
||||||
identity?: IdentityContext
|
identity?: IdentityContext
|
||||||
environmentVariables?: Record<string, string>
|
environmentVariables?: Record<string, string>
|
||||||
isScim?: boolean
|
isScim?: boolean
|
||||||
|
ip?: string
|
||||||
automationId?: string
|
automationId?: string
|
||||||
isMigrating?: boolean
|
isMigrating?: boolean
|
||||||
vm?: VM
|
vm?: VM
|
||||||
|
|
|
@ -2,7 +2,7 @@ import env from "../environment"
|
||||||
import * as crypto from "crypto"
|
import * as crypto from "crypto"
|
||||||
import * as context from "../context"
|
import * as context from "../context"
|
||||||
import { PostHog, PostHogOptions } from "posthog-node"
|
import { PostHog, PostHogOptions } from "posthog-node"
|
||||||
import { FeatureFlag, UserCtx } from "@budibase/types"
|
import { FeatureFlag } from "@budibase/types"
|
||||||
import tracer from "dd-trace"
|
import tracer from "dd-trace"
|
||||||
import { Duration } from "../utils"
|
import { Duration } from "../utils"
|
||||||
|
|
||||||
|
@ -142,23 +142,17 @@ export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
|
||||||
return this.flagSchema[name as keyof T] !== undefined
|
return this.flagSchema[name as keyof T] !== undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
async get<K extends keyof T>(
|
async get<K extends keyof T>(key: K): Promise<FlagValues<T>[K]> {
|
||||||
key: K,
|
const flags = await this.fetch()
|
||||||
ctx?: UserCtx
|
|
||||||
): Promise<FlagValues<T>[K]> {
|
|
||||||
const flags = await this.fetch(ctx)
|
|
||||||
return flags[key]
|
return flags[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
async isEnabled<K extends KeysOfType<T, boolean>>(
|
async isEnabled<K extends KeysOfType<T, boolean>>(key: K): Promise<boolean> {
|
||||||
key: K,
|
const flags = await this.fetch()
|
||||||
ctx?: UserCtx
|
|
||||||
): Promise<boolean> {
|
|
||||||
const flags = await this.fetch(ctx)
|
|
||||||
return flags[key]
|
return flags[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch(ctx?: UserCtx): Promise<FlagValues<T>> {
|
async fetch(): Promise<FlagValues<T>> {
|
||||||
return await tracer.trace("features.fetch", async span => {
|
return await tracer.trace("features.fetch", async span => {
|
||||||
const cachedFlags = context.getFeatureFlags<FlagValues<T>>(this.setId)
|
const cachedFlags = context.getFeatureFlags<FlagValues<T>>(this.setId)
|
||||||
if (cachedFlags) {
|
if (cachedFlags) {
|
||||||
|
@ -199,36 +193,11 @@ export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
|
||||||
tags[`flags.${key}.source`] = "environment"
|
tags[`flags.${key}.source`] = "environment"
|
||||||
}
|
}
|
||||||
|
|
||||||
const license = ctx?.user?.license
|
|
||||||
if (license) {
|
|
||||||
tags[`readFromLicense`] = true
|
|
||||||
|
|
||||||
for (const feature of license.features) {
|
|
||||||
if (!this.isFlagName(feature)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
flagValues[feature] === true ||
|
|
||||||
specificallySetFalse.has(feature)
|
|
||||||
) {
|
|
||||||
// If the flag is already set to through environment variables, we
|
|
||||||
// don't want to override it back to false here.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-expect-error - TS does not like you writing into a generic type,
|
|
||||||
// but we know that it's okay in this case because it's just an object.
|
|
||||||
flagValues[feature] = true
|
|
||||||
tags[`flags.${feature}.source`] = "license"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const identity = context.getIdentity()
|
const identity = context.getIdentity()
|
||||||
|
|
||||||
let userId = identity?._id
|
let userId = identity?._id
|
||||||
if (!userId && ctx?.ip) {
|
if (!userId) {
|
||||||
userId = crypto.createHash("sha512").update(ctx.ip).digest("hex")
|
userId = context.getIP()
|
||||||
}
|
}
|
||||||
|
|
||||||
let tenantId = identity?.tenantId
|
let tenantId = identity?.tenantId
|
||||||
|
|
|
@ -20,3 +20,4 @@ export { default as correlation } from "../logging/correlation/middleware"
|
||||||
export { default as errorHandling } from "./errorHandling"
|
export { default as errorHandling } from "./errorHandling"
|
||||||
export { default as querystringToBody } from "./querystringToBody"
|
export { default as querystringToBody } from "./querystringToBody"
|
||||||
export * as joiValidator from "./joi-validator"
|
export * as joiValidator from "./joi-validator"
|
||||||
|
export { default as ip } from "./ip"
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { Ctx } from "@budibase/types"
|
||||||
|
import { doInIPContext } from "../context"
|
||||||
|
|
||||||
|
export default async (ctx: Ctx, next: any) => {
|
||||||
|
if (ctx.ip) {
|
||||||
|
doInIPContext(ctx.ip, () => {
|
||||||
|
return next()
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,6 +35,7 @@ export default function createKoaApp() {
|
||||||
|
|
||||||
app.use(middleware.correlation)
|
app.use(middleware.correlation)
|
||||||
app.use(middleware.pino)
|
app.use(middleware.pino)
|
||||||
|
app.use(middleware.ip)
|
||||||
app.use(userAgent)
|
app.use(userAgent)
|
||||||
|
|
||||||
const server = http.createServer(app.callback())
|
const server = http.createServer(app.callback())
|
||||||
|
|
|
@ -54,6 +54,7 @@ app.use(koaBody({ multipart: true }))
|
||||||
app.use(koaSession(app))
|
app.use(koaSession(app))
|
||||||
app.use(middleware.correlation)
|
app.use(middleware.correlation)
|
||||||
app.use(middleware.pino)
|
app.use(middleware.pino)
|
||||||
|
app.use(middleware.ip)
|
||||||
app.use(userAgent)
|
app.use(userAgent)
|
||||||
|
|
||||||
// authentication
|
// authentication
|
||||||
|
|
Loading…
Reference in New Issue