From 29438a11c1569040ee9cb8f9c13ae3b0030e3405 Mon Sep 17 00:00:00 2001 From: Hu Zongtang Date: Wed, 10 Apr 2019 10:03:34 +0800 Subject: [PATCH] [ISSUE #1078]fix User can't use mqadmin command normally if they don't copy the tool.yml file to related fold and AclEnable flag is closed. (#1079) * [issue#1078]fix User can't use mqadmin command normally if they don't copy the tool.yml file to their related fold and AclEnable flag is closed. --- .../apache/rocketmq/acl/common/AclUtils.java | 8 +++--- .../rocketmq/acl/common/AclUtilsTest.java | 16 ++++++++++-- .../resources/conf/plain_acl_format_error.yml | 26 +++++++++++++++++++ .../tools/command/MQAdminStartup.java | 17 +++++++++--- 4 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 acl/src/test/resources/conf/plain_acl_format_error.yml diff --git a/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java b/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java index 1a618456..ce63cbf2 100644 --- a/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java +++ b/acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java @@ -18,6 +18,7 @@ package org.apache.rocketmq.acl.common; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.Map; import java.util.SortedMap; @@ -124,14 +125,15 @@ public class AclUtils { try { fis = new FileInputStream(new File(path)); return ymal.loadAs(fis, clazz); + } catch (FileNotFoundException ignore) { + return null; } catch (Exception e) { - throw new AclException(String.format("The file for Plain mode was not found , paths %s", path), e); + throw new AclException(e.getMessage()); } finally { if (fis != null) { try { fis.close(); - } catch (IOException e) { - throw new AclException("close transport fileInputStream Exception", e); + } catch (IOException ignore) { } } } diff --git a/acl/src/test/java/org/apache/rocketmq/acl/common/AclUtilsTest.java b/acl/src/test/java/org/apache/rocketmq/acl/common/AclUtilsTest.java index 72bcda6b..12f43725 100644 --- a/acl/src/test/java/org/apache/rocketmq/acl/common/AclUtilsTest.java +++ b/acl/src/test/java/org/apache/rocketmq/acl/common/AclUtilsTest.java @@ -16,6 +16,7 @@ */ package org.apache.rocketmq.acl.common; +import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -133,9 +134,20 @@ public class AclUtilsTest { Assert.assertFalse(map.isEmpty()); } + @Test + public void getYamlDataIgnoreFileNotFoundExceptionTest() { + + JSONObject yamlDataObject = AclUtils.getYamlDataObject("plain_acl.yml", JSONObject.class); + Assert.assertTrue(yamlDataObject == null); + } + @Test(expected = Exception.class) - public void getYamlDataObjectExceptionTest() { + public void getYamlDataExceptionTest() { - AclUtils.getYamlDataObject("plain_acl.yml", Map.class); + AclUtils.getYamlDataObject("src/test/resources/conf/plain_acl_format_error.yml", Map.class); } + + + + } diff --git a/acl/src/test/resources/conf/plain_acl_format_error.yml b/acl/src/test/resources/conf/plain_acl_format_error.yml new file mode 100644 index 00000000..46782c56 --- /dev/null +++ b/acl/src/test/resources/conf/plain_acl_format_error.yml @@ -0,0 +1,26 @@ +# 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 + +date 2015-02-01 +accounts: + - name: Jai +accounts: +- accessKey: RocketMQ + secretKey: 12345678 + whiteRemoteAddress: 192.168.0.* + admin: false + diff --git a/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java b/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java index 2ca60aa8..da71513a 100644 --- a/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java +++ b/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java @@ -249,11 +249,22 @@ public class MQAdminStartup { public static RPCHook getAclRPCHook() { String fileHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV)); String fileName = "/conf/tools.yml"; - JSONObject yamlDataObject = AclUtils.getYamlDataObject(fileHome + fileName , + JSONObject yamlDataObject = null; + try { + yamlDataObject = AclUtils.getYamlDataObject(fileHome + fileName, JSONObject.class); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + if (yamlDataObject == null) { + System.out.printf("Cannot find conf file %s, acl isn't be enabled.%n" ,fileHome + fileName); + return null; + } - if (yamlDataObject == null || yamlDataObject.isEmpty()) { - System.out.printf(" Cannot find conf file %s, acl is not be enabled.%n" ,fileHome + fileName); + if (yamlDataObject.isEmpty()) { + System.out.printf("Content of conf file %s is empty, acl isn't be enabled.%n" ,fileHome + fileName); return null; } -- GitLab