@Test public void testGetBestPasswordOneNonRoot() { Set<Password> passwords = Sets.newLinkedHashSet(); passwords.add(new Password(1, "nonroot", "word")); VirtualGuestToNodeMetadata f = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention, virtualGuestToImage, virtualGuestToHardware); Password best = f.getBestPassword(passwords, null); assertEquals(best.getUsername(), "nonroot"); }
@VisibleForTesting Password getBestPassword(Set<Password> passwords, VirtualGuest context) { if (passwords == null || passwords.isEmpty()) { throw new IllegalStateException("No credentials declared for " + context); } if (passwords.size() == 1) { return Iterables.getOnlyElement(passwords); } Password bestPassword = null; Set<Password> alternates = Sets.newLinkedHashSet(); int bestScore = -1; for (Password p : passwords) { int score = -1; if ("root".equals(p.getUsername())) score = 10; else if ("root".equalsIgnoreCase(p.getUsername())) score = 4; else if ("ubuntu".equals(p.getUsername())) score = 8; else if ("ubuntu".equalsIgnoreCase(p.getUsername())) score = 3; else if ("administrator".equals(p.getUsername())) score = 5; else if ("administrator".equalsIgnoreCase(p.getUsername())) score = 2; else if (p.getUsername() != null && p.getUsername().length() > 1) score = 1; if (score > 0) { if (score > bestScore) { bestPassword = p; alternates.clear(); bestScore = score; } else if (score == bestScore) { alternates.add(p); } } } if (bestPassword == null) { throw new IllegalStateException("No valid credentials available for " + context + "; found: " + passwords); } if (!alternates.isEmpty()) { logger.warn("Multiple credentials for " + bestPassword.getUsername() + "@" + context + "; using first declared " + bestPassword + " and ignoring " + alternates); } else { logger.debug("Multiple credentials for " + context + "; using preferred username " + bestPassword.getUsername()); } return bestPassword; }
VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMetadata> { @VisibleForTesting Password getBestPassword(Set<Password> passwords, VirtualGuest context) { if (passwords == null || passwords.isEmpty()) { throw new IllegalStateException("No credentials declared for " + context); } if (passwords.size() == 1) { return Iterables.getOnlyElement(passwords); } Password bestPassword = null; Set<Password> alternates = Sets.newLinkedHashSet(); int bestScore = -1; for (Password p : passwords) { int score = -1; if ("root".equals(p.getUsername())) score = 10; else if ("root".equalsIgnoreCase(p.getUsername())) score = 4; else if ("ubuntu".equals(p.getUsername())) score = 8; else if ("ubuntu".equalsIgnoreCase(p.getUsername())) score = 3; else if ("administrator".equals(p.getUsername())) score = 5; else if ("administrator".equalsIgnoreCase(p.getUsername())) score = 2; else if (p.getUsername() != null && p.getUsername().length() > 1) score = 1; if (score > 0) { if (score > bestScore) { bestPassword = p; alternates.clear(); bestScore = score; } else if (score == bestScore) { alternates.add(p); } } } if (bestPassword == null) { throw new IllegalStateException("No valid credentials available for " + context + "; found: " + passwords); } if (!alternates.isEmpty()) { logger.warn("Multiple credentials for " + bestPassword.getUsername() + "@" + context + "; using first declared " + bestPassword + " and ignoring " + alternates); } else { logger.debug("Multiple credentials for " + context + "; using preferred username " + bestPassword.getUsername()); } return bestPassword; } }
VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMetadata> { @VisibleForTesting Password getBestPassword(Set<Password> passwords, VirtualGuest context) { if (passwords == null || passwords.isEmpty()) { throw new IllegalStateException("No credentials declared for " + context); } if (passwords.size() == 1) { return Iterables.getOnlyElement(passwords); } Password bestPassword = null; Set<Password> alternates = Sets.newLinkedHashSet(); int bestScore = -1; for (Password p : passwords) { int score = -1; if ("root".equals(p.getUsername())) score = 10; else if ("root".equalsIgnoreCase(p.getUsername())) score = 4; else if ("ubuntu".equals(p.getUsername())) score = 8; else if ("ubuntu".equalsIgnoreCase(p.getUsername())) score = 3; else if ("administrator".equals(p.getUsername())) score = 5; else if ("administrator".equalsIgnoreCase(p.getUsername())) score = 2; else if (p.getUsername() != null && p.getUsername().length() > 1) score = 1; if (score > 0) { if (score > bestScore) { bestPassword = p; alternates.clear(); bestScore = score; } else if (score == bestScore) { alternates.add(p); } } } if (bestPassword == null) { throw new IllegalStateException("No valid credentials available for " + context + "; found: " + passwords); } if (!alternates.isEmpty()) { logger.warn("Multiple credentials for " + bestPassword.getUsername() + "@" + context + "; using first declared " + bestPassword + " and ignoring " + alternates); } else { logger.debug("Multiple credentials for " + context + "; using preferred username " + bestPassword.getUsername()); } return bestPassword; } @Inject  VirtualGuestToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
         GroupNamingConvention.Factory namingConvention, VirtualGuestToImage virtualGuestToImage,
         VirtualGuestToHardware virtualGuestToHardware); }
VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMetadata> { @VisibleForTesting Password getBestPassword(Set<Password> passwords, VirtualGuest context) { if (passwords == null || passwords.isEmpty()) { throw new IllegalStateException("No credentials declared for " + context); } if (passwords.size() == 1) { return Iterables.getOnlyElement(passwords); } Password bestPassword = null; Set<Password> alternates = Sets.newLinkedHashSet(); int bestScore = -1; for (Password p : passwords) { int score = -1; if ("root".equals(p.getUsername())) score = 10; else if ("root".equalsIgnoreCase(p.getUsername())) score = 4; else if ("ubuntu".equals(p.getUsername())) score = 8; else if ("ubuntu".equalsIgnoreCase(p.getUsername())) score = 3; else if ("administrator".equals(p.getUsername())) score = 5; else if ("administrator".equalsIgnoreCase(p.getUsername())) score = 2; else if (p.getUsername() != null && p.getUsername().length() > 1) score = 1; if (score > 0) { if (score > bestScore) { bestPassword = p; alternates.clear(); bestScore = score; } else if (score == bestScore) { alternates.add(p); } } } if (bestPassword == null) { throw new IllegalStateException("No valid credentials available for " + context + "; found: " + passwords); } if (!alternates.isEmpty()) { logger.warn("Multiple credentials for " + bestPassword.getUsername() + "@" + context + "; using first declared " + bestPassword + " and ignoring " + alternates); } else { logger.debug("Multiple credentials for " + context + "; using preferred username " + bestPassword.getUsername()); } return bestPassword; } @Inject  VirtualGuestToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
         GroupNamingConvention.Factory namingConvention, VirtualGuestToImage virtualGuestToImage,
         VirtualGuestToHardware virtualGuestToHardware); @Override NodeMetadata apply(VirtualGuest from); }
VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMetadata> { @VisibleForTesting Password getBestPassword(Set<Password> passwords, VirtualGuest context) { if (passwords == null || passwords.isEmpty()) { throw new IllegalStateException("No credentials declared for " + context); } if (passwords.size() == 1) { return Iterables.getOnlyElement(passwords); } Password bestPassword = null; Set<Password> alternates = Sets.newLinkedHashSet(); int bestScore = -1; for (Password p : passwords) { int score = -1; if ("root".equals(p.getUsername())) score = 10; else if ("root".equalsIgnoreCase(p.getUsername())) score = 4; else if ("ubuntu".equals(p.getUsername())) score = 8; else if ("ubuntu".equalsIgnoreCase(p.getUsername())) score = 3; else if ("administrator".equals(p.getUsername())) score = 5; else if ("administrator".equalsIgnoreCase(p.getUsername())) score = 2; else if (p.getUsername() != null && p.getUsername().length() > 1) score = 1; if (score > 0) { if (score > bestScore) { bestPassword = p; alternates.clear(); bestScore = score; } else if (score == bestScore) { alternates.add(p); } } } if (bestPassword == null) { throw new IllegalStateException("No valid credentials available for " + context + "; found: " + passwords); } if (!alternates.isEmpty()) { logger.warn("Multiple credentials for " + bestPassword.getUsername() + "@" + context + "; using first declared " + bestPassword + " and ignoring " + alternates); } else { logger.debug("Multiple credentials for " + context + "; using preferred username " + bestPassword.getUsername()); } return bestPassword; } @Inject  VirtualGuestToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
         GroupNamingConvention.Factory namingConvention, VirtualGuestToImage virtualGuestToImage,
         VirtualGuestToHardware virtualGuestToHardware); @Override NodeMetadata apply(VirtualGuest from); static final Map<VirtualGuest.State, Status> serverStateToNodeStatus; }