Files
pedwfrontend/pages/storageAdmin.js
T
2025-08-12 12:49:42 +01:00

95 lines
2.8 KiB
JavaScript

import { DefaultAzureCredential } from "@azure/identity";
import { BlobServiceClient, ContainerClient } from "@azure/storage-blob";
import { v4 as uuidv4 } from "uuid";
import {
createContainerSas,
listContainers,
listBlobHierarchical,
listContainersForUser,
} from "../actions/azurestorage";
import { PrismaClient } from "@prisma/client";
import { consoleLogger, getIP } from "../actions";
export async function getServerSideProps({ req }) {
// Load allowed IPs from env and trim spaces
const ALLOWED_IPS = process.env.ALLOWED_IPS
? process.env.ALLOWED_IPS.split(",").map((ip) => ip.trim())
: [];
// Always allow localhost addresses
const LOCALHOST_IPS = ["127.0.0.1", "::1"];
// Get IP address from headers or socket
const forwarded = req.headers["x-forwarded-for"];
const ip =
typeof forwarded === "string"
? forwarded.split(",")[0]
: req.socket.remoteAddress;
console.log("Visitor IP:", ip);
// Check whitelist + localhost
if (![...ALLOWED_IPS, ...LOCALHOST_IPS].includes(ip)) {
return {
redirect: {
destination: "/403", // custom "Access Denied" page
permanent: false,
},
};
}
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
const creds = new DefaultAzureCredential();
const globalForPrisma = global;
const prisma = globalForPrisma.prisma || new PrismaClient();
const blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
creds
);
const users = await prisma.user.findMany();
const data = await Promise.all(
users.map(async (user) => {
const containers = await listContainersForUser(
blobServiceClient,
`${user.id}`,
user.email
);
return containers.length > 0
? { id: user.id, email: user.email, containers }
: null;
})
);
return { props: { data: data.filter(Boolean) } };
}
export default function StoragePage({ data }) {
return (
<div>
<h1>Storage Account Contents</h1>
{data.map((user) => (
<div key={user.id}>
{user.containers.map((c) => (
<div key={c.container}>
<h3>
{c.container} - {user.email}
</h3>
<ul>
{c.blobs.map((blob) => (
<li key={blob}>{blob}</li>
))}
</ul>
</div>
))}
</div>
))}
</div>
);
}