-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathConfig.cs
More file actions
199 lines (159 loc) · 8.24 KB
/
Copy pathConfig.cs
File metadata and controls
199 lines (159 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Linq;
using StackifyLib.Utils;
namespace StackifyLib
{
/// <summary>
/// Encapsulate settings retrieval mechanism. Currently supports config file and environment variables.
/// Could be expanded to include other type of configuration servers later.
/// </summary>
public class Config
{
#if NETCORE || NETCOREX
private static Microsoft.Extensions.Configuration.IConfigurationRoot _configuration = null;
public static void SetConfiguration(Microsoft.Extensions.Configuration.IConfigurationRoot configuration)
{
_configuration = configuration;
}
#endif
static Config()
{
LoadSettings();
}
public static void LoadSettings()
{
try
{
CaptureErrorPostdata = Get("Stackify.CaptureErrorPostdata", bool.FalseString).Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
CaptureServerVariables = Get("Stackify.CaptureServerVariables", bool.FalseString).Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
CaptureSessionVariables = Get("Stackify.CaptureSessionVariables", bool.FalseString).Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
CaptureErrorHeaders = Get("Stackify.CaptureErrorHeaders", bool.TrueString).Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
CaptureErrorCookies = Get("Stackify.CaptureErrorCookies", bool.FalseString).Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
ApiKey = Get("Stackify.ApiKey", ApiKey ?? string.Empty);
AppName = Get("Stackify.AppName", AppName ?? string.Empty);
Environment = Get("Stackify.Environment", Environment ?? string.Empty);
CaptureErrorHeadersWhitelist = Get("Stackify.CaptureErrorHeadersWhitelist", string.Empty);
if (string.IsNullOrEmpty(CaptureErrorHeadersWhitelist) == false)
{
ErrorHeaderGoodKeys = CaptureErrorHeadersWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
}
CaptureErrorHeadersBlacklist = Get("Stackify.CaptureErrorHeadersBlacklist", string.Empty);
if (string.IsNullOrEmpty(CaptureErrorHeadersBlacklist) == false)
{
ErrorHeaderBadKeys = CaptureErrorHeadersBlacklist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
}
CaptureErrorCookiesWhitelist = Get("Stackify.CaptureErrorCookiesWhitelist", string.Empty);
if (string.IsNullOrEmpty(CaptureErrorCookiesWhitelist) == false)
{
ErrorCookiesGoodKeys = CaptureErrorCookiesWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
}
CaptureErrorCookiesBlacklist = Get("Stackify.CaptureErrorCookiesBlacklist", string.Empty);
if (string.IsNullOrEmpty(CaptureErrorCookiesBlacklist) == false)
{
ErrorCookiesBadKeys = CaptureErrorCookiesBlacklist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
}
CaptureErrorSessionWhitelist = Get("Stackify.CaptureErrorSessionWhitelist", string.Empty);
if (string.IsNullOrEmpty(CaptureErrorSessionWhitelist) == false)
{
ErrorSessionGoodKeys = CaptureErrorSessionWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
}
// SF-6804: Frequent Calls to GetEC2InstanceId
var captureEc2InstanceMetadataUpdateThresholdMinutes = Get("Stackify.Ec2InstanceMetadataUpdateThresholdMinutes", string.Empty);
if (string.IsNullOrWhiteSpace(captureEc2InstanceMetadataUpdateThresholdMinutes) == false)
{
if (int.TryParse(captureEc2InstanceMetadataUpdateThresholdMinutes, out int minutes) && minutes != 0)
{
Ec2InstanceMetadataUpdateThresholdMinutes = minutes;
}
}
// SF-6204: Allow local overrides of EC2 detection
var isEc2 = Get("Stackify.IsEC2", string.Empty);
if (string.IsNullOrWhiteSpace(isEc2) == false)
{
IsEc2 = isEc2.Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
}
// RT-297
var apiLog = Get("Stackify.ApiLog", string.Empty);
if (string.IsNullOrWhiteSpace(apiLog) == false)
{
ApiLog = apiLog.Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#Config #LoadSettings failed", ex);
}
}
public static string ApiKey { get; set; }
public static string Environment { get; set; }
public static string AppName { get; set; }
public static List<string> ErrorHeaderGoodKeys = new List<string>();
public static List<string> ErrorHeaderBadKeys = new List<string>();
public static List<string> ErrorCookiesGoodKeys = new List<string>();
public static List<string> ErrorCookiesBadKeys = new List<string>();
public static List<string> ErrorSessionGoodKeys = new List<string>();
public static bool CaptureSessionVariables { get; set; }
public static bool CaptureServerVariables { get; set; }
public static bool CaptureErrorPostdata { get; set; }
public static bool CaptureErrorHeaders { get; set; } = true;
public static bool CaptureErrorCookies { get; set; }
public static string CaptureErrorSessionWhitelist { get; set; }
public static string CaptureErrorHeadersWhitelist { get; set; }
public static string CaptureErrorHeadersBlacklist { get; set; } = "cookie,authorization";
public static string CaptureErrorCookiesWhitelist { get; set; }
public static string CaptureErrorCookiesBlacklist { get; set; } = ".ASPXAUTH";
public static int Ec2InstanceMetadataUpdateThresholdMinutes { get; set; } = 60;
public static bool? IsEc2 { get; set; }
public static bool? ApiLog { get; set; }
/// <summary>
/// Attempts to fetch a setting value given the key.
/// .NET configuration file will be used first, if the key is not found, environment variable will be used next.
/// </summary>
/// <param name="key">configuration key in config file or environment variable name.</param>
/// <param name="defaultValue">If nothing is found, return optional defaultValue provided.</param>
/// <returns>string value for the requested setting key.</returns>
internal static string Get(string key, string defaultValue = null)
{
string v = null;
try
{
if (string.IsNullOrWhiteSpace(key) == false)
{
#if NETCORE || NETCOREX
if (_configuration != null)
{
var appSettings = _configuration.GetSection("Stackify");
v = appSettings[key.Replace("Stackify.", string.Empty)];
}
#endif
#if NETCOREX
if (_configuration != null)
{
var appSettings = _configuration.GetSection("Stackify");
v = appSettings[key.Replace("Stackify.", string.Empty)];
}
#endif
#if NETFULL
if (string.IsNullOrEmpty(v))
{
v = System.Configuration.ConfigurationManager.AppSettings[key];
}
#endif
if (string.IsNullOrEmpty(v))
{
v = System.Environment.GetEnvironmentVariable(key);
}
}
}
finally
{
if (v == null)
{
v = defaultValue;
}
}
return v;
}
}
}