|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.sidecar.modules; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import io.vertx.core.Vertx; |
| 26 | +import org.apache.cassandra.sidecar.acl.authentication.AuthenticationHandlerFactory; |
| 27 | +import org.apache.cassandra.sidecar.acl.authentication.AuthenticationHandlerFactoryRegistry; |
| 28 | +import org.apache.cassandra.sidecar.acl.authentication.MutualTlsAuthenticationHandlerFactory; |
| 29 | +import org.apache.cassandra.sidecar.acl.authorization.RoleBasedAuthorizationProvider; |
| 30 | +import org.apache.cassandra.sidecar.config.AccessControlConfiguration; |
| 31 | +import org.apache.cassandra.sidecar.config.ParameterizedClassConfiguration; |
| 32 | +import org.apache.cassandra.sidecar.config.SchemaKeyspaceConfiguration; |
| 33 | +import org.apache.cassandra.sidecar.config.ServiceConfiguration; |
| 34 | +import org.apache.cassandra.sidecar.config.SidecarConfiguration; |
| 35 | +import org.apache.cassandra.sidecar.exceptions.ConfigurationException; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 38 | +import static org.mockito.ArgumentMatchers.any; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | +import static org.mockito.Mockito.when; |
| 41 | + |
| 42 | +/** |
| 43 | + * Test for {@link AuthModule} |
| 44 | + */ |
| 45 | +class AuthModuleTest |
| 46 | +{ |
| 47 | + @Test |
| 48 | + void testAuthorizationProviderWithSchemaEnabled() |
| 49 | + { |
| 50 | + AuthModule authModule = new AuthModule(); |
| 51 | + SidecarConfiguration mockSidecarConfig = createSidecarConfiguration(true, true, true); |
| 52 | + |
| 53 | + // Should not throw when schema is enabled |
| 54 | + authModule.authorizationProvider(mockSidecarConfig, null); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testRoleBasedAuthorizationProviderWithSchemaDisabled() |
| 59 | + { |
| 60 | + AuthModule authModule = new AuthModule(); |
| 61 | + SidecarConfiguration mockSidecarConfig = createSidecarConfiguration(true, true,false); |
| 62 | + |
| 63 | + // Should throw when RoleBasedAuthorizationProvider is used but schema is disabled |
| 64 | + assertThatThrownBy(() -> authModule.authorizationProvider(mockSidecarConfig, null)) |
| 65 | + .isInstanceOf(ConfigurationException.class) |
| 66 | + .hasMessage(RoleBasedAuthorizationProvider.class.getName() + |
| 67 | + " requires sidecar schema to be enabled for role permissions storage"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testChainAuthHandlerValidatesPrerequisites() |
| 72 | + { |
| 73 | + AuthModule authModule = new AuthModule(); |
| 74 | + Vertx mockVertx = mock(Vertx.class); |
| 75 | + |
| 76 | + SidecarConfiguration mockSidecarConfig = createSidecarConfiguration(true, false, false); |
| 77 | + |
| 78 | + AuthenticationHandlerFactoryRegistry mockRegistry = mock(AuthenticationHandlerFactoryRegistry.class); |
| 79 | + AuthenticationHandlerFactory mtlsFactory = new MutualTlsAuthenticationHandlerFactory(null, null); |
| 80 | + when(mockRegistry.getFactory(any())).thenReturn(mtlsFactory); |
| 81 | + |
| 82 | + // Should throw ConfigurationException when factory validates prerequisites |
| 83 | + assertThatThrownBy(() -> authModule.chainAuthHandler(mockVertx, mockSidecarConfig, mockRegistry)) |
| 84 | + .isInstanceOf(ConfigurationException.class) |
| 85 | + .hasMessage("mTLS auth requires sidecar schema to be enabled for role processing"); |
| 86 | + } |
| 87 | + |
| 88 | + private SidecarConfiguration createSidecarConfiguration(boolean authenticationEnabled, |
| 89 | + boolean authorizationEnabled, |
| 90 | + boolean schemaEnabled) |
| 91 | + { |
| 92 | + SidecarConfiguration mockSidecarConfig = mock(SidecarConfiguration.class); |
| 93 | + ServiceConfiguration mockServiceConfig = mock(ServiceConfiguration.class); |
| 94 | + SchemaKeyspaceConfiguration mockSchemaConfig = mock(SchemaKeyspaceConfiguration.class); |
| 95 | + AccessControlConfiguration mockAccessControlConfig = mock(AccessControlConfiguration.class); |
| 96 | + ParameterizedClassConfiguration mockAuthenticatorConfig = mock(ParameterizedClassConfiguration.class); |
| 97 | + ParameterizedClassConfiguration mockAuthorizerConfig = mock(ParameterizedClassConfiguration.class); |
| 98 | + |
| 99 | + when(mockSidecarConfig.serviceConfiguration()).thenReturn(mockServiceConfig); |
| 100 | + when(mockServiceConfig.schemaKeyspaceConfiguration()).thenReturn(mockSchemaConfig); |
| 101 | + when(mockSchemaConfig.isEnabled()).thenReturn(schemaEnabled); |
| 102 | + when(mockSidecarConfig.accessControlConfiguration()).thenReturn(mockAccessControlConfig); |
| 103 | + when(mockAccessControlConfig.enabled()).thenReturn(true); |
| 104 | + |
| 105 | + if (authenticationEnabled) |
| 106 | + { |
| 107 | + when(mockAccessControlConfig.authenticatorsConfiguration()).thenReturn(List.of(mockAuthenticatorConfig)); |
| 108 | + when(mockAuthenticatorConfig.className()).thenReturn(MutualTlsAuthenticationHandlerFactory.class.getName()); |
| 109 | + } |
| 110 | + |
| 111 | + if (authorizationEnabled) |
| 112 | + { |
| 113 | + when(mockAccessControlConfig.authorizerConfiguration()).thenReturn(mockAuthorizerConfig); |
| 114 | + when(mockAuthorizerConfig.className()).thenReturn(RoleBasedAuthorizationProvider.class.getName()); |
| 115 | + } |
| 116 | + return mockSidecarConfig; |
| 117 | + } |
| 118 | +} |
0 commit comments