001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.Locale;
023import java.util.Set;
024
025public final class SensitiveUtils {
026
027    private static final Set<String> SENSITIVE_KEYS = Collections.unmodifiableSet(new HashSet<>(
028            Arrays.asList(
029                    // Generated by camel build tools - do NOT edit this list!
030                    // SENSITIVE-KEYS: START
031                    "accesskey",
032                    "accesstoken",
033                    "accesstokensecret",
034                    "accountkey",
035                    "accountsid",
036                    "acltoken",
037                    "api_key",
038                    "api_secret",
039                    "apipassword",
040                    "apiuser",
041                    "apiusername",
042                    "authkey",
043                    "authorizationtoken",
044                    "blobaccesskey",
045                    "blobstoragesharedkeycredential",
046                    "certresourcepassword",
047                    "cipherkey",
048                    "clientid",
049                    "clientsecret",
050                    "connectionstring",
051                    "consumerkey",
052                    "consumersecret",
053                    "emailaddress",
054                    "httpproxypassword",
055                    "keystore",
056                    "login",
057                    "oauthaccesstoken",
058                    "oauthappid",
059                    "oauthappsecret",
060                    "oauthclientid",
061                    "oauthclientsecret",
062                    "oauthtoken",
063                    "oauthtokenurl",
064                    "p12filename",
065                    "passcode",
066                    "passphrase",
067                    "password",
068                    "privatekey",
069                    "privatekeyfile",
070                    "privatekeyname",
071                    "privatekeypassword",
072                    "proxyauthpassword",
073                    "proxyauthusername",
074                    "proxypassword",
075                    "proxyuser",
076                    "publickeyid",
077                    "publishkey",
078                    "queueownerawsaccountid",
079                    "refreshtoken",
080                    "sasljaasconfig",
081                    "secretkey",
082                    "securerandom",
083                    "sharedaccesskey",
084                    "sourceblobaccesskey",
085                    "sslkeypassword",
086                    "sslkeystore",
087                    "sslkeystorepassword",
088                    "sslpassword",
089                    "ssltruststorepassword",
090                    "subscribekey",
091                    "systemid",
092                    "token",
093                    "tokencredential",
094                    "user",
095                    "username",
096                    "userpassword",
097                    "verificationcode",
098                    "webhookverifytoken",
099                    "zookeeperpassword"
100            // SENSITIVE-KEYS: END
101            )));
102
103    private static final String SENSITIVE_PATTERN = ""
104                                                    // Generated by camel build tools - do NOT edit this list!
105                                                    // SENSITIVE-PATTERN: START
106                                                    + "\\Qaccesskey\\E"
107                                                    + "|\\Qaccesstoken\\E"
108                                                    + "|\\Qaccesstokensecret\\E"
109                                                    + "|\\Qaccountkey\\E"
110                                                    + "|\\Qaccountsid\\E"
111                                                    + "|\\Qacltoken\\E"
112                                                    + "|\\Qapi_key\\E"
113                                                    + "|\\Qapi_secret\\E"
114                                                    + "|\\Qapipassword\\E"
115                                                    + "|\\Qapiuser\\E"
116                                                    + "|\\Qapiusername\\E"
117                                                    + "|\\Qauthkey\\E"
118                                                    + "|\\Qauthorizationtoken\\E"
119                                                    + "|\\Qblobaccesskey\\E"
120                                                    + "|\\Qblobstoragesharedkeycredential\\E"
121                                                    + "|\\Qcertresourcepassword\\E"
122                                                    + "|\\Qcipherkey\\E"
123                                                    + "|\\Qclientid\\E"
124                                                    + "|\\Qclientsecret\\E"
125                                                    + "|\\Qconnectionstring\\E"
126                                                    + "|\\Qconsumerkey\\E"
127                                                    + "|\\Qconsumersecret\\E"
128                                                    + "|\\Qemailaddress\\E"
129                                                    + "|\\Qhttpproxypassword\\E"
130                                                    + "|\\Qkeystore\\E"
131                                                    + "|\\Qlogin\\E"
132                                                    + "|\\Qoauthaccesstoken\\E"
133                                                    + "|\\Qoauthappid\\E"
134                                                    + "|\\Qoauthappsecret\\E"
135                                                    + "|\\Qoauthclientid\\E"
136                                                    + "|\\Qoauthclientsecret\\E"
137                                                    + "|\\Qoauthtoken\\E"
138                                                    + "|\\Qoauthtokenurl\\E"
139                                                    + "|\\Qp12filename\\E"
140                                                    + "|\\Qpasscode\\E"
141                                                    + "|\\Qpassphrase\\E"
142                                                    + "|\\Qpassword\\E"
143                                                    + "|\\Qprivatekey\\E"
144                                                    + "|\\Qprivatekeyfile\\E"
145                                                    + "|\\Qprivatekeyname\\E"
146                                                    + "|\\Qprivatekeypassword\\E"
147                                                    + "|\\Qproxyauthpassword\\E"
148                                                    + "|\\Qproxyauthusername\\E"
149                                                    + "|\\Qproxypassword\\E"
150                                                    + "|\\Qproxyuser\\E"
151                                                    + "|\\Qpublickeyid\\E"
152                                                    + "|\\Qpublishkey\\E"
153                                                    + "|\\Qqueueownerawsaccountid\\E"
154                                                    + "|\\Qrefreshtoken\\E"
155                                                    + "|\\Qsasljaasconfig\\E"
156                                                    + "|\\Qsecretkey\\E"
157                                                    + "|\\Qsecurerandom\\E"
158                                                    + "|\\Qsharedaccesskey\\E"
159                                                    + "|\\Qsourceblobaccesskey\\E"
160                                                    + "|\\Qsslkeypassword\\E"
161                                                    + "|\\Qsslkeystore\\E"
162                                                    + "|\\Qsslkeystorepassword\\E"
163                                                    + "|\\Qsslpassword\\E"
164                                                    + "|\\Qssltruststorepassword\\E"
165                                                    + "|\\Qsubscribekey\\E"
166                                                    + "|\\Qsystemid\\E"
167                                                    + "|\\Qtoken\\E"
168                                                    + "|\\Qtokencredential\\E"
169                                                    + "|\\Quser\\E"
170                                                    + "|\\Qusername\\E"
171                                                    + "|\\Quserpassword\\E"
172                                                    + "|\\Qverificationcode\\E"
173                                                    + "|\\Qwebhookverifytoken\\E"
174                                                    + "|\\Qzookeeperpassword\\E"
175    // SENSITIVE-PATTERN: END
176    ;
177
178    private SensitiveUtils() {
179    }
180
181    /**
182     * All the sensitive keys (unmodifiable) in lower-case
183     */
184    public static Set<String> getSensitiveKeys() {
185        return SENSITIVE_KEYS;
186    }
187
188    /**
189     * All the sensitive keys (unmodifiable) in lower-case for regular expression matching
190     */
191    public static String getSensitivePattern() {
192        return SENSITIVE_PATTERN;
193    }
194
195    /**
196     * Whether the given configuration property contains a sensitive key (such as password, accesstoken, etc.)
197     *
198     * @param  text the configuration property
199     * @return      true if sensitive, false otherwise
200     */
201    public static boolean containsSensitive(String text) {
202        int lastPeriod = text.lastIndexOf('.');
203        if (lastPeriod >= 0) {
204            text = text.substring(lastPeriod + 1);
205        }
206        text = text.toLowerCase(Locale.ENGLISH);
207        text = text.replace("-", "");
208        return SENSITIVE_KEYS.contains(text);
209    }
210
211}