ITHC: protect administrative representation unlock endpoint
This commit is contained in:
@@ -1,96 +1,9 @@
|
||||
import { DefaultAzureCredential } from "@azure/identity";
|
||||
import { BlobServiceClient } from "@azure/storage-blob";
|
||||
import { consoleLogger } from "../../../actions/core/logger";
|
||||
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
||||
import { respondError } from "../middleware/apiResponse";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== "POST") {
|
||||
return respondError(res, {
|
||||
status: 405,
|
||||
code: "METHOD_NOT_ALLOWED",
|
||||
message: "Method not allowed"
|
||||
});
|
||||
}
|
||||
|
||||
const { container, blobName } = req.body;
|
||||
|
||||
if (!container || !blobName) {
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "MISSING_REQUIRED_BODY",
|
||||
message: "Missing container or blobName"
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
|
||||
const creds = new DefaultAzureCredential();
|
||||
const blobServiceClient = new BlobServiceClient(
|
||||
`https://${accountName}.blob.core.windows.net`,
|
||||
creds
|
||||
);
|
||||
|
||||
const containerClient = blobServiceClient.getContainerClient(container);
|
||||
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
|
||||
|
||||
// Download blob content
|
||||
const downloadResponse = await blockBlobClient.download(0);
|
||||
const downloaded = await streamToString(
|
||||
downloadResponse.readableStreamBody
|
||||
);
|
||||
|
||||
// Parse JSON
|
||||
const json = JSON.parse(downloaded);
|
||||
|
||||
// Remove repComplete element if it exists
|
||||
if ("repComplete" in json) {
|
||||
delete json.repComplete;
|
||||
}
|
||||
|
||||
const tags = {
|
||||
containerid: json.containerID,
|
||||
caseID: json.ticketnumber,
|
||||
blobType: "Representation"
|
||||
};
|
||||
|
||||
// Convert back to string
|
||||
const updatedContent = JSON.stringify(json, null, 2);
|
||||
|
||||
// Upload updated content (overwrite)
|
||||
await blockBlobClient.upload(
|
||||
updatedContent,
|
||||
Buffer.byteLength(updatedContent),
|
||||
{
|
||||
blobHTTPHeaders: { blobContentType: "application/json" }
|
||||
}
|
||||
);
|
||||
|
||||
const withTags = await blockBlobClient.setTags(tags);
|
||||
const withMeta = await blockBlobClient.setMetadata(tags);
|
||||
|
||||
return respondSuccess(res, {
|
||||
message: "repComplete removed successfully"
|
||||
});
|
||||
} catch (error) {
|
||||
consoleLogger(error);
|
||||
return respondError(res, {
|
||||
status: 500,
|
||||
code: "EDIT_REP_JSON_FAILED",
|
||||
message: "Failed to edit rep.json"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to read stream to string
|
||||
async function streamToString(readableStream) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
readableStream.on("data", (data) => {
|
||||
chunks.push(data.toString());
|
||||
});
|
||||
readableStream.on("end", () => {
|
||||
resolve(chunks.join(""));
|
||||
});
|
||||
readableStream.on("error", reject);
|
||||
return respondError(res, {
|
||||
status: 410,
|
||||
code: "ENDPOINT_RETIRED",
|
||||
message: "Endpoint retired"
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user