|
| 1 | +package com.cloud.acl; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Properties; |
| 12 | + |
| 13 | +import javax.ejb.Local; |
| 14 | +import javax.naming.ConfigurationException; |
| 15 | + |
| 16 | +import org.apache.log4j.Logger; |
| 17 | + |
| 18 | +import com.cloud.exception.PermissionDeniedException; |
| 19 | +import com.cloud.server.ManagementServer; |
| 20 | +import com.cloud.user.Account; |
| 21 | +import com.cloud.user.AccountManager; |
| 22 | +import com.cloud.user.User; |
| 23 | +import com.cloud.utils.PropertiesUtil; |
| 24 | +import com.cloud.utils.component.AdapterBase; |
| 25 | +import com.cloud.utils.component.ComponentLocator; |
| 26 | +import com.cloud.utils.component.Inject; |
| 27 | +import com.cloud.utils.component.PluggableService; |
| 28 | + |
| 29 | +/* |
| 30 | + * This is the default API access checker that grab's the user's account |
| 31 | + * based on the account type, access is granted referring to commands in all *.properties files. |
| 32 | + */ |
| 33 | + |
| 34 | +@Local(value=APIAccessChecker.class) |
| 35 | +public class StaticRoleBasedAPIAccessChecker extends AdapterBase implements APIAccessChecker { |
| 36 | + |
| 37 | + protected static final Logger s_logger = Logger.getLogger(StaticRoleBasedAPIAccessChecker.class); |
| 38 | + public static final short ADMIN_COMMAND = 1; |
| 39 | + public static final short DOMAIN_ADMIN_COMMAND = 4; |
| 40 | + public static final short RESOURCE_DOMAIN_ADMIN_COMMAND = 2; |
| 41 | + public static final short USER_COMMAND = 8; |
| 42 | + private static List<String> s_userCommands = null; |
| 43 | + private static List<String> s_resellerCommands = null; // AKA domain-admin |
| 44 | + private static List<String> s_adminCommands = null; |
| 45 | + private static List<String> s_resourceDomainAdminCommands = null; |
| 46 | + private static List<String> s_allCommands = null; |
| 47 | + private static List<String> s_pluggableServiceCommands = null; |
| 48 | + |
| 49 | + protected @Inject AccountManager _accountMgr; |
| 50 | + |
| 51 | + static { |
| 52 | + s_allCommands = new ArrayList<String>(); |
| 53 | + s_userCommands = new ArrayList<String>(); |
| 54 | + s_resellerCommands = new ArrayList<String>(); |
| 55 | + s_adminCommands = new ArrayList<String>(); |
| 56 | + s_resourceDomainAdminCommands = new ArrayList<String>(); |
| 57 | + s_pluggableServiceCommands = new ArrayList<String>(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean canAccessAPI(User user, String apiCommandName) |
| 62 | + throws PermissionDeniedException{ |
| 63 | + |
| 64 | + boolean commandExists = s_allCommands.contains(apiCommandName); |
| 65 | + |
| 66 | + if(commandExists && user != null){ |
| 67 | + Long accountId = user.getAccountId(); |
| 68 | + Account userAccount = _accountMgr.getAccount(accountId); |
| 69 | + short accountType = userAccount.getType(); |
| 70 | + return isCommandAvailableForAccount(accountType, apiCommandName); |
| 71 | + } |
| 72 | + |
| 73 | + return commandExists; |
| 74 | + } |
| 75 | + |
| 76 | + private static boolean isCommandAvailableForAccount(short accountType, String commandName) { |
| 77 | + boolean isCommandAvailable = false; |
| 78 | + switch (accountType) { |
| 79 | + case Account.ACCOUNT_TYPE_ADMIN: |
| 80 | + isCommandAvailable = s_adminCommands.contains(commandName); |
| 81 | + break; |
| 82 | + case Account.ACCOUNT_TYPE_DOMAIN_ADMIN: |
| 83 | + isCommandAvailable = s_resellerCommands.contains(commandName); |
| 84 | + break; |
| 85 | + case Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN: |
| 86 | + isCommandAvailable = s_resourceDomainAdminCommands.contains(commandName); |
| 87 | + break; |
| 88 | + case Account.ACCOUNT_TYPE_NORMAL: |
| 89 | + isCommandAvailable = s_userCommands.contains(commandName); |
| 90 | + break; |
| 91 | + } |
| 92 | + return isCommandAvailable; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { |
| 98 | + super.configure(name, params); |
| 99 | + |
| 100 | + //load command.properties to build the static map per role. |
| 101 | + ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); |
| 102 | + String[] apiConfig = ((ManagementServer) ComponentLocator.getComponent(ManagementServer.Name)).getApiConfig(); |
| 103 | + |
| 104 | + processConfigFiles(apiConfig, false); |
| 105 | + |
| 106 | + // get commands for all pluggable services |
| 107 | + String[] pluggableServicesApiConfigs = getPluggableServicesApiConfigs(); |
| 108 | + processConfigFiles(pluggableServicesApiConfigs, true); |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + private String[] getPluggableServicesApiConfigs() { |
| 115 | + List<String> pluggableServicesApiConfigs = new ArrayList<String>(); |
| 116 | + |
| 117 | + ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); |
| 118 | + List<PluggableService> services = locator.getAllPluggableServices(); |
| 119 | + for (PluggableService service : services) { |
| 120 | + pluggableServicesApiConfigs.add(service.getPropertiesFile()); |
| 121 | + } |
| 122 | + return pluggableServicesApiConfigs.toArray(new String[0]); |
| 123 | + } |
| 124 | + |
| 125 | + private void processConfigFiles(String[] apiConfig, boolean pluggableServicesConfig) { |
| 126 | + try { |
| 127 | + Properties preProcessedCommands = new Properties(); |
| 128 | + if (apiConfig != null) { |
| 129 | + for (String configFile : apiConfig) { |
| 130 | + File commandsFile = PropertiesUtil.findConfigFile(configFile); |
| 131 | + if (commandsFile != null) { |
| 132 | + try { |
| 133 | + preProcessedCommands.load(new FileInputStream(commandsFile)); |
| 134 | + } catch (FileNotFoundException fnfex) { |
| 135 | + // in case of a file within a jar in classpath, try to open stream using url |
| 136 | + InputStream stream = PropertiesUtil.openStreamFromURL(configFile); |
| 137 | + if (stream != null) { |
| 138 | + preProcessedCommands.load(stream); |
| 139 | + } else { |
| 140 | + s_logger.error("Unable to find properites file", fnfex); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + for (Object key : preProcessedCommands.keySet()) { |
| 146 | + String preProcessedCommand = preProcessedCommands.getProperty((String) key); |
| 147 | + String[] commandParts = preProcessedCommand.split(";"); |
| 148 | + |
| 149 | + |
| 150 | + if (pluggableServicesConfig) { |
| 151 | + s_pluggableServiceCommands.add(commandParts[0]); |
| 152 | + } |
| 153 | + |
| 154 | + if (commandParts.length > 1) { |
| 155 | + try { |
| 156 | + short cmdPermissions = Short.parseShort(commandParts[1]); |
| 157 | + if ((cmdPermissions & ADMIN_COMMAND) != 0) { |
| 158 | + s_adminCommands.add((String) key); |
| 159 | + } |
| 160 | + if ((cmdPermissions & RESOURCE_DOMAIN_ADMIN_COMMAND) != 0) { |
| 161 | + s_resourceDomainAdminCommands.add((String) key); |
| 162 | + } |
| 163 | + if ((cmdPermissions & DOMAIN_ADMIN_COMMAND) != 0) { |
| 164 | + s_resellerCommands.add((String) key); |
| 165 | + } |
| 166 | + if ((cmdPermissions & USER_COMMAND) != 0) { |
| 167 | + s_userCommands.add((String) key); |
| 168 | + } |
| 169 | + s_allCommands.addAll(s_adminCommands); |
| 170 | + s_allCommands.addAll(s_resourceDomainAdminCommands); |
| 171 | + s_allCommands.addAll(s_userCommands); |
| 172 | + s_allCommands.addAll(s_resellerCommands); |
| 173 | + } catch (NumberFormatException nfe) { |
| 174 | + s_logger.info("Malformed command.properties permissions value, key = " + key + ", value = " + preProcessedCommand); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + } catch (FileNotFoundException fnfex) { |
| 181 | + s_logger.error("Unable to find properites file", fnfex); |
| 182 | + } catch (IOException ioex) { |
| 183 | + s_logger.error("Exception loading properties file", ioex); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | +} |
0 commit comments