|
| 1 | +/* |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | +import { Response } from '@adobe/fetch'; |
| 13 | +import { AuditLog } from '@adobe/helix-admin-support'; |
| 14 | +import { errorResponse } from '../support/utils.js'; |
| 15 | +import { |
| 16 | + decode, encode, getNextLinkUrl, parseIntWithCond, parseTimespan, |
| 17 | +} from './utils.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Total size of collected entries in log, when stringified. |
| 21 | + */ |
| 22 | +export const MAX_ENTRIES_SIZE = 3_000_000; |
| 23 | + |
| 24 | +/** |
| 25 | + * Query an audit log |
| 26 | + * |
| 27 | + * @param {import('./AdminContext.js').AdminContext} context context |
| 28 | + * @param {import('./RequestInfo.js').RequestInfo} info request info |
| 29 | + * @returns {Promise<Response>} response |
| 30 | + */ |
| 31 | +export default async function query(context, info) { |
| 32 | + const { |
| 33 | + log, data: { |
| 34 | + from: fromS, to: toS, since: sinceS, limit: limitS, nextToken, |
| 35 | + }, |
| 36 | + } = context; |
| 37 | + |
| 38 | + let from; |
| 39 | + let to; |
| 40 | + |
| 41 | + try { |
| 42 | + ([from, to] = parseTimespan(fromS, toS, sinceS)); |
| 43 | + } catch (e) { |
| 44 | + return errorResponse(log, 400, e.message); |
| 45 | + } |
| 46 | + |
| 47 | + const limit = parseIntWithCond(limitS, (value) => { |
| 48 | + if (value >= 1 && value <= 1000) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + log.warn(`'limit' should be between 1 and 1000: ' ${value}`); |
| 52 | + return false; |
| 53 | + }, 1000); |
| 54 | + |
| 55 | + const { org, site } = info; |
| 56 | + const auditLog = AuditLog.createReader(org, site, log); |
| 57 | + |
| 58 | + try { |
| 59 | + await auditLog.init(); |
| 60 | + |
| 61 | + const { entries, next } = await auditLog.getEntries( |
| 62 | + from, |
| 63 | + to, |
| 64 | + { limit, maxSize: MAX_ENTRIES_SIZE }, |
| 65 | + decode(nextToken), |
| 66 | + ); |
| 67 | + const result = { |
| 68 | + from: new Date(from).toISOString(), |
| 69 | + to: new Date(to).toISOString(), |
| 70 | + entries, |
| 71 | + }; |
| 72 | + if (next) { |
| 73 | + result.nextToken = encode(next); |
| 74 | + result.links = { |
| 75 | + next: getNextLinkUrl(info, { |
| 76 | + from: result.from, |
| 77 | + to: result.to, |
| 78 | + limit: limitS, |
| 79 | + nextToken: result.nextToken, |
| 80 | + }), |
| 81 | + }; |
| 82 | + } |
| 83 | + return new Response(JSON.stringify(result), { |
| 84 | + status: 200, |
| 85 | + headers: { |
| 86 | + 'content-type': 'application/json', |
| 87 | + }, |
| 88 | + }); |
| 89 | + } finally { |
| 90 | + auditLog.close(); |
| 91 | + } |
| 92 | +} |
0 commit comments