InstanceCertManager { void updateSSHHostPrincipals(SshHostCsr sshHostCsr, SSHCertRecord sshCertRecord) { if (sshHostCsr == null) { sshCertRecord.setPrincipals("127.0.0.1"); return; } String recordPrincipals = ""; String[] principals = sshHostCsr.getPrincipals(); String[] xprincipals = sshHostCsr.getXPrincipals(); if (principals != null || xprincipals != null) { Set<String> principalSet = new HashSet<>(); if (principals != null) { principalSet.addAll(Arrays.asList(principals)); } if (xprincipals != null) { principalSet.addAll(Arrays.asList(xprincipals)); } if (!principalSet.isEmpty()) { recordPrincipals = String.join(",", principalSet); } } if (recordPrincipals.isEmpty()) { sshCertRecord.setPrincipals("127.0.0.1"); } else { sshCertRecord.setPrincipals(recordPrincipals); } } InstanceCertManager(final PrivateKeyStore keyStore, Authorizer authorizer, HostnameResolver hostnameResolver,
            boolean readOnlyMode); void setCertStore(CertRecordStore certStore); void setSSHStore(SSHRecordStore sshStore); CertificateAuthorityBundle getCertificateAuthorityBundle(final String name); List<X509CertRecord> getUnrefreshedCertsNotifications(String serverHostName, String provider); X509CertRecord getX509CertRecord(final String provider, X509Certificate cert); X509CertRecord getX509CertRecord(final String provider, final String instanceId,
            final String service); boolean updateX509CertRecord(X509CertRecord certRecord); boolean deleteX509CertRecord(final String provider, final String instanceId,
            final String service); boolean insertX509CertRecord(X509CertRecord certRecord); String generateX509Certificate(final String csr, final String keyUsage, int expiryTime); String getCACertificate(); InstanceIdentity generateIdentity(String csr, String cn, String keyUsage,
            int expiryTime); String getX509CertificateSigner(); SSHCertificates generateSSHCertificates(Principal principal, SSHCertRequest certRequest); boolean generateSSHIdentity(Principal principal, InstanceIdentity identity, final String hostname,
            final String csr, SSHCertRecord sshCertRecord, final String certType); boolean authorizeLaunch(Principal providerService, String domain, String service,
            StringBuilder errorMsg); boolean verifyCertRefreshIPAddress(final String ipAddress); boolean verifyInstanceCertIPAddress(final String provider, final String ipAddress); boolean validPrincipals(final String hostname, SSHCertRecord sshCertRecord, SshHostCsr sshHostCsr); void logX509Cert(final Principal principal, final String ip, final String provider,
            final String instanceId, final X509Certificate x509Cert); SSHCertRecord getSSHCertRecord(final String instanceId, final String service); boolean updateSSHCertRecord(SSHCertRecord certRecord, boolean refresh); boolean enableCertStoreNotifications(NotificationManager notificationManager, RolesProvider rolesProvider, String serverName); boolean enableSSHStoreNotifications(NotificationManager notificationManager, RolesProvider rolesProvider, String serverName);  }
@Test public void testUpdateSSHHostPrincipals() { InstanceCertManager instance = new InstanceCertManager(null, null, null, true); SSHCertRecord record = new SSHCertRecord(); instance.updateSSHHostPrincipals(null, record); assertEquals(record.getPrincipals(), "127.0.0.1"); record.setPrincipals(null); SshHostCsr csr = new SshHostCsr(); instance.updateSSHHostPrincipals(csr, record); assertEquals(record.getPrincipals(), "127.0.0.1"); record.setPrincipals(null); String[] principals = new String[1]; principals[0] = "principal1"; csr.setPrincipals(principals); String[] xprincipals = new String[2]; xprincipals[0] = "xprincipal1"; xprincipals[1] = "xprincipal2"; csr.setXPrincipals(xprincipals); instance.updateSSHHostPrincipals(csr, record); Set<String> result = new HashSet<>(Arrays.asList(record.getPrincipals().split(","))); assertEquals(result.size(), 3); assertTrue(result.contains("principal1")); assertTrue(result.contains("xprincipal1")); assertTrue(result.contains("xprincipal2")); }
@Test public void testUpdateSSHHostPrincipalsDuplicateValues() { InstanceCertManager instance = new InstanceCertManager(null, null, null, true); SshHostCsr csr = new SshHostCsr(); String[] principals = new String[3]; principals[0] = "principal1"; principals[1] = "principal2"; principals[2] = "principal4"; csr.setPrincipals(principals); String[] xprincipals = new String[3]; xprincipals[0] = "principal1"; xprincipals[1] = "principal2"; xprincipals[2] = "principal5"; csr.setXPrincipals(xprincipals); SSHCertRecord record = new SSHCertRecord(); instance.updateSSHHostPrincipals(csr, record); Set<String> result = new HashSet<>(Arrays.asList(record.getPrincipals().split(","))); assertEquals(result.size(), 4); assertTrue(result.contains("principal1")); assertTrue(result.contains("principal2")); assertTrue(result.contains("principal4")); assertTrue(result.contains("principal5")); }
@Test public void testUpdateSSHHostPrincipalsNullValues() { InstanceCertManager instance = new InstanceCertManager(null, null, null, true); SshHostCsr csr = new SshHostCsr(); csr.setPrincipals(new String[0]); csr.setPrincipals(new String[0]); SSHCertRecord record = new SSHCertRecord(); instance.updateSSHHostPrincipals(csr, record); assertEquals(record.getPrincipals(), "127.0.0.1"); String[] principals = new String[3]; principals[0] = "principal1"; principals[1] = "principal2"; principals[2] = "principal4"; csr.setPrincipals(principals); csr.setXPrincipals(null); record = new SSHCertRecord(); instance.updateSSHHostPrincipals(csr, record); Set<String> result = new HashSet<>(Arrays.asList(record.getPrincipals().split(","))); assertEquals(result.size(), 3); assertTrue(result.contains("principal1")); assertTrue(result.contains("principal2")); assertTrue(result.contains("principal4")); csr.setPrincipals(null); csr.setXPrincipals(principals); record = new SSHCertRecord(); instance.updateSSHHostPrincipals(csr, record); result = new HashSet<>(Arrays.asList(record.getPrincipals().split(","))); assertEquals(result.size(), 3); assertTrue(result.contains("principal1")); assertTrue(result.contains("principal2")); assertTrue(result.contains("principal4")); }