forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
257 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
fe/fe-core/src/main/java/org/apache/doris/service/arrowflight/AuthResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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. | ||
// This file is copied from | ||
// https://github.com/dremio/dremio-oss/blob/master/services/arrow-flight/src/main/java/com/dremio/service/flight/ServerCookieMiddleware.java | ||
// and modified by Doris | ||
|
||
package org.apache.doris.service.arrowflight; | ||
|
||
import java.util.Date; | ||
|
||
import org.immutables.value.Value; | ||
|
||
/** | ||
* Result of Authentication. | ||
*/ | ||
@Value.Immutable | ||
public interface AuthResult { | ||
String getUserName(); | ||
Date getExpiresAt(); | ||
String getUserId(); | ||
|
||
static AuthResult of(String userName) { | ||
return new ImmutableAuthResult.Builder() | ||
.setUserName(userName) | ||
.build(); | ||
} | ||
|
||
static ImmutableAuthResult.Builder builder() { | ||
return new ImmutableAuthResult.Builder(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
.../main/java/org/apache/doris/service/arrowflight/FlightServerBearerTokenAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// 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. | ||
// This file is copied from | ||
// https://github.com/dremio/dremio-oss/blob/master/services/arrow-flight/src/main/java/com/dremio/service/flight/ServerCookieMiddleware.java | ||
// and modified by Doris | ||
|
||
package org.apache.doris.service.arrowflight; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import org.apache.arrow.flight.CallHeaders; | ||
import org.apache.arrow.flight.CallStatus; | ||
import org.apache.arrow.flight.auth2.Auth2Constants; | ||
import org.apache.arrow.flight.auth2.AuthUtilities; | ||
import org.apache.arrow.flight.auth2.BasicCallHeaderAuthenticator; | ||
import org.apache.arrow.flight.auth2.CallHeaderAuthenticator; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class FlightServerBearerTokenAuthenticator implements CallHeaderAuthenticator { | ||
private static final Logger LOG = LogManager.getLogger(FlightServerBearerTokenAuthenticator.class); | ||
|
||
private final CallHeaderAuthenticator initialAuthenticator; | ||
|
||
public FlightServerBearerTokenAuthenticator() { | ||
this.initialAuthenticator = new BasicCallHeaderAuthenticator(new FlightServerCredentialValidator()); | ||
} | ||
|
||
@Override | ||
public AuthResult authenticate(CallHeaders incomingHeaders) { | ||
final String bearerToken = AuthUtilities.getValueFromAuthHeader(incomingHeaders, | ||
Auth2Constants.BEARER_PREFIX); | ||
|
||
if (bearerToken != null) { | ||
return validateBearer(bearerToken); | ||
} else { | ||
final AuthResult result = initialAuthenticator.authenticate(incomingHeaders); | ||
return getAuthResultWithBearerToken(result, incomingHeaders); | ||
} | ||
} | ||
|
||
/** | ||
* Validates provided token. | ||
* | ||
* @param token the token to validate. | ||
* @return an AuthResult with the bearer token and peer identity. | ||
*/ | ||
@VisibleForTesting | ||
AuthResult validateBearer(String token) { | ||
try { | ||
// tokenManagerProvider.get().validateToken(token); | ||
return createAuthResultWithBearerToken(token); | ||
} catch (IllegalArgumentException e) { | ||
LOG.error("Bearer token validation failed.", e); | ||
throw CallStatus.UNAUTHENTICATED.toRuntimeException(); | ||
} | ||
} | ||
|
||
/** | ||
* Generates a bearer token, parses client properties from incoming headers, then creates a | ||
* UserSession associated with the generated token and client properties. | ||
* | ||
* @param authResult the AuthResult from initial authentication, with peer identity captured. | ||
* @param incomingHeaders the CallHeaders to parse client properties from. | ||
* @return an an AuthResult with the bearer token and peer identity. | ||
*/ | ||
@VisibleForTesting | ||
AuthResult getAuthResultWithBearerToken(AuthResult authResult, CallHeaders incomingHeaders) { | ||
final String username = authResult.getPeerIdentity(); | ||
// final String token = DremioFlightAuthUtils.createUserSessionWithTokenAndProperties( | ||
// tokenManagerProvider, | ||
// username); | ||
|
||
return createAuthResultWithBearerToken(username); | ||
} | ||
|
||
/** | ||
* Helper method to create an AuthResult. | ||
* | ||
* @param token the token to create a UserSession for. | ||
* @return a new AuthResult with functionality to add given bearer token to the outgoing header. | ||
*/ | ||
private AuthResult createAuthResultWithBearerToken(String token) { | ||
return new AuthResult() { | ||
@Override | ||
public void appendToOutgoingHeaders(CallHeaders outgoingHeaders) { | ||
outgoingHeaders.insert(Auth2Constants.AUTHORIZATION_HEADER, | ||
Auth2Constants.BEARER_PREFIX + token); | ||
} | ||
|
||
@Override | ||
public String getPeerIdentity() { | ||
return token; | ||
} | ||
}; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...e/src/main/java/org/apache/doris/service/arrowflight/FlightServerCredentialValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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. | ||
// This file is copied from | ||
// https://github.com/dremio/dremio-oss/blob/master/services/arrow-flight/src/main/java/com/dremio/service/flight/ServerCookieMiddleware.java | ||
// and modified by Doris | ||
|
||
package org.apache.doris.service.arrowflight; | ||
|
||
import org.apache.doris.analysis.UserIdentity; | ||
import org.apache.doris.catalog.Env; | ||
import org.apache.doris.common.AuthenticationException; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Lists; | ||
import org.apache.arrow.flight.CallStatus; | ||
import org.apache.arrow.flight.auth2.BasicCallHeaderAuthenticator; | ||
import org.apache.arrow.flight.auth2.CallHeaderAuthenticator.AuthResult; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Dremio authentication specialized CredentialValidator implementation. | ||
*/ | ||
public class FlightServerCredentialValidator implements BasicCallHeaderAuthenticator.CredentialValidator { | ||
private static final Logger LOG = LogManager.getLogger(FlightServerCredentialValidator.class); | ||
|
||
/** | ||
* Authenticates against Dremio with the provided username and password. | ||
* | ||
* @param username Dremio username. | ||
* @param password Dremio user password. | ||
* @return AuthResult with username as the peer identity. | ||
*/ | ||
@Override | ||
public AuthResult validate(String username, String password) { | ||
// String remoteIp = context.getMysqlChannel().getRemoteIp(); | ||
String remoteIp = "0.0.0.0"; | ||
List<UserIdentity> currentUserIdentity = Lists.newArrayList(); | ||
|
||
try { | ||
Env.getCurrentEnv().getAuth().checkPlainPassword(username, remoteIp, password, currentUserIdentity); | ||
} catch (AuthenticationException e) { | ||
LOG.error("Unable to authenticate user {}", username, e); | ||
final String errorMessage = "Unable to authenticate user " + username + ", exception: " + e.getMessage(); | ||
throw CallStatus.UNAUTHENTICATED.withCause(e).withDescription(errorMessage).toRuntimeException(); | ||
} | ||
Preconditions.checkState(currentUserIdentity.size() == 1); | ||
// context.setCurrentUserIdentity(currentUserIdentity.get(0)); | ||
// context.setRemoteIP(remoteIp); | ||
org.apache.doris.service.arrowflight.AuthResult authResult = org.apache.doris.service.arrowflight.AuthResult.of( | ||
username); | ||
return authResult::getUserName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters