roryqi commented on code in PR #11226: URL: https://github.com/apache/gravitino/pull/11226#discussion_r3304721606
########## server-common/src/main/java/org/apache/gravitino/server/plugin/ServerPluginBootstrap.java: ########## @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.server.plugin; + +/** + * Optional server plugin bootstrap hook discovered via {@link java.util.ServiceLoader}. + * + * <p>Implementations are provided by plugin jars on the server classpath. When a plugin jar is + * absent, its provider is not loaded and bootstrap is skipped. + */ +public interface ServerPluginBootstrap { + + /** + * Short name used in logs and error messages. + * + * @return The plugin bootstrap name. + */ + String name(); + + /** + * Initializes the plugin once per JVM. + * + * @throws Exception If initialization fails. + */ + void initialize() throws Exception; Review Comment: Should we have stop? Do we need properties? ########## plugins/idp-basic/build.gradle.kts: ########## @@ -69,12 +74,31 @@ tasks { duplicatesStrategy = DuplicatesStrategy.EXCLUDE } + val copyLibsToStandalonePackage by registering(Copy::class) { + dependsOn(jar) + from(layout.buildDirectory.dir("libs")) { + include("gravitino-idp-basic-*.jar") + exclude("*-javadoc.jar", "*-sources.jar") + } + from(configurations.runtimeClasspath) { + include("bcprov-jdk18on-*.jar") + } + into("$rootDir/distribution/gravitino-iceberg-rest-server/libs") + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + } + register("copyLibAndConfigs", Copy::class) { group = "gravitino distribution" description = "Copy idp-basic plugin jar into distribution package libs" dependsOn(copyLibs) } + register("copyLibAndConfigsToStandalonePackage", Copy::class) { Review Comment: Do we need to copy it to standalone library? ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/auth/BasicAuthenticator.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.idp.auth; + +import com.google.common.base.Preconditions; +import java.nio.charset.StandardCharsets; +import java.security.Principal; +import java.util.Base64; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.Config; +import org.apache.gravitino.UserGroup; +import org.apache.gravitino.UserPrincipal; +import org.apache.gravitino.auth.AuthConstants; +import org.apache.gravitino.exceptions.BadRequestException; +import org.apache.gravitino.exceptions.UnauthorizedException; +import org.apache.gravitino.idp.basic.password.PasswordHasher; +import org.apache.gravitino.idp.basic.password.PasswordHasherFactory; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.storage.po.IdpUserPO; +import org.apache.gravitino.idp.storage.service.IdpUserMetaService; +import org.apache.gravitino.server.authentication.Authenticator; + +/** Authenticates HTTP Basic credentials against built-in IdP user metadata. */ +public class BasicAuthenticator implements Authenticator { + + private static final String BASIC_CHALLENGE = AuthConstants.AUTHORIZATION_BASIC_HEADER.trim(); + + private IdpUserMetaService userMetaService; + private PasswordHasher passwordHasher; + + /** Creates a {@link BasicAuthenticator} for reflective loading. */ + public BasicAuthenticator() {} + + BasicAuthenticator(IdpUserMetaService userMetaService, PasswordHasher passwordHasher) { + this.userMetaService = userMetaService; + this.passwordHasher = passwordHasher; + } + + @Override + public boolean isDataFromToken() { + return true; + } + + @Override + public Principal authenticateToken(byte[] tokenData) { + Preconditions.checkState( + userMetaService != null && passwordHasher != null, + "Basic authenticator has not been initialized"); + String authData = requireBasicAuthHeader(tokenData); + BasicCredentials credentials = parseBasicCredentials(authData); + return authenticate(credentials, authData); + } + + @Override + public void initialize(Config config) { + this.userMetaService = IdpUserMetaService.getInstance(); + this.passwordHasher = PasswordHasherFactory.create(); + } + + @Override + public boolean supportsToken(byte[] tokenData) { + return tokenData != null + && new String(tokenData, StandardCharsets.UTF_8) + .startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER); + } + + private String requireBasicAuthHeader(byte[] tokenData) { + if (tokenData == null) { + throw unauthorized("Empty token authorization header"); + } + + String authData = new String(tokenData, StandardCharsets.UTF_8); + if (authData.trim().isEmpty()) { + throw unauthorized("Empty token authorization header"); + } + if (!authData.startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER)) { + throw unauthorized("Invalid token authorization header"); + } + return authData; + } + + private BasicCredentials parseBasicCredentials(String authData) { + String credential = authData.substring(AuthConstants.AUTHORIZATION_BASIC_HEADER.length()); + credential = credential.trim(); + if (credential.isEmpty()) { + throw new BadRequestException("Malformed Basic authorization header: missing credentials"); + } + + try { + String decodedCredential = + new String(Base64.getDecoder().decode(credential), StandardCharsets.UTF_8); + int separatorIndex = decodedCredential.indexOf(':'); + if (separatorIndex < 0) { + throw new BadRequestException( + "Malformed Basic authorization header: credentials must be in username:password format"); + } + + String userName = decodedCredential.substring(0, separatorIndex); + if (userName.isEmpty()) { + throw new BadRequestException( + "Malformed Basic authorization header: username must not be empty"); + } + + String password = decodedCredential.substring(separatorIndex + 1); + if (StringUtils.isBlank(password)) { + throw invalidCredentials(); + } + return new BasicCredentials(userName, password); + } catch (IllegalArgumentException e) { + throw new BadRequestException(e, "Malformed Basic authorization header: invalid base64"); + } + } + + private UserPrincipal authenticate(BasicCredentials credentials, String authData) { + IdpUserPO userPO = loadUser(credentials.userName()); + if (!passwordHasher.verify(credentials.password(), userPO.getPasswordHash())) { + throw invalidCredentials(); + } + + List<UserGroup> groups = + userMetaService.listGroupNamesByUsername(credentials.userName()).stream() Review Comment: You should access the UserGroupManager instead of service. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/auth/BasicAuthenticator.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.idp.auth; + +import com.google.common.base.Preconditions; +import java.nio.charset.StandardCharsets; +import java.security.Principal; +import java.util.Base64; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.Config; +import org.apache.gravitino.UserGroup; +import org.apache.gravitino.UserPrincipal; +import org.apache.gravitino.auth.AuthConstants; +import org.apache.gravitino.exceptions.BadRequestException; +import org.apache.gravitino.exceptions.UnauthorizedException; +import org.apache.gravitino.idp.basic.password.PasswordHasher; +import org.apache.gravitino.idp.basic.password.PasswordHasherFactory; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.storage.po.IdpUserPO; +import org.apache.gravitino.idp.storage.service.IdpUserMetaService; +import org.apache.gravitino.server.authentication.Authenticator; + +/** Authenticates HTTP Basic credentials against built-in IdP user metadata. */ +public class BasicAuthenticator implements Authenticator { + + private static final String BASIC_CHALLENGE = AuthConstants.AUTHORIZATION_BASIC_HEADER.trim(); + + private IdpUserMetaService userMetaService; + private PasswordHasher passwordHasher; + + /** Creates a {@link BasicAuthenticator} for reflective loading. */ + public BasicAuthenticator() {} + + BasicAuthenticator(IdpUserMetaService userMetaService, PasswordHasher passwordHasher) { + this.userMetaService = userMetaService; + this.passwordHasher = passwordHasher; + } + + @Override + public boolean isDataFromToken() { + return true; + } + + @Override + public Principal authenticateToken(byte[] tokenData) { + Preconditions.checkState( + userMetaService != null && passwordHasher != null, + "Basic authenticator has not been initialized"); + String authData = requireBasicAuthHeader(tokenData); + BasicCredentials credentials = parseBasicCredentials(authData); + return authenticate(credentials, authData); + } + + @Override + public void initialize(Config config) { + this.userMetaService = IdpUserMetaService.getInstance(); + this.passwordHasher = PasswordHasherFactory.create(); + } + + @Override + public boolean supportsToken(byte[] tokenData) { + return tokenData != null + && new String(tokenData, StandardCharsets.UTF_8) + .startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER); + } + + private String requireBasicAuthHeader(byte[] tokenData) { + if (tokenData == null) { + throw unauthorized("Empty token authorization header"); + } + + String authData = new String(tokenData, StandardCharsets.UTF_8); + if (authData.trim().isEmpty()) { + throw unauthorized("Empty token authorization header"); + } + if (!authData.startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER)) { + throw unauthorized("Invalid token authorization header"); + } + return authData; + } + + private BasicCredentials parseBasicCredentials(String authData) { + String credential = authData.substring(AuthConstants.AUTHORIZATION_BASIC_HEADER.length()); + credential = credential.trim(); + if (credential.isEmpty()) { + throw new BadRequestException("Malformed Basic authorization header: missing credentials"); + } + + try { + String decodedCredential = + new String(Base64.getDecoder().decode(credential), StandardCharsets.UTF_8); + int separatorIndex = decodedCredential.indexOf(':'); Review Comment: Why dont you use split? ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/auth/BasicAuthenticator.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.idp.auth; + +import com.google.common.base.Preconditions; +import java.nio.charset.StandardCharsets; +import java.security.Principal; +import java.util.Base64; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.Config; +import org.apache.gravitino.UserGroup; +import org.apache.gravitino.UserPrincipal; +import org.apache.gravitino.auth.AuthConstants; +import org.apache.gravitino.exceptions.BadRequestException; +import org.apache.gravitino.exceptions.UnauthorizedException; +import org.apache.gravitino.idp.basic.password.PasswordHasher; +import org.apache.gravitino.idp.basic.password.PasswordHasherFactory; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.storage.po.IdpUserPO; +import org.apache.gravitino.idp.storage.service.IdpUserMetaService; +import org.apache.gravitino.server.authentication.Authenticator; + +/** Authenticates HTTP Basic credentials against built-in IdP user metadata. */ +public class BasicAuthenticator implements Authenticator { + + private static final String BASIC_CHALLENGE = AuthConstants.AUTHORIZATION_BASIC_HEADER.trim(); + + private IdpUserMetaService userMetaService; + private PasswordHasher passwordHasher; + + /** Creates a {@link BasicAuthenticator} for reflective loading. */ + public BasicAuthenticator() {} + + BasicAuthenticator(IdpUserMetaService userMetaService, PasswordHasher passwordHasher) { Review Comment: Is this only visible for test? ########## plugins/idp-basic/src/test/java/org/apache/gravitino/idp/auth/TestServiceAdminInitializer.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.idp.auth; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import org.apache.gravitino.Config; +import org.apache.gravitino.idp.basic.password.PasswordHasher; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.storage.po.IdpUserPO; +import org.apache.gravitino.idp.storage.service.IdpUserMetaService; +import org.apache.gravitino.storage.IdGenerator; +import org.apache.gravitino.storage.relational.utils.POConverters; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +class TestServiceAdminInitializer { + private Config config; + private IdpUserMetaService userMetaService; + private PasswordHasher passwordHasher; + private IdGenerator idGenerator; + + @BeforeEach + void setUp() { + config = new Config(false) {}; + userMetaService = Mockito.mock(IdpUserMetaService.class); + passwordHasher = Mockito.mock(PasswordHasher.class); + idGenerator = Mockito.mock(IdGenerator.class); + } + + @Test + void testInitializeCreatesMissingServiceAdmin() throws IOException { + loadConfig("basic", "admin1,admin2"); + when(userMetaService.getIdpUserByUsername("admin1")) + .thenThrow(new NotFoundException("IdP user not found: %s", "admin1")); + when(userMetaService.getIdpUserByUsername("admin2")).thenReturn(existingUser("admin2")); + when(passwordHasher.hash("Passw0rd-For-Admin1")).thenReturn("hashed-password"); + when(idGenerator.nextId()).thenReturn(42L); + + initialize("[\"admin1:Passw0rd-For-Admin1\",\"admin2:Passw0rd-For-Admin2\"]"); + + ArgumentCaptor<IdpUserPO> userCaptor = ArgumentCaptor.forClass(IdpUserPO.class); + verify(userMetaService).insertIdpUser(userCaptor.capture()); + IdpUserPO userPO = userCaptor.getValue(); + assertEquals("admin1", userPO.getUsername()); + assertEquals("hashed-password", userPO.getPasswordHash()); + assertEquals(42L, userPO.getUserId()); + assertEquals(POConverters.INIT_VERSION, userPO.getCurrentVersion()); + verify(passwordHasher).hash("Passw0rd-For-Admin1"); + } + + @Test + void testInitializeSkipsWhenBasicAuthenticatorDisabledEvenIfPayloadInvalid() throws IOException { + loadConfig("simple", "admin1"); + initialize("not-json"); + verifyNoInteractions(userMetaService, passwordHasher, idGenerator); + } + + @Test + void testInitializeSkipsWhenNoServiceAdminsConfigured() throws IOException { + loadConfig("basic", ""); + initialize("[\"admin1:Passw0rd-For-Admin1\"]"); + verifyNoInteractions(userMetaService, passwordHasher, idGenerator); + } + + @Test + void testInitializeSkipsWhenAllServiceAdminsAlreadyExist() throws IOException { + loadConfig("basic", "admin1,admin2"); + when(userMetaService.getIdpUserByUsername("admin1")).thenReturn(existingUser("admin1")); + when(userMetaService.getIdpUserByUsername("admin2")).thenReturn(existingUser("admin2")); + + initialize(null); + + verify(userMetaService).getIdpUserByUsername("admin1"); + verify(userMetaService).getIdpUserByUsername("admin2"); + verify(userMetaService, never()).insertIdpUser(any()); + verifyNoInteractions(passwordHasher, idGenerator); + } + + @Test + void testInitializeFailsWhenRequiredPasswordMissing() throws IOException { + loadConfig("basic", "admin1"); + when(userMetaService.getIdpUserByUsername("admin1")) + .thenThrow(new NotFoundException("IdP user not found: %s", "admin1")); + + IllegalArgumentException exception = + assertThrows(IllegalArgumentException.class, () -> initialize(null)); + + assertEquals( + "Missing initial password for configured service admin admin1; declare" + + " GRAVITINO_INITIAL_ADMIN_PASSWORD", + exception.getMessage()); + verify(userMetaService, never()).insertIdpUser(any()); + } + + @ParameterizedTest + @MethodSource("invalidPasswordPayloads") + void testInitializeFailsOnInvalidPasswordPayload(String payload, String expectedMessage) { + loadConfig("basic", "admin1"); + + IllegalArgumentException exception = + assertThrows(IllegalArgumentException.class, () -> initialize(payload)); + + assertEquals(expectedMessage, exception.getMessage()); + verifyNoInteractions(userMetaService, passwordHasher, idGenerator); + } + + private static Stream<Arguments> invalidPasswordPayloads() { + return Stream.of( + Arguments.of( + "not-json", + "GRAVITINO_INITIAL_ADMIN_PASSWORD must be a JSON array of 'username:password' strings"), + Arguments.of( + "[\"admin1\"]", + "GRAVITINO_INITIAL_ADMIN_PASSWORD entry 'admin1' must use the format username:password"), + Arguments.of( + "[\"other:Passw0rd-For-Other\"]", + "GRAVITINO_INITIAL_ADMIN_PASSWORD entry 'other' is not a configured service admin"), + Arguments.of( + "[\"admin1:Passw0rd-For-Admin1\",\"admin1:Passw0rd-For-Admin1-Another\"]", + "GRAVITINO_INITIAL_ADMIN_PASSWORD contains duplicate entries for service admin admin1"), + Arguments.of("[\"admin1:short\"]", "Password length must be at least 12 characters")); + } + + private static IdpUserPO existingUser(String username) { + return IdpUserPO.builder() + .withUserId(1L) + .withUsername(username) + .withPasswordHash("hash") + .withCurrentVersion(POConverters.INIT_VERSION) + .withLastVersion(POConverters.INIT_VERSION) + .withDeletedAt(POConverters.DEFAULT_DELETED_AT) + .build(); + } + + private void loadConfig(String authenticators, String serviceAdmins) { + config.loadFromMap( + ImmutableMap.of( + "gravitino.authenticators", authenticators, + "gravitino.authorization.serviceAdmins", serviceAdmins), + t -> true); + } + + private void initialize(@Nullable String initialAdminPasswords) throws IOException { Review Comment: Could u avoid the @Nullable? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
