|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.license; |
| 7 | + |
| 8 | +import org.elasticsearch.Version; |
| 9 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 10 | +import org.elasticsearch.cluster.ClusterStateUpdateTask; |
| 11 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.common.transport.TransportAddress; |
| 14 | +import org.elasticsearch.common.unit.TimeValue; |
| 15 | + |
| 16 | +import java.net.InetAddress; |
| 17 | + |
| 18 | +import static java.util.Collections.emptyMap; |
| 19 | +import static java.util.Collections.emptySet; |
| 20 | +import static org.hamcrest.Matchers.containsString; |
| 21 | +import static org.mockito.Matchers.any; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | + |
| 25 | +public class LicenseTLSTests extends AbstractLicenseServiceTestCase { |
| 26 | + |
| 27 | + private InetAddress inetAddress; |
| 28 | + |
| 29 | + public void testApplyLicenseInDevMode() throws Exception { |
| 30 | + License newLicense = TestUtils.generateSignedLicense(randomFrom("gold", "platinum"), TimeValue.timeValueHours(24L)); |
| 31 | + PutLicenseRequest request = new PutLicenseRequest(); |
| 32 | + request.acknowledge(true); |
| 33 | + request.license(newLicense); |
| 34 | + Settings settings = Settings.builder().put("xpack.security.enabled", true).build(); |
| 35 | + XPackLicenseState licenseState = new XPackLicenseState(settings); |
| 36 | + inetAddress = InetAddress.getLoopbackAddress(); |
| 37 | + |
| 38 | + setInitialState(null, licenseState, settings); |
| 39 | + licenseService.start(); |
| 40 | + PlainActionFuture<PutLicenseResponse> responseFuture = new PlainActionFuture<>(); |
| 41 | + licenseService.registerLicense(request, responseFuture); |
| 42 | + verify(clusterService).submitStateUpdateTask(any(String.class), any(ClusterStateUpdateTask.class)); |
| 43 | + |
| 44 | + inetAddress = TransportAddress.META_ADDRESS; |
| 45 | + settings = Settings.builder() |
| 46 | + .put("xpack.security.enabled", true) |
| 47 | + .put("discovery.type", "single-node") |
| 48 | + .build(); |
| 49 | + licenseService.stop(); |
| 50 | + licenseState = new XPackLicenseState(settings); |
| 51 | + setInitialState(null, licenseState, settings); |
| 52 | + licenseService.start(); |
| 53 | + licenseService.registerLicense(request, responseFuture); |
| 54 | + verify(clusterService, times(2)).submitStateUpdateTask(any(String.class), any(ClusterStateUpdateTask.class)); |
| 55 | + } |
| 56 | + |
| 57 | + public void testApplyLicenseInProdMode() throws Exception { |
| 58 | + final String licenseType = randomFrom("GOLD", "PLATINUM"); |
| 59 | + License newLicense = TestUtils.generateSignedLicense(licenseType, TimeValue.timeValueHours(24L)); |
| 60 | + PutLicenseRequest request = new PutLicenseRequest(); |
| 61 | + request.acknowledge(true); |
| 62 | + request.license(newLicense); |
| 63 | + Settings settings = Settings.builder().put("xpack.security.enabled", true).build(); |
| 64 | + XPackLicenseState licenseState = new XPackLicenseState(settings); |
| 65 | + inetAddress = TransportAddress.META_ADDRESS; |
| 66 | + |
| 67 | + setInitialState(null, licenseState, settings); |
| 68 | + licenseService.start(); |
| 69 | + PlainActionFuture<PutLicenseResponse> responseFuture = new PlainActionFuture<>(); |
| 70 | + IllegalStateException e = expectThrows(IllegalStateException.class, () -> licenseService.registerLicense(request, responseFuture)); |
| 71 | + assertThat(e.getMessage(), |
| 72 | + containsString("Cannot install a [" + licenseType + "] license unless TLS is configured or security is disabled")); |
| 73 | + |
| 74 | + settings = Settings.builder().put("xpack.security.enabled", false).build(); |
| 75 | + licenseService.stop(); |
| 76 | + licenseState = new XPackLicenseState(settings); |
| 77 | + setInitialState(null, licenseState, settings); |
| 78 | + licenseService.start(); |
| 79 | + licenseService.registerLicense(request, responseFuture); |
| 80 | + verify(clusterService).submitStateUpdateTask(any(String.class), any(ClusterStateUpdateTask.class)); |
| 81 | + |
| 82 | + settings = Settings.builder() |
| 83 | + .put("xpack.security.enabled", true) |
| 84 | + .put("xpack.security.transport.ssl.enabled", true) |
| 85 | + .build(); |
| 86 | + licenseService.stop(); |
| 87 | + licenseState = new XPackLicenseState(settings); |
| 88 | + setInitialState(null, licenseState, settings); |
| 89 | + licenseService.start(); |
| 90 | + licenseService.registerLicense(request, responseFuture); |
| 91 | + verify(clusterService, times(2)).submitStateUpdateTask(any(String.class), any(ClusterStateUpdateTask.class)); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected DiscoveryNode getLocalNode() { |
| 96 | + return new DiscoveryNode("localnode", new TransportAddress(inetAddress, randomIntBetween(9300, 9399)), |
| 97 | + emptyMap(), emptySet(), Version.CURRENT); |
| 98 | + } |
| 99 | +} |
0 commit comments