@adobe/helix-admin-support 2.8.31 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +4 -2
- package/src/auditlog/AuditLog.js +42 -0
- package/src/auditlog/DateFormat.js +26 -0
- package/src/auditlog/Reader.js +227 -0
- package/src/auditlog/Writer.js +185 -0
- package/src/auditlog/utils.js +22 -0
- package/src/index.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [2.9.0](https://github.com/adobe/helix-admin-support/compare/v2.8.31...v2.9.0) (2024-08-22)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add audit log reader and writer ([#299](https://github.com/adobe/helix-admin-support/issues/299)) ([d2b80e3](https://github.com/adobe/helix-admin-support/commit/d2b80e3af2555d214054e789b8d5650286fc5b2d))
|
|
7
|
+
|
|
1
8
|
## [2.8.31](https://github.com/adobe/helix-admin-support/compare/v2.8.30...v2.8.31) (2024-08-17)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/helix-admin-support",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.0",
|
|
4
4
|
"description": "Helix Admin Support Library",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"homepage": "https://github.com/adobe/helix-admin-support#readme",
|
|
29
29
|
"mocha": {
|
|
30
30
|
"require": [
|
|
31
|
-
"test/setup-env.js"
|
|
31
|
+
"test/setup-env.js",
|
|
32
|
+
"mocha-suppress-logs"
|
|
32
33
|
],
|
|
33
34
|
"recursive": "true",
|
|
34
35
|
"reporter": "mocha-multi-reporters",
|
|
@@ -57,6 +58,7 @@
|
|
|
57
58
|
"lint-staged": "15.2.9",
|
|
58
59
|
"mocha": "10.7.3",
|
|
59
60
|
"mocha-multi-reporters": "1.5.1",
|
|
61
|
+
"mocha-suppress-logs": "0.5.1",
|
|
60
62
|
"nock": "13.5.4",
|
|
61
63
|
"semantic-release": "24.1.0",
|
|
62
64
|
"xml2js": "0.6.2",
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 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 http://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 AuditLogReader from './Reader.js';
|
|
13
|
+
import AuditLogWriter from './Writer.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create an audit log reader of an org and site.
|
|
17
|
+
*
|
|
18
|
+
* @param {String} org org
|
|
19
|
+
* @param {String|undefined} site site, optional
|
|
20
|
+
* @param {Logger} log logger
|
|
21
|
+
* @returns {AuditLogReader} reader
|
|
22
|
+
*/
|
|
23
|
+
function createReader(org, site, log) {
|
|
24
|
+
return new AuditLogReader(org, site, log);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create an audit log writer for an org and site.
|
|
29
|
+
*
|
|
30
|
+
* @param {String} org org
|
|
31
|
+
* @param {String|undefined} site site, optional
|
|
32
|
+
* @param {Logger} log logger
|
|
33
|
+
* @returns {AuditLogWriter} writer
|
|
34
|
+
*/
|
|
35
|
+
function createWriter(org, site, log) {
|
|
36
|
+
return new AuditLogWriter(org, site, log);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const AuditLog = {
|
|
40
|
+
createReader,
|
|
41
|
+
createWriter,
|
|
42
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 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 http://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
|
+
|
|
13
|
+
export default class DateFormat {
|
|
14
|
+
static parse(s) {
|
|
15
|
+
const chars = [...s.substring(0, 19)];
|
|
16
|
+
chars[10] = 'T';
|
|
17
|
+
chars[13] = ':';
|
|
18
|
+
chars[16] = ':';
|
|
19
|
+
chars.push('Z');
|
|
20
|
+
return new Date(chars.join(''));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static format(date) {
|
|
24
|
+
return date.toISOString().substring(0, 19).replace(/[T:]/g, '-');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 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 http://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 { promisify } from 'util';
|
|
13
|
+
import zlib from 'zlib';
|
|
14
|
+
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
|
15
|
+
import { Response } from '@adobe/fetch';
|
|
16
|
+
import { BUCKET_NAME, INDEX_FILE } from './utils.js';
|
|
17
|
+
import DateFormat from './DateFormat.js';
|
|
18
|
+
|
|
19
|
+
const gunzip = promisify(zlib.gunzip);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef LogLocation
|
|
23
|
+
* @property {number} fileIndex file index into log file array
|
|
24
|
+
* @property {number} entryIndex entry index into entries array of that log file
|
|
25
|
+
*
|
|
26
|
+
* @typedef LogEntries
|
|
27
|
+
* @property {number} fileIndex file index into log file array
|
|
28
|
+
* @property {number} entryIndex entry index into entries array of that log file
|
|
29
|
+
* @property {LogEntry[]} entries log entry
|
|
30
|
+
*
|
|
31
|
+
* @typedef LogResult
|
|
32
|
+
* @property {LogEntry[]} entries log entry
|
|
33
|
+
* @property {string} nextToken optional next token
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Audit log reader implemented in S3.
|
|
38
|
+
*
|
|
39
|
+
* @param {String} org org
|
|
40
|
+
* @param {String|undefined} site site, optional
|
|
41
|
+
* @param {import('@adobe/helix-universal').Logger} log logger
|
|
42
|
+
*/
|
|
43
|
+
export default class Reader {
|
|
44
|
+
constructor(org, site, log) {
|
|
45
|
+
this.prefix = site ? `${org}/${site}` : `${org}/.all`;
|
|
46
|
+
this.log = log;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Initialize this log, retrieves the list of log files available.
|
|
51
|
+
*/
|
|
52
|
+
async init() {
|
|
53
|
+
this.s3 = new S3Client({});
|
|
54
|
+
|
|
55
|
+
const { prefix } = this;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const res = await this.s3.send(new GetObjectCommand({
|
|
59
|
+
Bucket: BUCKET_NAME,
|
|
60
|
+
Key: `${prefix}/${INDEX_FILE}`,
|
|
61
|
+
}));
|
|
62
|
+
const logContents = await new Response(res.Body, {}).text();
|
|
63
|
+
this.logFiles = logContents.split('\n').map((name) => ({
|
|
64
|
+
name,
|
|
65
|
+
timestamp: DateFormat.parse(name).getTime(),
|
|
66
|
+
}));
|
|
67
|
+
} catch (e) {
|
|
68
|
+
/* c8 ignore next 3 */
|
|
69
|
+
if (e.$metadata?.httpStatusCode !== 404) {
|
|
70
|
+
throw e;
|
|
71
|
+
}
|
|
72
|
+
this.logFiles = [];
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Read the log contents of a log file from S3.
|
|
79
|
+
*
|
|
80
|
+
* @param {number} fileIndex file index
|
|
81
|
+
*
|
|
82
|
+
* @returns array of entries
|
|
83
|
+
*/
|
|
84
|
+
async #getLogContents(fileIndex) {
|
|
85
|
+
const {
|
|
86
|
+
prefix, logFiles, s3, log,
|
|
87
|
+
} = this;
|
|
88
|
+
|
|
89
|
+
const key = `${prefix}/${logFiles[fileIndex].name}.gz`;
|
|
90
|
+
try {
|
|
91
|
+
const res = await s3.send(new GetObjectCommand({
|
|
92
|
+
Bucket: BUCKET_NAME,
|
|
93
|
+
Key: key,
|
|
94
|
+
}));
|
|
95
|
+
const buf = await new Response(res.Body, {}).buffer();
|
|
96
|
+
return JSON.parse(await gunzip(buf));
|
|
97
|
+
} catch (e) {
|
|
98
|
+
log.debug(e);
|
|
99
|
+
throw new Error(`Unable to find audit log file '${key}': ${e.message}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Find entries starting at a given date.
|
|
105
|
+
*
|
|
106
|
+
* @param {number} after timestamp
|
|
107
|
+
*
|
|
108
|
+
* @returns {LogEntries>} entries, fileIndex and entryIndex
|
|
109
|
+
*/
|
|
110
|
+
async #findEntries(after) {
|
|
111
|
+
const { logFiles } = this;
|
|
112
|
+
|
|
113
|
+
// stop if there are no log files at all
|
|
114
|
+
if (logFiles.length === 0) {
|
|
115
|
+
return { entries: [] };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// find the last log file that started before the given date
|
|
119
|
+
const fileIndex = logFiles.findLastIndex(({ timestamp }) => timestamp <= after);
|
|
120
|
+
if (fileIndex === -1) {
|
|
121
|
+
// none found, so return the very first log file
|
|
122
|
+
return { entries: await this.#getLogContents(0), fileIndex: 0, entryIndex: 0 };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// we found a log file that was created earlier, so locate the first entry that started
|
|
126
|
+
// at or after the given date
|
|
127
|
+
let entries = await this.#getLogContents(fileIndex);
|
|
128
|
+
|
|
129
|
+
const entryIndex = entries.findIndex(({ timestamp }) => timestamp >= after);
|
|
130
|
+
if (entryIndex !== -1) {
|
|
131
|
+
// found: so return that slice
|
|
132
|
+
return { entries: entries.slice(entryIndex), fileIndex, entryIndex };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// the log file found stops before the date given, so start at next one
|
|
136
|
+
if (fileIndex === logFiles.length - 1) {
|
|
137
|
+
// there is no next log file
|
|
138
|
+
return { entries: [] };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
entries = await this.#getLogContents(fileIndex + 1);
|
|
142
|
+
return { entries, fileIndex: fileIndex + 1, entryIndex: 0 };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Locate entries given a previously saved location
|
|
147
|
+
*
|
|
148
|
+
* @param {LogLocation} location location
|
|
149
|
+
* @returns {LogEntries>} entries, fileIndex and entryIndex
|
|
150
|
+
*/
|
|
151
|
+
async #locateEntries(location) {
|
|
152
|
+
const { log, logFiles } = this;
|
|
153
|
+
|
|
154
|
+
const { fileIndex, entryIndex } = location;
|
|
155
|
+
if (!Number.isInteger(fileIndex) || !Number.isInteger(entryIndex)) {
|
|
156
|
+
log.warn(`Next file and entry index are not numbers: ${fileIndex}/${entryIndex}`);
|
|
157
|
+
return { entries: [] };
|
|
158
|
+
}
|
|
159
|
+
if (fileIndex < 0 || fileIndex >= logFiles.length) {
|
|
160
|
+
log.warn(`Next file index (${fileIndex}) out of log files range: ${logFiles.length}`);
|
|
161
|
+
return { entries: [] };
|
|
162
|
+
}
|
|
163
|
+
const entries = await this.#getLogContents(fileIndex);
|
|
164
|
+
if (entryIndex < 0 || entryIndex >= entries.length) {
|
|
165
|
+
log.warn(`Next entry index (${entryIndex}) out of entries range: ${entries.length}`);
|
|
166
|
+
return { entries: [] };
|
|
167
|
+
}
|
|
168
|
+
return { entries: entries.slice(entryIndex), fileIndex, entryIndex };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Lookup entries that lie between two timestamps
|
|
173
|
+
*
|
|
174
|
+
* @param {number} after from timestamp
|
|
175
|
+
* @param {number} before to timestamp
|
|
176
|
+
* @param {number} limit number of entries to return at most
|
|
177
|
+
* @param {LogLocation} location optional location with `fileIndex` and `entryIndex`
|
|
178
|
+
*
|
|
179
|
+
* @returns {Promise<LogResult>} log result
|
|
180
|
+
*/
|
|
181
|
+
async getEntries(after, before, limit, location) {
|
|
182
|
+
const { logFiles } = this;
|
|
183
|
+
const { entries, fileIndex, entryIndex } = location
|
|
184
|
+
? await this.#locateEntries(location)
|
|
185
|
+
: await this.#findEntries(after);
|
|
186
|
+
|
|
187
|
+
if (entries.length === 0) {
|
|
188
|
+
// no entries found at all, stop here to keep the following simpler
|
|
189
|
+
return { entries };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// find the utmost limit of items to return
|
|
193
|
+
let remaining = entries.findIndex(({ timestamp }) => timestamp > before);
|
|
194
|
+
if (remaining === -1) {
|
|
195
|
+
remaining = entries.length;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// return either `limit` number of items, or items left in that file, whichever is smaller
|
|
199
|
+
const count = Math.min(limit, remaining);
|
|
200
|
+
if (count < remaining) {
|
|
201
|
+
// `limit` was reached first, so continue after that
|
|
202
|
+
return {
|
|
203
|
+
entries: entries.slice(0, count),
|
|
204
|
+
next: { fileIndex, entryIndex: entryIndex + count },
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
if (remaining < entries.length) {
|
|
208
|
+
// `before` timestamp was reached first, so stop
|
|
209
|
+
return { entries: entries.slice(0, count) };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// we return all items in this file, and need to determine whether to continue paging
|
|
213
|
+
if (fileIndex === logFiles.length - 1 || logFiles[fileIndex + 1].timestamp > before) {
|
|
214
|
+
// there's no next page or it starts after `before` date, so stop
|
|
215
|
+
return { entries };
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
// next location will start at the beginning of the next file
|
|
219
|
+
entries,
|
|
220
|
+
next: { fileIndex: fileIndex + 1, entryIndex: 0 },
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
close() {
|
|
225
|
+
this.s3.destroy();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 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 http://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
|
+
|
|
13
|
+
import crypto from 'crypto';
|
|
14
|
+
import { promisify } from 'util';
|
|
15
|
+
import zlib from 'zlib';
|
|
16
|
+
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
|
17
|
+
import { Response } from '@adobe/fetch';
|
|
18
|
+
import { BUCKET_NAME, INDEX_FILE } from './utils.js';
|
|
19
|
+
import DateFormat from './DateFormat.js';
|
|
20
|
+
|
|
21
|
+
const gzip = promisify(zlib.gzip);
|
|
22
|
+
const gunzip = promisify(zlib.gunzip);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Metadata property for last event in log file. We update that property
|
|
26
|
+
* whenever we store events to a log file.
|
|
27
|
+
*/
|
|
28
|
+
const META_LAST_EVENT = 'last-event-time';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Threshold size to allow in one log file.
|
|
32
|
+
*/
|
|
33
|
+
const MAX_OBJECT_SIZE = 512 * 1024;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Generate log file from date and some random value.
|
|
37
|
+
*
|
|
38
|
+
* @returns log file, consisting of name and date created
|
|
39
|
+
*/
|
|
40
|
+
function generateID() {
|
|
41
|
+
return `${DateFormat.format(new Date())}-${crypto.randomBytes(8).toString('hex').toUpperCase()}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Audit log implemented in S3
|
|
46
|
+
*/
|
|
47
|
+
export default class Writer {
|
|
48
|
+
constructor(org, site, log) {
|
|
49
|
+
this.prefix = site ? `${org}/${site}` : `${org}/.all`;
|
|
50
|
+
this.log = log;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Initialize this log.
|
|
55
|
+
*/
|
|
56
|
+
async init() {
|
|
57
|
+
this.s3 = new S3Client({});
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Fetch index file
|
|
63
|
+
*
|
|
64
|
+
* @returns {String[]} log entry file names array
|
|
65
|
+
*/
|
|
66
|
+
async #fetchIndex() {
|
|
67
|
+
const { prefix, s3 } = this;
|
|
68
|
+
|
|
69
|
+
// fetch name of last log object
|
|
70
|
+
try {
|
|
71
|
+
const res = await s3.send(new GetObjectCommand({
|
|
72
|
+
Bucket: BUCKET_NAME,
|
|
73
|
+
Key: `${prefix}/${INDEX_FILE}`,
|
|
74
|
+
}));
|
|
75
|
+
const logContents = await new Response(res.Body, {}).text();
|
|
76
|
+
return logContents.split('\n');
|
|
77
|
+
} catch (e) {
|
|
78
|
+
/* c8 ignore next 3 */
|
|
79
|
+
if (e.$metadata.httpStatusCode !== 404) {
|
|
80
|
+
throw e;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Fetch log file. Returns null if the log file is not found or too large to accomodate
|
|
88
|
+
* another record.
|
|
89
|
+
*/
|
|
90
|
+
async #fetchLogFile(key) {
|
|
91
|
+
const { s3 } = this;
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const res = await s3.send(new GetObjectCommand({
|
|
95
|
+
Bucket: BUCKET_NAME,
|
|
96
|
+
Key: `${key}.gz`,
|
|
97
|
+
}));
|
|
98
|
+
if (res.ContentLength < MAX_OBJECT_SIZE) {
|
|
99
|
+
const buf = await new Response(res.Body, {}).buffer();
|
|
100
|
+
return { key, contents: JSON.parse(await gunzip(buf)) };
|
|
101
|
+
}
|
|
102
|
+
} catch (e) {
|
|
103
|
+
/* c8 ignore next 3 */
|
|
104
|
+
if (e.$metadata.httpStatusCode !== 404) {
|
|
105
|
+
throw e;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Retrieves the last log object or creates one if necessary.
|
|
113
|
+
*
|
|
114
|
+
* @returns an object containing a key and contents
|
|
115
|
+
*/
|
|
116
|
+
async #getOrCreateLogObject() {
|
|
117
|
+
const { prefix, s3 } = this;
|
|
118
|
+
|
|
119
|
+
// fetch list of log files
|
|
120
|
+
const logFiles = await this.#fetchIndex();
|
|
121
|
+
|
|
122
|
+
// fetch contents of last log object
|
|
123
|
+
if (logFiles.length) {
|
|
124
|
+
const key = `${prefix}/${logFiles[logFiles.length - 1]}`;
|
|
125
|
+
const logFile = await this.#fetchLogFile(key);
|
|
126
|
+
if (logFile) {
|
|
127
|
+
return logFile;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// generate new last log object, store modified list in index
|
|
132
|
+
logFiles.push(generateID());
|
|
133
|
+
await s3.send(new PutObjectCommand({
|
|
134
|
+
Body: logFiles.join('\n'),
|
|
135
|
+
Bucket: BUCKET_NAME,
|
|
136
|
+
ContentType: 'text/plain',
|
|
137
|
+
Key: `${prefix}/${INDEX_FILE}`,
|
|
138
|
+
}));
|
|
139
|
+
return { key: `${prefix}/${logFiles[logFiles.length - 1]}`, contents: [] };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Store a modified log file back to S3
|
|
144
|
+
*
|
|
145
|
+
* @param {string} key key for object to store, with project prefix
|
|
146
|
+
* @param {string} contents contents to store
|
|
147
|
+
* @param {object} metadata metadata to store to log file
|
|
148
|
+
*/
|
|
149
|
+
async #storeLogFile(key, contents, metadata) {
|
|
150
|
+
const { s3 } = this;
|
|
151
|
+
await s3.send(new PutObjectCommand({
|
|
152
|
+
Body: await gzip(JSON.stringify(contents)),
|
|
153
|
+
Bucket: BUCKET_NAME,
|
|
154
|
+
ContentEncoding: 'gzip',
|
|
155
|
+
ContentType: 'application/json',
|
|
156
|
+
Key: `${key}.gz`,
|
|
157
|
+
Metadata: metadata,
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Appends rows to the audit log
|
|
163
|
+
*
|
|
164
|
+
* @param {Array} updates added rows
|
|
165
|
+
* @returns {Promise<void>}
|
|
166
|
+
*/
|
|
167
|
+
async append(updates) {
|
|
168
|
+
const { log } = this;
|
|
169
|
+
|
|
170
|
+
if (updates.length) {
|
|
171
|
+
const { key, contents } = await this.#getOrCreateLogObject();
|
|
172
|
+
const lastEventTime = updates[updates.length - 1].timestamp;
|
|
173
|
+
contents.push(...updates);
|
|
174
|
+
await this.#storeLogFile(key, contents, {
|
|
175
|
+
[META_LAST_EVENT]: DateFormat.format(new Date(lastEventTime)),
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
log.info(`Appended ${updates.length} to ${key}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
close() {
|
|
183
|
+
this.s3.destroy();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 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 http://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
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Bucket to use for logging.
|
|
15
|
+
*/
|
|
16
|
+
export const BUCKET_NAME = 'helix-adobe-logs';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Index file in project folder. This is a LF separated text file with
|
|
20
|
+
* the list of all log files in ascending order.
|
|
21
|
+
*/
|
|
22
|
+
export const INDEX_FILE = '.index';
|
package/src/index.js
CHANGED