diff --git a/.github/workflows/git-secrets.yml b/.github/workflows/git-secrets.yml index a3dce06..a098f21 100644 --- a/.github/workflows/git-secrets.yml +++ b/.github/workflows/git-secrets.yml @@ -1,33 +1,31 @@ name: git-secrets - # Controls when the workflow will run # Triggers the workflow on push or pull request events but only for the main branch on: [push] - # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "main" git-secrets: # The type of runner that the job will run on - runs-on: ubuntu-18.04 - + runs-on: ubuntu-22.04 # Steps represent a sequence of tasks that will be executed as part of the job steps: - name: Check Out Source Code uses: actions/checkout@v2 - - name: Set up Python 3.8 uses: actions/setup-python@v2 with: python-version: 3.8 - name: Installing dependencies run: - sudo apt-get install git less openssh-server + sudo apt-get install less openssh-server - name: Installing scanning tool run: | + eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" brew install git-secrets git secrets --install git secrets --register-aws - name: Running scanning tool run: - git secrets --scan + eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" + git secrets --scan \ No newline at end of file diff --git a/README.md b/README.md index 757de39..86537a8 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ A few notes are worth mentioning regarding the use of the UDP protocol: ## Update log + +**2.1.5** +- Add sourceIP as a new field to each log + **2.1.4** - Replace from request to axios diff --git a/lib/logzio-nodejs.js b/lib/logzio-nodejs.js index 497f072..5da05f0 100755 --- a/lib/logzio-nodejs.js +++ b/lib/logzio-nodejs.js @@ -1,3 +1,4 @@ +const { networkInterfaces } = require('os'); const stringifySafe = require('json-stringify-safe'); const assign = require('lodash.assign'); const dgram = require('dgram'); @@ -108,6 +109,7 @@ class LogzioLogger { this.messages = []; this.bulkId = 1; this.extraFields = extraFields; + this.typeOfIP = 'IPv4'; } _setProtocol(port) { @@ -198,7 +200,37 @@ class LogzioLogger { if (this.addTimestampWithNanoSecs) { const time = process.hrtime(); - msg['@timestamp_nano'] = msg['@timestamp_nano'] || [now, time[1].toString().padStart(nanoSecDigits ,'0')].join('-'); + msg['@timestamp_nano'] = msg['@timestamp_nano'] || [now, time[1].toString().padStart(nanoSecDigits, '0')].join('-'); + } + } + + /** + * Attach a Source IP to the log record. + * @param msg - The message (Object) to append the timestamp to. + * @private + */ + _addSourceIP(msg) { + const { en0 } = networkInterfaces(); + if (en0 && en0.length > 0) { + const relevantIPs = []; + en0.forEach((ip) => { + // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses + // 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6 + const familyV4Value = typeof ip.family === 'string' ? this.typeOfIP : 4; + if (ip.family === familyV4Value && !ip.internal) { + relevantIPs.push(ip.address); + // msg.sourceIP = ip.address; + } + }); + + if (relevantIPs.length > 1) { + relevantIPs.forEach((ip, idx) => { + msg[`sourceIP_${idx}`] = ip; + }); + } else if (relevantIPs.length === 1) { + const [sourceIP] = relevantIPs; + msg.sourceIP = sourceIP; + } } } @@ -206,7 +238,7 @@ class LogzioLogger { if (this.closed === true) { throw new Error('Logging into a logger that has been closed!'); } - if (![null, undefined].includes(obj)){ + if (![null, undefined].includes(obj)) { msg += JSON.stringify(obj); } if (typeof msg === 'string') { @@ -214,6 +246,7 @@ class LogzioLogger { message: msg, }; } + this._addSourceIP(msg); msg = assign(msg, this.extraFields); if (!msg.type) { msg.type = this.type; diff --git a/package-lock.json b/package-lock.json index 9606a4a..1893414 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "logzio-nodejs", - "version": "2.0.4", + "version": "2.1.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "logzio-nodejs", - "version": "2.0.4", + "version": "2.1.4", "license": "(Apache-2.0)", "dependencies": { "axios": "0.27.2", diff --git a/package.json b/package.json index 5943bda..013774f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "logzio-nodejs", "description": "A nodejs implementation for sending logs to Logz.IO cloud service Copy of logzio-nodejs", - "version": "2.1.4", + "version": "2.1.5", "author": "Gilly Barr ", "contributors": [ { diff --git a/test/logger.test.js b/test/logger.test.js index 55a1581..2bc0d2e 100644 --- a/test/logger.test.js +++ b/test/logger.test.js @@ -1,3 +1,4 @@ +const { networkInterfaces } = require('os'); const sinon = require('sinon'); const nock = require('nock'); const assert = require('assert'); @@ -94,7 +95,30 @@ describe('logger', () => { logger._createBulk.restore(); logger.close(); }); + it('log with sourceIP', (done) => { + const logger = createLogger({ + bufferSize: 1, + callback: done, + }); + sinon.spy(logger, '_createBulk'); + const { en0 } = networkInterfaces(); + let sourceIP; + if (en0 && en0.length > 0) { + en0.forEach((ip) => { + // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses + // 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6 + const familyV4Value = typeof ip.family === 'string' ? 'IPv4' : 4; + if (ip.family === familyV4Value && !ip.internal) { + sourceIP = ip.address; + } + }); + } + logger.log({ message: 'sourceIp' }); + assert.equal(logger._createBulk.getCall(0).args[0][0].sourceIP, sourceIP); + logger._createBulk.restore(); + logger.close(); + }); it('sends log as a string with extra fields', (done) => { const logger = createLogger({ bufferSize: 1,