plugin: ACL Static Role Based api access checker

Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
This commit is contained in:
Rohit Yadav 2013-01-02 16:56:48 -08:00
parent d235859168
commit bc8e0af0a3
5 changed files with 231 additions and 189 deletions

View File

@ -25,6 +25,11 @@
<artifactId>cloud-server</artifactId> <artifactId>cloud-server</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-plugin-acl-static-role-based</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.cloudstack</groupId> <groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-plugin-user-authenticator-ldap</artifactId> <artifactId>cloud-plugin-user-authenticator-ldap</artifactId>

View File

@ -54,7 +54,7 @@ under the License.
<param name="premium">true</param> <param name="premium">true</param>
</dao> </dao>
<adapters key="com.cloud.acl.APIAccessChecker"> <adapters key="com.cloud.acl.APIAccessChecker">
<adapter name="StaticRoleBasedAPIAccessChecker" class="com.cloud.acl.StaticRoleBasedAPIAccessChecker"/> <adapter name="StaticRoleBasedAPIAccessChecker" class="org.apache.cloudstack.acl.StaticRoleBasedAPIAccessChecker"/>
</adapters> </adapters>
<adapters key="com.cloud.agent.manager.allocator.HostAllocator"> <adapters key="com.cloud.agent.manager.allocator.HostAllocator">
<adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.FirstFitRoutingAllocator"/> <adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.FirstFitRoutingAllocator"/>

View File

@ -0,0 +1,32 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-plugin-acl-static-role-based</artifactId>
<name>Apache CloudStack Plugin - ACL Static Role Based</name>
<parent>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloudstack-plugins</artifactId>
<version>4.1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
</project>

View File

@ -1,188 +1,192 @@
// Licensed to the Apache Software Foundation (ASF) under one // Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file // or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information // distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file // regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the // to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance // "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at // with the License. You may obtain a copy of the License at
// //
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, // Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an // software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the // KIND, either express or implied. See the License for the
// specific language governing permissions and limitations // specific language governing permissions and limitations
// under the License. // under the License.
package com.cloud.acl; package org.apache.cloudstack.acl;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
import javax.ejb.Local; import javax.ejb.Local;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import org.apache.log4j.Logger; import com.cloud.acl.APIAccessChecker;
import org.apache.log4j.Logger;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.server.ManagementServer; import com.cloud.exception.PermissionDeniedException;
import com.cloud.user.Account; import com.cloud.server.ManagementServer;
import com.cloud.user.AccountManager; import com.cloud.user.Account;
import com.cloud.user.User; import com.cloud.user.AccountManager;
import com.cloud.utils.PropertiesUtil; import com.cloud.user.User;
import com.cloud.utils.component.AdapterBase; import com.cloud.utils.PropertiesUtil;
import com.cloud.utils.component.ComponentLocator; import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.component.Inject; import com.cloud.utils.component.ComponentLocator;
import com.cloud.utils.component.PluggableService; import com.cloud.utils.component.Inject;
import com.cloud.utils.component.PluggableService;
/*
* This is the default API access checker that grab's the user's account /*
* based on the account type, access is granted referring to commands in all *.properties files. * This is the default API access checker that grab's the user's account
*/ * based on the account type, access is granted referring to commands in all *.properties files.
*/
@Local(value=APIAccessChecker.class)
public class StaticRoleBasedAPIAccessChecker extends AdapterBase implements APIAccessChecker { @Local(value=APIAccessChecker.class)
public class StaticRoleBasedAPIAccessChecker extends AdapterBase implements APIAccessChecker {
protected static final Logger s_logger = Logger.getLogger(StaticRoleBasedAPIAccessChecker.class);
public static final short ADMIN_COMMAND = 1; protected static final Logger s_logger = Logger.getLogger(StaticRoleBasedAPIAccessChecker.class);
public static final short DOMAIN_ADMIN_COMMAND = 4; public static final short ADMIN_COMMAND = 1;
public static final short RESOURCE_DOMAIN_ADMIN_COMMAND = 2; public static final short DOMAIN_ADMIN_COMMAND = 4;
public static final short USER_COMMAND = 8; public static final short RESOURCE_DOMAIN_ADMIN_COMMAND = 2;
private static List<String> s_userCommands = null; public static final short USER_COMMAND = 8;
private static List<String> s_resellerCommands = null; // AKA domain-admin private static List<String> s_userCommands = null;
private static List<String> s_adminCommands = null; private static List<String> s_resellerCommands = null; // AKA domain-admin
private static List<String> s_resourceDomainAdminCommands = null; private static List<String> s_adminCommands = null;
private static List<String> s_allCommands = null; private static List<String> s_resourceDomainAdminCommands = null;
private Properties _apiCommands = null; private static List<String> s_allCommands = null;
private Properties _apiCommands = null;
protected @Inject AccountManager _accountMgr;
protected @Inject AccountManager _accountMgr;
protected StaticRoleBasedAPIAccessChecker() {
super(); protected StaticRoleBasedAPIAccessChecker() {
s_allCommands = new ArrayList<String>(); super();
s_userCommands = new ArrayList<String>(); s_allCommands = new ArrayList<String>();
s_resellerCommands = new ArrayList<String>(); s_userCommands = new ArrayList<String>();
s_adminCommands = new ArrayList<String>(); s_resellerCommands = new ArrayList<String>();
s_resourceDomainAdminCommands = new ArrayList<String>(); s_adminCommands = new ArrayList<String>();
} s_resourceDomainAdminCommands = new ArrayList<String>();
}
@Override
public boolean canAccessAPI(User user, String apiCommandName) @Override
throws PermissionDeniedException{ public boolean canAccessAPI(User user, String apiCommandName)
throws PermissionDeniedException{
boolean commandExists = s_allCommands.contains(apiCommandName);
boolean commandExists = s_allCommands.contains(apiCommandName);
if(commandExists && user != null){
Long accountId = user.getAccountId(); if(commandExists && user != null){
Account userAccount = _accountMgr.getAccount(accountId); Long accountId = user.getAccountId();
short accountType = userAccount.getType(); Account userAccount = _accountMgr.getAccount(accountId);
return isCommandAvailableForAccount(accountType, apiCommandName); short accountType = userAccount.getType();
} return isCommandAvailableForAccount(accountType, apiCommandName);
}
return commandExists;
} return commandExists;
}
@Override
public Properties getApiCommands() { @Override
return _apiCommands; public Properties getApiCommands() {
} return _apiCommands;
}
private static boolean isCommandAvailableForAccount(short accountType, String commandName) {
boolean isCommandAvailable = false; private static boolean isCommandAvailableForAccount(short accountType, String commandName) {
switch (accountType) { boolean isCommandAvailable = false;
case Account.ACCOUNT_TYPE_ADMIN: switch (accountType) {
isCommandAvailable = s_adminCommands.contains(commandName); case Account.ACCOUNT_TYPE_ADMIN:
break; isCommandAvailable = s_adminCommands.contains(commandName);
case Account.ACCOUNT_TYPE_DOMAIN_ADMIN: break;
isCommandAvailable = s_resellerCommands.contains(commandName); case Account.ACCOUNT_TYPE_DOMAIN_ADMIN:
break; isCommandAvailable = s_resellerCommands.contains(commandName);
case Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN: break;
isCommandAvailable = s_resourceDomainAdminCommands.contains(commandName); case Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN:
break; isCommandAvailable = s_resourceDomainAdminCommands.contains(commandName);
case Account.ACCOUNT_TYPE_NORMAL: break;
isCommandAvailable = s_userCommands.contains(commandName); case Account.ACCOUNT_TYPE_NORMAL:
break; isCommandAvailable = s_userCommands.contains(commandName);
} break;
return isCommandAvailable; }
} return isCommandAvailable;
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { @Override
super.configure(name, params); 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); // Read command properties files to build the static map per role.
List<PluggableService> services = locator.getAllPluggableServices(); ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
services.add((PluggableService) ComponentLocator.getComponent(ManagementServer.Name)); List<PluggableService> services = locator.getAllPluggableServices();
services.add((PluggableService) ComponentLocator.getComponent(ManagementServer.Name));
List<String> configFiles = new ArrayList<String>();
for (PluggableService service : services) { List<String> configFiles = new ArrayList<String>();
configFiles.addAll(Arrays.asList(service.getPropertiesFiles())); for (PluggableService service : services) {
} configFiles.addAll(Arrays.asList(service.getPropertiesFiles()));
}
processConfigFiles(configFiles);
return true; processConfigFiles(configFiles);
} return true;
}
private void processConfigFiles(List<String> configFiles) {
try { private void processConfigFiles(List<String> configFiles) {
if (_apiCommands == null) if (_apiCommands == null)
_apiCommands = new Properties(); _apiCommands = new Properties();
Properties preProcessedCommands = new Properties(); Properties preProcessedCommands = new Properties();
for (String configFile : configFiles) {
File commandsFile = PropertiesUtil.findConfigFile(configFile); for (String configFile : configFiles) {
if (commandsFile != null) { File commandsFile = PropertiesUtil.findConfigFile(configFile);
try { if (commandsFile != null) {
preProcessedCommands.load(new FileInputStream(commandsFile)); try {
} catch (FileNotFoundException fnfex) { preProcessedCommands.load(new FileInputStream(commandsFile));
// in case of a file within a jar in classpath, try to open stream using url } catch (FileNotFoundException fnfex) {
InputStream stream = PropertiesUtil.openStreamFromURL(configFile); // in case of a file within a jar in classpath, try to open stream using url
if (stream != null) { InputStream stream = PropertiesUtil.openStreamFromURL(configFile);
preProcessedCommands.load(stream); if (stream != null) {
} else { try {
s_logger.error("Unable to find properites file", fnfex); preProcessedCommands.load(stream);
} } catch (IOException e) {
} s_logger.error("IO Exception, unable to find properties file:", fnfex);
} }
} } else {
for (Object key : preProcessedCommands.keySet()) { s_logger.error("Unable to find properites file", fnfex);
String preProcessedCommand = preProcessedCommands.getProperty((String) key); }
String[] commandParts = preProcessedCommand.split(";"); } catch (IOException ioe) {
_apiCommands.setProperty(key.toString(), commandParts[0]); s_logger.error("IO Exception loading properties file", ioe);
}
if (commandParts.length > 1) { }
try { }
short cmdPermissions = Short.parseShort(commandParts[1]);
if ((cmdPermissions & ADMIN_COMMAND) != 0) { for (Object key : preProcessedCommands.keySet()) {
s_adminCommands.add((String) key); String preProcessedCommand = preProcessedCommands.getProperty((String) key);
} String[] commandParts = preProcessedCommand.split(";");
if ((cmdPermissions & RESOURCE_DOMAIN_ADMIN_COMMAND) != 0) { _apiCommands.setProperty(key.toString(), commandParts[0]);
s_resourceDomainAdminCommands.add((String) key);
} if (commandParts.length > 1) {
if ((cmdPermissions & DOMAIN_ADMIN_COMMAND) != 0) { try {
s_resellerCommands.add((String) key); short cmdPermissions = Short.parseShort(commandParts[1]);
} if ((cmdPermissions & ADMIN_COMMAND) != 0) {
if ((cmdPermissions & USER_COMMAND) != 0) { s_adminCommands.add((String) key);
s_userCommands.add((String) key); }
} if ((cmdPermissions & RESOURCE_DOMAIN_ADMIN_COMMAND) != 0) {
s_allCommands.addAll(s_adminCommands); s_resourceDomainAdminCommands.add((String) key);
s_allCommands.addAll(s_resourceDomainAdminCommands); }
s_allCommands.addAll(s_userCommands); if ((cmdPermissions & DOMAIN_ADMIN_COMMAND) != 0) {
s_allCommands.addAll(s_resellerCommands); s_resellerCommands.add((String) key);
} catch (NumberFormatException nfe) { }
s_logger.info("Malformed command.properties permissions value, key = " + key + ", value = " + preProcessedCommand); if ((cmdPermissions & USER_COMMAND) != 0) {
} s_userCommands.add((String) key);
} }
} s_allCommands.addAll(s_adminCommands);
} catch (FileNotFoundException fnfex) { s_allCommands.addAll(s_resourceDomainAdminCommands);
s_logger.error("Unable to find properties file", fnfex); s_allCommands.addAll(s_userCommands);
} catch (IOException ioex) { s_allCommands.addAll(s_resellerCommands);
s_logger.error("IO Exception loading properties file", ioex); } catch (NumberFormatException nfe) {
} s_logger.info("Malformed command.properties permissions value, key = " + key + ", value = " + preProcessedCommand);
} }
} }
}
}
}

View File

@ -32,6 +32,7 @@
<testSourceDirectory>test</testSourceDirectory> <testSourceDirectory>test</testSourceDirectory>
</build> </build>
<modules> <modules>
<module>acl/static-role-based</module>
<module>deployment-planners/user-concentrated-pod</module> <module>deployment-planners/user-concentrated-pod</module>
<module>deployment-planners/user-dispersing</module> <module>deployment-planners/user-dispersing</module>
<module>host-allocators/random</module> <module>host-allocators/random</module>