@defra/grants-config-utils 0.6.0 → 1.1.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 +15 -0
- package/package.json +22 -12
- package/src/broker/broker-auth-helper.js +7 -60
- package/src/broker/broker-auth-helper.test.js +50 -231
- package/src/config/config.js +16 -12
- package/src/service/config-store-and-inform.js +7 -40
- package/src/service/config-store-and-inform.test.js +9 -61
- package/src/sqs/sqs-subscriber.js +122 -0
- package/src/sqs/sqs-subscriber.test.js +236 -0
- package/src/sqs/trace-parent.js +8 -0
- package/src/sqs/trace-parent.test.js +13 -0
- 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
|
@@ -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,17 +104,17 @@ 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");
|
|
109
|
+
const configPublishUser = config.get("configPublish.user");
|
|
111
110
|
|
|
112
111
|
// iterate each grant configuration, notify config available at current service version
|
|
113
112
|
for (const configAtServiceVersion of configsAtServiceVersion) {
|
|
114
113
|
await sendConfigMessageToBroker(
|
|
115
114
|
configSNSTopic,
|
|
116
|
-
configBrokerEndpoint,
|
|
117
115
|
configAtServiceVersion,
|
|
118
116
|
configPublishStatus,
|
|
117
|
+
configPublishUser,
|
|
119
118
|
logger,
|
|
120
119
|
);
|
|
121
120
|
}
|
|
@@ -123,59 +122,27 @@ const notifyConfigBrokerServiceVersionAvailable = async (
|
|
|
123
122
|
|
|
124
123
|
const sendConfigMessageToBroker = async (
|
|
125
124
|
configSNSTopic,
|
|
126
|
-
configBrokerEndpoint,
|
|
127
125
|
configAtServiceVersion,
|
|
128
126
|
configPublishStatus,
|
|
127
|
+
configPublishUser,
|
|
129
128
|
logger,
|
|
130
129
|
) => {
|
|
131
130
|
const { grant, version, files } = configAtServiceVersion;
|
|
132
131
|
// files is an array of tuples, we only want the S3 paths here
|
|
133
132
|
const s3Paths = files.map(([_, s3Path]) => s3Path);
|
|
134
133
|
|
|
135
|
-
// When CDP is updated to inject user that did deployment of container, reference that injected variable from config here
|
|
136
|
-
const user = "system";
|
|
137
|
-
|
|
138
134
|
const payload = {
|
|
139
135
|
grant,
|
|
140
136
|
version,
|
|
141
137
|
files: s3Paths,
|
|
142
138
|
status: configPublishStatus,
|
|
143
|
-
user,
|
|
139
|
+
user: configPublishUser,
|
|
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 = {
|
|
@@ -56,6 +50,7 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
56
50
|
config.set("serviceName", "grants-config-playground");
|
|
57
51
|
config.set("configBroker.apiEndpoint", "https://broker.unit.test");
|
|
58
52
|
config.set("configPublish.status", "active");
|
|
53
|
+
config.set("configPublish.user", "tom_the_cat");
|
|
59
54
|
config.set(
|
|
60
55
|
"aws.sns.configVersionTopicArn",
|
|
61
56
|
"arn:aws:sns:us-east-1:123456789012:config-version-topic",
|
|
@@ -80,7 +75,6 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
80
75
|
expect(listFiles).not.toHaveBeenCalled();
|
|
81
76
|
expect(readFileSync).not.toHaveBeenCalled();
|
|
82
77
|
expect(uploadBlob).not.toHaveBeenCalled();
|
|
83
|
-
expect(fetch).not.toHaveBeenCalled();
|
|
84
78
|
});
|
|
85
79
|
|
|
86
80
|
describe("when configurations exist", () => {
|
|
@@ -98,45 +92,6 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
98
92
|
]);
|
|
99
93
|
});
|
|
100
94
|
|
|
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
95
|
it("should store config and inform config broker via SNS if setup", async () => {
|
|
141
96
|
await storeConfigVersionAndInformBroker(mockLogger);
|
|
142
97
|
|
|
@@ -159,7 +114,7 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
159
114
|
version: "1.2.3",
|
|
160
115
|
files: ["grant-1/1.2.3/main.json"],
|
|
161
116
|
status: "active",
|
|
162
|
-
user: "
|
|
117
|
+
user: "tom_the_cat",
|
|
163
118
|
});
|
|
164
119
|
});
|
|
165
120
|
|
|
@@ -171,35 +126,28 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
171
126
|
expect(listFiles).toHaveBeenCalledTimes(1);
|
|
172
127
|
expect(readFileSync).not.toHaveBeenCalled();
|
|
173
128
|
expect(uploadBlob).not.toHaveBeenCalled();
|
|
174
|
-
expect(
|
|
129
|
+
expect(publishMessage).not.toHaveBeenCalled();
|
|
175
130
|
});
|
|
176
131
|
|
|
177
|
-
it("should stop and print
|
|
132
|
+
it("should stop and print error if no mechanism setup to contact config-broker", async () => {
|
|
178
133
|
config.set(
|
|
179
134
|
"aws.sns.configVersionTopicArn",
|
|
180
135
|
config.default("aws.sns.configVersionTopicArn"),
|
|
181
136
|
);
|
|
182
|
-
config.set("configBroker.apiEndpoint", "");
|
|
183
137
|
|
|
184
138
|
await storeConfigVersionAndInformBroker(mockLogger);
|
|
185
139
|
|
|
186
|
-
expect(mockLogger.
|
|
187
|
-
"config SNS topic not set,
|
|
140
|
+
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
141
|
+
"config SNS topic not set, so cannot release config",
|
|
188
142
|
);
|
|
189
143
|
|
|
190
144
|
expect(listFiles).toHaveBeenCalledTimes(1);
|
|
191
|
-
expect(fetch).not.toHaveBeenCalled();
|
|
192
145
|
expect(isClientSetup).not.toHaveBeenCalled();
|
|
193
146
|
expect(setupClient).not.toHaveBeenCalled();
|
|
194
147
|
expect(publishMessage).not.toHaveBeenCalled();
|
|
195
148
|
});
|
|
196
149
|
|
|
197
150
|
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
151
|
readdirSync.mockRestore();
|
|
204
152
|
readdirSync
|
|
205
153
|
.mockReturnValueOnce([
|
|
@@ -241,15 +189,15 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
241
189
|
"successfully uploaded '3' files across '2' configs",
|
|
242
190
|
);
|
|
243
191
|
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
244
|
-
"successfully notified the config broker about 'grant-1' at version '1.2.3'",
|
|
192
|
+
"successfully notified the config broker about 'grant-1' at version '1.2.3' via SNS",
|
|
245
193
|
);
|
|
246
194
|
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
247
|
-
"successfully notified the config broker about 'grant-2' at version '1.2.3'",
|
|
195
|
+
"successfully notified the config broker about 'grant-2' at version '1.2.3' via SNS",
|
|
248
196
|
);
|
|
249
197
|
expect(listFiles).toHaveBeenCalledTimes(2);
|
|
250
198
|
expect(readFileSync).toHaveBeenCalledTimes(3);
|
|
251
199
|
expect(uploadBlob).toHaveBeenCalledTimes(3);
|
|
252
|
-
expect(
|
|
200
|
+
expect(publishMessage).toHaveBeenCalledTimes(2);
|
|
253
201
|
});
|
|
254
202
|
});
|
|
255
203
|
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeleteMessageCommand,
|
|
3
|
+
ReceiveMessageCommand,
|
|
4
|
+
SendMessageCommand,
|
|
5
|
+
SQSClient,
|
|
6
|
+
} from "@aws-sdk/client-sqs";
|
|
7
|
+
import { setTimeout } from "node:timers/promises";
|
|
8
|
+
import { withTraceParent } from "./trace-parent.js";
|
|
9
|
+
const DEFAULT_POLL_TIMEOUT_MS = 30000;
|
|
10
|
+
|
|
11
|
+
export class SqsSubscriber {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.queueUrl = options.queueUrl;
|
|
14
|
+
this.onMessage = options.onMessage;
|
|
15
|
+
this.logger = options.logger;
|
|
16
|
+
this.isRunning = false;
|
|
17
|
+
this.timeoutOnPollErrorMs =
|
|
18
|
+
options.timeoutOnErrorMs || DEFAULT_POLL_TIMEOUT_MS;
|
|
19
|
+
|
|
20
|
+
this.sqsClient = new SQSClient({
|
|
21
|
+
region: options.region,
|
|
22
|
+
endpoint: options.awsEndpointUrl,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async start() {
|
|
27
|
+
this.isRunning = true;
|
|
28
|
+
await this.poll();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async stop() {
|
|
32
|
+
this.isRunning = false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async poll() {
|
|
36
|
+
this.logger.info(`Started polling SQS queue: ${this.queueUrl}`);
|
|
37
|
+
|
|
38
|
+
while (this.isRunning) {
|
|
39
|
+
try {
|
|
40
|
+
const messages = await this.getMessages();
|
|
41
|
+
await Promise.all(messages.map((m) => this.processMessage(m)));
|
|
42
|
+
} catch (err) {
|
|
43
|
+
this.logger.error(
|
|
44
|
+
`Error polling SQS queue ${this.queueUrl}: ${err.message}`,
|
|
45
|
+
);
|
|
46
|
+
await setTimeout(this.timeoutOnPollErrorMs);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.logger.info(`Stopped polling SQS queue: ${this.queueUrl}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async processMessage(message) {
|
|
54
|
+
this.logger.info(`Processing SQS message ${message.MessageId}`);
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const body = JSON.parse(message.Body);
|
|
58
|
+
await withTraceParent(body.traceparent, () =>
|
|
59
|
+
this.onMessage(
|
|
60
|
+
body,
|
|
61
|
+
this.extractMessageAttributes(message),
|
|
62
|
+
message.Attributes.SentTimestamp,
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
await this.deleteMessage(message);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
this.logger.error(
|
|
68
|
+
{ error },
|
|
69
|
+
`Error processing SQS message ${message.MessageId}`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async getMessages() {
|
|
75
|
+
const response = await this.sqsClient.send(
|
|
76
|
+
new ReceiveMessageCommand({
|
|
77
|
+
QueueUrl: this.queueUrl,
|
|
78
|
+
MaxNumberOfMessages: 10,
|
|
79
|
+
WaitTimeSeconds: 20,
|
|
80
|
+
AttributeNames: ["All"],
|
|
81
|
+
MessageAttributeNames: ["All"],
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
return response.Messages || [];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async sendMessage(message) {
|
|
89
|
+
// we could pass in attributes and send them on, but we don't need them yet
|
|
90
|
+
const params = {
|
|
91
|
+
QueueUrl: this.queueUrl,
|
|
92
|
+
MessageBody: JSON.stringify(message),
|
|
93
|
+
};
|
|
94
|
+
await this.sqsClient.send(new SendMessageCommand(params));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async deleteMessage(message) {
|
|
98
|
+
await this.sqsClient.send(
|
|
99
|
+
new DeleteMessageCommand({
|
|
100
|
+
QueueUrl: this.queueUrl,
|
|
101
|
+
ReceiptHandle: message.ReceiptHandle,
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
extractMessageAttributes(message) {
|
|
107
|
+
const attributes = {};
|
|
108
|
+
for (const key in message.MessageAttributes) {
|
|
109
|
+
if (Object.hasOwn(message.MessageAttributes, key)) {
|
|
110
|
+
const attr = message.MessageAttributes[key];
|
|
111
|
+
if (attr.DataType === "String" || attr.DataType === "Number") {
|
|
112
|
+
attributes[key] = attr.StringValue;
|
|
113
|
+
} else if (attr.DataType === "String.Array") {
|
|
114
|
+
attributes[key] = JSON.parse(attr.StringValue);
|
|
115
|
+
} else {
|
|
116
|
+
attributes[key] = attr.BinaryValue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return attributes;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeleteMessageCommand,
|
|
3
|
+
ReceiveMessageCommand,
|
|
4
|
+
SendMessageCommand,
|
|
5
|
+
SQSClient,
|
|
6
|
+
} from "@aws-sdk/client-sqs";
|
|
7
|
+
import { SqsSubscriber } from "./sqs-subscriber.js";
|
|
8
|
+
|
|
9
|
+
const mockLogger = {
|
|
10
|
+
info: vi.fn(),
|
|
11
|
+
error: vi.fn(),
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
vi.mock("@aws-sdk/client-sqs");
|
|
15
|
+
|
|
16
|
+
const timeoutSleepMs = 100;
|
|
17
|
+
|
|
18
|
+
let consumer;
|
|
19
|
+
let onMessage;
|
|
20
|
+
|
|
21
|
+
beforeEach(async () => {
|
|
22
|
+
onMessage = vi.fn().mockResolvedValue();
|
|
23
|
+
|
|
24
|
+
consumer = new SqsSubscriber({
|
|
25
|
+
queueUrl: "https://sqs.eu-west-2.amazonaws.com/123456789012/test-queue",
|
|
26
|
+
onMessage,
|
|
27
|
+
logger: mockLogger,
|
|
28
|
+
region: "eu-west-2",
|
|
29
|
+
awsEndpointUrl: "http://localhost:4566",
|
|
30
|
+
timeoutOnErrorMs: timeoutSleepMs,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("constructor", () => {
|
|
35
|
+
it("instantiates with options", () => {
|
|
36
|
+
expect(consumer.queueUrl).toBe(
|
|
37
|
+
"https://sqs.eu-west-2.amazonaws.com/123456789012/test-queue",
|
|
38
|
+
);
|
|
39
|
+
expect(consumer.onMessage).toBe(onMessage);
|
|
40
|
+
expect(consumer.isRunning).toBe(false);
|
|
41
|
+
expect(SQSClient).toHaveBeenCalledWith({
|
|
42
|
+
endpoint: "http://localhost:4566",
|
|
43
|
+
region: "eu-west-2",
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("start", () => {
|
|
49
|
+
it("sets isRunning to true and starts polling", async () => {
|
|
50
|
+
consumer.poll = vi.fn().mockResolvedValue();
|
|
51
|
+
|
|
52
|
+
await consumer.start();
|
|
53
|
+
|
|
54
|
+
expect(consumer.isRunning).toBe(true);
|
|
55
|
+
expect(consumer.poll).toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("stop", () => {
|
|
60
|
+
it("sets isRunning to false", async () => {
|
|
61
|
+
consumer.isRunning = true;
|
|
62
|
+
|
|
63
|
+
await consumer.stop();
|
|
64
|
+
|
|
65
|
+
expect(consumer.isRunning).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("error on polling", () => {
|
|
70
|
+
it("error when polling waits for timeout period before running again, and logs errors", async () => {
|
|
71
|
+
const timeCaptures = [];
|
|
72
|
+
consumer.sqsClient.send.mockImplementationOnce(() => {
|
|
73
|
+
timeCaptures.push(Date.now());
|
|
74
|
+
throw new Error("Test polling error");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
consumer.sqsClient.send.mockImplementationOnce(() => {
|
|
78
|
+
timeCaptures.push(Date.now());
|
|
79
|
+
consumer.isRunning = false; // stop now
|
|
80
|
+
throw new Error("Test polling error");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await consumer.start();
|
|
84
|
+
|
|
85
|
+
expect(timeCaptures[1] - timeCaptures[0]).toBeGreaterThanOrEqual(
|
|
86
|
+
timeoutSleepMs - 2,
|
|
87
|
+
); //2ms tolerance
|
|
88
|
+
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
89
|
+
"Error polling SQS queue https://sqs.eu-west-2.amazonaws.com/123456789012/test-queue: Test polling error",
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("message processing", () => {
|
|
95
|
+
it("processes and deletes messages correctly", async () => {
|
|
96
|
+
const mockMessages = [
|
|
97
|
+
{
|
|
98
|
+
MessageId: "msg-1",
|
|
99
|
+
Body: '{ "message" : "Test message 1" }',
|
|
100
|
+
ReceiptHandle: "receipt-1",
|
|
101
|
+
MessageAttributes: {
|
|
102
|
+
Attribute1: {
|
|
103
|
+
StringValue: "Value1",
|
|
104
|
+
DataType: "String",
|
|
105
|
+
},
|
|
106
|
+
Attribute2: {
|
|
107
|
+
StringValue: "1",
|
|
108
|
+
DataType: "Number",
|
|
109
|
+
},
|
|
110
|
+
Attribute3: {
|
|
111
|
+
BinaryValue: new TextEncoder().encode("Value3"),
|
|
112
|
+
DataType: "Binary",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
Attributes: {
|
|
116
|
+
SentTimestamp: "1780599163000",
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
consumer.sqsClient.send.mockImplementation(async (command) => {
|
|
122
|
+
if (command instanceof ReceiveMessageCommand) {
|
|
123
|
+
return { Messages: mockMessages };
|
|
124
|
+
}
|
|
125
|
+
if (command instanceof DeleteMessageCommand) {
|
|
126
|
+
return {};
|
|
127
|
+
}
|
|
128
|
+
return {};
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
consumer.onMessage.mockReset();
|
|
132
|
+
|
|
133
|
+
consumer.onMessage.mockImplementationOnce(() => {
|
|
134
|
+
consumer.isRunning = false; // just take 1 message
|
|
135
|
+
return Promise.resolve("processed");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
await consumer.start();
|
|
139
|
+
|
|
140
|
+
expect(ReceiveMessageCommand).toHaveBeenCalledWith({
|
|
141
|
+
QueueUrl: consumer.queueUrl,
|
|
142
|
+
MaxNumberOfMessages: 10,
|
|
143
|
+
WaitTimeSeconds: 20,
|
|
144
|
+
AttributeNames: ["All"],
|
|
145
|
+
MessageAttributeNames: ["All"],
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
expect(onMessage).toHaveBeenCalledWith(
|
|
149
|
+
{ message: "Test message 1" },
|
|
150
|
+
{
|
|
151
|
+
Attribute1: "Value1",
|
|
152
|
+
Attribute2: "1",
|
|
153
|
+
Attribute3: new TextEncoder().encode("Value3"),
|
|
154
|
+
},
|
|
155
|
+
"1780599163000",
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
expect(DeleteMessageCommand).toHaveBeenCalledWith({
|
|
159
|
+
QueueUrl: consumer.queueUrl,
|
|
160
|
+
ReceiptHandle: "receipt-1",
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("handles errors gracefully in processing", async () => {
|
|
165
|
+
const mockMessages = [
|
|
166
|
+
{
|
|
167
|
+
MessageId: "msg-1",
|
|
168
|
+
Body: '{ "message" : "Test message 1" }',
|
|
169
|
+
ReceiptHandle: "receipt-1",
|
|
170
|
+
Attributes: {
|
|
171
|
+
SentTimestamp: "1780599163000",
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
// mock the fetch part to return our test message
|
|
177
|
+
consumer.sqsClient.send.mockImplementation(async (command) => {
|
|
178
|
+
if (command instanceof ReceiveMessageCommand) {
|
|
179
|
+
return { Messages: mockMessages };
|
|
180
|
+
}
|
|
181
|
+
return {};
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
consumer.onMessage.mockReset();
|
|
185
|
+
|
|
186
|
+
const err = new Error("Test error");
|
|
187
|
+
|
|
188
|
+
consumer.onMessage.mockImplementationOnce(() => {
|
|
189
|
+
consumer.isRunning = false; // just take 1 message
|
|
190
|
+
return Promise.reject(err);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
await consumer.start();
|
|
194
|
+
|
|
195
|
+
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
196
|
+
{ error: err },
|
|
197
|
+
"Error processing SQS message msg-1",
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe("sendMessage", () => {
|
|
203
|
+
it("sends a message via client", async () => {
|
|
204
|
+
const mockMessage = {
|
|
205
|
+
grant: "grant-1",
|
|
206
|
+
version: "1.2.3",
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
await consumer.sendMessage(mockMessage);
|
|
210
|
+
|
|
211
|
+
expect(SendMessageCommand).toHaveBeenCalledWith({
|
|
212
|
+
QueueUrl: consumer.queueUrl,
|
|
213
|
+
MessageBody: '{"grant":"grant-1","version":"1.2.3"}',
|
|
214
|
+
});
|
|
215
|
+
expect(consumer.sqsClient.send).toHaveBeenCalledWith(
|
|
216
|
+
expect.any(SendMessageCommand),
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("deleteMessage", () => {
|
|
222
|
+
it("deletes a message", async () => {
|
|
223
|
+
const mockMessage = {
|
|
224
|
+
MessageId: "msg-1",
|
|
225
|
+
ReceiptHandle: "receipt-1",
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
await consumer.deleteMessage(mockMessage);
|
|
229
|
+
|
|
230
|
+
expect(DeleteMessageCommand).toHaveBeenCalledWith({
|
|
231
|
+
QueueUrl: consumer.queueUrl,
|
|
232
|
+
ReceiptHandle: "receipt-1",
|
|
233
|
+
});
|
|
234
|
+
expect(consumer.sqsClient.send).toHaveBeenCalled();
|
|
235
|
+
});
|
|
236
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
const asyncLocalStorage = new AsyncLocalStorage();
|
|
4
|
+
|
|
5
|
+
export const withTraceParent = (traceParent, fn) =>
|
|
6
|
+
asyncLocalStorage.run(traceParent, fn);
|
|
7
|
+
|
|
8
|
+
export const getTraceParent = () => asyncLocalStorage.getStore();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { setTimeout } from "node:timers/promises";
|
|
2
|
+
import { getTraceParent, withTraceParent } from "./trace-parent.js";
|
|
3
|
+
|
|
4
|
+
describe("traceParent", () => {
|
|
5
|
+
it("runs a function with traceParent in async scope", async () => {
|
|
6
|
+
const traceParent = await withTraceParent("1234-0987", async () => {
|
|
7
|
+
await setTimeout(1);
|
|
8
|
+
return getTraceParent();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
expect(traceParent).toEqual("1234-0987");
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -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
|
+
};
|