Skip to content

Commit

Permalink
Add source IP to the log (#100)
Browse files Browse the repository at this point in the history
* Add sourceIP to the log

* update if exist object en0

* fix test

* update git-secrets

* exclude to function
  • Loading branch information
resdenia authored Oct 11, 2022
1 parent a7151da commit 3fbdefc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/git-secrets.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 35 additions & 2 deletions lib/logzio-nodejs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { networkInterfaces } = require('os');
const stringifySafe = require('json-stringify-safe');
const assign = require('lodash.assign');
const dgram = require('dgram');
Expand Down Expand Up @@ -108,6 +109,7 @@ class LogzioLogger {
this.messages = [];
this.bulkId = 1;
this.extraFields = extraFields;
this.typeOfIP = 'IPv4';
}

_setProtocol(port) {
Expand Down Expand Up @@ -198,22 +200,53 @@ 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;
}
}
}

log(msg, obj) {
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') {
msg = {
message: msg,
};
}
this._addSourceIP(msg);
msg = assign(msg, this.extraFields);
if (!msg.type) {
msg.type = this.type;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"contributors": [
{
Expand Down
24 changes: 24 additions & 0 deletions test/logger.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { networkInterfaces } = require('os');
const sinon = require('sinon');
const nock = require('nock');
const assert = require('assert');
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3fbdefc

Please sign in to comment.