@defra/grants-config-utils 0.1.1 → 0.2.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.
@@ -5,6 +5,11 @@ on:
5
5
  branches:
6
6
  - main
7
7
  workflow_dispatch:
8
+ inputs:
9
+ commit_message:
10
+ description: 'commit message'
11
+ required: false
12
+ type: string
8
13
 
9
14
  permissions:
10
15
  contents: write
@@ -97,6 +102,9 @@ jobs:
97
102
  ref: 'main',
98
103
  headers: {
99
104
  'X-GitHub-Api-Version': '2022-11-28'
105
+ },
106
+ inputs: {
107
+ commit_message: 'Create new config version'
100
108
  }
101
109
  });
102
110
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @defra/grants-config-utils
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - dabf7f1: feat: Add SNS client
8
+
9
+ ## 0.1.2
10
+
11
+ ### Patch Changes
12
+
13
+ - c6fc2ef: fix: Skips auto merge stage on second run through
14
+
3
15
  ## 0.1.1
4
16
 
5
17
  ### Patch Changes
package/CONTRIBUTING.md CHANGED
@@ -19,6 +19,5 @@ To make changes to this library, please follow the steps below:
19
19
  4. Push to branch, and create a pull request.
20
20
  5. Github actions will verify the build
21
21
  6. After review, merge the pull request.
22
- 7. A second PR will be created by the release action.
23
- 8. Merge this PR
24
- 9. Github actions will automatically increment the version and publish to NPM.
22
+ 7. A second PR will be created and auto-merged by the release action.
23
+ 8. Github actions will automatically increment the version and publish to NPM.
package/README.md CHANGED
@@ -15,6 +15,19 @@ npm install --save @defra/grants-config-utils
15
15
 
16
16
  Refer to the [contributing documentation](CONTRIBUTING.md).
17
17
 
18
+ ## Environment Variables
19
+
20
+ The following variables are used in this module:
21
+
22
+ - `SERVICE_VERSION` - **Required** This is the CDP service version that should be injected automatically in all consuming services
23
+ - `AWS_REGION` - **Required** Region where the service is deployed
24
+ - `CONFIG_BUCKET_NAME` - **Required** The S3 bucket where the config is to be stored
25
+ - `FORCE_PATH_STYLE` - Optional flag to force path style URLs when using S3 bucket (default true)
26
+ - `AWS_ENDPOINT_URL` - Optional AWS endpoint URL for local development
27
+ - `GRANTS_CONFIG_BROKER_AUTH_TOKEN` - **Required** Auth token for the grants config broker API
28
+ - `GRANTS_CONFIG_BROKER_ENCRYPTION_KEY` - **Required** Encryption key for the grants config broker API
29
+ - `GRANTS_CONFIG_BROKER_API_ENDPOINT` - **Required** Endpoint for the grants config broker API
30
+
18
31
  ## Licence
19
32
 
20
33
  THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/grants-config-utils",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "NPM module for providing utils for publishing grants config",
5
5
  "engines": {
6
6
  "node": ">=24",
@@ -31,6 +31,7 @@
31
31
  "exports": {
32
32
  ".": "./src/service/config-store-and-inform.js",
33
33
  "./s3-interactions": "./src/s3/s3-interactions.js",
34
+ "./sns-client": "./src/sns/sns-client.js",
34
35
  "./config": "./src/config/config.js",
35
36
  "./broker": "./src/broker/broker-auth-helper.js"
36
37
  },
@@ -41,6 +42,7 @@
41
42
  "homepage": "https://github.com/DEFRA/grants-config-utils#readme",
42
43
  "dependencies": {
43
44
  "@aws-sdk/client-s3": "3.1047.0",
45
+ "@aws-sdk/client-sns": "3.1047.0",
44
46
  "convict": "6.2.5",
45
47
  "convict-format-with-validator": "6.2.0"
46
48
  },
@@ -39,6 +39,14 @@ export const config = convict({
39
39
  env: "FORCE_PATH_STYLE",
40
40
  },
41
41
  },
42
+ sns: {
43
+ configVersionTopicArn: {
44
+ doc: "ARN of the SNS topic to publish new config version events",
45
+ format: String,
46
+ default: "arn:aws:sns:eu-west-2:000000000000:gfr__sns___config_version",
47
+ env: "GFR__SNS__CONFIG_VERSION_ARN",
48
+ },
49
+ },
42
50
  },
43
51
  configBroker: {
44
52
  auth: {
@@ -0,0 +1,66 @@
1
+ import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
2
+ import { config } from "../config/config.js";
3
+
4
+ let defaultTopic;
5
+ let loggerInstance;
6
+ let clientSetup = false;
7
+
8
+ let snsClient;
9
+
10
+ export const isClientSetup = () => clientSetup;
11
+
12
+ export const setupClient = (
13
+ logger,
14
+ {
15
+ region = config.get("aws.region"),
16
+ awsEndpointUrl = config.get("aws.endpointUrl"),
17
+ publishToTopic = config.get("aws.sns.configVersionTopicArn"),
18
+ } = {},
19
+ ) => {
20
+ snsClient = new SNSClient({
21
+ region,
22
+ endpoint: awsEndpointUrl,
23
+ });
24
+ loggerInstance = logger;
25
+ defaultTopic = publishToTopic;
26
+ clientSetup = true;
27
+ };
28
+
29
+ export const publishMessage = async (
30
+ data,
31
+ messageAttributes = {},
32
+ topic = defaultTopic,
33
+ ) => {
34
+ if (!clientSetup) {
35
+ throw new Error(
36
+ "SNS client not setup. Call setupClient() before publishing messages.",
37
+ );
38
+ }
39
+ loggerInstance.info(`Publish command ${topic}`);
40
+ await snsClient.send(
41
+ new PublishCommand({
42
+ TopicArn: topic,
43
+ Message: JSON.stringify(data),
44
+ MessageAttributes: convertMessageAttributes(messageAttributes),
45
+ }),
46
+ );
47
+ };
48
+
49
+ /* Converts a simple key-value object into the format required by SNS for message attributes.
50
+ Note only supports string values currently
51
+ Example input: { key1: "value1", key2: "value2" }
52
+ Example output: {
53
+ key1: { DataType: "String", StringValue: "value1" },
54
+ key2: { DataType: "String", StringValue: "value2" }
55
+ }
56
+ */
57
+ const convertMessageAttributes = (attributes) => {
58
+ const convertedAttributes = {};
59
+ for (const key in attributes) {
60
+ convertedAttributes[key] = {
61
+ DataType: "String",
62
+ StringValue: attributes[key],
63
+ };
64
+ }
65
+ return convertedAttributes;
66
+ };
@@ -0,0 +1,154 @@
1
+ import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
2
+ import { isClientSetup, publishMessage, setupClient } from "./sns-client.js";
3
+ import { config } from "../config/config.js";
4
+
5
+ vi.mock("./metrics.js");
6
+ vi.mock("@aws-sdk/client-sns", () => ({
7
+ SNSClient: vi.fn(),
8
+ PublishCommand: vi.fn(),
9
+ }));
10
+
11
+ const mockLogger = {
12
+ info: vi.fn(),
13
+ error: vi.fn(),
14
+ };
15
+
16
+ const topicArn = "arn:aws:sns:us-east-1:123456789012:MyTopic";
17
+
18
+ describe("publish", () => {
19
+ it("error thrown if client not set up", async () => {
20
+ const message = {
21
+ key: "value",
22
+ };
23
+
24
+ const send = vi.fn();
25
+
26
+ SNSClient.mockReturnValue({
27
+ send,
28
+ });
29
+
30
+ expect(isClientSetup()).toBe(false);
31
+
32
+ await expect(publishMessage(message)).rejects.toThrow(
33
+ "SNS client not setup. Call setupClient() before publishing messages.",
34
+ );
35
+
36
+ expect(PublishCommand).toHaveBeenCalledTimes(0);
37
+
38
+ expect(send).toHaveBeenCalledTimes(0);
39
+ });
40
+
41
+ it("publishes a message to a topic", async () => {
42
+ const message = {
43
+ key: "value",
44
+ };
45
+
46
+ const send = vi.fn();
47
+
48
+ SNSClient.mockImplementation(function () {
49
+ return { send };
50
+ });
51
+
52
+ PublishCommand.mockImplementation(function (params) {
53
+ return params;
54
+ });
55
+ setupClient(mockLogger, {
56
+ region: "us-east-1",
57
+ awsEndpointUrl: "http://localhost:4566",
58
+ publishToTopic: topicArn,
59
+ });
60
+
61
+ expect(isClientSetup()).toBe(true);
62
+
63
+ await publishMessage(message);
64
+
65
+ expect(PublishCommand).toHaveBeenCalledWith({
66
+ TopicArn: topicArn,
67
+ Message: '{"key":"value"}',
68
+ MessageAttributes: {},
69
+ });
70
+
71
+ expect(send).toHaveBeenCalledWith({
72
+ TopicArn: topicArn,
73
+ Message: '{"key":"value"}',
74
+ MessageAttributes: {},
75
+ });
76
+ });
77
+
78
+ it("publishes a message including custom message attribute", async () => {
79
+ const message = {
80
+ key: "value",
81
+ };
82
+
83
+ const send = vi.fn();
84
+
85
+ SNSClient.mockImplementation(function () {
86
+ return { send };
87
+ });
88
+
89
+ PublishCommand.mockImplementation(function (params) {
90
+ return params;
91
+ });
92
+ setupClient(mockLogger, {
93
+ region: "us-east-1",
94
+ awsEndpointUrl: "http://localhost:4566",
95
+ });
96
+
97
+ await publishMessage(
98
+ message,
99
+ { customAttribute: "customValue" },
100
+ "specialTopicArn",
101
+ );
102
+
103
+ expect(PublishCommand).toHaveBeenCalledWith({
104
+ TopicArn: "specialTopicArn",
105
+ Message: '{"key":"value"}',
106
+ MessageAttributes: {
107
+ customAttribute: {
108
+ DataType: "String",
109
+ StringValue: "customValue",
110
+ },
111
+ },
112
+ });
113
+
114
+ expect(send).toHaveBeenCalledWith({
115
+ TopicArn: "specialTopicArn",
116
+ Message: '{"key":"value"}',
117
+ MessageAttributes: {
118
+ customAttribute: {
119
+ DataType: "String",
120
+ StringValue: "customValue",
121
+ },
122
+ },
123
+ });
124
+ });
125
+
126
+ it("sets up client with defaults if config object is not supplied", async () => {
127
+ const send = vi.fn();
128
+ SNSClient.mockImplementation(function () {
129
+ return { send };
130
+ });
131
+
132
+ config.set("aws.region", "eu-west-1");
133
+ config.set("aws.endpointUrl", "http://localhost:4566");
134
+ config.set("aws.sns.configVersionTopicArn", "default-topic-arn");
135
+
136
+ setupClient(mockLogger);
137
+
138
+ expect(SNSClient).toHaveBeenCalledWith(
139
+ expect.objectContaining({
140
+ region: "eu-west-1",
141
+ endpoint: "http://localhost:4566",
142
+ }),
143
+ );
144
+
145
+ const message = { key: "value" };
146
+ await publishMessage(message);
147
+
148
+ expect(PublishCommand).toHaveBeenCalledWith(
149
+ expect.objectContaining({
150
+ TopicArn: "default-topic-arn",
151
+ }),
152
+ );
153
+ });
154
+ });