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