Skip to content

Commit

Permalink
Merge branch 'apache:develop' into optimize-isSetEqual-method
Browse files Browse the repository at this point in the history
  • Loading branch information
Willhow-Gao authored May 7, 2024
2 parents d656b4c + 1a2fc17 commit 049d20d
Show file tree
Hide file tree
Showing 38 changed files with 461 additions and 66 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ jobs:
with:
fail_ci_if_error: true
verbose: true
token: cf0cba0a-22f8-4580-89ab-4f1dec3bda6f
7 changes: 7 additions & 0 deletions .github/workflows/maven.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ jobs:
cache: "maven"
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Upload JVM crash logs
if: failure()
uses: actions/upload-artifact@v4
with:
name: jvm-crash-logs
path: /Users/runner/work/rocketmq/rocketmq/auth/hs_err_pid*.log
retention-days: 1
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ java_library(
"@maven//:org_awaitility_awaitility",
"@maven//:org_openjdk_jmh_jmh_core",
"@maven//:org_openjdk_jmh_jmh_generator_annprocess",
"@maven//:org_mockito_mockito_junit_jupiter",
],
)
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ maven_install(
"com.alipay.sofa:jraft-core:1.3.14",
"com.alipay.sofa:hessian:3.3.6",
"io.netty:netty-tcnative-boringssl-static:2.0.48.Final",
"org.mockito:mockito-junit-jupiter:4.11.0",
],
fetch_sources = True,
repositories = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,12 @@ public static PlainAccessResource parse(RemotingCommand request, String remoteAd
switch (request.getCode()) {
case RequestCode.SEND_MESSAGE:
final String topic = request.getExtFields().get("topic");
if (PlainAccessResource.isRetryTopic(topic)) {
accessResource.addResourceAndPerm(getRetryTopic(request.getExtFields().get("group")), Permission.SUB);
} else {
accessResource.addResourceAndPerm(topic, Permission.PUB);
}
accessResource.addResourceAndPerm(topic, PlainAccessResource.isRetryTopic(topic) ? Permission.SUB : Permission.PUB);
break;
case RequestCode.SEND_MESSAGE_V2:
case RequestCode.SEND_BATCH_MESSAGE:
final String topicV2 = request.getExtFields().get("b");
if (PlainAccessResource.isRetryTopic(topicV2)) {
accessResource.addResourceAndPerm(getRetryTopic(request.getExtFields().get("a")), Permission.SUB);
} else {
accessResource.addResourceAndPerm(topicV2, Permission.PUB);
}
accessResource.addResourceAndPerm(topicV2, PlainAccessResource.isRetryTopic(topicV2) ? Permission.SUB : Permission.PUB);
break;
case RequestCode.CONSUMER_SEND_MSG_BACK:
accessResource.addResourceAndPerm(getRetryTopic(request.getExtFields().get("group")), Permission.SUB);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.rocketmq.acl.plain;

import java.util.HashMap;
import java.util.Map;
import org.apache.rocketmq.acl.common.Permission;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.protocol.RequestCode;
import org.apache.rocketmq.remoting.protocol.header.SendMessageRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.SendMessageRequestHeaderV2;
import org.junit.Assert;
import org.junit.Test;

public class PlainAccessResourceTest {
public static final String DEFAULT_TOPIC = "topic-acl";
public static final String DEFAULT_PRODUCER_GROUP = "PID_acl";
public static final String DEFAULT_CONSUMER_GROUP = "GID_acl";
public static final String DEFAULT_REMOTE_ADDR = "192.128.1.1";

@Test
public void testParseSendNormal() {
SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
requestHeader.setTopic(DEFAULT_TOPIC);
requestHeader.setProducerGroup(DEFAULT_PRODUCER_GROUP);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
request.makeCustomHeaderToNet();
PlainAccessResource accessResource = PlainAccessResource.parse(request, DEFAULT_REMOTE_ADDR);

Map<String, Byte> permMap = new HashMap<>(1);
permMap.put(DEFAULT_TOPIC, Permission.PUB);

Assert.assertEquals(permMap, accessResource.getResourcePermMap());
}

@Test
public void testParseSendRetry() {
SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
requestHeader.setTopic(MixAll.getRetryTopic(DEFAULT_CONSUMER_GROUP));
requestHeader.setProducerGroup(DEFAULT_PRODUCER_GROUP);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
request.makeCustomHeaderToNet();
PlainAccessResource accessResource = PlainAccessResource.parse(request, DEFAULT_REMOTE_ADDR);

Map<String, Byte> permMap = new HashMap<>(1);
permMap.put(MixAll.getRetryTopic(DEFAULT_CONSUMER_GROUP), Permission.SUB);

Assert.assertEquals(permMap, accessResource.getResourcePermMap());
}

@Test
public void testParseSendNormalV2() {
SendMessageRequestHeaderV2 requestHeaderV2 = new SendMessageRequestHeaderV2();
requestHeaderV2.setB(DEFAULT_TOPIC);
requestHeaderV2.setA(DEFAULT_PRODUCER_GROUP);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE_V2, requestHeaderV2);
request.makeCustomHeaderToNet();
PlainAccessResource accessResource = PlainAccessResource.parse(request, DEFAULT_REMOTE_ADDR);

Map<String, Byte> permMap = new HashMap<>(1);
permMap.put(DEFAULT_TOPIC, Permission.PUB);

Assert.assertEquals(permMap, accessResource.getResourcePermMap());
}

@Test
public void testParseSendRetryV2() {
SendMessageRequestHeaderV2 requestHeaderV2 = new SendMessageRequestHeaderV2();
requestHeaderV2.setB(MixAll.getRetryTopic(DEFAULT_CONSUMER_GROUP));
requestHeaderV2.setA(DEFAULT_PRODUCER_GROUP);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE_V2, requestHeaderV2);
request.makeCustomHeaderToNet();
PlainAccessResource accessResource = PlainAccessResource.parse(request, DEFAULT_REMOTE_ADDR);

Map<String, Byte> permMap = new HashMap<>(1);
permMap.put(MixAll.getRetryTopic(DEFAULT_CONSUMER_GROUP), Permission.SUB);

Assert.assertEquals(permMap, accessResource.getResourcePermMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected HandlerChain<DefaultAuthenticationContext, CompletableFuture<Void>> ne
.addNext(new DefaultAuthenticationHandler(this.authConfig, metadataService));
}

private void doAuditLog(DefaultAuthenticationContext context, Throwable ex) {
protected void doAuditLog(DefaultAuthenticationContext context, Throwable ex) {
if (StringUtils.isBlank(context.getUsername())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected HandlerChain<DefaultAuthorizationContext, CompletableFuture<Void>> new
.addNext(new AclAuthorizationHandler(authConfig, metadataService));
}

private void doAuditLog(DefaultAuthorizationContext context, Throwable ex) {
protected void doAuditLog(DefaultAuthorizationContext context, Throwable ex) {
if (context.getSubject() == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.rocketmq.auth.authentication.model.User;
import org.apache.rocketmq.auth.config.AuthConfig;
import org.apache.rocketmq.auth.helper.AuthTestHelper;
import org.apache.rocketmq.common.MixAll;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -39,6 +40,9 @@ public class AuthenticationEvaluatorTest {

@Before
public void setUp() throws Exception {
if (MixAll.isMac()) {
return;
}
this.authConfig = AuthTestHelper.createDefaultConfig();
this.evaluator = new AuthenticationEvaluator(authConfig);
this.authenticationMetadataManager = AuthenticationFactory.getMetadataManager(authConfig);
Expand All @@ -47,12 +51,18 @@ public void setUp() throws Exception {

@After
public void tearDown() throws Exception {
if (MixAll.isMac()) {
return;
}
this.clearAllUsers();
this.authenticationMetadataManager.shutdown();
}

@Test
public void evaluate1() {
if (MixAll.isMac()) {
return;
}
User user = User.of("test", "test");
this.authenticationMetadataManager.createUser(user);

Expand All @@ -66,6 +76,9 @@ public void evaluate1() {

@Test
public void evaluate2() {
if (MixAll.isMac()) {
return;
}
DefaultAuthenticationContext context = new DefaultAuthenticationContext();
context.setRpcCode("11");
context.setUsername("test");
Expand All @@ -76,6 +89,9 @@ public void evaluate2() {

@Test
public void evaluate3() {
if (MixAll.isMac()) {
return;
}
User user = User.of("test", "test");
this.authenticationMetadataManager.createUser(user);

Expand All @@ -89,6 +105,9 @@ public void evaluate3() {

@Test
public void evaluate4() {
if (MixAll.isMac()) {
return;
}
this.authConfig.setAuthenticationWhitelist("11");
this.evaluator = new AuthenticationEvaluator(authConfig);

Expand All @@ -102,6 +121,9 @@ public void evaluate4() {

@Test
public void evaluate5() {
if (MixAll.isMac()) {
return;
}
this.authConfig.setAuthenticationEnabled(false);
this.evaluator = new AuthenticationEvaluator(authConfig);

Expand All @@ -114,6 +136,9 @@ public void evaluate5() {
}

private void clearAllUsers() {
if (MixAll.isMac()) {
return;
}
List<User> users = this.authenticationMetadataManager.listUser(null).join();
if (CollectionUtils.isEmpty(users)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.rocketmq.auth.authentication.model.User;
import org.apache.rocketmq.auth.config.AuthConfig;
import org.apache.rocketmq.auth.helper.AuthTestHelper;
import org.apache.rocketmq.common.MixAll;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -36,19 +37,28 @@ public class AuthenticationMetadataManagerTest {

@Before
public void setUp() throws Exception {
if (MixAll.isMac()) {
return;
}
this.authConfig = AuthTestHelper.createDefaultConfig();
this.authenticationMetadataManager = AuthenticationFactory.getMetadataManager(this.authConfig);
this.clearAllUsers();
}

@After
public void tearDown() throws Exception {
if (MixAll.isMac()) {
return;
}
this.clearAllUsers();
this.authenticationMetadataManager.shutdown();
}

@Test
public void createUser() {
if (MixAll.isMac()) {
return;
}
User user = User.of("test", "test");
this.authenticationMetadataManager.createUser(user).join();
user = this.authenticationMetadataManager.getUser("test").join();
Expand Down Expand Up @@ -77,6 +87,9 @@ public void createUser() {

@Test
public void updateUser() {
if (MixAll.isMac()) {
return;
}
User user = User.of("test", "test");
this.authenticationMetadataManager.createUser(user).join();
user = this.authenticationMetadataManager.getUser("test").join();
Expand Down Expand Up @@ -113,6 +126,9 @@ public void updateUser() {

@Test
public void deleteUser() {
if (MixAll.isMac()) {
return;
}
User user = User.of("test", "test");
this.authenticationMetadataManager.createUser(user).join();
user = this.authenticationMetadataManager.getUser("test").join();
Expand All @@ -126,6 +142,9 @@ public void deleteUser() {

@Test
public void getUser() {
if (MixAll.isMac()) {
return;
}
User user = User.of("test", "test");
this.authenticationMetadataManager.createUser(user).join();
user = this.authenticationMetadataManager.getUser("test").join();
Expand All @@ -140,6 +159,9 @@ public void getUser() {

@Test
public void listUser() {
if (MixAll.isMac()) {
return;
}
List<User> users = this.authenticationMetadataManager.listUser(null).join();
Assert.assertTrue(CollectionUtils.isEmpty(users));

Expand Down
Loading

0 comments on commit 049d20d

Please sign in to comment.