1+ /*
2+ * Licensed to Elasticsearch B.V. under one or more contributor
3+ * license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright
5+ * ownership. Elasticsearch B.V. licenses this file to you under
6+ * the Apache License, Version 2.0 (the "License"); you may
7+ * not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+
20+ package org .logstash .secret ;
21+
22+ import com .google .common .annotations .VisibleForTesting ;
23+ import org .apache .logging .log4j .util .Strings ;
24+
25+ import java .util .Arrays ;
26+ import java .util .List ;
27+ import java .util .Objects ;
28+ import java .util .regex .Pattern ;
29+ import java .util .regex .Matcher ;
30+ import java .util .stream .Collectors ;
31+
32+ public class KeyValidator {
33+
34+ @ VisibleForTesting
35+ protected static final List <String > RESTRICTED_SYMBOLS = Arrays .asList ("?" , ".." , "/" , "\\ " , "'" , "\" " , "$" , "*" , "|" , "<" , ">" , " " );
36+ private static final Pattern RESTRICTED_PATTERN = buildPattern ();
37+
38+ private static Pattern buildPattern () {
39+ String pattern = RESTRICTED_SYMBOLS .stream ()
40+ .map (Pattern ::quote )
41+ .collect (Collectors .joining ("|" ));
42+ return Pattern .compile (pattern );
43+ }
44+
45+ /**
46+ * Validates the key against the {@link KeyValidator#RESTRICTED_SYMBOLS} list.
47+ * Throws {@link IllegalArgumentException} if the key contains any of them.
48+ * @param key A key to be validated
49+ * @param keyName A key name mapped to the key
50+ */
51+ public static void validateKey (final String key , final String keyName ) {
52+ if (Strings .isBlank (key )) {
53+ throw new IllegalArgumentException (String .format ("%s may not be null or blank" , keyName ));
54+ }
55+
56+ Matcher matcher = RESTRICTED_PATTERN .matcher (key );
57+ if (matcher .find ()) {
58+ String foundSymbol = matcher .group ();
59+ foundSymbol = Objects .equals (foundSymbol , " " ) ? "whitespace" : foundSymbol ;
60+ throw new IllegalArgumentException (String .format ("%s can not contain %s" , keyName , foundSymbol ));
61+ }
62+ }
63+ }
0 commit comments