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:
process.env.UNDEPLOYER_STATE || '/srv/deployments/autoundeploy.state',
BUILD_LOGS: process.env.BUILD_LOGS || '/srv/logs',
UPLOAD_MAX_SIZE: process.env.BUILD_LOGS || '1MB',
OPENPGP_PASSPHRASE: process.env.OPENPGP_PASSPHRASE,
OPENPGP_PRIVATE_KEY_FILE: process.env.OPENPGP_PRIVATE_KEY_FILE,
};

View File

@ -10,7 +10,8 @@ import { Uploader } from './upload.js';
const app = express();
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) {
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', rawParser, async (req, res) => {
app.post('/upload/config', configUploadParser, async (req, res) => {
try {
const id = await uploader.upload(req.body);
const id = await configUploader.upload(req.body);
res.json({
id
});

View File

@ -43,9 +43,6 @@ export const b64ToBytes = (base64): Uint8Array => {
return bytes;
};
const decrypt = async (binaryMessage: Uint8Array): Promise<any> => {
const message = await openpgp.readMessage({
binaryMessage,
@ -69,21 +66,25 @@ export class Uploader {
async upload(body: string | Uint8Array): Promise<string> {
let raw: any;
try {
raw = b64ToBytes(body);
} catch {
raw = body;
}
try {
raw = b64ToBytes(body);
} catch {
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);
validateConfig(obj);
let id = randomId();
let id: string;
let destination: string;
do {
id = randomId();
destination = `${this.directory}/${id}`;
} while (fs.existsSync(destination));
console.log(`Wrote config to: ${destination}`);
fs.writeFileSync(destination, raw);
return id;
}