-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mysql client OpenID Connect Pluggable Authentication
- Loading branch information
1 parent
77e0535
commit f3179bd
Showing
14 changed files
with
470 additions
and
1 deletion.
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
37 changes: 37 additions & 0 deletions
37
fe/fe-core/src/main/java/com/starrocks/authentication/JwkMgr.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,37 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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 com.starrocks.authentication; | ||
|
||
import com.nimbusds.jose.jwk.JWKSet; | ||
import com.starrocks.StarRocksFE; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.text.ParseException; | ||
|
||
public class JwkMgr { | ||
public JWKSet getJwkSet(String jwksUrl) throws IOException, ParseException { | ||
InputStream jwksInputStream; | ||
if (jwksUrl.startsWith("http://") || jwksUrl.startsWith("https://")) { | ||
jwksInputStream = new URL(jwksUrl).openStream(); | ||
} else { | ||
String filePath = StarRocksFE.STARROCKS_HOME_DIR + "/conf/" + jwksUrl; | ||
jwksInputStream = new FileInputStream(filePath); | ||
} | ||
return JWKSet.load(jwksInputStream); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...-core/src/main/java/com/starrocks/authentication/OpenIdConnectAuthenticationProvider.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,72 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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 com.starrocks.authentication; | ||
|
||
import com.nimbusds.jose.jwk.JWKSet; | ||
import com.starrocks.mysql.MysqlPassword; | ||
import com.starrocks.mysql.MysqlProto; | ||
import com.starrocks.mysql.privilege.AuthPlugin; | ||
import com.starrocks.server.GlobalStateMgr; | ||
import com.starrocks.sql.ast.UserAuthOption; | ||
import com.starrocks.sql.ast.UserIdentity; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.text.ParseException; | ||
|
||
public class OpenIdConnectAuthenticationProvider implements AuthenticationProvider { | ||
public static final String PLUGIN_NAME = AuthPlugin.AUTHENTICATION_OPENID_CONNECT.name(); | ||
|
||
private final String jwksUrl; | ||
private final String principalFiled; | ||
private final String requireIssuer; | ||
private final String requireAudience; | ||
|
||
public OpenIdConnectAuthenticationProvider(String jwksUrl, String principalFiled, | ||
String requireIssuer, String requireAudience) { | ||
this.jwksUrl = jwksUrl; | ||
this.principalFiled = principalFiled; | ||
this.requireIssuer = requireIssuer; | ||
this.requireAudience = requireAudience; | ||
} | ||
|
||
@Override | ||
public UserAuthenticationInfo analyzeAuthOption(UserIdentity userIdentity, UserAuthOption userAuthOption) | ||
throws AuthenticationException { | ||
UserAuthenticationInfo info = new UserAuthenticationInfo(); | ||
info.setAuthPlugin(PLUGIN_NAME); | ||
info.setPassword(MysqlPassword.EMPTY_PASSWORD); | ||
info.setOrigUserHost(userIdentity.getUser(), userIdentity.getHost()); | ||
info.setTextForAuthPlugin(userAuthOption == null ? null : userAuthOption.getAuthString()); | ||
return info; | ||
} | ||
|
||
@Override | ||
public void authenticate(String user, String host, byte[] authResponse, byte[] randomString, | ||
UserAuthenticationInfo authenticationInfo) throws AuthenticationException { | ||
ByteBuffer authBuffer = ByteBuffer.wrap(authResponse); | ||
//1 Byte for capability mysql client | ||
MysqlProto.readInt1(authBuffer); | ||
byte[] openIdConnect = MysqlProto.readLenEncodedString(authBuffer); | ||
|
||
JWKSet jwkSet; | ||
try { | ||
jwkSet = GlobalStateMgr.getCurrentState().getJwkMgr().getJwkSet(jwksUrl); | ||
} catch (IOException | ParseException e) { | ||
throw new AuthenticationException(e.getMessage()); | ||
} | ||
OpenIdConnectVerifier.verify(new String(openIdConnect), user, jwkSet, principalFiled, requireIssuer, requireAudience); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
fe/fe-core/src/main/java/com/starrocks/authentication/OpenIdConnectVerifier.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,86 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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 com.starrocks.authentication; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jose.crypto.RSASSAVerifier; | ||
import com.nimbusds.jose.jwk.JWK; | ||
import com.nimbusds.jose.jwk.JWKSet; | ||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import com.nimbusds.jwt.SignedJWT; | ||
import org.json.JSONObject; | ||
|
||
import java.security.interfaces.RSAPublicKey; | ||
import java.text.ParseException; | ||
|
||
public class OpenIdConnectVerifier { | ||
|
||
public static void verify(String oidcToken, | ||
String userName, | ||
JWKSet jwkSet, | ||
String principalFiled, | ||
String requiredIssuer, | ||
String requiredAudience) throws AuthenticationException { | ||
try { | ||
JSONObject openIdConnectJson = new JSONObject(oidcToken); | ||
String accessToken = openIdConnectJson.getString("access_token"); | ||
OpenIdConnectVerifier.verifyJWT(accessToken, jwkSet); | ||
|
||
String idToken = openIdConnectJson.getString("id_token"); | ||
OpenIdConnectVerifier.verifyJWT(idToken, jwkSet); | ||
|
||
SignedJWT signedJWT = SignedJWT.parse(idToken); | ||
JWTClaimsSet claims = signedJWT.getJWTClaimsSet(); | ||
String jwtUserName = claims.getStringClaim(principalFiled); | ||
|
||
if (jwtUserName == null) { | ||
throw new AuthenticationException("Can not get specified principal " + principalFiled); | ||
} | ||
|
||
if (!jwtUserName.equalsIgnoreCase(userName)) { | ||
throw new AuthenticationException("Login name " + userName + " is not matched to user " + jwtUserName); | ||
} | ||
|
||
if (requiredIssuer != null && !requiredIssuer.isEmpty() && !requiredIssuer.equals(claims.getIssuer())) { | ||
throw new AuthenticationException("Issuer (iss) field " + claims.getIssuer() + " is invalid"); | ||
} | ||
|
||
if (requiredAudience != null && !requiredAudience.isEmpty() && !claims.getAudience().contains(requiredAudience)) { | ||
throw new AuthenticationException("Audience (aud) field " + claims.getAudience() + " is invalid"); | ||
} | ||
} catch (Exception e) { | ||
throw new AuthenticationException(e.getMessage()); | ||
} | ||
} | ||
|
||
private static void verifyJWT(String jwt, JWKSet jwkSet) throws AuthenticationException, ParseException, JOSEException { | ||
Preconditions.checkNotNull(jwt); | ||
|
||
SignedJWT signedJWT = SignedJWT.parse(jwt); | ||
String kid = signedJWT.getHeader().getKeyID(); | ||
|
||
JWK jwk = jwkSet.getKeyByKeyId(kid); | ||
if (jwk == null) { | ||
throw new AuthenticationException("Cannot find public key for kid: " + kid); | ||
} | ||
|
||
RSAPublicKey publicKey = jwk.toRSAKey().toRSAPublicKey(); | ||
RSASSAVerifier verifier = new RSASSAVerifier(publicKey); | ||
if (!signedJWT.verify(verifier)) { | ||
throw new AuthenticationException("JWT with kid " + kid + " is invalid"); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.