@Test @Ignore public void testInternetWebsite() throws UnknownHostException { SSLCertMonitor monitor = new SSLCertMonitor(); Map<String, Object> parameters = new ConcurrentSkipListMap<String, Object>(); parameters.put("port", "443"); parameters.put("retry", "0"); parameters.put("timeout", "500"); parameters.put("verbose", "true"); parameters.put("days", "5"); MonitoredService svc = MonitorTestUtils.getMonitoredService(3, "www.google.com", "SSLCert", false); PollStatus status = monitor.poll(svc, parameters); assertTrue(status.isAvailable()); }
@Override public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("Required parameter 'port' is not present in supplied properties."); } int validityDays = ParameterMap.getKeyedInteger(parameters, PARAMETER_DAYS, DEFAULT_DAYS); if (validityDays <= 0) { throw new RuntimeException("Required parameter 'days' must be a positive value."); } Calendar calValid = GregorianCalendar.getInstance(); Calendar calCurrent = GregorianCalendar.getInstance(); calValid.setTimeInMillis(calCurrent.getTimeInMillis()); calValid.add(Calendar.DAY_OF_MONTH, validityDays); Calendar calBefore = GregorianCalendar.getInstance(); Calendar calAfter = GregorianCalendar.getInstance(); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); log().debug("poll: address=" + hostAddress + ", port=" + port + ", " + tracker); PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { Socket socket = null; try { tracker.startAttempt(); socket = new Socket(); socket.connect(new InetSocketAddress(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("Connected to host: " + ipv4Addr + " on port: " + port); SSLSocket sslSocket = (SSLSocket) getSocketWrapper().wrapSocket(socket); serviceStatus = PollStatus.unresponsive(); Certificate[] certs = sslSocket.getSession().getPeerCertificates(); for (int i = 0; i < certs.length && !serviceStatus.isAvailable(); i++) { if (certs[i] instanceof X509Certificate) { X509Certificate certx = (X509Certificate) certs[i]; log().debug("Checking validity against dates: [current: " + calCurrent.getTime() + ", valid: " + calValid.getTime() +"], NotBefore: " + certx.getNotBefore() + ", NotAfter: " + certx.getNotAfter()); calBefore.setTime(certx.getNotBefore()); calAfter.setTime(certx.getNotAfter()); if (calCurrent.before(calBefore)) { serviceStatus = logDown(Level.WARN, "Certificate is invalid, current time is before start time"); break; } else if (calCurrent.before(calAfter)) { if (calValid.before(calAfter)) { serviceStatus = logUp(Level.DEBUG, tracker.elapsedTimeInMillis(), "Certificate is valid, and does not expire before validity check date"); break; } else { serviceStatus = logDown(Level.ERROR, "Certificate is valid, but will expire in " + validityDays + " days."); break; } } else { serviceStatus = logDown(Level.ERROR, "Certificate has expired."); break; } } } } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) { socket.close(); } } catch (IOException e) { e.fillInStackTrace(); log().debug("poll: Error closing socket." + e); } } } return serviceStatus; }
SSLCertMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("Required parameter 'port' is not present in supplied properties."); } int validityDays = ParameterMap.getKeyedInteger(parameters, PARAMETER_DAYS, DEFAULT_DAYS); if (validityDays <= 0) { throw new RuntimeException("Required parameter 'days' must be a positive value."); } Calendar calValid = GregorianCalendar.getInstance(); Calendar calCurrent = GregorianCalendar.getInstance(); calValid.setTimeInMillis(calCurrent.getTimeInMillis()); calValid.add(Calendar.DAY_OF_MONTH, validityDays); Calendar calBefore = GregorianCalendar.getInstance(); Calendar calAfter = GregorianCalendar.getInstance(); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); log().debug("poll: address=" + hostAddress + ", port=" + port + ", " + tracker); PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { Socket socket = null; try { tracker.startAttempt(); socket = new Socket(); socket.connect(new InetSocketAddress(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("Connected to host: " + ipv4Addr + " on port: " + port); SSLSocket sslSocket = (SSLSocket) getSocketWrapper().wrapSocket(socket); serviceStatus = PollStatus.unresponsive(); Certificate[] certs = sslSocket.getSession().getPeerCertificates(); for (int i = 0; i < certs.length && !serviceStatus.isAvailable(); i++) { if (certs[i] instanceof X509Certificate) { X509Certificate certx = (X509Certificate) certs[i]; log().debug("Checking validity against dates: [current: " + calCurrent.getTime() + ", valid: " + calValid.getTime() +"], NotBefore: " + certx.getNotBefore() + ", NotAfter: " + certx.getNotAfter()); calBefore.setTime(certx.getNotBefore()); calAfter.setTime(certx.getNotAfter()); if (calCurrent.before(calBefore)) { serviceStatus = logDown(Level.WARN, "Certificate is invalid, current time is before start time"); break; } else if (calCurrent.before(calAfter)) { if (calValid.before(calAfter)) { serviceStatus = logUp(Level.DEBUG, tracker.elapsedTimeInMillis(), "Certificate is valid, and does not expire before validity check date"); break; } else { serviceStatus = logDown(Level.ERROR, "Certificate is valid, but will expire in " + validityDays + " days."); break; } } else { serviceStatus = logDown(Level.ERROR, "Certificate has expired."); break; } } } } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) { socket.close(); } } catch (IOException e) { e.fillInStackTrace(); log().debug("poll: Error closing socket." + e); } } } return serviceStatus; } }
SSLCertMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("Required parameter 'port' is not present in supplied properties."); } int validityDays = ParameterMap.getKeyedInteger(parameters, PARAMETER_DAYS, DEFAULT_DAYS); if (validityDays <= 0) { throw new RuntimeException("Required parameter 'days' must be a positive value."); } Calendar calValid = GregorianCalendar.getInstance(); Calendar calCurrent = GregorianCalendar.getInstance(); calValid.setTimeInMillis(calCurrent.getTimeInMillis()); calValid.add(Calendar.DAY_OF_MONTH, validityDays); Calendar calBefore = GregorianCalendar.getInstance(); Calendar calAfter = GregorianCalendar.getInstance(); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); log().debug("poll: address=" + hostAddress + ", port=" + port + ", " + tracker); PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { Socket socket = null; try { tracker.startAttempt(); socket = new Socket(); socket.connect(new InetSocketAddress(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("Connected to host: " + ipv4Addr + " on port: " + port); SSLSocket sslSocket = (SSLSocket) getSocketWrapper().wrapSocket(socket); serviceStatus = PollStatus.unresponsive(); Certificate[] certs = sslSocket.getSession().getPeerCertificates(); for (int i = 0; i < certs.length && !serviceStatus.isAvailable(); i++) { if (certs[i] instanceof X509Certificate) { X509Certificate certx = (X509Certificate) certs[i]; log().debug("Checking validity against dates: [current: " + calCurrent.getTime() + ", valid: " + calValid.getTime() +"], NotBefore: " + certx.getNotBefore() + ", NotAfter: " + certx.getNotAfter()); calBefore.setTime(certx.getNotBefore()); calAfter.setTime(certx.getNotAfter()); if (calCurrent.before(calBefore)) { serviceStatus = logDown(Level.WARN, "Certificate is invalid, current time is before start time"); break; } else if (calCurrent.before(calAfter)) { if (calValid.before(calAfter)) { serviceStatus = logUp(Level.DEBUG, tracker.elapsedTimeInMillis(), "Certificate is valid, and does not expire before validity check date"); break; } else { serviceStatus = logDown(Level.ERROR, "Certificate is valid, but will expire in " + validityDays + " days."); break; } } else { serviceStatus = logDown(Level.ERROR, "Certificate has expired."); break; } } } } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) { socket.close(); } } catch (IOException e) { e.fillInStackTrace(); log().debug("poll: Error closing socket." + e); } } } return serviceStatus; }  }
SSLCertMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("Required parameter 'port' is not present in supplied properties."); } int validityDays = ParameterMap.getKeyedInteger(parameters, PARAMETER_DAYS, DEFAULT_DAYS); if (validityDays <= 0) { throw new RuntimeException("Required parameter 'days' must be a positive value."); } Calendar calValid = GregorianCalendar.getInstance(); Calendar calCurrent = GregorianCalendar.getInstance(); calValid.setTimeInMillis(calCurrent.getTimeInMillis()); calValid.add(Calendar.DAY_OF_MONTH, validityDays); Calendar calBefore = GregorianCalendar.getInstance(); Calendar calAfter = GregorianCalendar.getInstance(); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); log().debug("poll: address=" + hostAddress + ", port=" + port + ", " + tracker); PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { Socket socket = null; try { tracker.startAttempt(); socket = new Socket(); socket.connect(new InetSocketAddress(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("Connected to host: " + ipv4Addr + " on port: " + port); SSLSocket sslSocket = (SSLSocket) getSocketWrapper().wrapSocket(socket); serviceStatus = PollStatus.unresponsive(); Certificate[] certs = sslSocket.getSession().getPeerCertificates(); for (int i = 0; i < certs.length && !serviceStatus.isAvailable(); i++) { if (certs[i] instanceof X509Certificate) { X509Certificate certx = (X509Certificate) certs[i]; log().debug("Checking validity against dates: [current: " + calCurrent.getTime() + ", valid: " + calValid.getTime() +"], NotBefore: " + certx.getNotBefore() + ", NotAfter: " + certx.getNotAfter()); calBefore.setTime(certx.getNotBefore()); calAfter.setTime(certx.getNotAfter()); if (calCurrent.before(calBefore)) { serviceStatus = logDown(Level.WARN, "Certificate is invalid, current time is before start time"); break; } else if (calCurrent.before(calAfter)) { if (calValid.before(calAfter)) { serviceStatus = logUp(Level.DEBUG, tracker.elapsedTimeInMillis(), "Certificate is valid, and does not expire before validity check date"); break; } else { serviceStatus = logDown(Level.ERROR, "Certificate is valid, but will expire in " + validityDays + " days."); break; } } else { serviceStatus = logDown(Level.ERROR, "Certificate has expired."); break; } } } } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) { socket.close(); } } catch (IOException e) { e.fillInStackTrace(); log().debug("poll: Error closing socket." + e); } } } return serviceStatus; }  @Override PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters); }
SSLCertMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("Required parameter 'port' is not present in supplied properties."); } int validityDays = ParameterMap.getKeyedInteger(parameters, PARAMETER_DAYS, DEFAULT_DAYS); if (validityDays <= 0) { throw new RuntimeException("Required parameter 'days' must be a positive value."); } Calendar calValid = GregorianCalendar.getInstance(); Calendar calCurrent = GregorianCalendar.getInstance(); calValid.setTimeInMillis(calCurrent.getTimeInMillis()); calValid.add(Calendar.DAY_OF_MONTH, validityDays); Calendar calBefore = GregorianCalendar.getInstance(); Calendar calAfter = GregorianCalendar.getInstance(); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); log().debug("poll: address=" + hostAddress + ", port=" + port + ", " + tracker); PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { Socket socket = null; try { tracker.startAttempt(); socket = new Socket(); socket.connect(new InetSocketAddress(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("Connected to host: " + ipv4Addr + " on port: " + port); SSLSocket sslSocket = (SSLSocket) getSocketWrapper().wrapSocket(socket); serviceStatus = PollStatus.unresponsive(); Certificate[] certs = sslSocket.getSession().getPeerCertificates(); for (int i = 0; i < certs.length && !serviceStatus.isAvailable(); i++) { if (certs[i] instanceof X509Certificate) { X509Certificate certx = (X509Certificate) certs[i]; log().debug("Checking validity against dates: [current: " + calCurrent.getTime() + ", valid: " + calValid.getTime() +"], NotBefore: " + certx.getNotBefore() + ", NotAfter: " + certx.getNotAfter()); calBefore.setTime(certx.getNotBefore()); calAfter.setTime(certx.getNotAfter()); if (calCurrent.before(calBefore)) { serviceStatus = logDown(Level.WARN, "Certificate is invalid, current time is before start time"); break; } else if (calCurrent.before(calAfter)) { if (calValid.before(calAfter)) { serviceStatus = logUp(Level.DEBUG, tracker.elapsedTimeInMillis(), "Certificate is valid, and does not expire before validity check date"); break; } else { serviceStatus = logDown(Level.ERROR, "Certificate is valid, but will expire in " + validityDays + " days."); break; } } else { serviceStatus = logDown(Level.ERROR, "Certificate has expired."); break; } } } } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) { socket.close(); } } catch (IOException e) { e.fillInStackTrace(); log().debug("poll: Error closing socket." + e); } } } return serviceStatus; }  @Override PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters); static final String PARAMETER_PORT; static final String PARAMETER_DAYS; }