Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 5f130e8

Browse files
improved display; hiding sensitive information
1 parent 7d90ad0 commit 5f130e8

1 file changed

Lines changed: 42 additions & 19 deletions

File tree

src/SqlStreamStore.Server/SqlStreamStoreServerConfiguration.cs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Reflection;
56
using System.Text;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.Configuration.EnvironmentVariables;
@@ -13,7 +14,8 @@ namespace SqlStreamStore.Server
1314
internal class SqlStreamStoreServerConfiguration
1415
{
1516
private readonly ConfigurationData _configuration;
16-
private Dictionary<string, (string source, string value)> _values;
17+
private readonly IDictionary<string, (string source, string value)> _values;
18+
private readonly string[] _sensitiveKeys;
1719

1820
public bool UseCanonicalUris => _configuration.UseCanonicalUris;
1921
public LogEventLevel LogLevel => _configuration.LogLevel;
@@ -31,6 +33,10 @@ public SqlStreamStoreServerConfiguration(
3133
throw new ArgumentNullException(nameof(args));
3234

3335
_values = new Dictionary<string, (string source, string value)>();
36+
_sensitiveKeys = typeof(ConfigurationData).GetProperties()
37+
.Where(property => property.GetCustomAttributes<SensitiveAttribute>().Any())
38+
.Select(property => property.Name)
39+
.ToArray();
3440

3541
void Log(string logName, IDictionary<string, string> data)
3642
{
@@ -50,28 +56,41 @@ void Log(string logName, IDictionary<string, string> data)
5056

5157
public override string ToString()
5258
{
53-
const string delimiter = " | ";
59+
const string delimiter = " ";
5460

5561
var column0Width = _values.Keys.Count > 0 ? _values.Keys.Max(x => x?.Length ?? 0) : 0;
5662
var column1Width = _values.Values.Count > 0 ? _values.Values.Max(_ => _.value?.Length ?? 0) : 0;
5763
var column2Width = _values.Values.Count > 0 ? _values.Values.Max(_ => _.source?.Length ?? 0) : 0;
5864

59-
StringBuilder Append(StringBuilder builder, string column0, string column1, string column2) =>
60-
builder
61-
.Append((column0 ?? string.Empty).PadRight(column0Width, ' '))
62-
.Append(delimiter)
63-
.Append((column1 ?? string.Empty).PadRight(column1Width, ' '))
64-
.Append(delimiter)
65-
.AppendLine(column2);
66-
67-
return _values.Keys.OrderBy(x => x).Aggregate(
68-
Append(new StringBuilder().AppendLine("SQL Stream Store Configuration:"), "Argument", "Value", "Source")
69-
.AppendLine(new string('-', column0Width + column1Width + column2Width)),
70-
(builder, key) => Append(
71-
builder,
72-
key,
73-
_values[key].value,
74-
_values[key].source)).ToString();
65+
return new[]
66+
{
67+
new[]
68+
{
69+
delimiter,
70+
"Argument",
71+
"Value",
72+
"Source"
73+
},
74+
new[]
75+
{
76+
"─┼─",
77+
new string('─', column0Width),
78+
new string('─', column1Width),
79+
new string('─', column2Width),
80+
}
81+
}
82+
.Concat(_values.Keys.OrderBy(key => key)
83+
.Select(key => new[] {delimiter, key, _values[key].value, _values[key].source}))
84+
.Aggregate(
85+
new StringBuilder().AppendLine("SQL Stream Store Configuration:"),
86+
(builder, values) => builder
87+
.Append((values[1] ?? string.Empty).PadRight(column0Width, ' '))
88+
.Append(values[0])
89+
.Append((_sensitiveKeys.Contains(values[1])
90+
? new string('*', Math.Min(column1Width, 8))
91+
: values[2] ?? string.Empty).PadRight(column1Width, ' '))
92+
.Append(values[0])
93+
.AppendLine(values[3])).ToString();
7594
}
7695

7796
private static string Computerize(string value) =>
@@ -87,7 +106,7 @@ private class ConfigurationData
87106

88107
public bool UseCanonicalUris => _configuration.GetValue<bool>(nameof(UseCanonicalUris));
89108
public LogEventLevel LogLevel => _configuration.GetValue(nameof(LogLevel), LogEventLevel.Information);
90-
public string ConnectionString => _configuration.GetValue<string>(nameof(ConnectionString));
109+
[Sensitive] public string ConnectionString => _configuration.GetValue<string>(nameof(ConnectionString));
91110
public string Schema => _configuration.GetValue<string>(nameof(Schema));
92111
public string Provider => _configuration.GetValue<string>(nameof(Provider));
93112

@@ -252,5 +271,9 @@ where key.StartsWith(_prefix)
252271
_log(nameof(EnvironmentVariablesConfigurationSource), Data);
253272
}
254273
}
274+
275+
private class SensitiveAttribute : Attribute
276+
{
277+
}
255278
}
256279
}

0 commit comments

Comments
 (0)