Skip to content

Commit 496d420

Browse files
[ASM] Refactor hardcoded secret analyzer (#5883)
## Summary of changes The Hardcoded analyzer has been refactored. These are the main changes: - Replaced the manual sleep and polling mechanism with an asynchronous delay using `Task.Delay`. - Added `ConfigureAwait(false)` to avoid capturing the synchronization context, which can prevent potential deadlocks and improve performance. - Introduced a `CancellationTokenSource` for managing the lifecycle of the polling operation. - Removed the `_started` flag as the `CancellationToken` now controls the start/stop logic. - Ensured proper disposal of resources by calling `_cancellationTokenSource.Cancel()` in the `Dispose` method. ## Reason for change This change was requested. ## Implementation details ## Test coverage ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. -->
1 parent 05db88a commit 496d420

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

tracer/src/Datadog.Trace/Iast/Analyzers/HardcodedSecretsAnalyzer.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,26 @@ internal class HardcodedSecretsAnalyzer : IDisposable
2323
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<HardcodedSecretsAnalyzer>();
2424
private static HardcodedSecretsAnalyzer? _instance = null;
2525

26-
private readonly ManualResetEventSlim _waitEvent = new(false);
26+
private readonly CancellationTokenSource _cancellationTokenSource = new();
2727
private readonly TimeSpan _regexTimeout;
28-
private bool _started = false;
2928
private List<SecretRegex>? _secretRules = null;
3029

3130
// Internal for testing
3231
internal HardcodedSecretsAnalyzer(TimeSpan regexTimeout)
3332
{
3433
Log.Debug("HardcodedSecretsAnalyzer -> Init");
3534
_regexTimeout = regexTimeout;
36-
_started = true;
37-
Task.Run(() => PoolingThread())
38-
.ContinueWith(t => Log.Error(t.Exception, "Error in Hardcoded secret analyzer"), TaskContinuationOptions.OnlyOnFaulted);
35+
Task.Run(() => PollingThread(_cancellationTokenSource.Token))
36+
.ContinueWith(t => Log.Error(t.Exception, "Error in Hardcoded secret analyzer"), TaskContinuationOptions.OnlyOnFaulted);
3937
}
4038

41-
private void PoolingThread()
39+
private async Task PollingThread(CancellationToken cancellationToken)
4240
{
4341
try
4442
{
4543
Log.Debug("HardcodedSecretsAnalyzer polling thread -> Started");
4644
var userStrings = new UserStringInterop[UserStringsArraySize];
47-
while (_started)
45+
while (!cancellationToken.IsCancellationRequested)
4846
{
4947
if (Tracer.Instance.Settings.IsIntegrationEnabled(IntegrationId.HardcodedSecret))
5048
{
@@ -81,7 +79,7 @@ private void PoolingThread()
8179
IntegrationId.HardcodedSecret));
8280
}
8381
}
84-
catch (Exception err)
82+
catch (Exception err) when (!(err is OperationCanceledException))
8583
{
8684
Log.Warning(err, "Exception in HardcodedSecretsAnalyzer polling thread loop.");
8785
}
@@ -94,12 +92,11 @@ private void PoolingThread()
9492
}
9593
}
9694

97-
_waitEvent.Wait(2_000);
95+
await Task.Delay(2_000, cancellationToken).ConfigureAwait(false);
9896
}
9997
}
100-
catch (Exception err)
98+
catch (Exception err) when (!(err is OperationCanceledException))
10199
{
102-
_started = false;
103100
Log.Warning(err, "Exception in HardcodedSecretsAnalyzer polling thread. Disabling feature.");
104101
}
105102

@@ -215,12 +212,8 @@ private static List<SecretRegex> GenerateSecretRules(TimeSpan timeout)
215212

216213
public void Dispose()
217214
{
218-
try
219-
{
220-
_started = false;
221-
_waitEvent.Set();
222-
}
223-
catch { }
215+
_cancellationTokenSource.Cancel();
216+
Log.Debug("HardcodedSecretsAnalyzer -> Disposed");
224217
}
225218

226219
private readonly struct SecretRegex

0 commit comments

Comments
 (0)