Add option p to the updateGlobalWhiteAddr command to indicate the full path of the ACL configuration file to be modified.If the option is null, updateGlobalWhiteAddr command will update the default acl configuration file.

This commit is contained in:
sunxi
2022-05-24 22:42:58 +08:00
parent de5e6d9e06
commit 53baee7803
15 changed files with 147 additions and 24 deletions
@@ -71,7 +71,7 @@ public interface AccessValidator {
*
* @return
*/
boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList);
boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList, String aclFileFullPath);
/**
* get broker cluster acl config information
@@ -155,8 +155,8 @@ public class PlainAccessValidator implements AccessValidator {
return aclPlugEngine.getAclConfigDataVersion();
}
@Override public boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList) {
return aclPlugEngine.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList);
@Override public boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList, String aclFileFullPath) {
return aclPlugEngine.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList, aclFileFullPath);
}
@Override public AclConfig getAllAclConfig() {
@@ -466,11 +466,11 @@ public class PlainPermissionManager {
return false;
}
public boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList) {
return this.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList, this.defaultAclFile);
}
public boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList, String fileName) {
if (fileName == null) {
fileName = this.defaultAclFile;
}
if (globalWhiteAddrsList == null) {
log.error("Parameter value globalWhiteAddrsList is null,Please check your parameter");
return false;
@@ -482,6 +482,16 @@ public class PlainPermissionManager {
return false;
}
if (!fileName.startsWith(fileHome)) {
log.error("Parameter value " + fileName + " is not in the directory rocketmq.home.dir");
return false;
}
if (!fileName.endsWith(".yml") && fileName.endsWith(".yaml")) {
log.error("Parameter value " + fileName + " is not a ACL configuration file");
return false;
}
Map<String, Object> aclAccessConfigMap = AclUtils.getYamlDataObject(fileName, Map.class);
if (aclAccessConfigMap == null) {
aclAccessConfigMap = new HashMap<>();
@@ -148,7 +148,7 @@ public class PlainAccessControlFlowTest {
}
@Test
public void testEmptyAclFolderCase() throws NoSuchFieldException, IllegalAccessException {
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());
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
@@ -160,7 +160,7 @@ public class PlainAccessControlFlowTest {
}
@Test
public void testOnlyAclFolderCase() throws NoSuchFieldException, IllegalAccessException {
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());
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
@@ -172,7 +172,7 @@ public class PlainAccessControlFlowTest {
@Test
public void testBothAclFileAndFolderCase() throws NoSuchFieldException, IllegalAccessException {
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();
@@ -183,7 +183,7 @@ public class PlainAccessControlFlowTest {
}
private void testValidationAfterConfigFileChanged(PlainAccessValidator plainAccessValidator) throws NoSuchFieldException, IllegalAccessException {
private void testValidationAfterConfigFileChanged(PlainAccessValidator plainAccessValidator) throws NoSuchFieldException, IllegalAccessException, InterruptedException {
PlainAccessConfig producerAccessConfig = generateProducerAccessConfig();
PlainAccessConfig consumerAccessConfig = generateConsumerAccessConfig();
List<PlainAccessConfig> plainAccessConfigList = new LinkedList<>();
@@ -237,7 +237,7 @@ public class PlainAccessControlFlowTest {
PlainAccessConfig consumerAccessConfig = generateConsumerAccessConfig();
plainAccessValidator.updateAccessConfig(consumerAccessConfig);
plainAccessValidator.updateGlobalWhiteAddrsConfig(DEFAULT_GLOBAL_WHITE_ADDRS_LIST);
plainAccessValidator.updateGlobalWhiteAddrsConfig(DEFAULT_GLOBAL_WHITE_ADDRS_LIST, null);
// check if the above config updated successfully
final AclConfig allAclConfig = plainAccessValidator.getAllAclConfig();
@@ -641,7 +641,7 @@ public class PlainAccessValidatorTest {
globalWhiteAddrsList.add("192.168.1.*");
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
Assert.assertEquals(plainAccessValidator.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList), true);
Assert.assertEquals(plainAccessValidator.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList, null), true);
String aclFileName = System.getProperty("rocketmq.home.dir")
+ File.separator + "conf/plain_acl.yml".replace("/", File.separator);
@@ -978,4 +978,53 @@ public class PlainAccessValidatorTest {
System.setProperty("rocketmq.acl.plain.file", "conf/plain_acl.yml".replace("/", File.separator));
}
}
@Test
public void testUpdateSpecifiedAclFileGlobalWhiteAddrsConfig() {
System.setProperty("rocketmq.home.dir", "src/test/resources/update_global_white_addr");
System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl.yml");
String targetFileName = "src/test/resources/update_global_white_addr/conf/plain_acl.yml";
Map<String, Object> backUpAclConfigMap = AclUtils.getYamlDataObject(targetFileName, Map.class);
String targetFileName1 = "src/test/resources/update_global_white_addr/conf/acl/plain_acl.yml";
Map<String, Object> backUpAclConfigMap1 = AclUtils.getYamlDataObject(targetFileName1, Map.class);
String targetFileName2 = "src/test/resources/update_global_white_addr/conf/acl/empty.yml";
Map<String, Object> backUpAclConfigMap2 = AclUtils.getYamlDataObject(targetFileName2, Map.class);
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
List<String> globalWhiteAddrsList1 = new ArrayList<String>();
globalWhiteAddrsList1.add("10.10.154.1");
List<String> globalWhiteAddrsList2 = new ArrayList<String>();
globalWhiteAddrsList2.add("10.10.154.2");
List<String> globalWhiteAddrsList3 = new ArrayList<String>();
globalWhiteAddrsList3.add("10.10.154.3");
//Test parameter p is null
plainAccessValidator.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList1, null);
String defaultAclFile = targetFileName;
Map<String, Object> defaultAclFileMap = AclUtils.getYamlDataObject(defaultAclFile, Map.class);
List<String> defaultAclFileGlobalWhiteAddrList = (List<String>)defaultAclFileMap.get(AclConstants.CONFIG_GLOBAL_WHITE_ADDRS);
Assert.assertTrue(defaultAclFileGlobalWhiteAddrList.contains("10.10.154.1"));
//Test parameter p is not null
plainAccessValidator.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList2, targetFileName1);
Map<String, Object> aclFileMap1 = AclUtils.getYamlDataObject(targetFileName1, Map.class);
List<String> aclFileGlobalWhiteAddrList1 = (List<String>)aclFileMap1.get(AclConstants.CONFIG_GLOBAL_WHITE_ADDRS);
Assert.assertTrue(aclFileGlobalWhiteAddrList1.contains("10.10.154.2"));
//Test parameter p is not null, but the file does not have globalWhiteRemoteAddresses
plainAccessValidator.updateGlobalWhiteAddrsConfig(globalWhiteAddrsList3, targetFileName2);
Map<String, Object> aclFileMap2 = AclUtils.getYamlDataObject(targetFileName2, Map.class);
List<String> aclFileGlobalWhiteAddrList2 = (List<String>)aclFileMap2.get(AclConstants.CONFIG_GLOBAL_WHITE_ADDRS);
Assert.assertTrue(aclFileGlobalWhiteAddrList2.contains("10.10.154.3"));
AclUtils.writeDataObject(targetFileName, backUpAclConfigMap);
AclUtils.writeDataObject(targetFileName1, backUpAclConfigMap1);
AclUtils.writeDataObject(targetFileName2, backUpAclConfigMap2);
System.setProperty("rocketmq.home.dir", "src/test/resources");
System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl.yml");
}
}
@@ -0,0 +1 @@
accounts: []
@@ -0,0 +1,22 @@
globalWhiteRemoteAddresses:
- 10.10.103.*
- 192.168.0.*
accounts:
- accessKey: RocketMQ
secretKey: 12345678
whiteRemoteAddress: 192.168.0.*
admin: false
defaultTopicPerm: DENY
defaultGroupPerm: SUB
topicPerms:
- topicA=DENY
- topicB=PUB|SUB
- topicC=SUB
groupPerms:
- groupA=DENY
- groupB=SUB
- groupC=SUB
- accessKey: rocketmq2
secretKey: 12345678
whiteRemoteAddress: 192.168.1.*
admin: true
@@ -0,0 +1,19 @@
accounts:
- accessKey: RocketMQ
secretKey: 12345678
whiteRemoteAddress: 192.168.0.*
admin: false
defaultTopicPerm: DENY
defaultGroupPerm: SUB
topicPerms:
- topicA=DENY
- topicB=PUB|SUB
- topicC=SUB
groupPerms:
- groupA=DENY
- groupB=SUB
- groupC=SUB
- accessKey: rocketmq2
secretKey: 12345678
whiteRemoteAddress: 192.168.1.*
admin: true
@@ -404,7 +404,8 @@ public class AdminBrokerProcessor extends AsyncNettyRequestProcessor implements
try {
AccessValidator accessValidator = this.brokerController.getAccessValidatorMap().get(PlainAccessValidator.class);
if (accessValidator.updateGlobalWhiteAddrsConfig(UtilAll.split(requestHeader.getGlobalWhiteAddrs(), ","))) {
if (accessValidator.updateGlobalWhiteAddrsConfig(UtilAll.split(requestHeader.getGlobalWhiteAddrs(), ","),
requestHeader.getAclFileFullPath())) {
response.setCode(ResponseCode.SUCCESS);
response.setOpaque(request.getOpaque());
response.markResponseType();
@@ -348,11 +348,11 @@ public class MQClientAPIImpl {
throw new MQClientException(response.getCode(), response.getRemark());
}
public void updateGlobalWhiteAddrsConfig(final String addr, final String globalWhiteAddrs, final long timeoutMillis)
throws RemotingException, InterruptedException, MQClientException {
public void updateGlobalWhiteAddrsConfig(final String addr, final String globalWhiteAddrs, String aclFileFullPath, final long timeoutMillis)
throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
UpdateGlobalWhiteAddrsConfigRequestHeader requestHeader = new UpdateGlobalWhiteAddrsConfigRequestHeader();
requestHeader.setGlobalWhiteAddrs(globalWhiteAddrs);
requestHeader.setAclFileFullPath(aclFileFullPath);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_GLOBAL_WHITE_ADDRS_CONFIG, requestHeader);
@@ -24,6 +24,8 @@ public class UpdateGlobalWhiteAddrsConfigRequestHeader implements CommandCustomH
@CFNotNull
private String globalWhiteAddrs;
@CFNotNull
private String aclFileFullPath;
@Override public void checkFields() throws RemotingCommandException {
@@ -36,4 +38,12 @@ public class UpdateGlobalWhiteAddrsConfigRequestHeader implements CommandCustomH
public void setGlobalWhiteAddrs(String globalWhiteAddrs) {
this.globalWhiteAddrs = globalWhiteAddrs;
}
public String getAclFileFullPath() {
return aclFileFullPath;
}
public void setAclFileFullPath(String aclFileFullPath) {
this.aclFileFullPath = aclFileFullPath;
}
}
@@ -181,8 +181,8 @@ public class DefaultMQAdminExt extends ClientConfig implements MQAdminExt {
}
@Override public void updateGlobalWhiteAddrConfig(String addr,
String globalWhiteAddrs) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
defaultMQAdminExtImpl.updateGlobalWhiteAddrConfig(addr, globalWhiteAddrs);
String globalWhiteAddrs, String aclFileFullPath) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
defaultMQAdminExtImpl.updateGlobalWhiteAddrConfig(addr, globalWhiteAddrs, aclFileFullPath);
}
@Override public ClusterAclVersionInfo examineBrokerClusterAclVersionInfo(
@@ -211,8 +211,8 @@ public class DefaultMQAdminExtImpl implements MQAdminExt, MQAdminExtInner {
}
@Override public void updateGlobalWhiteAddrConfig(String addr,
String globalWhiteAddrs) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
this.mqClientInstance.getMQClientAPIImpl().updateGlobalWhiteAddrsConfig(addr, globalWhiteAddrs, timeoutMillis);
String globalWhiteAddrs, String aclFileFullPath) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
this.mqClientInstance.getMQClientAPIImpl().updateGlobalWhiteAddrsConfig(addr, globalWhiteAddrs, aclFileFullPath, timeoutMillis);
}
@Override
@@ -77,7 +77,7 @@ public interface MQAdminExt extends MQAdmin {
void deletePlainAccessConfig(final String addr, final String accessKey) throws RemotingException, MQBrokerException,
InterruptedException, MQClientException;
void updateGlobalWhiteAddrConfig(final String addr, final String globalWhiteAddrs)throws RemotingException, MQBrokerException,
void updateGlobalWhiteAddrConfig(final String addr, final String globalWhiteAddrs, String aclFileFullPath)throws RemotingException, MQBrokerException,
InterruptedException, MQClientException;
ClusterAclVersionInfo examineBrokerClusterAclVersionInfo(final String addr) throws RemotingException, MQBrokerException,
@@ -55,6 +55,10 @@ public class UpdateGlobalWhiteAddrSubCommand implements SubCommand {
opt.setRequired(true);
options.addOption(opt);
opt = new Option("p", "aclFileFullPath", true, "update global white address of specified acl file,eg: /xxx/plain_test.yml");
opt.setRequired(false);
options.addOption(opt);
return options;
}
@@ -68,12 +72,19 @@ public class UpdateGlobalWhiteAddrSubCommand implements SubCommand {
// GlobalWhiteRemoteAddresses list value
String globalWhiteRemoteAddresses = commandLine.getOptionValue('g').trim();
String aclFileFullPath;
if (commandLine.hasOption('p')) {
aclFileFullPath = commandLine.getOptionValue('p').trim();
} else {
aclFileFullPath = null;
}
if (commandLine.hasOption('b')) {
String addr = commandLine.getOptionValue('b').trim();
defaultMQAdminExt.start();
defaultMQAdminExt.updateGlobalWhiteAddrConfig(addr, globalWhiteRemoteAddresses);
defaultMQAdminExt.updateGlobalWhiteAddrConfig(addr, globalWhiteRemoteAddresses, aclFileFullPath);
System.out.printf("update global white remote addresses to %s success.%n", addr);
return;
@@ -85,7 +96,7 @@ public class UpdateGlobalWhiteAddrSubCommand implements SubCommand {
Set<String> brokerAddrSet =
CommandUtil.fetchMasterAndSlaveAddrByClusterName(defaultMQAdminExt, clusterName);
for (String addr : brokerAddrSet) {
defaultMQAdminExt.updateGlobalWhiteAddrConfig(addr, globalWhiteRemoteAddresses);
defaultMQAdminExt.updateGlobalWhiteAddrConfig(addr, globalWhiteRemoteAddresses, aclFileFullPath);
System.out.printf("update global white remote addresses to %s success.%n", addr);
}
return;