All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.icellmobilsoft.coffee.tool.utils.string.StringHelper Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*-
 * #%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:
     * 
    *
  1. System properties
  2. *
  3. Environment properties
  4. *
  5. /META-INF/microprofile-config.properties
  6. *
* * 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: *
    *
  1. System properties
  2. *
  3. Environment properties
  4. *
  5. /META-INF/microprofile-config.properties
  6. *
* * 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
textresult
{@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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy