Skip to content

Commit

Permalink
samples
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-git committed Dec 26, 2024
1 parent 5e0c184 commit 10a0655
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,17 @@ protected async Task TestAuthenticatedRequest()
private string GetTestFileName(string testName) => $"{_testName}-{testName}";
}

public class AspNetCore5AutoUserEventsDefaultModeSecurityEnabled : AspNetCore5AutoUserEvents
{
public AspNetCore5AutoUserEventsDefaultModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: base(fixture, outputHelper, true)
{
}
}
public class AspNetCore5AutoUserEventsDefaultModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: AspNetCore5AutoUserEvents(fixture, outputHelper, true);

public class AspNetCore5AutoUserEventsExtendedModeSecurityEnabled : AspNetCore5AutoUserEvents
{
public AspNetCore5AutoUserEventsExtendedModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: base(fixture, outputHelper, true, "extended")
{
}
}
public class AspNetCore5AutoUserEventsExtendedModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: AspNetCore5AutoUserEvents(fixture, outputHelper, true, "extended");

public class AspNetCore5AutoUserEventsIndentModeSecurityEnabled : AspNetCore5AutoUserEvents
{
public AspNetCore5AutoUserEventsIndentModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: base(fixture, outputHelper, true, "ident")
{
}
}
public class AspNetCore5AutoUserEventsIndentModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: AspNetCore5AutoUserEvents(fixture, outputHelper, true, "ident");

public class AspNetCore5AutoUserEventsAnonModeSecurityEnabled : AspNetCore5AutoUserEvents
{
public AspNetCore5AutoUserEventsAnonModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: base(fixture, outputHelper, true, "anon")
{
}
}
public class AspNetCore5AutoUserEventsAnonModeSecurityEnabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: AspNetCore5AutoUserEvents(fixture, outputHelper, true, "anon");

public class AspNetCore5AutoUserEventsExtendedModeSecurityDisabled(AspNetCoreTestFixture fixture, ITestOutputHelper outputHelper)
: AspNetCore5AutoUserEvents(fixture, outputHelper, false, "extended")
Expand Down
7 changes: 7 additions & 0 deletions tracer/test/test-applications/Samples.Shared/SampleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SampleHelpers
private static readonly Type TracerConstantsType = Type.GetType("Datadog.Trace.TracerConstants, Datadog.Trace");
private static readonly Type ProcessHelpersType = Type.GetType("Datadog.Trace.Util.ProcessHelpers, Datadog.Trace");
private static readonly Type UserDetailsType = Type.GetType("Datadog.Trace.UserDetails, Datadog.Trace");
private static readonly Type EventTrackingSdk = Type.GetType("Datadog.Trace.AppSec.EventTrackingSdk, Datadog.Trace");
private static readonly Type SpanExtensionsType = Type.GetType("Datadog.Trace.SpanExtensions, Datadog.Trace");
public static readonly Type IpcClientType = Type.GetType("Datadog.Trace.Ci.Ipc.IpcClient, Datadog.Trace");
private static readonly PropertyInfo GetTracerManagerProperty = TracerType?.GetProperty("TracerManager", BindingFlags.NonPublic | BindingFlags.Instance);
Expand Down Expand Up @@ -51,6 +52,7 @@ public class SampleHelpers
private static readonly MethodInfo GetMetricMethod = SpanType?.GetMethod("GetMetric", BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly MethodInfo RunCommandMethod = ProcessHelpersType?.GetMethod("TestingOnly_RunCommand", BindingFlags.NonPublic | BindingFlags.Static);
private static readonly MethodInfo SetUserIdMethod = UserDetailsType?.GetProperty("Id", BindingFlags.Public | BindingFlags.Instance)?.SetMethod;
private static readonly MethodInfo TrackUserLoginSuccessEventMethod = EventTrackingSdk?.GetMethod("TrackUserLoginSuccessEvent", BindingFlags.Public | BindingFlags.Static, null, [typeof(string), typeof(IDictionary<string,string>)], null);
#if NETCOREAPP
private static readonly MethodInfo SetUserMethod = SpanExtensionsType?.GetMethod("SetUser", BindingFlags.Public | BindingFlags.Static | BindingFlags.DoNotWrapExceptions);
#else
Expand Down Expand Up @@ -398,6 +400,11 @@ public static Task WaitForDiscoveryService()
return result as Task ?? Task.CompletedTask;
}

public static void TrackUserLoginSuccessEvent(string userId, IDictionary<string, string> metadata)
{
TrackUserLoginSuccessEventMethod.Invoke(null, [userId, metadata]);
}

public static void SetUser(string userId)
{
if (SpanProperty is null || SetUserMethod is null || SetUserIdMethod is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public IActionResult SomeAuthenticatedAction()
{
return Content("Authorized content");
}

[HttpGet]
[Authorize]
public IActionResult SomeAuthenticatedActionWithSdk()
{
SampleHelpers.TrackUserLoginSuccessEvent("toto", null);
return Content("Authorized content");
}

[HttpPost]
public IActionResult LogOut()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@
#pragma warning disable ASP0019 // warning ASP0019: Use IHeaderDictionary.Append or the indexer to append or set headers. IDictionary.Add will throw an ArgumentException when attempting to add a duplicate key
namespace Samples.Security.AspNetCore5.Controllers
{
public class HomeController : Controller
public class HomeController(ILogger<HomeController> logger) : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
ViewBag.ProfilerAttached = SampleHelpers.IsProfilerAttached();
Expand Down

0 comments on commit 10a0655

Please sign in to comment.