-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add detailed assembly reporting to enable Vulnerability Managem…
…ent support. (#1685)
- Loading branch information
Showing
21 changed files
with
1,179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Agent/NewRelic/Agent/Core/JsonConverters/LoadedModuleWireModelCollectionJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using NewRelic.Agent.Core.WireModels; | ||
using Newtonsoft.Json; | ||
|
||
namespace NewRelic.Agent.Core.JsonConverters | ||
{ | ||
public class LoadedModuleWireModelCollectionJsonConverter : JsonConverter<LoadedModuleWireModelCollection> | ||
{ | ||
// The payload is labeled "Jars" since the collector method was originally meant for and used by Java. | ||
private const string JarsName = "Jars"; | ||
|
||
public override LoadedModuleWireModelCollection ReadJson(JsonReader reader, Type objectType, LoadedModuleWireModelCollection existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void WriteJson(JsonWriter jsonWriter, LoadedModuleWireModelCollection value, JsonSerializer serializer) | ||
{ | ||
WriteJsonImpl(jsonWriter, value); | ||
} | ||
|
||
private static void WriteJsonImpl(JsonWriter jsonWriter, LoadedModuleWireModelCollection value) | ||
{ | ||
jsonWriter.WriteValue(JarsName); | ||
|
||
jsonWriter.WriteStartArray(); | ||
|
||
foreach (var loadedModule in value.LoadedModules) | ||
{ | ||
// MODULE | ||
jsonWriter.WriteStartArray(); | ||
|
||
jsonWriter.WriteValue(loadedModule.AssemblyName); | ||
jsonWriter.WriteValue(loadedModule.Version ?? " "); | ||
|
||
// DATA DICTIONARY | ||
jsonWriter.WriteStartObject(); | ||
foreach (var item in loadedModule.Data) | ||
{ | ||
jsonWriter.WritePropertyName(item.Key); | ||
jsonWriter.WriteValue(item.Value.ToString()); | ||
} | ||
|
||
jsonWriter.WriteEndObject(); | ||
|
||
jsonWriter.WriteEndArray(); | ||
} | ||
|
||
jsonWriter.WriteEndArray(); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Agent/NewRelic/Agent/Core/Utilities/UpdatedLoadedModulesService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NewRelic.Agent.Configuration; | ||
using NewRelic.Agent.Core.DataTransport; | ||
using NewRelic.Agent.Core.Time; | ||
using NewRelic.Agent.Core.WireModels; | ||
|
||
namespace NewRelic.Agent.Core.Utilities | ||
{ | ||
public class UpdatedLoadedModulesService : DisposableService | ||
{ | ||
private readonly IList<string> _loadedModulesSeen = new List<string>(); | ||
private readonly IScheduler _scheduler; | ||
private readonly IDataTransportService _dataTransportService; | ||
private readonly IConfigurationService _configurationService; | ||
private IConfiguration _configuration => _configurationService?.Configuration; | ||
|
||
public UpdatedLoadedModulesService(IScheduler scheduler, IDataTransportService dataTransportService, IConfigurationService configurationService) | ||
{ | ||
_configurationService = configurationService; | ||
_dataTransportService = dataTransportService; | ||
_scheduler = scheduler; | ||
_scheduler.ExecuteEvery(GetLoadedModules, _configuration.UpdateLoadedModulesCycle); | ||
} | ||
|
||
private void GetLoadedModules() | ||
{ | ||
var assemblies = AppDomain.CurrentDomain.GetAssemblies() | ||
.Where(assembly => assembly != null) | ||
.Where(assembly => !_loadedModulesSeen.Contains(assembly.GetName().Name)) | ||
#if NETFRAMEWORK | ||
.Where(assembly => !(assembly is System.Reflection.Emit.AssemblyBuilder)) | ||
#endif | ||
.ToList(); | ||
|
||
if (assemblies.Count < 1) | ||
{ | ||
return; | ||
} | ||
|
||
var loadedModulesCollection = LoadedModuleWireModelCollection.Build(assemblies); | ||
|
||
SendUpdatedLoadedModules(loadedModulesCollection); | ||
} | ||
|
||
private void SendUpdatedLoadedModules(LoadedModuleWireModelCollection loadedModulesCollection) | ||
{ | ||
var responseStatus = _dataTransportService.Send(loadedModulesCollection); | ||
if (responseStatus != DataTransportResponseStatus.RequestSuccessful) | ||
{ | ||
// Try again next time | ||
return; | ||
} | ||
|
||
foreach (var module in loadedModulesCollection.LoadedModules) | ||
{ | ||
_loadedModulesSeen.Add(module.Data["namespace"].ToString()); | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Agent/NewRelic/Agent/Core/WireModels/LoadedModuleWireModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace NewRelic.Agent.Core.WireModels | ||
{ | ||
public class LoadedModuleWireModel | ||
{ | ||
public string AssemblyName { get; } | ||
|
||
public string Version { get; } | ||
|
||
public Dictionary<string, object> Data { get; } | ||
|
||
public LoadedModuleWireModel(string assemblyName, string version) | ||
{ | ||
AssemblyName = assemblyName; | ||
Version = version; | ||
Data = new Dictionary<string, object>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.