|
| 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.enrollment; |
| 9 | + |
| 10 | +import org.apache.lucene.util.SetOnce; |
| 11 | +import org.elasticsearch.client.internal.Client; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 14 | +import org.elasticsearch.core.TimeValue; |
| 15 | +import org.elasticsearch.test.SecuritySingleNodeTestCase; |
| 16 | +import org.elasticsearch.xpack.core.security.EnrollmentToken; |
| 17 | +import org.elasticsearch.xpack.core.security.action.enrollment.KibanaEnrollmentAction; |
| 18 | +import org.elasticsearch.xpack.core.security.action.enrollment.KibanaEnrollmentRequest; |
| 19 | +import org.elasticsearch.xpack.core.security.action.enrollment.KibanaEnrollmentResponse; |
| 20 | +import org.elasticsearch.xpack.core.security.action.user.AuthenticateAction; |
| 21 | +import org.elasticsearch.xpack.core.security.action.user.AuthenticateRequest; |
| 22 | +import org.elasticsearch.xpack.core.security.action.user.AuthenticateResponse; |
| 23 | +import org.elasticsearch.xpack.core.ssl.SSLService; |
| 24 | +import org.junit.BeforeClass; |
| 25 | +import org.mockito.Mockito; |
| 26 | + |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.Base64; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.CountDownLatch; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +import static org.elasticsearch.test.SecuritySettingsSource.addSSLSettingsForStore; |
| 35 | +import static org.elasticsearch.xpack.core.XPackSettings.ENROLLMENT_ENABLED; |
| 36 | +import static org.hamcrest.Matchers.equalTo; |
| 37 | +import static org.mockito.Mockito.spy; |
| 38 | + |
| 39 | +public class EnrollmentSingleNodeTests extends SecuritySingleNodeTestCase { |
| 40 | + |
| 41 | + @BeforeClass |
| 42 | + public static void muteInFips() { |
| 43 | + assumeFalse("Enrollment is not supported in FIPS 140-2 as we are using PKCS#12 keystores", inFipsJvm()); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected Settings nodeSettings() { |
| 48 | + Settings.Builder builder = Settings.builder().put(super.nodeSettings()); |
| 49 | + addSSLSettingsForStore( |
| 50 | + builder, |
| 51 | + "xpack.security.http.", |
| 52 | + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/httpCa.p12", |
| 53 | + "password", |
| 54 | + false |
| 55 | + ); |
| 56 | + builder.put("xpack.security.http.ssl.enabled", true).put(ENROLLMENT_ENABLED.getKey(), "true"); |
| 57 | + // Need at least 2 threads because enrollment token creator internally uses a client |
| 58 | + builder.put(EsExecutors.NODE_PROCESSORS_SETTING.getKey(), 2); |
| 59 | + return builder.build(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected boolean addMockHttpTransport() { |
| 64 | + return false; // enable http |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected boolean transportSSLEnabled() { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + public void testKibanaEnrollmentTokenCreation() throws Exception { |
| 73 | + final SSLService sslService = getInstanceFromNode(SSLService.class); |
| 74 | + |
| 75 | + final InternalEnrollmentTokenGenerator internalEnrollmentTokenGenerator = spy( |
| 76 | + new InternalEnrollmentTokenGenerator( |
| 77 | + newEnvironment(Settings.builder().put(ENROLLMENT_ENABLED.getKey(), "true").build()), |
| 78 | + sslService, |
| 79 | + node().client() |
| 80 | + ) |
| 81 | + ); |
| 82 | + // Mock the getHttpsCaFingerprint method because the real method requires createClassLoader permission |
| 83 | + Mockito.doReturn("fingerprint").when(internalEnrollmentTokenGenerator).getHttpsCaFingerprint(); |
| 84 | + |
| 85 | + final SetOnce<EnrollmentToken> enrollmentTokenSetOnce = new SetOnce<>(); |
| 86 | + final CountDownLatch latch = new CountDownLatch(1); |
| 87 | + |
| 88 | + // Create the kibana enrollment token and wait for the process to complete |
| 89 | + internalEnrollmentTokenGenerator.createKibanaEnrollmentToken(enrollmentToken -> { |
| 90 | + enrollmentTokenSetOnce.set(enrollmentToken); |
| 91 | + latch.countDown(); |
| 92 | + }, List.of(TimeValue.timeValueMillis(500)).iterator()); |
| 93 | + latch.await(20, TimeUnit.SECONDS); |
| 94 | + |
| 95 | + // The API key is created by the right user and should work |
| 96 | + final Client apiKeyClient = client().filterWithHeader( |
| 97 | + Map.of( |
| 98 | + "Authorization", |
| 99 | + "ApiKey " + Base64.getEncoder().encodeToString(enrollmentTokenSetOnce.get().getApiKey().getBytes(StandardCharsets.UTF_8)) |
| 100 | + ) |
| 101 | + ); |
| 102 | + final AuthenticateResponse authenticateResponse1 = apiKeyClient.execute( |
| 103 | + AuthenticateAction.INSTANCE, |
| 104 | + new AuthenticateRequest("_xpack_security") |
| 105 | + ).actionGet(); |
| 106 | + assertThat(authenticateResponse1.authentication().getUser().principal(), equalTo("_xpack_security")); |
| 107 | + |
| 108 | + final KibanaEnrollmentResponse kibanaEnrollmentResponse = apiKeyClient.execute( |
| 109 | + KibanaEnrollmentAction.INSTANCE, |
| 110 | + new KibanaEnrollmentRequest() |
| 111 | + ).actionGet(); |
| 112 | + |
| 113 | + // The service token should work |
| 114 | + final Client kibanaClient = client().filterWithHeader( |
| 115 | + Map.of("Authorization", "Bearer " + kibanaEnrollmentResponse.getTokenValue()) |
| 116 | + ); |
| 117 | + |
| 118 | + final AuthenticateResponse authenticateResponse2 = kibanaClient.execute( |
| 119 | + AuthenticateAction.INSTANCE, |
| 120 | + new AuthenticateRequest("elastic/kibana") |
| 121 | + ).actionGet(); |
| 122 | + assertThat(authenticateResponse2.authentication().getUser().principal(), equalTo("elastic/kibana")); |
| 123 | + } |
| 124 | +} |
0 commit comments