You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2019/06/12 02:24:05 UTC

[GitHub] [rocketmq] duhenglucky commented on a change in pull request #1217: [ISSUE #1156]new mqadmin API for ACL configuration

duhenglucky commented on a change in pull request #1217: [ISSUE #1156]new mqadmin API for ACL configuration
URL: https://github.com/apache/rocketmq/pull/1217#discussion_r292722291
 
 

 ##########
 File path: acl/src/main/java/org/apache/rocketmq/acl/plain/PlainPermissionLoader.java
 ##########
 @@ -89,10 +91,182 @@ public void load() {
             }
         }
 
+        //for loading dataversion part just
+        JSONArray dataVersion4Yaml = plainAclConfData.getJSONArray(AclConstants.CONFIG_DATA_VERSION);
+        if (dataVersion4Yaml != null && !dataVersion4Yaml.isEmpty()) {
+            List<DataVersion> dataVersion = dataVersion4Yaml.toJavaList(DataVersion.class);
+            DataVersion firstElement = dataVersion.get(0);
+            this.dataVersion.assignNewOne(firstElement);
+        }
+
         this.globalWhiteRemoteAddressStrategy = globalWhiteRemoteAddressStrategy;
         this.plainAccessResourceMap = plainAccessResourceMap;
     }
 
+    public String getAclConfigDataVersion() {
+        return this.dataVersion.toJson();
+    }
+
+    private Map<String, Object> updateAclConfigFileVersion(Map<String, Object> updateAclConfigMap) {
+
+        dataVersion.nextVersion();
+        List<Map<String, Object>> versionElement = new ArrayList<Map<String, Object>>();
+        Map<String, Object> accountsMap = new LinkedHashMap<String, Object>() {
+            {
+                put(AclConstants.CONFIG_COUNTER, dataVersion.getCounter().longValue());
+                put(AclConstants.CONFIG_TIME_STAMP, dataVersion.getTimestamp());
+            }
+        };
+        versionElement.add(accountsMap);
+        updateAclConfigMap.put(AclConstants.CONFIG_DATA_VERSION, versionElement);
+        return updateAclConfigMap;
+    }
+
+    public boolean updateAccessConfig(PlainAccessConfig plainAccessConfig) {
+
+        if (plainAccessConfig == null) {
+            log.error("parameter value plainAccessConfig is null,please check your parameter");
+            return false;
+        }
+
+        Map<String, Object> aclAccessConfigMap = AclUtils.getYamlDataObject(fileHome + File.separator + fileName,
+            Map.class);
+
+        List<Map<String, Object>> accounts = (List<Map<String, Object>>) aclAccessConfigMap.get(AclConstants.CONFIG_ACCOUNTS);
+        Map<String, Object> updateAccountMap = null;
+        if (accounts != null) {
+            for (Map<String, Object> account : accounts) {
+                if (account.get(AclConstants.CONFIG_ACCESS_KEY).equals(plainAccessConfig.getAccessKey())) {
+                    //update acl access config elements
+                    accounts.remove(account);
+                    updateAccountMap = createAclAccessConfigMap(account, plainAccessConfig);
+                    accounts.add(updateAccountMap);
+                    aclAccessConfigMap.put(AclConstants.CONFIG_ACCOUNTS, accounts);
+
+                    if (AclUtils.writeDataObject2Yaml(fileHome + File.separator + fileName, updateAclConfigFileVersion(aclAccessConfigMap))) {
+                        return true;
+                    }
+                    return false;
+                }
+            }
+            //create acl access config elements
+            accounts.add(createAclAccessConfigMap(null, plainAccessConfig));
+            aclAccessConfigMap.put(AclConstants.CONFIG_ACCOUNTS, accounts);
+            if (AclUtils.writeDataObject2Yaml(fileHome + File.separator + fileName, updateAclConfigFileVersion(aclAccessConfigMap))) {
+                return true;
+            }
+            return false;
+        }
+
+        log.error("users must ensure the acl yaml config file has accounts node element");
+        return false;
+    }
+
+    private Map<String, Object> createAclAccessConfigMap(Map<String, Object> existedAccoutMap, PlainAccessConfig plainAccessConfig) {
+
+
+        Map<String, Object> newAccountsMap = null;
+        if (existedAccoutMap == null) {
+            newAccountsMap = new LinkedHashMap<String, Object>();
+        } else {
+            newAccountsMap = existedAccoutMap;
+        }
+
+        if (StringUtils.isEmpty(plainAccessConfig.getAccessKey()) ||
+            plainAccessConfig.getAccessKey().length() <= 6) {
+            throw new AclException(String.format(
+                    "The accessKey=%s cannot be null and length should longer than 6",
+                    plainAccessConfig.getAccessKey()));
+        }
+        newAccountsMap.put(AclConstants.CONFIG_ACCESS_KEY, plainAccessConfig.getAccessKey());
+
+        if (!StringUtils.isEmpty(plainAccessConfig.getSecretKey())) {
+            if (plainAccessConfig.getSecretKey().length() <= 6) {
 
 Review comment:
   Try not to use magic numbers, yeah, I mean 6, maybe it just is a username in some situation.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services