Merge pull request #13048 from Budibase/revert-13047-revert-13043-fix/updating-users-via-public-api
Fix updating users via cross-service comms (public API)
This commit is contained in:
commit
5cb94c9fb5
|
@ -2,11 +2,12 @@ import { Header } from "../../constants"
|
||||||
|
|
||||||
const correlator = require("correlation-id")
|
const correlator = require("correlation-id")
|
||||||
|
|
||||||
export const setHeader = (headers: any) => {
|
export const setHeader = (headers: Record<string, string>) => {
|
||||||
const correlationId = correlator.getId()
|
const correlationId = correlator.getId()
|
||||||
if (correlationId) {
|
if (!correlationId) {
|
||||||
headers[Header.CORRELATION_ID] = correlationId
|
return
|
||||||
}
|
}
|
||||||
|
headers[Header.CORRELATION_ID] = correlationId
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getId() {
|
export function getId() {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import fetch from "node-fetch"
|
import fetch from "node-fetch"
|
||||||
import env from "../../environment"
|
import env from "../../environment"
|
||||||
import { checkSlashesInUrl } from "../../utilities"
|
import { checkSlashesInUrl } from "../../utilities"
|
||||||
import { request } from "../../utilities/workerRequests"
|
import { createRequest } from "../../utilities/workerRequests"
|
||||||
import { clearLock as redisClearLock } from "../../utilities/redis"
|
import { clearLock as redisClearLock } from "../../utilities/redis"
|
||||||
import { DocumentType } from "../../db/utils"
|
import { DocumentType } from "../../db/utils"
|
||||||
import {
|
import {
|
||||||
|
@ -13,14 +13,19 @@ import {
|
||||||
} from "@budibase/backend-core"
|
} from "@budibase/backend-core"
|
||||||
import { App } from "@budibase/types"
|
import { App } from "@budibase/types"
|
||||||
|
|
||||||
async function redirect(ctx: any, method: string, path: string = "global") {
|
async function redirect(
|
||||||
|
ctx: any,
|
||||||
|
method: "GET" | "POST" | "DELETE",
|
||||||
|
path: string = "global"
|
||||||
|
) {
|
||||||
const { devPath } = ctx.params
|
const { devPath } = ctx.params
|
||||||
const queryString = ctx.originalUrl.split("?")[1] || ""
|
const queryString = ctx.originalUrl.split("?")[1] || ""
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
checkSlashesInUrl(
|
checkSlashesInUrl(
|
||||||
`${env.WORKER_URL}/api/${path}/${devPath}?${queryString}`
|
`${env.WORKER_URL}/api/${path}/${devPath}?${queryString}`
|
||||||
),
|
),
|
||||||
request(ctx, {
|
createRequest({
|
||||||
|
ctx,
|
||||||
method,
|
method,
|
||||||
body: ctx.request.body,
|
body: ctx.request.body,
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
import { Response, default as fetch } from "node-fetch"
|
import {
|
||||||
|
Response,
|
||||||
|
default as fetch,
|
||||||
|
type RequestInit,
|
||||||
|
Headers,
|
||||||
|
HeadersInit,
|
||||||
|
} from "node-fetch"
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import { checkSlashesInUrl } from "./index"
|
import { checkSlashesInUrl } from "./index"
|
||||||
import {
|
import {
|
||||||
|
@ -7,36 +13,62 @@ import {
|
||||||
tenancy,
|
tenancy,
|
||||||
logging,
|
logging,
|
||||||
env as coreEnv,
|
env as coreEnv,
|
||||||
|
utils,
|
||||||
} from "@budibase/backend-core"
|
} from "@budibase/backend-core"
|
||||||
import { Ctx, User, EmailInvite } from "@budibase/types"
|
import { Ctx, User, EmailInvite } from "@budibase/types"
|
||||||
|
|
||||||
export function request(ctx?: Ctx, request?: any) {
|
interface Request {
|
||||||
if (!request.headers) {
|
ctx?: Ctx
|
||||||
request.headers = {}
|
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"
|
||||||
|
headers?: { [key: string]: string }
|
||||||
|
body?: { [key: string]: any }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createRequest(request: Request): RequestInit {
|
||||||
|
const headers: Record<string, string> = {}
|
||||||
|
const requestInit: RequestInit = {
|
||||||
|
method: request.method,
|
||||||
}
|
}
|
||||||
if (!ctx) {
|
|
||||||
request.headers[constants.Header.API_KEY] = coreEnv.INTERNAL_API_KEY
|
const ctx = request.ctx
|
||||||
if (tenancy.isTenantIdSet()) {
|
|
||||||
request.headers[constants.Header.TENANT_ID] = tenancy.getTenantId()
|
if (!ctx && coreEnv.INTERNAL_API_KEY) {
|
||||||
|
headers[constants.Header.API_KEY] = coreEnv.INTERNAL_API_KEY
|
||||||
|
} else if (ctx && ctx.headers) {
|
||||||
|
// copy all Budibase utilised headers over - copying everything can have
|
||||||
|
// side effects like requests being rejected due to odd content types etc
|
||||||
|
for (let header of Object.values(constants.Header)) {
|
||||||
|
const value = ctx.headers[header]
|
||||||
|
if (value === undefined) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
headers[header] = Array.isArray(value) ? value[0] : value
|
||||||
|
}
|
||||||
|
// be specific about auth headers
|
||||||
|
const cookie = ctx.headers[constants.Header.COOKIE],
|
||||||
|
apiKey = ctx.headers[constants.Header.API_KEY]
|
||||||
|
if (cookie) {
|
||||||
|
headers[constants.Header.COOKIE] = cookie
|
||||||
|
} else if (apiKey) {
|
||||||
|
headers[constants.Header.API_KEY] = Array.isArray(apiKey)
|
||||||
|
? apiKey[0]
|
||||||
|
: apiKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// apply tenancy if its available
|
||||||
|
if (tenancy.isTenantIdSet()) {
|
||||||
|
headers[constants.Header.TENANT_ID] = tenancy.getTenantId()
|
||||||
|
}
|
||||||
|
|
||||||
if (request.body && Object.keys(request.body).length > 0) {
|
if (request.body && Object.keys(request.body).length > 0) {
|
||||||
request.headers["Content-Type"] = "application/json"
|
headers["Content-Type"] = "application/json"
|
||||||
request.body =
|
requestInit.body = JSON.stringify(request.body)
|
||||||
typeof request.body === "object"
|
|
||||||
? JSON.stringify(request.body)
|
|
||||||
: request.body
|
|
||||||
} else {
|
|
||||||
delete request.body
|
|
||||||
}
|
|
||||||
if (ctx && ctx.headers) {
|
|
||||||
request.headers = ctx.headers
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add x-budibase-correlation-id header
|
logging.correlation.setHeader(headers)
|
||||||
logging.correlation.setHeader(request.headers)
|
requestInit.headers = headers
|
||||||
|
return requestInit
|
||||||
return request
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkResponse(
|
async function checkResponse(
|
||||||
|
@ -54,7 +86,7 @@ async function checkResponse(
|
||||||
}
|
}
|
||||||
const msg = `Unable to ${errorMsg} - ${responseErrorMessage}`
|
const msg = `Unable to ${errorMsg} - ${responseErrorMessage}`
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
ctx.throw(msg, response.status)
|
ctx.throw(response.status || 500, msg)
|
||||||
} else {
|
} else {
|
||||||
throw msg
|
throw msg
|
||||||
}
|
}
|
||||||
|
@ -85,7 +117,7 @@ export async function sendSmtpEmail({
|
||||||
// tenant ID will be set in header
|
// tenant ID will be set in header
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
checkSlashesInUrl(env.WORKER_URL + `/api/global/email/send`),
|
checkSlashesInUrl(env.WORKER_URL + `/api/global/email/send`),
|
||||||
request(undefined, {
|
createRequest({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: {
|
body: {
|
||||||
email: to,
|
email: to,
|
||||||
|
@ -107,7 +139,8 @@ export async function removeAppFromUserRoles(ctx: Ctx, appId: string) {
|
||||||
const prodAppId = dbCore.getProdAppID(appId)
|
const prodAppId = dbCore.getProdAppID(appId)
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`),
|
checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`),
|
||||||
request(ctx, {
|
createRequest({
|
||||||
|
ctx,
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -118,7 +151,7 @@ export async function allGlobalUsers(ctx: Ctx) {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
|
||||||
// we don't want to use API key when getting self
|
// we don't want to use API key when getting self
|
||||||
request(ctx, { method: "GET" })
|
createRequest({ ctx, method: "GET" })
|
||||||
)
|
)
|
||||||
return checkResponse(response, "get users", { ctx })
|
return checkResponse(response, "get users", { ctx })
|
||||||
}
|
}
|
||||||
|
@ -127,7 +160,7 @@ export async function saveGlobalUser(ctx: Ctx) {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
|
||||||
// we don't want to use API key when getting self
|
// we don't want to use API key when getting self
|
||||||
request(ctx, { method: "POST", body: ctx.request.body })
|
createRequest({ ctx, method: "POST", body: ctx.request.body })
|
||||||
)
|
)
|
||||||
return checkResponse(response, "save user", { ctx })
|
return checkResponse(response, "save user", { ctx })
|
||||||
}
|
}
|
||||||
|
@ -138,7 +171,7 @@ export async function deleteGlobalUser(ctx: Ctx) {
|
||||||
env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
|
env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
|
||||||
),
|
),
|
||||||
// we don't want to use API key when getting self
|
// we don't want to use API key when getting self
|
||||||
request(ctx, { method: "DELETE" })
|
createRequest({ ctx, method: "DELETE" })
|
||||||
)
|
)
|
||||||
return checkResponse(response, "delete user", { ctx })
|
return checkResponse(response, "delete user", { ctx })
|
||||||
}
|
}
|
||||||
|
@ -149,7 +182,7 @@ export async function readGlobalUser(ctx: Ctx): Promise<User> {
|
||||||
env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
|
env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
|
||||||
),
|
),
|
||||||
// we don't want to use API key when getting self
|
// we don't want to use API key when getting self
|
||||||
request(ctx, { method: "GET" })
|
createRequest({ ctx, method: "GET" })
|
||||||
)
|
)
|
||||||
return checkResponse(response, "get user", { ctx })
|
return checkResponse(response, "get user", { ctx })
|
||||||
}
|
}
|
||||||
|
@ -159,7 +192,7 @@ export async function getChecklist(): Promise<{
|
||||||
}> {
|
}> {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
checkSlashesInUrl(env.WORKER_URL + "/api/global/configs/checklist"),
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/configs/checklist"),
|
||||||
request(undefined, { method: "GET" })
|
createRequest({ method: "GET" })
|
||||||
)
|
)
|
||||||
return checkResponse(response, "get checklist")
|
return checkResponse(response, "get checklist")
|
||||||
}
|
}
|
||||||
|
@ -167,7 +200,7 @@ export async function getChecklist(): Promise<{
|
||||||
export async function generateApiKey(userId: string) {
|
export async function generateApiKey(userId: string) {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
checkSlashesInUrl(env.WORKER_URL + "/api/global/self/api_key"),
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/self/api_key"),
|
||||||
request(undefined, { method: "POST", body: { userId } })
|
createRequest({ method: "POST", body: { userId } })
|
||||||
)
|
)
|
||||||
return checkResponse(response, "generate API key")
|
return checkResponse(response, "generate API key")
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,4 +16,5 @@ export enum Header {
|
||||||
CORRELATION_ID = "x-budibase-correlation-id",
|
CORRELATION_ID = "x-budibase-correlation-id",
|
||||||
AUTHORIZATION = "authorization",
|
AUTHORIZATION = "authorization",
|
||||||
MIGRATING_APP = "x-budibase-migrating-app",
|
MIGRATING_APP = "x-budibase-migrating-app",
|
||||||
|
COOKIE = "cookie",
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue