View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import java.util.regex.Pattern;
19  
20  import org.codelibs.fess.Constants;
21  import org.codelibs.fess.mylasta.direction.FessConfig;
22  
23  /**
24   * This class provides system-related utility methods.
25   * It extends {@link org.codelibs.core.lang.SystemUtil} and adds
26   * methods specific to the Fess application.
27   */
28  public class SystemUtil extends org.codelibs.core.lang.SystemUtil {
29  
30      private static final String DEFAULT_SENSITIVE_PATTERN = ".*password.*|.*secret.*|.*key.*|.*token.*|.*credential.*|.*auth.*|.*private.*";
31  
32      private static volatile Pattern sensitivePattern;
33  
34      /**
35       * Private constructor to prevent instantiation.
36       */
37      private SystemUtil() {
38      }
39  
40      /**
41       * Gets the HTTP address of the search engine.
42       *
43       * @return The search engine HTTP address.
44       */
45      public static String getSearchEngineHttpAddress() {
46          return System.getProperty(Constants.FESS_SEARCH_ENGINE_HTTP_ADDRESS);
47      }
48  
49      /**
50       * Gets the compiled pattern for matching sensitive property/environment variable keys.
51       * The pattern is read from the system property 'app.log.sensitive.property.pattern'.
52       * If not set, a default pattern matching common sensitive key names is used.
53       *
54       * @return The compiled Pattern for sensitive key matching
55       */
56      private static Pattern getSensitivePattern() {
57          if (sensitivePattern == null) {
58              synchronized (SystemUtil.class) {
59                  if (sensitivePattern == null) {
60                      final String patternStr = System.getProperty(FessConfig.APP_LOG_SENSITIVE_PROPERTY_PATTERN, DEFAULT_SENSITIVE_PATTERN);
61                      sensitivePattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
62                  }
63              }
64          }
65          return sensitivePattern;
66      }
67  
68      /**
69       * Masks sensitive values for logging purposes.
70       * Keys matching the pattern defined in 'app.log.sensitive.property.pattern' system property
71       * will have their values replaced with "********".
72       *
73       * @param key The key name to check
74       * @param value The value to potentially mask
75       * @return The masked value if the key matches a sensitive pattern, otherwise the original value
76       */
77      public static String maskSensitiveValue(final String key, final String value) {
78          if (key == null || value == null) {
79              return value;
80          }
81          if (getSensitivePattern().matcher(key).matches()) {
82              return "********";
83          }
84          return value;
85      }
86  }