Change EmailSslTest for FIPS 140 JVMs (#46278)

This commit changes the SSLContext for the email server we use in
the tests so that it loads its key material from an in memory
keystore (that is in turn built from a pair of PEM encoded private key
and certificate) instead of a PKCS#12 one. This is done so that when 
we run our tests in FIPS 140-2 JVMs, the keystore is of a type that the
Security Provider actually supports.

This also mutes testCanSendMessageToSmtpServerByDisablingVerification
as we can't run tests with verification set to `none` in FIPS 140
JVMs.
This commit is contained in:
Ioannis Kakavas 2019-09-10 14:38:35 +03:00
parent 0ac52d0e72
commit 690164d0be
4 changed files with 48 additions and 6 deletions

View File

@ -12,6 +12,8 @@ import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.TestEnvironment;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.ssl.CertParsingUtils;
import org.elasticsearch.xpack.core.ssl.PemUtils;
import org.elasticsearch.xpack.core.ssl.SSLService;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.watch.Payload;
@ -31,7 +33,8 @@ import javax.mail.internet.MimeMessage;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.ArrayList;
@ -50,18 +53,26 @@ public class EmailSslTests extends ESTestCase {
@Before
public void startSmtpServer() throws GeneralSecurityException, IOException {
final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
// Keystore and private key will share the same password
final char[] keystorePassword = "test-smtp".toCharArray();
try (InputStream is = getDataInputStream("test-smtp.p12")) {
keyStore.load(is, keystorePassword);
}
final Path tempDir = createTempDir();
final Path certPath = tempDir.resolve("test-smtp.crt");
final Path keyPath = tempDir.resolve("test-smtp.pem");
Files.copy(getDataPath("/org/elasticsearch/xpack/watcher/actions/email/test-smtp.crt"), certPath);
Files.copy(getDataPath("/org/elasticsearch/xpack/watcher/actions/email/test-smtp.pem"), keyPath);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, keystorePassword);
keyStore.setKeyEntry("test-smtp", PemUtils.readPrivateKey(keyPath, keystorePassword::clone), keystorePassword,
CertParsingUtils.readCertificates(Collections.singletonList(certPath)));
final SSLContext sslContext = new SSLContextBuilder().loadKeyMaterial(keyStore, keystorePassword).build();
server = EmailServer.localhost(logger, sslContext);
}
@After
public void stopSmtpServer() {
server.stop();
if (null != server) {
server.stop();
}
}
public void testFailureSendingMessageToSmtpServerWithUntrustedCertificateAuthority() throws Exception {
@ -96,6 +107,7 @@ public class EmailSslTests extends ESTestCase {
}
public void testCanSendMessageToSmtpServerByDisablingVerification() throws Exception {
assumeFalse("Can't run in a FIPS JVM with verification mode None", inFipsJvm());
List<MimeMessage> messages = new ArrayList<>();
server.addListener(messages::add);
try {

View File

@ -0,0 +1,30 @@
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,AD2AC08225DD9EA7A429BB867D62D2D1
Hv/myqMGjejCI3OFUSwyykeAvVMccqe/pntxjVjx9S5tqSr+gnfvKiUsDGPnoDeR
qP9dGKMA94oAgfRFTdk1nYOASB2C+fakMRtstK/N8K3sOsTPsh4oo+0RAM+ErN6Z
MFFkY+K9hxrhEeuD19M0ro8/U+KoKcaaSVuLZHfcJiBKBklOHAhPAKzTsS9u1LuJ
YyMPV6MtYxCfgZi+xdxedAPV0hp4eKZBA38fN6aZGR42Tr2e4aOgnFKGAA9lgyGg
TfZeqaLcxpGTkL4vPSptVdDlU3a4kHcskeJ7/FasYdXOfVU09Awcg3kBEnGHpkmO
6PifuRgsJyfvdUgJPw1Kjgh2a2s0spmWfSrwIAbWTrtBHfg7Pcok7EqeJ8KNH4R1
UBckUbtCfbsE6E+AnTDbQEiZZOcrn8QYPlyztQGUoZUOikBbEdUzfiHdM9FHKjfi
BD7M+NCwaBmAwdyyN1w9qcbRk6VZm35V4hxC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