|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. See accompanying LICENSE file. |
| 13 | + */ |
| 14 | +package com.hortonworks.registries.auth; |
| 15 | + |
| 16 | +import com.hortonworks.registries.auth.client.KerberosAuthenticator; |
| 17 | +import com.hortonworks.registries.auth.server.AuthenticationToken; |
| 18 | +import com.hortonworks.registries.auth.server.KerberosAuthenticationHandler; |
| 19 | +import com.hortonworks.registries.auth.util.JaasConfiguration; |
| 20 | +import com.hortonworks.registries.auth.util.KerberosUtil; |
| 21 | +import org.apache.commons.codec.binary.Base64; |
| 22 | +import org.apache.hadoop.minikdc.KerberosSecurityTestcase; |
| 23 | +import org.ietf.jgss.GSSContext; |
| 24 | +import org.ietf.jgss.GSSManager; |
| 25 | +import org.ietf.jgss.GSSName; |
| 26 | +import org.ietf.jgss.Oid; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import org.mockito.Mockito; |
| 32 | + |
| 33 | +import javax.security.auth.Subject; |
| 34 | +import javax.servlet.http.HttpServletRequest; |
| 35 | +import javax.servlet.http.HttpServletResponse; |
| 36 | +import java.io.File; |
| 37 | +import java.security.PrivilegedExceptionAction; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Properties; |
| 40 | +import java.util.concurrent.Callable; |
| 41 | + |
| 42 | +public class TestKerberosLogin extends KerberosSecurityTestcase { |
| 43 | + |
| 44 | + protected KerberosAuthenticationHandler handler; |
| 45 | + |
| 46 | + protected KerberosAuthenticationHandler getNewAuthenticationHandler() { |
| 47 | + return new KerberosAuthenticationHandler(); |
| 48 | + } |
| 49 | + |
| 50 | + protected Properties getDefaultProperties() { |
| 51 | + Properties props = new Properties(); |
| 52 | + props.setProperty(KerberosAuthenticationHandler.PRINCIPAL, |
| 53 | + KerberosTestUtils.getServerPrincipal()); |
| 54 | + props.setProperty(KerberosAuthenticationHandler.KEYTAB, |
| 55 | + KerberosTestUtils.getKeytabFile()); |
| 56 | + props.setProperty(KerberosAuthenticationHandler.NAME_RULES, |
| 57 | + "RULE:[1:$1@$0](.*@" + KerberosTestUtils.getRealm() + ")s/@.*//\n"); |
| 58 | + return props; |
| 59 | + } |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setup() throws Exception { |
| 63 | + // create keytab |
| 64 | + File keytabFile = new File(KerberosTestUtils.getKeytabFile()); |
| 65 | + String clientPrincipal = KerberosTestUtils.getClientPrincipal(); |
| 66 | + String serverPrincipal = KerberosTestUtils.getServerPrincipal(); |
| 67 | + serverPrincipal = serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@")); |
| 68 | + clientPrincipal = clientPrincipal.substring(0, clientPrincipal.lastIndexOf("@")); |
| 69 | + getKdc().createPrincipal(keytabFile, serverPrincipal, clientPrincipal); |
| 70 | + // handler |
| 71 | + handler = getNewAuthenticationHandler(); |
| 72 | + Properties props = getDefaultProperties(); |
| 73 | + try { |
| 74 | + handler.init(props); |
| 75 | + } catch (Exception ex) { |
| 76 | + handler = null; |
| 77 | + throw ex; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test(timeout = 60000) |
| 82 | + public void testRequestWithKerberosAuthorization() throws Exception { |
| 83 | + KerberosLogin login = new KerberosLogin(); |
| 84 | + System.out.println(KerberosTestUtils.getJaasConfigForClientPrincipal()); |
| 85 | + login.configure(new HashMap<>(), "RegistryClient", |
| 86 | + new JaasConfiguration("RegistryClient", KerberosTestUtils.getJaasConfigForClientPrincipal())); |
| 87 | + login.login(); |
| 88 | + |
| 89 | + String token = Subject.doAs(login.loginContext.getSubject(), (PrivilegedExceptionAction<String>) () -> { |
| 90 | + GSSManager gssManager = GSSManager.getInstance(); |
| 91 | + GSSContext gssContext = null; |
| 92 | + try { |
| 93 | + String servicePrincipal = KerberosTestUtils.getServerPrincipal(); |
| 94 | + Oid oid = KerberosUtil.getOidInstance("NT_GSS_KRB5_PRINCIPAL"); |
| 95 | + GSSName serviceName = gssManager.createName(servicePrincipal, |
| 96 | + oid); |
| 97 | + oid = KerberosUtil.getOidInstance("GSS_KRB5_MECH_OID"); |
| 98 | + gssContext = gssManager.createContext(serviceName, oid, null, |
| 99 | + GSSContext.DEFAULT_LIFETIME); |
| 100 | + gssContext.requestCredDeleg(true); |
| 101 | + gssContext.requestMutualAuth(true); |
| 102 | + |
| 103 | + byte[] inToken = new byte[0]; |
| 104 | + byte[] outToken = gssContext.initSecContext(inToken, 0, inToken.length); |
| 105 | + Base64 base64 = new Base64(0); |
| 106 | + return base64.encodeToString(outToken); |
| 107 | + |
| 108 | + } finally { |
| 109 | + if (gssContext != null) { |
| 110 | + gssContext.dispose(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); |
| 117 | + HttpServletResponse response = Mockito.mock(HttpServletResponse.class); |
| 118 | + |
| 119 | + Mockito.when(request.getHeader(KerberosAuthenticator.AUTHORIZATION)) |
| 120 | + .thenReturn(KerberosAuthenticator.NEGOTIATE + " " + token); |
| 121 | + Mockito.when(request.getServerName()).thenReturn("localhost"); |
| 122 | + Mockito.when(request.getMethod()).thenReturn("GET"); |
| 123 | + |
| 124 | + AuthenticationToken authToken = handler.authenticate(request, response); |
| 125 | + |
| 126 | + if (authToken != null) { |
| 127 | + Mockito.verify(response).setHeader(Mockito.eq(KerberosAuthenticator.WWW_AUTHENTICATE), |
| 128 | + Mockito.matches(KerberosAuthenticator.NEGOTIATE + " .*")); |
| 129 | + Mockito.verify(response).setStatus(HttpServletResponse.SC_OK); |
| 130 | + |
| 131 | + Assert.assertEquals(KerberosTestUtils.getClientPrincipal(), authToken.getName()); |
| 132 | + Assert.assertTrue(KerberosTestUtils.getClientPrincipal().startsWith(authToken.getUserName())); |
| 133 | + } else { |
| 134 | + Mockito.verify(response).setHeader(Mockito.eq(KerberosAuthenticator.WWW_AUTHENTICATE), |
| 135 | + Mockito.matches(KerberosAuthenticator.NEGOTIATE + " .*")); |
| 136 | + Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @After |
| 141 | + public void tearDown() throws Exception { |
| 142 | + if (handler != null) { |
| 143 | + handler.destroy(); |
| 144 | + handler = null; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments