ListCertificatesController extends AbstractTomcatContainerController { public List<Cert> getCertificates(String storeType, String storeFile, String storePassword) throws Exception { KeyStore keyStore; if (storeType != null) { keyStore = KeyStore.getInstance(storeType); } else { keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); } char[] password = null; if (storePassword != null) { password = storePassword.toCharArray(); } try (InputStream storeInput = getStoreInputStream(storeFile)) { keyStore.load(storeInput, password); } catch (IOException e) { logger.error("Error loading store file {}", storeFile, e); return null; } List<Cert> certs = new ArrayList<>(); for (String alias : Collections.list(keyStore.aliases())) { Certificate[] certificateChains = keyStore.getCertificateChain(alias); if (certificateChains != null) { for (Certificate certificateChain : certificateChains) { X509Certificate x509Cert = (X509Certificate) certificateChain; addToStore(certs, alias, x509Cert); } } else { X509Certificate x509Cert = (X509Certificate) keyStore.getCertificate(alias); addToStore(certs, alias, x509Cert); } } return certs; }  @RequestMapping(path = "/certificates.htm") @Override ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response); List<Cert> getCertificates(String storeType, String storeFile, String storePassword); @Value("certificates") @Override void setViewName(String viewName);  }
@Test public void testGetCertificates() throws Exception { ListCertificatesController controller = new ListCertificatesController(); String storeType = "jks"; File storeFile = ctx.getResource("classpath:certs/localhost-truststore.jks").getFile(); String storePassword = "123456"; List<Cert> certs = controller.getCertificates(storeType, storeFile.toString(), storePassword); assertThat(certs).doesNotContainNull(); assertThat(certs.size()).isEqualTo(2); assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com"); assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017"); }
@Test public void testGetCertificatesRelative() throws Exception { ListCertificatesController controller = new ListCertificatesController(); String storeType = "jks"; File certFolder = ctx.getResource("classpath:certs").getFile(); System.setProperty("catalina.base", certFolder.getPath()); String storePassword = "123456"; List<Cert> certs = controller.getCertificates(storeType, "localhost-truststore.jks", storePassword); assertThat(certs).doesNotContainNull(); assertThat(certs.size()).isEqualTo(2); assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com"); assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017"); }
@Test public void testGetCertificatesRelativeUri() throws Exception { ListCertificatesController controller = new ListCertificatesController(); String storeType = "jks"; File storeFile = ctx.getResource("classpath:certs/localhost-truststore.jks").getFile(); File certFolder = ctx.getResource("classpath:certs").getFile(); System.setProperty("catalina.base", certFolder.getPath()); String storePassword = "123456"; List<Cert> certs = controller.getCertificates(storeType, storeFile.toURI().toString(), storePassword); assertThat(certs).doesNotContainNull(); assertThat(certs.size()).isEqualTo(2); assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com"); assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017"); }
@Test public void testGetCertificatesAbsoluteUri() throws Exception { ListCertificatesController controller = new ListCertificatesController(); String storeType = "jks"; File certFolder = ctx.getResource("classpath:certs").getFile(); System.setProperty("catalina.base", certFolder.getPath()); String storePassword = "123456"; List<Cert> certs = controller.getCertificates(storeType, "./localhost-truststore.jks", storePassword); assertThat(certs).doesNotContainNull(); assertThat(certs.size()).isEqualTo(2); assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com"); assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017"); }