Skip to content

Commit

Permalink
[ISSUE apache#1078]fix User can't use mqadmin command normally if the…
Browse files Browse the repository at this point in the history
…y don't copy the tool.yml file to related fold and AclEnable flag is closed. (apache#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.
  • Loading branch information
zongtanghu authored Apr 10, 2019
1 parent b80c262 commit 29438a1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.SortedMap;
Expand Down Expand Up @@ -124,14 +125,15 @@ public static <T> T getYamlDataObject(String path, Class<T> clazz) {
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) {
}
}
}
Expand Down
16 changes: 14 additions & 2 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,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;
Expand Down Expand Up @@ -133,9 +134,20 @@ public void getYamlDataObjectTest() {
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);
}




}
26 changes: 26 additions & 0 deletions acl/src/test/resources/conf/plain_acl_format_error.yml
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,22 @@ public static void initCommand(SubCommand command) {
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;
}

Expand Down

0 comments on commit 29438a1

Please sign in to comment.