@Test public void testRevokeCertsWithShortHostnames() throws FreeIpaClientException { Set<String> hosts = Set.of( "test-wl-1-worker0", "test-wl-1-worker1", "test-wl-1-master2", "test-wl-1-compute3" ); Set<Cert> certs = Set.of( createCert("CN=test-wl-2-master2", 1, false), createCert("CN=test-wl-1-master2", 2, false), createCert("CN=test-wl-3-master1", 3, false), createCert("CN=test-datalake-1-master1", 4, false), createCert("CN=ipaserver0.env.xyz.wl.cloudera.site,O=ENV.XYZ.WL.CLOUDERA.SITE", 50, false) ); FreeIpaClient freeIpaClient = mock(FreeIpaClient.class); when(freeIpaClient.findAllCert()).thenReturn(certs); when(freeIpaClientFactory.getFreeIpaClientForStackId(STACK_ID)).thenReturn(freeIpaClient); Pair<Set<String>, Map<String, String>> result = cleanupService.revokeCerts(STACK_ID, hosts); verify(freeIpaClient, times(1)).revokeCert(2); verifyRevokeNotInvoked(freeIpaClient, 1, 3, 4, 50); assertEquals(1, result.getFirst().size()); assertEquals(0, result.getSecond().size()); assertTrue(result.getFirst().stream().allMatch("CN=test-wl-1-master2"::equals)); }
public Pair<Set<String>, Map<String, String>> revokeCerts(Long stackId, Set<String> hosts) throws FreeIpaClientException { FreeIpaClient client = getFreeIpaClient(stackId); Set<String> certCleanupSuccess = new HashSet<>(); Map<String, String> certCleanupFailed = new HashMap<>(); Set<Cert> certs = client.findAllCert(); certs.stream() .filter(cert -> hosts.stream().anyMatch(host -> substringBefore(host, ".").equals(substringBefore(removeStart(cert.getSubject(), "CN="), ".")))) .filter(cert -> !cert.isRevoked()) .forEach(cert -> { try { client.revokeCert(cert.getSerialNumber()); certCleanupSuccess.add(cert.getSubject()); } catch (FreeIpaClientException e) { LOGGER.error("Couldn't revoke certificate: {}", cert, e); certCleanupFailed.put(cert.getSubject(), e.getMessage()); } }); return Pair.of(certCleanupSuccess, certCleanupFailed); }
CleanupService { public Pair<Set<String>, Map<String, String>> revokeCerts(Long stackId, Set<String> hosts) throws FreeIpaClientException { FreeIpaClient client = getFreeIpaClient(stackId); Set<String> certCleanupSuccess = new HashSet<>(); Map<String, String> certCleanupFailed = new HashMap<>(); Set<Cert> certs = client.findAllCert(); certs.stream() .filter(cert -> hosts.stream().anyMatch(host -> substringBefore(host, ".").equals(substringBefore(removeStart(cert.getSubject(), "CN="), ".")))) .filter(cert -> !cert.isRevoked()) .forEach(cert -> { try { client.revokeCert(cert.getSerialNumber()); certCleanupSuccess.add(cert.getSubject()); } catch (FreeIpaClientException e) { LOGGER.error("Couldn't revoke certificate: {}", cert, e); certCleanupFailed.put(cert.getSubject(), e.getMessage()); } }); return Pair.of(certCleanupSuccess, certCleanupFailed); } }
CleanupService { public Pair<Set<String>, Map<String, String>> revokeCerts(Long stackId, Set<String> hosts) throws FreeIpaClientException { FreeIpaClient client = getFreeIpaClient(stackId); Set<String> certCleanupSuccess = new HashSet<>(); Map<String, String> certCleanupFailed = new HashMap<>(); Set<Cert> certs = client.findAllCert(); certs.stream() .filter(cert -> hosts.stream().anyMatch(host -> substringBefore(host, ".").equals(substringBefore(removeStart(cert.getSubject(), "CN="), ".")))) .filter(cert -> !cert.isRevoked()) .forEach(cert -> { try { client.revokeCert(cert.getSerialNumber()); certCleanupSuccess.add(cert.getSubject()); } catch (FreeIpaClientException e) { LOGGER.error("Couldn't revoke certificate: {}", cert, e); certCleanupFailed.put(cert.getSubject(), e.getMessage()); } }); return Pair.of(certCleanupSuccess, certCleanupFailed); }  }
CleanupService { public Pair<Set<String>, Map<String, String>> revokeCerts(Long stackId, Set<String> hosts) throws FreeIpaClientException { FreeIpaClient client = getFreeIpaClient(stackId); Set<String> certCleanupSuccess = new HashSet<>(); Map<String, String> certCleanupFailed = new HashMap<>(); Set<Cert> certs = client.findAllCert(); certs.stream() .filter(cert -> hosts.stream().anyMatch(host -> substringBefore(host, ".").equals(substringBefore(removeStart(cert.getSubject(), "CN="), ".")))) .filter(cert -> !cert.isRevoked()) .forEach(cert -> { try { client.revokeCert(cert.getSerialNumber()); certCleanupSuccess.add(cert.getSubject()); } catch (FreeIpaClientException e) { LOGGER.error("Couldn't revoke certificate: {}", cert, e); certCleanupFailed.put(cert.getSubject(), e.getMessage()); } }); return Pair.of(certCleanupSuccess, certCleanupFailed); }  OperationStatus cleanup(String accountId, CleanupRequest request); Pair<Set<String>, Map<String, String>> revokeCerts(Long stackId, Set<String> hosts); Pair<Set<String>, Map<String, String>> removeDnsEntries(Long stackId, Set<String> hosts, Set<String> ips, String domain); Pair<Set<String>, Map<String, String>> removeHosts(Long stackId, Set<String> hosts); Pair<Set<String>, Map<String, String>> removeServers(Long stackId, Set<String> hosts); Pair<Set<String>, Map<String, String>> removeUsers(Long stackId, Set<String> users, String clusterName, String environmentCrn); Pair<Set<String>, Map<String, String>> removeRoles(Long stackId, Set<String> roles); Pair<Set<String>, Map<String, String>> removeVaultEntries(Long stackId, Set<String> hosts); }
CleanupService { public Pair<Set<String>, Map<String, String>> revokeCerts(Long stackId, Set<String> hosts) throws FreeIpaClientException { FreeIpaClient client = getFreeIpaClient(stackId); Set<String> certCleanupSuccess = new HashSet<>(); Map<String, String> certCleanupFailed = new HashMap<>(); Set<Cert> certs = client.findAllCert(); certs.stream() .filter(cert -> hosts.stream().anyMatch(host -> substringBefore(host, ".").equals(substringBefore(removeStart(cert.getSubject(), "CN="), ".")))) .filter(cert -> !cert.isRevoked()) .forEach(cert -> { try { client.revokeCert(cert.getSerialNumber()); certCleanupSuccess.add(cert.getSubject()); } catch (FreeIpaClientException e) { LOGGER.error("Couldn't revoke certificate: {}", cert, e); certCleanupFailed.put(cert.getSubject(), e.getMessage()); } }); return Pair.of(certCleanupSuccess, certCleanupFailed); }  OperationStatus cleanup(String accountId, CleanupRequest request); Pair<Set<String>, Map<String, String>> revokeCerts(Long stackId, Set<String> hosts); Pair<Set<String>, Map<String, String>> removeDnsEntries(Long stackId, Set<String> hosts, Set<String> ips, String domain); Pair<Set<String>, Map<String, String>> removeHosts(Long stackId, Set<String> hosts); Pair<Set<String>, Map<String, String>> removeServers(Long stackId, Set<String> hosts); Pair<Set<String>, Map<String, String>> removeUsers(Long stackId, Set<String> users, String clusterName, String environmentCrn); Pair<Set<String>, Map<String, String>> removeRoles(Long stackId, Set<String> roles); Pair<Set<String>, Map<String, String>> removeVaultEntries(Long stackId, Set<String> hosts);  }