Skip to content

Commit

Permalink
Prevent an unhandled SecurityException in DatadogLogging (DataDog#613)
Browse files Browse the repository at this point in the history
Move initialization of the WindowsDefaultDirectory variable in DatadogLogging to the constructor to catch possible SecurityExceptions. Also, add DD_MAX_LOGFILE_SIZE and DD_TRACE_LOG_PATH as ConfigurationKeys constants.
  • Loading branch information
zacharycmontoya authored Jan 23, 2020
1 parent a9efb8a commit d8096d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/Datadog.Trace/Configuration/ConfigurationKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static class ConfigurationKeys

/// <summary>
/// Configuration key for the DogStatsd port where the Tracer can send metrics.
/// Default value is 8125/
/// Default value is 8125.
/// </summary>
public const string DogStatsdPort = "DD_DOGSTATSD_PORT";

Expand All @@ -143,6 +143,20 @@ public static class ConfigurationKeys
/// </summary>
public const string TracerMetricsEnabled = "DD_TRACE_METRICS_ENABLED";

/// <summary>
/// Configuration key for setting the approximate maximum size,
/// in bytes, for Tracer log files.
/// Default value is 10 MB.
/// </summary>
public const string MaxLogFileSize = "DD_MAX_LOGFILE_SIZE";

/// <summary>
/// Configuration key for setting the path to the profiler log file.
/// Default value is "%ProgramData%"\Datadog .NET Tracer\logs\dotnet-profiler.log" on Windows
/// or "/var/log/datadog/dotnet-profiler.log" on Linux.
/// </summary>
public const string ProfilerLogPath = "DD_TRACE_LOG_PATH";

/// <summary>
/// String format patterns used to match integration-specific configuration keys.
/// </summary>
Expand Down
13 changes: 9 additions & 4 deletions src/Datadog.Trace/Logging/DatadogLogging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Datadog.Trace.Configuration;
using Datadog.Trace.Vendors.Serilog;
using Datadog.Trace.Vendors.Serilog.Events;
using Datadog.Trace.Vendors.Serilog.Sinks.File;
Expand All @@ -11,8 +12,7 @@ namespace Datadog.Trace.Logging
internal static class DatadogLogging
{
private const string NixDefaultDirectory = "/var/log/datadog/";
private static readonly string WindowsDefaultDirectory =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Datadog .NET Tracer", "logs");
private static readonly string WindowsDefaultDirectory;

private static readonly long? MaxLogFileSize = 10 * 1024 * 1024;
private static readonly LogEventLevel MinimumLogEventLevel = LogEventLevel.Warning;
Expand All @@ -36,23 +36,28 @@ static DatadogLogging()
MinimumLogEventLevel = LogEventLevel.Verbose;
}

var maxLogSizeVar = Environment.GetEnvironmentVariable("DD_MAX_LOGFILE_SIZE");
var maxLogSizeVar = Environment.GetEnvironmentVariable(ConfigurationKeys.MaxLogFileSize);
if (long.TryParse(maxLogSizeVar, out var maxLogSize))
{
// No verbose or debug logs
MaxLogFileSize = maxLogSize;
}

var nativeLogFile = Environment.GetEnvironmentVariable("DD_TRACE_LOG_PATH");
var nativeLogFile = Environment.GetEnvironmentVariable(ConfigurationKeys.ProfilerLogPath);
string logDirectory = null;

if (!string.IsNullOrEmpty(nativeLogFile))
{
logDirectory = Path.GetDirectoryName(nativeLogFile);
}

// This entire block may throw a SecurityException if not granted the System.Security.Permissions.FileIOPermission because of the following API calls
// - Directory.Exists
// - Environment.GetFolderPath
// - Path.GetTempPath
if (logDirectory == null)
{
WindowsDefaultDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Datadog .NET Tracer", "logs");
if (Directory.Exists(WindowsDefaultDirectory))
{
logDirectory = WindowsDefaultDirectory;
Expand Down

0 comments on commit d8096d1

Please sign in to comment.