|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch 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 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.elasticsearch.index.reindex; |
| 21 | + |
| 22 | +import org.apache.http.conn.ssl.DefaultHostnameVerifier; |
| 23 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; |
| 24 | +import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy; |
| 25 | +import org.elasticsearch.common.Strings; |
| 26 | +import org.elasticsearch.common.settings.SecureSetting; |
| 27 | +import org.elasticsearch.common.settings.SecureString; |
| 28 | +import org.elasticsearch.common.settings.Setting; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.common.ssl.SslConfiguration; |
| 31 | +import org.elasticsearch.common.ssl.SslConfigurationKeys; |
| 32 | +import org.elasticsearch.common.ssl.SslConfigurationLoader; |
| 33 | +import org.elasticsearch.env.Environment; |
| 34 | +import org.elasticsearch.watcher.FileChangesListener; |
| 35 | +import org.elasticsearch.watcher.FileWatcher; |
| 36 | +import org.elasticsearch.watcher.ResourceWatcherService; |
| 37 | + |
| 38 | +import javax.net.ssl.HostnameVerifier; |
| 39 | +import javax.net.ssl.SSLContext; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.UncheckedIOException; |
| 42 | +import java.nio.file.Path; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.HashMap; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
| 48 | +import java.util.function.Function; |
| 49 | + |
| 50 | +import static org.elasticsearch.common.settings.Setting.listSetting; |
| 51 | +import static org.elasticsearch.common.settings.Setting.simpleString; |
| 52 | + |
| 53 | +/** |
| 54 | + * Loads "reindex.ssl.*" configuration from Settings, and makes the applicable configuration (trust manager / key manager / hostname |
| 55 | + * verification / cipher-suites) available for reindex-from-remote. |
| 56 | + */ |
| 57 | +class ReindexSslConfig { |
| 58 | + |
| 59 | + private static final Map<String, Setting<?>> SETTINGS = new HashMap<>(); |
| 60 | + private static final Map<String, Setting<SecureString>> SECURE_SETTINGS = new HashMap<>(); |
| 61 | + |
| 62 | + static { |
| 63 | + Setting.Property[] defaultProperties = new Setting.Property[] { Setting.Property.NodeScope, Setting.Property.Filtered }; |
| 64 | + Setting.Property[] deprecatedProperties = new Setting.Property[] { Setting.Property.Deprecated, Setting.Property.NodeScope, |
| 65 | + Setting.Property.Filtered }; |
| 66 | + for (String key : SslConfigurationKeys.getStringKeys()) { |
| 67 | + String settingName = "reindex.ssl." + key; |
| 68 | + final Setting.Property[] properties = SslConfigurationKeys.isDeprecated(key) ? deprecatedProperties : defaultProperties; |
| 69 | + SETTINGS.put(settingName, simpleString(settingName, properties)); |
| 70 | + } |
| 71 | + for (String key : SslConfigurationKeys.getListKeys()) { |
| 72 | + String settingName = "reindex.ssl." + key; |
| 73 | + final Setting.Property[] properties = SslConfigurationKeys.isDeprecated(key) ? deprecatedProperties : defaultProperties; |
| 74 | + SETTINGS.put(settingName, listSetting(settingName, Collections.emptyList(), Function.identity(), properties)); |
| 75 | + } |
| 76 | + for (String key : SslConfigurationKeys.getSecureStringKeys()) { |
| 77 | + String settingName = "reindex.ssl." + key; |
| 78 | + SECURE_SETTINGS.put(settingName, SecureSetting.secureString(settingName, null)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private final SslConfiguration configuration; |
| 83 | + private volatile SSLContext context; |
| 84 | + |
| 85 | + public static List<Setting<?>> getSettings() { |
| 86 | + List<Setting<?>> settings = new ArrayList<>(); |
| 87 | + settings.addAll(SETTINGS.values()); |
| 88 | + settings.addAll(SECURE_SETTINGS.values()); |
| 89 | + return settings; |
| 90 | + } |
| 91 | + |
| 92 | + ReindexSslConfig(Settings settings, Environment environment, ResourceWatcherService resourceWatcher) { |
| 93 | + final SslConfigurationLoader loader = new SslConfigurationLoader("reindex.ssl.") { |
| 94 | + |
| 95 | + @Override |
| 96 | + protected String getSettingAsString(String key) { |
| 97 | + return settings.get(key); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected char[] getSecureSetting(String key) { |
| 102 | + final Setting<SecureString> setting = SECURE_SETTINGS.get(key); |
| 103 | + if (setting == null) { |
| 104 | + throw new IllegalArgumentException("The secure setting [" + key + "] is not registered"); |
| 105 | + } |
| 106 | + return setting.get(settings).getChars(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected List<String> getSettingAsList(String key) throws Exception { |
| 111 | + return settings.getAsList(key); |
| 112 | + } |
| 113 | + }; |
| 114 | + configuration = loader.load(environment.configFile()); |
| 115 | + reload(); |
| 116 | + |
| 117 | + final FileChangesListener listener = new FileChangesListener() { |
| 118 | + @Override |
| 119 | + public void onFileCreated(Path file) { |
| 120 | + onFileChanged(file); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onFileDeleted(Path file) { |
| 125 | + onFileChanged(file); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onFileChanged(Path file) { |
| 130 | + ReindexSslConfig.this.reload(); |
| 131 | + } |
| 132 | + }; |
| 133 | + for (Path file : configuration.getDependentFiles()) { |
| 134 | + try { |
| 135 | + final FileWatcher watcher = new FileWatcher(file); |
| 136 | + watcher.addListener(listener); |
| 137 | + resourceWatcher.add(watcher, ResourceWatcherService.Frequency.HIGH); |
| 138 | + } catch (IOException e) { |
| 139 | + throw new UncheckedIOException("cannot watch file [" + file + "]", e); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private void reload() { |
| 145 | + this.context = configuration.createSslContext(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Encapsulate the loaded SSL configuration as a HTTP-client {@link SSLIOSessionStrategy}. |
| 150 | + * The returned strategy is immutable, but successive calls will return different objects that may have different |
| 151 | + * configurations if the underlying key/certificate files are modified. |
| 152 | + */ |
| 153 | + SSLIOSessionStrategy getStrategy() { |
| 154 | + final HostnameVerifier hostnameVerifier = configuration.getVerificationMode().isHostnameVerificationEnabled() |
| 155 | + ? new DefaultHostnameVerifier() |
| 156 | + : new NoopHostnameVerifier(); |
| 157 | + final String[] protocols = configuration.getSupportedProtocols().toArray(Strings.EMPTY_ARRAY); |
| 158 | + final String[] cipherSuites = configuration.getCipherSuites().toArray(Strings.EMPTY_ARRAY); |
| 159 | + return new SSLIOSessionStrategy(context, protocols, cipherSuites, hostnameVerifier); |
| 160 | + } |
| 161 | +} |
0 commit comments