mirror of https://github.com/Budibase/budibase.git
10 changed files with 115 additions and 93 deletions
@ -1,56 +1,32 @@ |
|||
const fs = require("fs") |
|||
const { join } = require("../../utilities/centralPath") |
|||
const readline = require("readline") |
|||
const { budibaseAppsDir } = require("../../utilities/budibaseDir") |
|||
const env = require("../../environment") |
|||
const ENV_FILE_PATH = "/.env" |
|||
const builderDB = require("../../db/builder") |
|||
|
|||
exports.fetch = async function(ctx) { |
|||
ctx.status = 200 |
|||
ctx.body = { |
|||
budibase: env.BUDIBASE_API_KEY, |
|||
userId: env.USERID_API_KEY, |
|||
try { |
|||
const mainDoc = await builderDB.getBuilderMainDoc() |
|||
ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {} |
|||
} catch (err) { |
|||
/* istanbul ignore next */ |
|||
ctx.throw(400, err) |
|||
} |
|||
} |
|||
|
|||
exports.update = async function(ctx) { |
|||
const key = `${ctx.params.key.toUpperCase()}_API_KEY` |
|||
const key = ctx.params.key |
|||
const value = ctx.request.body.value |
|||
|
|||
// set environment variables
|
|||
env._set(key, value) |
|||
|
|||
// Write to file
|
|||
await updateValues([key, value]) |
|||
|
|||
ctx.status = 200 |
|||
ctx.message = `Updated ${ctx.params.key} API key succesfully.` |
|||
ctx.body = { [ctx.params.key]: ctx.request.body.value } |
|||
} |
|||
|
|||
async function updateValues([key, value]) { |
|||
let newContent = "" |
|||
let keyExists = false |
|||
let envPath = join(budibaseAppsDir(), ENV_FILE_PATH) |
|||
const readInterface = readline.createInterface({ |
|||
input: fs.createReadStream(envPath), |
|||
output: process.stdout, |
|||
console: false, |
|||
}) |
|||
readInterface.on("line", function(line) { |
|||
// Mutate lines and change API Key
|
|||
if (line.startsWith(key)) { |
|||
line = `${key}=${value}` |
|||
keyExists = true |
|||
try { |
|||
const mainDoc = await builderDB.getBuilderMainDoc() |
|||
if (mainDoc.apiKeys == null) { |
|||
mainDoc.apiKeys = {} |
|||
} |
|||
newContent = `${newContent}\n${line}` |
|||
}) |
|||
readInterface.on("close", function() { |
|||
// Write file here
|
|||
if (!keyExists) { |
|||
// Add API Key if it doesn't exist in the file at all
|
|||
newContent = `${newContent}\n${key}=${value}` |
|||
mainDoc.apiKeys[key] = value |
|||
const resp = await builderDB.setBuilderMainDoc(mainDoc) |
|||
ctx.body = { |
|||
_id: resp.id, |
|||
_rev: resp.rev, |
|||
} |
|||
fs.writeFileSync(envPath, newContent) |
|||
}) |
|||
} catch (err) { |
|||
/* istanbul ignore next */ |
|||
ctx.throw(400, err) |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,38 @@ |
|||
const CouchDB = require("./index") |
|||
const { StaticDatabases } = require("./utils") |
|||
const env = require("../environment") |
|||
|
|||
const SELF_HOST_ERR = "Unable to access builder DB/doc - not self hosted." |
|||
const BUILDER_DB = StaticDatabases.BUILDER |
|||
|
|||
/** |
|||
* This is the builder database, right now this is a single, static database |
|||
* that is present across the whole system and determines some core functionality |
|||
* for the builder (e.g. storage of API keys). This has been limited to self hosting |
|||
* as it doesn't make as much sense against the currently design Cloud system. |
|||
*/ |
|||
|
|||
exports.getBuilderMainDoc = async () => { |
|||
if (!env.SELF_HOSTED) { |
|||
throw SELF_HOST_ERR |
|||
} |
|||
const db = new CouchDB(BUILDER_DB.name) |
|||
try { |
|||
return await db.get(BUILDER_DB.baseDoc) |
|||
} catch (err) { |
|||
// doesn't exist yet, nothing to get
|
|||
return { |
|||
_id: BUILDER_DB.baseDoc, |
|||
} |
|||
} |
|||
} |
|||
|
|||
exports.setBuilderMainDoc = async doc => { |
|||
if (!env.SELF_HOSTED) { |
|||
throw SELF_HOST_ERR |
|||
} |
|||
// make sure to override the ID
|
|||
doc._id = BUILDER_DB.baseDoc |
|||
const db = new CouchDB(BUILDER_DB.name) |
|||
return db.put(doc) |
|||
} |
|||
Loading…
Reference in new issue