AmbariAuthToLocalUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String principal) throws UsernameNotFoundException { try { String username; synchronized (KerberosName.class) { KerberosName.setRules(authToLocalRules); username = new KerberosName(principal).getShortName(); } if (username == null) { String message = String.format("Failed to translate %s to a local username during Kerberos authentication.", principal); LOG.warn(message); throw new UsernameNotFoundException(message); } LOG.info("Translated {} to {} using auth-to-local rules during Kerberos authentication.", principal, username); return createUser(username); } catch (IOException e) { String message = String.format("Failed to translate %s to a local username during Kerberos authentication: %s", principal, e.getLocalizedMessage()); LOG.warn(message); throw new UsernameNotFoundException(message, e); } } AmbariAuthToLocalUserDetailsService(Configuration configuration, Users users); @Override UserDetails loadUserByUsername(String principal);  }
@Test public void loadUserByUsernameSuccess() throws Exception { AmbariKerberosAuthenticationProperties properties = new AmbariKerberosAuthenticationProperties(); Configuration configuration = createMock(Configuration.class); expect(configuration.getKerberosAuthenticationProperties()).andReturn(properties).once(); User user = createMock(User.class); expect(user.getUserName()).andReturn("user1").once(); expect(user.getUserType()).andReturn(UserType.LDAP).once(); Collection<AmbariGrantedAuthority> userAuthorities = Collections.singletonList(createNiceMock(AmbariGrantedAuthority.class)); Users users = createMock(Users.class); expect(users.getUser("user1", UserType.LDAP)).andReturn(user).once(); expect(users.getUserAuthorities("user1", UserType.LDAP)).andReturn(userAuthorities).once(); replayAll(); UserDetailsService userdetailsService = new AmbariAuthToLocalUserDetailsService(configuration, users); UserDetails userDetails = userdetailsService.loadUserByUsername("user1@EXAMPLE.COM"); verifyAll(); Assert.assertNotNull(userDetails); Assert.assertEquals("user1", userDetails.getUsername()); Assert.assertEquals(userAuthorities.size(), userDetails.getAuthorities().size()); Assert.assertEquals("", userDetails.getPassword()); }
@Test(expected = UsernameNotFoundException.class) public void loadUserByUsernameUserNotFound() throws Exception { AmbariKerberosAuthenticationProperties properties = new AmbariKerberosAuthenticationProperties(); Configuration configuration = createMock(Configuration.class); expect(configuration.getKerberosAuthenticationProperties()).andReturn(properties).once(); Users users = createMock(Users.class); expect(users.getUser("user1", UserType.LDAP)).andReturn(null).once(); expect(users.getUser("user1", UserType.LOCAL)).andReturn(null).once(); replayAll(); UserDetailsService userdetailsService = new AmbariAuthToLocalUserDetailsService(configuration, users); userdetailsService.loadUserByUsername("user1@EXAMPLE.COM"); verifyAll(); Assert.fail("UsernameNotFoundException was not thrown"); }