AmbariPamAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if(isPamEnabled()){ PAM pam; try{ String pamConfig = configuration.getPamConfigurationFile(); pam = new PAM(pamConfig); } catch(PAMException ex) { LOG.error("Unable to Initialize PAM." + ex.getMessage()); throw new AuthenticationServiceException("Unable to Initialize PAM - ", ex); } return authenticateViaPam(pam, authentication); } else { return null; } } @Inject  AmbariPamAuthenticationProvider(Configuration configuration); @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication);  }
@Test(expected = AuthenticationException.class) public void testBadCredential() throws Exception { Authentication authentication = new UsernamePasswordAuthenticationToken("notFound", "wrong"); authenticationProvider.authenticate(authentication); }
@Test public void testAuthenticate() throws Exception { PAM pam = createNiceMock(PAM.class); UnixUser unixUser = createNiceMock(UnixUser.class); expect(pam.authenticate(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn(unixUser).atLeastOnce(); expect(unixUser.getGroups()).andReturn(new HashSet<String>(Arrays.asList("group"))).atLeastOnce(); EasyMock.replay(unixUser); EasyMock.replay(pam); Authentication authentication = new UsernamePasswordAuthenticationToken("allowedUser", "password"); Authentication result = authenticationProvider.authenticateViaPam(pam,authentication); assertEquals("allowedUser", result.getName()); }
@Test public void testDisabled() throws Exception { configuration.setClientSecurityType(ClientSecurityType.LOCAL); Authentication authentication = new UsernamePasswordAuthenticationToken("allowedUser", "password"); Authentication auth = authenticationProvider.authenticate(authentication); Assert.assertTrue(auth == null); }