· 4 years ago · Jul 10, 2021, 12:24 PM
1/*
2 Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this
4 software and associated documentation files (the "Software"), to deal in the Software
5 without restriction, including without limitation the rights to use, copy, modify,
6 merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7 permit persons to whom the Software is furnished to do so.
8 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
9 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
10 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
11 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
12 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
13 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14*/
15
16// Load Stomp, Apache MQ JS Client
17var Stomp = require('stomp-client');
18
19// Load the AWS SDK
20var AWS = require('aws-sdk'),
21 region = "ap-southeast-2",
22 secretName = "",
23 secret = "nothing",
24 decodedBinarySecret;
25
26
27// Create a Secrets Manager client
28var secret_client = new AWS.SecretsManager({
29 region: region
30});
31
32// Create a new SSM Client
33var ssm_client = new AWS.SSM({
34 region: region
35});
36
37
38
39
40
41exports.lambdaHandler = async (event, context, callback) => {
42
43 //console.debug("Invoked with Event: ", event);
44
45 if(event.source == "aws.events"){
46 await automatedInvocation(event);
47 }else{
48 await manualInvocation(event);
49 }
50
51
52
53};
54
55async function manualInvocation(event){
56
57 console.debug("Manual Invocation with event data: ", event);
58
59 var options = await fetchMQDetails(event.MQ_SERVER)
60
61 var stomp_client = new Stomp(options);
62
63 stomp_client.connect(() => {
64
65 stomp_client.publish(event.MQ_QUEUE, event.payload);
66 stomp_client.disconnect();
67
68 });
69
70
71}
72
73
74async function automatedInvocation(event){
75
76 console.debug("Automated Invocation with event data: ", event);
77
78 var paramName = process.env.lamda-mq-parametername;
79
80 var parameters = await fetchSSMDetails(paramName);
81
82
83 console.log("Received Parameters: ", parameters);
84
85 parameters.tasks.forEach(task => {
86 manualInvocation(task);
87 });
88
89
90
91
92
93
94
95}
96
97async function fetchSSMDetails(parameterName){
98
99
100 var param = await ssm_client.getParameter({Name: parameterName}).promise();
101
102 var parsedParam = JSON.parse(param.Parameter.Value);
103
104 return parsedParam;
105}
106
107
108async function fetchMQDetails(serverName){
109
110 var options = {
111 address: "unknown",
112 user: "",
113 pass: "",
114 tls: {},
115 port: null
116 }
117
118 var secretName = process.env.MQ_SECRET_PREFIX + serverName;
119
120 var secret = await secret_client.getSecretValue({SecretId: secretName}).promise();
121 var parsedSecret = JSON.parse(secret.SecretString);
122
123 options.address = parsedSecret.MQ_ADDRESS;
124 options.port = parsedSecret.MQ_PORT;
125 options.user = parsedSecret.MQ_USERNAME;
126 options.pass = parsedSecret.MQ_PASSWORD;
127 options.tls = parsedSecret.MQ_TLS;
128
129 // console.debug("Returning the following options: ", options);
130
131 return options;
132
133}
134
135
136
137
138// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
139// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
140// We rethrow the exception by default.
141
142secret_client.getSecretValue({ SecretId: secretName }, function(err, data) {
143
144 if (err) {
145 if (err.code === 'DecryptionFailureException')
146 // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
147 // Deal with the exception here, and/or rethrow at your discretion.
148 throw err;
149 else if (err.code === 'InternalServiceErrorException')
150 // An error occurred on the server side.
151 // Deal with the exception here, and/or rethrow at your discretion.
152 throw err;
153 else if (err.code === 'InvalidParameterException')
154 // You provided an invalid value for a parameter.
155 // Deal with the exception here, and/or rethrow at your discretion.
156 throw err;
157 else if (err.code === 'InvalidRequestException')
158 // You provided a parameter value that is not valid for the current state of the resource.
159 // Deal with the exception here, and/or rethrow at your discretion.
160 throw err;
161 else if (err.code === 'ResourceNotFoundException')
162 // We can't find the resource that you asked for.
163 // Deal with the exception here, and/or rethrow at your discretion.
164 throw err;
165 } else {
166 // Decrypts secret using the associated KMS CMK.
167 // Depending on whether the secret is a string or binary, one of these fields will be populated.
168 if ('SecretString' in data) {
169 secret = data.SecretString;
170
171 } else {
172 let buff = new Buffer(data.SecretBinary, 'base64');
173 decodedBinarySecret = buff.toString('ascii');
174
175 }
176 }
177
178});