StyleTang commented on a change in pull request #71: URL: https://github.com/apache/rocketmq-dashboard/pull/71#discussion_r810600432
########## File path: src/main/java/org/apache/rocketmq/dashboard/model/request/AclRequest.java ########## @@ -0,0 +1,59 @@ +/* + * 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.dashboard.model.request; + +import org.apache.rocketmq.common.PlainAccessConfig; + Review comment: Instead of adding getters/setters/toString manually, Lombok `@Data` can be used. ~public PlainAccessConfig getConfig() {~ ~return config;~ ~}~ ```java @Data public class AclRequest { ``` ########## File path: src/main/java/org/apache/rocketmq/dashboard/controller/AclController.java ########## @@ -0,0 +1,146 @@ +/* + * 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.dashboard.controller; + +import com.google.common.base.Preconditions; +import java.util.List; +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.common.AclConfig; +import org.apache.rocketmq.common.PlainAccessConfig; +import org.apache.rocketmq.dashboard.config.RMQConfigure; +import org.apache.rocketmq.dashboard.model.User; +import org.apache.rocketmq.dashboard.model.UserInfo; +import org.apache.rocketmq.dashboard.model.request.AclRequest; +import org.apache.rocketmq.dashboard.permisssion.Permission; +import org.apache.rocketmq.dashboard.service.AclService; +import org.apache.rocketmq.dashboard.support.JsonResult; +import org.apache.rocketmq.dashboard.util.WebUtil; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/acl") +@Permission +public class AclController { + + @Resource + private AclService aclService; + + @Resource + private RMQConfigure configure; + + @GetMapping("/enable.query") + public Object isEnableAcl() { + return new JsonResult<>(configure.isACLEnabled()); + } + + @GetMapping("/config.query") + public AclConfig getAclConfig(HttpServletRequest request) { + if (!configure.isLoginRequired()) { + return aclService.getAclConfig(false); + } + UserInfo userInfo = (UserInfo) WebUtil.getValueFromSession(request, WebUtil.USER_INFO); + // if user info is null but reach here, must exclude secret key for safety. + return aclService.getAclConfig(userInfo == null || userInfo.getUser().getType() != User.ADMIN); + } + + @PostMapping("/add.do") + public Object addAclConfig(@RequestBody PlainAccessConfig config) { + Preconditions.checkArgument(StringUtils.isNotEmpty(config.getAccessKey()), "accessKey is null"); + Preconditions.checkArgument(StringUtils.isNotEmpty(config.getSecretKey()), "secretKey is null"); + aclService.addAclConfig(config); + return new JsonResult(0, ""); Review comment: We can just return true. ```java return true; ``` ########## File path: src/main/java/org/apache/rocketmq/dashboard/service/impl/AclServiceImpl.java ########## @@ -0,0 +1,360 @@ +/* + * 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.dashboard.service.impl; + +import com.google.common.base.Throwables; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.common.AclConfig; +import org.apache.rocketmq.common.MixAll; +import org.apache.rocketmq.common.PlainAccessConfig; +import org.apache.rocketmq.common.protocol.body.ClusterInfo; +import org.apache.rocketmq.common.protocol.route.BrokerData; +import org.apache.rocketmq.dashboard.model.request.AclRequest; +import org.apache.rocketmq.dashboard.service.AbstractCommonService; +import org.apache.rocketmq.dashboard.service.AclService; +import org.apache.rocketmq.remoting.exception.RemotingConnectException; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.apache.rocketmq.remoting.exception.RemotingSendRequestException; +import org.apache.rocketmq.remoting.exception.RemotingTimeoutException; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class AclServiceImpl extends AbstractCommonService implements AclService { + + @Override + public AclConfig getAclConfig(boolean excludeSecretKey) { + try { + Optional<String> addr = getMasterSet().stream().findFirst(); + if (addr.isPresent()) { + if (!excludeSecretKey) { + return mqAdminExt.examineBrokerClusterAclConfig(addr.get()); + } else { + AclConfig aclConfig = mqAdminExt.examineBrokerClusterAclConfig(addr.get()); + if (CollectionUtils.isNotEmpty(aclConfig.getPlainAccessConfigs())) { + aclConfig.getPlainAccessConfigs().forEach(pac -> pac.setSecretKey(null)); + } + return aclConfig; + } + } + } catch (Exception e) { + log.error("getAclConfig error.", e); + throw Throwables.propagate(e); + } + AclConfig aclConfig = new AclConfig(); + aclConfig.setGlobalWhiteAddrs(Collections.emptyList()); + aclConfig.setPlainAccessConfigs(Collections.emptyList()); + return aclConfig; + } + + @Override + public void addAclConfig(PlainAccessConfig config) { + try { + Set<String> masterSet = getMasterSet(); + + if (masterSet.isEmpty()) { + throw new IllegalStateException("broker addr list is empty"); + } + // check to see if account is exists + for (String addr : + masterSet) { Review comment: can be on the same line. ########## File path: src/main/java/org/apache/rocketmq/dashboard/controller/AclController.java ########## @@ -0,0 +1,146 @@ +/* + * 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.dashboard.controller; + +import com.google.common.base.Preconditions; +import java.util.List; +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.common.AclConfig; +import org.apache.rocketmq.common.PlainAccessConfig; +import org.apache.rocketmq.dashboard.config.RMQConfigure; +import org.apache.rocketmq.dashboard.model.User; +import org.apache.rocketmq.dashboard.model.UserInfo; +import org.apache.rocketmq.dashboard.model.request.AclRequest; +import org.apache.rocketmq.dashboard.permisssion.Permission; +import org.apache.rocketmq.dashboard.service.AclService; +import org.apache.rocketmq.dashboard.support.JsonResult; +import org.apache.rocketmq.dashboard.util.WebUtil; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/acl") +@Permission +public class AclController { + + @Resource + private AclService aclService; + + @Resource + private RMQConfigure configure; + + @GetMapping("/enable.query") + public Object isEnableAcl() { + return new JsonResult<>(configure.isACLEnabled()); + } + + @GetMapping("/config.query") + public AclConfig getAclConfig(HttpServletRequest request) { + if (!configure.isLoginRequired()) { + return aclService.getAclConfig(false); + } + UserInfo userInfo = (UserInfo) WebUtil.getValueFromSession(request, WebUtil.USER_INFO); + // if user info is null but reach here, must exclude secret key for safety. + return aclService.getAclConfig(userInfo == null || userInfo.getUser().getType() != User.ADMIN); + } + + @PostMapping("/add.do") + public Object addAclConfig(@RequestBody PlainAccessConfig config) { + Preconditions.checkArgument(StringUtils.isNotEmpty(config.getAccessKey()), "accessKey is null"); + Preconditions.checkArgument(StringUtils.isNotEmpty(config.getSecretKey()), "secretKey is null"); + aclService.addAclConfig(config); + return new JsonResult(0, ""); + } + + @PostMapping("/delete.do") + public Object deleteAclConfig(@RequestBody PlainAccessConfig config) { + Preconditions.checkArgument(StringUtils.isNotEmpty(config.getAccessKey()), "accessKey is null"); + aclService.deleteAclConfig(config); + return new JsonResult(0, ""); Review comment: ditto -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
