|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.acl; |
18 | 18 |
|
19 | | -import java.io.File; |
20 | | -import java.io.FileInputStream; |
21 | | -import java.io.FileNotFoundException; |
22 | | -import java.io.IOException; |
23 | | -import java.io.InputStream; |
24 | | -import java.util.ArrayList; |
25 | | -import java.util.Arrays; |
| 19 | +import static org.apache.cloudstack.acl.RoleType.Admin; |
| 20 | +import static org.apache.cloudstack.acl.RoleType.DomainAdmin; |
| 21 | +import static org.apache.cloudstack.acl.RoleType.ResourceAdmin; |
| 22 | +import static org.apache.cloudstack.acl.RoleType.User; |
| 23 | + |
| 24 | +import java.util.HashMap; |
26 | 25 | import java.util.HashSet; |
27 | 26 | import java.util.List; |
28 | 27 | import java.util.Map; |
29 | | -import java.util.Properties; |
30 | 28 | import java.util.Set; |
31 | 29 |
|
32 | 30 | import javax.ejb.Local; |
|
37 | 35 |
|
38 | 36 | import com.cloud.exception.PermissionDeniedException; |
39 | 37 | import com.cloud.user.AccountManager; |
40 | | -import com.cloud.utils.PropertiesUtil; |
41 | 38 | import com.cloud.utils.component.AdapterBase; |
42 | 39 | import com.cloud.utils.component.PluggableService; |
43 | 40 |
|
@@ -70,104 +67,70 @@ protected StaticRoleBasedAPIAccessChecker() { |
70 | 67 | } |
71 | 68 |
|
72 | 69 | @Override |
73 | | - public boolean canAccessAPI(RoleType roleType, String apiCommandName) |
74 | | - throws PermissionDeniedException{ |
75 | | - |
76 | | - boolean commandExists = s_allCommands.contains(apiCommandName); |
77 | | - |
78 | | - if(commandExists) { |
79 | | - return isCommandAvailableForAccount(roleType, apiCommandName); |
80 | | - } |
81 | | - |
82 | | - return commandExists; |
83 | | - } |
84 | | - |
85 | | - private static boolean isCommandAvailableForAccount(RoleType roleType, String commandName) { |
86 | | - boolean isCommandAvailable = false; |
87 | | - switch (roleType) { |
88 | | - case Admin: |
89 | | - isCommandAvailable = s_adminCommands.contains(commandName); |
90 | | - break; |
91 | | - case DomainAdmin: |
92 | | - isCommandAvailable = s_resellerCommands.contains(commandName); |
93 | | - break; |
94 | | - case ResourceAdmin: |
95 | | - isCommandAvailable = s_resourceDomainAdminCommands.contains(commandName); |
96 | | - break; |
97 | | - case User: |
98 | | - isCommandAvailable = s_userCommands.contains(commandName); |
99 | | - break; |
| 70 | + public boolean canAccessAPI(RoleType roleType, String commandName) |
| 71 | + throws PermissionDeniedException { |
| 72 | + |
| 73 | + boolean commandExists = s_allCommands.contains(commandName); |
| 74 | + boolean commandAccessible = false; |
| 75 | + |
| 76 | + if (commandExists) { |
| 77 | + switch (roleType) { |
| 78 | + case Admin: |
| 79 | + commandAccessible = s_adminCommands.contains(commandName); |
| 80 | + break; |
| 81 | + case DomainAdmin: |
| 82 | + commandAccessible = s_resellerCommands.contains(commandName); |
| 83 | + break; |
| 84 | + case ResourceAdmin: |
| 85 | + commandAccessible = s_resourceDomainAdminCommands.contains(commandName); |
| 86 | + break; |
| 87 | + case User: |
| 88 | + commandAccessible = s_userCommands.contains(commandName); |
| 89 | + break; |
| 90 | + } |
100 | 91 | } |
101 | | - return isCommandAvailable; |
| 92 | + return commandExists && commandAccessible; |
102 | 93 | } |
103 | 94 |
|
104 | 95 | @Override |
105 | 96 | public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { |
106 | 97 | super.configure(name, params); |
107 | 98 |
|
108 | | - List<String> configFiles = new ArrayList<String>(); |
| 99 | + // Read command properties files to build the static map per role. |
| 100 | + Map<String, String> configPropertiesMap = new HashMap<String, String>(); |
109 | 101 | for (PluggableService service : _services) { |
110 | | - configFiles.addAll(Arrays.asList(service.getPropertiesFiles())); |
| 102 | + configPropertiesMap.putAll(service.getProperties()); |
111 | 103 | } |
112 | 104 |
|
113 | | - processConfigFiles(configFiles); |
| 105 | + processConfigFiles(configPropertiesMap); |
114 | 106 | return true; |
115 | 107 | } |
116 | 108 |
|
117 | | - private void processConfigFiles(List<String> configFiles) { |
118 | | - Properties preProcessedCommands = new Properties(); |
119 | | - |
120 | | - for (String configFile : configFiles) { |
121 | | - File commandsFile = PropertiesUtil.findConfigFile(configFile); |
122 | | - if (commandsFile != null) { |
123 | | - try { |
124 | | - preProcessedCommands.load(new FileInputStream(commandsFile)); |
125 | | - } catch (FileNotFoundException fnfex) { |
126 | | - // in case of a file within a jar in classpath, try to open stream using url |
127 | | - InputStream stream = PropertiesUtil.openStreamFromURL(configFile); |
128 | | - if (stream != null) { |
129 | | - try { |
130 | | - preProcessedCommands.load(stream); |
131 | | - } catch (IOException e) { |
132 | | - s_logger.error("IO Exception, unable to find properties file:", fnfex); |
133 | | - } |
134 | | - } else { |
135 | | - s_logger.error("Unable to find properites file", fnfex); |
136 | | - } |
137 | | - } catch (IOException ioe) { |
138 | | - s_logger.error("IO Exception loading properties file", ioe); |
139 | | - } |
140 | | - } |
141 | | - } |
142 | | - |
143 | | - for (Object key : preProcessedCommands.keySet()) { |
144 | | - String preProcessedCommand = preProcessedCommands.getProperty((String) key); |
145 | | - int splitIndex = preProcessedCommand.lastIndexOf(";"); |
146 | | - // Backward compatible to old style, apiname=pkg;mask |
147 | | - String mask = preProcessedCommand.substring(splitIndex+1); |
148 | | - |
| 109 | + private void processConfigFiles(Map<String, String> config) { |
| 110 | + for (Map.Entry<String, String> entry: config.entrySet()) { |
| 111 | + String apiName = entry.getKey(); |
| 112 | + String roleMask = entry.getValue(); |
149 | 113 | try { |
150 | | - short cmdPermissions = Short.parseShort(mask); |
| 114 | + short cmdPermissions = Short.parseShort(roleMask); |
151 | 115 | if ((cmdPermissions & Admin.getValue()) != 0) { |
152 | | - s_adminCommands.add((String) key); |
| 116 | + s_adminCommands.add(apiName); |
153 | 117 | } |
154 | 118 | if ((cmdPermissions & ResourceAdmin.getValue()) != 0) { |
155 | | - s_resourceDomainAdminCommands.add((String) key); |
| 119 | + s_resourceDomainAdminCommands.add(apiName); |
156 | 120 | } |
157 | 121 | if ((cmdPermissions & DomainAdmin.getValue()) != 0) { |
158 | | - s_resellerCommands.add((String) key); |
| 122 | + s_resellerCommands.add(apiName); |
159 | 123 | } |
160 | 124 | if ((cmdPermissions & User.getValue()) != 0) { |
161 | | - s_userCommands.add((String) key); |
| 125 | + s_userCommands.add(apiName); |
162 | 126 | } |
163 | | - s_allCommands.addAll(s_adminCommands); |
164 | | - s_allCommands.addAll(s_resourceDomainAdminCommands); |
165 | | - s_allCommands.addAll(s_userCommands); |
166 | | - s_allCommands.addAll(s_resellerCommands); |
167 | 127 | } catch (NumberFormatException nfe) { |
168 | | - s_logger.info("Malformed command.properties permissions value, key = " + key + ", value = " + preProcessedCommand); |
| 128 | + s_logger.info("Malformed commands.properties permissions value, for entry: " + entry.toString()); |
169 | 129 | } |
170 | 130 | } |
| 131 | + s_allCommands.addAll(s_adminCommands); |
| 132 | + s_allCommands.addAll(s_resourceDomainAdminCommands); |
| 133 | + s_allCommands.addAll(s_userCommands); |
| 134 | + s_allCommands.addAll(s_resellerCommands); |
171 | 135 | } |
172 | | - |
173 | 136 | } |
0 commit comments