Persist NinjaOne context for scheduled scans
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Text.RegularExpressions;
|
||||
using OCSentinelCli.Configuration;
|
||||
|
||||
namespace OCSentinelCli;
|
||||
|
||||
@@ -68,7 +69,7 @@ internal sealed class AttackScanner
|
||||
{
|
||||
SchemaVersion = "2.0",
|
||||
MachineName = Environment.MachineName,
|
||||
NinjaOne = GetNinjaOneContext(),
|
||||
NinjaOne = GetNinjaOneContext(options, errors),
|
||||
GeneratedAtLocal = generatedAtLocal,
|
||||
GeneratedAtUtc = generatedAtUtc,
|
||||
ClientVersion = BuildMetadata.Version,
|
||||
@@ -93,19 +94,38 @@ internal sealed class AttackScanner
|
||||
};
|
||||
}
|
||||
|
||||
private static NinjaOneContext GetNinjaOneContext()
|
||||
private static NinjaOneContext GetNinjaOneContext(ScanOptions options, List<string> errors)
|
||||
{
|
||||
ClientConfiguration? clientConfiguration = null;
|
||||
if (!string.IsNullOrWhiteSpace(options.ClientConfigPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
clientConfiguration = ClientConfiguration.Load(options.ClientConfigPath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
errors.Add($"Could not load persisted NinjaOne context: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return new NinjaOneContext
|
||||
{
|
||||
OrganizationId = ReadEnvironmentVariable("NINJA_ORGANIZATION_ID"),
|
||||
OrganizationName = ReadEnvironmentVariable("NINJA_ORGANIZATION_NAME"),
|
||||
MachineId = ReadEnvironmentVariable("NINJA_AGENT_MACHINE_ID"),
|
||||
NodeId = ReadEnvironmentVariable("NINJA_AGENT_NODE_ID"),
|
||||
LocationId = ReadEnvironmentVariable("NINJA_LOCATION_ID"),
|
||||
LocationName = ReadEnvironmentVariable("NINJA_LOCATION_NAME")
|
||||
OrganizationId = ReadContextValue("NINJA_ORGANIZATION_ID", clientConfiguration?.NinjaOrganizationId),
|
||||
OrganizationName = ReadContextValue("NINJA_ORGANIZATION_NAME", clientConfiguration?.NinjaOrganizationName),
|
||||
MachineId = ReadContextValue("NINJA_AGENT_MACHINE_ID", clientConfiguration?.NinjaMachineId),
|
||||
NodeId = ReadContextValue("NINJA_AGENT_NODE_ID", clientConfiguration?.NinjaNodeId),
|
||||
LocationId = ReadContextValue("NINJA_LOCATION_ID", clientConfiguration?.NinjaLocationId),
|
||||
LocationName = ReadContextValue("NINJA_LOCATION_NAME", clientConfiguration?.NinjaLocationName)
|
||||
};
|
||||
}
|
||||
|
||||
private static string ReadContextValue(string environmentName, string? persistedValue)
|
||||
{
|
||||
string currentValue = ReadEnvironmentVariable(environmentName);
|
||||
return string.IsNullOrWhiteSpace(currentValue) ? persistedValue?.Trim() ?? string.Empty : currentValue;
|
||||
}
|
||||
|
||||
private static string ReadEnvironmentVariable(string name)
|
||||
{
|
||||
return Environment.GetEnvironmentVariable(name)?.Trim() ?? string.Empty;
|
||||
|
||||
Reference in New Issue
Block a user