Support uploading config files. #14

Merged
telackey merged 5 commits from telackey/configupload into main 2024-08-27 19:44:53 +00:00
3 changed files with 15 additions and 13 deletions
Showing only changes of commit e578605777 - Show all commits

View File

@ -19,6 +19,7 @@ export const Config = {
UNDEPLOYER_STATE: UNDEPLOYER_STATE:
process.env.UNDEPLOYER_STATE || '/srv/deployments/autoundeploy.state', process.env.UNDEPLOYER_STATE || '/srv/deployments/autoundeploy.state',
BUILD_LOGS: process.env.BUILD_LOGS || '/srv/logs', BUILD_LOGS: process.env.BUILD_LOGS || '/srv/logs',
UPLOAD_MAX_SIZE: process.env.BUILD_LOGS || '1MB',
OPENPGP_PASSPHRASE: process.env.OPENPGP_PASSPHRASE, OPENPGP_PASSPHRASE: process.env.OPENPGP_PASSPHRASE,
OPENPGP_PRIVATE_KEY_FILE: process.env.OPENPGP_PRIVATE_KEY_FILE, OPENPGP_PRIVATE_KEY_FILE: process.env.OPENPGP_PRIVATE_KEY_FILE,
}; };

View File

@ -10,7 +10,8 @@ import { Uploader } from './upload.js';
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
const uploader = new Uploader(Config.UPLOAD_DIRECTORY); const configUploader = new Uploader(Config.UPLOAD_DIRECTORY);
const configUploadParser = bodyParser.raw({limit: Config.UPLOAD_MAX_SIZE, type: "*/*"})
app.use(function (_req, res, next) { app.use(function (_req, res, next) {
res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Origin', '*');
@ -99,10 +100,9 @@ app.get('/:id/log', async (req, res) => {
} }
}); });
const rawParser = bodyParser.raw({limit: "1MB", type: "*/*"}) app.post('/upload/config', configUploadParser, async (req, res) => {
app.post('/upload/config', rawParser, async (req, res) => {
try { try {
const id = await uploader.upload(req.body); const id = await configUploader.upload(req.body);
res.json({ res.json({
id id
}); });

View File

@ -43,9 +43,6 @@ export const b64ToBytes = (base64): Uint8Array => {
return bytes; return bytes;
}; };
const decrypt = async (binaryMessage: Uint8Array): Promise<any> => { const decrypt = async (binaryMessage: Uint8Array): Promise<any> => {
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
binaryMessage, binaryMessage,
@ -69,21 +66,25 @@ export class Uploader {
async upload(body: string | Uint8Array): Promise<string> { async upload(body: string | Uint8Array): Promise<string> {
let raw: any; let raw: any;
try { try {
raw = b64ToBytes(body); raw = b64ToBytes(body);
} catch { } catch {
raw = body; raw = body;
} }
// We decrypt only to make sure the content is valid.
// Once we know it is good, we want to store the encrypted copy.
const obj = await decrypt(raw); const obj = await decrypt(raw);
validateConfig(obj); validateConfig(obj);
let id = randomId(); let id: string;
let destination: string; let destination: string;
do { do {
id = randomId(); id = randomId();
destination = `${this.directory}/${id}`; destination = `${this.directory}/${id}`;
} while (fs.existsSync(destination)); } while (fs.existsSync(destination));
console.log(`Wrote config to: ${destination}`);
fs.writeFileSync(destination, raw); fs.writeFileSync(destination, raw);
return id; return id;
} }