lasdf1234 commented on code in PR #11226: URL: https://github.com/apache/gravitino/pull/11226#discussion_r3315752509
########## clients/client-python/gravitino/auth/basic_auth_provider.py: ########## @@ -0,0 +1,47 @@ +# 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. + +import base64 + +from gravitino.auth.auth_constants import AuthConstants +from gravitino.auth.auth_data_provider import AuthDataProvider +from gravitino.exceptions.base import IllegalArgumentException + + +class BasicAuthProvider(AuthDataProvider): + """Provides HTTP Basic credentials for Gravitino built-in IdP authentication.""" + + def __init__(self, username: str, password: str): + if username is None or not username.strip(): + raise IllegalArgumentException("username can't be blank") + if password is None or not password.strip(): + raise IllegalArgumentException("password can't be blank") + + user_information = f"{username}:{password}" + self._token = ( + AuthConstants.AUTHORIZATION_BASIC_HEADER + + base64.b64encode(user_information.encode("utf-8")).decode("utf-8") + ).encode("utf-8") Review Comment: Got.Revised the method. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/auth/BasicAuthenticator.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.GravitinoEnv; +import org.apache.gravitino.UserGroup; +import org.apache.gravitino.UserPrincipal; +import org.apache.gravitino.auth.AuthConstants; +import org.apache.gravitino.exceptions.UnauthorizedException; +import org.apache.gravitino.idp.IdpUserGroupManager; +import org.apache.gravitino.idp.model.IdpUser; +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 IdpUserGroupManager userGroupManager; + + public BasicAuthenticator() {} + + @Override + public boolean isDataFromToken() { + return true; + } + + @Override + public Principal authenticateToken(byte[] tokenData) { + Preconditions.checkState( + userGroupManager != 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) { + GravitinoEnv env = GravitinoEnv.getInstance(); + this.userGroupManager = IdpUserGroupManager.getInstance(config, env.idGenerator()); + } + + @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 new UnauthorizedException("Empty token authorization header", BASIC_CHALLENGE); + } + + String authData = new String(tokenData, StandardCharsets.UTF_8); + if (StringUtils.isBlank(authData)) { + throw new UnauthorizedException("Empty token authorization header", BASIC_CHALLENGE); + } + if (!authData.startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER)) { + throw new UnauthorizedException("Invalid token authorization header", BASIC_CHALLENGE); + } + return authData; + } + + private BasicCredentials parseBasicCredentials(String authData) { + String credential = authData.substring(AuthConstants.AUTHORIZATION_BASIC_HEADER.length()); + if (StringUtils.isBlank(credential)) { + throw new UnauthorizedException( + "Malformed Basic authorization header: missing credentials", BASIC_CHALLENGE); + } + credential = credential.trim(); + + try { + String decodedCredential = + new String(Base64.getDecoder().decode(credential), StandardCharsets.UTF_8); + String[] parts = decodedCredential.split(":", 2); + if (parts.length != 2) { + throw new UnauthorizedException( + "Malformed Basic authorization header: credentials must be in username:password format", + BASIC_CHALLENGE); + } + + String username = parts[0]; + if (StringUtils.isBlank(username)) { + throw new UnauthorizedException( + "Malformed Basic authorization header: username must not be blank", BASIC_CHALLENGE); + } + + String password = parts[1]; + if (StringUtils.isBlank(password)) { + throw new UnauthorizedException("Invalid username or password", BASIC_CHALLENGE); + } + return new BasicCredentials(username, password); + } catch (IllegalArgumentException e) { Review Comment: Got cahtch base64 method, code has been modified. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/auth/BasicAuthenticator.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.GravitinoEnv; +import org.apache.gravitino.UserGroup; +import org.apache.gravitino.UserPrincipal; +import org.apache.gravitino.auth.AuthConstants; +import org.apache.gravitino.exceptions.UnauthorizedException; +import org.apache.gravitino.idp.IdpUserGroupManager; +import org.apache.gravitino.idp.model.IdpUser; +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 IdpUserGroupManager userGroupManager; + + public BasicAuthenticator() {} + + @Override + public boolean isDataFromToken() { + return true; + } + + @Override + public Principal authenticateToken(byte[] tokenData) { + Preconditions.checkState( + userGroupManager != 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) { + GravitinoEnv env = GravitinoEnv.getInstance(); + this.userGroupManager = IdpUserGroupManager.getInstance(config, env.idGenerator()); + } + + @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) { Review Comment: Got the logical has been simplified ########## docs/security/how-to-authenticate.md: ########## @@ -43,6 +43,41 @@ curl -v -X GET \ http://localhost:8090/api/version ``` +### Basic mode + +In Basic mode, Gravitino verifies HTTP Basic credentials against built-in IDP user metadata stored +in the relational entity store. + +To enable Basic mode: + +- Set `gravitino.server.rest.extensionPackages` to `org.apache.gravitino.idp.web.rest.feature`. +- Set `gravitino.authorization.serviceAdmins` to the service admin usernames that should exist in + the built-in IDP. +- On the first startup, if any configured service admin does not yet have a password, set the + `GRAVITINO_INITIAL_ADMIN_PASSWORD` environment variable to the initial password (12 to 64 + characters) before starting Gravitino. The same password is applied to every configured service + admin that does not yet exist in the built-in IDP. + +For the client side, enable Basic mode with the following code: + +```java +GravitinoClient client = GravitinoClient.builder(uri) + .withMetalake("metalake") + .withBasicAuth("admin", "YourSecureGravitinoPassword") + .build(); +``` + +```python +from gravitino.auth.basic_auth_provider import BasicAuthProvider +from gravitino.client.gravitino_client import GravitinoClient + +client = GravitinoClient( + uri="http://localhost:8090", + metalake_name="metalake", + auth_data_provider=BasicAuthProvider("admin", "YourSecureGravitinoPassword"), +) +``` Review Comment: God shell example has been added. ########## .github/workflows/idp-basic-test.yml: ########## @@ -60,11 +60,22 @@ jobs: run: | dev/ci/util_free_space.sh - - name: Run idp-basic tests + - name: Run idp-basic unit tests + run: | + ./gradlew :plugins:idp-basic:test -PskipITs -PskipDockerTests=true -PskipWeb=true + + - name: Run idp-basic REST API integration tests env: dockerTest: true run: | - ./gradlew :plugins:idp-basic:test -PskipITs -PskipDockerTests=false -PskipWeb=true + for backend in h2 mysql postgresql; do + ./gradlew :plugins:idp-basic:test \ + -PtestMode=embedded \ + -PjdbcBackend="${backend}" \ + -PskipDockerTests=false \ + -PskipWeb=true \ + --tests "org.apache.gravitino.idp.integration.test.IdpRESTApiIT" Review Comment: Got code has been modified. ########## .github/workflows/idp-basic-test.yml: ########## @@ -60,11 +60,22 @@ jobs: run: | dev/ci/util_free_space.sh - - name: Run idp-basic tests + - name: Run idp-basic unit tests + run: | + ./gradlew :plugins:idp-basic:test -PskipITs -PskipDockerTests=true -PskipWeb=true + + - name: Run idp-basic REST API integration tests env: dockerTest: true run: | - ./gradlew :plugins:idp-basic:test -PskipITs -PskipDockerTests=false -PskipWeb=true + for backend in h2 mysql postgresql; do + ./gradlew :plugins:idp-basic:test \ + -PtestMode=embedded \ Review Comment: Got deploy mode has been added. -- 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]
