File tree Expand file tree Collapse file tree 4 files changed +30
-6
lines changed Expand file tree Collapse file tree 4 files changed +30
-6
lines changed Original file line number Diff line number Diff line change 1
1
# @elastic/ecs-helpers Changelog
2
2
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
+
3
10
## v0.6.0
4
11
5
12
- Add ` formatError ` for adding [ ECS Error fields] ( https://www.elastic.co/guide/en/ecs/current/ecs-error.html )
Original file line number Diff line number Diff line change @@ -22,8 +22,6 @@ function formatHttpRequest (ecs, req) {
22
22
id,
23
23
method,
24
24
url,
25
- remoteAddress,
26
- remotePort,
27
25
headers,
28
26
hostname,
29
27
httpVersion,
@@ -66,10 +64,21 @@ function formatHttpRequest (ecs, req) {
66
64
}
67
65
}
68
66
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
73
82
}
74
83
75
84
const hasHeaders = Object . keys ( headers ) . length > 0
Original file line number Diff line number Diff line change @@ -105,6 +105,7 @@ const stringify = build({
105
105
type : 'object' ,
106
106
properties : {
107
107
address : string ,
108
+ ip : string ,
108
109
port : number
109
110
}
110
111
} ,
Original file line number Diff line number Diff line change @@ -175,6 +175,13 @@ test('formatHttpRequest and formatHttpResponse should return a valid ecs object'
175
175
body : { bytes : contentLen }
176
176
}
177
177
} )
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' )
178
185
179
186
res . end ( resBody )
180
187
}
You can’t perform that action at this time.
0 commit comments