hu.icellmobilsoft.coffee.tool.utils.string.StringHelper Maven / Gradle / Ivy
/*-
* #%L
* Coffee
* %%
* Copyright (C) 2020 i-Cell Mobilsoft Zrt.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package hu.icellmobilsoft.coffee.tool.utils.string;
import java.util.Optional;
import jakarta.enterprise.context.Dependent;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import hu.icellmobilsoft.coffee.cdi.config.IConfigKey;
/**
* Helper class for String utility functions with 3rd party dependencies (ie. microprofile-config)
*
* @author mark.petrenyi
* @since 1.0.0
*/
@Dependent
public class StringHelper {
/** Constant DEFAULT_PATTERN=".*?(pass|secret).*?"
*/
public static final String[] DEFAULT_PATTERN = { "[\\w\\s]*?secret[\\w\\s]*?", "[\\w\\s]*?pass[\\w\\s]*?" };
// PM: Only the default config sources can be pulled in, as this class is used in the etcd config source
// (hence the @org.eclipse.microprofile.config.inject.ConfigProperty annotation is not appropriate).
private static Config config = ConfigProviderResolver.instance().getBuilder().addDefaultSources().build();
/**
* Default constructor, constructs a new object.
*/
public StringHelper() {
super();
}
/**
* Masks value, if key ignore-case matches a pattern.
*
* The pattern can be set with {@value IConfigKey#LOG_SENSITIVE_KEY_PATTERN} using microprofile-config's default config sources:
*
* - System properties
* - Environment properties
* - /META-INF/microprofile-config.properties
*
*
* If pattern is not set via config it dafaults to {@link #DEFAULT_PATTERN}
*
* @param key
* The key to check against keyPattern.
* @param value
* The value to mask
* @return "*" if key and pattern are not blank and key matches pattern (case ignored); value otherwise
*/
public static String maskPropertyValue(String key, Object value) {
return StringUtil.maskPropertyValue(key, value, getSensitiveKeyPattern());
}
/**
* Masks values belonging to properties ignore-case matching a defined keyPattern in XML or JSON texts.
* The pattern can be set with {@value IConfigKey#LOG_SENSITIVE_KEY_PATTERN} using microprofile-config's default config sources:
*
* - System properties
* - Environment properties
* - /META-INF/microprofile-config.properties
*
*
* If pattern is not set via config it defaults to {@link #DEFAULT_PATTERN}
* ie witch default pattern:
* keypattern = {@code .*?(pass|secret).*?}
*
*
* Example input-output pairs
*
* text
* result
*
*
* {@code abc }
* {@code * }
*
*
* {@code abc }
* {@code * }
*
*
* {@code abc }
* {@code abc }
*
*
* "pass":"abc"
* "pass":"*"
*
*
* "userPassword":"abc"
* "userPassword":"*"
*
*
* "userName":"abc"
* "userName":"abc"
*
*
*
*
* @param text
* XML or JSON text to replace sensitive data
* @return masked text
*/
public static String maskValueInXmlJson(String text) {
// Default config sources (sys, env, microprofile-config.properties)
return StringUtil.maskValueInXmlJson(text, getSensitiveKeyPattern());
}
/**
*
* getSensitiveKeyPattern.
*
*
* @return value of config key {@value IConfigKey#LOG_SENSITIVE_KEY_PATTERN} if set, {@link #DEFAULT_PATTERN} otherwise.
*/
public static String[] getSensitiveKeyPattern() {
// Default config sources (sys, env, microprofile-config.properties)
Optional patternOpt = config.getOptionalValue(IConfigKey.LOG_SENSITIVE_KEY_PATTERN, String[].class);
return patternOpt.filter(patternArray -> patternArray.length > 0).orElse(DEFAULT_PATTERN);
}
}