|  | 
|  | 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 | +package org.elasticsearch.bootstrap; | 
|  | 20 | + | 
|  | 21 | +import org.apache.lucene.util.IOUtils; | 
|  | 22 | +import org.elasticsearch.common.settings.KeyStoreCommandTestCase; | 
|  | 23 | +import org.elasticsearch.common.settings.KeyStoreWrapper; | 
|  | 24 | +import org.elasticsearch.common.settings.SecureSettings; | 
|  | 25 | +import org.elasticsearch.common.settings.SecureString; | 
|  | 26 | +import org.elasticsearch.common.settings.Settings; | 
|  | 27 | +import org.elasticsearch.env.Environment; | 
|  | 28 | +import org.elasticsearch.test.ESTestCase; | 
|  | 29 | +import org.junit.After; | 
|  | 30 | +import org.junit.Before; | 
|  | 31 | + | 
|  | 32 | +import java.io.IOException; | 
|  | 33 | +import java.nio.file.FileSystem; | 
|  | 34 | +import java.nio.file.Files; | 
|  | 35 | +import java.nio.file.Path; | 
|  | 36 | +import java.util.ArrayList; | 
|  | 37 | +import java.util.List; | 
|  | 38 | + | 
|  | 39 | +public class BootstrapTests extends ESTestCase { | 
|  | 40 | +    Environment env; | 
|  | 41 | +    List<FileSystem> fileSystems = new ArrayList<>(); | 
|  | 42 | + | 
|  | 43 | +    @After | 
|  | 44 | +    public void closeMockFileSystems() throws IOException { | 
|  | 45 | +        IOUtils.close(fileSystems); | 
|  | 46 | +    } | 
|  | 47 | + | 
|  | 48 | +    @Before | 
|  | 49 | +    public void setupEnv() throws IOException { | 
|  | 50 | +        env = KeyStoreCommandTestCase.setupEnv(true, fileSystems); | 
|  | 51 | +    } | 
|  | 52 | + | 
|  | 53 | +    public void testLoadSecureSettingsCreatesKeystore() throws BootstrapException { | 
|  | 54 | +        final Path configPath = env.configFile(); | 
|  | 55 | +        assertFalse(Files.exists(configPath.resolve("elasticsearch.keystore"))); | 
|  | 56 | +        Bootstrap.loadSecureSettings(env); | 
|  | 57 | +        assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore"))); | 
|  | 58 | +    } | 
|  | 59 | + | 
|  | 60 | +    public void testLoadSecureSettings() throws Exception { | 
|  | 61 | +        final Path configPath = env.configFile(); | 
|  | 62 | +        final SecureString seed; | 
|  | 63 | +        try (KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.create(new char[0])) { | 
|  | 64 | +            seed = KeyStoreWrapper.SEED_SETTING.get(Settings.builder().setSecureSettings(keyStoreWrapper).build()); | 
|  | 65 | +            assertNotNull(seed); | 
|  | 66 | +            assertTrue(seed.length() > 0); | 
|  | 67 | +            keyStoreWrapper.save(configPath); | 
|  | 68 | +        } | 
|  | 69 | +        assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore"))); | 
|  | 70 | +        try (SecureSettings secureSettings = Bootstrap.loadSecureSettings(env)) { | 
|  | 71 | +            SecureString seedAfterLoad = KeyStoreWrapper.SEED_SETTING.get(Settings.builder().setSecureSettings(secureSettings).build()); | 
|  | 72 | +            assertEquals(seedAfterLoad.toString(), seed.toString()); | 
|  | 73 | +            assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore"))); | 
|  | 74 | +        } | 
|  | 75 | +    } | 
|  | 76 | +} | 
0 commit comments