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                    "apikey",
040                    "apipassword",
041                    "apiuser",
042                    "apiusername",
043                    "authenticationtoken",
044                    "authkey",
045                    "authorizationtoken",
046                    "authtoken",
047                    "azureclientid",
048                    "azureclientsecret",
049                    "blobaccesskey",
050                    "blobstoragesharedkeycredential",
051                    "certresourcepassword",
052                    "clientid",
053                    "clientsecret",
054                    "clientsecretcredential",
055                    "connectionstring",
056                    "consumerkey",
057                    "consumersecret",
058                    "emailaddress",
059                    "httpproxypassword",
060                    "kerberosauthticket",
061                    "keypassword",
062                    "keystorePassword",
063                    "keystorepassword",
064                    "login",
065                    "oauthclientid",
066                    "oauthclientsecret",
067                    "oauthtoken",
068                    "oauthtokenurl",
069                    "p12filename",
070                    "passcode",
071                    "passphrase",
072                    "password",
073                    "personalaccesstoken",
074                    "privatekey",
075                    "privatekeyfile",
076                    "privatekeyname",
077                    "privatekeypassword",
078                    "proxyauthpassword",
079                    "proxyauthusername",
080                    "proxypassword",
081                    "proxyuser",
082                    "proxyusername",
083                    "publickeyid",
084                    "publishkey",
085                    "queueownerawsaccountid",
086                    "realm",
087                    "refreshtoken",
088                    "sascredential",
089                    "sasljaasconfig",
090                    "saslpassword",
091                    "sassignature",
092                    "secret",
093                    "secretkey",
094                    "securerandom",
095                    "serviceinstanceid",
096                    "sessiontoken",
097                    "sharedaccesskey",
098                    "sourceblobaccesskey",
099                    "sslkeypassword",
100                    "sslkeystorepassword",
101                    "sslpassword",
102                    "ssltruststorepassword",
103                    "subscribekey",
104                    "systemid",
105                    "token",
106                    "tokencredential",
107                    "truststorepassword",
108                    "user",
109                    "userauthenticationcredentials",
110                    "username",
111                    "userpassword",
112                    "verificationcode",
113                    "webhookverifytoken",
114                    "zookeeperpassword"
115            // SENSITIVE-KEYS: END
116            )));
117
118    private static final String SENSITIVE_PATTERN = ""
119                                                    // Generated by camel build tools - do NOT edit this list!
120                                                    // SENSITIVE-PATTERN: START
121                                                    + "\\Qaccesskey\\E"
122                                                    + "|\\Qaccesstoken\\E"
123                                                    + "|\\Qaccesstokensecret\\E"
124                                                    + "|\\Qaccountkey\\E"
125                                                    + "|\\Qaccountsid\\E"
126                                                    + "|\\Qacltoken\\E"
127                                                    + "|\\Qapi_key\\E"
128                                                    + "|\\Qapi_secret\\E"
129                                                    + "|\\Qapikey\\E"
130                                                    + "|\\Qapipassword\\E"
131                                                    + "|\\Qapiuser\\E"
132                                                    + "|\\Qapiusername\\E"
133                                                    + "|\\Qauthenticationtoken\\E"
134                                                    + "|\\Qauthkey\\E"
135                                                    + "|\\Qauthorizationtoken\\E"
136                                                    + "|\\Qauthtoken\\E"
137                                                    + "|\\Qazureclientid\\E"
138                                                    + "|\\Qazureclientsecret\\E"
139                                                    + "|\\Qblobaccesskey\\E"
140                                                    + "|\\Qblobstoragesharedkeycredential\\E"
141                                                    + "|\\Qcertresourcepassword\\E"
142                                                    + "|\\Qclientid\\E"
143                                                    + "|\\Qclientsecret\\E"
144                                                    + "|\\Qclientsecretcredential\\E"
145                                                    + "|\\Qconnectionstring\\E"
146                                                    + "|\\Qconsumerkey\\E"
147                                                    + "|\\Qconsumersecret\\E"
148                                                    + "|\\Qemailaddress\\E"
149                                                    + "|\\Qhttpproxypassword\\E"
150                                                    + "|\\Qkerberosauthticket\\E"
151                                                    + "|\\Qkeypassword\\E"
152                                                    + "|\\QkeystorePassword\\E"
153                                                    + "|\\Qkeystorepassword\\E"
154                                                    + "|\\Qlogin\\E"
155                                                    + "|\\Qoauthclientid\\E"
156                                                    + "|\\Qoauthclientsecret\\E"
157                                                    + "|\\Qoauthtoken\\E"
158                                                    + "|\\Qoauthtokenurl\\E"
159                                                    + "|\\Qp12filename\\E"
160                                                    + "|\\Qpasscode\\E"
161                                                    + "|\\Qpassphrase\\E"
162                                                    + "|\\Qpassword\\E"
163                                                    + "|\\Qpersonalaccesstoken\\E"
164                                                    + "|\\Qprivatekey\\E"
165                                                    + "|\\Qprivatekeyfile\\E"
166                                                    + "|\\Qprivatekeyname\\E"
167                                                    + "|\\Qprivatekeypassword\\E"
168                                                    + "|\\Qproxyauthpassword\\E"
169                                                    + "|\\Qproxyauthusername\\E"
170                                                    + "|\\Qproxypassword\\E"
171                                                    + "|\\Qproxyuser\\E"
172                                                    + "|\\Qproxyusername\\E"
173                                                    + "|\\Qpublickeyid\\E"
174                                                    + "|\\Qpublishkey\\E"
175                                                    + "|\\Qqueueownerawsaccountid\\E"
176                                                    + "|\\Qrealm\\E"
177                                                    + "|\\Qrefreshtoken\\E"
178                                                    + "|\\Qsascredential\\E"
179                                                    + "|\\Qsasljaasconfig\\E"
180                                                    + "|\\Qsaslpassword\\E"
181                                                    + "|\\Qsassignature\\E"
182                                                    + "|\\Qsecret\\E"
183                                                    + "|\\Qsecretkey\\E"
184                                                    + "|\\Qsecurerandom\\E"
185                                                    + "|\\Qserviceinstanceid\\E"
186                                                    + "|\\Qsessiontoken\\E"
187                                                    + "|\\Qsharedaccesskey\\E"
188                                                    + "|\\Qsourceblobaccesskey\\E"
189                                                    + "|\\Qsslkeypassword\\E"
190                                                    + "|\\Qsslkeystorepassword\\E"
191                                                    + "|\\Qsslpassword\\E"
192                                                    + "|\\Qssltruststorepassword\\E"
193                                                    + "|\\Qsubscribekey\\E"
194                                                    + "|\\Qsystemid\\E"
195                                                    + "|\\Qtoken\\E"
196                                                    + "|\\Qtokencredential\\E"
197                                                    + "|\\Qtruststorepassword\\E"
198                                                    + "|\\Quser\\E"
199                                                    + "|\\Quserauthenticationcredentials\\E"
200                                                    + "|\\Qusername\\E"
201                                                    + "|\\Quserpassword\\E"
202                                                    + "|\\Qverificationcode\\E"
203                                                    + "|\\Qwebhookverifytoken\\E"
204                                                    + "|\\Qzookeeperpassword\\E"
205    // SENSITIVE-PATTERN: END
206    ;
207
208    private SensitiveUtils() {
209    }
210
211    /**
212     * All the sensitive keys (unmodifiable) in lower-case
213     */
214    public static Set<String> getSensitiveKeys() {
215        return SENSITIVE_KEYS;
216    }
217
218    /**
219     * All the sensitive keys (unmodifiable) in lower-case for regular expression matching
220     */
221    public static String getSensitivePattern() {
222        return SENSITIVE_PATTERN;
223    }
224
225    /**
226     * Whether the given configuration property contains a sensitive key (such as password, accesstoken, etc.)
227     *
228     * @param  text the configuration property
229     * @return      true if sensitive, false otherwise
230     */
231    public static boolean containsSensitive(String text) {
232        int lastPeriod = text.lastIndexOf('.');
233        if (lastPeriod >= 0) {
234            text = text.substring(lastPeriod + 1);
235        }
236        text = text.toLowerCase(Locale.ENGLISH);
237        text = text.replace("-", "");
238        return SENSITIVE_KEYS.contains(text);
239    }
240
241}