Skip to content

[ISSUE #3515] Do some code optimization[AuthTokenUtils] #3644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void list(HttpExchange httpExchange) throws IOException {
});

String result = JsonUtils.toJSONString(getClientResponseList);
httpExchange.sendResponseHeaders(200, result.getBytes().length);
httpExchange.sendResponseHeaders(200, result.getBytes(Constants.DEFAULT_CHARSET).length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here Constants.DEFAULT_CHARSET needs to be imported:
org.apache.eventmesh.common.Constants;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

out.write(result.getBytes());
} catch (Exception e) {
StringWriter writer = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import org.apache.eventmesh.api.acl.AclProperties;
import org.apache.eventmesh.api.exception.AclException;
import org.apache.eventmesh.common.config.CommonConfiguration;
import org.apache.eventmesh.common.utils.ConfigurationContextUtil;

import org.apache.commons.lang3.StringUtils;

Expand All @@ -32,6 +30,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Objects;
import java.util.Set;

import io.jsonwebtoken.Claims;
Expand All @@ -52,88 +51,19 @@ public static void authTokenByPublicKey(AclProperties aclProperties) {
+ aclProperties.getTopic());
}
String publicKeyUrl = null;
token = token.replace("Bearer ", "");
for (String key : ConfigurationContextUtil.KEYS) {
CommonConfiguration commonConfiguration = ConfigurationContextUtil.get(key);
if (null == commonConfiguration) {
continue;
}
if (StringUtils.isBlank(commonConfiguration.getEventMeshSecurityPublickey())) {
throw new AclException("publicKeyUrl cannot be null");
}
publicKeyUrl = commonConfiguration.getEventMeshSecurityPublickey();
}
byte[] validationKeyBytes = new byte[0];
try {
validationKeyBytes = Files.readAllBytes(Paths.get(publicKeyUrl));
X509EncodedKeySpec spec = new X509EncodedKeySpec(validationKeyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
Key validationKey = kf.generatePublic(spec);
JwtParser signedParser = Jwts.parserBuilder().setSigningKey(validationKey).build();
Jwt<?, Claims> signJwt = signedParser.parseClaimsJws(token);
String sub = signJwt.getBody().get("sub", String.class);
if (!sub.contains(aclProperties.getExtendedField("group").toString()) && !sub.contains("pulsar-admin")) {
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access eventMesh:"
+ aclProperties.getTopic());
}
} catch (IOException e) {
throw new AclException("public key read error!", e);
} catch (NoSuchAlgorithmException e) {
throw new AclException("no such RSA algorithm!", e);
} catch (InvalidKeySpecException e) {
throw new AclException("invalid public key spec!", e);
} catch (JwtException e) {
throw new AclException("invalid token!", e);
}

String sub = validateTokenAndGetSub(token, publicKeyUrl, aclProperties);
} else {
{
throw new AclException("invalid token!");
}
throw new AclException("invalid token!");
}
}

public static void helloTaskAuthTokenByPublicKey(AclProperties aclProperties) {
String token = aclProperties.getToken();
if (StringUtils.isNotBlank(token)) {
String publicKeyUrl = null;
token = token.replace("Bearer ", "");
for (String key : ConfigurationContextUtil.KEYS) {
CommonConfiguration commonConfiguration = ConfigurationContextUtil.get(key);
if (null == commonConfiguration) {
continue;
}
if (StringUtils.isBlank(commonConfiguration.getEventMeshSecurityPublickey())) {
throw new AclException("publicKeyUrl cannot be null");
}
publicKeyUrl = commonConfiguration.getEventMeshSecurityPublickey();
}
byte[] validationKeyBytes = new byte[0];
try {
validationKeyBytes = Files.readAllBytes(Paths.get(publicKeyUrl));
X509EncodedKeySpec spec = new X509EncodedKeySpec(validationKeyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
Key validationKey = kf.generatePublic(spec);
JwtParser signedParser = Jwts.parserBuilder().setSigningKey(validationKey).build();
Jwt<?, Claims> signJwt = signedParser.parseClaimsJws(token);
String sub = signJwt.getBody().get("sub", String.class);
if (!sub.contains(aclProperties.getExtendedField("group").toString()) && !sub.contains("pulsar-admin")) {
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access eventMesh:"
+ aclProperties.getTopic());
}
} catch (IOException e) {
throw new AclException("public key read error!", e);
} catch (NoSuchAlgorithmException e) {
throw new AclException("no such RSA algorithm!", e);
} catch (InvalidKeySpecException e) {
throw new AclException("invalid public key spec!", e);
} catch (JwtException e) {
throw new AclException("invalid token!", e);
}
String sub = validateTokenAndGetSub(token, publicKeyUrl, aclProperties);
} else {
{
throw new AclException("invalid token!");
}
throw new AclException("invalid token!");
}
}

Expand All @@ -143,12 +73,36 @@ public static boolean authAccess(AclProperties aclProperties) {

Set<String> groupTopics = (Set<String>) aclProperties.getExtendedField("topics");

if (groupTopics.contains(topic)) {
return true;
} else {
return false;
}
return groupTopics.contains(topic);

}

public static String validateTokenAndGetSub(String token, String publicKeyUrl, AclProperties aclProperties) {
String sub = null;
token = token.replace("Bearer ", "");
byte[] validationKeyBytes;
try {
validationKeyBytes = Files.readAllBytes(Paths.get(Objects.requireNonNull(publicKeyUrl)));
X509EncodedKeySpec spec = new X509EncodedKeySpec(validationKeyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
Key validationKey = kf.generatePublic(spec);
JwtParser signedParser = Jwts.parserBuilder().setSigningKey(validationKey).build();
Jwt<?, Claims> signJwt = signedParser.parseClaimsJws(token);
sub = signJwt.getBody().get("sub", String.class);
if (!sub.contains(aclProperties.getExtendedField("group").toString()) && !sub.contains("pulsar-admin")) {
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access eventMesh:"
+ aclProperties.getTopic());
}
} catch (IOException e) {
throw new AclException("public key read error!", e);
} catch (NoSuchAlgorithmException e) {
throw new AclException("no such RSA algorithm!", e);
} catch (InvalidKeySpecException e) {
throw new AclException("invalid public key spec!", e);
} catch (JwtException e) {
throw new AclException("invalid token!", e);
}
return sub;
}

}