@defra/grants-config-utils 0.0.1

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.
Files changed (38) hide show
  1. package/.changeset/README.md +8 -0
  2. package/.changeset/config.json +14 -0
  3. package/.github/dependabot.yml +28 -0
  4. package/.github/workflows/check-pull-request.yml +44 -0
  5. package/.github/workflows/release.yml +60 -0
  6. package/.husky/pre-commit +1 -0
  7. package/.nvmrc +1 -0
  8. package/.prettierignore +4 -0
  9. package/.scannerwork/.sonar_lock +0 -0
  10. package/.scannerwork/architecture/js/src_broker_broker-auth-helper_js.udg +0 -0
  11. package/.scannerwork/architecture/js/src_broker_broker-auth-helper_test_js.udg +0 -0
  12. package/.scannerwork/architecture/js/src_config_config_js.udg +0 -0
  13. package/.scannerwork/architecture/js/src_s3_s3-client_js.udg +0 -0
  14. package/.scannerwork/architecture/js/src_s3_s3-client_test_js.udg +0 -0
  15. package/.scannerwork/architecture/js/src_s3_s3-interactions_js.udg +0 -0
  16. package/.scannerwork/architecture/js/src_s3_s3-interactions_test_js.udg +0 -0
  17. package/.scannerwork/architecture/js/src_service_config-store-and-inform_js.udg +0 -0
  18. package/.scannerwork/architecture/js/src_service_config-store-and-inform_test_js.udg +0 -0
  19. package/.scannerwork/report-task.txt +7 -0
  20. package/.vite/setup-files.js +15 -0
  21. package/CONTRIBUTING.md +26 -0
  22. package/LICENCE +8 -0
  23. package/README.md +32 -0
  24. package/babel.config.cjs +17 -0
  25. package/ci/release-it.sh +19 -0
  26. package/eslint.config.js +12 -0
  27. package/package.json +92 -0
  28. package/sonar-project.properties +15 -0
  29. package/src/broker/broker-auth-helper.js +74 -0
  30. package/src/broker/broker-auth-helper.test.js +262 -0
  31. package/src/config/config.js +146 -0
  32. package/src/s3/s3-client.js +20 -0
  33. package/src/s3/s3-client.test.js +56 -0
  34. package/src/s3/s3-interactions.js +35 -0
  35. package/src/s3/s3-interactions.test.js +82 -0
  36. package/src/service/config-store-and-inform.js +151 -0
  37. package/src/service/config-store-and-inform.test.js +168 -0
  38. package/vitest.config.js +18 -0
@@ -0,0 +1,8 @@
1
+ # Changesets
2
+
3
+ Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4
+ with multi-package repos, or single-package repos to help you version and publish your code. You can
5
+ find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6
+
7
+ We have a quick list of common questions to get you started engaging with this project in
8
+ [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
@@ -0,0 +1,14 @@
1
+ {
2
+ "$schema": "https://unpkg.com/@changesets/config@3.1.4/schema.json",
3
+ "changelog": [
4
+ "@changesets/changelog-github",
5
+ { "repo": "DEFRA/grants-config-utils" }
6
+ ],
7
+ "commit": false,
8
+ "fixed": [],
9
+ "linked": [],
10
+ "access": "public",
11
+ "baseBranch": "main",
12
+ "updateInternalDependencies": "patch",
13
+ "ignore": []
14
+ }
@@ -0,0 +1,28 @@
1
+ version: 2
2
+
3
+ updates:
4
+ # Update npm packages
5
+ - package-ecosystem: npm
6
+ directory: /
7
+ open-pull-requests-limit: 10
8
+
9
+ # Group packages into shared PR
10
+ groups:
11
+ weekly-dependencies:
12
+ patterns:
13
+ - '*'
14
+ update-types:
15
+ - 'minor'
16
+ - 'patch'
17
+ ignore:
18
+ - dependency-name: 'eslint-*'
19
+ - dependency-name: 'eslint'
20
+ schedule:
21
+ interval: weekly
22
+ day: monday
23
+
24
+ # Update GitHub Actions
25
+ - package-ecosystem: github-actions
26
+ directory: /
27
+ schedule:
28
+ interval: weekly
@@ -0,0 +1,44 @@
1
+ name: Check Pull Request
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - main
7
+ types:
8
+ - opened
9
+ - reopened
10
+ - synchronize
11
+ - ready_for_review
12
+
13
+ jobs:
14
+ cdp-workflows:
15
+ name: Run Pull Request Checks
16
+ runs-on: ubuntu-latest
17
+ steps:
18
+ - uses: actions/checkout@v6
19
+ - uses: actions/setup-node@v6
20
+ with:
21
+ node-version-file: .nvmrc
22
+ registry-url: "https://registry.npmjs.org/"
23
+ cache: npm
24
+ - name: Install dependencies
25
+ run: npm install --ignore-scripts
26
+
27
+ - name: Scan security vulnerabilities
28
+ run: npm audit --audit-level=high --omit dev
29
+
30
+ - name: Lint
31
+ run: npm run lint
32
+
33
+ - name: Format check
34
+ run: npm run prettier
35
+
36
+ - name: Test
37
+ run: npm test
38
+
39
+ - name: SonarCloud Scan
40
+ if: github.actor != 'dependabot[bot]'
41
+ uses: SonarSource/sonarqube-scan-action@v8.1.0
42
+ env:
43
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
+ SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
@@ -0,0 +1,60 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ permissions:
9
+ contents: write
10
+ packages: write
11
+
12
+ jobs:
13
+ release:
14
+ runs-on: ubuntu-latest
15
+
16
+ steps:
17
+ - name: Checkout repository
18
+ uses: actions/checkout@v6
19
+ with:
20
+ fetch-depth: 0
21
+ fetch-tags: true
22
+
23
+ - name: Set up Node.js
24
+ uses: actions/setup-node@v6
25
+ with:
26
+ node-version-file: .nvmrc
27
+ registry-url: "https://registry.npmjs.org/"
28
+
29
+ - name: Install dependencies
30
+ run: npm ci --ignore-scripts
31
+
32
+ - name: Run tests
33
+ run: npm test
34
+
35
+ - name: SonarCloud Scan
36
+ if: github.actor != 'dependabot[bot]'
37
+ uses: SonarSource/sonarqube-scan-action@v8.1.0
38
+ env:
39
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40
+ SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
41
+
42
+ - name: Version changed packages
43
+ run: npm run version:changed
44
+
45
+ - name: Commit version bumps & push
46
+ run: |
47
+ git config user.name "github-actions[bot]"
48
+ git config user.email "github-actions[bot]@users.noreply.github.com"
49
+ git add .
50
+ git commit -m "Version package [skip ci]" || echo "No changes to commit"
51
+ git push
52
+ - name: Create Git tag & GitHub Release
53
+ run: npm run github:release
54
+ env:
55
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56
+
57
+ - name: Publish changed packages to npm
58
+ run: npm run publish:changed
59
+ env:
60
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1 @@
1
+ npx lint-staged
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ v24.15.0
@@ -0,0 +1,4 @@
1
+ package-lock.json
2
+ node_modules
3
+ coverage
4
+ scripts
File without changes
@@ -0,0 +1,7 @@
1
+ organization=defra
2
+ projectKey=DEFRA_grants-config-utils
3
+ serverUrl=https://sonarcloud.io
4
+ serverVersion=8.0.0.88577
5
+ dashboardUrl=https://sonarcloud.io/dashboard?id=DEFRA_grants-config-utils&branch=main
6
+ ceTaskId=AZ5utCgLLZ8POhXHmJsR
7
+ ceTaskUrl=https://sonarcloud.io/api/ce/task?id=AZ5utCgLLZ8POhXHmJsR
@@ -0,0 +1,15 @@
1
+ import { afterAll, beforeAll } from "vitest";
2
+ import createFetchMock from "vitest-fetch-mock";
3
+
4
+ const fetchMock = createFetchMock(vi);
5
+
6
+ beforeAll(async () => {
7
+ // Setup fetch mock
8
+ fetchMock.enableMocks();
9
+ global.fetch = fetchMock;
10
+ global.fetchMock = fetchMock;
11
+ });
12
+
13
+ afterAll(async () => {
14
+ fetchMock.disableMocks();
15
+ });
@@ -0,0 +1,26 @@
1
+ # Contributing
2
+
3
+ ## Coding Standard
4
+
5
+ - The code needs to adhere to the [Defra JavaScript Standards](https://defra.github.io/software-development-standards/standards/javascript_standards/)
6
+ - Use [neostandard](https://github.com/neostandard/neostandard) to lint your code
7
+
8
+ ## Commit Messages
9
+
10
+ Use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/).
11
+
12
+ By default, a commit into the trunk will cause a `minor` version increment. If you need either a `major` or `patch` version, append `#major` or `#patch` to the commit message.
13
+
14
+ ## Making changes
15
+
16
+ Install [pre-commit](https://pre-commit.com/), as it is used to scan commits for secrets using [Gitleaks](https://github.com/gitleaks/gitleaks).
17
+
18
+ To make changes to this library, please follow the steps below:
19
+ 1. Make the changes as usual to the codebase. Ensure your changes are exported via the index.js file!
20
+ 2. Run `npm run version:create` OR `npx @changesets/cli` to create a changeset file. This will guide you through the process of documenting your changes.`
21
+ 3. Ensure the changeset .md file is added to the commit (should be done automatically now).
22
+ 4. Push to branch, and create a pull request.
23
+ 5. Github actions will verify the build
24
+ 6. After review, merge the pull request.
25
+ 7. Github action will automatically increment the version and publish to NPM.
26
+
package/LICENCE ADDED
@@ -0,0 +1,8 @@
1
+ The Open Government Licence (OGL) Version 3
2
+
3
+ Copyright (c) 2022 Defra
4
+
5
+ This source code is licensed under the Open Government Licence v3.0. To view this
6
+ licence, visit www.nationalarchives.gov.uk/doc/open-government-licence/version/3
7
+ or write to the Information Policy Team, The National Archives, Kew, Richmond,
8
+ Surrey, TW9 4DU.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # grants-config-utils
2
+
3
+ NPM module for providing utils for publishing grants config
4
+
5
+ ## Usage
6
+
7
+ ### Installation
8
+
9
+ ```
10
+ npm install --save @defra/grants-config-utils
11
+
12
+ ```
13
+
14
+ ## Making changes
15
+
16
+ Refer to the [contributing documentation](CONTRIBUTING.md).
17
+
18
+ ## Licence
19
+
20
+ THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:
21
+
22
+ <http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3>
23
+
24
+ The following attribution statement MUST be cited in your products and applications when using this information.
25
+
26
+ > Contains public sector information licensed under the Open Government license v3
27
+
28
+ ### About the licence
29
+
30
+ The Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.
31
+
32
+ It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.
@@ -0,0 +1,17 @@
1
+ const { NODE_ENV } = process.env
2
+
3
+ module.exports = {
4
+ presets: [
5
+ [
6
+ '@babel/preset-env',
7
+ {
8
+ modules: NODE_ENV === 'test' ? 'auto' : false
9
+ }
10
+ ]
11
+ ],
12
+ env: {
13
+ test: {
14
+ plugins: ['babel-plugin-transform-import-meta']
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ name=$1
6
+
7
+ version=$(jq -r '.version' "package.json")
8
+ tag="${name}-${version}"
9
+
10
+ echo "Processing $name ($tag) from: $(pwd)"
11
+
12
+ if git rev-parse "$tag" >/dev/null 2>&1; then
13
+ echo "Tag $tag already exists — skipping tagging & GitHub release."
14
+ else
15
+ echo "Tag does not exist — running release-it for $tag..."
16
+ npx release-it@19.0.4 --no-increment --ci || echo "Release-it failed for $tag"
17
+ fi
18
+
19
+ echo "Finished processing $tag"
@@ -0,0 +1,12 @@
1
+ import neostandard from "neostandard";
2
+ import eslintConfigPrettier from "eslint-config-prettier/flat";
3
+
4
+ export default [
5
+ ...neostandard({
6
+ env: ["node", "vitest"],
7
+ ignores: [...neostandard.resolveIgnoresFromGitignore()],
8
+ noJsx: true,
9
+ noStyle: true,
10
+ }),
11
+ eslintConfigPrettier,
12
+ ];
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "@defra/grants-config-utils",
3
+ "version": "0.0.1",
4
+ "description": "NPM module for providing utils for publishing grants config",
5
+ "engines": {
6
+ "node": ">=24",
7
+ "npm": ">=11.10.0"
8
+ },
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/DEFRA/grants-config-utils.git"
15
+ },
16
+ "type": "module",
17
+ "scripts": {
18
+ "prepare": "husky",
19
+ "github:release": "npx cdp-github-release @defra/grants-config-utils",
20
+ "lint": "eslint --cache --cache-strategy content \"**/*.{cjs,js}\"",
21
+ "lint:fix": "npm run lint -- --fix",
22
+ "prettier": "prettier --check \"**/*.{cjs,js,json}\"",
23
+ "prettier:fix": "prettier --write \"**/*.{cjs,js,json}\"",
24
+ "publish:changed": "changeset publish",
25
+ "test": "TZ=UTC vitest run --coverage",
26
+ "version:changed": "changeset version",
27
+ "version:create": "changeset"
28
+ },
29
+ "author": "Defra Grants Platform Experience Team",
30
+ "main": "src/service/config-store-and-inform.js",
31
+ "exports": {
32
+ ".": "./src/service/config-store-and-inform.js",
33
+ "./s3-interactions": "./src/s3/s3-interactions.js",
34
+ "./config": "./src/config/config.js",
35
+ "./broker": "./src/broker/broker-auth-helper.js"
36
+ },
37
+ "license": "OGL-UK-3.0",
38
+ "bugs": {
39
+ "url": "https://github.com/DEFRA/grants-config-utils/issues"
40
+ },
41
+ "homepage": "https://github.com/DEFRA/grants-config-utils#readme",
42
+ "dependencies": {
43
+ "@aws-sdk/client-s3": "3.1047.0",
44
+ "convict": "6.2.5",
45
+ "convict-format-with-validator": "6.2.0"
46
+ },
47
+ "devDependencies": {
48
+ "@babel/core": "7.29.0",
49
+ "@babel/preset-env": "7.29.5",
50
+ "@changesets/changelog-github": "0.7.0",
51
+ "@changesets/cli": "2.31.0",
52
+ "@vitest/coverage-v8": "4.1.6",
53
+ "eslint": "9.39.4",
54
+ "eslint-config-prettier": "10.1.1",
55
+ "husky": "9.1.7",
56
+ "lint-staged": "17.0.5",
57
+ "neostandard": "0.13.0",
58
+ "prettier": "3.8.3",
59
+ "vitest": "4.1.6",
60
+ "vitest-fetch-mock": "0.4.5"
61
+ },
62
+ "release-it": {
63
+ "npm": {
64
+ "publish": false
65
+ },
66
+ "github": {
67
+ "release": true,
68
+ "releaseName": "Release ${version}",
69
+ "releaseNotes": "echo ''"
70
+ },
71
+ "git": {
72
+ "commitMessage": "Tagged @defra/grants-config-utils ${version}",
73
+ "tagName": "@defra/grants-config-utils-${version}",
74
+ "requireCleanWorkingDir": false,
75
+ "commitArgs": [
76
+ "--no-verify"
77
+ ]
78
+ }
79
+ },
80
+ "bin": {
81
+ "cdp-github-release": "ci/release-it.sh"
82
+ },
83
+ "lint-staged": {
84
+ "*.js": [
85
+ "eslint --fix",
86
+ "prettier --write"
87
+ ],
88
+ "*.{json,md}": [
89
+ "prettier --write"
90
+ ]
91
+ }
92
+ }
@@ -0,0 +1,15 @@
1
+ sonar.projectKey=DEFRA_grants-config-utils
2
+ sonar.organization=defra
3
+
4
+ sonar.links.homepage=https://github.com/DEFRA/grants-config-utils
5
+ sonar.links.ci=https://github.com/DEFRA/grants-config-utils/actions
6
+ sonar.links.scm=https://github.com/DEFRA/grants-config-utils
7
+ sonar.links.issue=https://github.com/DEFRA/grants-config-utils/issues
8
+
9
+ sonar.sources=src/
10
+ sonar.exclusions=src/**/*.test.js
11
+ sonar.coverage.exclusions=src/config/config.js
12
+ sonar.tests=src/
13
+ sonar.test.inclusions=src/**/*.test.js
14
+
15
+ sonar.javascript.lcov.reportPaths=./coverage/lcov.info
@@ -0,0 +1,74 @@
1
+ import { config } from "../config/config.js";
2
+ import crypto from "node:crypto";
3
+
4
+ const IV_LENGTH_BYTES = 12;
5
+ const KEY_LENGTH_BYTES = 32;
6
+ const SCRYPT_SALT = "salt";
7
+ const CIPHER_ALGORITHM = "aes-256-gcm";
8
+ /** @type {Record<string, BufferEncoding>} */
9
+ const ENCODING = {
10
+ UTF8: "utf8",
11
+ BASE64: "base64",
12
+ };
13
+
14
+ const CONTENT_TYPE_JSON = "application/json";
15
+ const AUTH_SCHEME = "Bearer";
16
+ const GRANTS_CONFIG_BROKER_AUTH_TOKEN = config.get("configBroker.auth.token");
17
+ const ENCRYPTION_KEY = config.get("configBroker.auth.encryptionKey");
18
+ /**
19
+ * Encrypts the bearer token using AES-256-GCM
20
+ * @param {string} token - The token to encrypt
21
+ * @param {string} encryptionKey - Encryption key
22
+ * @returns {string} Encrypted token in format: iv:authTag:encryptedData (base64)
23
+ */
24
+ export function encryptToken(token, encryptionKey) {
25
+ const iv = crypto.randomBytes(IV_LENGTH_BYTES);
26
+ const key = crypto.scryptSync(encryptionKey, SCRYPT_SALT, KEY_LENGTH_BYTES);
27
+ const cipher = crypto.createCipheriv(CIPHER_ALGORITHM, key, iv);
28
+
29
+ let encrypted = cipher.update(token, ENCODING.UTF8, ENCODING.BASE64);
30
+ encrypted += cipher.final(ENCODING.BASE64);
31
+
32
+ const authTag = cipher.getAuthTag();
33
+
34
+ return `${iv.toString(ENCODING.BASE64)}:${authTag.toString(ENCODING.BASE64)}:${encrypted}`;
35
+ }
36
+
37
+ /**
38
+ * Creates headers for authenticating with the config-broker API
39
+ * @param {string} token - Auth token
40
+ * @param {string} encryptionKey - Encryption key
41
+ * @param {object} baseHeaders - Base headers to extend
42
+ * @returns {object} Headers with authentication if token is available
43
+ */
44
+ export function createAuthenticatedHeaders(
45
+ token,
46
+ encryptionKey,
47
+ baseHeaders = {},
48
+ ) {
49
+ const headers = { ...baseHeaders };
50
+
51
+ if (token) {
52
+ const encryptedToken = encryptToken(token, encryptionKey);
53
+ const authCredentials = Buffer.from(encryptedToken).toString(
54
+ ENCODING.BASE64,
55
+ );
56
+ headers.Authorization = `${AUTH_SCHEME} ${authCredentials}`;
57
+ }
58
+
59
+ return headers;
60
+ }
61
+
62
+ /**
63
+ * Creates standard headers for API requests to grants-config-broker
64
+ * @returns {object} Headers with Content-Type and authentication
65
+ */
66
+ export function createApiHeadersForConfigBroker() {
67
+ return createAuthenticatedHeaders(
68
+ GRANTS_CONFIG_BROKER_AUTH_TOKEN,
69
+ ENCRYPTION_KEY,
70
+ {
71
+ "Content-Type": CONTENT_TYPE_JSON,
72
+ },
73
+ );
74
+ }