|
| 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.pulsar.broker.authentication; |
| 21 | + |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | +import static org.testng.Assert.assertEquals; |
| 25 | +import static org.testng.Assert.assertFalse; |
| 26 | +import static org.testng.Assert.assertNotNull; |
| 27 | +import static org.testng.Assert.assertNull; |
| 28 | +import static org.testng.Assert.assertSame; |
| 29 | +import static org.testng.Assert.assertThrows; |
| 30 | +import static org.testng.Assert.assertTrue; |
| 31 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 32 | +import org.apache.pulsar.common.api.AuthData; |
| 33 | +import org.testng.annotations.Test; |
| 34 | +import javax.naming.AuthenticationException; |
| 35 | +import javax.servlet.http.HttpServletRequest; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.concurrent.CompletableFuture; |
| 38 | +import java.util.concurrent.atomic.LongAdder; |
| 39 | + |
| 40 | +public class OneStageAuthenticationStateTest { |
| 41 | + |
| 42 | + public static class CountingAuthenticationProvider implements AuthenticationProvider { |
| 43 | + public LongAdder authCallCount = new LongAdder(); |
| 44 | + |
| 45 | + @Override |
| 46 | + public void initialize(ServiceConfiguration config) throws IOException { |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getAuthMethodName() { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void close() throws IOException { |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public CompletableFuture<String> authenticateAsync(AuthenticationDataSource authData) { |
| 60 | + authCallCount.increment(); |
| 61 | + return CompletableFuture.completedFuture(authData.getCommandData()); |
| 62 | + } |
| 63 | + |
| 64 | + public int getAuthCallCount() { |
| 65 | + return authCallCount.intValue(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void verifyAuthenticateAsyncIsCalledExactlyOnceAndSetsRole() throws Exception { |
| 71 | + CountingAuthenticationProvider provider = new CountingAuthenticationProvider(); |
| 72 | + AuthData authData = AuthData.of("role".getBytes()); |
| 73 | + OneStageAuthenticationState authState = new OneStageAuthenticationState(authData, null, null, provider); |
| 74 | + assertEquals(provider.getAuthCallCount(), 0, "Auth count should not increase yet"); |
| 75 | + AuthData challenge = authState.authenticateAsync(authData).get(); |
| 76 | + assertNull(challenge); |
| 77 | + assertEquals(provider.getAuthCallCount(), 1, "Call authenticate only once"); |
| 78 | + assertEquals(authState.getAuthRole(), "role"); |
| 79 | + AuthenticationDataSource firstAuthenticationDataSource = authState.getAuthDataSource(); |
| 80 | + assertTrue(firstAuthenticationDataSource instanceof AuthenticationDataCommand); |
| 81 | + |
| 82 | + // Verify subsequent call to authenticate does not change data |
| 83 | + AuthData secondChallenge = authState.authenticateAsync(AuthData.of("admin".getBytes())).get(); |
| 84 | + assertNull(secondChallenge); |
| 85 | + assertEquals(authState.getAuthRole(), "role"); |
| 86 | + AuthenticationDataSource secondAuthenticationDataSource = authState.getAuthDataSource(); |
| 87 | + assertSame(secondAuthenticationDataSource, firstAuthenticationDataSource); |
| 88 | + assertEquals(provider.getAuthCallCount(), 1, "Call authenticate only once, even later."); |
| 89 | + } |
| 90 | + |
| 91 | + @SuppressWarnings("deprecation") |
| 92 | + @Test |
| 93 | + public void verifyAuthenticateIsCalledExactlyOnceAndSetsRole() throws Exception { |
| 94 | + CountingAuthenticationProvider provider = new CountingAuthenticationProvider(); |
| 95 | + AuthData authData = AuthData.of("role".getBytes()); |
| 96 | + OneStageAuthenticationState authState = new OneStageAuthenticationState(authData, null, null, provider); |
| 97 | + assertEquals(provider.getAuthCallCount(), 0, "Auth count should not increase yet"); |
| 98 | + assertFalse(authState.isComplete()); |
| 99 | + AuthData challenge = authState.authenticate(authData); |
| 100 | + assertNull(challenge); |
| 101 | + assertTrue(authState.isComplete()); |
| 102 | + assertEquals(provider.getAuthCallCount(), 1, "Call authenticate only once"); |
| 103 | + assertEquals(authState.getAuthRole(), "role"); |
| 104 | + AuthenticationDataSource firstAuthenticationDataSource = authState.getAuthDataSource(); |
| 105 | + assertTrue(firstAuthenticationDataSource instanceof AuthenticationDataCommand); |
| 106 | + |
| 107 | + // Verify subsequent call to authenticate does not change data |
| 108 | + AuthData secondChallenge = authState.authenticate(AuthData.of("admin".getBytes())); |
| 109 | + assertNull(secondChallenge); |
| 110 | + assertEquals(authState.getAuthRole(), "role"); |
| 111 | + AuthenticationDataSource secondAuthenticationDataSource = authState.getAuthDataSource(); |
| 112 | + assertSame(secondAuthenticationDataSource, firstAuthenticationDataSource); |
| 113 | + assertEquals(provider.getAuthCallCount(), 1, "Call authenticate only once, even later."); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void verifyGetAuthRoleBeforeAuthenticateFails() { |
| 118 | + CountingAuthenticationProvider provider = new CountingAuthenticationProvider(); |
| 119 | + AuthData authData = AuthData.of("role".getBytes()); |
| 120 | + OneStageAuthenticationState authState = new OneStageAuthenticationState(authData, null, null, provider); |
| 121 | + assertThrows(AuthenticationException.class, authState::getAuthRole); |
| 122 | + assertNull(authState.getAuthDataSource()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void verifyHttpAuthConstructorInitializesAuthDataSourceAndDoesNotAuthenticateData() { |
| 127 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 128 | + when(request.getRemoteAddr()).thenReturn("localhost"); |
| 129 | + when(request.getRemotePort()).thenReturn(8080); |
| 130 | + CountingAuthenticationProvider provider = new CountingAuthenticationProvider(); |
| 131 | + OneStageAuthenticationState authState = new OneStageAuthenticationState(request, provider); |
| 132 | + assertNotNull(authState.getAuthDataSource()); |
| 133 | + assertEquals(provider.getAuthCallCount(), 0); |
| 134 | + } |
| 135 | +} |
0 commit comments