forked from tsai/budibase
10 changed files with 115 additions and 93 deletions
@ -1,56 +1,32 @@ |
|||||
const fs = require("fs") |
const builderDB = require("../../db/builder") |
||||
const { join } = require("../../utilities/centralPath") |
|
||||
const readline = require("readline") |
|
||||
const { budibaseAppsDir } = require("../../utilities/budibaseDir") |
|
||||
const env = require("../../environment") |
|
||||
const ENV_FILE_PATH = "/.env" |
|
||||
|
|
||||
exports.fetch = async function(ctx) { |
exports.fetch = async function(ctx) { |
||||
ctx.status = 200 |
try { |
||||
ctx.body = { |
const mainDoc = await builderDB.getBuilderMainDoc() |
||||
budibase: env.BUDIBASE_API_KEY, |
ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {} |
||||
userId: env.USERID_API_KEY, |
} catch (err) { |
||||
|
/* istanbul ignore next */ |
||||
|
ctx.throw(400, err) |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
exports.update = async function(ctx) { |
exports.update = async function(ctx) { |
||||
const key = `${ctx.params.key.toUpperCase()}_API_KEY` |
const key = ctx.params.key |
||||
const value = ctx.request.body.value |
const value = ctx.request.body.value |
||||
|
|
||||
// set environment variables
|
try { |
||||
env._set(key, value) |
const mainDoc = await builderDB.getBuilderMainDoc() |
||||
|
if (mainDoc.apiKeys == null) { |
||||
// Write to file
|
mainDoc.apiKeys = {} |
||||
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 |
|
||||
} |
} |
||||
newContent = `${newContent}\n${line}` |
mainDoc.apiKeys[key] = value |
||||
}) |
const resp = await builderDB.setBuilderMainDoc(mainDoc) |
||||
readInterface.on("close", function() { |
ctx.body = { |
||||
// Write file here
|
_id: resp.id, |
||||
if (!keyExists) { |
_rev: resp.rev, |
||||
// Add API Key if it doesn't exist in the file at all
|
|
||||
newContent = `${newContent}\n${key}=${value}` |
|
||||
} |
} |
||||
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