Minor refactoring.

This commit is contained in:
Thomas E Lackey 2024-08-26 13:19:23 -05:00
parent 2cce37ef1f
commit e578605777
3 changed files with 15 additions and 13 deletions

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,
@ -74,16 +71,20 @@ export class Uploader {
} 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;
}