@Test public void testExecute() { UpdateAccessConfigSubCommand cmd = new UpdateAccessConfigSubCommand(); Options options = ServerUtil.buildCommandlineOptions(new Options()); String[] subargs = new String[] { "-b 127.0.0.1:10911", "-a RocketMQ", "-s 12345678", "-w 192.168.0.*", "-i DENY", "-u SUB", "-t topicA=DENY;topicB=PUB|SUB", "-g groupA=DENY;groupB=SUB", "-m true"}; final CommandLine commandLine = ServerUtil.parseCmdLine("mqadmin " + cmd.commandName(), subargs, cmd.buildCommandlineOptions(options), new PosixParser()); assertThat(commandLine.getOptionValue('b').trim()).isEqualTo("127.0.0.1:10911"); assertThat(commandLine.getOptionValue('a').trim()).isEqualTo("RocketMQ"); assertThat(commandLine.getOptionValue('s').trim()).isEqualTo("12345678"); assertThat(commandLine.getOptionValue('w').trim()).isEqualTo("192.168.0.*"); assertThat(commandLine.getOptionValue('i').trim()).isEqualTo("DENY"); assertThat(commandLine.getOptionValue('u').trim()).isEqualTo("SUB"); assertThat(commandLine.getOptionValue('t').trim()).isEqualTo("topicA=DENY;topicB=PUB|SUB"); assertThat(commandLine.getOptionValue('g').trim()).isEqualTo("groupA=DENY;groupB=SUB"); assertThat(commandLine.getOptionValue('m').trim()).isEqualTo("true"); PlainAccessConfig accessConfig = new PlainAccessConfig(); if (commandLine.hasOption('t')) { String[] topicPerms = commandLine.getOptionValue('t').trim().split(";"); List<String> topicPermList = new ArrayList<String>(); if (topicPerms != null) { for (String topicPerm : topicPerms) { topicPermList.add(topicPerm); } } accessConfig.setTopicPerms(topicPermList); } if (commandLine.hasOption('g')) { String[] groupPerms = commandLine.getOptionValue('g').trim().split(";"); List<String> groupPermList = new ArrayList<String>(); if (groupPerms != null) { for (String groupPerm : groupPerms) { groupPermList.add(groupPerm); } } accessConfig.setGroupPerms(groupPermList); } Assert.assertTrue(accessConfig.getTopicPerms().contains("topicB=PUB|SUB")); Assert.assertTrue(accessConfig.getGroupPerms().contains("groupB=SUB")); }
@Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook); defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis())); try { PlainAccessConfig accessConfig = new PlainAccessConfig(); accessConfig.setAccessKey(commandLine.getOptionValue('a').trim()); if (commandLine.hasOption('s')) { accessConfig.setSecretKey(commandLine.getOptionValue('s').trim()); } if (commandLine.hasOption('m')) { accessConfig.setAdmin(Boolean.parseBoolean(commandLine.getOptionValue('m').trim())); } if (commandLine.hasOption('i')) { accessConfig.setDefaultTopicPerm(commandLine.getOptionValue('i').trim()); } if (commandLine.hasOption('u')) { accessConfig.setDefaultGroupPerm(commandLine.getOptionValue('u').trim()); } if (commandLine.hasOption('w')) { accessConfig.setWhiteRemoteAddress(commandLine.getOptionValue('w').trim()); } if (commandLine.hasOption('t')) { String[] topicPerms = commandLine.getOptionValue('t').trim().split(","); List<String> topicPermList = new ArrayList<String>(); if (topicPerms != null) { for (String topicPerm : topicPerms) { topicPermList.add(topicPerm); } } accessConfig.setTopicPerms(topicPermList); } if (commandLine.hasOption('g')) { String[] groupPerms = commandLine.getOptionValue('g').trim().split(","); List<String> groupPermList = new ArrayList<String>(); if (groupPerms != null) { for (String groupPerm : groupPerms) { groupPermList.add(groupPerm); } } accessConfig.setGroupPerms(groupPermList); } if (commandLine.hasOption('b')) { String addr = commandLine.getOptionValue('b').trim(); defaultMQAdminExt.start(); defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); System.out.printf("%s", accessConfig); return; } else if (commandLine.hasOption('c')) { String clusterName = commandLine.getOptionValue('c').trim(); defaultMQAdminExt.start(); Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName); for (String addr : masterSet) { defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); } System.out.printf("%s", accessConfig); return; } ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options); } catch (Exception e) { throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e); } finally { defaultMQAdminExt.shutdown(); } }
UpdateAccessConfigSubCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook); defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis())); try { PlainAccessConfig accessConfig = new PlainAccessConfig(); accessConfig.setAccessKey(commandLine.getOptionValue('a').trim()); if (commandLine.hasOption('s')) { accessConfig.setSecretKey(commandLine.getOptionValue('s').trim()); } if (commandLine.hasOption('m')) { accessConfig.setAdmin(Boolean.parseBoolean(commandLine.getOptionValue('m').trim())); } if (commandLine.hasOption('i')) { accessConfig.setDefaultTopicPerm(commandLine.getOptionValue('i').trim()); } if (commandLine.hasOption('u')) { accessConfig.setDefaultGroupPerm(commandLine.getOptionValue('u').trim()); } if (commandLine.hasOption('w')) { accessConfig.setWhiteRemoteAddress(commandLine.getOptionValue('w').trim()); } if (commandLine.hasOption('t')) { String[] topicPerms = commandLine.getOptionValue('t').trim().split(","); List<String> topicPermList = new ArrayList<String>(); if (topicPerms != null) { for (String topicPerm : topicPerms) { topicPermList.add(topicPerm); } } accessConfig.setTopicPerms(topicPermList); } if (commandLine.hasOption('g')) { String[] groupPerms = commandLine.getOptionValue('g').trim().split(","); List<String> groupPermList = new ArrayList<String>(); if (groupPerms != null) { for (String groupPerm : groupPerms) { groupPermList.add(groupPerm); } } accessConfig.setGroupPerms(groupPermList); } if (commandLine.hasOption('b')) { String addr = commandLine.getOptionValue('b').trim(); defaultMQAdminExt.start(); defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); System.out.printf("%s", accessConfig); return; } else if (commandLine.hasOption('c')) { String clusterName = commandLine.getOptionValue('c').trim(); defaultMQAdminExt.start(); Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName); for (String addr : masterSet) { defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); } System.out.printf("%s", accessConfig); return; } ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options); } catch (Exception e) { throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e); } finally { defaultMQAdminExt.shutdown(); } } }
UpdateAccessConfigSubCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook); defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis())); try { PlainAccessConfig accessConfig = new PlainAccessConfig(); accessConfig.setAccessKey(commandLine.getOptionValue('a').trim()); if (commandLine.hasOption('s')) { accessConfig.setSecretKey(commandLine.getOptionValue('s').trim()); } if (commandLine.hasOption('m')) { accessConfig.setAdmin(Boolean.parseBoolean(commandLine.getOptionValue('m').trim())); } if (commandLine.hasOption('i')) { accessConfig.setDefaultTopicPerm(commandLine.getOptionValue('i').trim()); } if (commandLine.hasOption('u')) { accessConfig.setDefaultGroupPerm(commandLine.getOptionValue('u').trim()); } if (commandLine.hasOption('w')) { accessConfig.setWhiteRemoteAddress(commandLine.getOptionValue('w').trim()); } if (commandLine.hasOption('t')) { String[] topicPerms = commandLine.getOptionValue('t').trim().split(","); List<String> topicPermList = new ArrayList<String>(); if (topicPerms != null) { for (String topicPerm : topicPerms) { topicPermList.add(topicPerm); } } accessConfig.setTopicPerms(topicPermList); } if (commandLine.hasOption('g')) { String[] groupPerms = commandLine.getOptionValue('g').trim().split(","); List<String> groupPermList = new ArrayList<String>(); if (groupPerms != null) { for (String groupPerm : groupPerms) { groupPermList.add(groupPerm); } } accessConfig.setGroupPerms(groupPermList); } if (commandLine.hasOption('b')) { String addr = commandLine.getOptionValue('b').trim(); defaultMQAdminExt.start(); defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); System.out.printf("%s", accessConfig); return; } else if (commandLine.hasOption('c')) { String clusterName = commandLine.getOptionValue('c').trim(); defaultMQAdminExt.start(); Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName); for (String addr : masterSet) { defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); } System.out.printf("%s", accessConfig); return; } ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options); } catch (Exception e) { throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e); } finally { defaultMQAdminExt.shutdown(); } }  }
UpdateAccessConfigSubCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook); defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis())); try { PlainAccessConfig accessConfig = new PlainAccessConfig(); accessConfig.setAccessKey(commandLine.getOptionValue('a').trim()); if (commandLine.hasOption('s')) { accessConfig.setSecretKey(commandLine.getOptionValue('s').trim()); } if (commandLine.hasOption('m')) { accessConfig.setAdmin(Boolean.parseBoolean(commandLine.getOptionValue('m').trim())); } if (commandLine.hasOption('i')) { accessConfig.setDefaultTopicPerm(commandLine.getOptionValue('i').trim()); } if (commandLine.hasOption('u')) { accessConfig.setDefaultGroupPerm(commandLine.getOptionValue('u').trim()); } if (commandLine.hasOption('w')) { accessConfig.setWhiteRemoteAddress(commandLine.getOptionValue('w').trim()); } if (commandLine.hasOption('t')) { String[] topicPerms = commandLine.getOptionValue('t').trim().split(","); List<String> topicPermList = new ArrayList<String>(); if (topicPerms != null) { for (String topicPerm : topicPerms) { topicPermList.add(topicPerm); } } accessConfig.setTopicPerms(topicPermList); } if (commandLine.hasOption('g')) { String[] groupPerms = commandLine.getOptionValue('g').trim().split(","); List<String> groupPermList = new ArrayList<String>(); if (groupPerms != null) { for (String groupPerm : groupPerms) { groupPermList.add(groupPerm); } } accessConfig.setGroupPerms(groupPermList); } if (commandLine.hasOption('b')) { String addr = commandLine.getOptionValue('b').trim(); defaultMQAdminExt.start(); defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); System.out.printf("%s", accessConfig); return; } else if (commandLine.hasOption('c')) { String clusterName = commandLine.getOptionValue('c').trim(); defaultMQAdminExt.start(); Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName); for (String addr : masterSet) { defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); } System.out.printf("%s", accessConfig); return; } ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options); } catch (Exception e) { throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e); } finally { defaultMQAdminExt.shutdown(); } }  @Override String commandName(); @Override String commandDesc(); @Override Options buildCommandlineOptions(Options options); @Override void execute(CommandLine commandLine, Options options,
        RPCHook rpcHook); }
UpdateAccessConfigSubCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook); defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis())); try { PlainAccessConfig accessConfig = new PlainAccessConfig(); accessConfig.setAccessKey(commandLine.getOptionValue('a').trim()); if (commandLine.hasOption('s')) { accessConfig.setSecretKey(commandLine.getOptionValue('s').trim()); } if (commandLine.hasOption('m')) { accessConfig.setAdmin(Boolean.parseBoolean(commandLine.getOptionValue('m').trim())); } if (commandLine.hasOption('i')) { accessConfig.setDefaultTopicPerm(commandLine.getOptionValue('i').trim()); } if (commandLine.hasOption('u')) { accessConfig.setDefaultGroupPerm(commandLine.getOptionValue('u').trim()); } if (commandLine.hasOption('w')) { accessConfig.setWhiteRemoteAddress(commandLine.getOptionValue('w').trim()); } if (commandLine.hasOption('t')) { String[] topicPerms = commandLine.getOptionValue('t').trim().split(","); List<String> topicPermList = new ArrayList<String>(); if (topicPerms != null) { for (String topicPerm : topicPerms) { topicPermList.add(topicPerm); } } accessConfig.setTopicPerms(topicPermList); } if (commandLine.hasOption('g')) { String[] groupPerms = commandLine.getOptionValue('g').trim().split(","); List<String> groupPermList = new ArrayList<String>(); if (groupPerms != null) { for (String groupPerm : groupPerms) { groupPermList.add(groupPerm); } } accessConfig.setGroupPerms(groupPermList); } if (commandLine.hasOption('b')) { String addr = commandLine.getOptionValue('b').trim(); defaultMQAdminExt.start(); defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); System.out.printf("%s", accessConfig); return; } else if (commandLine.hasOption('c')) { String clusterName = commandLine.getOptionValue('c').trim(); defaultMQAdminExt.start(); Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName); for (String addr : masterSet) { defaultMQAdminExt.createAndUpdatePlainAccessConfig(addr, accessConfig); System.out.printf("create or update plain access config to %s success.%n", addr); } System.out.printf("%s", accessConfig); return; } ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options); } catch (Exception e) { throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e); } finally { defaultMQAdminExt.shutdown(); } }  @Override String commandName(); @Override String commandDesc(); @Override Options buildCommandlineOptions(Options options); @Override void execute(CommandLine commandLine, Options options,
        RPCHook rpcHook);  }