-
Notifications
You must be signed in to change notification settings - Fork 689
feat(config): show non-default values on startup #11523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a80c95d
ae4df88
e930e1a
05d5210
4ff9cc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,10 +2,14 @@ | |||||||||
| // SPDX-License-Identifier: LGPL-3.0-only | ||||||||||
|
|
||||||||||
| using System; | ||||||||||
| using System.Collections; | ||||||||||
| using System.Collections.Concurrent; | ||||||||||
| using System.Collections.Generic; | ||||||||||
| using System.ComponentModel; | ||||||||||
| using System.Linq; | ||||||||||
| using System.Reflection; | ||||||||||
| using Nethermind.Core; | ||||||||||
| using Nethermind.Core.Extensions; | ||||||||||
|
|
||||||||||
| namespace Nethermind.Config; | ||||||||||
|
|
||||||||||
|
|
@@ -41,4 +45,71 @@ public static T GetDefaultValue<T>(this IConfig config, string propertyName) | |||||||||
| string? defaultValue = attribute.DefaultValue; | ||||||||||
| return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(defaultValue!)!; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// <summary> | ||||||||||
| /// Enumerates configuration properties whose current value differs from the | ||||||||||
| /// implementation's default. Useful for surfacing on startup which knobs the | ||||||||||
| /// operator has actually changed, rather than dumping every value. | ||||||||||
| /// </summary> | ||||||||||
| /// <remarks> | ||||||||||
| /// Defaults come from a freshly constructed instance of the implementing | ||||||||||
| /// type, so initializers and constructors are honoured exactly as production | ||||||||||
| /// wiring would do (no parsing of the <see cref="ConfigItemAttribute.DefaultValue"/> string). | ||||||||||
| /// Properties flagged <see cref="ConfigItemAttribute.IsSensitive"/> are skipped. | ||||||||||
| /// </remarks> | ||||||||||
| /// <param name="configProvider">The provider to query.</param> | ||||||||||
| /// <param name="onConfigError"> | ||||||||||
| /// Optional callback invoked when a single config interface cannot be enumerated | ||||||||||
| /// (provider lookup or fresh-default construction throws). Enumeration of the | ||||||||||
| /// remaining interfaces continues regardless. If <c>null</c>, failures are silent. | ||||||||||
| /// </param> | ||||||||||
| public static IEnumerable<(string Category, string Name, object? CurrentValue, object? DefaultValue)> | ||||||||||
| GetNonDefaultValues(this IConfigProvider configProvider, Action<Type, Exception>? onConfigError = null) | ||||||||||
| { | ||||||||||
| ArgumentNullException.ThrowIfNull(configProvider); | ||||||||||
|
|
||||||||||
| foreach (Type configInterface in TypeDiscovery.FindNethermindBasedTypes(typeof(IConfig))) | ||||||||||
| { | ||||||||||
| if (!configInterface.IsInterface) continue; | ||||||||||
|
|
||||||||||
| IConfig current; | ||||||||||
| IConfig fresh; | ||||||||||
| string category; | ||||||||||
| try | ||||||||||
| { | ||||||||||
| current = configProvider.GetConfig(configInterface); | ||||||||||
| fresh = (IConfig)Activator.CreateInstance(current.GetType())!; | ||||||||||
| category = GetCategoryName(configInterface) ?? string.Empty; | ||||||||||
| } | ||||||||||
| catch (Exception e) | ||||||||||
| { | ||||||||||
| onConfigError?.Invoke(configInterface, e); | ||||||||||
| continue; | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [High] Sensitive data exposure Properties like Consider adding an
Suggested change
Or at a minimum filter out properties where the attribute is not present or |
||||||||||
| foreach (PropertyInfo property in configInterface.GetProperties()) | ||||||||||
| { | ||||||||||
| if (!property.CanRead) continue; | ||||||||||
| if (property.GetCustomAttribute<ConfigItemAttribute>()?.IsSensitive == true) continue; | ||||||||||
|
|
||||||||||
| object? actual; | ||||||||||
| object? defaultValue; | ||||||||||
| try | ||||||||||
| { | ||||||||||
| actual = property.GetValue(current); | ||||||||||
| defaultValue = property.GetValue(fresh); | ||||||||||
| } | ||||||||||
| catch (Exception e) | ||||||||||
| { | ||||||||||
| onConfigError?.Invoke(configInterface, e); | ||||||||||
| continue; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (!StructuralComparisons.StructuralEqualityComparer.Equals(actual, defaultValue)) | ||||||||||
| { | ||||||||||
| yield return (category, property.Name, actual, defaultValue); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Medium] Incomplete exception handling
Activator.CreateInstancecan also throwTargetInvocationException(if the constructor body throws),TypeLoadException, andMethodAccessException. Any of these would propagate and crash startup for what is a diagnostic-only feature. Broaden the catch: