Fix presigned URL generation race conditions

This commit is contained in:
Lauri Võsandi 2022-02-15 23:16:38 +02:00 committed by Lauri Võsandi
parent a08f1d37b7
commit 52ecba53cc
3 changed files with 20 additions and 18 deletions

View File

@ -10,6 +10,7 @@ const mongoUri = process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/default?rep
// Minio set-up variables // Minio set-up variables
const minioURI = new URL(process.env.MINIO_URI || 'http://kspace-mugshot:2mSI6HdbJ8@127.0.0.1:9000/kspace-mugshot'); const minioURI = new URL(process.env.MINIO_URI || 'http://kspace-mugshot:2mSI6HdbJ8@127.0.0.1:9000/kspace-mugshot');
const minioBucket = minioURI.pathname.substring(1); const minioBucket = minioURI.pathname.substring(1);
console.info("Using bucket:", minioBucket);
const historyNumber = parseInt(process.env.HISTORY_AMOUNT) || 1000; const historyNumber = parseInt(process.env.HISTORY_AMOUNT) || 1000;
// Stream set-up variables // Stream set-up variables
@ -44,14 +45,12 @@ async function run() {
secretKey: minioURI.password secretKey: minioURI.password
}); });
function wrapEvent(doc) { async function wrapEvent(doc) {
if (doc && doc.screenshot_count) { if (doc && doc.screenshot_count) {
doc.screenshots = []; doc.screenshots = [];
for (let i = 1; i <= doc.screenshot_count ; i++) { for (let i = 1; i <= doc.screenshot_count ; i++) {
minioClient.presignedUrl('GET', minioBucket, `${doc.camera}/${doc._id}/${i}.jpg`, 60 * 60, (err, presignedUrl) => { let presignedUrl = await minioClient.presignedUrl('GET', minioBucket, `${doc.camera}/${doc._id}/${i}.jpg`, 60 * 60);
if (err) { return console.log(err) };
doc.screenshots.push({ url: presignedUrl }); doc.screenshots.push({ url: presignedUrl });
})
} }
}; };
let blob = JSON.stringify(doc); let blob = JSON.stringify(doc);
@ -62,14 +61,14 @@ async function run() {
const header = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive' }; const header = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive' };
response.writeHead(200, "OK", header); response.writeHead(200, "OK", header);
const historyCursor = collection.find({}).sort({$natural : -1}).limit(historyNumber); const historyCursor = collection.find().sort({$natural:-1}).limit(historyNumber);
historyCursor.forEach((document) => { historyCursor.forEach(async (document) => {
response.write(wrapEvent(document)); response.write(await wrapEvent(document));
}) })
changeStream.on("change", data => { changeStream.on("change", async(data) => {
response.write(wrapEvent(data.fullDocument)); response.write(await wrapEvent(data.fullDocument));
}); });
}); });

View File

@ -9,17 +9,11 @@ x-common: &common
ME_CONFIG_MONGODB_ENABLE_ADMIN: 'true' ME_CONFIG_MONGODB_ENABLE_ADMIN: 'true'
ME_CONFIG_MONGODB_SERVER: '127.0.0.1' ME_CONFIG_MONGODB_SERVER: '127.0.0.1'
ME_CONFIG_MONGODB_AUTH_DATABASE: admin ME_CONFIG_MONGODB_AUTH_DATABASE: admin
MONGO_URI: mongodb://127.0.0.1:27017/default?replicaSet=rs0
MONGO_COLLECTION: eventlog
HISTORY_AMOUNT: 10
MINIO_ACCESS_KEY: kspace-mugshot MINIO_ACCESS_KEY: kspace-mugshot
MINIO_SECRET_KEY: 2mSI6HdbJ8 MINIO_SECRET_KEY: 2mSI6HdbJ8
MINIO_DEFAULT_BUCKETS: kspace-mugshot:download MINIO_DEFAULT_BUCKETS: kspace-mugshot:download
MINIO_CONSOLE_PORT_NUMBER: 9001
MINIO_URI: 'https://kspace-mugshot:2mSI6HdbJ8@127.0.0.1:9000/kspace-mugshot' MINIO_URI: 'https://kspace-mugshot:2mSI6HdbJ8@127.0.0.1:9000/kspace-mugshot'
services: services:
mongoexpress: mongoexpress:

View File

@ -1,3 +1,4 @@
from datetime import datetime
from pymongo import MongoClient from pymongo import MongoClient
from bson.objectid import ObjectId from bson.objectid import ObjectId
from time import sleep from time import sleep
@ -7,6 +8,14 @@ MONGO_URI="mongodb://127.0.0.1:27017/default"
db = MongoClient(MONGO_URI).get_default_database() db = MongoClient(MONGO_URI).get_default_database()
while True: while True:
db.eventlog.insert_one({ "timestamp": "...", "event": "motion-detected", "started": "...", "finished": "...", "component": "motion-detect", "camera": "Server room", "action": "event", db.eventlog.insert_one({
"timestamp": datetime.utcnow(),
"event": "motion-detected",
"started": "...",
"finished": "...",
"component": "motion-detect",
"camera": "Server room",
"action": "event",
"screenshot_count": 1
}) })
sleep(1) sleep(1)