|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.security.ssl; |
| 9 | + |
| 10 | +import org.elasticsearch.common.io.Streams; |
| 11 | +import org.elasticsearch.common.settings.SecureString; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 14 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 15 | +import org.elasticsearch.test.cluster.LogType; |
| 16 | +import org.elasticsearch.test.cluster.MutableSettingsProvider; |
| 17 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 18 | +import org.elasticsearch.xpack.security.SecurityOnTrialLicenseRestTestCase; |
| 19 | +import org.junit.ClassRule; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | + |
| 25 | +import static org.hamcrest.Matchers.is; |
| 26 | + |
| 27 | +public class SslEntitlementRestIT extends ESRestTestCase { |
| 28 | + |
| 29 | + private static final MutableSettingsProvider settingsProvider = new MutableSettingsProvider(); |
| 30 | + |
| 31 | + @ClassRule |
| 32 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 33 | + .apply(SecurityOnTrialLicenseRestTestCase.commonTrialSecurityClusterConfig) |
| 34 | + .settings(settingsProvider) |
| 35 | + .systemProperty("es.entitlements.enabled", "true") |
| 36 | + .build(); |
| 37 | + |
| 38 | + @Override |
| 39 | + protected String getTestRestCluster() { |
| 40 | + return cluster.getHttpAddresses(); |
| 41 | + } |
| 42 | + |
| 43 | + public void testSslEntitlementInaccessiblePath() throws IOException { |
| 44 | + settingsProvider.put("xpack.security.transport.ssl.key", "/bad/path/transport.key"); |
| 45 | + settingsProvider.put("xpack.security.transport.ssl.certificate", "/bad/path/transport.crt"); |
| 46 | + expectThrows(Exception.class, () -> cluster.restart(false)); |
| 47 | + AtomicBoolean found = new AtomicBoolean(false); |
| 48 | + for (int i = 0; i < cluster.getNumNodes(); i++) { |
| 49 | + try (InputStream log = cluster.getNodeLog(i, LogType.SERVER)) { |
| 50 | + Streams.readAllLines(log, line -> { |
| 51 | + if (line.contains("failed to load SSL configuration") && line.contains("because access to read the file is blocked")) { |
| 52 | + found.set(true); |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + assertThat("cluster logs did not include events of blocked file access", found.get(), is(true)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected Settings restAdminSettings() { |
| 62 | + String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); |
| 63 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected Settings restClientSettings() { |
| 68 | + String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); |
| 69 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected boolean preserveClusterUponCompletion() { |
| 74 | + // as the cluster is dead its state can not be wiped successfully so we have to bypass wiping the cluster |
| 75 | + return true; |
| 76 | + } |
| 77 | +} |
0 commit comments