|
| 1 | +import javax.net.ssl.KeyManager; |
| 2 | +import javax.net.ssl.SSLContext; |
| 3 | +import javax.net.ssl.TrustManager; |
| 4 | +import javax.net.ssl.TrustManagerFactory; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.HttpURLConnection; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URL; |
| 10 | +import java.security.*; |
| 11 | +import java.security.cert.CertificateException; |
| 12 | + |
| 13 | +public class HttpsConnectionCheck { |
| 14 | + |
| 15 | + /* |
| 16 | + javac HttpsConnectionCheck.java |
| 17 | + java "https://elasticsearch.devops.vantage.org" "/home/projects/temp/myTrustStore" "storepass" "https_user" "https_passw" |
| 18 | + */ |
| 19 | + public static void main(String[] args) { |
| 20 | + String url = args[0]; |
| 21 | + String trustStoreFile = args[1]; |
| 22 | + String trustStorePassword = args[2]; |
| 23 | + String httpUsername = args[3]; |
| 24 | + String httpPassword = args[4]; |
| 25 | + |
| 26 | + System.out.println("attempt to connect to: " + url); |
| 27 | + try (FileInputStream truststoreFile = new FileInputStream(trustStoreFile)) { |
| 28 | + initSslContext(trustStorePassword, truststoreFile); |
| 29 | + } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException ex) { |
| 30 | + System.err.println("can't read properly data from remote host:" + ex.getMessage()); |
| 31 | + System.exit(1); |
| 32 | + } catch (KeyManagementException ex) { |
| 33 | + System.err.println("key management exception :" + ex.getMessage()); |
| 34 | + System.exit(2); |
| 35 | + } |
| 36 | + |
| 37 | + HttpURLConnection urlConnection = null; |
| 38 | + try { |
| 39 | + URL httpsUrl = new URL(url); |
| 40 | + String encoding = new sun.misc.BASE64Encoder().encode((httpUsername + ":" + httpPassword).getBytes()); |
| 41 | + urlConnection = (HttpURLConnection) httpsUrl.openConnection(); |
| 42 | + urlConnection.setRequestProperty("Authorization", "Basic " + encoding); |
| 43 | + urlConnection.connect(); |
| 44 | + |
| 45 | + System.out.println(urlConnection.getResponseCode()); |
| 46 | + } catch (MalformedURLException ex) { |
| 47 | + System.err.println("wrong url :" + ex.getMessage()); |
| 48 | + System.exit(3); |
| 49 | + } catch (IOException ex) { |
| 50 | + System.err.println("input/output exception: :" + ex.getMessage()); |
| 51 | + System.exit(4); |
| 52 | + } finally { |
| 53 | + urlConnection.disconnect(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static void initSslContext(String trustStorePassword, FileInputStream truststoreFile) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException { |
| 58 | + SSLContext.getInstance("TLSv1.2").init(new KeyManager[]{}, getTrustManagers(getKeyStore(trustStorePassword, truststoreFile)), new SecureRandom()); |
| 59 | + } |
| 60 | + |
| 61 | + private static KeyStore getKeyStore(String trustStorePassword, FileInputStream truststoreFile) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { |
| 62 | + KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 63 | + truststore.load(truststoreFile, trustStorePassword.toCharArray()); |
| 64 | + return truststore; |
| 65 | + } |
| 66 | + |
| 67 | + private static TrustManager[] getTrustManagers(KeyStore truststore) throws NoSuchAlgorithmException, KeyStoreException { |
| 68 | + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 69 | + trustManagerFactory.init(truststore); |
| 70 | + TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); |
| 71 | + return trustManagers; |
| 72 | + } |
| 73 | +} |
0 commit comments