Skip to content

Commit bfd437b

Browse files
SpencerLNtrentm
andcommitted
fix(helpers): fix setting remote address and port in formatHttpRequest
Co-authored-by: Trent Mick <[email protected]> Refs: #19 Fixes: #18
1 parent 18d106e commit bfd437b

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

helpers/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @elastic/ecs-helpers Changelog
22

3+
## Unreleased
4+
5+
- Fix the setting of the remote IP and port
6+
[ECS client fields](https://www.elastic.co/guide/en/ecs/current/ecs-client.html):
7+
`client.address`, `client.ip`, `client.port`. This also supports using
8+
Express's `req.ip`.
9+
310
## v0.6.0
411

512
- Add `formatError` for adding [ECS Error fields](https://www.elastic.co/guide/en/ecs/current/ecs-error.html)

helpers/lib/http-formatters.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ function formatHttpRequest (ecs, req) {
2222
id,
2323
method,
2424
url,
25-
remoteAddress,
26-
remotePort,
2725
headers,
2826
hostname,
2927
httpVersion,
@@ -66,10 +64,21 @@ function formatHttpRequest (ecs, req) {
6664
}
6765
}
6866

69-
if (remoteAddress || remotePort) {
70-
ecs.client = ecs.client || {}
71-
ecs.client.address = remoteAddress
72-
ecs.client.port = remotePort
67+
// https://www.elastic.co/guide/en/ecs/current/ecs-client.html
68+
ecs.client = ecs.client || {}
69+
let ip
70+
if (req.ip) {
71+
// Express provides req.ip that may handle X-Forward-For processing.
72+
// https://expressjs.com/en/5x/api.html#req.ip
73+
ip = req.ip
74+
} else if (socket && socket.remoteAddress) {
75+
ip = socket.remoteAddress
76+
}
77+
if (ip) {
78+
ecs.client.ip = ecs.client.address = ip
79+
}
80+
if (socket) {
81+
ecs.client.port = socket.remotePort
7382
}
7483

7584
const hasHeaders = Object.keys(headers).length > 0

helpers/lib/serializer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const stringify = build({
105105
type: 'object',
106106
properties: {
107107
address: string,
108+
ip: string,
108109
port: number
109110
}
110111
},

helpers/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ test('formatHttpRequest and formatHttpResponse should return a valid ecs object'
175175
body: { bytes: contentLen }
176176
}
177177
})
178+
// https://www.elastic.co/guide/en/ecs/current/ecs-client.html fields
179+
t.ok(line.client, 'client fields are set')
180+
t.ok(line.client.address === '127.0.0.1' || line.client.address === '::ffff:127.0.0.1',
181+
'client.address is set')
182+
t.ok(line.client.ip === line.client.address,
183+
'client.address duplicated to client.ip')
184+
t.equal(typeof (line.client.port), 'number')
178185

179186
res.end(resBody)
180187
}

0 commit comments

Comments
 (0)