Skip to content

Commit b66081b

Browse files
Merge pull request #68 from AikidoSec/Reported-version-fix
cleanup version before reporting to zen
2 parents c8e2e74 + a252b0f commit b66081b

File tree

2 files changed

+76
-37
lines changed

2 files changed

+76
-37
lines changed
Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,59 @@
11
using Aikido.Zen.Core.Models;
22
using System;
3+
using System.Linq;
34
using System.Runtime.CompilerServices;
45
using System.Runtime.InteropServices;
56
[assembly: InternalsVisibleTo("Aikido.Zen.Tests")]
67
namespace Aikido.Zen.Core.Helpers
78
{
8-
/// <summary>
9-
/// Helper class for retrieving agent information about the current runtime environment.
10-
/// </summary>
11-
internal class AgentInfoHelper
12-
{
13-
// Cache the AgentInfo object to avoid repeated creation
14-
private static readonly AgentInfo _cachedAgentInfo = new AgentInfo
15-
{
16-
Hostname = Environment.MachineName,
17-
Os = new Os
18-
{
19-
Version = Environment.OSVersion.VersionString,
20-
Name = Environment.OSVersion.Platform.ToString()
21-
},
22-
Platform = new Platform
23-
{
24-
Version = Environment.Version.ToString(),
25-
Arch = RuntimeInformation.ProcessArchitecture.ToString()
26-
},
27-
IpAddress = IPHelper.Server,
28-
Library = "firewall-dotnet",
29-
Version = typeof(AgentInfoHelper).Assembly.GetName().Version.ToString(),
30-
// Determine if running in a serverless environment
31-
Serverless = Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") != null || Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID") != null
32-
};
9+
/// <summary>
10+
/// Helper class for retrieving agent information about the current runtime environment.
11+
/// </summary>
12+
internal class AgentInfoHelper
13+
{
14+
// Cache the AgentInfo object to avoid repeated creation
15+
private static readonly AgentInfo _cachedAgentInfo = new AgentInfo
16+
{
17+
Hostname = Environment.MachineName,
18+
Os = new Os
19+
{
20+
Version = Environment.OSVersion.VersionString,
21+
Name = Environment.OSVersion.Platform.ToString()
22+
},
23+
Platform = new Platform
24+
{
25+
Version = Environment.Version.ToString(),
26+
Arch = RuntimeInformation.ProcessArchitecture.ToString()
27+
},
28+
IpAddress = IPHelper.Server,
29+
Library = "firewall-dotnet",
30+
Version = CleanVersion(typeof(AgentInfoHelper).Assembly.GetName().Version.ToString()),
31+
// Determine if running in a serverless environment
32+
Serverless = Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") != null || Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID") != null
33+
};
3334

34-
/// <summary>
35-
/// Gets information about the current runtime environment including OS, platform, and configuration details.
36-
/// </summary>
37-
/// <returns>An AgentInfo object containing the environment information.</returns>
38-
public static AgentInfo GetInfo()
39-
{
40-
// Update only the fields that can change
41-
_cachedAgentInfo.DryMode = EnvironmentHelper.DryMode;
42-
_cachedAgentInfo.Serverless = Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") != null || Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID") != null;
35+
/// <summary>
36+
/// Gets information about the current runtime environment including OS, platform, and configuration details.
37+
/// </summary>
38+
/// <returns>An AgentInfo object containing the environment information.</returns>
39+
public static AgentInfo GetInfo()
40+
{
41+
// Update only the fields that can change
42+
_cachedAgentInfo.DryMode = EnvironmentHelper.DryMode;
43+
_cachedAgentInfo.Serverless = Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") != null || Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID") != null;
4344

44-
return _cachedAgentInfo;
45-
}
46-
}
45+
return _cachedAgentInfo;
46+
}
47+
48+
internal static string CleanVersion(string version)
49+
{
50+
// remove the build number
51+
version = version.Split('+')[0];
52+
// remove 4th version number, not used by nuget
53+
version = string.Join(".", version.Split('.').Take(3));
54+
// remove the prerelease version
55+
version = version.Split('-')[0];
56+
return version;
57+
}
58+
}
4759
}

Aikido.Zen.Test/AgentInfoHelperTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,32 @@ private void SetEnvironmentVariable(string variable, string value)
131131
Environment.SetEnvironmentVariable(variable, value);
132132
}
133133
}
134+
135+
[Test]
136+
public void CleanVersion_ShouldRemoveBuildNumber()
137+
{
138+
var version = "1.2.3+4";
139+
var otherVersion = "1.2.3.0";
140+
var cleanedVersion = AgentInfoHelper.CleanVersion(version);
141+
var cleanedOtherVersion = AgentInfoHelper.CleanVersion(otherVersion);
142+
Assert.That(cleanedVersion, Is.EqualTo("1.2.3"));
143+
Assert.That(cleanedOtherVersion, Is.EqualTo("1.2.3"));
144+
}
145+
146+
[Test]
147+
public void CleanVersion_ShouldRemovePrereleaseVersion()
148+
{
149+
var version = "1.2.3-alpha";
150+
var cleanedVersion = AgentInfoHelper.CleanVersion(version);
151+
Assert.That(cleanedVersion, Is.EqualTo("1.2.3"));
152+
}
153+
154+
[Test]
155+
public void CleanVersion_ShouldRemoveBuildNumberAndPrereleaseVersion()
156+
{
157+
var version = "1.2.3+4-alpha";
158+
var cleanedVersion = AgentInfoHelper.CleanVersion(version);
159+
Assert.That(cleanedVersion, Is.EqualTo("1.2.3"));
160+
}
134161
}
135162
}

0 commit comments

Comments
 (0)