Skip to content

Commit 8693f3a

Browse files
authored
Implement ECS schema 1.5 (#60)
Implement ECS schema 1.5
1 parent 795eafc commit 8693f3a

38 files changed

+27664
-1153
lines changed

src/Elastic.CommonSchema.Serilog/Http/HttpContextAccessorAdapter.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,35 @@ public Url Url
109109
Original = _httpContextAccessor.HttpContext.Request.Path,
110110
Full = uri.ToString(),
111111
Scheme = uri.Scheme,
112-
Query = uri.Query,
112+
Query = string.IsNullOrEmpty(uri.Query) ? null : uri.Query,
113113
Domain = uri.Authority,
114114
Port = uri.Port
115115
};
116116
}
117117
}
118118

119-
public Server Server => _httpContextAccessor.HttpContext == null ? null : new Server
119+
public Server Server
120120
{
121-
Domain = ConvertToUri(_httpContextAccessor.HttpContext.Request).Authority
122-
};
121+
get
122+
{
123+
if (_httpContextAccessor.HttpContext == null)
124+
return null;
125+
126+
var ip4 = _httpContextAccessor.HttpContext.Connection.LocalIpAddress.MapToIPv4();
127+
128+
var uri = ConvertToUri(_httpContextAccessor.HttpContext.Request);
129+
130+
return new Server
131+
{
132+
Address = ip4.ToString(),
133+
Ip = ip4.ToString(),
134+
Domain = uri.Authority
135+
};
136+
}
137+
}
123138

124-
private Uri ConvertToUri(Microsoft.AspNetCore.Http.HttpRequest request)
125-
=> new Uri($"{request.Scheme}://{request.Host}{request.Path}");
139+
private static Uri ConvertToUri(Microsoft.AspNetCore.Http.HttpRequest request) =>
140+
new Uri($"{request.Scheme}://{request.Host}{request.Path}");
126141

127142
public Client Client
128143
{
@@ -131,12 +146,12 @@ public Client Client
131146
if (_httpContextAccessor.HttpContext == null)
132147
return null;
133148

134-
var address = _httpContextAccessor.HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress.ToString();
149+
var ip4 = _httpContextAccessor.HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress.MapToIPv4();
135150

136151
return new Client
137152
{
138-
Address = address,
139-
Ip = address,
153+
Address = ip4.ToString(),
154+
Ip = ip4.ToString(),
140155
Bytes = _httpContextAccessor.HttpContext.Request.ContentLength,
141156
User = User
142157
};
@@ -168,7 +183,7 @@ public User User
168183

169184
return new User
170185
{
171-
Id = hasIdClaim ? new[] { idClaim.First().Value } : null,
186+
Id = hasIdClaim ? idClaim.First().Value : null,
172187
Name = hasNameClaim ? nameClaim.First().Value : null,
173188
Email = hasEmailClaim ? emailClaim.First().Value : null,
174189
Hash = hasHashClaim ? hashClaim.First().Value : null,

src/Elastic.CommonSchema.Serilog/LogEventConverter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,22 @@ private static Process GetProcess(LogEvent e, bool mapFromCurrentThread)
224224
{
225225
return new Process
226226
{
227-
Title = processName,
227+
Title = string.IsNullOrEmpty(processName) ? null : processName,
228228
Name = processName,
229229
Pid = pid,
230-
Thread = int.TryParse(threadId ?? processId ?? "", out var id)
231-
? new ProcessThread() { Id = id }
230+
Thread = int.TryParse(threadId ?? processId, out var id)
231+
? new ProcessThread { Id = id }
232232
: null,
233233
};
234234
}
235235

236236
var currentThread = Thread.CurrentThread;
237237
var process = TryGetProcess(pid);
238238

239+
var mainWindowTitle = process?.MainWindowTitle;
239240
return new Process
240241
{
241-
Title = process?.MainWindowTitle,
242+
Title = string.IsNullOrEmpty(mainWindowTitle) ? null : mainWindowTitle,
242243
Name = process?.ProcessName ?? processName,
243244
Pid = process?.Id ?? pid,
244245
Executable = process?.ProcessName ?? processName,

src/Elastic.CommonSchema/Base.Serialization.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ namespace Elastic.CommonSchema.Serialization
1616
{
1717
/// <summary>
1818
/// This static class allows you to deserialize subclasses of <see cref="Base"/>
19-
/// If you are dealing with <see cref="Base"/> directly you do not need to use this class.
20-
/// Use <see cref="Base.Deserialize(string)"/> and the overloads instead.
21-
/// Note this class should only be used for advanced use cases, for simpler use cases you can utilise the <see cref="Base.Metadata"/> property.
19+
/// If you are dealing with <see cref="Base"/> directly you do not need to use this class,
20+
/// use <see cref="Base.Deserialize(string)"/> and the overloads instead.
2221
/// </summary>
22+
/// <remarks>
23+
/// This class should only be used for advanced use cases, for simpler use cases you can utilise the <see cref="Base.Metadata"/> property.
24+
/// </remarks>
2325
/// <typeparam name="TBase">Type of the <see cref="Base"/> subclass</typeparam>
2426
public static class EcsSerializerFactory<TBase> where TBase : Base, new()
2527
{

src/Elastic.CommonSchema/Serialization/BaseJsonConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public override TBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
1616
reader.Read();
1717
return null;
1818
}
19-
if (reader.TokenType != JsonTokenType.StartObject) throw new JsonException();
19+
if (reader.TokenType != JsonTokenType.StartObject)
20+
throw new JsonException();
2021

2122
var ecsEvent = new TBase();
2223

src/Elastic.CommonSchema/Serialization/BaseJsonFormatter.Generated.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ ref string loglevel
4141
"as" => ReadProp<As>(ref reader, "as", ecsEvent, (b, v) => b.As = v),
4242
"client" => ReadProp<Client>(ref reader, "client", ecsEvent, (b, v) => b.Client = v),
4343
"cloud" => ReadProp<Cloud>(ref reader, "cloud", ecsEvent, (b, v) => b.Cloud = v),
44+
"code_signature" => ReadProp<CodeSignature>(ref reader, "code_signature", ecsEvent, (b, v) => b.CodeSignature = v),
4445
"container" => ReadProp<Container>(ref reader, "container", ecsEvent, (b, v) => b.Container = v),
4546
"destination" => ReadProp<Destination>(ref reader, "destination", ecsEvent, (b, v) => b.Destination = v),
47+
"dll" => ReadProp<Dll>(ref reader, "dll", ecsEvent, (b, v) => b.Dll = v),
4648
"dns" => ReadProp<Dns>(ref reader, "dns", ecsEvent, (b, v) => b.Dns = v),
4749
"ecs" => ReadProp<Ecs>(ref reader, "ecs", ecsEvent, (b, v) => b.Ecs = v),
4850
"error" => ReadProp<Error>(ref reader, "error", ecsEvent, (b, v) => b.Error = v),
@@ -53,12 +55,14 @@ ref string loglevel
5355
"hash" => ReadProp<Hash>(ref reader, "hash", ecsEvent, (b, v) => b.Hash = v),
5456
"host" => ReadProp<Host>(ref reader, "host", ecsEvent, (b, v) => b.Host = v),
5557
"http" => ReadProp<Http>(ref reader, "http", ecsEvent, (b, v) => b.Http = v),
58+
"interface" => ReadProp<Interface>(ref reader, "interface", ecsEvent, (b, v) => b.Interface = v),
5659
"log" => ReadProp<Log>(ref reader, "log", ecsEvent, (b, v) => b.Log = v),
5760
"network" => ReadProp<Network>(ref reader, "network", ecsEvent, (b, v) => b.Network = v),
5861
"observer" => ReadProp<Observer>(ref reader, "observer", ecsEvent, (b, v) => b.Observer = v),
5962
"organization" => ReadProp<Organization>(ref reader, "organization", ecsEvent, (b, v) => b.Organization = v),
6063
"os" => ReadProp<Os>(ref reader, "os", ecsEvent, (b, v) => b.Os = v),
6164
"package" => ReadProp<Package>(ref reader, "package", ecsEvent, (b, v) => b.Package = v),
65+
"pe" => ReadProp<Pe>(ref reader, "pe", ecsEvent, (b, v) => b.Pe = v),
6266
"process" => ReadProp<Process>(ref reader, "process", ecsEvent, (b, v) => b.Process = v),
6367
"registry" => ReadProp<Registry>(ref reader, "registry", ecsEvent, (b, v) => b.Registry = v),
6468
"related" => ReadProp<Related>(ref reader, "related", ecsEvent, (b, v) => b.Related = v),
@@ -71,6 +75,7 @@ ref string loglevel
7175
"url" => ReadProp<Url>(ref reader, "url", ecsEvent, (b, v) => b.Url = v),
7276
"user" => ReadProp<User>(ref reader, "user", ecsEvent, (b, v) => b.User = v),
7377
"user_agent" => ReadProp<UserAgent>(ref reader, "user_agent", ecsEvent, (b, v) => b.UserAgent = v),
78+
"vlan" => ReadProp<Vlan>(ref reader, "vlan", ecsEvent, (b, v) => b.Vlan = v),
7479
"vulnerability" => ReadProp<Vulnerability>(ref reader, "vulnerability", ecsEvent, (b, v) => b.Vulnerability = v),
7580
_ =>
7681
typeof(Base) == ecsEvent.GetType()
@@ -103,8 +108,10 @@ public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOpt
103108
WriteProp(writer, "as", value.As);
104109
WriteProp(writer, "client", value.Client);
105110
WriteProp(writer, "cloud", value.Cloud);
111+
WriteProp(writer, "code_signature", value.CodeSignature);
106112
WriteProp(writer, "container", value.Container);
107113
WriteProp(writer, "destination", value.Destination);
114+
WriteProp(writer, "dll", value.Dll);
108115
WriteProp(writer, "dns", value.Dns);
109116
WriteProp(writer, "ecs", value.Ecs);
110117
WriteProp(writer, "error", value.Error);
@@ -115,12 +122,14 @@ public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOpt
115122
WriteProp(writer, "hash", value.Hash);
116123
WriteProp(writer, "host", value.Host);
117124
WriteProp(writer, "http", value.Http);
125+
WriteProp(writer, "interface", value.Interface);
118126
WriteProp(writer, "log", value.Log);
119127
WriteProp(writer, "network", value.Network);
120128
WriteProp(writer, "observer", value.Observer);
121129
WriteProp(writer, "organization", value.Organization);
122130
WriteProp(writer, "os", value.Os);
123131
WriteProp(writer, "package", value.Package);
132+
WriteProp(writer, "pe", value.Pe);
124133
WriteProp(writer, "process", value.Process);
125134
WriteProp(writer, "registry", value.Registry);
126135
WriteProp(writer, "related", value.Related);
@@ -133,6 +142,7 @@ public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOpt
133142
WriteProp(writer, "url", value.Url);
134143
WriteProp(writer, "user", value.User);
135144
WriteProp(writer, "user_agent", value.UserAgent);
145+
WriteProp(writer, "vlan", value.Vlan);
136146
WriteProp(writer, "vulnerability", value.Vulnerability);
137147
if (typeof(Base) != value.GetType())
138148
value.WriteAdditionalProperties((k, v) => WriteProp(writer, k, v));

src/Elastic.CommonSchema/Serialization/EcsJsonConverterBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ internal static object ReadPropDeserialize(ref Utf8JsonReader reader, Type type)
5454
return JsonSerializer.Deserialize(ref reader, type, options);
5555
}
5656

57-
protected static TValue ReadProp<TValue>(ref Utf8JsonReader reader, string key)
58-
where TValue : class
57+
protected static TValue ReadProp<TValue>(ref Utf8JsonReader reader, string key) where TValue : class
5958
{
6059
if (reader.TokenType == JsonTokenType.Null) return null;
6160

src/Elastic.CommonSchema/Serialization/LogJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override Log Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSeri
3838
"origin" => ReadProp<LogOrigin>(ref reader, "origin", log, (b, v) => b.Origin = v),
3939
"original" => ReadString(ref reader, ref original),
4040
"level" => ReadString(ref reader, ref loglevel),
41-
"syslog" => ReadProp<LogSyslog[]>(ref reader, "syslog", log, (b, v) => b.Syslog = v),
41+
"syslog" => ReadProp<LogSyslog>(ref reader, "syslog", log, (b, v) => b.Syslog = v),
4242
"logger" => ReadString(ref reader, ref logger),
4343
_ => false
4444
};

0 commit comments

Comments
 (0)