-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: centralize configuration logging (#501)
Signed-off-by: Matt Peterson <[email protected]>
- Loading branch information
1 parent
d391365
commit 07a3ba8
Showing
32 changed files
with
514 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
server/src/main/java/com/hedera/block/server/config/logging/ConfigurationLogging.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.hedera.block.server.config.logging; | ||
|
||
/** | ||
* Use this interface to log configuration data. | ||
*/ | ||
public interface ConfigurationLogging { | ||
/** | ||
* Log the configuration data. | ||
*/ | ||
void log(); | ||
} |
132 changes: 132 additions & 0 deletions
132
server/src/main/java/com/hedera/block/server/config/logging/ConfigurationLoggingImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.hedera.block.server.config.logging; | ||
|
||
import static java.lang.System.Logger.Level.INFO; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.swirlds.common.metrics.config.MetricsConfig; | ||
import com.swirlds.common.metrics.platform.prometheus.PrometheusConfig; | ||
import com.swirlds.config.api.ConfigData; | ||
import com.swirlds.config.api.ConfigProperty; | ||
import com.swirlds.config.api.Configuration; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.RecordComponent; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
import javax.inject.Inject; | ||
|
||
/** | ||
* Use this class to log configuration data. | ||
*/ | ||
public class ConfigurationLoggingImpl implements ConfigurationLogging { | ||
|
||
private final System.Logger LOGGER = System.getLogger(getClass().getName()); | ||
|
||
private final Configuration configuration; | ||
private final Set<Record> loggablePackages = new HashSet<>(); | ||
|
||
/** | ||
* Create a new instance of ConfigurationLoggingImpl. | ||
* | ||
* @param configuration the resolved configuration to log | ||
*/ | ||
@Inject | ||
public ConfigurationLoggingImpl(@NonNull final Configuration configuration) { | ||
this.configuration = requireNonNull(configuration); | ||
|
||
// The configuration properties in these packages | ||
// are out of our control. So allow them to be logged. | ||
loggablePackages.add(configuration.getConfigData(MetricsConfig.class)); | ||
loggablePackages.add(configuration.getConfigData(PrometheusConfig.class)); | ||
} | ||
|
||
/** | ||
* Log the configuration data. | ||
*/ | ||
@Override | ||
public void log() { | ||
|
||
// FINE, FINER and FINEST will be allowed to log the configuration | ||
if (LOGGER.isLoggable(INFO)) { | ||
final Map<String, Object> config = collectConfig(configuration); | ||
|
||
// Header | ||
final String bannerLine = "=".repeat(Math.max(0, calculateMaxLineLength(config))); | ||
|
||
LOGGER.log(INFO, bannerLine); | ||
LOGGER.log(INFO, "Block Node Configuration"); | ||
LOGGER.log(INFO, bannerLine); | ||
|
||
// Log the configuration | ||
for (Map.Entry<String, Object> e : config.entrySet()) { | ||
LOGGER.log(INFO, e.getKey() + "=" + e.getValue()); | ||
} | ||
|
||
// Footer | ||
LOGGER.log(INFO, bannerLine); | ||
} | ||
} | ||
|
||
@NonNull | ||
Map<String, Object> collectConfig(@NonNull final Configuration configuration) { | ||
final Map<String, Object> config = new TreeMap<>(); | ||
|
||
// Iterate over all the configuration data types | ||
for (Class<? extends Record> configType : configuration.getConfigDataTypes()) { | ||
|
||
// Only log record components that are annotated with @ConfigData | ||
final ConfigData configDataAnnotation = configType.getDeclaredAnnotation(ConfigData.class); | ||
if (configDataAnnotation != null) { | ||
|
||
// For each record component, check the field annotations | ||
for (RecordComponent component : configType.getRecordComponents()) { | ||
if (component.isAnnotationPresent(ConfigProperty.class)) { | ||
|
||
final String fieldName = component.getName(); | ||
final Record configRecord = configuration.getConfigData(configType); | ||
try { | ||
final Object value = component.getAccessor().invoke(configRecord); | ||
|
||
// If the field is not annotated as '@Loggable' and it's | ||
// not exempted in loggablePackages then it's a sensitive | ||
// value and needs to be handled differently. | ||
if (component.getAnnotation(Loggable.class) == null | ||
&& !loggablePackages.contains(configuration.getConfigData(configType))) { | ||
|
||
// If the field is blank then log the value as a blank string | ||
// to let an admin know the sensitive value was not injected. | ||
// Otherwise, log the value as '*****' to mask it. | ||
final String maskedValue = (value.toString().isEmpty()) ? "" : "*****"; | ||
config.put(configDataAnnotation.value() + "." + fieldName, maskedValue); | ||
} else { | ||
// Log clear text values which were annotated with | ||
// '@Loggable' or which were explicitly added to | ||
// loggablePackages. | ||
config.put(configDataAnnotation.value() + "." + fieldName, value); | ||
} | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return config; | ||
} | ||
|
||
static int calculateMaxLineLength(@NonNull final Map<String, Object> output) { | ||
int maxLength = 0; | ||
for (Map.Entry<String, Object> e : output.entrySet()) { | ||
|
||
int lineLength = e.getKey().length() + e.getValue().toString().length(); | ||
if (lineLength > maxLength) { | ||
maxLength = lineLength; | ||
} | ||
} | ||
return maxLength; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
server/src/main/java/com/hedera/block/server/config/logging/Loggable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.hedera.block.server.config.logging; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.RECORD_COMPONENT}) | ||
public @interface Loggable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.