@defra/grants-config-utils 0.6.0 → 1.0.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/.github/workflows/check-pull-request.yml +4 -4
- package/.github/workflows/release.yml +3 -3
- package/AGENTS.md +4 -0
- package/CHANGELOG.md +9 -0
- package/package.json +17 -10
- package/src/broker/broker-auth-helper.js +7 -60
- package/src/broker/broker-auth-helper.test.js +50 -231
- package/src/config/config.js +10 -12
- package/src/service/config-store-and-inform.js +3 -36
- package/src/service/config-store-and-inform.test.js +7 -60
- package/src/sts/grants-config-broker-token.js +19 -0
- package/src/sts/grants-config-broker-token.test.js +44 -0
- package/src/sts/sts-client-plugin.js +19 -0
- package/src/sts/sts-client-plugin.test.js +76 -0
|
@@ -11,18 +11,18 @@ on:
|
|
|
11
11
|
- ready_for_review
|
|
12
12
|
|
|
13
13
|
permissions:
|
|
14
|
-
contents:
|
|
14
|
+
contents: write
|
|
15
15
|
|
|
16
16
|
jobs:
|
|
17
17
|
cdp-workflows:
|
|
18
18
|
name: Run Pull Request Checks
|
|
19
19
|
runs-on: ubuntu-latest
|
|
20
20
|
steps:
|
|
21
|
-
- uses: actions/checkout@
|
|
21
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
|
|
22
22
|
with:
|
|
23
23
|
fetch-depth: 0
|
|
24
24
|
|
|
25
|
-
- uses: actions/setup-node@
|
|
25
|
+
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 #v7.0.0
|
|
26
26
|
with:
|
|
27
27
|
node-version-file: .nvmrc
|
|
28
28
|
registry-url: "https://registry.npmjs.org/"
|
|
@@ -46,7 +46,7 @@ jobs:
|
|
|
46
46
|
- name: SonarCloud Scan
|
|
47
47
|
if: github.actor != 'dependabot[bot]' &&
|
|
48
48
|
!startsWith(github.head_ref, 'changeset-release/')
|
|
49
|
-
uses: SonarSource/sonarqube-scan-action@
|
|
49
|
+
uses: SonarSource/sonarqube-scan-action@22918119ff8e1ca75a623e15c8296b6ea4fbe28f #8.2.1
|
|
50
50
|
env:
|
|
51
51
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
52
52
|
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
|
@@ -25,13 +25,13 @@ jobs:
|
|
|
25
25
|
|
|
26
26
|
steps:
|
|
27
27
|
- name: Checkout repository
|
|
28
|
-
uses: actions/checkout@
|
|
28
|
+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
|
|
29
29
|
with:
|
|
30
30
|
fetch-depth: 0
|
|
31
31
|
fetch-tags: true
|
|
32
32
|
|
|
33
33
|
- name: Set up Node.js
|
|
34
|
-
uses: actions/setup-node@
|
|
34
|
+
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 #v7.0.0
|
|
35
35
|
with:
|
|
36
36
|
node-version-file: .nvmrc
|
|
37
37
|
registry-url: "https://registry.npmjs.org/"
|
|
@@ -44,7 +44,7 @@ jobs:
|
|
|
44
44
|
|
|
45
45
|
- name: SonarCloud Scan
|
|
46
46
|
if: github.actor != 'dependabot[bot]' && github.actor != 'github-actions[bot]' && !contains(github.event.head_commit.message, 'Create new config version')
|
|
47
|
-
uses: SonarSource/sonarqube-scan-action@
|
|
47
|
+
uses: SonarSource/sonarqube-scan-action@22918119ff8e1ca75a623e15c8296b6ea4fbe28f #8.2.1
|
|
48
48
|
env:
|
|
49
49
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
50
50
|
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
package/AGENTS.md
CHANGED
|
@@ -27,3 +27,7 @@ Developers can add their own `AGENTS.local.md` and should be read as an addendum
|
|
|
27
27
|
## Testing Guidelines
|
|
28
28
|
|
|
29
29
|
Add or update focused unit tests with shared utility changes. Run the relevant Vitest file, then `npm test`, `npm run lint`, and `npm run format:check`.
|
|
30
|
+
|
|
31
|
+
## Command preference
|
|
32
|
+
|
|
33
|
+
Use the `npm run` command whenever possible, and avoid `npx` where possible.
|
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra/grants-config-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "NPM module for providing utils for publishing grants config",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24",
|
|
@@ -33,7 +33,9 @@
|
|
|
33
33
|
"./s3-interactions": "./src/s3/s3-interactions.js",
|
|
34
34
|
"./sns-client": "./src/sns/sns-client.js",
|
|
35
35
|
"./config": "./src/config/config.js",
|
|
36
|
-
"./broker": "./src/broker/broker-auth-helper.js"
|
|
36
|
+
"./broker": "./src/broker/broker-auth-helper.js",
|
|
37
|
+
"./sts-client-plugin": "./src/sts/sts-client-plugin.js",
|
|
38
|
+
"./grants-config-broker-token": "./src/sts/grants-config-broker-token.js"
|
|
37
39
|
},
|
|
38
40
|
"license": "OGL-UK-3.0",
|
|
39
41
|
"bugs": {
|
|
@@ -41,8 +43,9 @@
|
|
|
41
43
|
},
|
|
42
44
|
"homepage": "https://github.com/DEFRA/grants-config-utils#readme",
|
|
43
45
|
"dependencies": {
|
|
44
|
-
"@aws-sdk/client-s3": "3.
|
|
45
|
-
"@aws-sdk/client-sns": "3.
|
|
46
|
+
"@aws-sdk/client-s3": "3.1091.0",
|
|
47
|
+
"@aws-sdk/client-sns": "3.1091.0",
|
|
48
|
+
"@aws-sdk/client-sts": "3.1091.0",
|
|
46
49
|
"convict": "6.2.5",
|
|
47
50
|
"convict-format-with-validator": "6.2.0"
|
|
48
51
|
},
|
|
@@ -50,17 +53,21 @@
|
|
|
50
53
|
"@babel/core": "7.29.7",
|
|
51
54
|
"@babel/preset-env": "7.29.7",
|
|
52
55
|
"@changesets/changelog-github": "0.7.0",
|
|
53
|
-
"@changesets/cli": "2.31.
|
|
54
|
-
"@vitest/coverage-v8": "4.1.
|
|
55
|
-
"eslint": "
|
|
56
|
+
"@changesets/cli": "2.31.1",
|
|
57
|
+
"@vitest/coverage-v8": "4.1.10",
|
|
58
|
+
"eslint": "10.7.0",
|
|
56
59
|
"eslint-config-prettier": "10.1.1",
|
|
57
60
|
"husky": "9.1.7",
|
|
58
|
-
"lint-staged": "17.0
|
|
61
|
+
"lint-staged": "17.1.0",
|
|
59
62
|
"neostandard": "0.13.0",
|
|
60
|
-
"prettier": "3.
|
|
61
|
-
"vitest": "4.1.
|
|
63
|
+
"prettier": "3.9.5",
|
|
64
|
+
"vitest": "4.1.10",
|
|
62
65
|
"vitest-fetch-mock": "0.4.5"
|
|
63
66
|
},
|
|
67
|
+
"overrides": {
|
|
68
|
+
"brace-expansion": "5.0.8",
|
|
69
|
+
"eslint": "10.7.0"
|
|
70
|
+
},
|
|
64
71
|
"release-it": {
|
|
65
72
|
"npm": {
|
|
66
73
|
"publish": false
|
|
@@ -1,74 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import crypto from "node:crypto";
|
|
1
|
+
import { generateToken } from "../sts/grants-config-broker-token.js";
|
|
3
2
|
|
|
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
3
|
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
4
|
|
|
37
5
|
/**
|
|
38
|
-
* Creates headers for authenticating with the config-broker API
|
|
39
|
-
* @param {
|
|
40
|
-
* @param {string} encryptionKey - Encryption key
|
|
6
|
+
* Creates headers for authenticating with the config-broker API using service to service authentication
|
|
7
|
+
* @param {object} requestOrServer - Request or Server object
|
|
41
8
|
* @param {object} baseHeaders - Base headers to extend
|
|
42
9
|
* @returns {object} Headers with authentication if token is available
|
|
43
10
|
*/
|
|
44
|
-
export function createAuthenticatedHeaders(
|
|
45
|
-
|
|
46
|
-
encryptionKey,
|
|
11
|
+
export async function createAuthenticatedHeaders(
|
|
12
|
+
requestOrServer,
|
|
47
13
|
baseHeaders = {},
|
|
48
14
|
) {
|
|
49
15
|
const headers = { ...baseHeaders };
|
|
50
16
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const authCredentials = Buffer.from(encryptedToken).toString(
|
|
54
|
-
ENCODING.BASE64,
|
|
55
|
-
);
|
|
56
|
-
headers.Authorization = `${AUTH_SCHEME} ${authCredentials}`;
|
|
57
|
-
}
|
|
17
|
+
const authCredentials = await generateToken(requestOrServer.sts);
|
|
18
|
+
headers.Authorization = `${AUTH_SCHEME} ${authCredentials}`;
|
|
58
19
|
|
|
59
20
|
return headers;
|
|
60
21
|
}
|
|
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
|
-
}
|
|
@@ -1,262 +1,81 @@
|
|
|
1
|
-
import { vi } from "vitest";
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { createAuthenticatedHeaders } from "./broker-auth-helper.js";
|
|
3
|
+
import { generateToken } from "../sts/grants-config-broker-token.js";
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const HEADER_OBJECTS = {
|
|
7
|
-
CONTENT_TYPE_JSON: { "Content-Type": CONTENT_TYPE_JSON },
|
|
8
|
-
EMPTY: {},
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const CONFIG_AUTH_TOKEN = "configBroker.auth.token";
|
|
12
|
-
const CONFIG_ENCRYPTION_KEY = "configBroker.auth.encryptionKey";
|
|
13
|
-
|
|
14
|
-
const MOCK_TOKENS = {
|
|
15
|
-
DEFAULT: "test-token-123",
|
|
16
|
-
API: "api-test-token",
|
|
17
|
-
ALTERNATIVE: "test-token-456",
|
|
18
|
-
THIRD: "test-token-789",
|
|
19
|
-
MUTATION_TEST: "test-token-mutation",
|
|
20
|
-
SECRET: "my-secret-token",
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const TEST_HEADERS = {
|
|
24
|
-
USER_AGENT: "test-agent",
|
|
25
|
-
CUSTOM_VALUE: "custom-value",
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const mockConfigGet = vi.fn((key) => {
|
|
29
|
-
if (key === "configBroker.auth.token") {
|
|
30
|
-
return "default-token";
|
|
31
|
-
}
|
|
32
|
-
if (key === "configBroker.auth.encryptionKey") {
|
|
33
|
-
return TEST_ENCRYPTION_KEY;
|
|
34
|
-
}
|
|
35
|
-
return null;
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
vi.mock("../config/config.js", () => ({
|
|
39
|
-
config: {
|
|
40
|
-
get: mockConfigGet,
|
|
41
|
-
},
|
|
5
|
+
vi.mock("../sts/grants-config-broker-token.js", () => ({
|
|
6
|
+
generateToken: vi.fn(),
|
|
42
7
|
}));
|
|
43
8
|
|
|
44
|
-
async function importConfigBrokerHelper() {
|
|
45
|
-
return await import("./broker-auth-helper.js");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function setupMockConfig(value, encryptionKey = TEST_ENCRYPTION_KEY) {
|
|
49
|
-
const configValues = {
|
|
50
|
-
[CONFIG_AUTH_TOKEN]: value,
|
|
51
|
-
[CONFIG_ENCRYPTION_KEY]: encryptionKey,
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
mockConfigGet.mockImplementation((key) => configValues[key] || null);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
9
|
describe("Broker Auth Helper", () => {
|
|
10
|
+
let mockStsClient;
|
|
11
|
+
|
|
58
12
|
beforeEach(() => {
|
|
59
13
|
vi.clearAllMocks();
|
|
60
|
-
|
|
14
|
+
mockStsClient = {
|
|
15
|
+
send: vi.fn(),
|
|
16
|
+
};
|
|
61
17
|
});
|
|
62
18
|
|
|
63
|
-
describe("
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
19
|
+
describe("createAuthenticatedHeaders", () => {
|
|
20
|
+
it("should add Authorization header with Bearer token from generateToken", async () => {
|
|
21
|
+
const mockToken = "mock-sts-token";
|
|
22
|
+
vi.mocked(generateToken).mockResolvedValue(mockToken);
|
|
23
|
+
const baseHeaders = { "Content-Type": "application/json" };
|
|
67
24
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
);
|
|
25
|
+
const headers = await createAuthenticatedHeaders(
|
|
26
|
+
{ sts: mockStsClient },
|
|
27
|
+
baseHeaders,
|
|
28
|
+
);
|
|
73
29
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
30
|
+
expect(generateToken).toHaveBeenCalledWith(mockStsClient);
|
|
31
|
+
expect(headers).toEqual({
|
|
32
|
+
"Content-Type": "application/json",
|
|
33
|
+
Authorization: "Bearer mock-sts-token",
|
|
77
34
|
});
|
|
78
35
|
});
|
|
79
36
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
37
|
+
it("should work with empty base headers", async () => {
|
|
38
|
+
const mockToken = "mock-sts-token";
|
|
39
|
+
vi.mocked(generateToken).mockResolvedValue(mockToken);
|
|
83
40
|
|
|
84
|
-
|
|
85
|
-
await importConfigBrokerHelper();
|
|
86
|
-
const headers = createApiHeadersForConfigBroker(
|
|
87
|
-
HEADER_OBJECTS.CONTENT_TYPE_JSON,
|
|
88
|
-
);
|
|
41
|
+
const headers = await createAuthenticatedHeaders({ sts: mockStsClient });
|
|
89
42
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
expect(headers).toHaveProperty("Authorization");
|
|
93
|
-
expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
|
|
94
|
-
|
|
95
|
-
const base64Part = headers.Authorization.replace("Bearer ", "");
|
|
96
|
-
const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
|
|
97
|
-
expect(decoded).toMatch(
|
|
98
|
-
/^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
|
|
99
|
-
);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it("should work with empty base headers", async () => {
|
|
103
|
-
setupMockConfig(MOCK_TOKENS.ALTERNATIVE);
|
|
104
|
-
|
|
105
|
-
const { createApiHeadersForConfigBroker } =
|
|
106
|
-
await importConfigBrokerHelper();
|
|
107
|
-
const headers = createApiHeadersForConfigBroker(undefined);
|
|
108
|
-
|
|
109
|
-
expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_AUTH_TOKEN);
|
|
110
|
-
expect(headers).toHaveProperty("Authorization");
|
|
111
|
-
expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
|
|
112
|
-
|
|
113
|
-
const base64Part = headers.Authorization.replace("Bearer ", "");
|
|
114
|
-
const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
|
|
115
|
-
expect(decoded).toMatch(
|
|
116
|
-
/^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
|
|
117
|
-
);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it("should preserve all base headers when adding auth", async () => {
|
|
121
|
-
const baseHeaders = {
|
|
122
|
-
"Content-Type": CONTENT_TYPE_JSON,
|
|
123
|
-
"User-Agent": TEST_HEADERS.USER_AGENT,
|
|
124
|
-
"X-Custom-Header": TEST_HEADERS.CUSTOM_VALUE,
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
const { createAuthenticatedHeaders } = await importConfigBrokerHelper();
|
|
128
|
-
const headers = createAuthenticatedHeaders(
|
|
129
|
-
MOCK_TOKENS.THIRD,
|
|
130
|
-
TEST_ENCRYPTION_KEY,
|
|
131
|
-
baseHeaders,
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_AUTH_TOKEN);
|
|
135
|
-
expect(headers).toHaveProperty("Content-Type", CONTENT_TYPE_JSON);
|
|
136
|
-
expect(headers).toHaveProperty("User-Agent", TEST_HEADERS.USER_AGENT);
|
|
137
|
-
expect(headers).toHaveProperty(
|
|
138
|
-
"X-Custom-Header",
|
|
139
|
-
TEST_HEADERS.CUSTOM_VALUE,
|
|
140
|
-
);
|
|
141
|
-
expect(headers).toHaveProperty("Authorization");
|
|
142
|
-
expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
|
|
143
|
-
|
|
144
|
-
const base64Part = headers.Authorization.replace("Bearer ", "");
|
|
145
|
-
const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
|
|
146
|
-
expect(decoded).toMatch(
|
|
147
|
-
/^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
|
|
148
|
-
);
|
|
43
|
+
expect(headers).toEqual({
|
|
44
|
+
Authorization: "Bearer mock-sts-token",
|
|
149
45
|
});
|
|
150
46
|
});
|
|
151
47
|
|
|
152
48
|
it("should not mutate original base headers object", async () => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
setupMockConfig(MOCK_TOKENS.MUTATION_TEST);
|
|
156
|
-
|
|
157
|
-
const { createApiHeadersForConfigBroker } =
|
|
158
|
-
await importConfigBrokerHelper();
|
|
159
|
-
const headers = createApiHeadersForConfigBroker(baseHeaders);
|
|
160
|
-
|
|
161
|
-
expect(baseHeaders).toEqual(HEADER_OBJECTS.CONTENT_TYPE_JSON);
|
|
162
|
-
expect(baseHeaders.Authorization).toBeUndefined();
|
|
163
|
-
expect(headers.Authorization).toBeDefined();
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it("should handle null token correctly", async () => {
|
|
167
|
-
setupMockConfig(null, TEST_ENCRYPTION_KEY);
|
|
49
|
+
vi.mocked(generateToken).mockResolvedValue("token");
|
|
50
|
+
const baseHeaders = { "Content-Type": "application/json" };
|
|
168
51
|
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
"Content-Type": CONTENT_TYPE_JSON,
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
expect(headers).toEqual(HEADER_OBJECTS.CONTENT_TYPE_JSON);
|
|
176
|
-
expect(headers.Authorization).toBeUndefined();
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
describe("createApiHeaders", () => {
|
|
181
|
-
it("should return headers with Content-Type and no auth when no token configured", async () => {
|
|
182
|
-
setupMockConfig("", TEST_ENCRYPTION_KEY);
|
|
183
|
-
|
|
184
|
-
const { createApiHeadersForConfigBroker } =
|
|
185
|
-
await importConfigBrokerHelper();
|
|
186
|
-
const headers = createApiHeadersForConfigBroker();
|
|
187
|
-
|
|
188
|
-
expect(headers).toEqual(HEADER_OBJECTS.CONTENT_TYPE_JSON);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it("should return headers with Content-Type and Authorization when token configured", async () => {
|
|
192
|
-
setupMockConfig(MOCK_TOKENS.API);
|
|
193
|
-
|
|
194
|
-
const { createApiHeadersForConfigBroker } =
|
|
195
|
-
await importConfigBrokerHelper();
|
|
196
|
-
const headers = createApiHeadersForConfigBroker();
|
|
197
|
-
|
|
198
|
-
expect(headers).toHaveProperty("Content-Type", "application/json");
|
|
199
|
-
expect(headers).toHaveProperty("Authorization");
|
|
200
|
-
expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
|
|
201
|
-
|
|
202
|
-
const base64Part = headers.Authorization.replace("Bearer ", "");
|
|
203
|
-
const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
|
|
204
|
-
expect(decoded).toMatch(
|
|
205
|
-
/^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
|
|
206
|
-
);
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
describe("Base64 encoding", () => {
|
|
211
|
-
it("should encode encrypted token with blank username correctly", async () => {
|
|
212
|
-
setupMockConfig(MOCK_TOKENS.SECRET);
|
|
213
|
-
|
|
214
|
-
const { createApiHeadersForConfigBroker } =
|
|
215
|
-
await importConfigBrokerHelper();
|
|
216
|
-
const headers = createApiHeadersForConfigBroker();
|
|
217
|
-
const authHeader = headers.Authorization;
|
|
218
|
-
|
|
219
|
-
const base64Part = authHeader.replace("Bearer ", "");
|
|
220
|
-
const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
|
|
221
|
-
|
|
222
|
-
expect(decoded).toMatch(
|
|
223
|
-
/^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
|
|
52
|
+
const headers = await createAuthenticatedHeaders(
|
|
53
|
+
{ sts: mockStsClient },
|
|
54
|
+
baseHeaders,
|
|
224
55
|
);
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
56
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
// No longer needed with new mock structure
|
|
57
|
+
expect(headers).not.toBe(baseHeaders);
|
|
58
|
+
expect(baseHeaders).toEqual({ "Content-Type": "application/json" });
|
|
59
|
+
expect(baseHeaders.Authorization).toBeUndefined();
|
|
232
60
|
});
|
|
233
61
|
|
|
234
|
-
it("should
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
62
|
+
it("should preserve all base headers", async () => {
|
|
63
|
+
vi.mocked(generateToken).mockResolvedValue("token");
|
|
64
|
+
const baseHeaders = {
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
"User-Agent": "test-agent",
|
|
67
|
+
"X-Custom-Header": "custom-value",
|
|
238
68
|
};
|
|
239
69
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
await importConfigBrokerHelper();
|
|
244
|
-
const headers = createApiHeadersForConfigBroker(
|
|
245
|
-
HEADER_OBJECTS.CONTENT_TYPE_JSON,
|
|
70
|
+
const headers = await createAuthenticatedHeaders(
|
|
71
|
+
{ sts: mockStsClient },
|
|
72
|
+
baseHeaders,
|
|
246
73
|
);
|
|
247
74
|
|
|
248
|
-
expect(
|
|
249
|
-
expect(
|
|
250
|
-
expect(headers
|
|
251
|
-
expect(headers
|
|
252
|
-
|
|
253
|
-
const base64Part = headers.Authorization.replace("Bearer ", "");
|
|
254
|
-
const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
|
|
255
|
-
const tokenPart = decoded.substring(1);
|
|
256
|
-
|
|
257
|
-
expect(tokenPart).toMatch(
|
|
258
|
-
/^[A-Za-z0-9+/]+=*:[A-Za-z0-9+/]+=*:[A-Za-z0-9+/]+=*$/,
|
|
259
|
-
);
|
|
75
|
+
expect(headers).toHaveProperty("Content-Type", "application/json");
|
|
76
|
+
expect(headers).toHaveProperty("User-Agent", "test-agent");
|
|
77
|
+
expect(headers).toHaveProperty("X-Custom-Header", "custom-value");
|
|
78
|
+
expect(headers).toHaveProperty("Authorization", "Bearer token");
|
|
260
79
|
});
|
|
261
80
|
});
|
|
262
81
|
});
|
package/src/config/config.js
CHANGED
|
@@ -49,20 +49,18 @@ export const config = convict({
|
|
|
49
49
|
},
|
|
50
50
|
},
|
|
51
51
|
configBroker: {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
doc: "
|
|
52
|
+
serviceAuth: {
|
|
53
|
+
audience: {
|
|
54
|
+
doc: "JWT audience sent in the token request",
|
|
55
55
|
format: String,
|
|
56
|
-
default: "",
|
|
57
|
-
env: "
|
|
58
|
-
sensitive: true,
|
|
56
|
+
default: "grants-config-broker",
|
|
57
|
+
env: "BACKEND_SERVICE_AUTH_AUDIENCE",
|
|
59
58
|
},
|
|
60
|
-
|
|
61
|
-
doc: "
|
|
62
|
-
format:
|
|
63
|
-
default:
|
|
64
|
-
env: "
|
|
65
|
-
sensitive: true,
|
|
59
|
+
tokenDuration: {
|
|
60
|
+
doc: "Token lifetime in seconds (max 900)",
|
|
61
|
+
format: Number,
|
|
62
|
+
default: 60,
|
|
63
|
+
env: "BACKEND_SERVICE_AUTH_TOKEN_DURATION",
|
|
66
64
|
},
|
|
67
65
|
},
|
|
68
66
|
apiEndpoint: {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { readFileSync, existsSync, lstatSync, readdirSync } from "node:fs";
|
|
2
2
|
import { config } from "../config/config.js";
|
|
3
3
|
import { listFiles, uploadBlob } from "../s3/s3-interactions.js";
|
|
4
|
-
import { createApiHeadersForConfigBroker } from "../broker/broker-auth-helper.js";
|
|
5
4
|
import {
|
|
6
5
|
isClientSetup,
|
|
7
6
|
publishMessage,
|
|
@@ -105,7 +104,6 @@ const notifyConfigBrokerServiceVersionAvailable = async (
|
|
|
105
104
|
configsAtServiceVersion,
|
|
106
105
|
logger,
|
|
107
106
|
) => {
|
|
108
|
-
const configBrokerEndpoint = config.get("configBroker.apiEndpoint");
|
|
109
107
|
const configSNSTopic = config.get("aws.sns.configVersionTopicArn");
|
|
110
108
|
const configPublishStatus = config.get("configPublish.status");
|
|
111
109
|
|
|
@@ -113,7 +111,6 @@ const notifyConfigBrokerServiceVersionAvailable = async (
|
|
|
113
111
|
for (const configAtServiceVersion of configsAtServiceVersion) {
|
|
114
112
|
await sendConfigMessageToBroker(
|
|
115
113
|
configSNSTopic,
|
|
116
|
-
configBrokerEndpoint,
|
|
117
114
|
configAtServiceVersion,
|
|
118
115
|
configPublishStatus,
|
|
119
116
|
logger,
|
|
@@ -123,7 +120,6 @@ const notifyConfigBrokerServiceVersionAvailable = async (
|
|
|
123
120
|
|
|
124
121
|
const sendConfigMessageToBroker = async (
|
|
125
122
|
configSNSTopic,
|
|
126
|
-
configBrokerEndpoint,
|
|
127
123
|
configAtServiceVersion,
|
|
128
124
|
configPublishStatus,
|
|
129
125
|
logger,
|
|
@@ -144,38 +140,9 @@ const sendConfigMessageToBroker = async (
|
|
|
144
140
|
};
|
|
145
141
|
|
|
146
142
|
if (setToUseSNS(configSNSTopic)) {
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (!configBrokerEndpoint?.length) {
|
|
151
|
-
logger.warn(
|
|
152
|
-
`config SNS topic not set, and config broker endpoint not set, so skipping release config call`,
|
|
153
|
-
);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const url = new URL(`/api/release-config`, configBrokerEndpoint);
|
|
158
|
-
try {
|
|
159
|
-
const response = await fetch(url.href, {
|
|
160
|
-
method: "POST",
|
|
161
|
-
headers: {
|
|
162
|
-
...createApiHeadersForConfigBroker(),
|
|
163
|
-
"Content-Type": "application/json",
|
|
164
|
-
},
|
|
165
|
-
body: JSON.stringify(payload),
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
if (response.ok) {
|
|
169
|
-
logger.info(
|
|
170
|
-
`successfully notified the config broker about '${grant}' at version '${version}'`,
|
|
171
|
-
);
|
|
172
|
-
} else {
|
|
173
|
-
logger.error(
|
|
174
|
-
`call to release config failed with status '${response.status}' and text '${response.statusText}'`,
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
} catch (err) {
|
|
178
|
-
logger.error("call to release config failed", err);
|
|
143
|
+
await sendViaSNS(payload, configSNSTopic, logger);
|
|
144
|
+
} else {
|
|
145
|
+
logger.error(`config SNS topic not set, so cannot release config`);
|
|
179
146
|
}
|
|
180
147
|
};
|
|
181
148
|
|
|
@@ -20,12 +20,6 @@ vi.mock("../s3/s3-interactions.js", () => ({
|
|
|
20
20
|
uploadBlob: vi.fn(),
|
|
21
21
|
}));
|
|
22
22
|
|
|
23
|
-
vi.mock("../broker/broker-auth-helper.js", () => ({
|
|
24
|
-
createApiHeadersForConfigBroker: vi.fn(() => ({
|
|
25
|
-
authorization: "Bearer token",
|
|
26
|
-
})),
|
|
27
|
-
}));
|
|
28
|
-
|
|
29
23
|
vi.mock("../sns/sns-client.js");
|
|
30
24
|
|
|
31
25
|
const mockLogger = {
|
|
@@ -80,7 +74,6 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
80
74
|
expect(listFiles).not.toHaveBeenCalled();
|
|
81
75
|
expect(readFileSync).not.toHaveBeenCalled();
|
|
82
76
|
expect(uploadBlob).not.toHaveBeenCalled();
|
|
83
|
-
expect(fetch).not.toHaveBeenCalled();
|
|
84
77
|
});
|
|
85
78
|
|
|
86
79
|
describe("when configurations exist", () => {
|
|
@@ -98,45 +91,6 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
98
91
|
]);
|
|
99
92
|
});
|
|
100
93
|
|
|
101
|
-
it("should store config and inform config broker via REST API if SNS not setup", async () => {
|
|
102
|
-
//reset topic to default, should indicate not setup
|
|
103
|
-
config.set(
|
|
104
|
-
"aws.sns.configVersionTopicArn",
|
|
105
|
-
config.default("aws.sns.configVersionTopicArn"),
|
|
106
|
-
);
|
|
107
|
-
await storeConfigVersionAndInformBroker(mockLogger);
|
|
108
|
-
|
|
109
|
-
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
110
|
-
"uploading 'grant-1/1.2.3/main.json' to S3",
|
|
111
|
-
);
|
|
112
|
-
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
113
|
-
"successfully uploaded '1' files across '1' configs",
|
|
114
|
-
);
|
|
115
|
-
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
116
|
-
"successfully notified the config broker about 'grant-1' at version '1.2.3'",
|
|
117
|
-
);
|
|
118
|
-
expect(listFiles).toHaveBeenCalledTimes(1);
|
|
119
|
-
expect(readFileSync).toHaveBeenCalledTimes(1);
|
|
120
|
-
expect(uploadBlob).toHaveBeenCalledTimes(1);
|
|
121
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
122
|
-
"https://broker.unit.test/api/release-config",
|
|
123
|
-
{
|
|
124
|
-
method: "POST",
|
|
125
|
-
headers: {
|
|
126
|
-
"Content-Type": "application/json",
|
|
127
|
-
authorization: "Bearer token",
|
|
128
|
-
},
|
|
129
|
-
body: JSON.stringify({
|
|
130
|
-
grant: "grant-1",
|
|
131
|
-
version: "1.2.3",
|
|
132
|
-
files: ["grant-1/1.2.3/main.json"],
|
|
133
|
-
status: "active",
|
|
134
|
-
user: "system",
|
|
135
|
-
}),
|
|
136
|
-
},
|
|
137
|
-
);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
94
|
it("should store config and inform config broker via SNS if setup", async () => {
|
|
141
95
|
await storeConfigVersionAndInformBroker(mockLogger);
|
|
142
96
|
|
|
@@ -171,35 +125,28 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
171
125
|
expect(listFiles).toHaveBeenCalledTimes(1);
|
|
172
126
|
expect(readFileSync).not.toHaveBeenCalled();
|
|
173
127
|
expect(uploadBlob).not.toHaveBeenCalled();
|
|
174
|
-
expect(
|
|
128
|
+
expect(publishMessage).not.toHaveBeenCalled();
|
|
175
129
|
});
|
|
176
130
|
|
|
177
|
-
it("should stop and print
|
|
131
|
+
it("should stop and print error if no mechanism setup to contact config-broker", async () => {
|
|
178
132
|
config.set(
|
|
179
133
|
"aws.sns.configVersionTopicArn",
|
|
180
134
|
config.default("aws.sns.configVersionTopicArn"),
|
|
181
135
|
);
|
|
182
|
-
config.set("configBroker.apiEndpoint", "");
|
|
183
136
|
|
|
184
137
|
await storeConfigVersionAndInformBroker(mockLogger);
|
|
185
138
|
|
|
186
|
-
expect(mockLogger.
|
|
187
|
-
"config SNS topic not set,
|
|
139
|
+
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
140
|
+
"config SNS topic not set, so cannot release config",
|
|
188
141
|
);
|
|
189
142
|
|
|
190
143
|
expect(listFiles).toHaveBeenCalledTimes(1);
|
|
191
|
-
expect(fetch).not.toHaveBeenCalled();
|
|
192
144
|
expect(isClientSetup).not.toHaveBeenCalled();
|
|
193
145
|
expect(setupClient).not.toHaveBeenCalled();
|
|
194
146
|
expect(publishMessage).not.toHaveBeenCalled();
|
|
195
147
|
});
|
|
196
148
|
|
|
197
149
|
it("should call config broker for each config (subdirectory) found in configurations folder", async () => {
|
|
198
|
-
//reset topic to default, should indicate not setup
|
|
199
|
-
config.set(
|
|
200
|
-
"aws.sns.configVersionTopicArn",
|
|
201
|
-
config.default("aws.sns.configVersionTopicArn"),
|
|
202
|
-
);
|
|
203
150
|
readdirSync.mockRestore();
|
|
204
151
|
readdirSync
|
|
205
152
|
.mockReturnValueOnce([
|
|
@@ -241,15 +188,15 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
241
188
|
"successfully uploaded '3' files across '2' configs",
|
|
242
189
|
);
|
|
243
190
|
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
244
|
-
"successfully notified the config broker about 'grant-1' at version '1.2.3'",
|
|
191
|
+
"successfully notified the config broker about 'grant-1' at version '1.2.3' via SNS",
|
|
245
192
|
);
|
|
246
193
|
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
247
|
-
"successfully notified the config broker about 'grant-2' at version '1.2.3'",
|
|
194
|
+
"successfully notified the config broker about 'grant-2' at version '1.2.3' via SNS",
|
|
248
195
|
);
|
|
249
196
|
expect(listFiles).toHaveBeenCalledTimes(2);
|
|
250
197
|
expect(readFileSync).toHaveBeenCalledTimes(3);
|
|
251
198
|
expect(uploadBlob).toHaveBeenCalledTimes(3);
|
|
252
|
-
expect(
|
|
199
|
+
expect(publishMessage).toHaveBeenCalledTimes(2);
|
|
253
200
|
});
|
|
254
201
|
});
|
|
255
202
|
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { GetWebIdentityTokenCommand } from "@aws-sdk/client-sts";
|
|
2
|
+
import { config } from "../config/config.js";
|
|
3
|
+
|
|
4
|
+
const serviceAuthAudience = config.get("configBroker.serviceAuth.audience");
|
|
5
|
+
const serviceAuthTokenDuration = config.get(
|
|
6
|
+
"configBroker.serviceAuth.tokenDuration",
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
export const generateToken = async (stsClient) => {
|
|
10
|
+
const input = {
|
|
11
|
+
SigningAlgorithm: "RS256",
|
|
12
|
+
Audience: [serviceAuthAudience],
|
|
13
|
+
DurationSeconds: serviceAuthTokenDuration,
|
|
14
|
+
};
|
|
15
|
+
const command = new GetWebIdentityTokenCommand(input);
|
|
16
|
+
|
|
17
|
+
const { WebIdentityToken } = await stsClient.send(command);
|
|
18
|
+
return WebIdentityToken;
|
|
19
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { GetWebIdentityTokenCommand } from "@aws-sdk/client-sts";
|
|
3
|
+
import { generateToken } from "./grants-config-broker-token.js";
|
|
4
|
+
|
|
5
|
+
vi.mock("@aws-sdk/client-sts", () => {
|
|
6
|
+
return {
|
|
7
|
+
GetWebIdentityTokenCommand: vi.fn(),
|
|
8
|
+
};
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe("grants-config-broker-token", () => {
|
|
12
|
+
let stsClient;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
vi.clearAllMocks();
|
|
16
|
+
stsClient = {
|
|
17
|
+
send: vi.fn(),
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should generate a token successfully", async () => {
|
|
22
|
+
const mockToken = "mock-web-identity-token";
|
|
23
|
+
stsClient.send.mockResolvedValue({ WebIdentityToken: mockToken });
|
|
24
|
+
|
|
25
|
+
const token = await generateToken(stsClient);
|
|
26
|
+
|
|
27
|
+
expect(token).toBe(mockToken);
|
|
28
|
+
expect(GetWebIdentityTokenCommand).toHaveBeenCalledWith({
|
|
29
|
+
SigningAlgorithm: "RS256",
|
|
30
|
+
Audience: ["grants-config-broker"],
|
|
31
|
+
DurationSeconds: 60,
|
|
32
|
+
});
|
|
33
|
+
expect(stsClient.send).toHaveBeenCalledWith(
|
|
34
|
+
expect.any(GetWebIdentityTokenCommand),
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should throw an error if stsClient.send fails", async () => {
|
|
39
|
+
const mockError = new Error("STS Error");
|
|
40
|
+
stsClient.send.mockRejectedValue(mockError);
|
|
41
|
+
|
|
42
|
+
await expect(generateToken(stsClient)).rejects.toThrow("STS Error");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { STSClient } from "@aws-sdk/client-sts";
|
|
2
|
+
|
|
3
|
+
export const stsClientPlugin = {
|
|
4
|
+
plugin: {
|
|
5
|
+
name: "sts-client",
|
|
6
|
+
version: "1.0.0",
|
|
7
|
+
register(server) {
|
|
8
|
+
const sts = new STSClient();
|
|
9
|
+
|
|
10
|
+
server.decorate("server", "sts", sts);
|
|
11
|
+
server.decorate("request", "sts", () => sts, { apply: true });
|
|
12
|
+
|
|
13
|
+
server.events.on("stop", () => {
|
|
14
|
+
server.logger.info("Closing AWS SDK clients");
|
|
15
|
+
sts.destroy();
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { stsClientPlugin } from "./sts-client-plugin.js";
|
|
2
|
+
import { STSClient } from "@aws-sdk/client-sts";
|
|
3
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
4
|
+
|
|
5
|
+
vi.mock("@aws-sdk/client-sts", () => {
|
|
6
|
+
return {
|
|
7
|
+
STSClient: vi.fn().mockImplementation(function () {
|
|
8
|
+
this.destroy = vi.fn();
|
|
9
|
+
return this;
|
|
10
|
+
}),
|
|
11
|
+
};
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("stsClientPlugin", () => {
|
|
15
|
+
let server;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
vi.clearAllMocks();
|
|
19
|
+
|
|
20
|
+
server = {
|
|
21
|
+
decorate: vi.fn(),
|
|
22
|
+
events: {
|
|
23
|
+
on: vi.fn(),
|
|
24
|
+
},
|
|
25
|
+
logger: {
|
|
26
|
+
info: vi.fn(),
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should have correct name and version", () => {
|
|
32
|
+
expect(stsClientPlugin.plugin.name).toBe("sts-client");
|
|
33
|
+
expect(stsClientPlugin.plugin.version).toBe("1.0.0");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should register the plugin and decorate server and request", () => {
|
|
37
|
+
stsClientPlugin.plugin.register(server);
|
|
38
|
+
|
|
39
|
+
expect(STSClient).toHaveBeenCalled();
|
|
40
|
+
const mockStsInstance = vi.mocked(STSClient).mock.results[0].value;
|
|
41
|
+
expect(server.decorate).toHaveBeenCalledWith(
|
|
42
|
+
"server",
|
|
43
|
+
"sts",
|
|
44
|
+
mockStsInstance,
|
|
45
|
+
);
|
|
46
|
+
expect(server.decorate).toHaveBeenCalledWith(
|
|
47
|
+
"request",
|
|
48
|
+
"sts",
|
|
49
|
+
expect.any(Function),
|
|
50
|
+
{ apply: true },
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const decorateRequestFn = server.decorate.mock.calls.find(
|
|
54
|
+
(call) => call[0] === "request",
|
|
55
|
+
)[2];
|
|
56
|
+
expect(decorateRequestFn()).toBe(mockStsInstance);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("should handle server stop event", () => {
|
|
60
|
+
let stopHandler;
|
|
61
|
+
server.events.on.mockImplementation((event, handler) => {
|
|
62
|
+
if (event === "stop") {
|
|
63
|
+
stopHandler = handler;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
stsClientPlugin.plugin.register(server);
|
|
67
|
+
expect(server.events.on).toHaveBeenCalledWith("stop", expect.any(Function));
|
|
68
|
+
expect(stopHandler).toBeDefined();
|
|
69
|
+
|
|
70
|
+
stopHandler();
|
|
71
|
+
|
|
72
|
+
const mockStsInstance = vi.mocked(STSClient).mock.results[0].value;
|
|
73
|
+
expect(server.logger.info).toHaveBeenCalledWith("Closing AWS SDK clients");
|
|
74
|
+
expect(mockStsInstance.destroy).toHaveBeenCalled();
|
|
75
|
+
});
|
|
76
|
+
});
|