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                    "sassignature",
091                    "secret",
092                    "secretkey",
093                    "securerandom",
094                    "serviceinstanceid",
095                    "sessiontoken",
096                    "sharedaccesskey",
097                    "sourceblobaccesskey",
098                    "sslkeypassword",
099                    "sslkeystorepassword",
100                    "sslpassword",
101                    "ssltruststorepassword",
102                    "subscribekey",
103                    "systemid",
104                    "token",
105                    "tokencredential",
106                    "truststorepassword",
107                    "user",
108                    "userauthenticationcredentials",
109                    "username",
110                    "userpassword",
111                    "verificationcode",
112                    "webhookverifytoken",
113                    "zookeeperpassword"
114            // SENSITIVE-KEYS: END
115            )));
116
117    private static final String SENSITIVE_PATTERN = ""
118                                                    // Generated by camel build tools - do NOT edit this list!
119                                                    // SENSITIVE-PATTERN: START
120                                                    + "\\Qaccesskey\\E"
121                                                    + "|\\Qaccesstoken\\E"
122                                                    + "|\\Qaccesstokensecret\\E"
123                                                    + "|\\Qaccountkey\\E"
124                                                    + "|\\Qaccountsid\\E"
125                                                    + "|\\Qacltoken\\E"
126                                                    + "|\\Qapi_key\\E"
127                                                    + "|\\Qapi_secret\\E"
128                                                    + "|\\Qapikey\\E"
129                                                    + "|\\Qapipassword\\E"
130                                                    + "|\\Qapiuser\\E"
131                                                    + "|\\Qapiusername\\E"
132                                                    + "|\\Qauthenticationtoken\\E"
133                                                    + "|\\Qauthkey\\E"
134                                                    + "|\\Qauthorizationtoken\\E"
135                                                    + "|\\Qauthtoken\\E"
136                                                    + "|\\Qazureclientid\\E"
137                                                    + "|\\Qazureclientsecret\\E"
138                                                    + "|\\Qblobaccesskey\\E"
139                                                    + "|\\Qblobstoragesharedkeycredential\\E"
140                                                    + "|\\Qcertresourcepassword\\E"
141                                                    + "|\\Qclientid\\E"
142                                                    + "|\\Qclientsecret\\E"
143                                                    + "|\\Qclientsecretcredential\\E"
144                                                    + "|\\Qconnectionstring\\E"
145                                                    + "|\\Qconsumerkey\\E"
146                                                    + "|\\Qconsumersecret\\E"
147                                                    + "|\\Qemailaddress\\E"
148                                                    + "|\\Qhttpproxypassword\\E"
149                                                    + "|\\Qkerberosauthticket\\E"
150                                                    + "|\\Qkeypassword\\E"
151                                                    + "|\\QkeystorePassword\\E"
152                                                    + "|\\Qkeystorepassword\\E"
153                                                    + "|\\Qlogin\\E"
154                                                    + "|\\Qoauthclientid\\E"
155                                                    + "|\\Qoauthclientsecret\\E"
156                                                    + "|\\Qoauthtoken\\E"
157                                                    + "|\\Qoauthtokenurl\\E"
158                                                    + "|\\Qp12filename\\E"
159                                                    + "|\\Qpasscode\\E"
160                                                    + "|\\Qpassphrase\\E"
161                                                    + "|\\Qpassword\\E"
162                                                    + "|\\Qpersonalaccesstoken\\E"
163                                                    + "|\\Qprivatekey\\E"
164                                                    + "|\\Qprivatekeyfile\\E"
165                                                    + "|\\Qprivatekeyname\\E"
166                                                    + "|\\Qprivatekeypassword\\E"
167                                                    + "|\\Qproxyauthpassword\\E"
168                                                    + "|\\Qproxyauthusername\\E"
169                                                    + "|\\Qproxypassword\\E"
170                                                    + "|\\Qproxyuser\\E"
171                                                    + "|\\Qproxyusername\\E"
172                                                    + "|\\Qpublickeyid\\E"
173                                                    + "|\\Qpublishkey\\E"
174                                                    + "|\\Qqueueownerawsaccountid\\E"
175                                                    + "|\\Qrealm\\E"
176                                                    + "|\\Qrefreshtoken\\E"
177                                                    + "|\\Qsascredential\\E"
178                                                    + "|\\Qsasljaasconfig\\E"
179                                                    + "|\\Qsassignature\\E"
180                                                    + "|\\Qsecret\\E"
181                                                    + "|\\Qsecretkey\\E"
182                                                    + "|\\Qsecurerandom\\E"
183                                                    + "|\\Qserviceinstanceid\\E"
184                                                    + "|\\Qsessiontoken\\E"
185                                                    + "|\\Qsharedaccesskey\\E"
186                                                    + "|\\Qsourceblobaccesskey\\E"
187                                                    + "|\\Qsslkeypassword\\E"
188                                                    + "|\\Qsslkeystorepassword\\E"
189                                                    + "|\\Qsslpassword\\E"
190                                                    + "|\\Qssltruststorepassword\\E"
191                                                    + "|\\Qsubscribekey\\E"
192                                                    + "|\\Qsystemid\\E"
193                                                    + "|\\Qtoken\\E"
194                                                    + "|\\Qtokencredential\\E"
195                                                    + "|\\Qtruststorepassword\\E"
196                                                    + "|\\Quser\\E"
197                                                    + "|\\Quserauthenticationcredentials\\E"
198                                                    + "|\\Qusername\\E"
199                                                    + "|\\Quserpassword\\E"
200                                                    + "|\\Qverificationcode\\E"
201                                                    + "|\\Qwebhookverifytoken\\E"
202                                                    + "|\\Qzookeeperpassword\\E"
203    // SENSITIVE-PATTERN: END
204    ;
205
206    private SensitiveUtils() {
207    }
208
209    /**
210     * All the sensitive keys (unmodifiable) in lower-case
211     */
212    public static Set<String> getSensitiveKeys() {
213        return SENSITIVE_KEYS;
214    }
215
216    /**
217     * All the sensitive keys (unmodifiable) in lower-case for regular expression matching
218     */
219    public static String getSensitivePattern() {
220        return SENSITIVE_PATTERN;
221    }
222
223    /**
224     * Whether the given configuration property contains a sensitive key (such as password, accesstoken, etc.)
225     *
226     * @param  text the configuration property
227     * @return      true if sensitive, false otherwise
228     */
229    public static boolean containsSensitive(String text) {
230        int lastPeriod = text.lastIndexOf('.');
231        if (lastPeriod >= 0) {
232            text = text.substring(lastPeriod + 1);
233        }
234        text = text.toLowerCase(Locale.ENGLISH);
235        text = text.replace("-", "");
236        return SENSITIVE_KEYS.contains(text);
237    }
238
239}