Skip to content

Commit

Permalink
seperate the old and new flashing into diff endpoints
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: Ryan Cooke <[email protected]>
  • Loading branch information
rcooke-warwick committed Dec 13, 2023
1 parent d3cf5eb commit 8961044
Showing 1 changed file with 67 additions and 88 deletions.
155 changes: 67 additions & 88 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,109 +342,88 @@ async function setup(
) {
res.status(500).send(err.message);
});
// app.post(
// '/dut/flash',
// jsonParser,
// async (req: express.Request, res: express.Response) => {
// // this still seems to be needed to keep the connection alive while doing the flashing of DUT storage media
// const timer = setInterval(() => {
// res.write('status: pending');
// }, 5000)

// res.writeHead(202, {
// 'Content-Type': 'text/event-stream',
// Connection: 'keep-alive',
// });
// let FILENAME = req.body.filename;


// // how do we make this backward compatible - for older versions of core it will still try to stream the image over the req object
// try {
// if(FILENAME.includes(`.gz`)){
// console.log(`Unzipping file`)
// // is the original unzipped file removed?
// await execSync(`gunzip -f ${FILENAME}`)
// FILENAME = FILENAME.replace(/\.gz$/, '');
// }

// console.log(`attempting to flash ${FILENAME}...`);
// await worker.flash(FILENAME);

// // if there is an error , currently this error is not propogated to core... this is bad as tests will try to continue
// } catch (e) {
// if (e instanceof Error) {
// console.log(e)
// }
// } finally {
// res.end();
// clearInterval(timer);
// }
// },
// );

// keep this for legacy core versions
app.post(
'/dut/flash',
async (req: express.Request, res: express.Response) => {

res.setTimeout(0);
console.log(`http keepalive timeout is ${httpServer.keepAliveTimeout}`)
console.log(`http headertimeout is ${httpServer.headersTimeout}`);
function onProgress(progress: multiWrite.MultiDestinationProgress): void {
res.write(`progress: ${JSON.stringify(progress)}`);
}

res.writeHead(202, {
'Content-Type': 'text/event-stream',
Connection: 'keep-alive',
});

const timer = setInterval(() => {
res.write('status: pending');
}, 5000);

const FILENAME = '/data/os.img';
try {
worker.on('progress', onProgress);
const imageStream = createGunzip();
const fileStream = createWriteStream(FILENAME);
console.log(`Streaming image to file...`)
await pipeline(
req,
imageStream,
fileStream
)

console.log(`attempting to flash...`)
await worker.flash(FILENAME);
} catch (e) {
if (e instanceof Error) {
console.log(e)
res.write(`error: ${e.message}`);
}
} finally {
worker.removeListener('progress', onProgress);
res.write('status: done');
res.end();
clearInterval(timer);
}
},
);

app.post(
'/dut/flashFromFile',
jsonParser,
async (req: express.Request, res: express.Response) => {
res.writeHead(202, {
'Content-Type': 'text/event-stream',
Connection: 'keep-alive',
});

// Check if the content type is JSON
if (req.is('application/json')) {
// Handle JSON request
try {
console.log(req.body);
let FILENAME = req.body.filename;
if(FILENAME.includes(`.gz`)){
console.log(`Unzipping file`)
// is the original unzipped file removed?
await execSync(`gunzip -f ${FILENAME}`)
FILENAME = FILENAME.replace(/\.gz$/, '');
}
console.log(`Handling JSON request to flash ${FILENAME}...`);
await worker.flash(FILENAME);
res.status(200).send('JSON request processed successfully');
clearInterval(timer);
} catch (e) {
console.error(e);
res.status(500).send('Internal Server Error');
clearInterval(timer);
}
} else {
// Handle file upload
function onProgress(progress: multiWrite.MultiDestinationProgress): void {
res.write(`progress: ${JSON.stringify(progress)}`);
}
const FILENAME = '/data/os.img';
try {
worker.on('progress', onProgress);
const imageStream = createGunzip();
const fileStream = createWriteStream(FILENAME);
console.log(`Streaming image to file...`)
await pipeline(
req,
imageStream,
fileStream
)

console.log(`attempting to flash...`)
await worker.flash(FILENAME);
} catch (e) {
console.log(e)
res.write(`error: ${e.message}`);
} finally {
worker.removeListener('progress', onProgress);
res.write('status: done');
res.end();
clearInterval(timer);
const timer = setInterval(() => {
res.write('status: pending');
}, 5000);


try {
let FILENAME = req.body.filename;
if(FILENAME.includes(`.gz`)){
console.log(`Unzipping file`)
console.log(await execSync(`gunzip -f ${FILENAME}`))

Check failure

Code scanning / CodeQL

Uncontrolled command line Critical

This command line depends on a
user-provided value
.
FILENAME = FILENAME.replace(/\.gz$/, '');
}
console.log(`Attempting to flash with file: ${FILENAME}...`);
await worker.flash(FILENAME);
clearInterval(timer);
res.end()
} catch (e) {
//TODO: respdond with error instead of just doing nothing
console.log(e);
clearInterval(timer);
res.end()
}
},
}

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a system command
, but is not rate-limited.
);

app.get('/heartbeat', async (req: express.Request, res: express.Response) => {
Expand Down

0 comments on commit 8961044

Please sign in to comment.