// 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.cloudstack.acl;

import com.cloud.server.ManagementServer;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.component.ComponentLocator;
import com.cloud.utils.component.PluggableService;

import javax.ejb.Local;
import javax.naming.ConfigurationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

// This is the default API access checker that grab's the user's account
// based on the account type, access is granted
@Local(value=APIChecker.class)
public class StaticRoleBasedAPIAccessChecker extends AdapterBase implements APIChecker {

    protected static final Logger s_logger = Logger.getLogger(StaticRoleBasedAPIAccessChecker.class);

    private static Map<RoleType, Set<String>> s_roleBasedApisMap =
            new HashMap<RoleType, Set<String>>();

    protected StaticRoleBasedAPIAccessChecker() {
        super();
        for (RoleType roleType: RoleType.values()) {
            s_roleBasedApisMap.put(roleType, new HashSet<String>());
        }
    }

    @Override
    public boolean checkAccess(RoleType roleType, String commandName) {
            return s_roleBasedApisMap.get(roleType).contains(commandName);
    }

    @Override
    public boolean checkExistence(String apiName) {
        for (RoleType roleType: RoleType.values()) {
            if (s_roleBasedApisMap.get(roleType).contains(apiName))
                return true;
        }
        return false;
    }

    @Override
    public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
        super.configure(name, params);

        // Read command properties files to build the static map per role.
        ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
        List<PluggableService> services = locator.getAllPluggableServices();
        services.add((PluggableService) ComponentLocator.getComponent(ManagementServer.Name));

        Map<String, String> configPropertiesMap = new HashMap<String, String>();
        for (PluggableService service : services) {
            configPropertiesMap.putAll(service.getProperties());
        }

        processConfigFiles(configPropertiesMap);
        return true;
    }

    private void processConfigFiles(Map<String, String> configMap) {
        for (Map.Entry<String, String> entry: configMap.entrySet()) {
            String apiName = entry.getKey();
            String roleMask = entry.getValue();
            try {
                short cmdPermissions = Short.parseShort(roleMask);
                for (RoleType roleType: RoleType.values()) {
                    if ((cmdPermissions & roleType.getValue()) != 0)
                        s_roleBasedApisMap.get(roleType).add(apiName);
                }
            } catch (NumberFormatException nfe) {
                s_logger.info("Malformed commands.properties permissions value, for entry: " + entry.toString());
            }
        }
    }
}

@@ -1814,34 +1814,32 @@
 leType.values())
- %7B
 %0A            s_r
@@ -1892,26 +1892,16 @@
 ng%3E());%0A
-        %7D%0A
     %7D%0A%0A 
@@ -2825,193 +2825,135 @@
     
-Map%3CString, String%3E configPropertiesMap = new HashMap%3CString, String%3E();%0A        for (PluggableService service : services) %7B%0A            configPropertiesMap.putAll(service.getProperties
+for (PluggableService service : services) %7B%0A            processConfigFiles(service.getProperties(), service.getClass().toString
 ());
@@ -2965,59 +2965,91 @@
     
-%7D%0A%0A
     
-    processConfigFiles(configPropertiesMap);
+s_logger.info(%22Processed role based acl for: %22 + service.toString());%0A        %7D
 %0A   
@@ -3138,16 +3138,32 @@
 onfigMap
+, String service
 ) %7B%0A    
@@ -3720,46 +3720,82 @@
 med 
-commands.properties permissions value,
+getProperties() value for service: %22 + service%0A                        + %22
  for

Fix acl cfg processing error messages