|
| 1 | +package io.kafbat.ui.config.auth.azure; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 5 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 6 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | +import static org.mockito.ArgumentMatchers.any; |
| 11 | +import static org.mockito.ArgumentMatchers.anyString; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import static org.mockito.Mockito.times; |
| 14 | +import static org.mockito.Mockito.verify; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | + |
| 17 | +import com.azure.core.credential.AccessToken; |
| 18 | +import com.azure.core.credential.TokenCredential; |
| 19 | +import com.azure.core.credential.TokenRequestContext; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import javax.security.auth.callback.Callback; |
| 24 | +import javax.security.auth.callback.UnsupportedCallbackException; |
| 25 | +import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken; |
| 26 | +import org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.mockito.ArgumentCaptor; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 33 | +import reactor.core.publisher.Mono; |
| 34 | + |
| 35 | +@ExtendWith(MockitoExtension.class) |
| 36 | +public class AzureEntraLoginCallbackHandlerTest { |
| 37 | + |
| 38 | + // These are not real tokens. It was generated using fake values with an invalid signature, |
| 39 | + // so it is safe to store here. |
| 40 | + private static final String VALID_SAMPLE_TOKEN = |
| 41 | + "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RE2X1QXGXA9ukZtptMzP_0wcoqDDjNrys" |
| 51 | + + "GrNhztyeOr0YSeMMFq2NQ5vMBzLapwONwsnv55Hn0jOje9cqnMf43z1LHI6q6-rIIRz-SiTuoYUgOTxzFftpt-7FSqLjQpYEH7bL" |
| 52 | + + "p-0yIU_aJUSb5HQTJbtYYOb54hsZ6VXpaiZ013qGtKODbHTG37kdoIw2MPn66CxanLZKeZM31IVxC-duAqxDgK4O2Ne6xRZRIPW1" |
| 53 | + + "yt61QnZutWTJ4bAyhmplym3OWZ369cyiSJek0uyS5tibXeCYG4Kk8UQSFcsyfwgOsD0xvvcXcLexcUcEekoNBj6ixDhWssFzhC8T" |
| 54 | + + "Npy8-QKNe_Tp6qHzJdI6OV71jpDkGvcmseLHC9GOxBWB0IdYbePTFK-rz2dkN3uMUiFwQJvEbORsq1IaQXj2esT0F7sMfqzWQF9h" |
| 55 | + + "koVy4mJg_auvrZlnQkNPdLHfCacU33ZPwtuSS6b-0XolbxZ5DlJ4p1OJPeHl2xsi61qiHuCBsmnkLNtHmyxNTXGs7xc4dEQokaCK" |
| 56 | + + "-FB_lzC3D4mkJMxKWopQGXnQtizaZjyclGpiUFs3mEauxC7RpsbanitxPFs7FK3mY0MQJk9JNVi1oM-8qfEp8nYT2DwFBhLcIp2z" |
| 57 | + + "Q"; |
| 58 | + |
| 59 | + @Mock |
| 60 | + private OAuthBearerTokenCallback oauthBearerTokenCallBack; |
| 61 | + |
| 62 | + @Mock |
| 63 | + private OAuthBearerToken oauthBearerToken; |
| 64 | + |
| 65 | + @Mock |
| 66 | + private TokenCredential tokenCredential; |
| 67 | + |
| 68 | + @Mock |
| 69 | + private AccessToken accessToken; |
| 70 | + |
| 71 | + private AzureEntraLoginCallbackHandler azureEntraLoginCallbackHandler; |
| 72 | + |
| 73 | + @BeforeEach |
| 74 | + public void beforeEach() { |
| 75 | + azureEntraLoginCallbackHandler = new AzureEntraLoginCallbackHandler(); |
| 76 | + azureEntraLoginCallbackHandler.setTokenCredential(tokenCredential); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void shouldProvideTokenToCallbackWithSuccessfulTokenRequest() |
| 81 | + throws UnsupportedCallbackException { |
| 82 | + final Map<String, Object> configs = new HashMap<>(); |
| 83 | + configs.put( |
| 84 | + "bootstrap.servers", |
| 85 | + List.of("test-eh.servicebus.windows.net:9093")); |
| 86 | + |
| 87 | + when(tokenCredential.getToken(any(TokenRequestContext.class))).thenReturn(Mono.just(accessToken)); |
| 88 | + when(accessToken.getToken()).thenReturn(VALID_SAMPLE_TOKEN); |
| 89 | + |
| 90 | + azureEntraLoginCallbackHandler.configure(configs, null, null); |
| 91 | + azureEntraLoginCallbackHandler.handle(new Callback[] {oauthBearerTokenCallBack}); |
| 92 | + |
| 93 | + final ArgumentCaptor<TokenRequestContext> contextCaptor = |
| 94 | + ArgumentCaptor.forClass(TokenRequestContext.class); |
| 95 | + final ArgumentCaptor<OAuthBearerToken> tokenCaptor = |
| 96 | + ArgumentCaptor.forClass(OAuthBearerToken.class); |
| 97 | + |
| 98 | + verify(tokenCredential, times(1)).getToken(contextCaptor.capture()); |
| 99 | + verify(oauthBearerTokenCallBack, times(0)).error(anyString(), anyString(), anyString()); |
| 100 | + verify(oauthBearerTokenCallBack, times(1)).token(tokenCaptor.capture()); |
| 101 | + |
| 102 | + final TokenRequestContext tokenRequestContext = contextCaptor.getValue(); |
| 103 | + assertThat(tokenRequestContext, is(notNullValue())); |
| 104 | + assertThat( |
| 105 | + tokenRequestContext.getScopes(), |
| 106 | + is(List.of("https://test-eh.servicebus.windows.net/.default"))); |
| 107 | + assertThat(tokenRequestContext.getClaims(), is(nullValue())); |
| 108 | + assertThat(tokenRequestContext.getTenantId(), is(nullValue())); |
| 109 | + assertFalse(tokenRequestContext.isCaeEnabled()); |
| 110 | + |
| 111 | + assertThat(tokenCaptor.getValue(), is(notNullValue())); |
| 112 | + assertEquals(VALID_SAMPLE_TOKEN, tokenCaptor.getValue().value()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void shouldProvideErrorToCallbackWithTokenError() throws UnsupportedCallbackException { |
| 117 | + final Map<String, Object> configs = new HashMap<>(); |
| 118 | + configs.put( |
| 119 | + "bootstrap.servers", |
| 120 | + List.of("test-eh.servicebus.windows.net:9093")); |
| 121 | + |
| 122 | + when(tokenCredential.getToken(any(TokenRequestContext.class))) |
| 123 | + .thenThrow(new RuntimeException("failed to acquire token")); |
| 124 | + |
| 125 | + azureEntraLoginCallbackHandler.configure(configs, null, null); |
| 126 | + azureEntraLoginCallbackHandler.handle(new Callback[] {oauthBearerTokenCallBack}); |
| 127 | + |
| 128 | + verify(oauthBearerTokenCallBack, times(1)) |
| 129 | + .error( |
| 130 | + "invalid_grant", |
| 131 | + "Failed to acquire Azure token for Event Hub Authentication. " |
| 132 | + + "Please ensure valid Azure credentials are configured.", |
| 133 | + null); |
| 134 | + verify(oauthBearerTokenCallBack, times(0)).token(any()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void shouldThrowExceptionWithNullBootstrapServers() { |
| 139 | + final Map<String, Object> configs = new HashMap<>(); |
| 140 | + |
| 141 | + assertThrows(IllegalArgumentException.class, () -> azureEntraLoginCallbackHandler.configure( |
| 142 | + configs, null, null)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void shouldThrowExceptionWithMultipleBootstrapServers() { |
| 147 | + final Map<String, Object> configs = new HashMap<>(); |
| 148 | + configs.put("bootstrap.servers", List.of("server1", "server2")); |
| 149 | + |
| 150 | + assertThrows(IllegalArgumentException.class, () -> azureEntraLoginCallbackHandler.configure( |
| 151 | + configs, null, null)); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void shouldThrowExceptionWithUnsupportedCallback() { |
| 156 | + assertThrows(UnsupportedCallbackException.class, () -> azureEntraLoginCallbackHandler.handle( |
| 157 | + new Callback[] {mock(Callback.class)})); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void shouldDoNothingOnClose() { |
| 162 | + azureEntraLoginCallbackHandler.close(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void shouldSupportDefaultConstructor() { |
| 167 | + new AzureEntraLoginCallbackHandler(); |
| 168 | + } |
| 169 | +} |
0 commit comments