5252import javax .servlet .http .HttpSession ;
5353
5454import org .apache .cloudstack .acl .APIChecker ;
55- import org .apache .cloudstack .acl .AclPermissionVO ;
56- import org .apache .cloudstack .acl .AclPolicyPermissionMapVO ;
55+ import org .apache .cloudstack .acl .AclPolicyPermissionVO ;
5756import org .apache .cloudstack .acl .PermissionScope ;
5857import org .apache .cloudstack .acl .RoleType ;
59- import org .apache .cloudstack .acl .AclPermission .Permission ;
58+ import org .apache .cloudstack .acl .AclPolicyPermission .Permission ;
6059import org .apache .cloudstack .acl .SecurityChecker .AccessType ;
61- import org .apache .cloudstack .acl .dao .AclPermissionDao ;
62- import org .apache .cloudstack .acl . dao . AclPolicyPermissionMapDao ;
60+ import org .apache .cloudstack .acl .dao .AclPolicyPermissionDao ;
61+ import org .apache .cloudstack .affinity . AffinityGroupVMMapVO ;
6362import org .apache .cloudstack .api .APICommand ;
6463import org .apache .cloudstack .api .ApiErrorCode ;
6564import org .apache .cloudstack .api .BaseAsyncCmd ;
149148import com .cloud .user .UserVO ;
150149import com .cloud .utils .NumbersUtil ;
151150import com .cloud .utils .Pair ;
151+ import com .cloud .utils .PropertiesUtil ;
152152import com .cloud .utils .StringUtils ;
153153import com .cloud .utils .component .ComponentContext ;
154154import com .cloud .utils .component .ManagerBase ;
155155import com .cloud .utils .component .PluggableService ;
156156import com .cloud .utils .concurrency .NamedThreadFactory ;
157157import com .cloud .utils .db .EntityManager ;
158+ import com .cloud .utils .db .SearchBuilder ;
158159import com .cloud .utils .db .SearchCriteria ;
159160import com .cloud .utils .db .TransactionLegacy ;
160161import com .cloud .utils .exception .CloudRuntimeException ;
@@ -180,23 +181,26 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer
180181 List <PluggableService > _pluggableServices ;
181182 List <APIChecker > _apiAccessCheckers ;
182183 @ Inject
183- private AclPermissionDao _aclPermissionDao ;
184- @ Inject
185- private AclPolicyPermissionMapDao _aclPolicyPermissionMapDao ;
184+ private AclPolicyPermissionDao _aclPermissionDao ;
186185
187186 @ Inject
188187 protected ApiAsyncJobDispatcher _asyncDispatcher ;
189188 private static int _workerCount = 0 ;
190189 private static final DateFormat _dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssZ" );
191190 private static Map <String , List <Class <?>>> _apiNameCmdClassMap = new HashMap <String , List <Class <?>>>();
192191
192+ private static Set <String > commandsPropertiesOverrides = new HashSet <String >();
193+ private static Map <RoleType , Set <String >> commandsPropertiesRoleBasedApisMap = new HashMap <RoleType , Set <String >>();
194+
195+
193196 private static ExecutorService _executor = new ThreadPoolExecutor (10 , 150 , 60 , TimeUnit .SECONDS , new LinkedBlockingQueue <Runnable >(), new NamedThreadFactory ("ApiServer" ));
194197
195198 public ApiServer () {
196199 }
197200
198201 @ Override
199202 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
203+ processMapping (PropertiesUtil .processConfigFile (new String [] { "commands.properties" }));
200204 return true ;
201205 }
202206
@@ -233,6 +237,40 @@ public boolean start() {
233237 }
234238 }
235239
240+ // drop all default policy api permissions - we reload them every time
241+ // to include any chanegs done to the @APICommand or
242+ // commands.properties.
243+ SearchBuilder <AclPolicyPermissionVO > sb = _aclPermissionDao .createSearchBuilder ();
244+ sb .and ("policyId" , sb .entity ().getAclPolicyId (), SearchCriteria .Op .EQ );
245+ sb .and ("resourceType" , sb .entity ().getEntityType (), SearchCriteria .Op .NULL );
246+ sb .and ("scope" , sb .entity ().getScope (), SearchCriteria .Op .EQ );
247+ sb .done ();
248+
249+ SearchCriteria <AclPolicyPermissionVO > permissionSC = sb .create ();
250+
251+ for (RoleType role : RoleType .values ()) {
252+ permissionSC .setParameters ("policyId" , role .ordinal () + 1 );
253+ switch (role ) {
254+ case User :
255+ permissionSC .setParameters ("scope" , PermissionScope .ACCOUNT .toString ());
256+ break ;
257+
258+ case Admin :
259+ permissionSC .setParameters ("scope" , PermissionScope .ALL .toString ());
260+ break ;
261+
262+ case DomainAdmin :
263+ permissionSC .setParameters ("scope" , PermissionScope .DOMAIN .toString ());
264+ break ;
265+
266+ case ResourceAdmin :
267+ permissionSC .setParameters ("scope" , PermissionScope .DOMAIN .toString ());
268+ break ;
269+ }
270+ _aclPermissionDao .expunge (permissionSC );
271+
272+ }
273+
236274 for (Class <?> cmdClass : cmdClasses ) {
237275 APICommand at = cmdClass .getAnnotation (APICommand .class );
238276 if (at == null ) {
@@ -246,52 +284,27 @@ public boolean start() {
246284 }
247285 apiCmdList .add (cmdClass );
248286
249- boolean isReadCommand = false ;
250- BaseCmd cmdObj ;
251- try {
252- cmdObj = (BaseCmd ) cmdClass .newInstance ();
253- if (cmdObj instanceof BaseListCmd ) {
254- isReadCommand = true ;
287+ if (!commandsPropertiesOverrides .contains (apiName )) {
288+ for (RoleType role : at .authorized ()) {
289+ addDefaultAclPolicyPermission (apiName , cmdClass , role );
255290 }
256- } catch (Exception e ) {
257291 }
292+ }
258293
259- for (RoleType role : at .authorized ()) {
260- AclPermissionVO apiPermission = null ;
261- switch (role ) {
262- case User :
263- apiPermission = new AclPermissionVO (apiName , null , null , PermissionScope .ACCOUNT , null ,
264- Permission .Allow );
265- break ;
266-
267- case Admin :
268- apiPermission = new AclPermissionVO (apiName , null , null , PermissionScope .ALL , null ,
269- Permission .Allow );
270- break ;
271-
272- case DomainAdmin :
273- apiPermission = new AclPermissionVO (apiName , null , null , PermissionScope .DOMAIN , null ,
274- Permission .Allow );
275- break ;
276-
277- case ResourceAdmin :
278- apiPermission = new AclPermissionVO (apiName , null , null , PermissionScope .DOMAIN , null ,
279- Permission .Allow );
280- break ;
281- }
294+ // read commands.properties and load api acl permissions -
295+ // commands.properties overrides any @APICommand authorization
282296
283- if (apiPermission != null ) {
284- if (isReadCommand ) {
285- apiPermission .setAccessType (AccessType .ListEntry );
286- }
287- _aclPermissionDao .persist (apiPermission );
288- AclPolicyPermissionMapVO policyPermMapEntry = new AclPolicyPermissionMapVO (role .ordinal () + 1 ,
289- apiPermission .getId ());
290- _aclPolicyPermissionMapDao .persist (policyPermMapEntry );
297+ for (String apiName : commandsPropertiesOverrides ) {
298+ Class <?> cmdClass = getCmdClass (apiName );
299+ for (RoleType role : RoleType .values ()) {
300+ if (commandsPropertiesRoleBasedApisMap .get (role ).contains (apiName )) {
301+ // insert permission for this role for this api
302+ addDefaultAclPolicyPermission (apiName , cmdClass , role );
291303 }
292304 }
293305 }
294306
307+
295308 encodeApiResponse = Boolean .valueOf (_configDao .getValue (Config .EncodeApiResponse .key ()));
296309 String jsonType = _configDao .getValue (Config .JavaScriptDefaultContentType .key ());
297310 if (jsonType != null ) {
@@ -306,6 +319,74 @@ public boolean start() {
306319 return true ;
307320 }
308321
322+ private void processMapping (Map <String , String > configMap ) {
323+ for (RoleType roleType : RoleType .values ()) {
324+ commandsPropertiesRoleBasedApisMap .put (roleType , new HashSet <String >());
325+ }
326+
327+ for (Map .Entry <String , String > entry : configMap .entrySet ()) {
328+ String apiName = entry .getKey ();
329+ String roleMask = entry .getValue ();
330+ commandsPropertiesOverrides .add (apiName );
331+ try {
332+ short cmdPermissions = Short .parseShort (roleMask );
333+ for (RoleType roleType : RoleType .values ()) {
334+ if ((cmdPermissions & roleType .getValue ()) != 0 )
335+ commandsPropertiesRoleBasedApisMap .get (roleType ).add (apiName );
336+ }
337+ } catch (NumberFormatException nfe ) {
338+ s_logger .info ("Malformed key=value pair for entry: " + entry .toString ());
339+ }
340+ }
341+ }
342+
343+ private void addDefaultAclPolicyPermission (String apiName , Class <?> cmdClass , RoleType role ) {
344+
345+ boolean isReadCommand = false ;
346+ if (cmdClass != null ) {
347+ BaseCmd cmdObj ;
348+ try {
349+ cmdObj = (BaseCmd ) cmdClass .newInstance ();
350+ if (cmdObj instanceof BaseListCmd ) {
351+ isReadCommand = true ;
352+ }
353+ } catch (Exception e ) {
354+ throw new CloudRuntimeException (String .format (
355+ "%s is claimed as an API command, but it cannot be instantiated" , cmdClass .getName ()));
356+ }
357+ }
358+
359+ AclPolicyPermissionVO apiPermission = null ;
360+ switch (role ) {
361+ case User :
362+ apiPermission = new AclPolicyPermissionVO (role .ordinal () + 1 , apiName , null , null , PermissionScope .ACCOUNT ,
363+ null , Permission .Allow );
364+ break ;
365+
366+ case Admin :
367+ apiPermission = new AclPolicyPermissionVO (role .ordinal () + 1 , apiName , null , null , PermissionScope .ALL ,
368+ null , Permission .Allow );
369+ break ;
370+
371+ case DomainAdmin :
372+ apiPermission = new AclPolicyPermissionVO (role .ordinal () + 1 , apiName , null , null , PermissionScope .DOMAIN ,
373+ null , Permission .Allow );
374+ break ;
375+
376+ case ResourceAdmin :
377+ apiPermission = new AclPolicyPermissionVO (role .ordinal () + 1 , apiName , null , null , PermissionScope .DOMAIN ,
378+ null , Permission .Allow );
379+ break ;
380+ }
381+
382+ if (apiPermission != null ) {
383+ if (isReadCommand ) {
384+ apiPermission .setAccessType (AccessType .ListEntry );
385+ }
386+ _aclPermissionDao .persist (apiPermission );
387+ }
388+ }
389+
309390 // NOTE: handle() only handles over the wire (OTW) requests from integration.api.port 8096
310391 // If integration api port is not configured, actual OTW requests will be received by ApiServlet
311392 @ SuppressWarnings ({ "unchecked" , "rawtypes" })
@@ -945,7 +1026,8 @@ else if (cmdList.size() == 1)
9451026 else {
9461027 // determine the cmd class based on calling context
9471028 ResponseView view = ResponseView .Restricted ;
948- if (_accountMgr .isRootAdmin (CallContext .current ().getCallingAccount ().getId ())) {
1029+ if (CallContext .current () != null
1030+ && _accountMgr .isRootAdmin (CallContext .current ().getCallingAccount ().getId ())) {
9491031 view = ResponseView .Full ;
9501032 }
9511033 for (Class <?> cmdClass : cmdList ) {
0 commit comments