Skip to content
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

[ISSUE #9156] Use fastjson2 in AclUtils#getAclRPCHook #9157

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Map;
import java.util.SortedMap;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.org.slf4j.Logger;
Expand Down
124 changes: 76 additions & 48 deletions acl/src/test/java/org/apache/rocketmq/acl/common/AclUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.rocketmq.acl.common;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.acl.plain.PlainAccessData;
import org.apache.rocketmq.common.PlainAccessConfig;
Expand All @@ -32,8 +32,15 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class AclUtilsTest {

@Test
Expand All @@ -47,7 +54,7 @@ public void testGetAddresses() {
addressList.add("1.1.1.2");
addressList.add("1.1.1.3");
addressList.add("1.1.1.4");
Assert.assertEquals(newAddressList, addressList);
assertEquals(newAddressList, addressList);
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any benefits to this modification?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use static import to reduce redundant code.

Copy link
Contributor

Choose a reason for hiding this comment

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

They are both one line of code. Changing it will lose the git blame.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I will return to them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It has been adjusted. Please review it again. Thank you.


// IPv6 test
String ipv6Address = "1:ac41:9987::bb22:666:{1,2,3,4}";
Expand All @@ -60,7 +67,7 @@ public void testGetAddresses() {
ipv6AddressList.add("1:ac41:9987::bb22:666:2");
ipv6AddressList.add("1:ac41:9987::bb22:666:3");
ipv6AddressList.add("1:ac41:9987::bb22:666:4");
Assert.assertEquals(newIPv6AddressList, ipv6AddressList);
assertEquals(newIPv6AddressList, ipv6AddressList);
}

@Test
Expand All @@ -70,9 +77,9 @@ public void testIsScope_StringArray() {
for (int i = 0; i < 6; i++) {
boolean isScope = AclUtils.isScope(address, 4);
if (i == 3) {
Assert.assertTrue(isScope);
assertTrue(isScope);
} else {
Assert.assertFalse(isScope);
assertFalse(isScope);
}
address = address + ".12";
}
Expand All @@ -82,46 +89,46 @@ public void testIsScope_StringArray() {
public void testIsScope_Array() {
String[] address = StringUtils.split("12.12.12.12", ".");
boolean isScope = AclUtils.isScope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);
isScope = AclUtils.isScope(address, 3);
Assert.assertTrue(isScope);
assertTrue(isScope);

address = StringUtils.split("12.12.1222.1222", ".");
isScope = AclUtils.isScope(address, 4);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isScope(address, 3);
Assert.assertFalse(isScope);
assertFalse(isScope);

// IPv6 test
address = StringUtils.split("1050:0000:0000:0000:0005:0600:300c:326b", ":");
isScope = AclUtils.isIPv6Scope(address, 8);
Assert.assertTrue(isScope);
assertTrue(isScope);
isScope = AclUtils.isIPv6Scope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);

address = StringUtils.split("1050:9876:0000:0000:0005:akkg:300c:326b", ":");
isScope = AclUtils.isIPv6Scope(address, 8);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isIPv6Scope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);

address = StringUtils.split(AclUtils.expandIP("1050::0005:akkg:300c:326b", 8), ":");
isScope = AclUtils.isIPv6Scope(address, 8);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isIPv6Scope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);
}

@Test
public void testIsScope_String() {
for (int i = 0; i < 256; i++) {
boolean isScope = AclUtils.isScope(i + "");
Assert.assertTrue(isScope);
assertTrue(isScope);
}
boolean isScope = AclUtils.isScope("-1");
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isScope("256");
Assert.assertFalse(isScope);
assertFalse(isScope);
}

@Test
Expand All @@ -131,9 +138,9 @@ public void testIsScope_Integral() {
Assert.assertTrue(isScope);
}
boolean isScope = AclUtils.isScope(-1);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isScope(256);
Assert.assertFalse(isScope);
assertFalse(isScope);

// IPv6 test
int min = Integer.parseInt("0", 16);
Expand All @@ -143,71 +150,71 @@ public void testIsScope_Integral() {
Assert.assertTrue(isScope);
}
isScope = AclUtils.isIPv6Scope(-1);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isIPv6Scope(max + 1);
Assert.assertFalse(isScope);
assertFalse(isScope);
}

@Test
public void testIsAsterisk() {
boolean isAsterisk = AclUtils.isAsterisk("*");
Assert.assertTrue(isAsterisk);
assertTrue(isAsterisk);

isAsterisk = AclUtils.isAsterisk(",");
Assert.assertFalse(isAsterisk);
assertFalse(isAsterisk);
}

@Test
public void testIsComma() {
boolean isColon = AclUtils.isComma(",");
Assert.assertTrue(isColon);
assertTrue(isColon);

isColon = AclUtils.isComma("-");
Assert.assertFalse(isColon);
assertFalse(isColon);
}

@Test
public void testIsMinus() {
boolean isMinus = AclUtils.isMinus("-");
Assert.assertTrue(isMinus);
assertTrue(isMinus);

isMinus = AclUtils.isMinus("*");
Assert.assertFalse(isMinus);
assertFalse(isMinus);
}

@Test
public void testV6ipProcess() {
String remoteAddr = "5::7:6:1-200:*";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0007:0006");

remoteAddr = "5::7:6:1-200";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
remoteAddr = "5::7:6:*";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");

remoteAddr = "5:7:6:*";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0007:0006");
}

@Test
public void testExpandIP() {
Assert.assertEquals(AclUtils.expandIP("::", 8), "0000:0000:0000:0000:0000:0000:0000:0000");
Assert.assertEquals(AclUtils.expandIP("::1", 8), "0000:0000:0000:0000:0000:0000:0000:0001");
Assert.assertEquals(AclUtils.expandIP("3::", 8), "0003:0000:0000:0000:0000:0000:0000:0000");
Assert.assertEquals(AclUtils.expandIP("2::2", 8), "0002:0000:0000:0000:0000:0000:0000:0002");
Assert.assertEquals(AclUtils.expandIP("4::aac4:92", 8), "0004:0000:0000:0000:0000:0000:AAC4:0092");
Assert.assertEquals(AclUtils.expandIP("ab23:56:901a::cc6:765:bb:9011", 8), "AB23:0056:901A:0000:0CC6:0765:00BB:9011");
Assert.assertEquals(AclUtils.expandIP("ab23:56:901a:1:cc6:765:bb:9011", 8), "AB23:0056:901A:0001:0CC6:0765:00BB:9011");
Assert.assertEquals(AclUtils.expandIP("5::7:6", 6), "0005:0000:0000:0000:0007:0006");
assertEquals(AclUtils.expandIP("::", 8), "0000:0000:0000:0000:0000:0000:0000:0000");
assertEquals(AclUtils.expandIP("::1", 8), "0000:0000:0000:0000:0000:0000:0000:0001");
assertEquals(AclUtils.expandIP("3::", 8), "0003:0000:0000:0000:0000:0000:0000:0000");
assertEquals(AclUtils.expandIP("2::2", 8), "0002:0000:0000:0000:0000:0000:0000:0002");
assertEquals(AclUtils.expandIP("4::aac4:92", 8), "0004:0000:0000:0000:0000:0000:AAC4:0092");
assertEquals(AclUtils.expandIP("ab23:56:901a::cc6:765:bb:9011", 8), "AB23:0056:901A:0000:0CC6:0765:00BB:9011");
assertEquals(AclUtils.expandIP("ab23:56:901a:1:cc6:765:bb:9011", 8), "AB23:0056:901A:0001:0CC6:0765:00BB:9011");
assertEquals(AclUtils.expandIP("5::7:6", 6), "0005:0000:0000:0000:0007:0006");
}

@SuppressWarnings("unchecked")
@Test
public void testGetYamlDataObject() throws IOException {
try (InputStream is = AclUtilsTest.class.getClassLoader().getResourceAsStream("conf/plain_acl_correct.yml")) {
Map<String, Object> map = AclUtils.getYamlDataObject(is, Map.class);
Assert.assertNotNull(map);
Assert.assertFalse(map.isEmpty());
assertNotNull(map);
assertFalse(map.isEmpty());
}
}

Expand All @@ -225,7 +232,7 @@ private static String randomTmpFile() {
public void writeDataObject2YamlFileTest() throws IOException {
String targetFileName = randomTmpFile();
File transport = new File(targetFileName);
Assert.assertTrue(transport.createNewFile());
assertTrue(transport.createNewFile());
transport.deleteOnExit();

PlainAccessData aclYamlMap = new PlainAccessData();
Expand All @@ -248,14 +255,14 @@ public void writeDataObject2YamlFileTest() throws IOException {
};
accounts.add(accountsMap);
aclYamlMap.setAccounts(accounts);
Assert.assertTrue(AclUtils.writeDataObject(targetFileName, aclYamlMap));
assertTrue(AclUtils.writeDataObject(targetFileName, aclYamlMap));
}

@Test
public void updateExistedYamlFileTest() throws IOException {
String targetFileName = randomTmpFile();
File transport = new File(targetFileName);
Assert.assertTrue(transport.createNewFile());
assertTrue(transport.createNewFile());
transport.deleteOnExit();

PlainAccessData aclYamlMap = new PlainAccessData();
Expand All @@ -279,21 +286,42 @@ public void updateExistedYamlFileTest() throws IOException {

PlainAccessData readableMap = AclUtils.getYamlDataObject(targetFileName, PlainAccessData.class);
List<String> updatedGlobalWhiteRemoteAddrs = readableMap.getGlobalWhiteRemoteAddresses();
Assert.assertEquals("192.168.1.2", updatedGlobalWhiteRemoteAddrs.get(0));
assertEquals("192.168.1.2", updatedGlobalWhiteRemoteAddrs.get(0));
}

@Test
public void getYamlDataIgnoreFileNotFoundExceptionTest() {

JSONObject yamlDataObject = AclUtils.getYamlDataObject("plain_acl.yml", JSONObject.class);
Assert.assertNull(yamlDataObject);
assertNull(yamlDataObject);
}

@Test
public void getAclRPCHookTest() throws IOException {
try (InputStream is = AclUtilsTest.class.getClassLoader().getResourceAsStream("conf/plain_acl_incomplete.yml")) {
RPCHook incompleteContRPCHook = AclUtils.getAclRPCHook(is);
Assert.assertNull(incompleteContRPCHook);
assertNull(incompleteContRPCHook);
}
}

@Test
public void testGetAclRPCHookByFileName() {
RPCHook actual = AclUtils.getAclRPCHook(Objects.requireNonNull(AclUtilsTest.class.getResource("/acl_hook/plain_acl.yml")).getPath());
assertNotNull(actual);
assertTrue(actual instanceof AclClientRPCHook);
assertAclClientRPCHook((AclClientRPCHook) actual);
}

@Test
public void testGetAclRPCHookByInputStream() {
RPCHook actual = AclUtils.getAclRPCHook(Objects.requireNonNull(AclUtilsTest.class.getResourceAsStream("/acl_hook/plain_acl.yml")));
assertNotNull(actual);
assertTrue(actual instanceof AclClientRPCHook);
assertAclClientRPCHook((AclClientRPCHook) actual);
}

private void assertAclClientRPCHook(final AclClientRPCHook actual) {
assertEquals("rocketmq2", actual.getSessionCredentials().getAccessKey());
assertEquals("12345678", actual.getSessionCredentials().getSecretKey());
}
}
21 changes: 21 additions & 0 deletions acl/src/test/resources/acl_hook/plain_acl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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.

## suggested format

- accessKey: rocketmq2
secretKey: 12345678
whiteRemoteAddress: 192.168.1.*
admin: true
Loading