|
| 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.transport; |
| 9 | + |
| 10 | +import org.elasticsearch.common.settings.MockSecureSettings; |
| 11 | +import org.elasticsearch.common.settings.Settings; |
| 12 | +import org.elasticsearch.common.ssl.PemKeyConfig; |
| 13 | +import org.elasticsearch.test.SecurityIntegTestCase; |
| 14 | + |
| 15 | +import static org.elasticsearch.xpack.security.transport.CrossClusterApiKeySignerSettings.SIGNING_CERT_PATH; |
| 16 | +import static org.elasticsearch.xpack.security.transport.CrossClusterApiKeySignerSettings.SIGNING_KEYSTORE_ALIAS; |
| 17 | +import static org.elasticsearch.xpack.security.transport.CrossClusterApiKeySignerSettings.SIGNING_KEYSTORE_PATH; |
| 18 | +import static org.elasticsearch.xpack.security.transport.CrossClusterApiKeySignerSettings.SIGNING_KEYSTORE_SECURE_PASSWORD; |
| 19 | +import static org.elasticsearch.xpack.security.transport.CrossClusterApiKeySignerSettings.SIGNING_KEYSTORE_TYPE; |
| 20 | +import static org.elasticsearch.xpack.security.transport.CrossClusterApiKeySignerSettings.SIGNING_KEY_PATH; |
| 21 | +import static org.hamcrest.Matchers.equalToIgnoringCase; |
| 22 | + |
| 23 | +public class CrossClusterApiKeySignerIntegTests extends SecurityIntegTestCase { |
| 24 | + |
| 25 | + private static final String DYNAMIC_TEST_CLUSTER_ALIAS = "dynamic_test_cluster"; |
| 26 | + private static final String STATIC_TEST_CLUSTER_ALIAS = "static_test_cluster"; |
| 27 | + |
| 28 | + public void testSignWithPemKeyConfig() { |
| 29 | + final CrossClusterApiKeySigner signer = internalCluster().getInstance( |
| 30 | + CrossClusterApiKeySigner.class, |
| 31 | + internalCluster().getRandomNodeName() |
| 32 | + ); |
| 33 | + final String[] testHeaders = randomArray(5, String[]::new, () -> randomAlphanumericOfLength(randomInt(20))); |
| 34 | + |
| 35 | + X509CertificateSignature signature = signer.sign(STATIC_TEST_CLUSTER_ALIAS, testHeaders); |
| 36 | + signature.certificate().getPublicKey(); |
| 37 | + |
| 38 | + var keyConfig = new PemKeyConfig( |
| 39 | + "signing_rsa.crt", |
| 40 | + "signing_rsa.key", |
| 41 | + new char[0], |
| 42 | + getDataPath("/org/elasticsearch/xpack/security/signature/signing_rsa.crt").getParent() |
| 43 | + ); |
| 44 | + |
| 45 | + assertThat(signature.algorithm(), equalToIgnoringCase(keyConfig.getKeys().getFirst().v2().getSigAlgName())); |
| 46 | + assertEquals(signature.certificate(), keyConfig.getKeys().getFirst().v2()); |
| 47 | + } |
| 48 | + |
| 49 | + public void testSignUnknownClusterAlias() { |
| 50 | + final CrossClusterApiKeySigner signer = internalCluster().getInstance( |
| 51 | + CrossClusterApiKeySigner.class, |
| 52 | + internalCluster().getRandomNodeName() |
| 53 | + ); |
| 54 | + final String[] testHeaders = randomArray(5, String[]::new, () -> randomAlphanumericOfLength(randomInt(20))); |
| 55 | + |
| 56 | + X509CertificateSignature signature = signer.sign("unknowncluster", testHeaders); |
| 57 | + assertNull(signature); |
| 58 | + } |
| 59 | + |
| 60 | + public void testSeveralKeyStoreAliases() { |
| 61 | + final CrossClusterApiKeySigner signer = internalCluster().getInstance( |
| 62 | + CrossClusterApiKeySigner.class, |
| 63 | + internalCluster().getRandomNodeName() |
| 64 | + ); |
| 65 | + |
| 66 | + try { |
| 67 | + // Create a new config without an alias. Since there are several aliases in the keystore, no signature should be generated |
| 68 | + updateClusterSettings( |
| 69 | + Settings.builder() |
| 70 | + .put( |
| 71 | + SIGNING_KEYSTORE_TYPE.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey(), |
| 72 | + inFipsJvm() ? "BCFKS" : "PKCS12" |
| 73 | + ) |
| 74 | + .put( |
| 75 | + SIGNING_KEYSTORE_PATH.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey(), |
| 76 | + getDataPath("/org/elasticsearch/xpack/security/signature/signing." + (inFipsJvm() ? "bcfks" : "jks")) |
| 77 | + ) |
| 78 | + ); |
| 79 | + |
| 80 | + { |
| 81 | + X509CertificateSignature signature = signer.sign(DYNAMIC_TEST_CLUSTER_ALIAS, "test", "test"); |
| 82 | + assertNull(signature); |
| 83 | + } |
| 84 | + |
| 85 | + // Add an alias from the keystore |
| 86 | + updateClusterSettings( |
| 87 | + Settings.builder() |
| 88 | + .put(SIGNING_KEYSTORE_ALIAS.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey(), "wholelottakey") |
| 89 | + ); |
| 90 | + { |
| 91 | + X509CertificateSignature signature = signer.sign(DYNAMIC_TEST_CLUSTER_ALIAS, "test", "test"); |
| 92 | + assertNotNull(signature); |
| 93 | + } |
| 94 | + |
| 95 | + // Add an alias not in the keystore, settings should silently fail to apply |
| 96 | + updateClusterSettings( |
| 97 | + Settings.builder() |
| 98 | + .put(SIGNING_KEYSTORE_ALIAS.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey(), "idonotexist") |
| 99 | + ); |
| 100 | + { |
| 101 | + X509CertificateSignature signature = signer.sign(DYNAMIC_TEST_CLUSTER_ALIAS, "test", "test"); |
| 102 | + assertNotNull(signature); |
| 103 | + } |
| 104 | + } finally { |
| 105 | + updateClusterSettings( |
| 106 | + Settings.builder() |
| 107 | + .putNull(SIGNING_KEYSTORE_PATH.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey()) |
| 108 | + .putNull(SIGNING_KEYSTORE_ALIAS.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey()) |
| 109 | + .putNull(SIGNING_KEYSTORE_TYPE.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey()) |
| 110 | + .setSecureSettings(new MockSecureSettings()) |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 117 | + var builder = Settings.builder(); |
| 118 | + MockSecureSettings secureSettings = (MockSecureSettings) builder.put(super.nodeSettings(nodeOrdinal, otherSettings)) |
| 119 | + .put( |
| 120 | + SIGNING_CERT_PATH.getConcreteSettingForNamespace(STATIC_TEST_CLUSTER_ALIAS).getKey(), |
| 121 | + getDataPath("/org/elasticsearch/xpack/security/signature/signing_rsa.crt") |
| 122 | + ) |
| 123 | + .put( |
| 124 | + SIGNING_KEY_PATH.getConcreteSettingForNamespace(STATIC_TEST_CLUSTER_ALIAS).getKey(), |
| 125 | + getDataPath("/org/elasticsearch/xpack/security/signature/signing_rsa.key") |
| 126 | + ) |
| 127 | + .getSecureSettings(); |
| 128 | + secureSettings.setString( |
| 129 | + SIGNING_KEYSTORE_SECURE_PASSWORD.getConcreteSettingForNamespace(DYNAMIC_TEST_CLUSTER_ALIAS).getKey(), |
| 130 | + "secretpassword" |
| 131 | + ); |
| 132 | + return builder.build(); |
| 133 | + } |
| 134 | +} |
0 commit comments