Skip to content

Commit 2d8da68

Browse files
authored
fix: obfuscate logged appSettings values (#2110)
* Obfuscate license key when logging values read from app settings * Constant-ify the app settings keys used by the agent
1 parent 9cdbe2f commit 2d8da68

File tree

9 files changed

+60
-39
lines changed

9 files changed

+60
-39
lines changed

src/Agent/NewRelic/Agent/Core/Config/ConfigurationLoader.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NewRelic.Agent.Core.Events;
55
using NewRelic.Agent.Core.Utilities;
66
using NewRelic.Core;
7+
using NewRelic.Agent.Core.Configuration;
78
using NewRelic.Core.Logging;
89
using NewRelic.SystemInterfaces;
910
using System;
@@ -15,7 +16,6 @@
1516
using System.Xml.Serialization;
1617
#if NETSTANDARD2_0
1718
using System.Reflection;
18-
using NewRelic.Agent.Core.Configuration;
1919
#endif
2020

2121
namespace NewRelic.Agent.Core.Config
@@ -139,7 +139,7 @@ public static string GetAgentConfigFileName()
139139
if (fileName != null)
140140
return fileName;
141141

142-
throw new Exception(string.Format("Could not find {0} in NewRelic.ConfigFile path, application root, New Relic home directory, or working directory.", NewRelicConfigFileName));
142+
throw new Exception(string.Format("Could not find {0} in {1} path, application root, New Relic home directory, or working directory.", NewRelicConfigFileName, Constants.AppSettingsConfigFile));
143143
}
144144

145145
private static string TryGetAgentConfigFileFromAppConfig()
@@ -149,13 +149,13 @@ private static string TryGetAgentConfigFileFromAppConfig()
149149

150150
try
151151
{
152-
var fileName = AppSettingsConfigResolveWhenUsed.GetAppSetting("NewRelic.ConfigFile");
152+
var fileName = AppSettingsConfigResolveWhenUsed.GetAppSetting(Constants.AppSettingsConfigFile);
153153
if (!File.Exists(fileName))
154154
{
155155
return null;
156156
}
157157

158-
Log.Info("Configuration file found in path pointed to by NewRelic.ConfigFile appSetting: {0}", fileName);
158+
Log.Info("Configuration file found in path pointed to by {0} appSetting: {1}", Constants.AppSettingsConfigFile, fileName);
159159
return fileName;
160160
}
161161
catch (Exception)
@@ -166,13 +166,13 @@ private static string TryGetAgentConfigFileFromAppConfig()
166166
#else
167167
try
168168
{
169-
var fileName = GetConfigSetting("NewRelic.ConfigFile").Value;
169+
var fileName = GetConfigSetting(Constants.AppSettingsConfigFile).Value;
170170
if (!FileExists(fileName))
171171
{
172172
return null;
173173
}
174174

175-
Log.Info("Configuration file found in path pointed to by NewRelic.ConfigFile appSetting of app/web config: {0}", fileName);
175+
Log.Info("Configuration file found in path pointed to by {0} appSetting of app/web config: {1}", Constants.AppSettingsConfigFile, fileName);
176176
return fileName;
177177
}
178178
catch (Exception)
@@ -674,7 +674,7 @@ public configuration Initialize(string xml, string provenance)
674674

675675
try
676676
{
677-
var enabledProvenance = ConfigurationLoader.GetConfigSetting("NewRelic.AgentEnabled");
677+
var enabledProvenance = ConfigurationLoader.GetConfigSetting(Constants.AppSettingsAgentEnabled);
678678
if (enabledProvenance != null && enabledProvenance.Value != null && bool.Parse(enabledProvenance.Value) == false)
679679
{
680680
agentEnabled = false;
@@ -683,7 +683,7 @@ public configuration Initialize(string xml, string provenance)
683683
}
684684
catch
685685
{
686-
Log.Error("Failed to read NewRelic.AgentEnabled from local config.");
686+
Log.Error($"Failed to read {Constants.AppSettingsAgentEnabled} from local config.");
687687
}
688688

689689
return this;

src/Agent/NewRelic/Agent/Core/Configuration/AppSettingsConfigResolveWhenUsed.cs

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.IO;
77
using Microsoft.Extensions.Configuration;
8+
using NewRelic.Core;
89
using NewRelic.Core.Logging;
910

1011
namespace NewRelic.Agent.Core.Configuration
@@ -79,6 +80,10 @@ public static string GetAppSetting(string key)
7980
}
8081
else
8182
{
83+
if (key.Equals(Constants.AppSettingsLicenseKey))
84+
{
85+
value = Strings.ObfuscateLicenseKey(value);
86+
}
8287
Log.Debug($"Reading value from appsettings.json and appsettings.*.json: '{key}={value}'");
8388
}
8489
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace NewRelic.Agent.Core.Configuration
5+
{
6+
public static class Constants
7+
{
8+
public const string AppSettingsLicenseKey = "NewRelic.LicenseKey";
9+
public const string AppSettingsAgentEnabled = "NewRelic.AgentEnabled";
10+
public const string AppSettingsAppName = "NewRelic.AppName";
11+
public const string AppSettingsLabels = "NewRelic.Labels";
12+
public const string AppSettingsConfigFile = "NewRelic.ConfigFile";
13+
14+
}
15+
}

src/Agent/NewRelic/Agent/Core/Configuration/DefaultConfiguration.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public virtual bool AgentEnabled
198198
{
199199
lock (_lockObj)
200200
{
201-
_agentEnabledAppSettingParsed ??= bool.TryParse(_configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled"),
201+
_agentEnabledAppSettingParsed ??= bool.TryParse(_configurationManagerStatic.GetAppSetting(Constants.AppSettingsAgentEnabled),
202202
out _appSettingAgentEnabled);
203203
}
204204
}
@@ -216,7 +216,7 @@ public virtual string AgentLicenseKey
216216
if (_agentLicenseKey != null)
217217
return _agentLicenseKey;
218218

219-
_agentLicenseKey = _configurationManagerStatic.GetAppSetting("NewRelic.LicenseKey")
219+
_agentLicenseKey = _configurationManagerStatic.GetAppSetting(Constants.AppSettingsLicenseKey)
220220
?? EnvironmentOverrides(_localConfiguration.service.licenseKey, "NEW_RELIC_LICENSE_KEY", "NEWRELIC_LICENSEKEY");
221221

222222
if (_agentLicenseKey != null)
@@ -243,7 +243,7 @@ private IEnumerable<string> GetApplicationNames()
243243
return runtimeAppNames;
244244
}
245245

246-
var appName = _configurationManagerStatic.GetAppSetting("NewRelic.AppName");
246+
var appName = _configurationManagerStatic.GetAppSetting(Constants.AppSettingsAppName);
247247
if (appName != null)
248248
{
249249
Log.Info("Application name from web.config or app.config.");
@@ -1361,7 +1361,7 @@ public virtual string Labels
13611361
{
13621362
if (!_labelsChecked)
13631363
{
1364-
var labels = _configurationManagerStatic.GetAppSetting("NewRelic.Labels");
1364+
var labels = _configurationManagerStatic.GetAppSetting(Constants.AppSettingsLabels);
13651365
if (labels != null)
13661366
{
13671367
Log.Info("Application labels from web.config, app.config, or appsettings.json.");

tests/Agent/UnitTests/CompositeTests/CompositeTestAgent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public CompositeTestAgent(bool shouldAllowThreads, bool includeAsyncLocalStorage
141141
.DoInstead<WaitCallback>(callback => { lock (_queuedCallbacksLockObject) { _queuedCallbacks.Add(callback); } });
142142

143143
var configurationManagerStatic = Mock.Create<IConfigurationManagerStatic>();
144-
Mock.Arrange(() => configurationManagerStatic.GetAppSetting("NewRelic.LicenseKey"))
144+
Mock.Arrange(() => configurationManagerStatic.GetAppSetting(NewRelic.Agent.Core.Configuration.Constants.AppSettingsLicenseKey))
145145
.Returns("Composite test license key");
146146

147147
// Construct services

tests/Agent/UnitTests/Core.UnitTest/Config/ConfigurationLoaderTests.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 New Relic, Inc. All rights reserved.
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

44
#if NETFRAMEWORK
@@ -11,6 +11,7 @@
1111
using System.Text;
1212
using System.Threading.Tasks;
1313
using System.Web;
14+
using NewRelic.Agent.Core.Configuration;
1415
using NUnit.Framework;
1516

1617
namespace NewRelic.Agent.Core.Config
@@ -119,7 +120,7 @@ public void GetAgentConfigFileName_ReturnsConfigFileFromAppConfig()
119120
{
120121
const string expectedFileName = "filenameFromAppConfig";
121122
var testWebConfiguration = ConfigurationManager.OpenExeConfiguration(null);
122-
testWebConfiguration.AppSettings.Settings.Add("NewRelic.ConfigFile", expectedFileName);
123+
testWebConfiguration.AppSettings.Settings.Add(Constants.AppSettingsConfigFile, expectedFileName);
123124

124125
staticMocks.UseAppDomainAppIdFunc(() => "testAppId");
125126
staticMocks.UseAppDomainAppVirtualPathFunc(() => "testVirtualPath");
@@ -138,7 +139,7 @@ public void TryGetAgentConfigFileFromAppConfig_ReturnsNullWhenFileDoesNotExist()
138139
{
139140
const string expectedFileName = "filenameFromAppConfig";
140141
var testWebConfiguration = ConfigurationManager.OpenExeConfiguration(null);
141-
testWebConfiguration.AppSettings.Settings.Add("NewRelic.ConfigFile", expectedFileName);
142+
testWebConfiguration.AppSettings.Settings.Add(Constants.AppSettingsConfigFile, expectedFileName);
142143

143144
staticMocks.UseAppDomainAppIdFunc(() => "testAppId");
144145
staticMocks.UseAppDomainAppVirtualPathFunc(() => "testVirtualPath");
@@ -157,7 +158,7 @@ public void TryGetAgentConfigFileFromAppConfig_ReturnsNullOnException()
157158
{
158159
const string expectedFileName = "filenameFromAppConfig";
159160
var testWebConfiguration = ConfigurationManager.OpenExeConfiguration(null);
160-
testWebConfiguration.AppSettings.Settings.Add("NewRelic.ConfigFile", expectedFileName);
161+
testWebConfiguration.AppSettings.Settings.Add(Constants.AppSettingsConfigFile, expectedFileName);
161162

162163
staticMocks.UseAppDomainAppIdFunc(() => "testAppId");
163164
staticMocks.UseAppDomainAppVirtualPathFunc(() => "testVirtualPath");

0 commit comments

Comments
 (0)