45
45
import org .apache .commons .collections .CollectionUtils ;
46
46
import org .apache .commons .lang3 .StringUtils ;
47
47
import org .apache .rocketmq .acl .AccessValidator ;
48
+ import org .apache .rocketmq .acl .common .AclException ;
48
49
import org .apache .rocketmq .acl .plain .PlainAccessValidator ;
49
50
import org .apache .rocketmq .auth .authentication .enums .UserType ;
50
51
import org .apache .rocketmq .auth .authentication .exception .AuthenticationException ;
@@ -771,26 +772,15 @@ private void deleteTopicInBroker(String topic) {
771
772
this .brokerController .getMessageStore ().deleteTopics (Sets .newHashSet (topic ));
772
773
}
773
774
774
- private synchronized RemotingCommand updateAndCreateAccessConfig (ChannelHandlerContext ctx ,
775
- RemotingCommand request ) throws RemotingCommandException {
775
+ private synchronized RemotingCommand updateAndCreateAccessConfig (ChannelHandlerContext ctx , RemotingCommand request ) {
776
776
final RemotingCommand response = RemotingCommand .createResponseCommand (null );
777
777
778
- final CreateAccessConfigRequestHeader requestHeader =
779
- (CreateAccessConfigRequestHeader ) request .decodeCommandCustomHeader (CreateAccessConfigRequestHeader .class );
780
-
781
- PlainAccessConfig accessConfig = new PlainAccessConfig ();
782
- accessConfig .setAccessKey (requestHeader .getAccessKey ());
783
- accessConfig .setSecretKey (requestHeader .getSecretKey ());
784
- accessConfig .setWhiteRemoteAddress (requestHeader .getWhiteRemoteAddress ());
785
- accessConfig .setDefaultTopicPerm (requestHeader .getDefaultTopicPerm ());
786
- accessConfig .setDefaultGroupPerm (requestHeader .getDefaultGroupPerm ());
787
- accessConfig .setTopicPerms (UtilAll .split (requestHeader .getTopicPerms (), "," ));
788
- accessConfig .setGroupPerms (UtilAll .split (requestHeader .getGroupPerms (), "," ));
789
- accessConfig .setAdmin (requestHeader .isAdmin ());
790
778
try {
779
+ ensureAclEnabled ();
791
780
781
+ final CreateAccessConfigRequestHeader requestHeader = request .decodeCommandCustomHeader (CreateAccessConfigRequestHeader .class );
792
782
AccessValidator accessValidator = this .brokerController .getAccessValidatorMap ().get (PlainAccessValidator .class );
793
- if (accessValidator .updateAccessConfig (accessConfig )) {
783
+ if (accessValidator .updateAccessConfig (createAccessConfig ( requestHeader ) )) {
794
784
response .setCode (ResponseCode .SUCCESS );
795
785
response .setOpaque (request .getOpaque ());
796
786
response .markResponseType ();
@@ -813,15 +803,28 @@ private synchronized RemotingCommand updateAndCreateAccessConfig(ChannelHandlerC
813
803
return null ;
814
804
}
815
805
816
- private synchronized RemotingCommand deleteAccessConfig (ChannelHandlerContext ctx ,
817
- RemotingCommand request ) throws RemotingCommandException {
806
+ private PlainAccessConfig createAccessConfig (final CreateAccessConfigRequestHeader requestHeader ) {
807
+ PlainAccessConfig accessConfig = new PlainAccessConfig ();
808
+ accessConfig .setAccessKey (requestHeader .getAccessKey ());
809
+ accessConfig .setSecretKey (requestHeader .getSecretKey ());
810
+ accessConfig .setWhiteRemoteAddress (requestHeader .getWhiteRemoteAddress ());
811
+ accessConfig .setDefaultTopicPerm (requestHeader .getDefaultTopicPerm ());
812
+ accessConfig .setDefaultGroupPerm (requestHeader .getDefaultGroupPerm ());
813
+ accessConfig .setTopicPerms (UtilAll .split (requestHeader .getTopicPerms (), "," ));
814
+ accessConfig .setGroupPerms (UtilAll .split (requestHeader .getGroupPerms (), "," ));
815
+ accessConfig .setAdmin (requestHeader .isAdmin ());
816
+ return accessConfig ;
817
+ }
818
+
819
+ private synchronized RemotingCommand deleteAccessConfig (ChannelHandlerContext ctx , RemotingCommand request ) {
818
820
final RemotingCommand response = RemotingCommand .createResponseCommand (null );
819
821
820
- final DeleteAccessConfigRequestHeader requestHeader =
821
- (DeleteAccessConfigRequestHeader ) request .decodeCommandCustomHeader (DeleteAccessConfigRequestHeader .class );
822
822
LOGGER .info ("DeleteAccessConfig called by {}" , RemotingHelper .parseChannelRemoteAddr (ctx .channel ()));
823
823
824
824
try {
825
+ ensureAclEnabled ();
826
+
827
+ final DeleteAccessConfigRequestHeader requestHeader = request .decodeCommandCustomHeader (DeleteAccessConfigRequestHeader .class );
825
828
String accessKey = requestHeader .getAccessKey ();
826
829
AccessValidator accessValidator = this .brokerController .getAccessValidatorMap ().get (PlainAccessValidator .class );
827
830
if (accessValidator .deleteAccessConfig (accessKey )) {
@@ -848,15 +851,13 @@ private synchronized RemotingCommand deleteAccessConfig(ChannelHandlerContext ct
848
851
return null ;
849
852
}
850
853
851
- private synchronized RemotingCommand updateGlobalWhiteAddrsConfig (ChannelHandlerContext ctx ,
852
- RemotingCommand request ) throws RemotingCommandException {
853
-
854
+ private synchronized RemotingCommand updateGlobalWhiteAddrsConfig (ChannelHandlerContext ctx , RemotingCommand request ) {
854
855
final RemotingCommand response = RemotingCommand .createResponseCommand (null );
855
856
856
- final UpdateGlobalWhiteAddrsConfigRequestHeader requestHeader =
857
- (UpdateGlobalWhiteAddrsConfigRequestHeader ) request .decodeCommandCustomHeader (UpdateGlobalWhiteAddrsConfigRequestHeader .class );
858
-
859
857
try {
858
+ ensureAclEnabled ();
859
+
860
+ final UpdateGlobalWhiteAddrsConfigRequestHeader requestHeader = request .decodeCommandCustomHeader (UpdateGlobalWhiteAddrsConfigRequestHeader .class );
860
861
AccessValidator accessValidator = this .brokerController .getAccessValidatorMap ().get (PlainAccessValidator .class );
861
862
if (accessValidator .updateGlobalWhiteAddrsConfig (UtilAll .split (requestHeader .getGlobalWhiteAddrs (), "," ),
862
863
requestHeader .getAclFileFullPath ())) {
@@ -883,18 +884,12 @@ private synchronized RemotingCommand updateGlobalWhiteAddrsConfig(ChannelHandler
883
884
}
884
885
885
886
private RemotingCommand getBrokerAclConfigVersion (ChannelHandlerContext ctx , RemotingCommand request ) {
886
-
887
887
final RemotingCommand response = RemotingCommand .createResponseCommand (GetBrokerAclConfigResponseHeader .class );
888
888
889
- if (!brokerController .getBrokerConfig ().isAclEnable ()) {
890
- response .setCode (ResponseCode .SYSTEM_ERROR );
891
- response .setRemark ("The broker does not enable acl." );
892
- return response ;
893
- }
894
-
895
- final GetBrokerAclConfigResponseHeader responseHeader = (GetBrokerAclConfigResponseHeader ) response .readCustomHeader ();
896
-
897
889
try {
890
+ ensureAclEnabled ();
891
+
892
+ final GetBrokerAclConfigResponseHeader responseHeader = (GetBrokerAclConfigResponseHeader ) response .readCustomHeader ();
898
893
AccessValidator accessValidator = this .brokerController .getAccessValidatorMap ().get (PlainAccessValidator .class );
899
894
900
895
responseHeader .setVersion (accessValidator .getAclConfigVersion ());
@@ -907,9 +902,16 @@ private RemotingCommand getBrokerAclConfigVersion(ChannelHandlerContext ctx, Rem
907
902
return response ;
908
903
} catch (Exception e ) {
909
904
LOGGER .error ("Failed to generate a proper getBrokerAclConfigVersion response" , e );
905
+ response .setCode (ResponseCode .SYSTEM_ERROR );
906
+ response .setRemark (e .getMessage ());
907
+ return response ;
910
908
}
909
+ }
911
910
912
- return null ;
911
+ private void ensureAclEnabled () {
912
+ if (!brokerController .getBrokerConfig ().isAclEnable ()) {
913
+ throw new AclException ("The broker does not enable acl." );
914
+ }
913
915
}
914
916
915
917
private RemotingCommand getUnknownCmdResponse (ChannelHandlerContext ctx , RemotingCommand request ) {
0 commit comments