|
19 | 19 | import org.elasticsearch.ElasticsearchException;
|
20 | 20 | import org.elasticsearch.common.CheckedRunnable;
|
21 | 21 | import org.elasticsearch.common.Strings;
|
| 22 | +import org.elasticsearch.common.SuppressForbidden; |
22 | 23 | import org.elasticsearch.common.settings.MockSecureSettings;
|
23 | 24 | import org.elasticsearch.common.settings.Settings;
|
24 | 25 | import org.elasticsearch.env.Environment;
|
|
35 | 36 | import javax.net.ssl.SSLContext;
|
36 | 37 | import javax.net.ssl.SSLEngine;
|
37 | 38 | import javax.net.ssl.SSLParameters;
|
| 39 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 40 | +import javax.net.ssl.SSLSession; |
| 41 | +import javax.net.ssl.SSLSessionContext; |
38 | 42 | import javax.net.ssl.SSLSocket;
|
39 | 43 | import javax.net.ssl.SSLSocketFactory;
|
40 | 44 | import javax.net.ssl.X509ExtendedTrustManager;
|
| 45 | +import javax.security.cert.X509Certificate; |
41 | 46 | import java.nio.file.Path;
|
42 | 47 | import java.security.AccessController;
|
| 48 | +import java.security.Principal; |
43 | 49 | import java.security.PrivilegedActionException;
|
44 | 50 | import java.security.PrivilegedExceptionAction;
|
| 51 | +import java.security.cert.Certificate; |
45 | 52 | import java.util.ArrayList;
|
46 | 53 | import java.util.Arrays;
|
47 | 54 | import java.util.Collections;
|
48 | 55 | import java.util.Comparator;
|
| 56 | +import java.util.Enumeration; |
| 57 | +import java.util.HashMap; |
49 | 58 | import java.util.Iterator;
|
50 | 59 | import java.util.List;
|
| 60 | +import java.util.Map; |
| 61 | +import java.util.concurrent.atomic.AtomicInteger; |
51 | 62 |
|
52 | 63 | import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
|
53 | 64 | import static org.hamcrest.Matchers.contains;
|
@@ -654,6 +665,57 @@ public void testReadCertificateInformation() throws Exception {
|
654 | 665 | assertFalse(iterator.hasNext());
|
655 | 666 | }
|
656 | 667 |
|
| 668 | + public void testSSLSessionInvalidationHandlesNullSessions() { |
| 669 | + final int numEntries = randomIntBetween(1, 32); |
| 670 | + final AtomicInteger invalidationCounter = new AtomicInteger(); |
| 671 | + int numNull = 0; |
| 672 | + final Map<byte[], SSLSession> sessionMap = new HashMap<>(); |
| 673 | + for (int i = 0; i < numEntries; i++) { |
| 674 | + final byte[] id = randomByteArrayOfLength(2); |
| 675 | + final SSLSession sslSession; |
| 676 | + if (rarely()) { |
| 677 | + sslSession = null; |
| 678 | + numNull++; |
| 679 | + } else { |
| 680 | + sslSession = new MockSSLSession(id, invalidationCounter::incrementAndGet); |
| 681 | + } |
| 682 | + sessionMap.put(id, sslSession); |
| 683 | + } |
| 684 | + |
| 685 | + SSLSessionContext sslSessionContext = new SSLSessionContext() { |
| 686 | + @Override |
| 687 | + public SSLSession getSession(byte[] sessionId) { |
| 688 | + return sessionMap.get(sessionId); |
| 689 | + } |
| 690 | + |
| 691 | + @Override |
| 692 | + public Enumeration<byte[]> getIds() { |
| 693 | + return Collections.enumeration(sessionMap.keySet()); |
| 694 | + } |
| 695 | + |
| 696 | + @Override |
| 697 | + public void setSessionTimeout(int seconds) throws IllegalArgumentException { |
| 698 | + } |
| 699 | + |
| 700 | + @Override |
| 701 | + public int getSessionTimeout() { |
| 702 | + return 0; |
| 703 | + } |
| 704 | + |
| 705 | + @Override |
| 706 | + public void setSessionCacheSize(int size) throws IllegalArgumentException { |
| 707 | + } |
| 708 | + |
| 709 | + @Override |
| 710 | + public int getSessionCacheSize() { |
| 711 | + return 0; |
| 712 | + } |
| 713 | + }; |
| 714 | + |
| 715 | + SSLService.invalidateSessions(sslSessionContext); |
| 716 | + assertEquals(numEntries - numNull, invalidationCounter.get()); |
| 717 | + } |
| 718 | + |
657 | 719 | @Network
|
658 | 720 | public void testThatSSLContextWithoutSettingsWorks() throws Exception {
|
659 | 721 | SSLService sslService = new SSLService(Settings.EMPTY, env);
|
@@ -761,4 +823,120 @@ private static void privilegedConnect(CheckedRunnable<Exception> runnable) throw
|
761 | 823 | }
|
762 | 824 | }
|
763 | 825 |
|
| 826 | + private static final class MockSSLSession implements SSLSession { |
| 827 | + |
| 828 | + private final byte[] id; |
| 829 | + private final Runnable invalidation; |
| 830 | + |
| 831 | + private MockSSLSession(byte[] id, Runnable invalidation) { |
| 832 | + this.id = id; |
| 833 | + this.invalidation = invalidation; |
| 834 | + } |
| 835 | + |
| 836 | + @Override |
| 837 | + public byte[] getId() { |
| 838 | + return id; |
| 839 | + } |
| 840 | + |
| 841 | + @Override |
| 842 | + public SSLSessionContext getSessionContext() { |
| 843 | + return null; |
| 844 | + } |
| 845 | + |
| 846 | + @Override |
| 847 | + public long getCreationTime() { |
| 848 | + return 0; |
| 849 | + } |
| 850 | + |
| 851 | + @Override |
| 852 | + public long getLastAccessedTime() { |
| 853 | + return 0; |
| 854 | + } |
| 855 | + |
| 856 | + @Override |
| 857 | + public void invalidate() { |
| 858 | + invalidation.run(); |
| 859 | + } |
| 860 | + |
| 861 | + @Override |
| 862 | + public boolean isValid() { |
| 863 | + return false; |
| 864 | + } |
| 865 | + |
| 866 | + @Override |
| 867 | + public void putValue(String name, Object value) { |
| 868 | + |
| 869 | + } |
| 870 | + |
| 871 | + @Override |
| 872 | + public Object getValue(String name) { |
| 873 | + return null; |
| 874 | + } |
| 875 | + |
| 876 | + @Override |
| 877 | + public void removeValue(String name) { |
| 878 | + |
| 879 | + } |
| 880 | + |
| 881 | + @Override |
| 882 | + public String[] getValueNames() { |
| 883 | + return new String[0]; |
| 884 | + } |
| 885 | + |
| 886 | + @Override |
| 887 | + public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException { |
| 888 | + return new Certificate[0]; |
| 889 | + } |
| 890 | + |
| 891 | + @Override |
| 892 | + public Certificate[] getLocalCertificates() { |
| 893 | + return new Certificate[0]; |
| 894 | + } |
| 895 | + |
| 896 | + @SuppressForbidden(reason = "need to reference deprecated class to implement JDK interface") |
| 897 | + @Override |
| 898 | + public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException { |
| 899 | + return new X509Certificate[0]; |
| 900 | + } |
| 901 | + |
| 902 | + @Override |
| 903 | + public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { |
| 904 | + return null; |
| 905 | + } |
| 906 | + |
| 907 | + @Override |
| 908 | + public Principal getLocalPrincipal() { |
| 909 | + return null; |
| 910 | + } |
| 911 | + |
| 912 | + @Override |
| 913 | + public String getCipherSuite() { |
| 914 | + return null; |
| 915 | + } |
| 916 | + |
| 917 | + @Override |
| 918 | + public String getProtocol() { |
| 919 | + return null; |
| 920 | + } |
| 921 | + |
| 922 | + @Override |
| 923 | + public String getPeerHost() { |
| 924 | + return null; |
| 925 | + } |
| 926 | + |
| 927 | + @Override |
| 928 | + public int getPeerPort() { |
| 929 | + return 0; |
| 930 | + } |
| 931 | + |
| 932 | + @Override |
| 933 | + public int getPacketBufferSize() { |
| 934 | + return 0; |
| 935 | + } |
| 936 | + |
| 937 | + @Override |
| 938 | + public int getApplicationBufferSize() { |
| 939 | + return 0; |
| 940 | + } |
| 941 | + } |
764 | 942 | }
|
0 commit comments