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