@alertlogic/al-collector-js 3.0.6 → 3.0.7

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/README.md CHANGED
@@ -25,7 +25,9 @@ const {
25
25
  AzcollectC,
26
26
  EndpointsC,
27
27
  AlLog,
28
- Parse
28
+ Parse ,
29
+ RestServiceClient,
30
+ CollectorStatusC
29
31
  } = require('@alertlogic/al-collector-js');
30
32
  ```
31
33
 
@@ -90,6 +92,14 @@ const azCollectClient = new AzcollectC(apiEndpoint, aimsCreds, collectorType, se
90
92
  ```javascript
91
93
  const alEndpointsClient = EndpointsC(apiEndpoint, aimsCreds, retryOption);
92
94
  ```
95
+ ## CollectorStatusC
96
+ * @param {string} apiEndpoint - Alert Logic API hostname.
97
+ * @param {Object} aimsCreds - Alert Logic API credentials object, refer to AimsC.
98
+ * @param {*} retryOptions.
99
+
100
+ ```javascript
101
+ const alCollectorStatusClient = CollectorStatusC(apiEndpoint, aimsCreds, retryOption);
102
+ ```
93
103
 
94
104
  ## AlLog
95
105
  * @param hostId - host uuid obtained at collector registration
package/al_util.js CHANGED
@@ -15,6 +15,7 @@ let MAX_CONNS_PER_SERVICE = 128;
15
15
 
16
16
  /**
17
17
  * @default Refer to https://www.npmjs.com/package/retry
18
+ * set maxRetryTime to 180seconds(3min) to avoid lambda timeout.
18
19
  */
19
20
  let DEFAULT_RETRY = {
20
21
  // Default values
@@ -22,7 +23,8 @@ let DEFAULT_RETRY = {
22
23
  factor: 7,
23
24
  minTimeout: 300,
24
25
  retries: 2,
25
- maxTimeout: 10000
26
+ maxTimeout: 10000,
27
+ maxRetryTime: 180000 // Maximum time to spend retrying in milliseconds.
26
28
  };
27
29
 
28
30
  /**
@@ -31,7 +31,9 @@ class CollectorStatusC extends AlServiceC {
31
31
  },
32
32
  body: data
33
33
  };
34
- return this.put(`/statuses/${statusId}/streams/${stream}`, payload);
34
+ // Few collector stream contain `/` which not excepted by collectors_status service. so Encode the stream before making the api call.It will encodes special characters including: , / ? : @ & = + $ #
35
+ const encodedStream = encodeURIComponent(stream);
36
+ return this.put(`/statuses/${statusId}/streams/${encodedStream}`, payload);
35
37
  }
36
38
  }
37
39
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alertlogic/al-collector-js",
3
- "version": "3.0.6",
3
+ "version": "3.0.7",
4
4
  "license": "MIT",
5
5
  "description": "Alert Logic Collector Common Library",
6
6
  "repository": {
package/test/al_mock.js CHANGED
@@ -159,6 +159,10 @@ const AIMS_RESPONSE_200 = {
159
159
  }
160
160
  };
161
161
 
162
+ const SERVER_ERROR_500 = {
163
+ statusCode: 500,
164
+ message: "Internal Server Error"
165
+ };
162
166
  function gen_auth_response() {
163
167
  return {
164
168
  authentication : {
@@ -188,6 +192,7 @@ module.exports = {
188
192
  AZURE_CHECKIN_VALUES: AZURE_CHECKIN_VALUES,
189
193
  SEND_COLLECTOR_STATUS_BODY_DATA: SEND_COLLECTOR_STATUS_BODY_DATA,
190
194
  COLLECTOR_STATUS_API: COLLECTOR_STATUS_API,
195
+ SERVER_ERROR_500: SERVER_ERROR_500,
191
196
 
192
197
  gen_auth_response : gen_auth_response
193
198
  };
@@ -132,5 +132,51 @@ describe('Unit Tests', function() {
132
132
  done();
133
133
  });
134
134
 
135
+ it('check it should return server error after retry', function (done) {
136
+ var restC = new RestServiceClient(m_alMock.AL_API);
137
+
138
+ nock('https://' + m_alMock.AL_API, {
139
+ reqheaders: {
140
+ 'accept': 'application/json',
141
+ 'host': m_alMock.AL_API,
142
+ 'some_header': 'some_value'
143
+ }
144
+ })
145
+ .post(TEST_PATH, TEST_BODY)
146
+ .reply(500, m_alMock.SERVER_ERROR_500);
147
+
148
+ restC.post(TEST_PATH, {
149
+ headers: { 'some_header': 'some_value' },
150
+ body: TEST_BODY
151
+ })
152
+ .catch(err => {
153
+ console.log(`err ${err}`);
154
+ done();
155
+ });
156
+ });
157
+
158
+ it('check it should success if last response is successful after server error 500', function (done) {
159
+ var restC = new RestServiceClient(m_alMock.AL_API);
160
+ var restMock = nock('https://' + m_alMock.AL_API, {
161
+ reqheaders: {
162
+ 'accept': 'application/json',
163
+ 'host': m_alMock.AL_API,
164
+ 'content-type': 'application/json',
165
+ 'some_header': 'some_value'
166
+ }
167
+ })
168
+ .post(TEST_PATH, TEST_BODY)
169
+ .reply(500, m_alMock.SERVER_ERROR_500)
170
+ .post(TEST_PATH, TEST_BODY)
171
+ .reply(200, m_alMock.AIMS_RESPONSE_200);
172
+
173
+ restC.post(TEST_PATH, {
174
+ headers: { 'some_header': 'some_value' },
175
+ body: TEST_BODY
176
+ }).then(res => {
177
+ assert.ok(restMock.isDone());
178
+ done();
179
+ });
180
+ });
135
181
  });
136
182
  });
@@ -54,6 +54,24 @@ describe('Unit Tests', function () {
54
54
  });
55
55
  });
56
56
 
57
+ it('it should encode the stream if it contain special char', function (done) {
58
+ let stream = 'projects/appliance-builds';
59
+ const encodedStream = encodeURIComponent(stream);
60
+ fakePut = sinon.stub(AlServiceC.prototype, 'put').callsFake(
61
+ function fakeFn(path, extraOptions) {
62
+ assert.equal(extraOptions.body, m_alMock.SEND_COLLECTOR_STATUS_BODY_DATA);
63
+ assert.equal(path, `/statuses/${m_alMock.SEND_COLLECTOR_STATUS_BODY_DATA.status_id}/streams/${encodedStream}`);
64
+ done();
65
+ });
66
+
67
+ var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
68
+ var collectorStatus = new CollectorStatusC(m_alMock.COLLECTOR_STATUS_API, aimsc);
69
+ m_alMock.SEND_COLLECTOR_STATUS_BODY_DATA.stream = stream;
70
+ collectorStatus.sendStatus(m_alMock.SEND_COLLECTOR_STATUS_BODY_DATA.status_id, m_alMock.SEND_COLLECTOR_STATUS_BODY_DATA.stream, m_alMock.SEND_COLLECTOR_STATUS_BODY_DATA).then(res => {
71
+ fakePut.restore();
72
+ });
73
+ });
74
+
57
75
  it('If sequence of parameter is not correct then api throw the error', function (done) {
58
76
  const error = {
59
77
  "errorinfo": {
@@ -14,6 +14,7 @@ const nock = require('nock');
14
14
  const m_alMock = require('./al_mock');
15
15
  const m_alService = require('../al_servicec');
16
16
  const EndpointsC = require('../al_servicec').EndpointsC;
17
+ const moment = require('moment');
17
18
 
18
19
  describe('HTTP request retry tests', function() {
19
20
  beforeEach(function(done) {
@@ -202,4 +203,33 @@ describe('HTTP request retry tests', function() {
202
203
  return done();
203
204
  });
204
205
  });
206
+
207
+ it('Retry 500 with custom retry config with maxRetryTime', function (done) {
208
+ const maxRetryTime = 1500;
209
+ const retryOptions = {
210
+ retries: 3,
211
+ factor: 2,
212
+ minTimeout: 300,
213
+ maxTimeout: 1000,
214
+ maxRetryTime: maxRetryTime,
215
+ };
216
+ nock('https://' + m_alMock.AL_API)
217
+ .post('/aims/v1/authenticate')
218
+ .times(4)
219
+ .reply(500, m_alMock.SERVER_ERROR_500);
220
+ var startTime = moment();
221
+ var aimsc = new m_alService.AimsC(
222
+ m_alMock.AL_API, m_alMock.AIMS_CREDS, '/tmp', retryOptions);
223
+ aimsc.authenticate()
224
+ .catch(err => {
225
+ assert.equal(err.statusCode, 500);
226
+ var nowMoment = moment();
227
+ const elapsedTime = nowMoment.diff(startTime, 'milliseconds');
228
+ assert.ok(
229
+ elapsedTime >= maxRetryTime && elapsedTime <= maxRetryTime + retryOptions.maxTimeout,
230
+ 'Test case failed: Request did not complete within maxRetryTime.'
231
+ );
232
+ return done();
233
+ });
234
+ });
205
235
  });