@defra/grants-config-utils 1.4.1 → 1.5.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.
@@ -32,7 +32,6 @@ jobs:
32
32
 
33
33
  - name: Scan security vulnerabilities
34
34
  run: npm audit --audit-level=high --omit dev
35
- continue-on-error: true
36
35
 
37
36
  - name: Lint
38
37
  run: npm run lint
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @defra/grants-config-utils
2
2
 
3
+ ## 1.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - bc3fde6: Allow external allowlist
8
+
3
9
  ## 1.4.1
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/grants-config-utils",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "NPM module for providing utils for publishing grants config",
5
5
  "engines": {
6
6
  "node": ">=24.22.1",
@@ -52,7 +52,8 @@
52
52
  "@aws-sdk/client-sts": "3.1113.0",
53
53
  "brace-expansion": "5.0.12",
54
54
  "convict": "6.2.5",
55
- "convict-format-with-validator": "6.2.0"
55
+ "convict-format-with-validator": "6.2.0",
56
+ "yaml": "2.9.1"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@babel/core": "8.0.1",
@@ -84,6 +84,43 @@ export const config = convict({
84
84
  env: "SERVICE_DEPLOYED_BY",
85
85
  },
86
86
  },
87
+ allowlist: {
88
+ devSBIs: {
89
+ doc: "Comma separated SBIs for dev",
90
+ format: String,
91
+ nullable: true,
92
+ default: null,
93
+ env: "ALLOWLIST_DEV_SBI",
94
+ },
95
+ testSBIs: {
96
+ doc: "Comma separated SBIs for test",
97
+ format: String,
98
+ nullable: true,
99
+ default: null,
100
+ env: "ALLOWLIST_TEST_SBI",
101
+ },
102
+ perfTestSBIs: {
103
+ doc: "Comma separated SBIs for perf-test",
104
+ format: String,
105
+ nullable: true,
106
+ default: null,
107
+ env: "ALLOWLIST_PERF_TEST_SBI",
108
+ },
109
+ extTestSBIs: {
110
+ doc: "Comma separated SBIs for ext-test",
111
+ format: String,
112
+ nullable: true,
113
+ default: null,
114
+ env: "ALLOWLIST_EXT_TEST_SBI",
115
+ },
116
+ prodSBIs: {
117
+ doc: "Comma separated SBIs for prod",
118
+ format: String,
119
+ nullable: true,
120
+ default: null,
121
+ env: "ALLOWLIST_PROD_SBI",
122
+ },
123
+ },
87
124
  });
88
125
 
89
126
  config.validate({ allowed: "strict" });
@@ -1,4 +1,5 @@
1
1
  import { readFileSync, existsSync, lstatSync, readdirSync } from "node:fs";
2
+ import YAML from "yaml";
2
3
  import { config } from "../config/config.js";
3
4
  import { listFiles, uploadBlob } from "../s3/s3-interactions.js";
4
5
  import {
@@ -92,7 +93,11 @@ const storeConfigAtServiceVersion = async (configsAtServiceVersion, logger) => {
92
93
  );
93
94
  for (const [localPath, s3Path] of allConfigFiles) {
94
95
  logger.info(`uploading '${s3Path}' to S3`);
95
- await uploadBlob(logger, s3Path, readFileSync(localPath, "utf8"));
96
+ const contents = updateContentsWithInternalData(
97
+ localPath,
98
+ readFileSync(localPath, "utf8"),
99
+ );
100
+ await uploadBlob(logger, s3Path, contents);
96
101
  }
97
102
 
98
103
  logger.info(
@@ -100,6 +105,47 @@ const storeConfigAtServiceVersion = async (configsAtServiceVersion, logger) => {
100
105
  );
101
106
  };
102
107
 
108
+ const getAllowListFor = (configPath) => {
109
+ return (config.get(configPath) || "")
110
+ .split(",")
111
+ .map((value) => value.trim())
112
+ .filter(Boolean);
113
+ };
114
+
115
+ const updateContentsWithInternalData = (localPath, contents) => {
116
+ // allowlist file
117
+ if (localPath.endsWith("allowlist.yaml")) {
118
+ const devSBIs = getAllowListFor("allowlist.devSBIs");
119
+ const testSBIs = getAllowListFor("allowlist.testSBIs");
120
+ const perfTestSBIs = getAllowListFor("allowlist.perfTestSBIs");
121
+ const extTestSBIs = getAllowListFor("allowlist.extTestSBIs");
122
+ const prodSBIs = getAllowListFor("allowlist.prodSBIs");
123
+
124
+ const allowlist = YAML.parse(contents);
125
+
126
+ if (allowlist["dev"]?.sbis && devSBIs.length > 0) {
127
+ allowlist["dev"].sbis = devSBIs;
128
+ }
129
+ if (allowlist["test"]?.sbis && testSBIs.length > 0) {
130
+ allowlist["test"].sbis = testSBIs;
131
+ }
132
+ if (allowlist["perf-test"]?.sbis && perfTestSBIs.length > 0) {
133
+ allowlist["perf-test"].sbis = perfTestSBIs;
134
+ }
135
+ if (allowlist["ext-test"]?.sbis && extTestSBIs.length > 0) {
136
+ allowlist["ext-test"].sbis = extTestSBIs;
137
+ }
138
+ if (allowlist["prod"]?.sbis && prodSBIs.length > 0) {
139
+ allowlist["prod"].sbis = prodSBIs;
140
+ }
141
+
142
+ return YAML.stringify(allowlist);
143
+ }
144
+
145
+ // no internal data to add, return original contents
146
+ return contents;
147
+ };
148
+
103
149
  const notifyConfigBrokerServiceVersionAvailable = async (
104
150
  configsAtServiceVersion,
105
151
  logger,
@@ -1,5 +1,6 @@
1
1
  import { storeConfigVersionAndInformBroker } from "./config-store-and-inform.js";
2
2
  import { readFileSync, existsSync, lstatSync, readdirSync } from "node:fs";
3
+ import YAML from "yaml";
3
4
  import { listFiles, uploadBlob } from "../s3/s3-interactions.js";
4
5
  import {
5
6
  isClientSetup,
@@ -199,5 +200,175 @@ describe("storeConfigVersionAndInformBroker", () => {
199
200
  expect(uploadBlob).toHaveBeenCalledTimes(3);
200
201
  expect(publishMessage).toHaveBeenCalledTimes(2);
201
202
  });
203
+
204
+ it("should special-case allowlist.yaml files by adding all internal only data (SBIs)", async () => {
205
+ const initialYaml =
206
+ "dev:\n sbis: []\ntest:\n sbis: []\nperf-test:\n sbis: []\next-test:\n sbis: []\nprod:\n sbis: []\n";
207
+
208
+ config.set("allowlist.devSBIs", "1, 2");
209
+ config.set("allowlist.testSBIs", "3, 4");
210
+ config.set("allowlist.perfTestSBIs", "5, 6");
211
+ config.set("allowlist.extTestSBIs", "7, 8");
212
+ config.set("allowlist.prodSBIs", "9, 10");
213
+
214
+ readFileSync.mockReturnValueOnce(initialYaml);
215
+ readdirSync.mockRestore();
216
+ readdirSync
217
+ .mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
218
+ .mockReturnValueOnce([
219
+ {
220
+ name: "allowlist.yaml",
221
+ isFile: () => true,
222
+ parentPath: "configurations/grant-1",
223
+ },
224
+ ]);
225
+
226
+ await storeConfigVersionAndInformBroker(mockLogger);
227
+
228
+ expect(uploadBlob).toHaveBeenCalledWith(
229
+ mockLogger,
230
+ "grant-1/1.2.3/allowlist.yaml",
231
+ expect.any(String),
232
+ );
233
+
234
+ const uploadedContent = uploadBlob.mock.calls[0][2];
235
+ const parsedContent = YAML.parse(uploadedContent);
236
+
237
+ expect(parsedContent["dev"].sbis).toEqual(["1", "2"]);
238
+ expect(parsedContent["test"].sbis).toEqual(["3", "4"]);
239
+ expect(parsedContent["perf-test"].sbis).toEqual(["5", "6"]);
240
+ expect(parsedContent["ext-test"].sbis).toEqual(["7", "8"]);
241
+ expect(parsedContent["prod"].sbis).toEqual(["9", "10"]);
242
+ });
243
+
244
+ it("should not modify non-allowlist files", async () => {
245
+ const content = "some: content";
246
+ readFileSync.mockReturnValueOnce(content);
247
+
248
+ readdirSync.mockRestore();
249
+ readdirSync
250
+ .mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
251
+ .mockReturnValueOnce([
252
+ {
253
+ name: "other.yaml",
254
+ isFile: () => true,
255
+ parentPath: "configurations/grant-1",
256
+ },
257
+ ]);
258
+
259
+ await storeConfigVersionAndInformBroker(mockLogger);
260
+
261
+ const uploadedContent = uploadBlob.mock.calls[0][2];
262
+ expect(uploadedContent).toBe(content);
263
+ });
264
+
265
+ it("should handle empty or malformed SBI configuration gracefully", async () => {
266
+ const initialYaml =
267
+ "dev:\n sbis: []\ntest:\n sbis: []\nperf-test:\n sbis: []\next-test:\n sbis: []\nprod:\n sbis: []\n";
268
+ config.set("allowlist.devSBIs", " , 123, , 456 "); // should filter empty and trim
269
+ config.set("allowlist.testSBIs", ""); // should skip
270
+ config.set("allowlist.perfTestSBIs", null); // should skip
271
+ config.set("allowlist.extTestSBIs", undefined); // should skip
272
+ config.set("allowlist.prodSBIs", " "); // should skip
273
+
274
+ readFileSync.mockReturnValueOnce(initialYaml);
275
+ readdirSync.mockRestore();
276
+ readdirSync
277
+ .mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
278
+ .mockReturnValueOnce([
279
+ {
280
+ name: "allowlist.yaml",
281
+ isFile: () => true,
282
+ parentPath: "configurations/grant-1",
283
+ },
284
+ ]);
285
+
286
+ await storeConfigVersionAndInformBroker(mockLogger);
287
+
288
+ const uploadedContent = uploadBlob.mock.calls[0][2];
289
+ const parsedContent = YAML.parse(uploadedContent);
290
+ expect(parsedContent["dev"].sbis).toEqual(["123", "456"]);
291
+ expect(parsedContent["test"].sbis).toEqual([]);
292
+ });
293
+
294
+ it("should handle missing sections in allowlist.yaml gracefully", async () => {
295
+ const initialYaml = "dev:\n sbis: []\n";
296
+ // test section is missing in initialYaml
297
+ config.set("allowlist.testSBIs", "123");
298
+
299
+ readFileSync.mockReturnValueOnce(initialYaml);
300
+ readdirSync.mockRestore();
301
+ readdirSync
302
+ .mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
303
+ .mockReturnValueOnce([
304
+ {
305
+ name: "allowlist.yaml",
306
+ isFile: () => true,
307
+ parentPath: "configurations/grant-1",
308
+ },
309
+ ]);
310
+
311
+ await expect(
312
+ storeConfigVersionAndInformBroker(mockLogger),
313
+ ).resolves.not.toThrow();
314
+ });
315
+
316
+ it("should not add dev.sbis if dev.allowAll=true and sbis is not present", async () => {
317
+ const initialYaml = "dev:\n allowAll: true\n";
318
+ config.set("allowlist.devSBIs", "123, 456");
319
+
320
+ readFileSync.mockReturnValueOnce(initialYaml);
321
+ readdirSync.mockRestore();
322
+ readdirSync
323
+ .mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
324
+ .mockReturnValueOnce([
325
+ {
326
+ name: "allowlist.yaml",
327
+ isFile: () => true,
328
+ parentPath: "configurations/grant-1",
329
+ },
330
+ ]);
331
+
332
+ await storeConfigVersionAndInformBroker(mockLogger);
333
+
334
+ const uploadedContent = uploadBlob.mock.calls[0][2];
335
+ const parsedContent = YAML.parse(uploadedContent);
336
+
337
+ expect(parsedContent["dev"].allowAll).toBe(true);
338
+ expect(parsedContent["dev"].sbis).toBeUndefined();
339
+ });
340
+
341
+ it.each([
342
+ ["test", "allowlist.testSBIs"],
343
+ ["perf-test", "allowlist.perfTestSBIs"],
344
+ ["ext-test", "allowlist.extTestSBIs"],
345
+ ["prod", "allowlist.prodSBIs"],
346
+ ])(
347
+ "should not add %s.sbis if allowAll=true and sbis is not present",
348
+ async (env, configKey) => {
349
+ const initialYaml = `${env}:\n allowAll: true\n`;
350
+ config.set(configKey, "123, 456");
351
+
352
+ readFileSync.mockReturnValueOnce(initialYaml);
353
+ readdirSync.mockRestore();
354
+ readdirSync
355
+ .mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
356
+ .mockReturnValueOnce([
357
+ {
358
+ name: "allowlist.yaml",
359
+ isFile: () => true,
360
+ parentPath: "configurations/grant-1",
361
+ },
362
+ ]);
363
+
364
+ await storeConfigVersionAndInformBroker(mockLogger);
365
+
366
+ const uploadedContent = uploadBlob.mock.calls[0][2];
367
+ const parsedContent = YAML.parse(uploadedContent);
368
+
369
+ expect(parsedContent[env].allowAll).toBe(true);
370
+ expect(parsedContent[env].sbis).toBeUndefined();
371
+ },
372
+ );
202
373
  });
203
374
  });