Some minor updates after reviewing.

This commit is contained in:
mike12345567 2023-01-19 19:10:41 +00:00
parent 6e5c126d5a
commit e7e6e3419e
2 changed files with 56 additions and 43 deletions

View File

@ -8,24 +8,16 @@ import {
} from "../../db/utils" } from "../../db/utils"
import { destroy as tableDestroy } from "./table/internal" import { destroy as tableDestroy } from "./table/internal"
import { BuildSchemaErrors, InvalidColumns } from "../../constants" import { BuildSchemaErrors, InvalidColumns } from "../../constants"
import { getIntegration, getDefinitions } from "../../integrations" import { getIntegration } from "../../integrations"
import { getDatasourceAndQuery } from "./row/utils" import { getDatasourceAndQuery } from "./row/utils"
import { invalidateDynamicVariables } from "../../threads/utils" import { invalidateDynamicVariables } from "../../threads/utils"
import { db as dbCore, context, events } from "@budibase/backend-core" import { db as dbCore, context, events } from "@budibase/backend-core"
import { import { UserCtx, Datasource, Row } from "@budibase/types"
UserCtx,
Datasource,
Row,
DatasourceFieldType,
PASSWORD_REPLACEMENT,
} from "@budibase/types"
import sdk from "../../sdk" import sdk from "../../sdk"
import { removeSecrets } from "../../sdk/app/datasources/datasources"
export async function fetch(ctx: UserCtx) { export async function fetch(ctx: UserCtx) {
// Get internal tables // Get internal tables
const db = context.getAppDB() const db = context.getAppDB()
const definitions = await getDefinitions()
const internalTables = await db.allDocs( const internalTables = await db.allDocs(
getTableParams(null, { getTableParams(null, {
include_docs: true, include_docs: true,
@ -52,10 +44,12 @@ export async function fetch(ctx: UserCtx) {
) )
).rows.map(row => row.doc) ).rows.map(row => row.doc)
const allDatasources: Datasource[] = [bbInternalDb, ...datasources] const allDatasources: Datasource[] = await sdk.datasources.removeSecrets([
bbInternalDb,
...datasources,
])
for (let datasource of allDatasources) { for (let datasource of allDatasources) {
datasource = sdk.datasources.removeSecrets(definitions, datasource)
if (datasource.type === dbCore.BUDIBASE_DATASOURCE_TYPE) { if (datasource.type === dbCore.BUDIBASE_DATASOURCE_TYPE) {
datasource.entities = internal[datasource._id!] datasource.entities = internal[datasource._id!]
} }
@ -156,9 +150,12 @@ export async function update(ctx: UserCtx) {
const datasourceId = ctx.params.datasourceId const datasourceId = ctx.params.datasourceId
let datasource = await sdk.datasources.get(datasourceId) let datasource = await sdk.datasources.get(datasourceId)
const auth = datasource.config?.auth const auth = datasource.config?.auth
const definitions = await getDefinitions()
await invalidateVariables(datasource, ctx.request.body) await invalidateVariables(datasource, ctx.request.body)
if (!sdk.datasources.isValid(datasource)) {
ctx.throw(400, "Environment variables binding format incorrect")
}
const isBudibaseSource = datasource.type === dbCore.BUDIBASE_DATASOURCE_TYPE const isBudibaseSource = datasource.type === dbCore.BUDIBASE_DATASOURCE_TYPE
const dataSourceBody = isBudibaseSource const dataSourceBody = isBudibaseSource
@ -186,7 +183,7 @@ export async function update(ctx: UserCtx) {
ctx.status = 200 ctx.status = 200
ctx.message = "Datasource saved successfully." ctx.message = "Datasource saved successfully."
ctx.body = { ctx.body = {
datasource: sdk.datasources.removeSecrets(definitions, datasource), datasource: await sdk.datasources.removeSecretSingle(datasource),
} }
} }
@ -194,7 +191,6 @@ export async function save(ctx: UserCtx) {
const db = context.getAppDB() const db = context.getAppDB()
const plus = ctx.request.body.datasource.plus const plus = ctx.request.body.datasource.plus
const fetchSchema = ctx.request.body.fetchSchema const fetchSchema = ctx.request.body.fetchSchema
const definitions = await getDefinitions()
const datasource = { const datasource = {
_id: generateDatasourceID({ plus }), _id: generateDatasourceID({ plus }),
@ -202,6 +198,10 @@ export async function save(ctx: UserCtx) {
...ctx.request.body.datasource, ...ctx.request.body.datasource,
} }
if (!sdk.datasources.isValid(datasource)) {
ctx.throw(400, "Environment variables binding format incorrect")
}
let schemaError = null let schemaError = null
if (fetchSchema) { if (fetchSchema) {
const { tables, error } = await buildSchemaHelper(datasource) const { tables, error } = await buildSchemaHelper(datasource)
@ -223,7 +223,7 @@ export async function save(ctx: UserCtx) {
} }
const response: any = { const response: any = {
datasource: sdk.datasources.removeSecrets(definitions, datasource), datasource: await sdk.datasources.removeSecretSingle(datasource),
} }
if (schemaError) { if (schemaError) {
response.error = schemaError response.error = schemaError
@ -292,9 +292,8 @@ export async function destroy(ctx: UserCtx) {
export async function find(ctx: UserCtx) { export async function find(ctx: UserCtx) {
const database = context.getAppDB() const database = context.getAppDB()
const definitions = await getDefinitions()
const datasource = await database.get(ctx.params.datasourceId) const datasource = await database.get(ctx.params.datasourceId)
ctx.body = sdk.datasources.removeSecrets(definitions, datasource) ctx.body = await sdk.datasources.removeSecretSingle(datasource)
} }
// dynamic query functionality // dynamic query functionality

View File

@ -7,8 +7,10 @@ import {
PASSWORD_REPLACEMENT, PASSWORD_REPLACEMENT,
} from "@budibase/types" } from "@budibase/types"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { env } from "process"
import { getEnvironmentVariables } from "../../utils" import { getEnvironmentVariables } from "../../utils"
import { getDefinitions } from "../../../integrations"
const ENV_VAR_PREFIX = "env."
async function enrichDatasourceWithValues(datasource: Datasource) { async function enrichDatasourceWithValues(datasource: Datasource) {
const cloned = cloneDeep(datasource) const cloned = cloneDeep(datasource)
@ -42,18 +44,24 @@ export async function getWithEnvVars(datasourceId: string) {
const appDb = context.getAppDB() const appDb = context.getAppDB()
const datasource = await appDb.get(datasourceId) const datasource = await appDb.get(datasourceId)
const blocks = findHBSBlocks(JSON.stringify(datasource)) const blocks = findHBSBlocks(JSON.stringify(datasource))
const usesEnvVars = blocks.find(block => block.includes("env.")) != null const usesEnvVars =
blocks.find(block => block.includes(ENV_VAR_PREFIX)) != null
if (usesEnvVars) { if (usesEnvVars) {
return enrichDatasourceWithValues(datasource) return enrichDatasourceWithValues(datasource)
} else { } else {
throw new Error("Environment variables binding format incorrect") return datasource
} }
} }
export function removeSecrets( export function isValid(datasource: Datasource) {
definitions: Record<string, Integration>, const blocks = findHBSBlocks(JSON.stringify(datasource))
datasource: Datasource const validList = blocks.filter(block => block.includes(ENV_VAR_PREFIX))
) { return blocks.length === validList.length
}
export async function removeSecrets(datasources: Datasource[]) {
const definitions = await getDefinitions()
for (let datasource of datasources) {
const schema = definitions[datasource.source] const schema = definitions[datasource.source]
if (datasource.config) { if (datasource.config) {
// strip secrets from response, so they don't show in the network request // strip secrets from response, so they don't show in the network request
@ -66,7 +74,8 @@ export function removeSecrets(
continue continue
} }
const blocks = findHBSBlocks(datasource.config[key] as string) const blocks = findHBSBlocks(datasource.config[key] as string)
const usesEnvVars = blocks.find(block => block.includes("env.")) != null const usesEnvVars =
blocks.find(block => block.includes(ENV_VAR_PREFIX)) != null
if ( if (
!usesEnvVars && !usesEnvVars &&
schema.datasource?.[key]?.type === DatasourceFieldType.PASSWORD schema.datasource?.[key]?.type === DatasourceFieldType.PASSWORD
@ -75,5 +84,10 @@ export function removeSecrets(
} }
} }
} }
return datasource }
return datasources
}
export async function removeSecretSingle(datasource: Datasource) {
return (await removeSecrets([datasource]))[0]
} }