|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.druid.security.kerberos; |
| 21 | + |
| 22 | +import org.apache.druid.server.security.AuthenticationResult; |
| 23 | +import org.apache.hadoop.security.authentication.server.AuthenticationToken; |
| 24 | +import org.apache.hadoop.security.authentication.util.KerberosName; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests verifying that AuthenticationToken userName (short name) is used for |
| 32 | + * AuthenticationResult identity, not the full Kerberos principal. |
| 33 | + * |
| 34 | + * <p>The DruidKerberosAuthenticationHandler creates tokens with: |
| 35 | + * {@code new AuthenticationToken(userName, clientPrincipal, getType())} |
| 36 | + * where userName is the result of KerberosName.getShortName() (applying authToLocal rules). |
| 37 | + * |
| 38 | + * <p>The KerberosAuthenticator must use token.getUserName() (not token.getName()) when |
| 39 | + * constructing AuthenticationResult so that the identity matches the short name |
| 40 | + * produced by authToLocal rules. |
| 41 | + */ |
| 42 | +public class DruidKerberosAuthenticationHandlerTokenTest |
| 43 | +{ |
| 44 | + private static final String SAFE_RULES = "RULE:[1:$1] RULE:[2:$1]"; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() |
| 48 | + { |
| 49 | + KerberosName.setRules(SAFE_RULES); |
| 50 | + } |
| 51 | + |
| 52 | + @After |
| 53 | + public void tearDown() |
| 54 | + { |
| 55 | + KerberosName.setRules(SAFE_RULES); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testSimplePrincipalMapping() |
| 60 | + { |
| 61 | + final String shortName = "druid"; |
| 62 | + final String principal = "druid@EXAMPLE.COM"; |
| 63 | + verifyTokenIdentity(shortName, principal); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testServicePrincipalMapping() |
| 68 | + { |
| 69 | + final String shortName = "druid"; |
| 70 | + final String principal = "druid/host.example.com@EXAMPLE.COM"; |
| 71 | + verifyTokenIdentity(shortName, principal); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testPrincipalWithDifferentShortName() |
| 76 | + { |
| 77 | + // Simulates authToLocal rule mapping a principal to a different local name |
| 78 | + final String shortName = "admin"; |
| 79 | + final String principal = "admin/secure@CORP.EXAMPLE.COM"; |
| 80 | + verifyTokenIdentity(shortName, principal); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testAuthToLocalRuleExtractsShortName() throws Exception |
| 85 | + { |
| 86 | + // Use an explicit rule that strips the realm, matching what DEFAULT does with a krb5.conf |
| 87 | + KerberosName.setRules("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//"); |
| 88 | + final KerberosName kerberosName = new KerberosName("user@EXAMPLE.COM"); |
| 89 | + final String shortName = kerberosName.getShortName(); |
| 90 | + |
| 91 | + Assert.assertEquals("user", shortName); |
| 92 | + |
| 93 | + // This is what DruidKerberosAuthenticationHandler does |
| 94 | + final AuthenticationToken token = new AuthenticationToken(shortName, "user@EXAMPLE.COM", "kerberos"); |
| 95 | + |
| 96 | + // And KerberosAuthenticator must use getUserName() for identity |
| 97 | + final AuthenticationResult result = new AuthenticationResult( |
| 98 | + token.getUserName(), |
| 99 | + "authorizer", |
| 100 | + "kerberos", |
| 101 | + null |
| 102 | + ); |
| 103 | + Assert.assertEquals("user", result.getIdentity()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testCustomAuthToLocalRule() throws Exception |
| 108 | + { |
| 109 | + // Test with a custom rule that maps druid@EXAMPLE.COM -> druid |
| 110 | + KerberosName.setRules("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*// DEFAULT"); |
| 111 | + final KerberosName kerberosName = new KerberosName("myuser@EXAMPLE.COM"); |
| 112 | + final String shortName = kerberosName.getShortName(); |
| 113 | + |
| 114 | + Assert.assertEquals("myuser", shortName); |
| 115 | + |
| 116 | + final AuthenticationToken token = new AuthenticationToken(shortName, "myuser@EXAMPLE.COM", "kerberos"); |
| 117 | + |
| 118 | + final AuthenticationResult result = new AuthenticationResult( |
| 119 | + token.getUserName(), |
| 120 | + "authorizer", |
| 121 | + "kerberos", |
| 122 | + null |
| 123 | + ); |
| 124 | + Assert.assertEquals("myuser", result.getIdentity()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testTwoComponentPrincipalRule() throws Exception |
| 129 | + { |
| 130 | + // Rule for two-component principals: extract the first component (service name) |
| 131 | + KerberosName.setRules("RULE:[2:$1@$0](.*@EXAMPLE.COM)s/@.*//"); |
| 132 | + final KerberosName kerberosName = new KerberosName("HTTP/host.example.com@EXAMPLE.COM"); |
| 133 | + final String shortName = kerberosName.getShortName(); |
| 134 | + |
| 135 | + Assert.assertEquals("HTTP", shortName); |
| 136 | + |
| 137 | + final AuthenticationToken token = new AuthenticationToken( |
| 138 | + shortName, |
| 139 | + "HTTP/host.example.com@EXAMPLE.COM", |
| 140 | + "kerberos" |
| 141 | + ); |
| 142 | + |
| 143 | + final AuthenticationResult result = new AuthenticationResult( |
| 144 | + token.getUserName(), |
| 145 | + "authorizer", |
| 146 | + "kerberos", |
| 147 | + null |
| 148 | + ); |
| 149 | + Assert.assertEquals("HTTP", result.getIdentity()); |
| 150 | + Assert.assertNotEquals("HTTP/host.example.com@EXAMPLE.COM", result.getIdentity()); |
| 151 | + } |
| 152 | + |
| 153 | + private void verifyTokenIdentity(String expectedShortName, String fullPrincipal) |
| 154 | + { |
| 155 | + final AuthenticationToken token = new AuthenticationToken(expectedShortName, fullPrincipal, "kerberos"); |
| 156 | + |
| 157 | + // getUserName() should return the short name |
| 158 | + Assert.assertEquals(expectedShortName, token.getUserName()); |
| 159 | + // getName() should return the full principal |
| 160 | + Assert.assertEquals(fullPrincipal, token.getName()); |
| 161 | + |
| 162 | + // AuthenticationResult identity must use the short name |
| 163 | + final AuthenticationResult result = new AuthenticationResult( |
| 164 | + token.getUserName(), |
| 165 | + "testAuthorizer", |
| 166 | + "kerberos", |
| 167 | + null |
| 168 | + ); |
| 169 | + Assert.assertEquals(expectedShortName, result.getIdentity()); |
| 170 | + } |
| 171 | +} |
0 commit comments