AmbariKerberosAuthenticationFilter extends SpnegoAuthenticationProcessingFilter implements AmbariAuthenticationFilter { @Override public boolean shouldApply(HttpServletRequest httpServletRequest) { if (kerberosAuthenticationEnabled) { String header = httpServletRequest.getHeader("Authorization"); return (header != null) && (header.startsWith("Negotiate ") || header.startsWith("Kerberos ")); } else { return false; } } AmbariKerberosAuthenticationFilter(AuthenticationManager authenticationManager, final AuthenticationEntryPoint entryPoint, Configuration configuration, final AuditLogger auditLogger, final PermissionHelper permissionHelper); @Override boolean shouldApply(HttpServletRequest httpServletRequest); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain);  }
@Test public void shouldApplyTrue() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); expect(httpServletRequest.getHeader("Authorization")).andReturn("Negotiate .....").once(); AmbariKerberosAuthenticationProperties properties = createMock(AmbariKerberosAuthenticationProperties.class); expect(properties.isKerberosAuthenticationEnabled()).andReturn(true).once(); Configuration configuration = createMock(Configuration.class); expect(configuration.getKerberosAuthenticationProperties()).andReturn(properties).once(); AuthenticationManager authenticationManager = createMock(AuthenticationManager.class); AuthenticationEntryPoint entryPoint = createMock(AuthenticationEntryPoint.class); AuditLogger auditLogger = createMock(AuditLogger.class); PermissionHelper permissionHelper = createMock(PermissionHelper.class); replayAll(); AmbariKerberosAuthenticationFilter filter = new AmbariKerberosAuthenticationFilter( authenticationManager, entryPoint, configuration, auditLogger, permissionHelper ); Assert.assertTrue(filter.shouldApply(httpServletRequest)); verifyAll(); }
@Test public void shouldApplyFalseMissingHeader() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); expect(httpServletRequest.getHeader("Authorization")).andReturn(null).once(); AmbariKerberosAuthenticationProperties properties = createMock(AmbariKerberosAuthenticationProperties.class); expect(properties.isKerberosAuthenticationEnabled()).andReturn(true).once(); Configuration configuration = createMock(Configuration.class); expect(configuration.getKerberosAuthenticationProperties()).andReturn(properties).once(); AuthenticationManager authenticationManager = createMock(AuthenticationManager.class); AuthenticationEntryPoint entryPoint = createMock(AuthenticationEntryPoint.class); AuditLogger auditLogger = createMock(AuditLogger.class); PermissionHelper permissionHelper = createMock(PermissionHelper.class); replayAll(); AmbariKerberosAuthenticationFilter filter = new AmbariKerberosAuthenticationFilter( authenticationManager, entryPoint, configuration, auditLogger, permissionHelper ); Assert.assertFalse(filter.shouldApply(httpServletRequest)); verifyAll(); }
@Test public void shouldApplyNotFalseEnabled() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); AmbariKerberosAuthenticationProperties properties = createMock(AmbariKerberosAuthenticationProperties.class); expect(properties.isKerberosAuthenticationEnabled()).andReturn(false).once(); Configuration configuration = createMock(Configuration.class); expect(configuration.getKerberosAuthenticationProperties()).andReturn(properties).once(); AuthenticationManager authenticationManager = createMock(AuthenticationManager.class); AuthenticationEntryPoint entryPoint = createMock(AuthenticationEntryPoint.class); AuditLogger auditLogger = createMock(AuditLogger.class); PermissionHelper permissionHelper = createMock(PermissionHelper.class); replayAll(); AmbariKerberosAuthenticationFilter filter = new AmbariKerberosAuthenticationFilter( authenticationManager, entryPoint, configuration, auditLogger, permissionHelper ); Assert.assertFalse(filter.shouldApply(httpServletRequest)); verifyAll(); }