Skip to content

Commit 342bee9

Browse files
committed
refactoring
1 parent 5e7f270 commit 342bee9

File tree

8 files changed

+243
-23
lines changed

8 files changed

+243
-23
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ var bombardierTestsGenerator = new BombardierTestsGenerator(options =>
3232
var bombardierTests = await bombardierTestsGenerator.Generate(requests);
3333

3434
//Run Bombardier Tests
35-
var bombardierTestsRunner = new BombardierTestsRunner(bombardierTests.ToList());
35+
var bombardierTestsRunner = new BombardierTestsRunner(bombardierTests.ToList(),
36+
options =>
37+
{
38+
options.ObfuscateAuthenticationHeader = false;
39+
});
3640
var bombardierResults = await bombardierTestsRunner.Run();
3741
```
3842

@@ -92,7 +96,7 @@ TO-DO
9296

9397
## TO-DO
9498

95-
- results obfuscation (authentication headers)
99+
N/A
96100

97101
## License
98102

src/QAToolKit.Engine.Bombardier/BombardierOptions.cs renamed to src/QAToolKit.Engine.Bombardier/BombardierGeneratorOptions.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,50 @@
33

44
namespace QAToolKit.Engine.Bombardier
55
{
6-
public class BombardierOptions
6+
/// <summary>
7+
/// Bombardier generator options
8+
/// </summary>
9+
public class BombardierGeneratorOptions
710
{
11+
/// <summary>
12+
/// A list of Oauth2 access tokens
13+
/// </summary>
814
internal Dictionary<AuthenticationType, string> AccessTokens { get; private set; } = new Dictionary<AuthenticationType, string>();
15+
/// <summary>
16+
/// Custom ApiKey sent as a ApiKey HTTP Header
17+
/// </summary>
918
internal string ApiKey { get; private set; }
19+
/// <summary>
20+
/// Basic Authentication user name
21+
/// </summary>
1022
internal string UserName { get; private set; }
23+
/// <summary>
24+
/// Basic Authentication password
25+
/// </summary>
1126
internal string Password { get; private set; }
27+
/// <summary>
28+
/// Bombardier concurrent users options
29+
/// </summary>
1230
public int BombardierConcurrentUsers { get; set; }
31+
/// <summary>
32+
/// Bombardier request timeout
33+
/// </summary>
1334
public int BombardierTimeout { get; set; }
35+
/// <summary>
36+
/// Bombardier test runner duration
37+
/// </summary>
1438
public int BombardierDuration { get; set; }
39+
/// <summary>
40+
/// Bombardier rate limiting per second
41+
/// </summary>
1542
public int BombardierRateLimit { get; set; }
43+
/// <summary>
44+
/// Bombardier use HTTP2 protocol
45+
/// </summary>
1646
public bool BombardierUseHttp2 { get; set; }
47+
/// <summary>
48+
/// What is the type of the test
49+
/// </summary>
1750
internal TestType TestType { get; } = TestType.LoadTest;
1851

1952
/// <summary>
@@ -22,7 +55,7 @@ public class BombardierOptions
2255
/// <param name="token"></param>
2356
/// <param name="authenticationType"></param>
2457
/// <returns></returns>
25-
public BombardierOptions AddOAuth2Token(string token, AuthenticationType authenticationType)
58+
public BombardierGeneratorOptions AddOAuth2Token(string token, AuthenticationType authenticationType)
2659
{
2760
AccessTokens.Add(authenticationType, token);
2861
return this;
@@ -33,7 +66,7 @@ public BombardierOptions AddOAuth2Token(string token, AuthenticationType authent
3366
/// </summary>
3467
/// <param name="apiKey"></param>
3568
/// <returns></returns>
36-
public BombardierOptions AddApiKey(string apiKey)
69+
public BombardierGeneratorOptions AddApiKey(string apiKey)
3770
{
3871
ApiKey = apiKey;
3972
return this;
@@ -45,7 +78,7 @@ public BombardierOptions AddApiKey(string apiKey)
4578
/// <param name="userName"></param>
4679
/// <param name="password"></param>
4780
/// <returns></returns>
48-
public BombardierOptions AddBasicAuthentication(string userName, string password)
81+
public BombardierGeneratorOptions AddBasicAuthentication(string userName, string password)
4982
{
5083
UserName = userName;
5184
Password = password;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace QAToolKit.Engine.Bombardier
2+
{
3+
/// <summary>
4+
/// Bombardier output options
5+
/// </summary>
6+
public class BombardierOutputOptions
7+
{
8+
/// <summary>
9+
/// Obfuscate authentication header in the results output
10+
/// </summary>
11+
public bool ObfuscateAuthenticationHeader { get; set; } = true;
12+
}
13+
}

src/QAToolKit.Engine.Bombardier/BombardierTestsGenerator.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace QAToolKit.Engine.Bombardier
1313
{
1414
public class BombardierTestsGenerator : IGenerator<IList<HttpTestRequest>, IEnumerable<BombardierTest>>
1515
{
16-
private readonly BombardierOptions _bombardierOptions;
16+
private readonly BombardierGeneratorOptions _bombardierGeneratorOptions;
1717

18-
public BombardierTestsGenerator(Action<BombardierOptions> options)
18+
public BombardierTestsGenerator(Action<BombardierGeneratorOptions> options)
1919
{
20-
_bombardierOptions = new BombardierOptions();
21-
options?.Invoke(_bombardierOptions);
20+
_bombardierGeneratorOptions = new BombardierGeneratorOptions();
21+
options?.Invoke(_bombardierGeneratorOptions);
2222
}
2323

2424
/// <summary>
@@ -41,20 +41,20 @@ public async Task<IEnumerable<BombardierTest>> Generate(IList<HttpTestRequest> r
4141
bombardierFullPath = Path.Combine("./bombardier", "linux", "bombardier");
4242
}
4343

44-
foreach (var request in restRequests.Where(request => request.TestTypes.Contains(_bombardierOptions.TestType)))
44+
foreach (var request in restRequests.Where(request => request.TestTypes.Contains(_bombardierGeneratorOptions.TestType)))
4545
{
46-
string authHeader = GeneratorHelper.GenerateAuthHeader(request, _bombardierOptions);
46+
string authHeader = GeneratorHelper.GenerateAuthHeader(request, _bombardierGeneratorOptions);
4747

4848
scriptBuilder.AppendLine($"{bombardierFullPath} " +
4949
$"-m {request.Method.ToString().ToUpper()} {GeneratorHelper.GenerateUrlParameters(request)} " +
50-
$"-c {_bombardierOptions.BombardierConcurrentUsers} " +
50+
$"-c {_bombardierGeneratorOptions.BombardierConcurrentUsers} " +
5151
$"{authHeader}" +
5252
$"{GeneratorHelper.GenerateContentTypeHeader(request)}" +
5353
$"{GeneratorHelper.GenerateJsonBody(request)}" +
54-
$"--{(Convert.ToBoolean(_bombardierOptions.BombardierUseHttp2) ? "http2" : "http1")} " +
55-
$"--timeout={_bombardierOptions.BombardierTimeout}s " +
56-
$"--duration={_bombardierOptions.BombardierDuration}s " +
57-
$"{GeneratorHelper.GenerateRateLimit(_bombardierOptions.BombardierRateLimit)}");
54+
$"--{(Convert.ToBoolean(_bombardierGeneratorOptions.BombardierUseHttp2) ? "http2" : "http1")} " +
55+
$"--timeout={_bombardierGeneratorOptions.BombardierTimeout}s " +
56+
$"--duration={_bombardierGeneratorOptions.BombardierDuration}s " +
57+
$"{GeneratorHelper.GenerateRateLimit(_bombardierGeneratorOptions.BombardierRateLimit)}");
5858

5959
bombardierTests.Add(new BombardierTest()
6060
{

src/QAToolKit.Engine.Bombardier/BombardierTestsRunner.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@ namespace QAToolKit.Engine.Bombardier
1313
public class BombardierTestsRunner
1414
{
1515
private readonly IList<BombardierTest> _bombardierTests;
16+
private readonly BombardierOutputOptions _bombardierParserOptions;
1617

17-
public BombardierTestsRunner(IList<BombardierTest> bombardierTests)
18+
public BombardierTestsRunner(IList<BombardierTest> bombardierTests, Action<BombardierOutputOptions> options)
1819
{
1920
_bombardierTests = bombardierTests;
21+
_bombardierParserOptions = new BombardierOutputOptions();
22+
23+
if (options == null)
24+
{
25+
_bombardierParserOptions.ObfuscateAuthenticationHeader = true;
26+
}
27+
else
28+
{
29+
options?.Invoke(_bombardierParserOptions);
30+
}
2031
}
2132

2233
/// <summary>
@@ -104,7 +115,7 @@ private BombardierResult ParseOutput(StringBuilder sb, string command, DateTime
104115

105116
var str = sb.ToString();
106117

107-
results.Command = command;
118+
results.Command = ObfuscateAuthenticationHeader(command);
108119
results.Counter1xx = Convert.ToInt32(StringHelper.RemoveAllNonNumericChars(StringHelper.Between(str, "1xx - ", ",")));
109120
results.Counter2xx = Convert.ToInt32(StringHelper.RemoveAllNonNumericChars(StringHelper.Between(str, "2xx - ", ",")));
110121
results.Counter3xx = Convert.ToInt32(StringHelper.RemoveAllNonNumericChars(StringHelper.Between(str, "3xx - ", ",")));
@@ -157,5 +168,17 @@ private decimal GetLatencyMiliseconds(string latency)
157168

158169
return digit;
159170
}
171+
172+
private string ObfuscateAuthenticationHeader(string command)
173+
{
174+
if (_bombardierParserOptions.ObfuscateAuthenticationHeader)
175+
{
176+
var result = StringHelper.ObfuscateStringBetween(command, "-H \"ApiKey:", "\"", " *** hidden *** ");
177+
result = StringHelper.ObfuscateStringBetween(result, "-H \"Authorization:", "\"", " *** hidden *** ");
178+
return result;
179+
}
180+
181+
return command;
182+
}
160183
}
161184
}

src/QAToolKit.Engine.Bombardier/Helpers/GeneratorHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ internal static string GenerateJsonBody(HttpTestRequest request)
101101
/// <param name="administratorAccessToken"></param>
102102
/// <param name="apiKey"></param>
103103
/// <returns></returns>
104-
internal static string GenerateAuthHeader(HttpTestRequest request, BombardierOptions bombardierOptions)
104+
internal static string GenerateAuthHeader(HttpTestRequest request, BombardierGeneratorOptions bombardierOptions)
105105
{
106106
//Check if Swagger operation description contains certain auth tags
107107
string authHeader;
@@ -152,7 +152,7 @@ private static string GetOauth2AuthenticationHeader(Dictionary<AuthenticationTyp
152152
return authHeader;
153153
}
154154

155-
private static string GetBasicAuthenticationHeader(BombardierOptions bombardierOptions)
155+
private static string GetBasicAuthenticationHeader(BombardierGeneratorOptions bombardierOptions)
156156
{
157157
string authHeader;
158158

@@ -170,7 +170,7 @@ private static string GetBasicAuthenticationHeader(BombardierOptions bombardierO
170170
return authHeader;
171171
}
172172

173-
private static string GetApiKeyAuthenticationHeader(BombardierOptions bombardierOptions)
173+
private static string GetApiKeyAuthenticationHeader(BombardierGeneratorOptions bombardierOptions)
174174
{
175175
string authHeader;
176176

src/QAToolKit.Engine.Bombardier/QAToolKit.Engine.Bombardier.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
66
<!-- NuGet Metadata -->
77
<IsPackable>true</IsPackable>

src/QAToolKit.Engine.Bombardier/QAToolKit.Engine.Bombardier.xml

Lines changed: 147 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)