mirror of
https://github.com/apache/rocketmq.git
synced 2026-08-28 20:09:14 +08:00
* Make PlainAccessControlFlowTest portable * Enable PlainAccessControlFlowTest in Bazel RBE * Add guava to acl/test module as an explicit dependency
This commit is contained in:
+23
-22
@@ -22,51 +22,52 @@ java_library(
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//common",
|
||||
"//logging",
|
||||
"//remoting",
|
||||
"//logging",
|
||||
"//srvutil",
|
||||
"@maven//:org_apache_commons_commons_lang3",
|
||||
"@maven//:commons_validator_commons_validator",
|
||||
"@maven//:com_github_luben_zstd_jni",
|
||||
"@maven//:org_lz4_lz4_java",
|
||||
"@maven//:com_alibaba_fastjson",
|
||||
"@maven//:io_netty_netty_all",
|
||||
"@maven//:org_yaml_snakeyaml",
|
||||
"@maven//:commons_codec_commons_codec",
|
||||
"@maven//:org_apache_rocketmq_rocketmq_proto",
|
||||
"@maven//:com_google_protobuf_protobuf_java",
|
||||
"@maven//:com_github_luben_zstd_jni",
|
||||
"@maven//:com_google_guava_guava",
|
||||
"@maven//:com_google_protobuf_protobuf_java",
|
||||
"@maven//:commons_codec_commons_codec",
|
||||
"@maven//:commons_validator_commons_validator",
|
||||
"@maven//:io_netty_netty_all",
|
||||
"@maven//:org_apache_commons_commons_lang3",
|
||||
"@maven//:org_apache_rocketmq_rocketmq_proto",
|
||||
"@maven//:org_lz4_lz4_java",
|
||||
"@maven//:org_yaml_snakeyaml",
|
||||
],
|
||||
)
|
||||
|
||||
java_library(
|
||||
name = "tests",
|
||||
srcs = glob(["src/test/java/**/*.java"]),
|
||||
resources = glob(["src/test/resources/**/*.yml"]),
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
":acl",
|
||||
"//:test_deps",
|
||||
"//common",
|
||||
"//remoting",
|
||||
"//:test_deps",
|
||||
"@maven//:org_apache_commons_commons_lang3",
|
||||
"@maven//:io_netty_netty_all",
|
||||
"@maven//:com_alibaba_fastjson",
|
||||
"@maven//:commons_codec_commons_codec",
|
||||
"@maven//:io_netty_netty_all",
|
||||
"@maven//:org_apache_commons_commons_lang3",
|
||||
"@maven//:org_springframework_spring_core",
|
||||
"@maven//:org_yaml_snakeyaml",
|
||||
"@maven//:commons_codec_commons_codec",
|
||||
"@maven//:com_alibaba_fastjson",
|
||||
"@maven//:com_google_guava_guava",
|
||||
],
|
||||
resources = glob(["src/test/resources/**/*.yml"])
|
||||
)
|
||||
|
||||
GenTestRules(
|
||||
name = "GeneratedTestRules",
|
||||
# The following tests are not hermetic. Fix them later.
|
||||
exclude_tests = [
|
||||
"src/test/java/org/apache/rocketmq/acl/plain/PlainPermissionManagerTest",
|
||||
"src/test/java/org/apache/rocketmq/acl/plain/PlainAccessValidatorTest",
|
||||
],
|
||||
test_files = glob(["src/test/java/**/*Test.java"]),
|
||||
deps = [
|
||||
":tests",
|
||||
],
|
||||
# The following tests are not hermetic. Fix them later.
|
||||
exclude_tests = [
|
||||
"src/test/java/org/apache/rocketmq/acl/plain/PlainAccessControlFlowTest",
|
||||
"src/test/java/org/apache/rocketmq/acl/plain/PlainPermissionManagerTest",
|
||||
"src/test/java/org/apache/rocketmq/acl/plain/PlainAccessValidatorTest",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -75,5 +75,10 @@
|
||||
<artifactId>protobuf-java-util</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -86,7 +86,7 @@ public class PlainPermissionManager {
|
||||
|
||||
public PlainPermissionManager() {
|
||||
this.defaultAclDir = MixAll.dealFilePath(fileHome + File.separator + "conf" + File.separator + "acl");
|
||||
this.defaultAclFile = MixAll.dealFilePath(fileHome + File.separator + System.getProperty("rocketmq.acl.plain.file", "conf/plain_acl.yml"));
|
||||
this.defaultAclFile = MixAll.dealFilePath(fileHome + File.separator + System.getProperty("rocketmq.acl.plain.file", "conf" + File.separator + "plain_acl.yml"));
|
||||
load();
|
||||
watch();
|
||||
}
|
||||
@@ -96,7 +96,7 @@ public class PlainPermissionManager {
|
||||
log.info("The default acl dir {} is not exist", path);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<String> allAclFileFullPath = new ArrayList<>();
|
||||
List<String> allAclFileFullPath = new ArrayList<>();
|
||||
File file = new File(path);
|
||||
File[] files = file.listFiles();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
@@ -165,7 +165,7 @@ public class PlainPermissionManager {
|
||||
plainAccessResourceMap.put(plainAccessResource.getAccessKey(), plainAccessResource);
|
||||
accessKeyTable.put(plainAccessResource.getAccessKey(), currentFile);
|
||||
} else {
|
||||
log.warn("The accesssKey {} is repeated in multiple ACL files", plainAccessResource.getAccessKey());
|
||||
log.warn("The accessKey {} is repeated in multiple ACL files", plainAccessResource.getAccessKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -239,7 +239,6 @@ public class PlainPermissionManager {
|
||||
this.globalWhiteRemoteAddressStrategyMap.put(aclFilePath, globalWhiteRemoteAddressStrategy);
|
||||
}
|
||||
|
||||
|
||||
JSONArray accounts = plainAclConfData.getJSONArray(AclConstants.CONFIG_ACCOUNTS);
|
||||
if (accounts != null && !accounts.isEmpty()) {
|
||||
List<PlainAccessConfig> plainAccessConfigList = accounts.toJavaList(PlainAccessConfig.class);
|
||||
@@ -270,7 +269,6 @@ public class PlainPermissionManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
public String getAclConfigDataVersion() {
|
||||
return this.dataVersion.toJson();
|
||||
@@ -647,7 +645,6 @@ public class PlainPermissionManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Check perm of each resource
|
||||
checkPerm(plainAccessResource, ownedAccess);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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 com.google.common.base.Preconditions;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.UUID;
|
||||
import org.junit.Assert;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
|
||||
public final class AclTestHelper {
|
||||
private AclTestHelper() {
|
||||
}
|
||||
|
||||
private static void copyTo(String path, InputStream src, File dstDir, String flag) throws IOException {
|
||||
Preconditions.checkNotNull(flag);
|
||||
String[] folders = path.split(File.separator);
|
||||
boolean found = false;
|
||||
File dir = dstDir;
|
||||
for (int i = 0; i < folders.length; i++) {
|
||||
if (!found && flag.equals(folders[i])) {
|
||||
found = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (found) {
|
||||
if (i == folders.length - 1) {
|
||||
dir = new File(dir, folders[i]);
|
||||
} else {
|
||||
dir = new File(dir, folders[i]);
|
||||
if (!dir.exists()) {
|
||||
Assert.assertTrue(dir.mkdir());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertTrue(dir.createNewFile());
|
||||
byte[] buffer = new byte[4096];
|
||||
BufferedInputStream bis = new BufferedInputStream(src);
|
||||
int len = 0;
|
||||
try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(dir.toPath()))) {
|
||||
while ((len = bis.read(buffer)) > 0) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void recursiveDelete(File file) {
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
return;
|
||||
}
|
||||
|
||||
File[] files = file.listFiles();
|
||||
for (File f : files) {
|
||||
recursiveDelete(f);
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public static File copyResources(String folder) throws IOException {
|
||||
File home = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString().replace('-', '_'));
|
||||
if (!home.exists()) {
|
||||
home.mkdirs();
|
||||
}
|
||||
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(AclTestHelper.class.getClassLoader());
|
||||
Resource[] resources = resolver.getResources(String.format("classpath:%s/**/*", folder));
|
||||
for (Resource resource : resources) {
|
||||
if (!resource.isReadable()) {
|
||||
continue;
|
||||
}
|
||||
String description = resource.getDescription();
|
||||
int start = description.indexOf('[');
|
||||
int end = description.lastIndexOf(']');
|
||||
String path = description.substring(start + 1, end);
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
copyTo(path, inputStream, home, folder);
|
||||
}
|
||||
}
|
||||
return home;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package org.apache.rocketmq.acl.plain;
|
||||
|
||||
import java.util.Collections;
|
||||
import org.apache.rocketmq.acl.common.AclClientRPCHook;
|
||||
import org.apache.rocketmq.acl.common.AclConstants;
|
||||
import org.apache.rocketmq.acl.common.AclException;
|
||||
@@ -30,9 +31,7 @@ import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader;
|
||||
import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeaderV2;
|
||||
import org.apache.rocketmq.remoting.exception.RemotingCommandException;
|
||||
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
@@ -40,10 +39,7 @@ import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -68,122 +64,48 @@ public class PlainAccessControlFlowTest {
|
||||
public static final String DEFAULT_CONSUMER_AK = "ak22222";
|
||||
|
||||
public static final String DEFAULT_GLOBAL_WHITE_ADDR = "172.16.123.123";
|
||||
public static final List<String> DEFAULT_GLOBAL_WHITE_ADDRS_LIST = Arrays.asList(DEFAULT_GLOBAL_WHITE_ADDR);
|
||||
|
||||
public static final Path EMPTY_ACL_FOLDER_PLAIN_ACL_YML_PATH = Paths.get("src/test/resources/empty_acl_folder_conf/conf/plain_acl.yml");
|
||||
private static final Path EMPTY_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH = Paths.get("src/test/resources/empty_acl_folder_conf/conf/plain_acl.yml.bak");
|
||||
|
||||
|
||||
public static final Path ONLY_ACL_FOLDER_DELETE_YML_PATH = Paths.get("src/test/resources/only_acl_folder_conf/conf/plain_acl.yml");
|
||||
private static final Path ONLY_ACL_FOLDER_PLAIN_ACL_YML_PATH = Paths.get("src/test/resources/only_acl_folder_conf/conf/acl/plain_acl.yml");
|
||||
private static final Path ONLY_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH = Paths.get("src/test/resources/only_acl_folder_conf/conf/acl/plain_acl.yml.bak");
|
||||
|
||||
private static final Path BOTH_ACL_FOLDER_PLAIN_ACL_YML_PATH = Paths.get("src/test/resources/both_acl_file_folder_conf/conf/acl/plain_acl.yml");
|
||||
private static final Path BOTH_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH = Paths.get("src/test/resources/both_acl_file_folder_conf/conf/acl/plain_acl.yml.bak");
|
||||
private static final Path BOTH_CONF_FOLDER_PLAIN_ACL_YML_PATH = Paths.get("src/test/resources/both_acl_file_folder_conf/conf/plain_acl.yml");
|
||||
private static final Path BOTH_CONF_FOLDER_PLAIN_ACL_YML_BAK_PATH = Paths.get("src/test/resources/both_acl_file_folder_conf/conf/plain_acl.yml.bak");
|
||||
|
||||
private boolean isCheckCase1 = false;
|
||||
private boolean isCheckCase2 = false;
|
||||
private boolean isCheckCase3 = false;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* backup ACL config files
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Before
|
||||
public void prepare() throws IOException {
|
||||
|
||||
Files.copy(EMPTY_ACL_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
EMPTY_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
|
||||
Files.copy(ONLY_ACL_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
ONLY_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
|
||||
Files.copy(BOTH_ACL_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
BOTH_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(BOTH_CONF_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
BOTH_CONF_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* restore ACL config files
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@After
|
||||
public void restore() throws IOException {
|
||||
if (this.isCheckCase1) {
|
||||
Files.copy(EMPTY_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
EMPTY_ACL_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
if (this.isCheckCase2) {
|
||||
Files.copy(ONLY_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
ONLY_ACL_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.deleteIfExists(ONLY_ACL_FOLDER_DELETE_YML_PATH);
|
||||
}
|
||||
|
||||
if (this.isCheckCase3) {
|
||||
Files.copy(BOTH_ACL_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
BOTH_ACL_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(BOTH_CONF_FOLDER_PLAIN_ACL_YML_BAK_PATH,
|
||||
BOTH_CONF_FOLDER_PLAIN_ACL_YML_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
public static final List<String> DEFAULT_GLOBAL_WHITE_ADDRS_LIST = Collections.singletonList(DEFAULT_GLOBAL_WHITE_ADDR);
|
||||
|
||||
@Test
|
||||
public void testEmptyAclFolderCase() throws NoSuchFieldException, IllegalAccessException,
|
||||
IOException {
|
||||
String folder = "empty_acl_folder_conf";
|
||||
File home = AclTestHelper.copyResources(folder);
|
||||
System.setProperty("rocketmq.home.dir", home.getAbsolutePath());
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
checkDefaultAclFileExists();
|
||||
testValidationAfterConsecutiveUpdates(plainAccessValidator);
|
||||
testValidationAfterConfigFileChanged(plainAccessValidator);
|
||||
AclTestHelper.recursiveDelete(home);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyAclFolderCase() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
|
||||
this.isCheckCase1 = true;
|
||||
System.setProperty("rocketmq.home.dir", Paths.get("src/test/resources/empty_acl_folder_conf").toString());
|
||||
public void testOnlyAclFolderCase() throws NoSuchFieldException, IllegalAccessException, IOException {
|
||||
String folder = "only_acl_folder_conf";
|
||||
File home = AclTestHelper.copyResources(folder);
|
||||
System.setProperty("rocketmq.home.dir", home.getAbsolutePath());
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
|
||||
checkDefaultAclFileExists(plainAccessValidator);
|
||||
checkDefaultAclFileExists();
|
||||
testValidationAfterConsecutiveUpdates(plainAccessValidator);
|
||||
testValidationAfterConfigFileChanged(plainAccessValidator);
|
||||
|
||||
AclTestHelper.recursiveDelete(home);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnlyAclFolderCase() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
|
||||
this.isCheckCase2 = true;
|
||||
System.setProperty("rocketmq.home.dir", Paths.get("src/test/resources/only_acl_folder_conf").toString());
|
||||
public void testBothAclFileAndFolderCase() throws NoSuchFieldException, IllegalAccessException,
|
||||
IOException {
|
||||
String folder = "both_acl_file_folder_conf";
|
||||
File root = AclTestHelper.copyResources(folder);
|
||||
System.setProperty("rocketmq.home.dir", root.getAbsolutePath());
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
|
||||
checkDefaultAclFileExists(plainAccessValidator);
|
||||
checkDefaultAclFileExists();
|
||||
testValidationAfterConsecutiveUpdates(plainAccessValidator);
|
||||
testValidationAfterConfigFileChanged(plainAccessValidator);
|
||||
AclTestHelper.recursiveDelete(root);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBothAclFileAndFolderCase() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
|
||||
this.isCheckCase3 = true;
|
||||
System.setProperty("rocketmq.home.dir", Paths.get("src/test/resources/both_acl_file_folder_conf").toString());
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
|
||||
checkDefaultAclFileExists(plainAccessValidator);
|
||||
testValidationAfterConsecutiveUpdates(plainAccessValidator);
|
||||
testValidationAfterConfigFileChanged(plainAccessValidator);
|
||||
|
||||
}
|
||||
|
||||
private void testValidationAfterConfigFileChanged(PlainAccessValidator plainAccessValidator) throws NoSuchFieldException, IllegalAccessException, InterruptedException {
|
||||
private void testValidationAfterConfigFileChanged(
|
||||
PlainAccessValidator plainAccessValidator) throws NoSuchFieldException, IllegalAccessException {
|
||||
PlainAccessConfig producerAccessConfig = generateProducerAccessConfig();
|
||||
PlainAccessConfig consumerAccessConfig = generateConsumerAccessConfig();
|
||||
List<PlainAccessConfig> plainAccessConfigList = new LinkedList<>();
|
||||
@@ -229,8 +151,8 @@ public class PlainAccessControlFlowTest {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void testValidationAfterConsecutiveUpdates(PlainAccessValidator plainAccessValidator) throws NoSuchFieldException, IllegalAccessException {
|
||||
private void testValidationAfterConsecutiveUpdates(
|
||||
PlainAccessValidator plainAccessValidator) throws NoSuchFieldException, IllegalAccessException {
|
||||
PlainAccessConfig producerAccessConfig = generateProducerAccessConfig();
|
||||
plainAccessValidator.updateAccessConfig(producerAccessConfig);
|
||||
|
||||
@@ -259,7 +181,7 @@ public class PlainAccessControlFlowTest {
|
||||
|
||||
// load from file
|
||||
loadConfigFile(plainAccessValidator,
|
||||
System.getProperty("rocketmq.home.dir") + File.separator + "conf/plain_acl.yml");
|
||||
System.getProperty("rocketmq.home.dir") + File.separator + "conf/plain_acl.yml");
|
||||
SessionCredentials unmatchedCredential = new SessionCredentials("non_exists_sk", "non_exists_sk");
|
||||
AclClientRPCHook dummyHook = new AclClientRPCHook(unmatchedCredential);
|
||||
validateSendMessage(RequestCode.SEND_MESSAGE, DEFAULT_TOPIC, dummyHook, DEFAULT_GLOBAL_WHITE_ADDR, plainAccessValidator);
|
||||
@@ -273,8 +195,9 @@ public class PlainAccessControlFlowTest {
|
||||
|
||||
}
|
||||
|
||||
private void loadConfigFile(PlainAccessValidator plainAccessValidator, String configFileName) throws NoSuchFieldException, IllegalAccessException {
|
||||
Class clazz = PlainAccessValidator.class;
|
||||
private void loadConfigFile(PlainAccessValidator plainAccessValidator,
|
||||
String configFileName) throws NoSuchFieldException, IllegalAccessException {
|
||||
Class<PlainAccessValidator> clazz = PlainAccessValidator.class;
|
||||
Field f = clazz.getDeclaredField("aclPlugEngine");
|
||||
f.setAccessible(true);
|
||||
PlainPermissionManager aclPlugEngine = (PlainPermissionManager) f.get(plainAccessValidator);
|
||||
@@ -283,41 +206,37 @@ public class PlainAccessControlFlowTest {
|
||||
|
||||
private PlainAccessConfig generateConsumerAccessConfig() {
|
||||
PlainAccessConfig plainAccessConfig2 = new PlainAccessConfig();
|
||||
String accessKey2 = DEFAULT_CONSUMER_AK;
|
||||
String secretKey2 = DEFAULT_CONSUMER_SK;
|
||||
plainAccessConfig2.setAccessKey(accessKey2);
|
||||
plainAccessConfig2.setSecretKey(secretKey2);
|
||||
plainAccessConfig2.setAccessKey(DEFAULT_CONSUMER_AK);
|
||||
plainAccessConfig2.setSecretKey(DEFAULT_CONSUMER_SK);
|
||||
plainAccessConfig2.setAdmin(false);
|
||||
plainAccessConfig2.setDefaultTopicPerm(AclConstants.DENY);
|
||||
plainAccessConfig2.setDefaultGroupPerm(AclConstants.DENY);
|
||||
plainAccessConfig2.setTopicPerms(Arrays.asList(DEFAULT_TOPIC + "=" + AclConstants.SUB));
|
||||
plainAccessConfig2.setGroupPerms(Arrays.asList(DEFAULT_GROUP + "=" + AclConstants.SUB));
|
||||
plainAccessConfig2.setTopicPerms(Collections.singletonList(DEFAULT_TOPIC + "=" + AclConstants.SUB));
|
||||
plainAccessConfig2.setGroupPerms(Collections.singletonList(DEFAULT_GROUP + "=" + AclConstants.SUB));
|
||||
return plainAccessConfig2;
|
||||
}
|
||||
|
||||
private PlainAccessConfig generateProducerAccessConfig() {
|
||||
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
|
||||
String accessKey = DEFAULT_PRODUCER_AK;
|
||||
String secretKey = DEFAULT_PRODUCER_SK;
|
||||
plainAccessConfig.setAccessKey(accessKey);
|
||||
plainAccessConfig.setSecretKey(secretKey);
|
||||
plainAccessConfig.setAccessKey(DEFAULT_PRODUCER_AK);
|
||||
plainAccessConfig.setSecretKey(DEFAULT_PRODUCER_SK);
|
||||
plainAccessConfig.setAdmin(false);
|
||||
plainAccessConfig.setDefaultTopicPerm(AclConstants.DENY);
|
||||
plainAccessConfig.setDefaultGroupPerm(AclConstants.DENY);
|
||||
plainAccessConfig.setTopicPerms(Arrays.asList(DEFAULT_TOPIC + "=" + AclConstants.PUB));
|
||||
plainAccessConfig.setTopicPerms(Collections.singletonList(DEFAULT_TOPIC + "=" + AclConstants.PUB));
|
||||
return plainAccessConfig;
|
||||
}
|
||||
|
||||
public void validatePullMessage(String topic,
|
||||
String group,
|
||||
AclClientRPCHook aclClientRPCHook,
|
||||
String remoteAddr,
|
||||
PlainAccessValidator plainAccessValidator) {
|
||||
String group,
|
||||
AclClientRPCHook aclClientRPCHook,
|
||||
String remoteAddr,
|
||||
PlainAccessValidator plainAccessValidator) {
|
||||
PullMessageRequestHeader pullMessageRequestHeader = new PullMessageRequestHeader();
|
||||
pullMessageRequestHeader.setTopic(topic);
|
||||
pullMessageRequestHeader.setConsumerGroup(group);
|
||||
RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.PULL_MESSAGE,
|
||||
pullMessageRequestHeader);
|
||||
pullMessageRequestHeader);
|
||||
aclClientRPCHook.doBeforeRequest(remoteAddr, remotingCommand);
|
||||
ByteBuffer buf = remotingCommand.encodeHeader();
|
||||
buf.getInt();
|
||||
@@ -325,7 +244,7 @@ public class PlainAccessControlFlowTest {
|
||||
buf.position(0);
|
||||
try {
|
||||
PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(
|
||||
RemotingCommand.decode(buf), remoteAddr);
|
||||
RemotingCommand.decode(buf), remoteAddr);
|
||||
plainAccessValidator.validate(accessResource);
|
||||
} catch (RemotingCommandException e) {
|
||||
e.printStackTrace();
|
||||
@@ -334,10 +253,10 @@ public class PlainAccessControlFlowTest {
|
||||
}
|
||||
|
||||
public void validateSendMessage(int requestCode,
|
||||
String topic,
|
||||
AclClientRPCHook aclClientRPCHook,
|
||||
String remoteAddr,
|
||||
PlainAccessValidator plainAccessValidator) {
|
||||
String topic,
|
||||
AclClientRPCHook aclClientRPCHook,
|
||||
String remoteAddr,
|
||||
PlainAccessValidator plainAccessValidator) {
|
||||
SendMessageRequestHeader messageRequestHeader = new SendMessageRequestHeader();
|
||||
messageRequestHeader.setTopic(topic);
|
||||
RemotingCommand remotingCommand;
|
||||
@@ -345,7 +264,7 @@ public class PlainAccessControlFlowTest {
|
||||
remotingCommand = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, messageRequestHeader);
|
||||
} else {
|
||||
remotingCommand = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE_V2,
|
||||
SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(messageRequestHeader));
|
||||
SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(messageRequestHeader));
|
||||
}
|
||||
|
||||
aclClientRPCHook.doBeforeRequest(remoteAddr, remotingCommand);
|
||||
@@ -356,7 +275,7 @@ public class PlainAccessControlFlowTest {
|
||||
buf.position(0);
|
||||
try {
|
||||
PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(
|
||||
RemotingCommand.decode(buf), remoteAddr);
|
||||
RemotingCommand.decode(buf), remoteAddr);
|
||||
System.out.println(accessResource.getWhiteRemoteAddress());
|
||||
plainAccessValidator.validate(accessResource);
|
||||
} catch (RemotingCommandException e) {
|
||||
@@ -365,8 +284,8 @@ public class PlainAccessControlFlowTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void checkPlainAccessConfig(final PlainAccessConfig plainAccessConfig, final List<PlainAccessConfig> plainAccessConfigs) {
|
||||
private void checkPlainAccessConfig(final PlainAccessConfig plainAccessConfig,
|
||||
final List<PlainAccessConfig> plainAccessConfigs) {
|
||||
for (PlainAccessConfig config : plainAccessConfigs) {
|
||||
if (config.getAccessKey().equals(plainAccessConfig.getAccessKey())) {
|
||||
Assert.assertEquals(plainAccessConfig.getSecretKey(), config.getSecretKey());
|
||||
@@ -386,11 +305,10 @@ public class PlainAccessControlFlowTest {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDefaultAclFileExists(PlainAccessValidator plainAccessValidator) {
|
||||
private void checkDefaultAclFileExists() {
|
||||
boolean isExists = Files.exists(Paths.get(System.getProperty("rocketmq.home.dir")
|
||||
+ File.separator + "conf/plain_acl.yml"));
|
||||
+ File.separator + "conf" + File.separator + "plain_acl.yml"));
|
||||
Assert.assertTrue("default acl config file should exist", isExists);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user