From baca5e4a44147df375b50e10eed10f99487cf017 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 28 May 2010 00:27:29 +0200 Subject: [PATCH 01/64] feature #203: Initial version for the Authorization Manager and Drivers --- include/AuthManager.h | 346 +++++++++++++++++++++++++++++++++ include/AuthManagerDriver.h | 92 +++++++++ src/authm/AuthManager.cc | 319 ++++++++++++++++++++++++++++++ src/authm/AuthManagerDriver.cc | 124 ++++++++++++ src/authm/SConstruct | 30 +++ 5 files changed, 911 insertions(+) create mode 100644 include/AuthManager.h create mode 100644 include/AuthManagerDriver.h create mode 100644 src/authm/AuthManager.cc create mode 100644 src/authm/AuthManagerDriver.cc create mode 100644 src/authm/SConstruct diff --git a/include/AuthManager.h b/include/AuthManager.h new file mode 100644 index 0000000000..ca1d01936f --- /dev/null +++ b/include/AuthManager.h @@ -0,0 +1,346 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed 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. */ +/* -------------------------------------------------------------------------- */ + +#ifndef AUTH_MANAGER_H_ +#define AUTH_MANAGER_H_ + +#include "MadManager.h" +#include "ActionManager.h" + +#include "AuthManagerDriver.h" + +using namespace std; + +//Forward definition of the AuthManager +class AuthManager; + +/** + * The AuthRequest class is used to pass an Authorization or Authentication + * request to the AuthManager. The result of the request will be stored + * in the result and message attributes of this class. + */ +class AuthRequest +{ +public: + AuthRequest(ActionManager *_am, int _uid, const string& _auth_driver): + result(false), + am(_am), + auth_driver(_auth_driver), + uid(_uid) + {}; + + ~AuthRequest(){}; + + /** + * Authorization Request Type + */ + enum Operation + { + CREATION, /** Authorization to create an object (host, vm, net, image)*/ + DELETION, /** Authorization to delete an object */ + USAGE, /** Authorization to use an object */ + MANAGE /** Authorization to manage an object */ + }; + + /** + * OpenNebula objects to perform an Operation + */ + enum Object + { + VM, + HOST, + NET, + IMAGE + }; + + /** + * Sets the challenge to authenticate an user + * @param challenge a driver specific authentication challenge + */ + void set_challenge(const string &ch) + { + challenge = ch; + } + + /** + * Adds a new authorization item to this requests + * @param op the operation to be authorized + * @param ob the object over which the operation will be performed + * @param oid id of the object + */ + void add_auth(Operation op, Object ob, int oid) + { + ostringstream oss; + + switch (op) + { + case CREATION: oss << "CREATION:" ; break; + case DELETION: oss << "DELETION:" ; break; + case USAGE: oss << "USAGE:" ; break; + case MANAGE: oss << "MANAGE:" ; break; + } + + switch (ob) + { + case VM: oss << "VM:" ; break; + case HOST: oss << "HOST:" ; break; + case NET: oss << "NET:" ; break; + case IMAGE: oss << "IMAGE:" ; break; + } + + oss << oid; + + auths.push_back(oss.str()); + }; + + /** + * Gets the authorization requests in a single string + * @return a space separated list of auth requests. + */ + string get_auths() + { + ostringstream oss; + + for (unsigned int i=0; itrigger(ActionListener::ACTION_FINALIZE,0); + }; + + /** + * The result of the request, true if authorized or authenticated + */ + bool result; + + /** + * Error message for negative results + */ + string message; + +private: + + friend class AuthManager; + + /** + * The ActionManager that will be notify when the request is ready. + */ + ActionManager * am; + + /** + * The name of the Authorization driver to use with this request + */ + string auth_driver; + + /** + * The user id for this request + */ + int uid; + + /** + * Authentication challenge + */ + string challenge; + + /** + * A list of authorization requests + */ + vector auths; +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +extern "C" void * authm_action_loop(void *arg); + +class AuthManager : public MadManager, public ActionListener +{ +public: + + AuthManager( + vector& _mads): + MadManager(_mads) + { + am.addListener(this); + }; + + ~AuthManager(){}; + + enum Actions + { + AUTHENTICATE, + AUTHORIZE, + FINALIZE + }; + + /** + * Triggers specific actions to the Auth Manager. This function + * wraps the ActionManager trigger function. + * @param action the Auth Manager action + * @param request an auth request + */ + void trigger( + Actions action, + AuthRequest * request); + + /** + * This functions starts the associated listener thread, and creates a + * new thread for the Information Manager. This thread will wait in + * an action loop till it receives ACTION_FINALIZE. + * @return 0 on success. + */ + int start(); + + /** + * Loads Virtual Machine Manager Mads defined in configuration file + * @param uid of the user executing the driver. When uid is 0 the nebula + * identity will be used. Otherwise the Mad will be loaded through the + * sudo application. + */ + void load_mads(int uid); + + /** + * Gets the thread identification. + * @return pthread_t for the manager thread (that in the action loop). + */ + pthread_t get_thread_id() const + { + return authm_thread; + }; + + /** + * Notify the result of an auth request + */ + void notify(int auth_id, bool result, const string& message); + +private: + /** + * Thread id for the Transfer Manager + */ + pthread_t authm_thread; + + /** + * Action engine for the Manager + */ + ActionManager am; + + /** + * Action engine for the Manager + */ + map auth_requests; + + /** + * Mutex to access the auth_requests + */ + pthread_mutex_t mutex; + + /** + * Returns a pointer to a Auth Manager driver. + * @param name of an attribute of the driver (e.g. its type) + * @param value of the attribute + * @return the Auth driver with attribute name equal to value + * or 0 in not found + */ + const AuthManagerDriver * get( + const string& name, + const string& value) + { + return static_cast + (MadManager::get(0,name,value)); + }; + + /** + * Returns a pointer to a Auth Manager driver. The driver is + * searched by its name. + * @param name the name of the driver + * @return the TM driver owned by uid with attribute name equal to value + * or 0 in not found + */ + const AuthManagerDriver * get(const string& name) + { + string _name("NAME"); + + return static_cast + (MadManager::get(0,_name,name)); + }; + + /** + * Function to execute the Manager action loop method within a new pthread + * (requires C linkage) + */ + friend void * authm_action_loop(void *arg); + + /** + * The action function executed when an action is triggered. + * @param action the name of the action + * @param arg arguments for the action function + */ + void do_action( + const string & action, + void * arg); + + /** + * This function authenticates a user + */ + void authenticate_action(AuthRequest * ar); + + /** + * This function authorizes a user request + */ + void authorize_action(AuthRequest * ar); + + /** + * Function to lock the pool + */ + void lock() + { + pthread_mutex_lock(&mutex); + }; + + /** + * Function to unlock the pool + */ + void unlock() + { + pthread_mutex_unlock(&mutex); + }; + + /** + * Add a new request to the Request map + * @param ar pointer to the AuthRequest + * @return the id for the request + */ + int add_request(AuthRequest *ar); + + /** + * Gets request from the Request map + * @param id for the request + * @return pointer to the AuthRequest + */ + AuthRequest * get_request(int id); +}; + +#endif /*AUTH_MANAGER_H*/ + diff --git a/include/AuthManagerDriver.h b/include/AuthManagerDriver.h new file mode 100644 index 0000000000..69764d06ea --- /dev/null +++ b/include/AuthManagerDriver.h @@ -0,0 +1,92 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed 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. */ +/* -------------------------------------------------------------------------- */ + +#ifndef AUTH_MANAGER_DRIVER_H_ +#define AUTH_MANAGER_DRIVER_H_ + +#include +#include +#include + +#include "Mad.h" + +using namespace std; + +//Forward definition of the AuthManager Class +class AuthManager; + +/** + * AuthManagerDriver provides a base class to implement TM + * Drivers. This class implements the protocol and recover functions + * from the Mad interface. + */ +class AuthManagerDriver : public Mad +{ +public: + + AuthManagerDriver( + int userid, + const map& attrs, + bool sudo, + AuthManager * _authm): + Mad(userid,attrs,sudo), authm(_authm){}; + + virtual ~AuthManagerDriver(){}; + + /** + * Implements the VM Manager driver protocol. + * @param message the string read from the driver + */ + void protocol( + string& message); + + /** + * Re-starts the driver + */ + void recover(); + +private: + friend class AuthManager; + + /** + * The AuthManager to notify results. + */ + AuthManager * authm; + + /** + * Sends an authorization request to the MAD: + * "AUTHORIZE OPERATION_ID USER_ID REQUEST1 REQUEST2..." + * @param oid an id to identify the request. + * @param uid the user id. + * @param requests space separated list of requests in the form OP:OBJ:ID + */ + void authorize(int oid, int uid, const string& requests) const; + + /** + * Sends an authorization request to the MAD: + * "AUTHENTICATE OPERATION_ID USER_ID CHALLENGE" + * @param oid an id to identify the request. + * @param uid the user id. + * @param challenge to authenticate the user + */ + void authenticate(int oid, int uid, const string& ch) const; +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +#endif /*AUTH_MANAGER_DRIVER_H_*/ + diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc new file mode 100644 index 0000000000..d6850f6d75 --- /dev/null +++ b/src/authm/AuthManager.cc @@ -0,0 +1,319 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed 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. */ +/* -------------------------------------------------------------------------- */ + +#include "AuthManager.h" +#include "NebulaLog.h" + +#include "Nebula.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +extern "C" void * authm_action_loop(void *arg) +{ + AuthManager * authm; + + if ( arg == 0 ) + { + return 0; + } + + authm = static_cast(arg); + + NebulaLog::log("AuM",Log::INFO,"Authorization Manager started."); + + authm->am.loop(0,0); + + NebulaLog::log("TrM",Log::INFO,"Authorization Manager stopped."); + + return 0; +} + +/* -------------------------------------------------------------------------- */ + +int AuthManager::start() +{ + int rc; + pthread_attr_t pattr; + + rc = MadManager::start(); + + if ( rc != 0 ) + { + return -1; + } + + NebulaLog::log("AuM",Log::INFO,"Starting Auth Manager..."); + + pthread_attr_init (&pattr); + pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE); + + rc = pthread_create(&authm_thread,&pattr,authm_action_loop,(void *) this); + + return rc; +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthManager::trigger(Actions action, AuthRequest * request) +{ + string aname; + + switch (action) + { + case AUTHENTICATE: + aname = "AUTHENTICATE"; + break; + + case AUTHORIZE: + aname = "AUTHORIZE"; + break; + + case FINALIZE: + aname = ACTION_FINALIZE; + break; + + default: + return; + } + + am.trigger(aname,request); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthManager::do_action(const string &action, void * arg) +{ + AuthRequest * request; + + if (arg == 0) + { + return; + } + + request = static_cast(arg); + + if (action == "AUTHENTICATE") + { + authenticate_action(request); + } + else if (action == "AUTHORIZE") + { + authorize_action(request); + } + else if (action == ACTION_FINALIZE) + { + NebulaLog::log("AuM",Log::INFO,"Stopping Authorization Manager..."); + + MadManager::stop(); + } + else + { + ostringstream oss; + oss << "Unknown action name: " << action; + + NebulaLog::log("AuM", Log::ERROR, oss); + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthManager::authenticate_action(AuthRequest * ar) +{ + const AuthManagerDriver * authm_md; + int id; + + // ------------------------------------------------------------------------ + // Get the driver + // ------------------------------------------------------------------------ + + authm_md = get(ar->auth_driver); + + if (authm_md == 0) + { + goto error_driver; + } + + // ------------------------------------------------------------------------ + // Queue the request + // ------------------------------------------------------------------------ + + id = add_request(ar); + + // ------------------------------------------------------------------------ + // Make the request to the driver + // ------------------------------------------------------------------------ + + authm_md->authenticate(id,ar->uid,ar->challenge); + + return; + +error_driver: + ar->result = false; + ar->message = "Could not find Authorization driver"; + ar->notify(); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthManager::authorize_action(AuthRequest * ar) +{ + const AuthManagerDriver * authm_md; + int id; + string auths; + + // ------------------------------------------------------------------------ + // Get the driver + // ------------------------------------------------------------------------ + + authm_md = get(ar->auth_driver); + + if (authm_md == 0) + { + goto error_driver; + } + + // ------------------------------------------------------------------------ + // Queue the request + // ------------------------------------------------------------------------ + + id = add_request(ar); + + // ------------------------------------------------------------------------ + // Make the request to the driver + // ------------------------------------------------------------------------ + + auths = ar->get_auths(); + + authm_md->authorize(id, ar->uid, auths); + + return; + +error_driver: + ar->result = false; + ar->message = "Could not find Authorization driver"; + ar->notify(); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AuthManager::add_request(AuthRequest *ar) +{ + static int auth_id = 0; + int id; + + lock(); + + id = auth_id++; + + auth_requests.insert(auth_requests.end(),make_pair(auth_id,ar)); + + unlock(); + + return id; +} + +/* -------------------------------------------------------------------------- */ + +AuthRequest * AuthManager::get_request(int id) +{ + AuthRequest * ar = 0; + map::iterator it; + + lock(); + + it=auth_requests.find(id); + + if ( it != auth_requests.end()) + { + ar = it->second; + } + + auth_requests.erase(it); + + unlock(); + + return ar; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthManager::notify(int auth_id, bool result, const string& message) +{ + + AuthRequest * ar; + + ar = get_request(auth_id); + + if ( ar == 0 ) + { + return; + } + + ar->result = result; + ar->message= message; + + ar->notify(); +} + +/* ************************************************************************** */ +/* MAD Loading */ +/* ************************************************************************** */ + +void AuthManager::load_mads(int uid) +{ + unsigned int i; + ostringstream oss; + const VectorAttribute * vattr; + int rc; + string name; + AuthManagerDriver * authm_driver = 0; + + oss << "Loading Authorization Manager drivers."; + + NebulaLog::log("AuM",Log::INFO,oss); + + for(i=0,oss.str("");i(mad_conf[i]); + + name = vattr->vector_value("NAME"); + + oss << "\tLoading driver: " << name; + NebulaLog::log("AuM", Log::INFO, oss); + + authm_driver = new AuthManagerDriver(uid,vattr->value(),(uid!=0),this); + + if ( authm_driver == 0 ) + continue; + + rc = add(authm_driver); + + if ( rc == 0 ) + { + oss.str(""); + oss << "\tDriver " << name << " loaded."; + + NebulaLog::log("AuM",Log::INFO,oss); + } + } +} diff --git a/src/authm/AuthManagerDriver.cc b/src/authm/AuthManagerDriver.cc new file mode 100644 index 0000000000..a712b97776 --- /dev/null +++ b/src/authm/AuthManagerDriver.cc @@ -0,0 +1,124 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed 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. */ +/* -------------------------------------------------------------------------- */ + +#include "AuthManagerDriver.h" +#include "AuthManager.h" +#include "NebulaLog.h" + +#include "Nebula.h" +#include + +/* ************************************************************************** */ +/* Driver ASCII Protocol Implementation */ +/* ************************************************************************** */ + +void AuthManagerDriver::authorize(int oid, int uid, const string& reqs) const +{ + ostringstream os; + + os << "AUTHORIZE " << oid << " " << uid << " " << reqs << endl; + + write(os); +} + +void AuthManagerDriver::authenticate(int oid, int uid, const string& ch) const +{ + ostringstream os; + + os << "AUTHENTICATE " << oid << " " << uid << " " << ch << endl; + + write(os); +} + +/* ************************************************************************** */ +/* MAD Interface */ +/* ************************************************************************** */ + +void AuthManagerDriver::protocol( + string& message) +{ + istringstream is(message); + ostringstream os; + + string action; + string result; + string info=""; + + int id; + + os << "Message received: " << message; + NebulaLog::log("AuM", Log::DEBUG, os); + + // Parse the driver message + if ( is.good() ) + is >> action >> ws; + else + return; + + if ( is.good() ) + is >> result >> ws; + else + return; + + if ( is.good() ) + { + is >> id >> ws; + + if ( is.fail() ) + { + if ( action == "LOG" ) + { + string info; + + is.clear(); + getline(is,info); + NebulaLog::log("AuM",Log::INFO, info.c_str()); + } + + return; + } + } + else + return; + + if (action == "LOG") + { + string info; + + getline(is,info); + NebulaLog::log("AuM",Log::INFO,info.c_str()); + } + else if (result == "SUCCESS") + { + authm->notify(id,true,info); + } + else + { + getline(is,info); + + authm->notify(id,false,info); + } + + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthManagerDriver::recover() +{ + NebulaLog::log("AuM",Log::INFO,"Recovering Authorization drivers"); +} diff --git a/src/authm/SConstruct b/src/authm/SConstruct new file mode 100644 index 0000000000..fd2c542edb --- /dev/null +++ b/src/authm/SConstruct @@ -0,0 +1,30 @@ +# SConstruct for src/vm + +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +Import('env') + +lib_name='nebula_authm' + +# Sources to generate the library +source_files=[ + 'AuthManager.cc', + 'AuthManagerDriver.cc' +] + +# Build library +env.StaticLibrary(lib_name, source_files) From 4cc03e0e978d973f22eef95279a58aa1a2eaf1aa Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 28 May 2010 00:29:23 +0200 Subject: [PATCH 02/64] feature #203: Added the Auth Manager to the SConstruct script --- SConstruct | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SConstruct b/SConstruct index 917346a2fe..f4cce1fba9 100644 --- a/SConstruct +++ b/SConstruct @@ -56,6 +56,7 @@ main_env.Append(LIBPATH=[ cwd+'/src/vnm', cwd+'/src/hm', cwd+'/src/um', + cwd+'/src/authm', ]) # Compile flags @@ -159,6 +160,7 @@ build_scripts=[ 'src/vnm/SConstruct', 'src/hm/SConstruct', 'src/um/SConstruct', + 'src/authm/SConstruct', ] for script in build_scripts: From 42161d1ac50df0cd6901f76cbd2ac777dbf9ff75 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 29 May 2010 03:42:30 +0200 Subject: [PATCH 03/64] feature #203: Syncrhonization functions for the AuthRequest. Added basic tests for the Authentication Manager --- include/AuthManager.h | 65 +++++++++-- src/authm/AuthManager.cc | 23 ++-- src/authm/AuthManagerDriver.cc | 9 +- src/authm/test/.sconsign.dblite | Bin 0 -> 10683 bytes src/authm/test/AuthManagerTest.cc | 179 ++++++++++++++++++++++++++++++ src/authm/test/SConstruct | 87 +++++++++++++++ src/authm/test/dummy | 44 ++++++++ 7 files changed, 380 insertions(+), 27 deletions(-) create mode 100644 src/authm/test/.sconsign.dblite create mode 100644 src/authm/test/AuthManagerTest.cc create mode 100644 src/authm/test/SConstruct create mode 100755 src/authm/test/dummy diff --git a/include/AuthManager.h b/include/AuthManager.h index ca1d01936f..9247894bd2 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -32,15 +32,17 @@ class AuthManager; * request to the AuthManager. The result of the request will be stored * in the result and message attributes of this class. */ -class AuthRequest +class AuthRequest : public ActionListener { public: - AuthRequest(ActionManager *_am, int _uid, const string& _auth_driver): + AuthRequest(int _uid, const string& _auth_driver): result(false), - am(_am), + timeout(false), auth_driver(_auth_driver), uid(_uid) - {}; + { + am.addListener(this); + }; ~AuthRequest(){}; @@ -127,7 +129,15 @@ public: */ void notify() { - am->trigger(ActionListener::ACTION_FINALIZE,0); + am.trigger(ActionListener::ACTION_FINALIZE,0); + }; + + /** + * Wait for the AuthRequest to be completed + */ + void wait(time_t timeout=30) + { + am.loop(timeout,0); }; /** @@ -140,6 +150,16 @@ public: */ string message; + /** + * Time out + */ + bool timeout; + + /** + * Identification of this request + */ + int id; + private: friend class AuthManager; @@ -147,7 +167,7 @@ private: /** * The ActionManager that will be notify when the request is ready. */ - ActionManager * am; + ActionManager am; /** * The name of the Authorization driver to use with this request @@ -168,6 +188,20 @@ private: * A list of authorization requests */ vector auths; + + /** + * + */ + void do_action(const string &name, void *args) + { + if (name == ACTION_TIMER) + { + result = false; + timeout = true; + + am.trigger(ActionListener::ACTION_FINALIZE,0); + } + } }; /* -------------------------------------------------------------------------- */ @@ -184,6 +218,7 @@ public: MadManager(_mads) { am.addListener(this); + pthread_mutex_init(&mutex,0); }; ~AuthManager(){}; @@ -207,7 +242,7 @@ public: /** * This functions starts the associated listener thread, and creates a - * new thread for the Information Manager. This thread will wait in + * new thread for the AuthManager. This thread will wait in * an action loop till it receives ACTION_FINALIZE. * @return 0 on success. */ @@ -233,7 +268,21 @@ public: /** * Notify the result of an auth request */ - void notify(int auth_id, bool result, const string& message); + void notify_request(int auth_id, bool result, const string& message); + + /** + * Discards a pending request. Call this before freeing not notified or + * timeout requests. + */ + void discard_request(int auth_id) + { + lock(); + + auth_requests.erase(auth_id); + + unlock(); + } + private: /** diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index d6850f6d75..54c25a444f 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -37,7 +37,7 @@ extern "C" void * authm_action_loop(void *arg) authm->am.loop(0,0); - NebulaLog::log("TrM",Log::INFO,"Authorization Manager stopped."); + NebulaLog::log("AuM",Log::INFO,"Authorization Manager stopped."); return 0; } @@ -101,7 +101,7 @@ void AuthManager::do_action(const string &action, void * arg) { AuthRequest * request; - if (arg == 0) + if ( arg == 0 && action != ACTION_FINALIZE ) { return; } @@ -137,7 +137,6 @@ void AuthManager::do_action(const string &action, void * arg) void AuthManager::authenticate_action(AuthRequest * ar) { const AuthManagerDriver * authm_md; - int id; // ------------------------------------------------------------------------ // Get the driver @@ -154,13 +153,13 @@ void AuthManager::authenticate_action(AuthRequest * ar) // Queue the request // ------------------------------------------------------------------------ - id = add_request(ar); + ar->id = add_request(ar); // ------------------------------------------------------------------------ // Make the request to the driver // ------------------------------------------------------------------------ - authm_md->authenticate(id,ar->uid,ar->challenge); + authm_md->authenticate(ar->id,ar->uid,ar->challenge); return; @@ -176,7 +175,6 @@ error_driver: void AuthManager::authorize_action(AuthRequest * ar) { const AuthManagerDriver * authm_md; - int id; string auths; // ------------------------------------------------------------------------ @@ -194,7 +192,7 @@ void AuthManager::authorize_action(AuthRequest * ar) // Queue the request // ------------------------------------------------------------------------ - id = add_request(ar); + ar->id = add_request(ar); // ------------------------------------------------------------------------ // Make the request to the driver @@ -202,7 +200,7 @@ void AuthManager::authorize_action(AuthRequest * ar) auths = ar->get_auths(); - authm_md->authorize(id, ar->uid, auths); + authm_md->authorize(ar->id, ar->uid, auths); return; @@ -224,7 +222,7 @@ int AuthManager::add_request(AuthRequest *ar) id = auth_id++; - auth_requests.insert(auth_requests.end(),make_pair(auth_id,ar)); + auth_requests.insert(auth_requests.end(),make_pair(id,ar)); unlock(); @@ -237,6 +235,7 @@ AuthRequest * AuthManager::get_request(int id) { AuthRequest * ar = 0; map::iterator it; + ostringstream oss; lock(); @@ -245,9 +244,9 @@ AuthRequest * AuthManager::get_request(int id) if ( it != auth_requests.end()) { ar = it->second; - } - auth_requests.erase(it); + auth_requests.erase(it); + } unlock(); @@ -257,7 +256,7 @@ AuthRequest * AuthManager::get_request(int id) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AuthManager::notify(int auth_id, bool result, const string& message) +void AuthManager::notify_request(int auth_id,bool result,const string& message) { AuthRequest * ar; diff --git a/src/authm/AuthManagerDriver.cc b/src/authm/AuthManagerDriver.cc index a712b97776..355788bddc 100644 --- a/src/authm/AuthManagerDriver.cc +++ b/src/authm/AuthManagerDriver.cc @@ -81,8 +81,6 @@ void AuthManagerDriver::protocol( { if ( action == "LOG" ) { - string info; - is.clear(); getline(is,info); NebulaLog::log("AuM",Log::INFO, info.c_str()); @@ -96,20 +94,17 @@ void AuthManagerDriver::protocol( if (action == "LOG") { - string info; - getline(is,info); NebulaLog::log("AuM",Log::INFO,info.c_str()); } else if (result == "SUCCESS") { - authm->notify(id,true,info); + authm->notify_request(id,true,info); } else { getline(is,info); - - authm->notify(id,false,info); + authm->notify_request(id,false,info); } return; diff --git a/src/authm/test/.sconsign.dblite b/src/authm/test/.sconsign.dblite new file mode 100644 index 0000000000000000000000000000000000000000..d9234a98a4ef1abf2fa1390cb7b9f749b3940572 GIT binary patch literal 10683 zcmdT~S(6*p753nLyo@&%V-f~1Mqo4QePJ_W*&asrjKNl$RhaI+x2J_?q&Zr^Hok$d z@B5Mff$V`KWG8zF*&u5rR3%TmLpkZemgsX}a6$mU)d@tM1bVtS`d`XSr9Inx3rql_s28 zhSTt6LUH*>15Pi)86l^;BDqwVXu!rYoarp}T))W~ZyI-2wE^%#1I{YL$M84e&)`=+ zUWT(4_A+! zvcBbo1Gx~hZVQ78av|ngF2n@8FvPUFNI+sHZ4Eb#hnO+jLQIm2d6GD*c>H%)jx<|! zikyZ7r#u5G;UqIqPT-`Q7EuDKdx37tvJ_BNp+q8{t;rZL161Th``er}2D;#6a84%Y zWF?&JnA0Vk)1`vb3eM?3tJ36*E)$F{XK+QrXgh-|XEM?)V%ff;nzHRvNz;gH=$>Xs zrWshate7y2jQ+T^&1eUMf?%|hGup)&?M@ht#Eh=uj7B-5)(|Xq&YG>1@~uj~NslyP zNvR%hbfTj6w{S<~L7t;sZgodR>8_*(nyu&(E=AyDF-g8_+cxzHR+vpQ74=%*e5xqi zJx~l{Ybp&~ z%v>&7|6JGJj(ZfXfnmRi{aqUSkCv_Xw!9-)>4vSFrs?^nCE2c|DMa&BUot(^3Zh!F zkge@&+pKz&tyROVW|(sNXyH0|c)?z=QZCUAIl#uoBbYgXB&*c*6`4}12bxEOaJ{;v z&8auxa@J=!?W|X@F~%4cg{1Cbus4y^F$Uv_q+asDf5mDU)Y36-l0|F}*H9}JNb4fkGCAE}DsuN4KRlclIk0_Z&<3UN{S{C;<`{qMsI9tGrF0^9G)>_Om@x~Q$s(zrzvW2%IQAt-MsKqnJ^w^s#(meqiIBe(Y3>w)XqFIJkc8IvG z47X>7h$Ce<8gf1zL&UK%+>s6u$4?j{SQ$RiIYiu9hP%X~ERL4rlVUmUW^hj~jMm{^ z2KVLoM}^V-hIpq$H8kDFPR#XGS1~jrur%WNhGE+}{`KMhaC(dkV1S~Vdw{`%xe#yZ zp%Cxm;SkwBA|R3Qqv7%K5Q#k&BA3Snm;MsP*~t|d^MoMuB!j1tjCq>DGfBoI6(6XQ zODzj`A^e~BhlXU}R;&^Qw1qen!phR&xSv=!v32WQb$)MeZ08hxpcERcz z3@})et@O2JSeLDIVIQP+0p{mvJT*Hj9TWD9w9+@8Q0ZruVN++Na|1zC*PT_%>K z7`g!$#7mVcK6=l(OzvHn3voRzVz4#49@}tab=GF|j8BO}H5hWUtCTO}=<2LYiZahD zQFg|99TA%GPLoe3gX9uU2Z}S=E1+MSnyOVZaM1+>Q=O4s!J+}C158XUnp5Z%YRV-U z9DIh1^xhe{*!oOmfW&izGJ0o(?qCKe@(Rt~VJ2%685n$qY%H3QO1lh~ggk{ir1H`- zT$UEl<<6jIx!7S?t}pwl=ewq92a=~)n8qe0=p4coGcCF8W%MUR%?vZxk;~eTg`(J= zI~>ZLNFnkgcl{sx_3pT4MvBpXEk5Te2BW#KP<$W9#}M&CM3M=LpKl^_Lzl62lZZud zE>UQpYZk?szZi}btFI4W-3g4S9V`_8kiqcL*&(_wVP{Gu6cJ*hWEeQs1ga+6l)xBb zl%8LjV2m@^$9eEEHsR5Ah`kyyqVZMmxQ4-gj`5ca2GNL)ql7L6mJ#^A?s{%uNrCK| z2Jvmnz;1J-_}72%5m;c97&si`ty;>%*KithJqg=tQ`arczxYpuSbl?Cl)+zU~nVH_;co76xlQk%~W;I#6>ktlwlbSUrX1d zKy_UTHzCHNN$xdMXAw7kq3m3~dGF}>p23T2gPTjk`$u+_2E~Qldxu933~oNK&)Hwt zg%1wx9o?~WbboxAKNAn(=FTnuP#JC!{9FdOUP=;-Eq9POG6GH}-87gvXr-~ddE=W~m67yKL zRD&q8t6?95b`|Q0f`4RBCl(Jc|&n$VDG29VW|dKns7wGiEOs;Ru0 z4mqEWErIvc>6UQp1Wmo8497bcnD^6SZDx_HJH^u6#Qd5uYDCoVRAFhm7 zI$R}g=ROAa=R#}@9&lz`^#@(<`#zL&hI>(gRATmw< zqs}?Anyy^=+h-P!eZ-sEUi;(DxgUy@y5CPYgCF{?#E*W`xp3C^#71zQb8EZ5PxYo- z?NH3HH=gcIklQe+AJLxupK-1{nV@tyZO=OP$zVj2X3 zKF%xQ$Qbu=#5VXUgU@mV;cUbRU2THTAp$oD1j6STe1Ri~NjFC5I_-WD5%{z#5Wd9V z%N#)*G{p#AkKVq52>j4ZAiT!l^_d7=kE_0l2>iH8AbgF%*H1v`cDVEgBJjf{f$$9m z-<*li^)TpLh`j5W+HU;?0$&|+_Mu1zhdy~nRn6EOa2Wa2rqd!X_($|?9GMiuFLjI7yEx4Ld}K>Q*L7%1Ll=#1^D1=f9KDy`gV&qzB4B;#XdJn +#include +#include +#include +#include + +#include "AuthManager.h" +#include "Template.h" +#include "NebulaLog.h" + +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/* ************************************************************************* */ +/* ************************************************************************* */ + +class AuthManagerTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE (AuthManagerTest); + + CPPUNIT_TEST (load); + + CPPUNIT_TEST (timeout); + CPPUNIT_TEST (authenticate); + + CPPUNIT_TEST_SUITE_END (); + + +public: + AuthManagerTest(){}; + + ~AuthManagerTest(){}; + + + /* ********************************************************************* */ + /* ********************************************************************* */ + void setUp() + { + + ostringstream oss; + vector am_mads; + char *error = 0; + + MadManager::mad_manager_system_init(); + + oss << "AUTH_MAD = [ name=\"dummy\", executable=\"/" << getenv("PWD") + << "/dummy\"]" << endl; + + t = new Template(); + + t->parse(oss.str(),&error); + + t->get("AUTH_MAD", am_mads); + + am = new AuthManager(am_mads); + }; + + void tearDown() + { + delete am; + delete t; + }; + + void load() + { + int rc; + + rc = am->start(); + + CPPUNIT_ASSERT(rc==0); + + am->load_mads(0); + + am->trigger(AuthManager::FINALIZE,0); + + pthread_join(am->get_thread_id(),0); + + CPPUNIT_ASSERT(0==0); + } + + void timeout() + { + int rc; + AuthRequest ar(2,"dummy"); + + rc = am->start(); + + CPPUNIT_ASSERT(rc==0); + + am->load_mads(0); + + ar.set_challenge("the_secret"); + ar.wait(1); + + CPPUNIT_ASSERT(ar.result==false); + CPPUNIT_ASSERT(ar.timeout==true); + + am->discard_request(ar.id); + + am->trigger(AuthManager::FINALIZE,0); + + pthread_join(am->get_thread_id(),0); + + CPPUNIT_ASSERT(0==0); + } + + void authenticate() + { + int rc; + AuthRequest ar(2,"dummy"); + + rc = am->start(); + + CPPUNIT_ASSERT(rc==0); + + am->load_mads(0); + + ar.set_challenge("the_secret"); + + am->trigger(AuthManager::AUTHENTICATE,&ar); + ar.wait(300); + + CPPUNIT_ASSERT(ar.result==true); + + am->trigger(AuthManager::FINALIZE,0); + + pthread_join(am->get_thread_id(),0); + + CPPUNIT_ASSERT(0==0); + } + +private: + AuthManager * am; + + Template * t; +}; + + +/* ************************************************************************* */ +/* ************************************************************************* */ + +int main(int argc, char ** argv) +{ + CppUnit::TextUi::TestRunner runner; + + NebulaLog::init_log_system(NebulaLog::FILE, Log::DEBUG,"test.log"); + NebulaLog::log("Test", Log::INFO, "Test started"); + + runner.addTest(AuthManagerTest::suite()); + + runner.run(); + + NebulaLog::finalize_log_system(); + + return 0; +} diff --git a/src/authm/test/SConstruct b/src/authm/test/SConstruct new file mode 100644 index 0000000000..1d92845c73 --- /dev/null +++ b/src/authm/test/SConstruct @@ -0,0 +1,87 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2009, Distributed Systems Architecture Group, Universidad # +# Complutense de Madrid (dsa-research.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +import os +import sys +import shutil +sys.path.append("../../../share/scons") +from lex_bison import * + +# This is the absolute path where the project is located +cwd="../../../" + +# Environment that will be applied to each scons child +main_env=Environment() +main_env['ENV']['PATH']=os.environ['PATH'] + +# Add builders for flex and bison +add_lex(main_env) +add_bison(main_env) + +# Include dirs +main_env.Append(CPPPATH=[ + cwd + '/include', + '/usr/include/cppunit/' +]) + +# Library dirs +main_env.Append(LIBPATH=[ + cwd + '/src/common', + cwd + '/src/template', + cwd + '/src/mad', + cwd + '/src/nebula', + cwd + '/src/authm', + cwd + '/src/sql', + cwd + '/src/log', +]) + +main_env.Append(LIBS=[ + 'nebula_template', + 'nebula_common', + 'nebula_core', + 'nebula_mad', + 'nebula_authm', + 'nebula_sql', + 'nebula_log', + 'cppunit', + 'dl', + 'pthread' +]) + +# Compile flags +main_env.Append(CPPFLAGS=[ + "-g", + "-Wall" +]) + +# MYSQL +main_env.Append(LIBPATH=["/usr/lib/mysql"]) +main_env.Append(CPPPATH=["/usr/include/mysql"]) + +sqlite=ARGUMENTS.get('sqlite', 'yes') +if sqlite=='yes': + main_env.Append(CPPFLAGS=["-DSQLITE_DB"]) + main_env.Append(LIBS=[ 'sqlite3', ]) + +# MySQL +mysql=ARGUMENTS.get('mysql', 'no') +if mysql=='yes': + main_env.Append(CPPFLAGS=["-DMYSQL_DB"]) + main_env.Append(LIBS=[ 'mysqlclient', ]) + +main_env.Program('test','AuthManagerTest.cc') +# diff --git a/src/authm/test/dummy b/src/authm/test/dummy new file mode 100755 index 0000000000..c4805279e1 --- /dev/null +++ b/src/authm/test/dummy @@ -0,0 +1,44 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- +# Copyright 2002-2006 GridWay Team, Distributed Systems Architecture +# Group, Universidad Complutense de Madrid +# +# Licensed 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. +# -------------------------------------------------------------------------- + +echo "MAD started" > mad.log + + +while read COMMAND ARG1 ARG2 ARG3 +do + echo "$COMMAND $ARG1 $ARG2 $ARG3" >> mad.log + case $COMMAND in + "INIT") + echo "INIT SUCCESS" + ;; + "FINALIZE") + echo "FINALIZE SUCCESS" + exit 0 + ;; + "AUTHORIZE") + echo "AUTHORIZE FAILURE $ARG1 $ARG3" + ;; + "AUTHENTICATE") + echo "AUTHENTICATE SUCCESS $ARG1" + ;; + *) + echo "$COMMAND - FAILURE Unknown command" + ;; + esac +done From 3b259c33aad5b26ffc873e2585f9297d089746b9 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 29 May 2010 03:43:43 +0200 Subject: [PATCH 04/64] feature #203: removed unneeded files --- src/authm/test/.sconsign.dblite | Bin 10683 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/authm/test/.sconsign.dblite diff --git a/src/authm/test/.sconsign.dblite b/src/authm/test/.sconsign.dblite deleted file mode 100644 index d9234a98a4ef1abf2fa1390cb7b9f749b3940572..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10683 zcmdT~S(6*p753nLyo@&%V-f~1Mqo4QePJ_W*&asrjKNl$RhaI+x2J_?q&Zr^Hok$d z@B5Mff$V`KWG8zF*&u5rR3%TmLpkZemgsX}a6$mU)d@tM1bVtS`d`XSr9Inx3rql_s28 zhSTt6LUH*>15Pi)86l^;BDqwVXu!rYoarp}T))W~ZyI-2wE^%#1I{YL$M84e&)`=+ zUWT(4_A+! zvcBbo1Gx~hZVQ78av|ngF2n@8FvPUFNI+sHZ4Eb#hnO+jLQIm2d6GD*c>H%)jx<|! zikyZ7r#u5G;UqIqPT-`Q7EuDKdx37tvJ_BNp+q8{t;rZL161Th``er}2D;#6a84%Y zWF?&JnA0Vk)1`vb3eM?3tJ36*E)$F{XK+QrXgh-|XEM?)V%ff;nzHRvNz;gH=$>Xs zrWshate7y2jQ+T^&1eUMf?%|hGup)&?M@ht#Eh=uj7B-5)(|Xq&YG>1@~uj~NslyP zNvR%hbfTj6w{S<~L7t;sZgodR>8_*(nyu&(E=AyDF-g8_+cxzHR+vpQ74=%*e5xqi zJx~l{Ybp&~ z%v>&7|6JGJj(ZfXfnmRi{aqUSkCv_Xw!9-)>4vSFrs?^nCE2c|DMa&BUot(^3Zh!F zkge@&+pKz&tyROVW|(sNXyH0|c)?z=QZCUAIl#uoBbYgXB&*c*6`4}12bxEOaJ{;v z&8auxa@J=!?W|X@F~%4cg{1Cbus4y^F$Uv_q+asDf5mDU)Y36-l0|F}*H9}JNb4fkGCAE}DsuN4KRlclIk0_Z&<3UN{S{C;<`{qMsI9tGrF0^9G)>_Om@x~Q$s(zrzvW2%IQAt-MsKqnJ^w^s#(meqiIBe(Y3>w)XqFIJkc8IvG z47X>7h$Ce<8gf1zL&UK%+>s6u$4?j{SQ$RiIYiu9hP%X~ERL4rlVUmUW^hj~jMm{^ z2KVLoM}^V-hIpq$H8kDFPR#XGS1~jrur%WNhGE+}{`KMhaC(dkV1S~Vdw{`%xe#yZ zp%Cxm;SkwBA|R3Qqv7%K5Q#k&BA3Snm;MsP*~t|d^MoMuB!j1tjCq>DGfBoI6(6XQ zODzj`A^e~BhlXU}R;&^Qw1qen!phR&xSv=!v32WQb$)MeZ08hxpcERcz z3@})et@O2JSeLDIVIQP+0p{mvJT*Hj9TWD9w9+@8Q0ZruVN++Na|1zC*PT_%>K z7`g!$#7mVcK6=l(OzvHn3voRzVz4#49@}tab=GF|j8BO}H5hWUtCTO}=<2LYiZahD zQFg|99TA%GPLoe3gX9uU2Z}S=E1+MSnyOVZaM1+>Q=O4s!J+}C158XUnp5Z%YRV-U z9DIh1^xhe{*!oOmfW&izGJ0o(?qCKe@(Rt~VJ2%685n$qY%H3QO1lh~ggk{ir1H`- zT$UEl<<6jIx!7S?t}pwl=ewq92a=~)n8qe0=p4coGcCF8W%MUR%?vZxk;~eTg`(J= zI~>ZLNFnkgcl{sx_3pT4MvBpXEk5Te2BW#KP<$W9#}M&CM3M=LpKl^_Lzl62lZZud zE>UQpYZk?szZi}btFI4W-3g4S9V`_8kiqcL*&(_wVP{Gu6cJ*hWEeQs1ga+6l)xBb zl%8LjV2m@^$9eEEHsR5Ah`kyyqVZMmxQ4-gj`5ca2GNL)ql7L6mJ#^A?s{%uNrCK| z2Jvmnz;1J-_}72%5m;c97&si`ty;>%*KithJqg=tQ`arczxYpuSbl?Cl)+zU~nVH_;co76xlQk%~W;I#6>ktlwlbSUrX1d zKy_UTHzCHNN$xdMXAw7kq3m3~dGF}>p23T2gPTjk`$u+_2E~Qldxu933~oNK&)Hwt zg%1wx9o?~WbboxAKNAn(=FTnuP#JC!{9FdOUP=;-Eq9POG6GH}-87gvXr-~ddE=W~m67yKL zRD&q8t6?95b`|Q0f`4RBCl(Jc|&n$VDG29VW|dKns7wGiEOs;Ru0 z4mqEWErIvc>6UQp1Wmo8497bcnD^6SZDx_HJH^u6#Qd5uYDCoVRAFhm7 zI$R}g=ROAa=R#}@9&lz`^#@(<`#zL&hI>(gRATmw< zqs}?Anyy^=+h-P!eZ-sEUi;(DxgUy@y5CPYgCF{?#E*W`xp3C^#71zQb8EZ5PxYo- z?NH3HH=gcIklQe+AJLxupK-1{nV@tyZO=OP$zVj2X3 zKF%xQ$Qbu=#5VXUgU@mV;cUbRU2THTAp$oD1j6STe1Ri~NjFC5I_-WD5%{z#5Wd9V z%N#)*G{p#AkKVq52>j4ZAiT!l^_d7=kE_0l2>iH8AbgF%*H1v`cDVEgBJjf{f$$9m z-<*li^)TpLh`j5W+HU;?0$&|+_Mu1zhdy~nRn6EOa2Wa2rqd!X_($|?9GMiuFLjI7yEx4Ld}K>Q*L7%1Ll=#1^D1=f9KDy`gV&qzB4B;#XdJn Date: Mon, 31 May 2010 23:37:47 +0200 Subject: [PATCH 05/64] feature #203: Authorize test --- src/authm/test/AuthManagerTest.cc | 33 ++++++++++++++++++++++++++++++- src/authm/test/dummy | 1 + 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index b62e9a5be4..f5c9024327 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -42,9 +42,9 @@ class AuthManagerTest : public CppUnit::TestFixture CPPUNIT_TEST_SUITE (AuthManagerTest); CPPUNIT_TEST (load); - CPPUNIT_TEST (timeout); CPPUNIT_TEST (authenticate); + CPPUNIT_TEST (authorize); CPPUNIT_TEST_SUITE_END (); @@ -152,6 +152,37 @@ public: CPPUNIT_ASSERT(0==0); } + + void authorize() + { + int rc; + AuthRequest ar(2,"dummy"); + + rc = am->start(); + + CPPUNIT_ASSERT(rc==0); + + am->load_mads(0); + + string astr="CREATION:VM:-1 USAGE:IMAGE:2 USAGE:NET:4 MANAGE:HOST:3"; + + ar.add_auth(AuthRequest::CREATION,AuthRequest::VM,-1); + ar.add_auth(AuthRequest::USAGE,AuthRequest::IMAGE,2); + ar.add_auth(AuthRequest::USAGE,AuthRequest::NET,4); + ar.add_auth(AuthRequest::MANAGE,AuthRequest::HOST,3); + + am->trigger(AuthManager::AUTHORIZE,&ar); + ar.wait(); + + CPPUNIT_ASSERT(ar.result==false); + CPPUNIT_ASSERT(ar.message==astr); + + am->trigger(AuthManager::FINALIZE,0); + + pthread_join(am->get_thread_id(),0); + + CPPUNIT_ASSERT(0==0); + } private: AuthManager * am; diff --git a/src/authm/test/dummy b/src/authm/test/dummy index c4805279e1..c49736261e 100755 --- a/src/authm/test/dummy +++ b/src/authm/test/dummy @@ -32,6 +32,7 @@ do exit 0 ;; "AUTHORIZE") + sleep 1 echo "AUTHORIZE FAILURE $ARG1 $ARG3" ;; "AUTHENTICATE") From a707f730f2be4015dc81210c8cbdfdf6cf472f46 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 6 Jul 2010 12:35:47 +0200 Subject: [PATCH 06/64] feature #203: Prevent race condition when a AuthRequests timeout. Now timeout timers are handled by the AuthManager. --- include/AuthManager.h | 439 ++++++++++++++++-------------- src/authm/AuthManager.cc | 50 +++- src/authm/test/AuthManagerTest.cc | 11 +- 3 files changed, 284 insertions(+), 216 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index 9247894bd2..fed8b017a1 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -17,6 +17,8 @@ #ifndef AUTH_MANAGER_H_ #define AUTH_MANAGER_H_ +#include + #include "MadManager.h" #include "ActionManager.h" @@ -24,8 +26,225 @@ using namespace std; -//Forward definition of the AuthManager -class AuthManager; +//Forward definition of the AuthRequest +class AuthRequest; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +extern "C" void * authm_action_loop(void *arg); + +class AuthManager : public MadManager, public ActionListener +{ +public: + + AuthManager( + time_t timer, + time_t __time_out, + vector& _mads): + MadManager(_mads), timer_period(timer) + { + _time_out = __time_out; + + am.addListener(this); + + pthread_mutex_init(&mutex,0); + }; + + ~AuthManager(){}; + + enum Actions + { + AUTHENTICATE, + AUTHORIZE, + FINALIZE + }; + + /** + * Triggers specific actions to the Auth Manager. This function + * wraps the ActionManager trigger function. + * @param action the Auth Manager action + * @param request an auth request + */ + void trigger( + Actions action, + AuthRequest * request); + + /** + * This functions starts the associated listener thread, and creates a + * new thread for the AuthManager. This thread will wait in + * an action loop till it receives ACTION_FINALIZE. + * @return 0 on success. + */ + int start(); + + /** + * Loads Virtual Machine Manager Mads defined in configuration file + * @param uid of the user executing the driver. When uid is 0 the nebula + * identity will be used. Otherwise the Mad will be loaded through the + * sudo application. + */ + void load_mads(int uid); + + /** + * Gets the thread identification. + * @return pthread_t for the manager thread (that in the action loop). + */ + pthread_t get_thread_id() const + { + return authm_thread; + }; + + /** + * Notify the result of an auth request + */ + void notify_request(int auth_id, bool result, const string& message); + + /** + * Discards a pending request. Call this before freeing not notified or + * timeout requests. + */ + void discard_request(int auth_id) + { + lock(); + + auth_requests.erase(auth_id); + + unlock(); + } + + /** + * Gets default timeout for Auth requests + */ + static time_t time_out() + { + return _time_out; + } + +private: + /** + * Thread id for the Transfer Manager + */ + pthread_t authm_thread; + + /** + * Action engine for the Manager + */ + ActionManager am; + + /** + * List of pending requests + */ + map auth_requests; + + /** + * Mutex to access the auth_requests + */ + pthread_mutex_t mutex; + + /** + * Default timeout for Auth requests + */ + static time_t _time_out; + + /** + * Timer for the Manager (periocally triggers timer action) + */ + time_t timer_period; + + /** + * Returns a pointer to a Auth Manager driver. + * @param name of an attribute of the driver (e.g. its type) + * @param value of the attribute + * @return the Auth driver with attribute name equal to value + * or 0 in not found + */ + const AuthManagerDriver * get( + const string& name, + const string& value) + { + return static_cast + (MadManager::get(0,name,value)); + }; + + /** + * Returns a pointer to a Auth Manager driver. The driver is + * searched by its name. + * @param name the name of the driver + * @return the TM driver owned by uid with attribute name equal to value + * or 0 in not found + */ + const AuthManagerDriver * get(const string& name) + { + string _name("NAME"); + + return static_cast + (MadManager::get(0,_name,name)); + }; + + /** + * Function to execute the Manager action loop method within a new pthread + * (requires C linkage) + */ + friend void * authm_action_loop(void *arg); + + /** + * The action function executed when an action is triggered. + * @param action the name of the action + * @param arg arguments for the action function + */ + void do_action( + const string & action, + void * arg); + + /** + * This function authenticates a user + */ + void authenticate_action(AuthRequest * ar); + + /** + * This function authorizes a user request + */ + void authorize_action(AuthRequest * ar); + + /** + * This function is periodically executed to check time_outs on requests + */ + void timer_action(); + + /** + * Function to lock the pool + */ + void lock() + { + pthread_mutex_lock(&mutex); + }; + + /** + * Function to unlock the pool + */ + void unlock() + { + pthread_mutex_unlock(&mutex); + }; + + /** + * Add a new request to the Request map + * @param ar pointer to the AuthRequest + * @return the id for the request + */ + int add_request(AuthRequest *ar); + + /** + * Gets request from the Request map + * @param id for the request + * @return pointer to the AuthRequest + */ + AuthRequest * get_request(int id); +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ /** * The AuthRequest class is used to pass an Authorization or Authentication @@ -39,7 +258,8 @@ public: result(false), timeout(false), auth_driver(_auth_driver), - uid(_uid) + uid(_uid), + time_out(0) { am.addListener(this); }; @@ -135,9 +355,11 @@ public: /** * Wait for the AuthRequest to be completed */ - void wait(time_t timeout=30) + void wait() { - am.loop(timeout,0); + time_out = time(0) + AuthManager::time_out(); + + am.loop(0,0); }; /** @@ -180,7 +402,12 @@ private: int uid; /** - * Authentication challenge + * Timeout for this request + */ + time_t time_out; + + /** + * Authentication challenge, as sent in the XML-RPC call */ string challenge; @@ -190,205 +417,9 @@ private: vector auths; /** - * + * No actions defined for the Auth request, just FINALIZE when done */ - void do_action(const string &name, void *args) - { - if (name == ACTION_TIMER) - { - result = false; - timeout = true; - - am.trigger(ActionListener::ACTION_FINALIZE,0); - } - } -}; - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -extern "C" void * authm_action_loop(void *arg); - -class AuthManager : public MadManager, public ActionListener -{ -public: - - AuthManager( - vector& _mads): - MadManager(_mads) - { - am.addListener(this); - pthread_mutex_init(&mutex,0); - }; - - ~AuthManager(){}; - - enum Actions - { - AUTHENTICATE, - AUTHORIZE, - FINALIZE - }; - - /** - * Triggers specific actions to the Auth Manager. This function - * wraps the ActionManager trigger function. - * @param action the Auth Manager action - * @param request an auth request - */ - void trigger( - Actions action, - AuthRequest * request); - - /** - * This functions starts the associated listener thread, and creates a - * new thread for the AuthManager. This thread will wait in - * an action loop till it receives ACTION_FINALIZE. - * @return 0 on success. - */ - int start(); - - /** - * Loads Virtual Machine Manager Mads defined in configuration file - * @param uid of the user executing the driver. When uid is 0 the nebula - * identity will be used. Otherwise the Mad will be loaded through the - * sudo application. - */ - void load_mads(int uid); - - /** - * Gets the thread identification. - * @return pthread_t for the manager thread (that in the action loop). - */ - pthread_t get_thread_id() const - { - return authm_thread; - }; - - /** - * Notify the result of an auth request - */ - void notify_request(int auth_id, bool result, const string& message); - - /** - * Discards a pending request. Call this before freeing not notified or - * timeout requests. - */ - void discard_request(int auth_id) - { - lock(); - - auth_requests.erase(auth_id); - - unlock(); - } - - -private: - /** - * Thread id for the Transfer Manager - */ - pthread_t authm_thread; - - /** - * Action engine for the Manager - */ - ActionManager am; - - /** - * Action engine for the Manager - */ - map auth_requests; - - /** - * Mutex to access the auth_requests - */ - pthread_mutex_t mutex; - - /** - * Returns a pointer to a Auth Manager driver. - * @param name of an attribute of the driver (e.g. its type) - * @param value of the attribute - * @return the Auth driver with attribute name equal to value - * or 0 in not found - */ - const AuthManagerDriver * get( - const string& name, - const string& value) - { - return static_cast - (MadManager::get(0,name,value)); - }; - - /** - * Returns a pointer to a Auth Manager driver. The driver is - * searched by its name. - * @param name the name of the driver - * @return the TM driver owned by uid with attribute name equal to value - * or 0 in not found - */ - const AuthManagerDriver * get(const string& name) - { - string _name("NAME"); - - return static_cast - (MadManager::get(0,_name,name)); - }; - - /** - * Function to execute the Manager action loop method within a new pthread - * (requires C linkage) - */ - friend void * authm_action_loop(void *arg); - - /** - * The action function executed when an action is triggered. - * @param action the name of the action - * @param arg arguments for the action function - */ - void do_action( - const string & action, - void * arg); - - /** - * This function authenticates a user - */ - void authenticate_action(AuthRequest * ar); - - /** - * This function authorizes a user request - */ - void authorize_action(AuthRequest * ar); - - /** - * Function to lock the pool - */ - void lock() - { - pthread_mutex_lock(&mutex); - }; - - /** - * Function to unlock the pool - */ - void unlock() - { - pthread_mutex_unlock(&mutex); - }; - - /** - * Add a new request to the Request map - * @param ar pointer to the AuthRequest - * @return the id for the request - */ - int add_request(AuthRequest *ar); - - /** - * Gets request from the Request map - * @param id for the request - * @return pointer to the AuthRequest - */ - AuthRequest * get_request(int id); + void do_action(const string &name, void *args){}; }; #endif /*AUTH_MANAGER_H*/ diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 54c25a444f..e6a47f00f8 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -22,6 +22,11 @@ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +time_t AuthManager::_time_out; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + extern "C" void * authm_action_loop(void *arg) { AuthManager * authm; @@ -35,7 +40,7 @@ extern "C" void * authm_action_loop(void *arg) NebulaLog::log("AuM",Log::INFO,"Authorization Manager started."); - authm->am.loop(0,0); + authm->am.loop(authm->timer_period,0); NebulaLog::log("AuM",Log::INFO,"Authorization Manager stopped."); @@ -101,21 +106,20 @@ void AuthManager::do_action(const string &action, void * arg) { AuthRequest * request; - if ( arg == 0 && action != ACTION_FINALIZE ) - { - return; - } - request = static_cast(arg); - if (action == "AUTHENTICATE") + if (action == "AUTHENTICATE" && request != 0) { authenticate_action(request); } - else if (action == "AUTHORIZE") + else if (action == "AUTHORIZE" && request != 0) { authorize_action(request); } + else if (action == ACTION_TIMER) + { + timer_action(); + } else if (action == ACTION_FINALIZE) { NebulaLog::log("AuM",Log::INFO,"Stopping Authorization Manager..."); @@ -209,6 +213,35 @@ error_driver: ar->message = "Could not find Authorization driver"; ar->notify(); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthManager::timer_action() +{ + map::iterator it; + + time_t the_time = time(0); + + lock(); + + for (it=auth_requests.begin();it!=auth_requests.end();it++) + { + if (the_time > it->second->time_out) + { + AuthRequest * ar = it->second; + auth_requests.erase(it); + + ar->result = false; + ar->timeout = true; + ar->message.clear(); + + ar->notify(); + } + } + + unlock(); + +} /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -229,6 +262,7 @@ int AuthManager::add_request(AuthRequest *ar) return id; } +/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ AuthRequest * AuthManager::get_request(int id) diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index f5c9024327..eb2dbf87c1 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -42,7 +42,7 @@ class AuthManagerTest : public CppUnit::TestFixture CPPUNIT_TEST_SUITE (AuthManagerTest); CPPUNIT_TEST (load); - CPPUNIT_TEST (timeout); + //CPPUNIT_TEST (timeout); CPPUNIT_TEST (authenticate); CPPUNIT_TEST (authorize); @@ -75,7 +75,7 @@ public: t->get("AUTH_MAD", am_mads); - am = new AuthManager(am_mads); + am = new AuthManager(1,3,am_mads); }; void tearDown() @@ -101,6 +101,7 @@ public: CPPUNIT_ASSERT(0==0); } + //This test needs a driver that takes more than 3 secs to AUTHENTICATE void timeout() { int rc; @@ -112,8 +113,10 @@ public: am->load_mads(0); + am->trigger(AuthManager::AUTHENTICATE,&ar); + ar.set_challenge("the_secret"); - ar.wait(1); + ar.wait(); CPPUNIT_ASSERT(ar.result==false); CPPUNIT_ASSERT(ar.timeout==true); @@ -141,7 +144,7 @@ public: ar.set_challenge("the_secret"); am->trigger(AuthManager::AUTHENTICATE,&ar); - ar.wait(300); + ar.wait(); CPPUNIT_ASSERT(ar.result==true); From 3388563a9df127a591d185100c5e5ac164ad184f Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 15:45:00 +0200 Subject: [PATCH 07/64] feature #203: Conforming new protocol spec --- include/AuthManager.h | 26 +++++++++++++++----- include/AuthManagerDriver.h | 12 ++++++--- src/authm/AuthManager.cc | 8 ++++-- src/authm/AuthManagerDriver.cc | 12 +++++++-- src/authm/test/AuthManagerTest.cc | 5 ++-- src/authm/test/dummy | 41 +++++++++++++++---------------- src/host/test/HostPoolTest.cc | 4 +-- 7 files changed, 70 insertions(+), 38 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index fed8b017a1..959b4f56af 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -145,12 +145,12 @@ private: /** * Default timeout for Auth requests */ - static time_t _time_out; + static time_t _time_out; /** * Timer for the Manager (periocally triggers timer action) */ - time_t timer_period; + time_t timer_period; /** * Returns a pointer to a Auth Manager driver. @@ -292,9 +292,13 @@ public: * Sets the challenge to authenticate an user * @param challenge a driver specific authentication challenge */ - void set_challenge(const string &ch) + void add_authenticate(const string &_username, + const string &_password, + const string &_session) { - challenge = ch; + username = _username; + password = _password; + session = _session; } /** @@ -407,9 +411,19 @@ private: time_t time_out; /** - * Authentication challenge, as sent in the XML-RPC call + * Username to authenticate the user */ - string challenge; + string username; + + /** + * User password to authenticate the user + */ + string password; + + /** + * Authentication token as sent in the XML-RPC call + */ + string session; /** * A list of authorization requests diff --git a/include/AuthManagerDriver.h b/include/AuthManagerDriver.h index 69764d06ea..50dde72e14 100644 --- a/include/AuthManagerDriver.h +++ b/include/AuthManagerDriver.h @@ -77,12 +77,18 @@ private: /** * Sends an authorization request to the MAD: - * "AUTHENTICATE OPERATION_ID USER_ID CHALLENGE" + * "AUTHENTICATE REQUEST_ID USER_ID USER_NAME PASSWORD XMLRPC_TOKEN" * @param oid an id to identify the request. * @param uid the user id. - * @param challenge to authenticate the user + * @param username + * @param password + * @param session token from the xml-rpc parameter */ - void authenticate(int oid, int uid, const string& ch) const; + void authenticate(int oid, + int uid, + const string& username, + const string& password, + const string& session) const; }; /* -------------------------------------------------------------------------- */ diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index e6a47f00f8..66ea6e7dcd 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -161,10 +161,14 @@ void AuthManager::authenticate_action(AuthRequest * ar) // ------------------------------------------------------------------------ // Make the request to the driver - // ------------------------------------------------------------------------ + // ---- -------------------------------------------------------------------- - authm_md->authenticate(ar->id,ar->uid,ar->challenge); + authm_md->authenticate(ar->id, + ar->uid, + ar->username, + ar->password, + ar->session); return; error_driver: diff --git a/src/authm/AuthManagerDriver.cc b/src/authm/AuthManagerDriver.cc index 355788bddc..b45d42cda9 100644 --- a/src/authm/AuthManagerDriver.cc +++ b/src/authm/AuthManagerDriver.cc @@ -34,11 +34,19 @@ void AuthManagerDriver::authorize(int oid, int uid, const string& reqs) const write(os); } -void AuthManagerDriver::authenticate(int oid, int uid, const string& ch) const +void AuthManagerDriver::authenticate(int oid, + int uid, + const string& username, + const string& password, + const string& session) const { ostringstream os; - os << "AUTHENTICATE " << oid << " " << uid << " " << ch << endl; + os << "AUTHENTICATE " << oid << " " + << uid << " " + << username << " " + << password << " " + << session << endl; write(os); } diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index eb2dbf87c1..11ad410b92 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -113,9 +113,10 @@ public: am->load_mads(0); + ar.add_authenticate("the_user","the_pass","the_secret"); + am->trigger(AuthManager::AUTHENTICATE,&ar); - ar.set_challenge("the_secret"); ar.wait(); CPPUNIT_ASSERT(ar.result==false); @@ -141,7 +142,7 @@ public: am->load_mads(0); - ar.set_challenge("the_secret"); + ar.add_authenticate("the_user","the_pass","the_secret"); am->trigger(AuthManager::AUTHENTICATE,&ar); ar.wait(); diff --git a/src/authm/test/dummy b/src/authm/test/dummy index c49736261e..00f1caef56 100755 --- a/src/authm/test/dummy +++ b/src/authm/test/dummy @@ -1,24 +1,24 @@ #!/bin/bash -# -------------------------------------------------------------------------- -# Copyright 2002-2006 GridWay Team, Distributed Systems Architecture -# Group, Universidad Complutense de Madrid -# -# Licensed 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. -# -------------------------------------------------------------------------- +# -------------------------------------------------------------------------- +# Copyright 2002-2006 GridWay Team, Distributed Systems Architecture +# Group, Universidad Complutense de Madrid +# +# Licensed 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. +# -------------------------------------------------------------------------- + +echo "MAD started" >> mad.log -echo "MAD started" > mad.log - while read COMMAND ARG1 ARG2 ARG3 do @@ -30,10 +30,9 @@ do "FINALIZE") echo "FINALIZE SUCCESS" exit 0 - ;; + ;; "AUTHORIZE") - sleep 1 - echo "AUTHORIZE FAILURE $ARG1 $ARG3" + echo "AUTHORIZE FAILURE $ARG1 $ARG3" ;; "AUTHENTICATE") echo "AUTHENTICATE SUCCESS $ARG1" diff --git a/src/host/test/HostPoolTest.cc b/src/host/test/HostPoolTest.cc index a3c444b8ba..10edc80cfa 100644 --- a/src/host/test/HostPoolTest.cc +++ b/src/host/test/HostPoolTest.cc @@ -332,9 +332,9 @@ public: } // Discover the enabled hosts - rc = hp->discover(&dh); + rc = hp->discover(&dh,5); CPPUNIT_ASSERT(rc == 0); - CPPUNIT_ASSERT(dh.size() == 8); + CPPUNIT_ASSERT(dh.size() == 5); for(i=0,it=dh.begin(),oss.str("");it!=dh.end();it++,i++,oss.str("")) { From 7f646b8b660ab51541e460fb12b6f14bb1cf39fb Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 16:36:33 +0200 Subject: [PATCH 08/64] feature 203: Requests now follow the new format --- include/AuthManager.h | 62 ++++++++++++++++++++++--------- src/authm/test/AuthManagerTest.cc | 34 ++++++++++++++--- 2 files changed, 73 insertions(+), 23 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index 959b4f56af..86d33badd5 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -271,10 +271,10 @@ public: */ enum Operation { - CREATION, /** Authorization to create an object (host, vm, net, image)*/ - DELETION, /** Authorization to delete an object */ - USAGE, /** Authorization to use an object */ - MANAGE /** Authorization to manage an object */ + CREATE, /** Authorization to create an object (host, vm, net, image)*/ + DELETE, /** Authorization to delete an object */ + USE, /** Authorization to use an object */ + MANAGE /** Authorization to manage an object */ }; /** @@ -302,23 +302,24 @@ public: } /** - * Adds a new authorization item to this requests - * @param op the operation to be authorized - * @param ob the object over which the operation will be performed - * @param oid id of the object + * Adds a new authorization item to this request + * + * OBJECT:OBJECT_ID:ACTION:OWNER:PUBLIC + * + * @param ob the object over which the operation will be performed + * @param ob_id the object unique id + * @param op the operation to be authorized + * @param owner id of user that owns the object + * @param pub public attribute */ - void add_auth(Operation op, Object ob, int oid) + void add_auth(Object ob, + const string& ob_id, + Operation op, + int owner, + bool pub) { ostringstream oss; - switch (op) - { - case CREATION: oss << "CREATION:" ; break; - case DELETION: oss << "DELETION:" ; break; - case USAGE: oss << "USAGE:" ; break; - case MANAGE: oss << "MANAGE:" ; break; - } - switch (ob) { case VM: oss << "VM:" ; break; @@ -327,11 +328,36 @@ public: case IMAGE: oss << "IMAGE:" ; break; } - oss << oid; + oss << ob_id << ":"; + + switch (op) + { + case CREATE: oss << "CREATE:" ; break; + case DELETE: oss << "DELETE:" ; break; + case USE: oss << "USE:" ; break; + case MANAGE: oss << "MANAGE:" ; break; + } + + oss << owner << ":" << pub; auths.push_back(oss.str()); }; + /** + * Adds a new authorization item to this requests + */ + void add_auth(Object ob, + int ob_id, + Operation op, + int owner, + bool pub) + { + ostringstream oss; + oss << ob_id; + + add_auth(ob,oss.str(),op,owner,pub); + }; + /** * Gets the authorization requests in a single string * @return a space separated list of auth requests. diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index 11ad410b92..5fbdfb8bce 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -168,12 +168,36 @@ public: am->load_mads(0); - string astr="CREATION:VM:-1 USAGE:IMAGE:2 USAGE:NET:4 MANAGE:HOST:3"; + //OBJECT:OBJECT_ID:ACTION:OWNER:PUBLIC - ar.add_auth(AuthRequest::CREATION,AuthRequest::VM,-1); - ar.add_auth(AuthRequest::USAGE,AuthRequest::IMAGE,2); - ar.add_auth(AuthRequest::USAGE,AuthRequest::NET,4); - ar.add_auth(AuthRequest::MANAGE,AuthRequest::HOST,3); + string astr="VM:dGhpcyBpcyBhIHNhbXBsZSB0ZW1wbGF0ZQ==:CREATE:-1:0 " + "IMAGE:2:USE:3:0 " + "NET:4:DELETE:5:1 " + "HOST:6:MANAGE:7:1"; + + ar.add_auth(AuthRequest::VM, + "dGhpcyBpcyBhIHNhbXBsZSB0ZW1wbGF0ZQ==", + AuthRequest::CREATE, + -1, + false); + + ar.add_auth(AuthRequest::IMAGE, + 2, + AuthRequest::USE, + 3, + false); + + ar.add_auth(AuthRequest::NET, + 4, + AuthRequest::DELETE, + 5, + true); + + ar.add_auth(AuthRequest::HOST, + 6, + AuthRequest::MANAGE, + 7, + true); am->trigger(AuthManager::AUTHORIZE,&ar); ar.wait(); From b14eefeeec0a97bc9b5d2ff92c853c2bd9c2cc47 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 17:50:49 +0200 Subject: [PATCH 09/64] feature #203: Plain authn/authz policies --- include/AuthManager.h | 47 +++++++++++++++++++++++---- src/authm/test/AuthManagerTest.cc | 54 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 6 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index 86d33badd5..b153bd04fe 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -259,7 +259,8 @@ public: timeout(false), auth_driver(_auth_driver), uid(_uid), - time_out(0) + time_out(0), + self_authorize(true) { am.addListener(this); }; @@ -309,7 +310,8 @@ public: * @param ob the object over which the operation will be performed * @param ob_id the object unique id * @param op the operation to be authorized - * @param owner id of user that owns the object + * @param owner id of user that owns the object. For creates MUST equals + uid, hosts owner is uid=0 * @param pub public attribute */ void add_auth(Object ob, @@ -319,6 +321,7 @@ public: bool pub) { ostringstream oss; + bool auth = owner == uid; switch (ob) { @@ -332,14 +335,31 @@ public: switch (op) { - case CREATE: oss << "CREATE:" ; break; - case DELETE: oss << "DELETE:" ; break; - case USE: oss << "USE:" ; break; - case MANAGE: oss << "MANAGE:" ; break; + case CREATE: + oss << "CREATE:" ; + break; + + case DELETE: + oss << "DELETE:" ; + break; + + case USE: + oss << "USE:" ; + if ( ob == NET || ob == IMAGE ) + { + auth = auth || (pub == true); + } + break; + + case MANAGE: + oss << "MANAGE:" ; + break; } oss << owner << ":" << pub; + self_authorize = self_authorize && auth; + auths.push_back(oss.str()); }; @@ -392,6 +412,16 @@ public: am.loop(0,0); }; + bool plain_authorize() + { + return ( uid == 0 || self_authorize ); + } + + bool plain_authenticate() + { + return (password == session); + } + /** * The result of the request, true if authorized or authenticated */ @@ -456,6 +486,11 @@ private: */ vector auths; + /** + * Plain authorization for the request + */ + bool self_authorize; + /** * No actions defined for the Auth request, just FINALIZE when done */ diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index 5fbdfb8bce..e7897225ed 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -45,6 +45,8 @@ class AuthManagerTest : public CppUnit::TestFixture //CPPUNIT_TEST (timeout); CPPUNIT_TEST (authenticate); CPPUNIT_TEST (authorize); + CPPUNIT_TEST (self_authorize); + CPPUNIT_TEST (self_authenticate); CPPUNIT_TEST_SUITE_END (); @@ -211,6 +213,58 @@ public: CPPUNIT_ASSERT(0==0); } + + + void self_authorize() + { + AuthRequest ar(2,"dummy"); + AuthRequest ar1(2,"dummy"); + AuthRequest ar2(3,"dummy"); + AuthRequest ar3(4,"dummy"); + AuthRequest ar4(2,"dummy"); + AuthRequest ar5(0,"dummy"); + AuthRequest ar6(0,"dummy"); + + ar.add_auth(AuthRequest::VM,"dGhpcy",AuthRequest::CREATE,2,false); + ar.add_auth(AuthRequest::NET,2,AuthRequest::USE,2,false); + ar.add_auth(AuthRequest::IMAGE,3,AuthRequest::USE,4,true); + + CPPUNIT_ASSERT(ar.plain_authorize() == true); + + ar1.add_auth(AuthRequest::VM,"dGhpcy",AuthRequest::CREATE,2,false); + ar1.add_auth(AuthRequest::NET,2,AuthRequest::USE,2,false); + ar1.add_auth(AuthRequest::IMAGE,3,AuthRequest::USE,4,false); + + CPPUNIT_ASSERT(ar1.plain_authorize() == false); + + ar2.add_auth(AuthRequest::HOST,"dGhpcy",AuthRequest::CREATE,0,false); + CPPUNIT_ASSERT(ar2.plain_authorize() == false); + + ar3.add_auth(AuthRequest::VM,5,AuthRequest::MANAGE,2,false); + CPPUNIT_ASSERT(ar3.plain_authorize() == false); + + ar4.add_auth(AuthRequest::VM,4,AuthRequest::MANAGE,2,false); + CPPUNIT_ASSERT(ar4.plain_authorize() == true); + + ar5.add_auth(AuthRequest::HOST,4,AuthRequest::MANAGE,0,false); + CPPUNIT_ASSERT(ar5.plain_authorize() == true); + + ar6.add_auth(AuthRequest::HOST,4,AuthRequest::CREATE,0,false); + CPPUNIT_ASSERT(ar6.plain_authorize() == true); + } + + void self_authenticate() + { + AuthRequest ar(2,"dummy"); + AuthRequest ar1(2,"dummy"); + + ar.add_authenticate("the_user","the_pass","the_secret"); + CPPUNIT_ASSERT(ar.plain_authenticate() == false); + + ar1.add_authenticate("the_user","the_pass","the_pass"); + CPPUNIT_ASSERT(ar1.plain_authenticate() == true); + } + private: AuthManager * am; From 53c06533d30647db0abbe8e29c8a3205229420e1 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 18:49:37 +0200 Subject: [PATCH 10/64] Better scope for the hook driver name, removed unused variable --- include/HookManager.h | 33 ++++++++++++++++----------------- src/hm/HookManager.cc | 32 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/include/HookManager.h b/include/HookManager.h index 453d371dc5..5495cbd549 100644 --- a/include/HookManager.h +++ b/include/HookManager.h @@ -31,20 +31,15 @@ class HookManager : public MadManager, public ActionListener public: HookManager(vector& _mads, VirtualMachinePool * _vmpool) - :MadManager(_mads),vmpool(_vmpool) + :MadManager(_mads),vmpool(_vmpool) { am.addListener(this); }; ~HookManager(){}; - - /** - * Generic name for the Hook driver - */ - static const char * hook_driver_name; /** - * This functions starts the associated listener thread, and creates a + * This functions starts the associated listener thread, and creates a * new thread for the Hook Manager. This thread will wait in * an action loop till it receives ACTION_FINALIZE. * @return 0 on success. @@ -61,12 +56,12 @@ public: }; /** - * + * */ void load_mads(int uid=0); /** - * + * */ void finalize() { @@ -74,39 +69,43 @@ public: }; /** - * Returns a pointer to a Information Manager MAD. The driver is + * Returns a pointer to a Information Manager MAD. The driver is * searched by its name and owned by oneadmin with uid=0. * @param name of the driver - * @return the Hook driver owned by uid 0, with attribute "NAME" equal to + * @return the Hook driver owned by uid 0, with attribute "NAME" equal to * name or 0 in not found */ const HookManagerDriver * get() { string name("NAME"); - string hook_name(hook_driver_name); - + return static_cast (MadManager::get(0,name,hook_driver_name)); }; - + private: + /** + * Generic name for the Hook driver + */ + static const char * hook_driver_name; + /** * Pointer to the VirtualMachine Pool */ VirtualMachinePool * vmpool; - + /** * Thread id for the HookManager */ pthread_t hm_thread; - + /** * Action engine for the Manager */ ActionManager am; /** - * Function to execute the Manager action loop method within a new pthread + * Function to execute the Manager action loop method within a new pthread * (requires C linkage) */ friend void * hm_action_loop(void *arg); diff --git a/src/hm/HookManager.cc b/src/hm/HookManager.cc index 3071249761..1fa91e9dc1 100644 --- a/src/hm/HookManager.cc +++ b/src/hm/HookManager.cc @@ -34,28 +34,28 @@ extern "C" void * hm_action_loop(void *arg) NebulaLog::log("HKM",Log::INFO,"Hook Manager started."); hm = static_cast(arg); - + hm->am.loop(0,0); NebulaLog::log("HKM",Log::INFO,"Hook Manager stopped."); - + return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void HookManager::load_mads(int uid) +void HookManager::load_mads(int uid) { HookManagerDriver * hm_mad; ostringstream oss; const VectorAttribute * vattr; int rc; - + NebulaLog::log("HKM",Log::INFO,"Loading Hook Manager driver."); - + vattr = static_cast(mad_conf[0]); - + if ( vattr == 0 ) { NebulaLog::log("HKM",Log::INFO,"Failed to load Hook Manager driver."); @@ -63,18 +63,18 @@ void HookManager::load_mads(int uid) } VectorAttribute hook_conf("HOOK_MAD",vattr->value()); - + hook_conf.replace("NAME",hook_driver_name); - + hm_mad = new HookManagerDriver(0,hook_conf.value(),false,vmpool); - + rc = add(hm_mad); - + if ( rc == 0 ) { - oss.str(""); + oss.str(""); oss << "\tHook Manager loaded"; - + NebulaLog::log("HKM",Log::INFO,oss); } } @@ -95,7 +95,7 @@ int HookManager::start() } NebulaLog::log("HKM",Log::INFO,"Starting Hook Manager..."); - + pthread_attr_init (&pattr); pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE); @@ -112,14 +112,14 @@ void HookManager::do_action(const string &action, void * arg) if (action == ACTION_FINALIZE) { NebulaLog::log("HKM",Log::INFO,"Stopping Hook Manager..."); - - MadManager::stop(); + + MadManager::stop(); } else { ostringstream oss; oss << "Unknown action name: " << action; - + NebulaLog::log("HKM", Log::ERROR, oss); } } From 76e83df632f55d4b6b2a7676f4198552433fde36 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 18:50:32 +0200 Subject: [PATCH 11/64] feature #203: Simplified driver load procedure and getter --- include/AuthManager.h | 21 ++++++++-------- src/authm/AuthManager.cc | 40 +++++++++++++++---------------- src/authm/test/AuthManagerTest.cc | 26 ++++++++++---------- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index b153bd04fe..6484d3cd1d 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -152,6 +152,11 @@ private: */ time_t timer_period; + /** + * Generic name for the Auth driver + */ + static const char * auth_driver_name; + /** * Returns a pointer to a Auth Manager driver. * @param name of an attribute of the driver (e.g. its type) @@ -174,12 +179,12 @@ private: * @return the TM driver owned by uid with attribute name equal to value * or 0 in not found */ - const AuthManagerDriver * get(const string& name) + const AuthManagerDriver * get() { - string _name("NAME"); + string name("NAME"); return static_cast - (MadManager::get(0,_name,name)); + (MadManager::get(0,name,auth_driver_name)); }; /** @@ -254,10 +259,9 @@ private: class AuthRequest : public ActionListener { public: - AuthRequest(int _uid, const string& _auth_driver): + AuthRequest(int _uid): result(false), timeout(false), - auth_driver(_auth_driver), uid(_uid), time_out(0), self_authorize(true) @@ -451,11 +455,6 @@ private: */ ActionManager am; - /** - * The name of the Authorization driver to use with this request - */ - string auth_driver; - /** * The user id for this request */ @@ -477,7 +476,7 @@ private: string password; /** - * Authentication token as sent in the XML-RPC call + * Authentication token as sent in the XML-RPC call (user:session) */ string session; diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 66ea6e7dcd..e8da646e69 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -24,6 +24,8 @@ time_t AuthManager::_time_out; +const char * AuthManager::auth_driver_name = "auth_exe"; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -146,7 +148,7 @@ void AuthManager::authenticate_action(AuthRequest * ar) // Get the driver // ------------------------------------------------------------------------ - authm_md = get(ar->auth_driver); + authm_md = get(); if (authm_md == 0) { @@ -189,7 +191,7 @@ void AuthManager::authorize_action(AuthRequest * ar) // Get the driver // ------------------------------------------------------------------------ - authm_md = get(ar->auth_driver); + authm_md = get(); if (authm_md == 0) { @@ -318,39 +320,37 @@ void AuthManager::notify_request(int auth_id,bool result,const string& message) void AuthManager::load_mads(int uid) { - unsigned int i; ostringstream oss; const VectorAttribute * vattr; int rc; string name; AuthManagerDriver * authm_driver = 0; - oss << "Loading Authorization Manager drivers."; + oss << "Loading Auth. Manager driver."; NebulaLog::log("AuM",Log::INFO,oss); - for(i=0,oss.str("");i(mad_conf[0]); + + if ( vattr == 0 ) { - vattr = static_cast(mad_conf[i]); + NebulaLog::log("AuM",Log::ERROR,"Failed to load Auth. Manager driver."); + return; + } - name = vattr->vector_value("NAME"); + VectorAttribute auth_conf("AUTH_MAD",vattr->value()); - oss << "\tLoading driver: " << name; - NebulaLog::log("AuM", Log::INFO, oss); + auth_conf.replace("NAME",auth_driver_name); - authm_driver = new AuthManagerDriver(uid,vattr->value(),(uid!=0),this); + authm_driver = new AuthManagerDriver(uid,auth_conf.value(),(uid!=0),this); - if ( authm_driver == 0 ) - continue; + rc = add(authm_driver); - rc = add(authm_driver); + if ( rc == 0 ) + { + oss.str(""); + oss << "\tAuth Manager loaded"; - if ( rc == 0 ) - { - oss.str(""); - oss << "\tDriver " << name << " loaded."; - - NebulaLog::log("AuM",Log::INFO,oss); - } + NebulaLog::log("AuM",Log::INFO,oss); } } diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index e7897225ed..b916b099ce 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -68,7 +68,7 @@ public: MadManager::mad_manager_system_init(); - oss << "AUTH_MAD = [ name=\"dummy\", executable=\"/" << getenv("PWD") + oss << "AUTH_MAD = [ executable=\"/" << getenv("PWD") << "/dummy\"]" << endl; t = new Template(); @@ -107,7 +107,7 @@ public: void timeout() { int rc; - AuthRequest ar(2,"dummy"); + AuthRequest ar(2); rc = am->start(); @@ -136,7 +136,7 @@ public: void authenticate() { int rc; - AuthRequest ar(2,"dummy"); + AuthRequest ar(2); rc = am->start(); @@ -162,7 +162,7 @@ public: void authorize() { int rc; - AuthRequest ar(2,"dummy"); + AuthRequest ar(2); rc = am->start(); @@ -217,13 +217,13 @@ public: void self_authorize() { - AuthRequest ar(2,"dummy"); - AuthRequest ar1(2,"dummy"); - AuthRequest ar2(3,"dummy"); - AuthRequest ar3(4,"dummy"); - AuthRequest ar4(2,"dummy"); - AuthRequest ar5(0,"dummy"); - AuthRequest ar6(0,"dummy"); + AuthRequest ar(2); + AuthRequest ar1(2); + AuthRequest ar2(3); + AuthRequest ar3(4); + AuthRequest ar4(2); + AuthRequest ar5(0); + AuthRequest ar6(0); ar.add_auth(AuthRequest::VM,"dGhpcy",AuthRequest::CREATE,2,false); ar.add_auth(AuthRequest::NET,2,AuthRequest::USE,2,false); @@ -255,8 +255,8 @@ public: void self_authenticate() { - AuthRequest ar(2,"dummy"); - AuthRequest ar1(2,"dummy"); + AuthRequest ar(2); + AuthRequest ar1(2); ar.add_authenticate("the_user","the_pass","the_secret"); CPPUNIT_ASSERT(ar.plain_authenticate() == false); From 3317695a3c95587191c82df6513d65dd98cee4a0 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 19:27:32 +0200 Subject: [PATCH 12/64] feature #203: Default message for timeouts --- src/authm/AuthManager.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index e8da646e69..b0cfe4090b 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -239,7 +239,7 @@ void AuthManager::timer_action() ar->result = false; ar->timeout = true; - ar->message.clear(); + ar->message = "Auth request timeout"; ar->notify(); } From 2cc7ff67be133982b1c782bd1fafa0ef1db6ecfa Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 19:28:10 +0200 Subject: [PATCH 13/64] feature #203: Nebula deamon starts a generic auth manager if defined --- include/Nebula.h | 15 +++++++++++++- share/etc/oned.conf | 24 +++++++++++++++++++--- src/nebula/Nebula.cc | 47 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/include/Nebula.h b/include/Nebula.h index 37d51b59af..d3baf79f70 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -33,6 +33,7 @@ #include "DispatchManager.h" #include "RequestManager.h" #include "HookManager.h" +#include "AuthManager.h" class Nebula { @@ -108,6 +109,12 @@ public: return hm; }; + + AuthManager * get_authm() + { + return authm; + }; + // -------------------------------------------------------------- // Environment & Configuration // -------------------------------------------------------------- @@ -217,7 +224,7 @@ private: // ----------------------------------------------------------------------- Nebula():nebula_configuration(0),db(0),vmpool(0),hpool(0),vnpool(0),upool(0), - ipool(0),lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0) + ipool(0),lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0),hm(0),authm(0) { const char * nl = getenv("ONE_LOCATION"); @@ -308,6 +315,11 @@ private: delete hm; } + if ( authm != 0) + { + delete authm; + } + if ( nebula_configuration != 0) { delete nebula_configuration; @@ -363,6 +375,7 @@ private: DispatchManager * dm; RequestManager * rm; HookManager * hm; + AuthManager * authm; // --------------------------------------------------------------- // Implementation functions diff --git a/share/etc/oned.conf b/share/etc/oned.conf index c719da67cd..0e356e4c04 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -258,12 +258,12 @@ TM_MAD = [ HM_MAD = [ executable = "one_hm" ] - + #-------------------------------- Image Hook ----------------------------------- # This hook is used to handle image saving and overwriting when virtual machines -# reach the DONE state after being shutdown. -# +# reach the DONE state after being shutdown. +# # Uncomment one of the following: # # Self-contained (substitute [ONE_LOCATION] with its proper path) @@ -302,3 +302,21 @@ HM_MAD = [ # arguments = "$VMID $NAME", # remote = "no" ] #------------------------------------------------------------------------------ + +#******************************************************************************* +# Auth Manager Configuration +#******************************************************************************* +# The Driver (AUTHM_MAD) that will be used to authenticate and authorize +# OpenNebula requests. If not defined OpenNebula will use the built-in auth +# policies +# executable: path of the auth driver executable, can be an +# absolute path or relative to $ONE_LOCATION/lib/mads (or +# /usr/lib/one/mads/ if OpenNebula was installed in /) +# +# arguments : for the driver executable, can be an absolute path or relative +# to $ONE_LOCATION/etc (or /etc/one/ if OpenNebula was installed +# in /) +#------------------------------------------------------------------------------- + +#AUTH_MAD = [ +# executable = "" ] \ No newline at end of file diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index 9ae00b5a9a..d6ab6b64d3 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -233,19 +233,19 @@ void Nebula::start() vnpool = new VirtualNetworkPool(db,mac_prefix,size); upool = new UserPool(db); - + nebula_configuration->get("IMAGE_REPOSITORY_PATH", repository_path); - + if (repository_path.empty()) // Defaults to ONE_LOCATION/var { repository_path = var_location; } - + nebula_configuration->get("DEFAULT_IMAGE_TYPE", default_image_type); nebula_configuration->get("DEFAULT_DEVICE_PREFIX", default_device_prefix); - - ipool = new ImagePool(db, + + ipool = new ImagePool(db, repository_path, default_image_type, default_device_prefix); @@ -447,6 +447,38 @@ void Nebula::start() throw runtime_error("Could not start the Hook Manager"); } + // ---- Auth Manager ---- + try + { + vector auth_mads; + + nebula_configuration->get("AUTH_MAD", auth_mads); + + if (!auth_mads.empty()) + { + //Defaults 60s to timeout auth requests + authm = new AuthManager(timer_period,60,auth_mads); + } + else + { + authm = 0; //Built-in authm/authz + } + } + catch (bad_alloc&) + { + throw; + } + + if (authm != 0) + { + rc = authm->start(); + + if ( rc != 0 ) + { + throw runtime_error("Could not start the Auth Manager"); + } + } + // ----------------------------------------------------------- // Load mads // ----------------------------------------------------------- @@ -459,6 +491,11 @@ void Nebula::start() tm->load_mads(0); hm->load_mads(0); + if ( authm != 0 ) + { + authm->load_mads(0); + } + // ----------------------------------------------------------- // Wait for a SIGTERM or SIGINT signal // ----------------------------------------------------------- From 284a2db716072f5844023e6623bbd87803b2c444 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 19:29:01 +0200 Subject: [PATCH 14/64] feature #203: Authenticate now uses the Auth Manager infrastructure --- include/User.h | 7 ------- src/nebula/SConstruct | 1 + src/um/User.cc | 15 --------------- src/um/UserPool.cc | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/include/User.h b/include/User.h index 77d536ab51..b4dc75f597 100644 --- a/include/User.h +++ b/include/User.h @@ -50,7 +50,6 @@ public: */ string& to_xml(string& xml) const; - /** * Get the User unique identifier UID, that matches the OID of the object * @return UID User identifier @@ -119,12 +118,6 @@ public: password = _password; }; - /** - * Looks for a match between _password and user password - * @return -1 if disabled or wrong password, uid otherwise - **/ - int authenticate(string _password); - /** * Splits an authentication token (:) * @param secret, the authentication token diff --git a/src/nebula/SConstruct b/src/nebula/SConstruct index b6943c3b91..ebc9f8145b 100644 --- a/src/nebula/SConstruct +++ b/src/nebula/SConstruct @@ -40,6 +40,7 @@ env.Append(LIBS=[ 'nebula_dm', 'nebula_tm', 'nebula_um', + 'nebula_authm', 'nebula_mad', 'nebula_template', 'nebula_image', diff --git a/src/um/User.cc b/src/um/User.cc index abcf2f4ade..85d5693127 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -307,21 +307,6 @@ string& User::to_str(string& str) const /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int User::authenticate(string _password) -{ - if (enabled && _password==password) - { - return oid; - } - else - { - return -1; - } -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - int User::split_secret(const string secret, string& user, string& pass) { size_t pos; diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index f7293ada1b..dc70bd6cc2 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -20,6 +20,8 @@ #include "UserPool.h" #include "NebulaLog.h" +#include "AuthManager.h" +#include "Nebula.h" #include #include @@ -178,8 +180,42 @@ int UserPool::authenticate(string& session) if ( index != known_users.end() ) { - User * user = get((int)index->second,false); - user_id = user->authenticate(password); + User * user = get((int)index->second,true); + + if ( user != 0 ) + { + AuthRequest ar(user->get_uid()); + + Nebula& nd = Nebula::instance(); + AuthManager * authm = nd.get_authm(); + + ar.add_authenticate(user->username, + user->password, + password); + if ( authm == 0 && ar.plain_authenticate() ) + { + user_id = user->get_uid(); + } + else + { + authm->trigger(AuthManager::AUTHENTICATE,&ar); + ar.wait(); + + if (ar.result==true) + { + user_id = user->get_uid(); + } + else + { + ostringstream oss; + oss << "Auth Error: " << ar.message; + + NebulaLog::log("AuM",Log::ERROR,oss); + } + } + + user->unlock(); + } } } From 04a85b90ad4c95bc979637f92b5952ad072b99c4 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 19:45:00 +0200 Subject: [PATCH 15/64] feature #203: New authorize method for users that use the AuthManager subsystem --- include/UserPool.h | 11 ++++++++++- src/um/UserPool.cc | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/include/UserPool.h b/include/UserPool.h index 73ecf072b5..810ffd20fb 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -29,6 +29,8 @@ using namespace std; +class AuthRequest; //Forward definition of AuthRequest + /** * The User Pool class. ... */ @@ -127,10 +129,17 @@ public: /** * Returns whether there is a user with given username/password or not * @param session, colon separated username and password string - * @return -1 if there is no such a user, uid of the user if it exists + * @return -1 if authn failed, uid of the user in other case */ int authenticate(string& session); + /** + * Returns whether there is a user with given username/password or not + * @param ar, an Authorization Request + * @return -1 if authz failed, 0 otherwise + */ + int authorize(AuthRequest& ar); + /** * Dumps the User pool in XML format. A filter can be also added to the * query diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index dc70bd6cc2..4a5e42655c 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -20,8 +20,8 @@ #include "UserPool.h" #include "NebulaLog.h" -#include "AuthManager.h" #include "Nebula.h" +#include "AuthManager.h" #include #include @@ -192,9 +192,12 @@ int UserPool::authenticate(string& session) ar.add_authenticate(user->username, user->password, password); - if ( authm == 0 && ar.plain_authenticate() ) + if (authm == 0) { - user_id = user->get_uid(); + if (ar.plain_authenticate()) + { + user_id = user->get_uid(); + } } else { @@ -225,6 +228,43 @@ int UserPool::authenticate(string& session) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int UserPool::authorize(AuthRequest& ar) +{ + Nebula& nd = Nebula::instance(); + AuthManager * authm = nd.get_authm(); + int rc = -1; + + if (authm == 0) + { + if (ar.plain_authorize()) + { + rc = 0; + } + } + else + { + authm->trigger(AuthManager::AUTHORIZE,&ar); + ar.wait(); + + if (ar.result==true) + { + rc = 0; + } + else + { + ostringstream oss; + oss << "Auth Error: " << ar.message; + + NebulaLog::log("AuM",Log::ERROR,oss); + } + } + + return rc; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int UserPool::dump_cb(void * _oss, int num, char **values, char **names) { ostringstream * oss; From 4a733a8450c74a64687fae6026648196261b20fd Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 Jul 2010 20:21:51 +0200 Subject: [PATCH 16/64] feature #203: Make use of AuthManager in vm.action method --- src/rm/RequestManagerAction.cc | 42 +++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/rm/RequestManagerAction.cc b/src/rm/RequestManagerAction.cc index af279c87cf..6a139e86cb 100644 --- a/src/rm/RequestManagerAction.cc +++ b/src/rm/RequestManagerAction.cc @@ -49,7 +49,6 @@ void RequestManager::VirtualMachineAction::execute( action = xmlrpc_c::value_string(paramList.getString(1)); vid = xmlrpc_c::value_int(paramList.getInt(2)); - // Get the VM vm = VirtualMachineAction::vmpool->get(vid,true); @@ -58,21 +57,34 @@ void RequestManager::VirtualMachineAction::execute( goto error_vm_get; } - uid = vm->get_uid(); - + uid = vm->get_uid(); + vm->unlock(); - // Only oneadmin or the VM owner can perform operations upon the VM + //Authenticate the user rc = VirtualMachineAction::upool->authenticate(session); - - if ( rc != 0 && rc != uid) - { - goto error_authenticate; + + if (rc == -1) + { + goto error_authenticate; } - + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::VM,vid,AuthRequest::MANAGE,uid,false); + + if (upool->authorize(ar) == -1) + { + goto error_authorize; + } + } + if (action == "shutdown") { - rc = dm->shutdown(vid); + rc = dm->shutdown(vid); } else if (action == "hold") { @@ -108,7 +120,7 @@ void RequestManager::VirtualMachineAction::execute( } else { - rc = -3; + rc = -3; } if (rc != 0) @@ -143,7 +155,12 @@ error_vm_get: goto error_common; error_authenticate: - oss << "User not authorized to perform operation upon VirtualMachine [" << vid << "]"; + oss << "Error in user authentication"; + goto error_common; + +error_authorize: + oss << "User not authorized to perform operation upon VirtualMachine [" + << vid << "]"; goto error_common; error_common: @@ -156,7 +173,6 @@ error_common: *retval = arrayresult_error; return; - } /* -------------------------------------------------------------------------- */ From bf1816e0680f4660deccfd4a1ffdc18f21a4b9fe Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 08:14:43 +0200 Subject: [PATCH 17/64] feature #203: New authenticate. --- src/um/UserPool.cc | 124 ++++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 42 deletions(-) diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index 4a5e42655c..3e5d030950 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -167,58 +167,98 @@ int UserPool::authenticate(string& session) { map::iterator index; + User * user = 0; string username; - string password; + string secret, u_pass; + int uid; - int user_id = -1; + int user_id = -1; + int rc; - // session holds username:password + Nebula& nd = Nebula::instance(); + AuthManager * authm = nd.get_authm(); - if ( User::split_secret(session,username,password) == 0 ) + rc = User::split_secret(session,username,secret); + + if ( rc != 0 ) { - index = known_users.find(username); + return -1; + } - if ( index != known_users.end() ) + index = known_users.find(username); + + if ( index != known_users.end() ) //User known to OpenNebula + { + user = get((int)index->second,true); + + if ( user == 0 ) { - User * user = get((int)index->second,true); + return -1; + } - if ( user != 0 ) + u_pass = user->password; + uid = user->get_uid(); + + user->unlock(); + } + else //External User + { + u_pass = "-"; + uid = -1; + } + + AuthRequest ar(uid); + + ar.add_authenticate(username,u_pass,secret); + + if ( uid == 0 ) //oneadmin + { + if (ar.plain_authenticate()) + { + user_id = 0; + } + } + else if (authm == 0) //plain auth + { + if ( user != 0 && ar.plain_authenticate()) //no plain for external users + { + user_id = uid; + } + } + else //use the driver + { + authm->trigger(AuthManager::AUTHENTICATE,&ar); + ar.wait(); + + if (ar.result==true) + { + if ( user != 0 ) //knwon user_id { - AuthRequest ar(user->get_uid()); - - Nebula& nd = Nebula::instance(); - AuthManager * authm = nd.get_authm(); - - ar.add_authenticate(user->username, - user->password, - password); - if (authm == 0) - { - if (ar.plain_authenticate()) - { - user_id = user->get_uid(); - } - } - else - { - authm->trigger(AuthManager::AUTHENTICATE,&ar); - ar.wait(); - - if (ar.result==true) - { - user_id = user->get_uid(); - } - else - { - ostringstream oss; - oss << "Auth Error: " << ar.message; - - NebulaLog::log("AuM",Log::ERROR,oss); - } - } - - user->unlock(); + user_id = uid; } + else //External user, user_id in driver message + { + istringstream is(ar.message); + + if ( is.good() ) + { + is >> user_id; + } + + if ( is.fail() || user_id <= 0 ) + { + ar.message = "Can't convert user_id from driver"; + user_id = -1; + } + } + } + + if (user_id == -1) + { + ostringstream oss; + oss << "Auth Error: " << ar.message; + + NebulaLog::log("AuM",Log::ERROR,oss); } } From c752146975f9ac0eccdbf8ea9ad529fff3f7615e Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 09:14:35 +0200 Subject: [PATCH 18/64] feature #203: Encode object_ids (templates) for CREATE operations --- include/AuthManager.h | 54 +++------------ src/authm/AuthManager.cc | 109 ++++++++++++++++++++++++++++++ src/authm/test/AuthManagerTest.cc | 4 +- src/authm/test/SConstruct | 3 +- 4 files changed, 123 insertions(+), 47 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index 6484d3cd1d..c51bd32164 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -322,50 +322,7 @@ public: const string& ob_id, Operation op, int owner, - bool pub) - { - ostringstream oss; - bool auth = owner == uid; - - switch (ob) - { - case VM: oss << "VM:" ; break; - case HOST: oss << "HOST:" ; break; - case NET: oss << "NET:" ; break; - case IMAGE: oss << "IMAGE:" ; break; - } - - oss << ob_id << ":"; - - switch (op) - { - case CREATE: - oss << "CREATE:" ; - break; - - case DELETE: - oss << "DELETE:" ; - break; - - case USE: - oss << "USE:" ; - if ( ob == NET || ob == IMAGE ) - { - auth = auth || (pub == true); - } - break; - - case MANAGE: - oss << "MANAGE:" ; - break; - } - - oss << owner << ":" << pub; - - self_authorize = self_authorize && auth; - - auths.push_back(oss.str()); - }; + bool pub); /** * Adds a new authorization item to this requests @@ -494,6 +451,15 @@ private: * No actions defined for the Auth request, just FINALIZE when done */ void do_action(const string &name, void *args){}; + + /** + * Base 64 encoding + * @param in the string to encoded + * @return a pointer to the encoded string (must be freed) or 0 in case of + * error + */ + static string * base64_encode(const string& in); + }; #endif /*AUTH_MANAGER_H*/ diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index b0cfe4090b..b1505f0e4d 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -19,6 +19,12 @@ #include "Nebula.h" +#include +#include +#include +#include +#include + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -29,6 +35,109 @@ const char * AuthManager::auth_driver_name = "auth_exe"; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +string * AuthRequest::base64_encode(const string& in) +{ + BIO * bio_mem; + BIO * bio_64; + + char * encoded_c; + long int size; + + bio_64 = BIO_new(BIO_f_base64()); + bio_mem = BIO_new(BIO_s_mem()); + + BIO_push(bio_64, bio_mem); + + BIO_set_flags(bio_64,BIO_FLAGS_BASE64_NO_NL); + + BIO_write(bio_64, in.c_str(), in.length()); + + if (BIO_flush(bio_64) != 1) + { + return 0; + } + + size = BIO_get_mem_data(bio_mem,&encoded_c); + + string * encoded = new string(encoded_c,size); + + BIO_free_all(bio_64); + + return encoded; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AuthRequest::add_auth(Object ob, + const string& ob_id, + Operation op, + int owner, + bool pub) +{ + ostringstream oss; + bool auth = owner == uid; + + switch (ob) + { + case VM: oss << "VM:" ; break; + case HOST: oss << "HOST:" ; break; + case NET: oss << "NET:" ; break; + case IMAGE: oss << "IMAGE:" ; break; + } + + if (op == CREATE) //encode the ob_id, it is a template + { + string * encoded_id = base64_encode(ob_id); + + if (encoded_id != 0) + { + oss << *encoded_id << ":"; + delete (encoded_id); + } + else + { + oss << "-:"; + } + } + else + { + oss << ob_id << ":"; + } + + switch (op) + { + case CREATE: + oss << "CREATE:" ; + break; + + case DELETE: + oss << "DELETE:" ; + break; + + case USE: + oss << "USE:" ; + if ( ob == NET || ob == IMAGE ) + { + auth = auth || (pub == true); + } + break; + + case MANAGE: + oss << "MANAGE:" ; + break; + } + + oss << owner << ":" << pub; + + self_authorize = self_authorize && auth; + + auths.push_back(oss.str()); +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + extern "C" void * authm_action_loop(void *arg) { AuthManager * authm; diff --git a/src/authm/test/AuthManagerTest.cc b/src/authm/test/AuthManagerTest.cc index b916b099ce..c2c13435fe 100644 --- a/src/authm/test/AuthManagerTest.cc +++ b/src/authm/test/AuthManagerTest.cc @@ -172,13 +172,13 @@ public: //OBJECT:OBJECT_ID:ACTION:OWNER:PUBLIC - string astr="VM:dGhpcyBpcyBhIHNhbXBsZSB0ZW1wbGF0ZQ==:CREATE:-1:0 " + string astr="VM:VGhpcyBpcyBhIHRlbXBsYXRlCg==:CREATE:-1:0 " "IMAGE:2:USE:3:0 " "NET:4:DELETE:5:1 " "HOST:6:MANAGE:7:1"; ar.add_auth(AuthRequest::VM, - "dGhpcyBpcyBhIHNhbXBsZSB0ZW1wbGF0ZQ==", + "This is a template\n", AuthRequest::CREATE, -1, false); diff --git a/src/authm/test/SConstruct b/src/authm/test/SConstruct index 1d92845c73..827237c72b 100644 --- a/src/authm/test/SConstruct +++ b/src/authm/test/SConstruct @@ -59,7 +59,8 @@ main_env.Append(LIBS=[ 'nebula_log', 'cppunit', 'dl', - 'pthread' + 'pthread', + 'ssl' ]) # Compile flags From fea2b21e4bf520686391e76ed82a5f4c568391b5 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 09:26:06 +0200 Subject: [PATCH 19/64] feature #203: Fix wrong conflict resolution --- src/host/test/HostPoolTest.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/host/test/HostPoolTest.cc b/src/host/test/HostPoolTest.cc index b3e278c65e..1fda53bb8a 100644 --- a/src/host/test/HostPoolTest.cc +++ b/src/host/test/HostPoolTest.cc @@ -315,10 +315,10 @@ public: } // Discover the enabled hosts - rc = hp->discover(&dh,5); + rc = hp->discover(&dh,100); CPPUNIT_ASSERT(rc == 0); - CPPUNIT_ASSERT(dh.size() == 5); + CPPUNIT_ASSERT(dh.size() == 8); for(i=0,it=dh.begin(),oss.str("");it!=dh.end();it++,i++,oss.str("")) { From 41b676e2c65a17fda72f1be224b70dfad543ea28 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 09:39:37 +0200 Subject: [PATCH 20/64] feature #203: Tests now works with the AuthManager --- share/scons/get_xmlrpc_config | 8 ++++---- share/test/do_tests.sh | 2 +- src/image/test/SConstruct | 4 +++- src/um/test/SConstruct | 4 ++++ src/vm/test/SConstruct | 4 +++- src/vnm/test/SConstruct | 4 ++++ 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/share/scons/get_xmlrpc_config b/share/scons/get_xmlrpc_config index ebbdd45b55..d89e721ed4 100755 --- a/share/scons/get_xmlrpc_config +++ b/share/scons/get_xmlrpc_config @@ -1,7 +1,8 @@ #!/usr/bin/env ruby # -------------------------------------------------------------------------- # -# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# Copyright 2002-2009, Distributed Systems Architecture Group, Universidad # +# Complutense de Madrid (dsa-research.org) # # # # Licensed under the Apache License, Version 2.0 (the "License"); you may # # not use this file except in compliance with the License. You may obtain # @@ -18,8 +19,7 @@ require 'pp' require 'digest/md5' -require 'ftools' - +require 'fileutils' ########################################### # Code to test compilation/linkning flags # @@ -207,7 +207,7 @@ def gen_test_file(kind) #fname="xmlrpc_test.cc" full_path=dir+"/"+fname - File.makedirs(dir) + FileUtils::mkdir_p(dir) f=open(full_path, "w") f.write(TestCode[kind]) f.close diff --git a/share/test/do_tests.sh b/share/test/do_tests.sh index 5bfedf7b97..55b6ca0d3d 100755 --- a/share/test/do_tests.sh +++ b/share/test/do_tests.sh @@ -78,7 +78,7 @@ elif [ "$VAL_CALL" = "yes" ] ; then CALLER="valgrind --tool=callgrind" fi -TESTS=`find $TWD_DIR -name test -type d` +TESTS=`find $TWD_DIR -name test -type d | grep -v ruby` for i in $TESTS ; do cd $BASE_DIR diff --git a/src/image/test/SConstruct b/src/image/test/SConstruct index bf11121dbb..34f67f44a3 100644 --- a/src/image/test/SConstruct +++ b/src/image/test/SConstruct @@ -51,6 +51,7 @@ main_env.Append(LIBPATH=[ cwd + '/src/mad', cwd + '/src/vnm', cwd + '/src/um', + cwd + '/src/authm', cwd + '/src/image', '/usr/include/openssl/', ]) @@ -59,11 +60,12 @@ main_env.Append(LIBS=[ 'nebula_image', 'nebula_um', 'nebula_vm', - 'nebula_mad', 'nebula_hm', 'nebula_vnm', + 'nebula_authm', 'nebula_template', 'nebula_pool', + 'nebula_mad', 'nebula_common', 'nebula_log', 'nebula_core', diff --git a/src/um/test/SConstruct b/src/um/test/SConstruct index 2b6eb98931..fcce418e74 100644 --- a/src/um/test/SConstruct +++ b/src/um/test/SConstruct @@ -41,10 +41,12 @@ main_env.Append(CPPPATH=[ # Library dirs main_env.Append(LIBPATH=[ cwd + '/src/common', + cwd + '/src/mad', cwd + '/src/log', cwd + '/src/nebula', cwd + '/src/pool', cwd + '/src/sql', + cwd + '/src/authm', cwd + '/src/um', '/usr/include/openssl/', ]) @@ -53,7 +55,9 @@ main_env.Append(LIBS=[ 'nebula_um', 'nebula_pool', 'nebula_log', + 'nebula_authm', 'nebula_common', + 'nebula_mad', 'nebula_core', 'nebula_sql', 'cppunit', diff --git a/src/vm/test/SConstruct b/src/vm/test/SConstruct index 9124675c1b..362a0dd4ea 100644 --- a/src/vm/test/SConstruct +++ b/src/vm/test/SConstruct @@ -48,6 +48,7 @@ main_env.Append(LIBPATH=[ cwd + '/src/template', cwd + '/src/vm', cwd + '/src/hm', + cwd + '/src/authm', cwd + '/src/mad', cwd + '/src/vnm', cwd + '/src/um', @@ -58,12 +59,13 @@ main_env.Append(LIBPATH=[ main_env.Append(LIBS=[ 'nebula_um', 'nebula_vm', - 'nebula_mad', 'nebula_hm', 'nebula_vnm', + 'nebula_authm', 'nebula_template', 'nebula_pool', 'nebula_image', + 'nebula_mad', 'nebula_common', 'nebula_log', 'nebula_core', diff --git a/src/vnm/test/SConstruct b/src/vnm/test/SConstruct index 221228318e..811f856d6b 100644 --- a/src/vnm/test/SConstruct +++ b/src/vnm/test/SConstruct @@ -40,7 +40,9 @@ main_env.Append(CPPPATH=[ # Library dirs main_env.Append(LIBPATH=[ + cwd + '/src/authm', cwd + '/src/common', + cwd + '/src/mad', cwd + '/src/nebula', cwd + '/src/sql', cwd + '/src/log', @@ -54,9 +56,11 @@ main_env.Append(LIBS=[ 'nebula_log', 'nebula_um', 'nebula_vnm', + 'nebula_authm', 'nebula_template', 'nebula_pool', 'nebula_common', + 'nebula_mad', 'nebula_core', 'nebula_sql', 'cppunit', From f9dbce8935e372223b5d0f44610d954c67212ed5 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 09:42:17 +0200 Subject: [PATCH 21/64] feature #203: Restored original scons helpers --- share/scons/get_xmlrpc_config | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/share/scons/get_xmlrpc_config b/share/scons/get_xmlrpc_config index d89e721ed4..ebbdd45b55 100755 --- a/share/scons/get_xmlrpc_config +++ b/share/scons/get_xmlrpc_config @@ -1,8 +1,7 @@ #!/usr/bin/env ruby # -------------------------------------------------------------------------- # -# Copyright 2002-2009, Distributed Systems Architecture Group, Universidad # -# Complutense de Madrid (dsa-research.org) # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # # # # Licensed under the Apache License, Version 2.0 (the "License"); you may # # not use this file except in compliance with the License. You may obtain # @@ -19,7 +18,8 @@ require 'pp' require 'digest/md5' -require 'fileutils' +require 'ftools' + ########################################### # Code to test compilation/linkning flags # @@ -207,7 +207,7 @@ def gen_test_file(kind) #fname="xmlrpc_test.cc" full_path=dir+"/"+fname - FileUtils::mkdir_p(dir) + File.makedirs(dir) f=open(full_path, "w") f.write(TestCode[kind]) f.close From edea2edf2bc0127c9caa10517415d19ae9621ea9 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 12:10:05 +0200 Subject: [PATCH 22/64] feature #203: Integrated VM allocation with AuthManager --- include/Image.h | 2 +- include/ImagePool.h | 28 ++--------------- include/UserPool.h | 2 +- include/VirtualMachine.h | 6 ++-- include/VirtualNetwork.h | 11 ++++++- include/VirtualNetworkPool.h | 27 ++--------------- src/image/ImagePool.cc | 35 ++++++++++++++++++++++ src/rm/RequestManagerAction.cc | 3 +- src/rm/RequestManagerAllocate.cc | 40 +++---------------------- src/rm/RequestManagerImageInfo.cc | 50 +++++++++++++++---------------- src/vm/VirtualMachine.cc | 39 ++++++++++++++++++++---- src/vnm/VirtualNetworkPool.cc | 38 +++++++++++++++++++++++ 12 files changed, 158 insertions(+), 123 deletions(-) diff --git a/include/Image.h b/include/Image.h index 5d6258a151..aa87243c2a 100644 --- a/include/Image.h +++ b/include/Image.h @@ -105,7 +105,7 @@ public: * Returns true if the image is public * @return true if the image is public */ - bool is_public() + bool isPublic() { return (public_img == 1); }; diff --git a/include/ImagePool.h b/include/ImagePool.h index d46c09c4ae..79b46705b4 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -29,6 +29,8 @@ using namespace std; +class AuthRequest; + /** * The Image Pool class. */ @@ -168,31 +170,7 @@ public: * @param disk the disk to be generated * @return 0 on success, -1 error, -2 not using the pool */ - int disk_attribute(VectorAttribute * disk, int * index) - { - string source; - Image * img; - - source = disk->vector_value("NAME"); - - if (source.empty()) - { - return -2; - } - - img = get(source,true); - - if (img == 0) - { - return -1; - } - - int rc = img->disk_attribute(disk,index); - - img->unlock(); - - return rc; - } + int disk_attribute(VectorAttribute * disk, int * index, AuthRequest * ar); static const string& source_prefix() { diff --git a/include/UserPool.h b/include/UserPool.h index 810ffd20fb..1c4fc3eb49 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -138,7 +138,7 @@ public: * @param ar, an Authorization Request * @return -1 if authz failed, 0 otherwise */ - int authorize(AuthRequest& ar); + static int authorize(AuthRequest& ar); /** * Dumps the User pool in XML format. A filter can be also added to the diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index b56f4bb8ef..3f942c8bda 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -31,6 +31,8 @@ using namespace std; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +class AuthRequest; + /** * The Virtual Machine class. It represents a VM... */ @@ -702,7 +704,7 @@ public: * Get all network leases for this Virtual Machine * @return 0 if success */ - int get_network_leases(); + int get_network_leases(AuthRequest *ar); /** * Releases all network leases taken by this Virtual Machine @@ -713,7 +715,7 @@ public: * Get all disk images for this Virtual Machine * @return 0 if success */ - int get_disk_images(); + int get_disk_images(AuthRequest *ar); /** * Releases all disk images taken by this Virtual Machine diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 87841fb94e..5d8ac3c8a7 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -58,6 +58,15 @@ public: // Virtual Network Public Methods // ************************************************************************* + /** + * Get the Vnet unique identifier VNID, that matches the OID of the object + * @return VNID Image identifier + */ + int get_vnid() const + { + return oid; + }; + /** * Gets the uid of the owner of the Virtual Network * @return uid @@ -71,7 +80,7 @@ public: * Returns true if the Virtual Network is public * @return true if the Virtual Network is public */ - bool is_public() + bool isPublic() { return (public_vnet == 1); }; diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index c5e0dd0816..57e7db8d3f 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -24,6 +24,7 @@ using namespace std; +class AuthRequest; /** * The Virtual Network Pool class. ... @@ -87,31 +88,7 @@ public: * @param vid of the VM requesting the lease * @return 0 on success, -1 error, -2 not using the pool */ - int nic_attribute(VectorAttribute * nic, int vid) - { - string network; - VirtualNetwork * vnet; - - network = nic->vector_value("NETWORK"); - - if (network.empty()) - { - return -2; - } - - vnet = get(network,true); - - if (vnet == 0) - { - return -1; - } - - int rc = vnet->nic_attribute(nic,vid); - - vnet->unlock(); - - return rc; - } + int nic_attribute(VectorAttribute * nic, int vid, AuthRequest *ar); /** * Updates the template of a VN, adding a new attribute (replacing it if diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index da71fb9d98..367409cf56 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -19,6 +19,7 @@ /* ************************************************************************** */ #include "ImagePool.h" +#include "AuthManager.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -187,3 +188,37 @@ int ImagePool::dump(ostringstream& oss, const string& where) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int ImagePool::disk_attribute(VectorAttribute * disk, + int * index, + AuthRequest * ar) +{ + string source; + Image * img; + + source = disk->vector_value("NAME"); + + if (source.empty()) + { + return -2; + } + + img = get(source,true); + + if (img == 0) + { + return -1; + } + + int rc = img->disk_attribute(disk,index); + + ar->add_auth(AuthRequest::IMAGE, + img->get_iid(), + AuthRequest::USE, + img->get_uid(), + img->isPublic()); + + img->unlock(); + + return rc; +} \ No newline at end of file diff --git a/src/rm/RequestManagerAction.cc b/src/rm/RequestManagerAction.cc index 6a139e86cb..a9be238951 100644 --- a/src/rm/RequestManagerAction.cc +++ b/src/rm/RequestManagerAction.cc @@ -18,6 +18,7 @@ #include "NebulaLog.h" #include "Nebula.h" +#include "UserPool.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -76,7 +77,7 @@ void RequestManager::VirtualMachineAction::execute( ar.add_auth(AuthRequest::VM,vid,AuthRequest::MANAGE,uid,false); - if (upool->authorize(ar) == -1) + if (UserPool::authorize(ar) == -1) { goto error_authorize; } diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 285f6c4d9b..59375783c1 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -27,55 +27,34 @@ void RequestManager::VirtualMachineAllocate::execute( xmlrpc_c::value * const retval) { string session; - string username; - string password; string vm_template; int vid; - int uid; int rc; Nebula& nd = Nebula::instance(); DispatchManager * dm = nd.get_dm(); - User * user; - ostringstream oss; vector arrayData; xmlrpc_c::value_array * arrayresult; - NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineAllocate invoked"); session = xmlrpc_c::value_string(paramList.getString(0)); vm_template = xmlrpc_c::value_string(paramList.getString(1)); vm_template += "\n"; - - // First, we need to authenticate the user + //Authenticate the user rc = VirtualMachineAllocate::upool->authenticate(session); - if ( rc == -1 ) + if (rc == -1) { goto error_authenticate; } - User::split_secret(session,username,password); - - // Now let's get the user - user = VirtualMachineAllocate::upool->get(username,true); - - if ( user == 0 ) - { - goto error_get_user; - } - - uid = user->get_uid(); - - user->unlock(); - - rc = dm->allocate(uid,vm_template,&vid); + rc = dm->allocate(rc,vm_template,&vid); if ( rc < 0 ) { @@ -99,19 +78,8 @@ error_authenticate: oss << "User not authenticated, aborting RequestManagerAllocate call."; goto error_common; -error_get_user: - oss << "User not recognized, cannot allocate VirtualMachine"; - goto error_common; - error_allocate: - if (rc == -1) - { - oss << "Error inserting VM in the database, check oned.log"; - } - else - { - oss << "Error parsing VM template"; - } + oss << "Error inserting VM in the database, check oned.log"; goto error_common; error_common: diff --git a/src/rm/RequestManagerImageInfo.cc b/src/rm/RequestManagerImageInfo.cc index 37beb6927c..b16860a7a5 100644 --- a/src/rm/RequestManagerImageInfo.cc +++ b/src/rm/RequestManagerImageInfo.cc @@ -23,14 +23,14 @@ void RequestManager::ImageInfo::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; - int iid; - int uid; + int iid; + int uid; int rc; Image * image; - + ostringstream oss; /* -- RPC specific vars -- */ @@ -42,30 +42,30 @@ void RequestManager::ImageInfo::execute( // Get the parameters session = xmlrpc_c::value_string(paramList.getString(0)); iid = xmlrpc_c::value_int (paramList.getInt(1)); - + // Get image from the ImagePool - image = ImageInfo::ipool->get(iid,true); - - if ( image == 0 ) - { - goto error_image_get; + image = ImageInfo::ipool->get(iid,true); + + if ( image == 0 ) + { + goto error_image_get; } - + uid = image->get_uid(); // Check if it is a valid user rc = ImageInfo::upool->authenticate(session); - if ( rc != 0 && rc != uid && !image->is_public()) - { - goto error_authenticate; + if ( rc != 0 && rc != uid && !image->isPublic()) + { + goto error_authenticate; } - + oss << *image; - + image->unlock(); - - // All nice, return the host info to the client + + // All nice, return the host info to the client arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS arrayData.push_back(xmlrpc_c::value_string(oss.str())); @@ -76,13 +76,13 @@ void RequestManager::ImageInfo::execute( delete arrayresult; // and get rid of the original return; - + error_image_get: - oss << "Error getting image with ID = " << iid; + oss << "Error getting image with ID = " << iid; goto error_common; error_authenticate: - oss << "User doesn't exist, or not authorized to use image with " << + oss << "User doesn't exist, or not authorized to use image with " << "ID = " << iid << " , ImageInfo call aborted."; image->unlock(); goto error_common; @@ -91,13 +91,13 @@ error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - + + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index a81b68a661..d538f12eb5 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -26,6 +26,7 @@ #include "VirtualMachine.h" #include "VirtualNetworkPool.h" #include "NebulaLog.h" +#include "AuthManager.h" #include "Nebula.h" @@ -267,6 +268,8 @@ int VirtualMachine::insert(SqlDB * db) string value; ostringstream oss; + AuthRequest ar(uid); + // ----------------------------------------------------------------------- // Set a template ID if it wasn't already assigned // ------------------------------------------------------------------------ @@ -303,7 +306,7 @@ int VirtualMachine::insert(SqlDB * db) // Get network leases // ------------------------------------------------------------------------ - rc = get_network_leases(); + rc = get_network_leases(&ar); if ( rc != 0 ) { @@ -314,7 +317,7 @@ int VirtualMachine::insert(SqlDB * db) // Get disk images // ------------------------------------------------------------------------ - rc = get_disk_images(); + rc = get_disk_images(&ar); if ( rc != 0 ) { @@ -341,6 +344,26 @@ int VirtualMachine::insert(SqlDB * db) parse_graphics(); + // ------------------------------------------------------------------------ + // Authorize this request + // ------------------------------------------------------------------------ + + if ( uid != 0 ) // uid == 0 means oneadmin + { + string t64; + + ar.add_auth(AuthRequest::VM, + vm_template.to_xml(t64), + AuthRequest::CREATE, + uid, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // ------------------------------------------------------------------------ // Insert the template first, so we get a valid template ID. Then the VM // ------------------------------------------------------------------------ @@ -385,6 +408,10 @@ error_context: error_requirements: NebulaLog::log("ONE",Log::ERROR, "Could not parse REQUIREMENTS for VM"); + goto error_common; + +error_authorize: + NebulaLog::log("ONE",Log::ERROR, "Error authorizing VM creation"); error_common: release_network_leases(); @@ -792,7 +819,7 @@ void VirtualMachine::get_requirements (int& cpu, int& memory, int& disk) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualMachine::get_disk_images() +int VirtualMachine::get_disk_images(AuthRequest *ar) { int num_disks, rc; vector disks; @@ -814,7 +841,7 @@ int VirtualMachine::get_disk_images() continue; } - rc = ipool->disk_attribute(disk, &index); + rc = ipool->disk_attribute(disk, &index, ar); if (rc == -1) // 0 OK, -2 not using the Image pool { @@ -875,7 +902,7 @@ void VirtualMachine::release_disk_images() /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualMachine::get_network_leases() +int VirtualMachine::get_network_leases(AuthRequest *ar) { int num_nics, rc; vector nics; @@ -896,7 +923,7 @@ int VirtualMachine::get_network_leases() continue; } - rc = vnpool->nic_attribute(nic, oid); + rc = vnpool->nic_attribute(nic, oid, ar); if (rc == -1) { diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index 80e5620a4d..513fb138b4 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -16,6 +16,7 @@ #include "VirtualNetworkPool.h" #include "NebulaLog.h" +#include "AuthManager.h" #include #include @@ -214,3 +215,40 @@ int VirtualNetworkPool::dump(ostringstream& oss, const string& where) return rc; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, + int vid, + AuthRequest * ar) +{ + string network; + VirtualNetwork * vnet; + + network = nic->vector_value("NETWORK"); + + if (network.empty()) + { + return -2; + } + + vnet = get(network,true); + + if (vnet == 0) + { + return -1; + } + + int rc = vnet->nic_attribute(nic,vid); + + ar->add_auth(AuthRequest::NET, + vnet->get_vnid(), + AuthRequest::USE, + vnet->get_uid(), + vnet->isPublic()); + + vnet->unlock(); + + return rc; +} \ No newline at end of file From 3fdd16f0940ab8ada341b1fae4e0b31d1df25682 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 12:43:20 +0200 Subject: [PATCH 23/64] feauture #203: Integrated VM operations with AuthManager --- src/rm/RequestManagerAction.cc | 4 +- src/rm/RequestManagerAllocate.cc | 2 +- src/rm/RequestManagerCancel.cc | 75 +++++++++++++++++++------------- src/rm/RequestManagerDeploy.cc | 68 ++++++++++++++++------------- src/rm/RequestManagerInfo.cc | 43 +++++++++++------- src/rm/RequestManagerMigrate.cc | 48 ++++++++++++-------- src/rm/RequestManagerPoolInfo.cc | 56 ++++++++---------------- 7 files changed, 158 insertions(+), 138 deletions(-) diff --git a/src/rm/RequestManagerAction.cc b/src/rm/RequestManagerAction.cc index a9be238951..27a24b798e 100644 --- a/src/rm/RequestManagerAction.cc +++ b/src/rm/RequestManagerAction.cc @@ -127,7 +127,6 @@ void RequestManager::VirtualMachineAction::execute( if (rc != 0) { goto error_operation; - } arrayData.push_back(xmlrpc_c::value_boolean(true)); @@ -160,8 +159,7 @@ error_authenticate: goto error_common; error_authorize: - oss << "User not authorized to perform operation upon VirtualMachine [" - << vid << "]"; + oss << "User not authorized to perform VM operation"; goto error_common; error_common: diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 59375783c1..84f5556838 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -75,7 +75,7 @@ void RequestManager::VirtualMachineAllocate::execute( return; error_authenticate: - oss << "User not authenticated, aborting RequestManagerAllocate call."; + oss << "Error in user authentication"; goto error_common; error_allocate: diff --git a/src/rm/RequestManagerCancel.cc b/src/rm/RequestManagerCancel.cc index 2175ee720f..91178be808 100644 --- a/src/rm/RequestManagerCancel.cc +++ b/src/rm/RequestManagerCancel.cc @@ -23,21 +23,21 @@ void RequestManager::VirtualMachineCancel::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; // of the vid to retrieve the information for - int vid; + int vid; int uid; VirtualMachine * vm; - + ostringstream oss; /* -- RPC specific vars -- */ vector arrayData; xmlrpc_c::value_array * arrayresult; - + Nebula& nd = Nebula::instance(); DispatchManager * dm = nd.get_dm(); @@ -47,62 +47,75 @@ void RequestManager::VirtualMachineCancel::execute( session = xmlrpc_c::value_string(paramList.getString(0)); vid = xmlrpc_c::value_int (paramList.getInt(1)); - - // Retrieve the VM from the vmpool + // Retrieve the VM from the vmpool vm = VirtualMachineCancel::vmpool->get(vid,true); - - if ( vm == 0 ) - { - goto error_vm_get; + + if ( vm == 0 ) + { + goto error_vm_get; } - + uid = vm->get_uid(); vm->unlock(); - // Only oneadmin or the VM owner can perform operations upon the VM + //Authenticate the user rc = VirtualMachineCancel::upool->authenticate(session); - - if ( rc != 0 && rc != uid) - { - goto error_authenticate; + + if ( rc == -1 ) + { + goto error_authenticate; } - + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::VM,vid,AuthRequest::MANAGE,uid,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + //Cancel the VM - dm->cancel(vid); - + // Send results to client - arrayData.push_back(xmlrpc_c::value_boolean(true)); - + arrayresult = new xmlrpc_c::value_array(arrayData); - + *retval = *arrayresult; - + delete arrayresult; - + return; error_authenticate: - oss << "User not authorized to cancel VM"; + oss << "Error in user authentication"; + goto error_common; + +error_authorize: + oss << "User not authorized to canel VM"; goto error_common; error_vm_get: - oss << "Error getting vm for cancelling with VID = " << vid; + oss << "Error getting vm for cancelling with VID = " << vid; goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - Nebula::log("ReM",Log::ERROR,oss); - + + Nebula::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/rm/RequestManagerDeploy.cc b/src/rm/RequestManagerDeploy.cc index 8b49d42ed7..d31a5d9460 100644 --- a/src/rm/RequestManagerDeploy.cc +++ b/src/rm/RequestManagerDeploy.cc @@ -56,10 +56,7 @@ void RequestManager::VirtualMachineDeploy::execute( vid = xmlrpc_c::value_int(paramList.getInt(1)); hid = xmlrpc_c::value_int(paramList.getInt(2)); - //We also need the hsid - //Get host info to deploy the VM - host = VirtualMachineDeploy::hpool->get(hid,true); if ( host == 0 ) @@ -76,7 +73,6 @@ void RequestManager::VirtualMachineDeploy::execute( host->unlock(); //Get the VM - vm = VirtualMachineDeploy::vmpool->get(vid,true); if ( vm == 0 ) @@ -86,21 +82,34 @@ void RequestManager::VirtualMachineDeploy::execute( uid = vm->get_uid(); - // Only oneadmin or the VM owner can perform operations upon the VM - rc = VirtualMachineDeploy::upool->authenticate(session); - - if ( rc != 0 && rc != uid) - { - goto error_authenticate; - } - if ( vm->get_state() != VirtualMachine::PENDING ) { goto error_state; } - //Update host info and share usage (cpu,mem....) + //Authenticate the user + rc = VirtualMachineDeploy::upool->authenticate(session); + if ( rc == -1 ) + { + goto error_authenticate; + } + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::VM,vid,AuthRequest::MANAGE,uid,false); + ar.add_auth(AuthRequest::HOST,hid,AuthRequest::USE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + //Update host info and share usage (cpu,mem....) vm->add_history(hid,hostname,vmdir,vmm_mad,tm_mad); rc = VirtualMachineDeploy::vmpool->update_history(vm); @@ -113,13 +122,11 @@ void RequestManager::VirtualMachineDeploy::execute( vmpool->update(vm); //Insert last_seq in the DB //Deploy the VM - dm->deploy(vm); vm->unlock(); // Send results to client - arrayData.push_back(xmlrpc_c::value_boolean(true)); arrayresult = new xmlrpc_c::value_array(arrayData); @@ -130,11 +137,6 @@ void RequestManager::VirtualMachineDeploy::execute( return; -error_authenticate: - vm->unlock(); - oss << "User not authorized to perform the deploy"; - goto error_common; - error_host_get: oss << "The host " << hid << " does not exists"; goto error_common; @@ -143,20 +145,26 @@ error_vm_get: oss << "The virtual machine " << vid << " does not exists"; goto error_common; -error_history: - vm->unlock(); - - oss << "Can not deploy VM " << vid << ", can not insert history"; - goto error_common; - error_state: - vm->unlock(); + oss << "Can not deploy VM, wrong state"; + goto error_common_lock; - oss << "Can not deploy VM " << vid << ", wrong state"; - goto error_common; +error_authenticate: + oss << "Error in user authentication"; + goto error_common_lock; + +error_authorize: + oss << "User not authorized to deploy VM on host"; + goto error_common_lock; + +error_history: + oss << "Can not insert history to deploy VM"; + goto error_common_lock; + +error_common_lock: + vm->unlock(); error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerInfo.cc b/src/rm/RequestManagerInfo.cc index 35574ad663..d53624efe8 100644 --- a/src/rm/RequestManagerInfo.cc +++ b/src/rm/RequestManagerInfo.cc @@ -23,10 +23,10 @@ void RequestManager::VirtualMachineInfo::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; - int vid; + int vid, rc; VirtualMachine * vm; ostringstream oss; @@ -41,19 +41,27 @@ void RequestManager::VirtualMachineInfo::execute( session = xmlrpc_c::value_string(paramList.getString(0)); vid = xmlrpc_c::value_int (paramList.getInt(1)); - // Get the details of the virtual machine + // Check if it is a valid user + rc = VirtualMachineInfo::upool->authenticate(session); + + if ( rc == -1 ) + { + goto error_authenticate; + } + + // Get the details of the virtual machine vm = VirtualMachineInfo::vmpool->get(vid,true); - if ( vm == 0 ) - { - goto error_vm_get; + if ( vm == 0 ) + { + goto error_vm_get; } - + oss << *vm; - + vm->unlock(); - - // All nice, return the vm info to the client + + // All nice, return the vm info to the client arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS arrayData.push_back(xmlrpc_c::value_string(oss.str())); @@ -65,21 +73,24 @@ void RequestManager::VirtualMachineInfo::execute( return; +error_authenticate: + oss << "Error in user authentication"; + goto error_common; + error_vm_get: - oss << "Error getting vm with VID = " << vid; + oss << "Error getting VM " << vid; goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - + + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/rm/RequestManagerMigrate.cc b/src/rm/RequestManagerMigrate.cc index 5b60552dcd..11088a6ddd 100644 --- a/src/rm/RequestManagerMigrate.cc +++ b/src/rm/RequestManagerMigrate.cc @@ -52,14 +52,12 @@ void RequestManager::VirtualMachineMigrate::execute( NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineMigrate invoked"); //Parse Arguments - session = xmlrpc_c::value_string(paramList.getString(0)); vid = xmlrpc_c::value_int(paramList.getInt(1)); hid = xmlrpc_c::value_int(paramList.getInt(2)); live = xmlrpc_c::value_boolean(paramList.getBoolean(3)); - //Get host info to deploy the VM - + //Get host info to migrate the VM host = VirtualMachineMigrate::hpool->get(hid,true); if ( host == 0 ) @@ -76,7 +74,6 @@ void RequestManager::VirtualMachineMigrate::execute( host->unlock(); //Get the VM and migrate it - vm = VirtualMachineMigrate::vmpool->get(vid,true); if ( vm == 0 ) @@ -89,11 +86,25 @@ void RequestManager::VirtualMachineMigrate::execute( // Only oneadmin or the VM owner can perform operations upon the VM rc = VirtualMachineMigrate::upool->authenticate(session); - if ( rc != 0 && rc != uid) + if ( rc == -1) { goto error_authenticate; } + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::VM,vid,AuthRequest::MANAGE,uid,false); + ar.add_auth(AuthRequest::HOST,hid,AuthRequest::USE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + if ((vm->get_state() != VirtualMachine::ACTIVE) || (vm->get_lcm_state() != VirtualMachine::RUNNING)) { @@ -123,7 +134,6 @@ void RequestManager::VirtualMachineMigrate::execute( vm->unlock(); // Send results to client - arrayData.push_back(xmlrpc_c::value_boolean(true)); arrayresult = new xmlrpc_c::value_array(arrayData); @@ -134,10 +144,6 @@ void RequestManager::VirtualMachineMigrate::execute( return; -error_authenticate: - vm->unlock(); - oss << "User not authorized to perform migration upon this VM"; - goto error_common; error_host_get: oss << "The host " << hid << " does not exists"; @@ -147,20 +153,26 @@ error_vm_get: oss << "The virtual machine " << vid << " does not exists"; goto error_common; -error_history: - vm->unlock(); +error_authenticate: + oss << "Error in user authentication"; + goto error_common_lock; - oss << "Can not migrate VM " << vid << ", can not insert history"; - goto error_common; +error_authorize: + oss << "User not authorized to migrate VM on host"; + goto error_common_lock; + +error_history: + oss << "Can not insert history to migrate VM"; + goto error_common_lock; error_state: - vm->unlock(); + oss << "Can not migrate VM, wrong state"; + goto error_common_lock; - oss << "Can not migrate VM " << vid << ", wrong state"; - goto error_common; +error_common_lock: + vm->unlock(); error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerPoolInfo.cc b/src/rm/RequestManagerPoolInfo.cc index 7461ff9d39..6196da19c7 100644 --- a/src/rm/RequestManagerPoolInfo.cc +++ b/src/rm/RequestManagerPoolInfo.cc @@ -23,19 +23,17 @@ void RequestManager::VirtualMachinePoolInfo::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; string username; string password; int filter_flag; int rc; - + ostringstream oss; ostringstream where_string; - User * user; - /* -- RPC specific vars -- */ vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -54,43 +52,28 @@ void RequestManager::VirtualMachinePoolInfo::execute( goto error_authenticate; } - where_string.str(""); - - /** Filter flag meaning table + /* Filter flag meaning table * <=-2 :: ALL VMs * -1 :: User's VMs * >=0 :: UID User's VMs - **/ + */ if (filter_flag == -1) { - User::split_secret(session,username,password); - - // Now let's get the user - user = VirtualMachinePoolInfo::upool->get(username,true); - - if ( user == 0 ) - { - goto error_get_user; - } - - where_string << "UID=" << user->get_uid(); - - user->unlock(); + where_string << "UID=" << rc; } else if (filter_flag>=0) - { - where_string << "UID=" << filter_flag; - } + { + where_string << "UID=" << filter_flag; + } - // Perform the allocation in the vmpool rc = VirtualMachinePoolInfo::vmpool->dump(oss,where_string.str()); - + if ( rc != 0 ) - { + { goto error_dump; } - - // All nice, return the vm info to the client + + // All nice, return the vm info to the client arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS arrayData.push_back(xmlrpc_c::value_string(oss.str())); @@ -103,11 +86,7 @@ void RequestManager::VirtualMachinePoolInfo::execute( return; error_authenticate: - oss << "User not authenticated, aborting RequestManagerPoolInfo call."; - goto error_common; - -error_get_user: - oss << "An error ocurred getting the user from the UserPool, aborting RequestManagerPoolInfo call"; + oss << "Error in user authentication"; goto error_common; error_dump: @@ -115,18 +94,17 @@ error_dump: goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - + NebulaLog::log("ReM",Log::ERROR,oss); - + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } /* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ \ No newline at end of file From 91722661a07ea9469c8d1013df39423aaae82835 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 13:09:55 +0200 Subject: [PATCH 24/64] feature #203: Hosts can be used by anybody in plain auth --- src/authm/AuthManager.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index b1505f0e4d..8879f588a7 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -121,6 +121,10 @@ void AuthRequest::add_auth(Object ob, { auth = auth || (pub == true); } + else if (ob == HOST) + { + auth = true; + } break; case MANAGE: From 1953d814f6fe970465908de190093c632ec9dc69 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 Jul 2010 13:38:23 +0200 Subject: [PATCH 25/64] feature #203: Hosts use AuthManager --- src/rm/RequestManagerAction.cc | 1 - src/rm/RequestManagerHostAllocate.cc | 67 +++++++++++++++++----------- src/rm/RequestManagerHostDelete.cc | 61 ++++++++++++++++--------- src/rm/RequestManagerHostEnable.cc | 62 ++++++++++++++++--------- src/rm/RequestManagerHostInfo.cc | 38 ++++++++-------- src/rm/RequestManagerHostPoolInfo.cc | 31 +++++++------ 6 files changed, 156 insertions(+), 104 deletions(-) diff --git a/src/rm/RequestManagerAction.cc b/src/rm/RequestManagerAction.cc index 27a24b798e..cb9032de67 100644 --- a/src/rm/RequestManagerAction.cc +++ b/src/rm/RequestManagerAction.cc @@ -18,7 +18,6 @@ #include "NebulaLog.h" #include "Nebula.h" -#include "UserPool.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerHostAllocate.cc b/src/rm/RequestManagerHostAllocate.cc index 7d3393f4d0..c78971497f 100644 --- a/src/rm/RequestManagerHostAllocate.cc +++ b/src/rm/RequestManagerHostAllocate.cc @@ -17,23 +17,25 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void RequestManager::HostAllocate::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; string hostname; - string im_mad_name; - string vmm_mad_name; + string im_mad_name; + string vmm_mad_name; string tm_mad_name; - int hid; + int hid; - int rc; + int rc; ostringstream oss; /* -- RPC specific vars -- */ @@ -49,25 +51,39 @@ void RequestManager::HostAllocate::execute( vmm_mad_name = xmlrpc_c::value_string(paramList.getString(3)); tm_mad_name = xmlrpc_c::value_string(paramList.getString(4)); - // Only oneadmin can add new hosts + //Authenticate the user rc = HostAllocate::upool->authenticate(session); - - if ( rc != 0 ) - { - goto error_authenticate; + + if ( rc == -1 ) + { + goto error_authenticate; } + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::HOST,-1,AuthRequest::CREATE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // Perform the allocation in the hostpool rc = HostAllocate::hpool->allocate(&hid, hostname, - im_mad_name, + im_mad_name, vmm_mad_name, - tm_mad_name); - if ( rc == -1 ) - { - goto error_host_allocate; + tm_mad_name); + if ( rc == -1 ) + { + goto error_host_allocate; } - - // All nice, return the new hid to client + + // All nice, return the new hid to client arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS arrayData.push_back(xmlrpc_c::value_int(hid)); arrayresult = new xmlrpc_c::value_array(arrayData); @@ -79,25 +95,26 @@ void RequestManager::HostAllocate::execute( return; error_authenticate: - oss << "User not authorized to add new hosts"; + oss << "Error in user authentication"; + goto error_common; + +error_authorize: + oss << "User not authorized to allocate a new HOST"; goto error_common; error_host_allocate: - oss << "Can not allocate host " << hostname << - " in the HostPool, returned error code [" << rc << "]"; + oss << "Error inserting HOST in the database, check oned.log"; goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - + + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - return; } diff --git a/src/rm/RequestManagerHostDelete.cc b/src/rm/RequestManagerHostDelete.cc index 9bafd227d2..4bd0c50333 100644 --- a/src/rm/RequestManagerHostDelete.cc +++ b/src/rm/RequestManagerHostDelete.cc @@ -17,17 +17,19 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void RequestManager::HostDelete::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; // of the host to delete from the HostPool - int hid; + int hid; Host * host; ostringstream oss; int rc; @@ -41,31 +43,44 @@ void RequestManager::HostDelete::execute( // Get the parameters session = xmlrpc_c::value_string(paramList.getString(0)); hid = xmlrpc_c::value_int (paramList.getInt(1)); - - // Only oneadmin can delete hosts + + //Authenticate the user rc = HostDelete::upool->authenticate(session); - - if ( rc != 0 ) - { - goto error_authenticate; + + if ( rc == -1 ) + { + goto error_authenticate; } - - // Perform the allocation in the hostpool - host = HostDelete::hpool->get(hid,true); - - if ( host == 0 ) - { - goto error_host_get; + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::HOST,hid,AuthRequest::DELETE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } } - + + // Perform the allocation in the hostpool + host = HostDelete::hpool->get(hid,true); + + if ( host == 0 ) + { + goto error_host_get; + } + rc = HostDelete::hpool->drop(host); host->unlock(); - // All nice, return the host info to the client + // All nice, return the host info to the client arrayData.push_back(xmlrpc_c::value_boolean( rc == 0 )); // SUCCESS arrayresult = new xmlrpc_c::value_array(arrayData); - + // Copy arrayresult into retval mem space *retval = *arrayresult; // and get rid of the original @@ -74,7 +89,11 @@ void RequestManager::HostDelete::execute( return; error_authenticate: - oss << "User not authorized to delete hosts"; + oss << "Error in user authentication"; + goto error_common; + +error_authorize: + oss << "User not authorized to delete HOST"; goto error_common; error_host_get: @@ -86,11 +105,11 @@ error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/rm/RequestManagerHostEnable.cc b/src/rm/RequestManagerHostEnable.cc index 4e000dadbd..78cdc70e34 100644 --- a/src/rm/RequestManagerHostEnable.cc +++ b/src/rm/RequestManagerHostEnable.cc @@ -17,13 +17,15 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void RequestManager::HostEnable::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; int hid; @@ -43,39 +45,51 @@ void RequestManager::HostEnable::execute( hid = xmlrpc_c::value_int(paramList.getInt(1)); enable = xmlrpc_c::value_boolean(paramList.getBoolean(2)); - // Only oneadmin can enable hosts + //Authenticate the user rc = HostEnable::upool->authenticate(session); - - if ( rc != 0) - { - goto error_authenticate; + + if ( rc == -1 ) + { + goto error_authenticate; } - host = HostEnable::hpool->get(hid,true); - - if ( host == 0 ) - { - goto error_host_get; + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::HOST,hid,AuthRequest::MANAGE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } } - + + host = HostEnable::hpool->get(hid,true); + + if ( host == 0 ) + { + goto error_host_get; + } + if ( enable == true) { - host->enable(); + host->enable(); } else { - host->disable(); + host->disable(); } HostEnable::hpool->update(host); - + host->unlock(); - + //Result - arrayData.push_back(xmlrpc_c::value_boolean(true)); arrayresult = new xmlrpc_c::value_array(arrayData); - + *retval = *arrayresult; delete arrayresult; @@ -83,15 +97,19 @@ void RequestManager::HostEnable::execute( return; error_authenticate: - oss << "Only oneadmin can enable hosts"; + oss << "Error in user authentication"; + goto error_common; + +error_authorize: + oss << "User not authorized to enable HOST"; goto error_common; error_host_get: - oss << "Error getting host with HID = " << hid; + oss << "Error getting host with HID = " << hid; goto error_common; error_common: - NebulaLog::log("ReM",Log::ERROR,oss); + NebulaLog::log("ReM",Log::ERROR,oss); arrayData.push_back(xmlrpc_c::value_boolean(false)); arrayData.push_back(xmlrpc_c::value_string(oss.str())); @@ -99,7 +117,7 @@ error_common: xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/rm/RequestManagerHostInfo.cc b/src/rm/RequestManagerHostInfo.cc index b586d59675..3bff4b530c 100644 --- a/src/rm/RequestManagerHostInfo.cc +++ b/src/rm/RequestManagerHostInfo.cc @@ -23,13 +23,13 @@ void RequestManager::HostInfo::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; - int hid; + int hid; int rc; Host * host; - + ostringstream oss; /* -- RPC specific vars -- */ @@ -42,7 +42,7 @@ void RequestManager::HostInfo::execute( session = xmlrpc_c::value_string(paramList.getString(0)); hid = xmlrpc_c::value_int (paramList.getInt(1)); - // Check if it is a valid user + //Authenticate the user rc = HostInfo::upool->authenticate(session); if ( rc == -1 ) @@ -51,18 +51,18 @@ void RequestManager::HostInfo::execute( } // Get the host from the HostPool - host = HostInfo::hpool->get(hid,true); - - if ( host == 0 ) - { - goto error_host_get; + host = HostInfo::hpool->get(hid,true); + + if ( host == 0 ) + { + goto error_host_get; } - + oss << *host; - + host->unlock(); - - // All nice, return the host info to the client + + // All nice, return the host info to the client arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS arrayData.push_back(xmlrpc_c::value_string(oss.str())); @@ -75,24 +75,24 @@ void RequestManager::HostInfo::execute( return; error_authenticate: - oss << "User not authenticated, HostInfo call aborted."; + oss << "Error in user authentication"; goto error_common; error_host_get: - oss << "Error getting host with HID = " << hid; + oss << "Error getting host with HID = " << hid; goto error_common; error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - + + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/rm/RequestManagerHostPoolInfo.cc b/src/rm/RequestManagerHostPoolInfo.cc index ba598a803d..945bbe6471 100755 --- a/src/rm/RequestManagerHostPoolInfo.cc +++ b/src/rm/RequestManagerHostPoolInfo.cc @@ -23,11 +23,11 @@ void RequestManager::HostPoolInfo::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; ostringstream oss; int rc; - + /* -- RPC specific vars -- */ vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -37,8 +37,7 @@ void RequestManager::HostPoolInfo::execute( // Get the parameters session = xmlrpc_c::value_string(paramList.getString(0)); - - // Check if it is a valid user + //Authenticate the user rc = HostPoolInfo::upool->authenticate(session); if ( rc == -1 ) @@ -46,16 +45,16 @@ void RequestManager::HostPoolInfo::execute( goto error_authenticate; } - // Perform the allocation in the vmpool + // Perform the allocation in the vmpool rc = HostPoolInfo::hpool->dump(oss,""); - + if ( rc != 0 ) - { + { goto error_dump; } - - //All nice, return the host info to the client - arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS + + //All nice, return the host info to the client + arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS arrayData.push_back(xmlrpc_c::value_string(oss.str())); arrayresult = new xmlrpc_c::value_array(arrayData); @@ -69,24 +68,24 @@ void RequestManager::HostPoolInfo::execute( return; error_authenticate: - oss << "User not authenticated, RequestManagerHostPoolInfo aborted."; + oss << "Error in user authentication"; goto error_common; error_dump: - oss << "Error getting host pool"; + oss << "Error getting host pool"; goto error_common; error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - + + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } From 8c3d64e95137d738cea4798f63c0979ca4f73365 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 9 Jul 2010 18:59:42 +0200 Subject: [PATCH 26/64] Added first version of auth mad files --- install.sh | 25 +- src/authm_mad/auth_mad | 1 + src/authm_mad/one_auth_mad | 39 ++ src/authm_mad/one_auth_mad.rb | 65 ++++ src/authm_mad/one_usage.rb | 86 +++++ src/authm_mad/quota.rb | 84 ++++ src/authm_mad/simple_auth.rb | 33 ++ src/authm_mad/spec/client_mock.rb | 55 +++ src/authm_mad/spec/db_helpers.rb | 25 ++ src/authm_mad/spec/oca_vms.yaml | 551 +++++++++++++++++++++++++++ src/authm_mad/spec/one_usage_spec.rb | 75 ++++ src/authm_mad/spec/quota_spec.rb | 82 ++++ src/authm_mad/spec/spec_common.rb | 25 ++ 13 files changed, 1140 insertions(+), 6 deletions(-) create mode 100644 src/authm_mad/auth_mad create mode 100755 src/authm_mad/one_auth_mad create mode 100755 src/authm_mad/one_auth_mad.rb create mode 100644 src/authm_mad/one_usage.rb create mode 100644 src/authm_mad/quota.rb create mode 100644 src/authm_mad/simple_auth.rb create mode 100644 src/authm_mad/spec/client_mock.rb create mode 100644 src/authm_mad/spec/db_helpers.rb create mode 100644 src/authm_mad/spec/oca_vms.yaml create mode 100644 src/authm_mad/spec/one_usage_spec.rb create mode 100644 src/authm_mad/spec/quota_spec.rb create mode 100644 src/authm_mad/spec/spec_common.rb diff --git a/install.sh b/install.sh index 2e42c13b10..e123cfd476 100755 --- a/install.sh +++ b/install.sh @@ -157,6 +157,7 @@ ETC_DIRS="$ETC_LOCATION/im_kvm \ $ETC_LOCATION/tm_dummy \ $ETC_LOCATION/tm_lvm \ $ETC_LOCATION/hm \ + $ETC_LOCATION/auth \ $ETC_LOCATION/ec2query_templates \ $ETC_LOCATION/occi_templates" @@ -240,10 +241,11 @@ INSTALL_ETC_FILES[10]="TM_SSH_ETC_FILES:$ETC_LOCATION/tm_ssh" INSTALL_ETC_FILES[11]="TM_DUMMY_ETC_FILES:$ETC_LOCATION/tm_dummy" INSTALL_ETC_FILES[12]="TM_LVM_ETC_FILES:$ETC_LOCATION/tm_lvm" INSTALL_ETC_FILES[13]="HM_ETC_FILES:$ETC_LOCATION/hm" -INSTALL_ETC_FILES[14]="ECO_ETC_FILES:$ETC_LOCATION" -INSTALL_ETC_FILES[15]="ECO_ETC_TEMPLATE_FILES:$ETC_LOCATION/ec2query_templates" -INSTALL_ETC_FILES[16]="OCCI_ETC_FILES:$ETC_LOCATION" -INSTALL_ETC_FILES[17]="OCCI_ETC_TEMPLATE_FILES:$ETC_LOCATION/occi_templates" +INSTALL_ETC_FILES[14]="AUTH_ETC_FILES:$ETC_LOCATION/auth" +INSTALL_ETC_FILES[15]="ECO_ETC_FILES:$ETC_LOCATION" +INSTALL_ETC_FILES[16]="ECO_ETC_TEMPLATE_FILES:$ETC_LOCATION/ec2query_templates" +INSTALL_ETC_FILES[17]="OCCI_ETC_FILES:$ETC_LOCATION" +INSTALL_ETC_FILES[18]="OCCI_ETC_TEMPLATE_FILES:$ETC_LOCATION/occi_templates" #------------------------------------------------------------------------------- # Binary files, to be installed under $BIN_LOCATION @@ -281,7 +283,10 @@ RUBY_LIB_FILES="src/mad/ruby/one_mad.rb \ src/cli/client_utilities.rb \ src/cli/command_parse.rb \ src/oca/ruby/OpenNebula.rb \ - src/tm_mad/TMScript.rb" + src/tm_mad/TMScript.rb \ + src/authm_mad/one_usage.rb \ + src/authm_mad/quota.rb \ + src/authm_mad/simple_auth.rb" RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \ src/oca/ruby/OpenNebula/HostPool.rb \ @@ -318,7 +323,9 @@ MADS_LIB_FILES="src/mad/sh/madcommon.sh \ src/tm_mad/one_tm \ src/tm_mad/one_tm.rb \ src/hm_mad/one_hm.rb \ - src/hm_mad/one_hm" + src/hm_mad/one_hm \ + src/authm_mad/one_auth_mad.rb \ + src/authm_mad/one_auth_mad" #------------------------------------------------------------------------------- # Information Manager Probes, to be installed under $LIB_LOCATION/im_probes @@ -436,6 +443,12 @@ TM_LVM_ETC_FILES="src/tm_mad/lvm/tm_lvm.conf \ HM_ETC_FILES="src/hm_mad/hmrc" +#------------------------------------------------------------------------------- +# Hook Manager driver config. files, to be installed under $ETC_LOCATION/hm +#------------------------------------------------------------------------------- + +AUTH_ETC_FILES="src/authm_mad/auth_mad" + #------------------------------------------------------------------------------- # Sample files, to be installed under $SHARE_LOCATION/examples #------------------------------------------------------------------------------- diff --git a/src/authm_mad/auth_mad b/src/authm_mad/auth_mad new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/src/authm_mad/auth_mad @@ -0,0 +1 @@ + diff --git a/src/authm_mad/one_auth_mad b/src/authm_mad/one_auth_mad new file mode 100755 index 0000000000..58df732596 --- /dev/null +++ b/src/authm_mad/one_auth_mad @@ -0,0 +1,39 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +if [ -z "${ONE_LOCATION}" ]; then + DRIVERRC=/etc/one/auth/authrc + MADCOMMON=/usr/lib/one/mads/madcommon.sh + VAR_LOCATION=/var/lib/one +else + DRIVERRC=$ONE_LOCATION/etc/auth/authrc + MADCOMMON=$ONE_LOCATION/lib/mads/madcommon.sh + VAR_LOCATION=$ONE_LOCATION/var +fi + +. $MADCOMMON + +# Export the vmm_mad specific rc +export_rc_vars $DRIVERRC + +# Go to ONE_LOCATION +cd $VAR_LOCATION + +# Execute the actual MAD +execute_mad $* + diff --git a/src/authm_mad/one_auth_mad.rb b/src/authm_mad/one_auth_mad.rb new file mode 100755 index 0000000000..4da410a75a --- /dev/null +++ b/src/authm_mad/one_auth_mad.rb @@ -0,0 +1,65 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" + ETC_LOCATION="/etc/one/" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" + ETC_LOCATION=ONE_LOCATION+"/etc/" +end + +$: << RUBY_LIB_LOCATION + +require 'pp' + +require 'OpenNebulaDriver' +require 'simple_auth' + +class AuthorizationManager < OpenNebulaDriver + def initialize + super(1, false) + + @authenticate=SimpleAuth.new + + register_action(:AUTHENTICATE, method('action_authenticate')) + register_action(:AUTHORIZE, method('action_authorize')) + end + + def action_authenticate(request_id, user_id, user, password, token) + auth=@authenticate.auth(user_id, user, password, token) + if auth==true + send_message('AUTHENTICATE', RESULT[:success], + request_id, 'Successfully authenticated') + else + send_message('AUTHENTICATE', RESULT[:failure], + request_id, auth) + end + end + + def action_authorize(request_id, user_id, *tokens) + send_message('AUTHORIZE', RESULT[:success], request_id, 'success') + end +end + + +am=AuthorizationManager.new +am.start_driver + diff --git a/src/authm_mad/one_usage.rb b/src/authm_mad/one_usage.rb new file mode 100644 index 0000000000..49c8ee9e28 --- /dev/null +++ b/src/authm_mad/one_usage.rb @@ -0,0 +1,86 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +require 'OpenNebula' + +# This class holds usage information for a virtual machine or +# total usage for a user. Variables inside are cpu and memory +# consumption +class VmUsage + attr_accessor :cpu, :memory + def initialize(cpu, memory) + @cpu=cpu + @memory=memory + end +end + +# This class retrieves and caches vms and its consuption grouped +# by users. 'update_user' method should be called to fill data for +# a user before any calculation is made +class OneUsage + # 'client' is an OpenNebula::Client object used to connect + # to OpenNebula daemon. Ideally it should connect as user 0 + def initialize(client) + @client=client + @users=Hash.new + end + + # Gets information about VMs defined for a user. It caches new + # VMs and takes out from the cache deleted VMs + def update_user(user) + @users[user]=Hash.new if !@users[user] + + vmpool=OpenNebula::VirtualMachinePool.new(@client, user) + vmpool.info + + one_ids=vmpool.map {|vm| vm.id } + vms=@users[user] + user_ids=vms.keys + + deleted_vms=user_ids-one_ids + added_vms=one_ids-user_ids + + deleted_vms.each {|vmid| vms.delete(vmid) } + added_vms.each do |vmid| + vm=OpenNebula::VirtualMachine.new( + OpenNebula::VirtualMachine.build_xml(vmid), @client) + vm.info + hash=vm.to_hash['VM']['TEMPLATE'] + usage=VmUsage.new(hash['CPU'].to_f, hash['MEMORY'].to_i) + vms[vmid.to_i]=usage + end + end + + # Returns the cache of defined VMs for a user. It is a hash with + # VM id as key and VmUsage as value + def vms(user) + vms=@users[user] + @users[user]=vms=Hash.new if !vms + vms + end + + # Returns total consumption by a user into a VmUsage object + def total(user) + usage=VmUsage.new(0.0, 0) + + @users[user].each do |id, vm| + usage.cpu+=vm.cpu + usage.memory+=vm.memory + end + + usage + end +end diff --git a/src/authm_mad/quota.rb b/src/authm_mad/quota.rb new file mode 100644 index 0000000000..36a0a588d3 --- /dev/null +++ b/src/authm_mad/quota.rb @@ -0,0 +1,84 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +# Quota functionality for auth driver. Stores in database limits for each +# user and using OneUsage is able to retrieve resource usage from +# OpenNebula daemon and check if it is below limits +class Quota + TABLE_NAME=:quotas + + # 'db' is a Sequel database where to store user limits and client + # is OpenNebula::Client used to connect to OpenNebula daemon + def initialize(db, client) + @db=db + @client=client + + @usage=OneUsage.new(@client) + + create_table + @table=@db[TABLE_NAME] + end + + # Creates database quota table if it does not exist + def create_table + @db.create_table?(TABLE_NAME) do + primary_key :id + Integer :uid + Float :cpu + Integer :memory + Integer :num_vms + index :uid + end + end + + # Adds new user limits + def add(uid, cpu, memory, num_vms) + @table.insert( + :uid => uid, + :cpu => cpu, + :memory => memory, + :num_vms => num_vms + ) + end + + # Gets user limits + def get(uid) + @table.filter(:uid => uid).first + end + + # Checks if the user is below resource limits. If new_vm is defined + # checks if its requirements fit in limits + def check(user, new_vm=nil) + usage=@usage.total(user) + user_quota=get(user) + if new_vm + usage.cpu+=new_vm.cpu + usage.memory+=new_vm.memory + end + usage.cpu<=user_quota[:cpu] && usage.memory<=user_quota[:memory] + end + + # Updates user resource consuption + def update(user) + @usage.update_user(user) + end + + # Get cache for the user + def get_user(user) + @usage.vms(user) + end +end + diff --git a/src/authm_mad/simple_auth.rb b/src/authm_mad/simple_auth.rb new file mode 100644 index 0000000000..793890c6c2 --- /dev/null +++ b/src/authm_mad/simple_auth.rb @@ -0,0 +1,33 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + + +class SimpleAuth + def initialize + end + + def auth(user_id, user, password, token) + STDERR.puts [user_id, user, password, token].inspect + STDERR.flush + t_user, t_password=token.split(':') + #auth=(user==t_user && password==t_password) + auth=(password==token) + auth="Invalid credentials" if auth!=true + auth + end +end + + diff --git a/src/authm_mad/spec/client_mock.rb b/src/authm_mad/spec/client_mock.rb new file mode 100644 index 0000000000..fed78630bd --- /dev/null +++ b/src/authm_mad/spec/client_mock.rb @@ -0,0 +1,55 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +class ClientMock + def initialize(mock_data) + @mock_data=mock_data + end + + def search_method(methods, args) + keys=methods.keys + + if keys.include? args[0] + value=methods[args[0]] + if value.class != Hash + return value + elsif args.length>1 + return search_method(value, args[1..-1]) + else + nil + end + else + nil + end + end + + def call(action, *args) + value=search_method(@mock_data, [action]+args.flatten) + if value + if value.class==Proc + value.call(action, args.flatten) + else + value + end + else + message="Action '#{action}(#{args.join(',')})' not defined" + STDERR.puts message + OpenNebula::Error.new(message) + end + end +end + + diff --git a/src/authm_mad/spec/db_helpers.rb b/src/authm_mad/spec/db_helpers.rb new file mode 100644 index 0000000000..ea3ca3f7a5 --- /dev/null +++ b/src/authm_mad/spec/db_helpers.rb @@ -0,0 +1,25 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +require 'rubygems' +require 'sequel' + +def check_column(schema, column) + schema.find {|c| c[0] == column }.should_not == nil +end + + + diff --git a/src/authm_mad/spec/oca_vms.yaml b/src/authm_mad/spec/oca_vms.yaml new file mode 100644 index 0000000000..b69377b77a --- /dev/null +++ b/src/authm_mad/spec/oca_vms.yaml @@ -0,0 +1,551 @@ + +vmpool.info: + 0: " + + + 0 + + + 0 + + + jfontan + + + test + + + 0 + + + 1 + + + 0 + + + 1278433840 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 1 + + + 0 + + + jfontan + + + test + + + 0 + + + 1 + + + 0 + + + 1278433841 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + " + 1: " + + + 2 + + + 1 + + + lero + + + test + + + 0 + + + 1 + + + 0 + + + 1278433868 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 3 + + + 1 + + + lero + + + test + + + 0 + + + 1 + + + 0 + + + 1278433869 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + " +vm.info: + 0: " + + 0 + + + 0 + + + test + + + 0 + + + 1 + + + 0 + + + 1278433840 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + " + 1: " + + 1 + + + 0 + + + test + + + 0 + + + 1 + + + 0 + + + 1278433841 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + " + 2: " + + 2 + + + 1 + + + test + + + 0 + + + 1 + + + 0 + + + 1278433868 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + " + 3: " + + 3 + + + 1 + + + test + + + 0 + + + 1 + + + 0 + + + 1278433869 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + " \ No newline at end of file diff --git a/src/authm_mad/spec/one_usage_spec.rb b/src/authm_mad/spec/one_usage_spec.rb new file mode 100644 index 0000000000..f20b6d111d --- /dev/null +++ b/src/authm_mad/spec/one_usage_spec.rb @@ -0,0 +1,75 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +require 'spec_common' + +require 'client_mock' +require 'one_usage' + +describe "OneUsage" do + before(:all) do + mock_data=YAML::load(File.read('spec/oca_vms.yaml')) + client=ClientMock.new(mock_data) + @one_usage=OneUsage.new(client) + end + + it "should get information about vm users" do + @one_usage.update_user(0) + vms=@one_usage.vms(0) + + vms.size.should == 2 + vms[0].cpu.should == 0.5 + vms[0].memory.should == 64 + vms[1].cpu.should == 1.0 + vms[1].memory.should == 256 + + usage=@one_usage.total(0) + usage.cpu.should == 1.5 + usage.memory.should == 64+256 + + @one_usage.update_user(1) + vms=@one_usage.vms(1) + + vms.size.should == 2 + vms[2].cpu.should == 1.5 + vms[2].memory.should == 512 + vms[3].cpu.should == 2.0 + vms[3].memory.should == 1024 + + usage=@one_usage.total(1) + usage.cpu.should == 3.5 + usage.memory.should == 512+1024 + end + + it "should update information" do + vms=@one_usage.vms(1) + vms[4]=VmUsage.new(4.0, 2048) + + usage=@one_usage.total(1) + usage.cpu.should == 1.5 + 2.0 + 4.0 + usage.memory.should == 512+1024+2048 + + vms.delete(3) + usage=@one_usage.total(1) + usage.cpu.should == 1.5 + 4.0 + usage.memory.should == 512+2048 + + @one_usage.update_user(1) + usage=@one_usage.total(1) + usage.cpu.should == 1.5 + 2.0 + usage.memory.should == 512+1024 + end +end diff --git a/src/authm_mad/spec/quota_spec.rb b/src/authm_mad/spec/quota_spec.rb new file mode 100644 index 0000000000..0e8e352f64 --- /dev/null +++ b/src/authm_mad/spec/quota_spec.rb @@ -0,0 +1,82 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +require 'spec_common' + +require 'quota' + +def check_quota(uid, cpu, memory, num_vms) + quota=@quota.get(uid) + quota.should_not == nil + quota[:cpu].should == cpu + quota[:memory].should == memory + quota[:num_vms].should == num_vms +end + +describe 'Quota' do + before(:all) do + @db=Sequel.sqlite + mock_data=YAML::load(File.read('spec/oca_vms.yaml')) + client=ClientMock.new(mock_data) + @quota=Quota.new(@db, client) + end + + it 'should create table' do + @db.table_exists?(:quotas).should == true + + schema=@db.schema(:quotas) + check_column(schema, :uid) + check_column(schema, :cpu) + check_column(schema, :memory) + check_column(schema, :num_vms) + end + + it 'should let add and retrieve quotas' do + @quota.add(0, 10.0, 1024, 10) + @quota.add(1, 20.0, 2048, 20) + @quota.add(2, 40.0, 4096, 40) + + check_quota(0, 10.0, 1024, 10) + check_quota(1, 20.0, 2048, 20) + check_quota(2, 40.0, 4096, 40) + + @quota.get(3).should == nil + end + + it 'should check for quotas' do + @quota.update(0) + @quota.update(1) + @quota.check(0).should == true + @quota.check(1).should == true + + vms=@quota.get_user(0) + vms[5]=VmUsage.new(40.0, 8192) + + vms=@quota.get_user(1) + vms[6]=VmUsage.new(40.0, 8192) + + @quota.check(0).should == false + @quota.check(1).should == false + + @quota.update(0) + @quota.update(1) + @quota.check(0).should == true + @quota.check(1).should == true + end + +end + + diff --git a/src/authm_mad/spec/spec_common.rb b/src/authm_mad/spec/spec_common.rb new file mode 100644 index 0000000000..aad9fa1fc1 --- /dev/null +++ b/src/authm_mad/spec/spec_common.rb @@ -0,0 +1,25 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +require 'pp' + +require 'db_helpers' + +$: << '../oca/ruby' + + + + From 0efe68bff19b1729261dff98e9c6aa34ec62e062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Fri, 9 Jul 2010 19:29:41 +0200 Subject: [PATCH 27/64] feature #203: Image use AuthManager --- src/rm/RequestManagerImageDelete.cc | 25 ++++++++++----- src/rm/RequestManagerImageEnable.cc | 25 ++++++++++----- src/rm/RequestManagerImageInfo.cc | 33 +++++++++++++++++--- src/rm/RequestManagerImagePublish.cc | 29 ++++++++++++----- src/rm/RequestManagerImageRemoveAttribute.cc | 30 ++++++++++++------ src/rm/RequestManagerImageUpdate.cc | 29 ++++++++++++----- 6 files changed, 125 insertions(+), 46 deletions(-) diff --git a/src/rm/RequestManagerImageDelete.cc b/src/rm/RequestManagerImageDelete.cc index f56473f114..403833f137 100644 --- a/src/rm/RequestManagerImageDelete.cc +++ b/src/rm/RequestManagerImageDelete.cc @@ -15,10 +15,12 @@ /* -------------------------------------------------------------------------- */ #include "RequestManager.h" -#include "NebulaLog.h" +#include "NebulaLog.h" #include "Nebula.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -55,6 +57,19 @@ void RequestManager::ImageDelete::execute( } uid = rc; + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE,iid,AuthRequest::DELETE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } // Get image from the ImagePool image = ImageDelete::ipool->get(iid,true); @@ -64,11 +79,6 @@ void RequestManager::ImageDelete::execute( goto error_image_get; } - if ( uid != 0 && uid != image->get_uid() ) - { - goto error_authorization; - } - rc = ImageDelete::ipool->drop(image); if ( rc < 0 ) @@ -98,9 +108,8 @@ error_image_get: oss << "Error getting image with ID = " << iid; goto error_common; -error_authorization: +error_authorize: oss << "User not authorized to delete image, aborting ImageDelete call."; - image->unlock(); goto error_common; error_delete: diff --git a/src/rm/RequestManagerImageEnable.cc b/src/rm/RequestManagerImageEnable.cc index 6d5cec9dcb..2f16ee6417 100644 --- a/src/rm/RequestManagerImageEnable.cc +++ b/src/rm/RequestManagerImageEnable.cc @@ -15,10 +15,12 @@ /* -------------------------------------------------------------------------- */ #include "RequestManager.h" -#include "NebulaLog.h" +#include "NebulaLog.h" #include "Nebula.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -57,6 +59,19 @@ void RequestManager::ImageEnable::execute( uid = rc; + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE,iid,AuthRequest::MANAGE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // Get image from the ImagePool image = ImageEnable::ipool->get(iid,true); @@ -65,11 +80,6 @@ void RequestManager::ImageEnable::execute( goto error_image_get; } - if ( uid != 0 && uid != image->get_uid() ) - { - goto error_authorization; - } - rc = image->enable(enable_flag); if ( rc < 0 ) @@ -101,10 +111,9 @@ error_image_get: oss << "[ImageEnable] Error getting image with ID = " << iid; goto error_common; -error_authorization: +error_authorize: oss << "[ImageEnable] User not authorized to enable/disable image" << " attributes, aborting call."; - image->unlock(); goto error_common; error_enable: diff --git a/src/rm/RequestManagerImageInfo.cc b/src/rm/RequestManagerImageInfo.cc index b16860a7a5..a21adaee2f 100644 --- a/src/rm/RequestManagerImageInfo.cc +++ b/src/rm/RequestManagerImageInfo.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -27,8 +29,8 @@ void RequestManager::ImageInfo::execute( string session; int iid; - int uid; - int rc; + int uid; // Image owner user id + int rc; // Requesting user id Image * image; ostringstream oss; @@ -56,10 +58,27 @@ void RequestManager::ImageInfo::execute( // Check if it is a valid user rc = ImageInfo::upool->authenticate(session); - if ( rc != 0 && rc != uid && !image->isPublic()) + if ( rc == -1 ) { goto error_authenticate; } + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::IMAGE, + iid, + AuthRequest::USE, + 0, + image->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } oss << *image; @@ -82,13 +101,17 @@ error_image_get: goto error_common; error_authenticate: - oss << "User doesn't exist, or not authorized to use image with " << +oss << "Cannot authenticate user, aborting ImageInfo call."; + image->unlock(); + goto error_common; + +error_authorize: + oss << "User not authorized to use image with " << "ID = " << iid << " , ImageInfo call aborted."; image->unlock(); goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerImagePublish.cc b/src/rm/RequestManagerImagePublish.cc index f88c328b72..cfd0bc8bd9 100644 --- a/src/rm/RequestManagerImagePublish.cc +++ b/src/rm/RequestManagerImagePublish.cc @@ -15,10 +15,12 @@ /* -------------------------------------------------------------------------- */ #include "RequestManager.h" -#include "NebulaLog.h" +#include "NebulaLog.h" #include "Nebula.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -57,6 +59,23 @@ void RequestManager::ImagePublish::execute( uid = rc; + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE, + iid, + AuthRequest::MANAGE, + 0, + image->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // Get image from the ImagePool image = ImagePublish::ipool->get(iid,true); @@ -65,11 +84,6 @@ void RequestManager::ImagePublish::execute( goto error_image_get; } - if ( uid != 0 && uid != image->get_uid() ) - { - goto error_authorization; - } - image->publish(publish_flag); ImagePublish::ipool->update(image); @@ -95,10 +109,9 @@ error_image_get: oss << "[ImagePublish] Error getting image with ID = " << iid; goto error_common; -error_authorization: +error_authorize: oss << "[ImagePublish] User not authorized to publish/unpublish image" << ", aborting call."; - image->unlock(); goto error_common; error_common: diff --git a/src/rm/RequestManagerImageRemoveAttribute.cc b/src/rm/RequestManagerImageRemoveAttribute.cc index 348fefb4bf..b00c88c1ef 100644 --- a/src/rm/RequestManagerImageRemoveAttribute.cc +++ b/src/rm/RequestManagerImageRemoveAttribute.cc @@ -15,10 +15,12 @@ /* -------------------------------------------------------------------------- */ #include "RequestManager.h" -#include "NebulaLog.h" +#include "NebulaLog.h" #include "Nebula.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -56,6 +58,23 @@ void RequestManager::ImageRemoveAttribute::execute( } uid = rc; + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE, + iid, + AuthRequest::MANAGE, + 0, + image->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } // Get image from the ImagePool image = ImageRemoveAttribute::ipool->get(iid,true); @@ -65,12 +84,6 @@ void RequestManager::ImageRemoveAttribute::execute( goto error_image_get; } - - if ( uid != 0 && uid != image->get_uid() ) - { - goto error_authorization; - } - rc = ImageRemoveAttribute::ipool->remove_attribute(image, name); if ( rc < 0 ) @@ -99,10 +112,9 @@ error_image_get: oss << "[ImageRemoveAttribute] Error getting image with ID = " << iid; goto error_common; -error_authorization: +error_authorize: oss << "[ImageRemoveAttribute] User not authorized to remove image" << " attributes aborting call."; - image->unlock(); goto error_common; error_remove_attribute: diff --git a/src/rm/RequestManagerImageUpdate.cc b/src/rm/RequestManagerImageUpdate.cc index 780eac63f4..4af4a054a7 100644 --- a/src/rm/RequestManagerImageUpdate.cc +++ b/src/rm/RequestManagerImageUpdate.cc @@ -15,10 +15,12 @@ /* -------------------------------------------------------------------------- */ #include "RequestManager.h" -#include "NebulaLog.h" +#include "NebulaLog.h" #include "Nebula.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -58,6 +60,23 @@ void RequestManager::ImageUpdate::execute( } uid = rc; + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE, + iid, + AuthRequest::MANAGE, + 0, + image->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } // Get image from the ImagePool image = ImageUpdate::ipool->get(iid,true); @@ -67,11 +86,6 @@ void RequestManager::ImageUpdate::execute( goto error_image_get; } - if ( uid != 0 && uid != image->get_uid() ) - { - goto error_authorization; - } - // This will perform the update on the DB as well, // so no need to do it manually rc = ImageUpdate::ipool->replace_attribute(image, name, value); @@ -102,10 +116,9 @@ error_image_get: oss << "Error getting image with ID = " << iid; goto error_common; -error_authorization: +error_authorize: oss << "User not authorized to modify image attributes " << ", aborting ImageUpdate call."; - image->unlock(); goto error_common; error_update: From f7d3833c104037c76686e4979a184e4f36dfec87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Fri, 9 Jul 2010 19:32:37 +0200 Subject: [PATCH 28/64] feature #203: Adding authorization for Image allocate --- src/rm/RequestManagerImageAllocate.cc | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index ce266d3a6d..1722c1a3d0 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -15,10 +15,12 @@ /* -------------------------------------------------------------------------- */ #include "RequestManager.h" -#include "NebulaLog.h" +#include "NebulaLog.h" #include "Nebula.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -55,6 +57,19 @@ void RequestManager::ImageAllocate::execute( } uid = rc; + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE,-1,AuthRequest::CREATE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } rc = ImageAllocate::ipool->allocate(uid,image_template,&iid); @@ -78,6 +93,10 @@ void RequestManager::ImageAllocate::execute( error_authenticate: oss << "User not authenticated, aborting ImageAllocate call."; goto error_common; + +error_authorize: + oss << "User not authorized to allocate a new IMAGE"; + goto error_common; error_allocate: if (rc == -1) From 6ab7f3e13c5ac6f9ef25c9a6f91bc5091b7c2bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Fri, 9 Jul 2010 19:53:45 +0200 Subject: [PATCH 29/64] feature #200: Auth for VirtualNetworks --- .../RequestManagerVirtualNetworkAllocate.cc | 19 ++++++++++ src/rm/RequestManagerVirtualNetworkDelete.cc | 38 ++++++++++++++----- src/rm/RequestManagerVirtualNetworkInfo.cc | 28 +++++++++++++- src/rm/RequestManagerVirtualNetworkPublish.cc | 18 +++++++-- 4 files changed, 88 insertions(+), 15 deletions(-) diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 791d0f4b57..2520fbcbc9 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -64,6 +66,19 @@ void RequestManager::VirtualNetworkAllocate::execute( uid = user->get_uid(); user->unlock(); + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::NET,-1,AuthRequest::CREATE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } rc = vnpool->allocate(uid,stemplate,&nid); @@ -91,6 +106,10 @@ error_session: error_get_user: oss << "User not recognized, cannot allocate VirtualNetwork"; goto error_common; + +error_authorize: + oss << "User not authorized to create a VirtualNetwork"; + goto error_common; error_vn_allocate: oss << "Error allocating VN with template: " << endl << stemplate; diff --git a/src/rm/RequestManagerVirtualNetworkDelete.cc b/src/rm/RequestManagerVirtualNetworkDelete.cc index b16e9803aa..6ffe35e113 100644 --- a/src/rm/RequestManagerVirtualNetworkDelete.cc +++ b/src/rm/RequestManagerVirtualNetworkDelete.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -45,6 +47,27 @@ void RequestManager::VirtualNetworkDelete::execute( session = xmlrpc_c::value_string(paramList.getString(0)); nid = xmlrpc_c::value_int (paramList.getInt (1)); + // Only oneadmin or the VN owner can perform operations upon the VN + rc = VirtualNetworkDelete::upool->authenticate(session); + + if ( rc == -1 ) + { + goto error_authenticate; + } + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::NET,nid,AuthRequest::DELETE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // Retrieve VN from the pool vn = vnpool->get(nid,true); @@ -54,14 +77,6 @@ void RequestManager::VirtualNetworkDelete::execute( } uid = vn->get_uid(); - - // Only oneadmin or the VN owner can perform operations upon the VN - rc = VirtualNetworkDelete::upool->authenticate(session); - - if ( rc != 0 && rc != uid) - { - goto error_authenticate; - } rc = vnpool->drop(vn); @@ -79,8 +94,11 @@ void RequestManager::VirtualNetworkDelete::execute( return; error_authenticate: - vn->unlock(); - oss << "User cannot delete VN"; + oss << "User not authenticated, aborting VirtualNetworkDelete call"; + goto error_common; + +error_authorize: + oss << "User not authorized to delete Virtual Network with NID = " << nid; goto error_common; error_vn_get: diff --git a/src/rm/RequestManagerVirtualNetworkInfo.cc b/src/rm/RequestManagerVirtualNetworkInfo.cc index 046f74b20d..5649c96b35 100644 --- a/src/rm/RequestManagerVirtualNetworkInfo.cc +++ b/src/rm/RequestManagerVirtualNetworkInfo.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -51,7 +53,7 @@ void RequestManager::VirtualNetworkInfo::execute( { goto error_authenticate; } - + vn = vnpool->get(nid,true); if ( vn == 0 ) @@ -59,6 +61,23 @@ void RequestManager::VirtualNetworkInfo::execute( goto error_vn_get; } + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::NET, + nid, + AuthRequest::USE, + 0, + vn->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + oss << *vn; vn->unlock(); @@ -82,9 +101,14 @@ error_authenticate: error_vn_get: oss << "Error getting Virtual Network with NID = " << nid; goto error_common; + +error_authorize: + vn->unlock(); + oss << "User not authorized to view VirtualNetwork" << + ", VirtualNetworkInfo call aborted."; + goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerVirtualNetworkPublish.cc b/src/rm/RequestManagerVirtualNetworkPublish.cc index 985ae289b0..42d8030491 100644 --- a/src/rm/RequestManagerVirtualNetworkPublish.cc +++ b/src/rm/RequestManagerVirtualNetworkPublish.cc @@ -65,9 +65,21 @@ void RequestManager::VirtualNetworkPublish::execute( goto error_vn_get; } - if ( uid != 0 && uid != vn->get_uid() ) + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin { - goto error_authorization; + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::NET, + nid, + AuthRequest::MANAGE, + 0, + vn->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } } vn->publish(publish_flag); @@ -95,7 +107,7 @@ error_vn_get: oss << "[VirtualNetworkPublish] Error getting VN with ID = " << nid; goto error_common; -error_authorization: +error_authorize: oss << "[VirtualNetworkPublish] User not authorized to (un)publish VN" << ", aborting call."; vn->unlock(); From 6641caef7e53d36f2d5cc3105a294b4fb89f4d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20Va=CC=81zquez?= Date: Fri, 9 Jul 2010 19:53:45 +0200 Subject: [PATCH 30/64] feature #203: Auth for VirtualNetworks --- .../RequestManagerVirtualNetworkAllocate.cc | 19 ++++++++++ src/rm/RequestManagerVirtualNetworkDelete.cc | 38 ++++++++++++++----- src/rm/RequestManagerVirtualNetworkInfo.cc | 28 +++++++++++++- src/rm/RequestManagerVirtualNetworkPublish.cc | 18 +++++++-- 4 files changed, 88 insertions(+), 15 deletions(-) diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 791d0f4b57..2520fbcbc9 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -64,6 +66,19 @@ void RequestManager::VirtualNetworkAllocate::execute( uid = user->get_uid(); user->unlock(); + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::NET,-1,AuthRequest::CREATE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } rc = vnpool->allocate(uid,stemplate,&nid); @@ -91,6 +106,10 @@ error_session: error_get_user: oss << "User not recognized, cannot allocate VirtualNetwork"; goto error_common; + +error_authorize: + oss << "User not authorized to create a VirtualNetwork"; + goto error_common; error_vn_allocate: oss << "Error allocating VN with template: " << endl << stemplate; diff --git a/src/rm/RequestManagerVirtualNetworkDelete.cc b/src/rm/RequestManagerVirtualNetworkDelete.cc index b16e9803aa..6ffe35e113 100644 --- a/src/rm/RequestManagerVirtualNetworkDelete.cc +++ b/src/rm/RequestManagerVirtualNetworkDelete.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -45,6 +47,27 @@ void RequestManager::VirtualNetworkDelete::execute( session = xmlrpc_c::value_string(paramList.getString(0)); nid = xmlrpc_c::value_int (paramList.getInt (1)); + // Only oneadmin or the VN owner can perform operations upon the VN + rc = VirtualNetworkDelete::upool->authenticate(session); + + if ( rc == -1 ) + { + goto error_authenticate; + } + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::NET,nid,AuthRequest::DELETE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // Retrieve VN from the pool vn = vnpool->get(nid,true); @@ -54,14 +77,6 @@ void RequestManager::VirtualNetworkDelete::execute( } uid = vn->get_uid(); - - // Only oneadmin or the VN owner can perform operations upon the VN - rc = VirtualNetworkDelete::upool->authenticate(session); - - if ( rc != 0 && rc != uid) - { - goto error_authenticate; - } rc = vnpool->drop(vn); @@ -79,8 +94,11 @@ void RequestManager::VirtualNetworkDelete::execute( return; error_authenticate: - vn->unlock(); - oss << "User cannot delete VN"; + oss << "User not authenticated, aborting VirtualNetworkDelete call"; + goto error_common; + +error_authorize: + oss << "User not authorized to delete Virtual Network with NID = " << nid; goto error_common; error_vn_get: diff --git a/src/rm/RequestManagerVirtualNetworkInfo.cc b/src/rm/RequestManagerVirtualNetworkInfo.cc index 046f74b20d..5649c96b35 100644 --- a/src/rm/RequestManagerVirtualNetworkInfo.cc +++ b/src/rm/RequestManagerVirtualNetworkInfo.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -51,7 +53,7 @@ void RequestManager::VirtualNetworkInfo::execute( { goto error_authenticate; } - + vn = vnpool->get(nid,true); if ( vn == 0 ) @@ -59,6 +61,23 @@ void RequestManager::VirtualNetworkInfo::execute( goto error_vn_get; } + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::NET, + nid, + AuthRequest::USE, + 0, + vn->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + oss << *vn; vn->unlock(); @@ -82,9 +101,14 @@ error_authenticate: error_vn_get: oss << "Error getting Virtual Network with NID = " << nid; goto error_common; + +error_authorize: + vn->unlock(); + oss << "User not authorized to view VirtualNetwork" << + ", VirtualNetworkInfo call aborted."; + goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerVirtualNetworkPublish.cc b/src/rm/RequestManagerVirtualNetworkPublish.cc index 985ae289b0..42d8030491 100644 --- a/src/rm/RequestManagerVirtualNetworkPublish.cc +++ b/src/rm/RequestManagerVirtualNetworkPublish.cc @@ -65,9 +65,21 @@ void RequestManager::VirtualNetworkPublish::execute( goto error_vn_get; } - if ( uid != 0 && uid != vn->get_uid() ) + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin { - goto error_authorization; + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::NET, + nid, + AuthRequest::MANAGE, + 0, + vn->isPublic()); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } } vn->publish(publish_flag); @@ -95,7 +107,7 @@ error_vn_get: oss << "[VirtualNetworkPublish] Error getting VN with ID = " << nid; goto error_common; -error_authorization: +error_authorize: oss << "[VirtualNetworkPublish] User not authorized to (un)publish VN" << ", aborting call."; vn->unlock(); From 7777e913ccf8c30796e63e44d989738a5501662b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Fri, 9 Jul 2010 19:56:55 +0200 Subject: [PATCH 31/64] feature #203: Missing header --- src/rm/RequestManagerVirtualNetworkPublish.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rm/RequestManagerVirtualNetworkPublish.cc b/src/rm/RequestManagerVirtualNetworkPublish.cc index 42d8030491..f60bb538eb 100644 --- a/src/rm/RequestManagerVirtualNetworkPublish.cc +++ b/src/rm/RequestManagerVirtualNetworkPublish.cc @@ -15,10 +15,12 @@ /* -------------------------------------------------------------------------- */ #include "RequestManager.h" -#include "NebulaLog.h" +#include "NebulaLog.h" #include "Nebula.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ From aea644f9440f6abe7760dcb517a9c2550edcc43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Fri, 9 Jul 2010 20:12:19 +0200 Subject: [PATCH 32/64] feature #203: Auth for USERS --- include/AuthManager.h | 3 ++- src/authm/AuthManager.cc | 1 + src/rm/RequestManagerUserAllocate.cc | 32 +++++++++++++++++++++++----- src/rm/RequestManagerUserDelete.cc | 29 ++++++++++++++++++++++--- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index c51bd32164..f9c7afdd94 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -290,7 +290,8 @@ public: VM, HOST, NET, - IMAGE + IMAGE, + USER }; /** diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 8879f588a7..e9093651c9 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -84,6 +84,7 @@ void AuthRequest::add_auth(Object ob, case HOST: oss << "HOST:" ; break; case NET: oss << "NET:" ; break; case IMAGE: oss << "IMAGE:" ; break; + case USER: oss << "USER:" ; break; } if (op == CREATE) //encode the ob_id, it is a template diff --git a/src/rm/RequestManagerUserAllocate.cc b/src/rm/RequestManagerUserAllocate.cc index 63aa6d18af..e38bb4ee45 100644 --- a/src/rm/RequestManagerUserAllocate.cc +++ b/src/rm/RequestManagerUserAllocate.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -48,13 +50,29 @@ void RequestManager::UserAllocate::execute( username = xmlrpc_c::value_string(paramList.getString(1)); password = xmlrpc_c::value_string(paramList.getString(2)); - // Only oneadmin can add users rc = UserAllocate::upool->authenticate(session); - if ( rc != 0 ) + if ( rc == -1 ) { goto error_authenticate; } + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::USER, + -1, + AuthRequest::CREATE, + 0, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } // Let's make sure that the user doesn't exist in the database user = UserAllocate::upool->get(username,false); @@ -85,16 +103,20 @@ void RequestManager::UserAllocate::execute( return; error_authenticate: - oss << "User not authorized to add new users"; + oss << "User not authenticated, aborting UserAllocate call."; goto error_common; +error_authorize: + oss << "User not authorized to add new users, aborting UserAllocate call."; + goto error_common; + error_duplicate: - oss << "Existing user, cannot duplicate"; + oss << "Existing user, cannot duplicate."; goto error_common; error_allocate: - oss << "Error allocating user"; + oss << "Error allocating user."; goto error_common; error_common: diff --git a/src/rm/RequestManagerUserDelete.cc b/src/rm/RequestManagerUserDelete.cc index 508580c1a3..d1a388ea67 100644 --- a/src/rm/RequestManagerUserDelete.cc +++ b/src/rm/RequestManagerUserDelete.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -48,13 +50,29 @@ void RequestManager::UserDelete::execute( goto error_oneadmin_deletion; } - // Only oneadmin can delete users rc = UserDelete::upool->authenticate(session); - if ( rc != 0 ) + if ( rc == -1 ) { goto error_authenticate; } + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::USER, + uid, + AuthRequest::DELETE, + 0, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } // Now let's get the user user = UserDelete::upool->get(uid,true); @@ -89,7 +107,12 @@ error_oneadmin_deletion: goto error_common; error_authenticate: - oss << "User not authorized to delete users"; + oss << "User not authenticated, aborting UserDelete call."; + goto error_common; + +error_authorize: + oss << "User not authorized to delete user with uid " << uid + << ", aborting UserDelete call."; goto error_common; error_get_user: From ccf56905137fa64ee881343a429d05abd718ed9e Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Mon, 12 Jul 2010 17:07:56 +0200 Subject: [PATCH 33/64] Duplicated mad installation --- install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index 155a0f242f..1f0ccee071 100755 --- a/install.sh +++ b/install.sh @@ -328,8 +328,8 @@ MADS_LIB_FILES="src/mad/sh/madcommon.sh \ src/hm_mad/one_hm.rb \ src/hm_mad/one_hm \ src/authm_mad/one_auth_mad.rb \ - src/authm_mad/one_auth_mad \ - src/hm_mad/one_hm" + src/authm_mad/one_auth_mad" + #------------------------------------------------------------------------------- # Information Manager Probes, to be installed under $LIB_LOCATION/im_probes #------------------------------------------------------------------------------- From 9f7544e617d7a7ce883c524e02d74348a529ed18 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 12 Jul 2010 17:45:28 +0200 Subject: [PATCH 34/64] feature #203: Cluster methods uses Auth Manager --- src/rm/RequestManagerClusterAdd.cc | 24 +++++++++++++++++--- src/rm/RequestManagerClusterAllocate.cc | 29 ++++++++++++++++++++----- src/rm/RequestManagerClusterDelete.cc | 25 ++++++++++++++++++--- src/rm/RequestManagerClusterInfo.cc | 4 +++- src/rm/RequestManagerClusterPoolInfo.cc | 11 +++++----- src/rm/RequestManagerClusterRemove.cc | 23 +++++++++++++++++--- 6 files changed, 96 insertions(+), 20 deletions(-) diff --git a/src/rm/RequestManagerClusterAdd.cc b/src/rm/RequestManagerClusterAdd.cc index ec5a8642f5..3f75b2c97e 100644 --- a/src/rm/RequestManagerClusterAdd.cc +++ b/src/rm/RequestManagerClusterAdd.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -45,15 +47,27 @@ void RequestManager::ClusterAdd::execute( hid = xmlrpc_c::value_int (paramList.getInt(1)); clid = xmlrpc_c::value_int (paramList.getInt(2)); - - // Only oneadmin can add hosts to clusters + //Authenticate the user rc = ClusterAdd::upool->authenticate(session); - if ( rc != 0 ) + if ( rc == -1 ) { goto error_authenticate; } + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::HOST,hid,AuthRequest::MANAGE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // Check if host exists host = ClusterAdd::hpool->get(hid,true); @@ -90,6 +104,10 @@ error_authenticate: oss << "User not authorized to add hosts to clusters"; goto error_common; +error_authorize: + oss << "User not authorized to manage HOST"; + goto error_common; + error_host_get: oss << "The host " << hid << " does not exists"; goto error_common; diff --git a/src/rm/RequestManagerClusterAllocate.cc b/src/rm/RequestManagerClusterAllocate.cc index ed77dea253..c64b72b9c4 100644 --- a/src/rm/RequestManagerClusterAllocate.cc +++ b/src/rm/RequestManagerClusterAllocate.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -43,14 +45,27 @@ void RequestManager::ClusterAllocate::execute( session = xmlrpc_c::value_string(paramList.getString(0)); clustername = xmlrpc_c::value_string(paramList.getString(1)); - // Only oneadmin can add new clusters + //Authenticate the user rc = ClusterAllocate::upool->authenticate(session); - if ( rc != 0 ) + if ( rc == -1 ) { goto error_authenticate; } + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::HOST,-1,AuthRequest::MANAGE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + // Perform the allocation in the hostpool rc = ClusterAllocate::hpool->allocate_cluster(&id, clustername); @@ -74,8 +89,12 @@ error_authenticate: oss << "User not authorized to add new clusters"; goto error_common; +error_authorize: + oss << "User not authorized to manage HOST"; + goto error_common; + error_cluster_allocate: - oss << "Can not allocate cluster " << clustername << + oss << "Can not allocate cluster " << clustername << " in the ClusterPool, returned error code [" << rc << "]"; goto error_common; @@ -84,8 +103,8 @@ error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - NebulaLog::log("ReM",Log::ERROR,oss); - + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; diff --git a/src/rm/RequestManagerClusterDelete.cc b/src/rm/RequestManagerClusterDelete.cc index 728da90d64..23c407fc09 100644 --- a/src/rm/RequestManagerClusterDelete.cc +++ b/src/rm/RequestManagerClusterDelete.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -29,7 +31,7 @@ void RequestManager::ClusterDelete::execute( // of the cluster to delete from the HostPool int clid; ostringstream oss; - int rc; + int rc; /* -- RPC specific vars -- */ vector arrayData; @@ -41,14 +43,27 @@ void RequestManager::ClusterDelete::execute( session = xmlrpc_c::value_string(paramList.getString(0)); clid = xmlrpc_c::value_int (paramList.getInt(1)); - // Only oneadmin can delete clusters + //Authenticate the user rc = ClusterDelete::upool->authenticate(session); - if ( rc != 0 ) + if ( rc == -1 ) { goto error_authenticate; } + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::HOST,-1,AuthRequest::MANAGE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + rc = ClusterDelete::hpool->drop_cluster(clid); if ( rc != 0 ) @@ -71,6 +86,10 @@ error_authenticate: oss << "User not authorized to delete clusters"; goto error_common; +error_authorize: + oss << "User not authorized to manage HOST"; + goto error_common; + error_cluster_delete: oss << "Can not delete cluster with CLID " << clid << " from the ClusterPool, returned error code [" << rc << "]"; diff --git a/src/rm/RequestManagerClusterInfo.cc b/src/rm/RequestManagerClusterInfo.cc index 2059858548..4c088636e1 100644 --- a/src/rm/RequestManagerClusterInfo.cc +++ b/src/rm/RequestManagerClusterInfo.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -42,7 +44,7 @@ void RequestManager::ClusterInfo::execute( session = xmlrpc_c::value_string(paramList.getString(0)); clid = xmlrpc_c::value_int (paramList.getInt(1)); - // Check if it is a valid user + //Authenticate the user rc = ClusterInfo::upool->authenticate(session); if ( rc == -1 ) diff --git a/src/rm/RequestManagerClusterPoolInfo.cc b/src/rm/RequestManagerClusterPoolInfo.cc index 0742be92cd..b3ec68a89a 100755 --- a/src/rm/RequestManagerClusterPoolInfo.cc +++ b/src/rm/RequestManagerClusterPoolInfo.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -37,8 +39,7 @@ void RequestManager::ClusterPoolInfo::execute( // Get the parameters session = xmlrpc_c::value_string(paramList.getString(0)); - - // Check if it is a valid user + //Authenticate the user rc = ClusterPoolInfo::upool->authenticate(session); if ( rc == -1 ) @@ -46,7 +47,7 @@ void RequestManager::ClusterPoolInfo::execute( goto error_authenticate; } - // Perform the allocation in the vmpool + // Perform the allocation in the vmpool rc = ClusterPoolInfo::hpool->dump_cluster(oss); if ( rc != 0 ) @@ -73,7 +74,7 @@ error_authenticate: goto error_common; error_dump: - oss << "Error getting Cluster pool"; + oss << "Error getting Cluster pool"; goto error_common; error_common: @@ -81,7 +82,7 @@ error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - NebulaLog::log("ReM",Log::ERROR,oss); + NebulaLog::log("ReM",Log::ERROR,oss); xmlrpc_c::value_array arrayresult_error(arrayData); diff --git a/src/rm/RequestManagerClusterRemove.cc b/src/rm/RequestManagerClusterRemove.cc index af02c1ab75..91df8d0a79 100644 --- a/src/rm/RequestManagerClusterRemove.cc +++ b/src/rm/RequestManagerClusterRemove.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -43,15 +45,26 @@ void RequestManager::ClusterRemove::execute( session = xmlrpc_c::value_string(paramList.getString(0)); hid = xmlrpc_c::value_int (paramList.getInt(1)); - - // Only oneadmin can remove hosts from clusters + // Only oneadmin can delete clusters rc = ClusterRemove::upool->authenticate(session); - if ( rc != 0 ) + if ( rc == -1 ) { goto error_authenticate; } + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::HOST,hid,AuthRequest::MANAGE,0,false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } // Check if host exists host = ClusterRemove::hpool->get(hid,true); @@ -89,6 +102,10 @@ error_authenticate: oss << "User not authorized to remove hosts from clusters"; goto error_common; +error_authorize: + oss << "User not authorized to manage HOST"; + goto error_common; + error_host_get: oss << "The host " << hid << " does not exists"; goto error_common; From 2fe5b7b740f5e7cd86e58e0d483f7e01e42e1bc2 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 12 Jul 2010 18:42:46 +0200 Subject: [PATCH 35/64] feature #203: Plain authorize method fix --- src/authm/AuthManager.cc | 52 +++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index e9093651c9..86d723a1fe 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -76,7 +76,7 @@ void AuthRequest::add_auth(Object ob, bool pub) { ostringstream oss; - bool auth = owner == uid; + bool auth; switch (ob) { @@ -118,14 +118,6 @@ void AuthRequest::add_auth(Object ob, case USE: oss << "USE:" ; - if ( ob == NET || ob == IMAGE ) - { - auth = auth || (pub == true); - } - else if (ob == HOST) - { - auth = true; - } break; case MANAGE: @@ -135,6 +127,48 @@ void AuthRequest::add_auth(Object ob, oss << owner << ":" << pub; + // ------------------------------------------------------------------------- + // Authorize the request for self authorization + // ------------------------------------------------------------------------- + + if ( uid == 0 ) + { + auth = true; + } + else + { + auth = false; + + switch (op) + { + case CREATE: + if ( ob == VM || ob == NET || ob == IMAGE ) + { + auth = true; + } + break; + + case DELETE: + auth = owner == uid; + break; + + case USE: + if (ob == NET || ob == IMAGE) + { + auth = (owner == uid) || pub + } + else if (ob == HOST) + { + auth = true + } + break; + + case MANAGE: + auth = owner == uid; + break; + } + } + self_authorize = self_authorize && auth; auths.push_back(oss.str()); From 89d60d65eafd84adc26e1e25003746aa0975e419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Mon, 12 Jul 2010 19:03:16 +0200 Subject: [PATCH 36/64] feature #203: Removed unused user info method --- include/RequestManager.h | 22 ------- src/rm/RequestManager.cc | 4 -- src/rm/RequestManagerUserInfo.cc | 100 ------------------------------- src/rm/SConstruct | 1 - 4 files changed, 127 deletions(-) delete mode 100644 src/rm/RequestManagerUserInfo.cc diff --git a/include/RequestManager.h b/include/RequestManager.h index ca8162d391..e49f28e891 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -620,28 +620,6 @@ private: UserPool * upool; }; - - /* ---------------------------------------------------------------------- */ - - class UserInfo: public xmlrpc_c::method - { - public: - UserInfo(UserPool * _upool):upool(_upool) - { - _signature="A:si"; - _help="Returns the Info of the user"; - }; - - ~UserInfo(){}; - - void execute( - xmlrpc_c::paramList const& paramList, - xmlrpc_c::value * const retvalP); - - private: - UserPool * upool; - }; - /* ---------------------------------------------------------------------- */ class UserDelete: public xmlrpc_c::method diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 315de7f2ef..b12c7094c5 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -264,9 +264,6 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr user_allocate(new RequestManager::UserAllocate(upool)); - xmlrpc_c::methodPtr user_info(new - RequestManager::UserInfo(upool)); - xmlrpc_c::methodPtr user_delete(new RequestManager::UserDelete(upool)); @@ -329,7 +326,6 @@ void RequestManager::register_xml_methods() /* User related methods*/ RequestManagerRegistry.addMethod("one.user.allocate", user_allocate); - RequestManagerRegistry.addMethod("one.user.info", user_info); RequestManagerRegistry.addMethod("one.user.delete", user_delete); RequestManagerRegistry.addMethod("one.userpool.info", userpool_info); diff --git a/src/rm/RequestManagerUserInfo.cc b/src/rm/RequestManagerUserInfo.cc deleted file mode 100644 index 1c7ea7fc19..0000000000 --- a/src/rm/RequestManagerUserInfo.cc +++ /dev/null @@ -1,100 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ -/* */ -/* Licensed 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. */ -/* -------------------------------------------------------------------------- */ - -#include "RequestManager.h" -#include "NebulaLog.h" - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void RequestManager::UserInfo::execute( - xmlrpc_c::paramList const& paramList, - xmlrpc_c::value * const retval) -{ - string session; - int uid; - - User * user; - - int rc; - ostringstream oss; - - /* -- RPC specific vars -- */ - vector arrayData; - xmlrpc_c::value_array * arrayresult; - - NebulaLog::log("ReM",Log::DEBUG,"UserInfo method invoked"); - - // Get the parameters - session = xmlrpc_c::value_string(paramList.getString(0)); - uid = xmlrpc_c::value_int(paramList.getInt(1)); - - // Only oneadmin can retrieve user information - rc = UserInfo::upool->authenticate(session); - - if ( rc != 0 ) - { - goto error_authenticate; - } - - // Now let's get the user - user = UserInfo::upool->get(uid,true); - - if ( user == 0 ) - { - goto error_get_user; - } - - oss << *user; - - user->unlock(); - - // All nice, return the new uid to client - arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS - arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - // Copy arrayresult into retval mem space - arrayresult = new xmlrpc_c::value_array(arrayData); - *retval = *arrayresult; - - delete arrayresult; // and get rid of the original - - return; - -error_authenticate: - oss << "User not authorized to retrieve user information"; - goto error_common; - -error_get_user: - oss << "Error getting user"; - goto error_common; - -error_common: - - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE - arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - - xmlrpc_c::value_array arrayresult_error(arrayData); - - *retval = arrayresult_error; - - return; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ diff --git a/src/rm/SConstruct b/src/rm/SConstruct index 2390bc160d..0e40ffb86f 100644 --- a/src/rm/SConstruct +++ b/src/rm/SConstruct @@ -48,7 +48,6 @@ source_files=[ 'RequestManagerVirtualNetworkPublish.cc', 'RequestManagerVirtualNetworkDelete.cc', 'RequestManagerUserAllocate.cc', - 'RequestManagerUserInfo.cc', 'RequestManagerUserDelete.cc', 'RequestManagerUserPoolInfo.cc' ] From 822f2bd09631982813c4526e91ae7632fdcafdef Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 12 Jul 2010 19:38:55 +0200 Subject: [PATCH 37/64] feature #203: Fix compilation error --- src/authm/AuthManager.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 86d723a1fe..4704c2031a 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -155,11 +155,11 @@ void AuthRequest::add_auth(Object ob, case USE: if (ob == NET || ob == IMAGE) { - auth = (owner == uid) || pub + auth = (owner == uid) || pub; } else if (ob == HOST) { - auth = true + auth = true; } break; From 8a433f451f502f7a9729c392d9578acd0a3dc040 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 12 Jul 2010 19:47:29 +0200 Subject: [PATCH 38/64] feature #203: Authorize methods for NET and IMAGE creation in insert --- src/image/Image.cc | 37 ++++++++++++++++++++++++++++++++++--- src/vnm/VirtualNetwork.cc | 30 +++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/image/Image.cc b/src/image/Image.cc index 3b419df06b..fe7b0f0943 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -25,6 +25,8 @@ #include "Image.h" #include "ImagePool.h" +#include "AuthManager.h" +#include "UserPool.h" /* ************************************************************************ */ /* Image :: Constructor/Destructor */ @@ -208,8 +210,29 @@ int Image::insert(SqlDB *db) source = tmp_sourcestream.str(); + // ------------------------------------------------------------------------ + // Authorize this request + // ------------------------------------------------------------------------ + + if ( uid != 0 ) // uid == 0 means oneadmin + { + string t64; + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE, + image_template.to_xml(t64), + AuthRequest::CREATE, + uid, + public_img); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + // ------------ INSERT THE TEMPLATE -------------------- - // Set up the template ID, to insert it if ( image_template.id == -1 ) { image_template.id = oid; @@ -217,7 +240,6 @@ int Image::insert(SqlDB *db) state = DISABLED; - // Insert the Template rc = image_template.insert(db); if ( rc != 0 ) @@ -225,7 +247,10 @@ int Image::insert(SqlDB *db) return rc; } - //Insert the Image + //-------------------------------------------------------------------------- + // Insert the Image + //-------------------------------------------------------------------------- + rc = insert_replace(db, false); if ( rc != 0 ) @@ -240,9 +265,15 @@ int Image::insert(SqlDB *db) error_name: NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template"); goto error_common; + error_type: NebulaLog::log("IMG", Log::ERROR, "Incorrect TYPE in image template"); goto error_common; + +error_authorize: + NebulaLog::log("IMG", Log::ERROR, "Error authorizing Image creation"); + goto error_common; + error_common: return -1; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index ee5e8d0740..fdcf678e00 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -22,6 +22,9 @@ #include "RangedLeases.h" #include "FixedLeases.h" +#include "AuthManager.h" +#include "UserPool.h" + /* ************************************************************************** */ /* Virtual Network :: Constructor/Destructor */ /* ************************************************************************** */ @@ -314,7 +317,28 @@ int VirtualNetwork::insert(SqlDB * db) vn_template.erase("PUBLIC"); - // ------------ TEMPLATE -------------------- + // ------------------------------------------------------------------------ + // Authorize this request + // ------------------------------------------------------------------------ + + if ( uid != 0 ) // uid == 0 means oneadmin + { + string t64; + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::NET, + vn_template.to_xml(t64), + AuthRequest::CREATE, + uid, + public_vnet); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + // ------------ INSERT THE TEMPLATE -------------------- if ( vn_template.id == -1 ) { @@ -416,6 +440,10 @@ error_bridge: ose << "No BRIDGE in template for Virtual Network id " << oid; goto error_common; +error_authorize: + ose << "Error authorizing Virtual Network creation"; + goto error_common; + error_template: ose << "Can not insert in DB template for Virtual Network id " << oid; goto error_common; From aff3a57c7377f9dd0bebf93a9ea643ba62a1d3ba Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Mon, 12 Jul 2010 19:49:46 +0200 Subject: [PATCH 39/64] Added simple permissions --- install.sh | 3 +- src/authm_mad/one_auth_mad.rb | 11 +++++- src/authm_mad/simple_permissions.rb | 59 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/authm_mad/simple_permissions.rb diff --git a/install.sh b/install.sh index 1f0ccee071..aa709e0ab7 100755 --- a/install.sh +++ b/install.sh @@ -287,7 +287,8 @@ RUBY_LIB_FILES="src/mad/ruby/one_mad.rb \ src/tm_mad/TMScript.rb \ src/authm_mad/one_usage.rb \ src/authm_mad/quota.rb \ - src/authm_mad/simple_auth.rb" + src/authm_mad/simple_auth.rb \ + src/authm_mad/simple_permissions.rb" RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \ src/oca/ruby/OpenNebula/HostPool.rb \ diff --git a/src/authm_mad/one_auth_mad.rb b/src/authm_mad/one_auth_mad.rb index 4da410a75a..a32bbf1c5d 100755 --- a/src/authm_mad/one_auth_mad.rb +++ b/src/authm_mad/one_auth_mad.rb @@ -32,12 +32,14 @@ require 'pp' require 'OpenNebulaDriver' require 'simple_auth' +require 'simple_permissions' class AuthorizationManager < OpenNebulaDriver def initialize super(1, false) @authenticate=SimpleAuth.new + @permissions=SimplePermissions.new register_action(:AUTHENTICATE, method('action_authenticate')) register_action(:AUTHORIZE, method('action_authorize')) @@ -55,7 +57,14 @@ class AuthorizationManager < OpenNebulaDriver end def action_authorize(request_id, user_id, *tokens) - send_message('AUTHORIZE', RESULT[:success], request_id, 'success') + auth=@permissions.auth(user_id, tokens.flatten) + if auth==true + send_message('AUTHORIZE', RESULT[:success], + request_id, 'success') + else + send_message('AUTHORIZE', RESULT[:failure], + request_id, auth) + end end end diff --git a/src/authm_mad/simple_permissions.rb b/src/authm_mad/simple_permissions.rb new file mode 100644 index 0000000000..ff7649a131 --- /dev/null +++ b/src/authm_mad/simple_permissions.rb @@ -0,0 +1,59 @@ + +class SimplePermissions + + def initialize(database=nil) + # initialize quota + end + + def auth_message(result, message) + result ? true : message + end + + def auth(uid, tokens) + result=true + + tokens.each do |token| + object, id, action, owner=token.split(':') + result=auth_object(uid.to_s, object, id, action, owner) + break result if result!=true + end + + result + end + + def auth_object(uid, object, id, action, owner) + return true if uid==0 + + case object + when 'VM' + return auth_vm(uid, object, id, action, owner) + when 'HOST' + return auth_host(uid, object, id, action, owner) + else + return auth_generic(uid, object, id, action, owner) + end + end + + def auth_vm(uid, object, id, action, owner) + case action + when 'CREATE' + # add quota here + return true + else + auth_message(uid==owner, "You cannot manage VM #{id}") + end + end + + def auth_host(uid, object, id, action, owner) + auth_message(action=='USE', 'Only oneadmin can manage hosts') + end + + def auth_generic(uid, object, id, action, owner) + if action=='CREATE' + true + else + auth_message(uid==owner, "You are not allowed to #{action} "<< + "#{object} #{id}") + end + end +end \ No newline at end of file From 3a76fea7110963cb392d324ffbda07571990bd9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Mon, 12 Jul 2010 20:06:26 +0200 Subject: [PATCH 40/64] feature #203: Removed authorization from Image and VirtualNetworks in the RM --- src/rm/RequestManagerImageAllocate.cc | 17 ----------------- src/rm/RequestManagerVirtualNetworkAllocate.cc | 17 ----------------- 2 files changed, 34 deletions(-) diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 1722c1a3d0..64f8d064c4 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -58,19 +58,6 @@ void RequestManager::ImageAllocate::execute( uid = rc; - //Authorize the operation - if ( uid != 0 ) // uid == 0 means oneadmin - { - AuthRequest ar(uid); - - ar.add_auth(AuthRequest::IMAGE,-1,AuthRequest::CREATE,0,false); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - rc = ImageAllocate::ipool->allocate(uid,image_template,&iid); if ( rc < 0 ) @@ -94,10 +81,6 @@ error_authenticate: oss << "User not authenticated, aborting ImageAllocate call."; goto error_common; -error_authorize: - oss << "User not authorized to allocate a new IMAGE"; - goto error_common; - error_allocate: if (rc == -1) { diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 2520fbcbc9..3c97f902b2 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -67,19 +67,6 @@ void RequestManager::VirtualNetworkAllocate::execute( user->unlock(); - //Authorize the operation - if ( uid != 0 ) // uid == 0 means oneadmin - { - AuthRequest ar(uid); - - ar.add_auth(AuthRequest::NET,-1,AuthRequest::CREATE,0,false); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - rc = vnpool->allocate(uid,stemplate,&nid); if ( rc < 0 ) @@ -107,10 +94,6 @@ error_get_user: oss << "User not recognized, cannot allocate VirtualNetwork"; goto error_common; -error_authorize: - oss << "User not authorized to create a VirtualNetwork"; - goto error_common; - error_vn_allocate: oss << "Error allocating VN with template: " << endl << stemplate; goto error_common; From 16f7982cf3061b7b31dfca5759ce8dfee2c99c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Mon, 12 Jul 2010 20:10:48 +0200 Subject: [PATCH 41/64] feature #203: Added error log functions to RM --- include/RequestManager.h | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/include/RequestManager.h b/include/RequestManager.h index 184c6db9e3..d59c0b9ed4 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -24,6 +24,8 @@ #include "VirtualNetworkPool.h" #include "ImagePool.h" +#include "NebulaLog.h" + #include #include #include @@ -166,7 +168,53 @@ private: void register_xml_methods(); int setup_socket(); + + // ---------------------------------------------------------------------- + // ---------------------------------------------------------------------- + // Error Messages + // ---------------------------------------------------------------------- + // ---------------------------------------------------------------------- + + static string authorization_error (const string& method, + const string &action, + const string &object, + int uid, + int id) + { + ostringstream oss; + oss << "[" << method << "]" << " User [" << uid << "] not authorized " + << " to perform " << action << " on " << object + << " [" << id << "]"; + + NebulaLog::log("RM",Log::ERROR,oss); + + return oss.str(); + } + + static string authenticate_error (const string& method) + { + ostringstream oss; + + oss << "[" << method << "]" << " User couldn't be authenticated," << + " aborting call"; + NebulaLog::log("RM",Log::ERROR,oss); + + return oss.str(); + } + static string get_error (const string& method, + const string &object, + int id) + { + ostringstream oss; + + oss << "[" << method << "]" << " Error getting " << + object << " [" << id << "]"; + NebulaLog::log("RM",Log::ERROR,oss); + + return oss.str(); + } + // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- // XML-RPC Methods From a342d00d8d6cfb15f7723272ad60888d01ada706 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Mon, 12 Jul 2010 22:48:48 +0200 Subject: [PATCH 42/64] feature #203: public flag is taken into account --- src/authm_mad/simple_permissions.rb | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/authm_mad/simple_permissions.rb b/src/authm_mad/simple_permissions.rb index ff7649a131..347205aa33 100644 --- a/src/authm_mad/simple_permissions.rb +++ b/src/authm_mad/simple_permissions.rb @@ -13,28 +13,28 @@ class SimplePermissions result=true tokens.each do |token| - object, id, action, owner=token.split(':') - result=auth_object(uid.to_s, object, id, action, owner) + object, id, action, owner, pub=token.split(':') + result=auth_object(uid.to_s, object, id, action, owner, pub) break result if result!=true end result end - def auth_object(uid, object, id, action, owner) + def auth_object(uid, object, id, action, owner, pub) return true if uid==0 case object when 'VM' - return auth_vm(uid, object, id, action, owner) + return auth_vm(uid, object, id, action, owner, pub) when 'HOST' - return auth_host(uid, object, id, action, owner) + return auth_host(uid, object, id, action, owner, pub) else - return auth_generic(uid, object, id, action, owner) + return auth_generic(uid, object, id, action, owner, pub) end end - def auth_vm(uid, object, id, action, owner) + def auth_vm(uid, object, id, action, owner, pub) case action when 'CREATE' # add quota here @@ -44,16 +44,20 @@ class SimplePermissions end end - def auth_host(uid, object, id, action, owner) + def auth_host(uid, object, id, action, owner, pub) auth_message(action=='USE', 'Only oneadmin can manage hosts') end - def auth_generic(uid, object, id, action, owner) - if action=='CREATE' + def auth_generic(uid, object, id, action, owner, pub) + case action + when 'CREATE' true + when 'USE' + auth_message(uid==owner || pub=='1', "You are not allowed to "<< + "#{action} #{object} #{id}") else - auth_message(uid==owner, "You are not allowed to #{action} "<< - "#{object} #{id}") + auth_message(uid==owner, "You are not allowed to "<< + "#{action} #{object} #{id}") end end end \ No newline at end of file From b624debb224bca8e70fbb2b936a911c1fd6fa900 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 13 Jul 2010 12:32:11 +0200 Subject: [PATCH 43/64] feature #203: added default and unlimited quota limits --- src/authm_mad/quota.rb | 40 +++++++++++++++++++++++++------ src/authm_mad/spec/quota_spec.rb | 21 ++++++++++++---- src/authm_mad/spec/spec_common.rb | 1 + 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/authm_mad/quota.rb b/src/authm_mad/quota.rb index 36a0a588d3..23eb021e3e 100644 --- a/src/authm_mad/quota.rb +++ b/src/authm_mad/quota.rb @@ -14,17 +14,29 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'one_usage' + # Quota functionality for auth driver. Stores in database limits for each # user and using OneUsage is able to retrieve resource usage from # OpenNebula daemon and check if it is below limits class Quota + attr_accessor :defaults + TABLE_NAME=:quotas # 'db' is a Sequel database where to store user limits and client # is OpenNebula::Client used to connect to OpenNebula daemon - def initialize(db, client) + def initialize(db, client, conf={}) @db=db @client=client + @conf={ + :defaults => { + :cpu => nil, + :memory => nil + } + }.merge(conf) + + @defaults=@conf[:defaults] @usage=OneUsage.new(@client) @@ -45,18 +57,30 @@ class Quota end # Adds new user limits - def add(uid, cpu, memory, num_vms) - @table.insert( - :uid => uid, + def set(uid, cpu, memory, num_vms) + data={ :cpu => cpu, :memory => memory, :num_vms => num_vms - ) + } + + quotas=@table.filter(:uid => uid) + + if quotas.first + quotas.update(data) + else + @table.insert(data.merge!(:uid => uid)) + end end # Gets user limits def get(uid) - @table.filter(:uid => uid).first + limit=@table.filter(:uid => uid).first + if limit + limit + else + @defaults + end end # Checks if the user is below resource limits. If new_vm is defined @@ -68,7 +92,9 @@ class Quota usage.cpu+=new_vm.cpu usage.memory+=new_vm.memory end - usage.cpu<=user_quota[:cpu] && usage.memory<=user_quota[:memory] + + (!user_quota[:cpu] || usage.cpu<=user_quota[:cpu]) && + (!user_quota[:memory] || usage.memory<=user_quota[:memory]) end # Updates user resource consuption diff --git a/src/authm_mad/spec/quota_spec.rb b/src/authm_mad/spec/quota_spec.rb index 0e8e352f64..e4e796f888 100644 --- a/src/authm_mad/spec/quota_spec.rb +++ b/src/authm_mad/spec/quota_spec.rb @@ -16,6 +16,7 @@ require 'spec_common' +require 'client_mock' require 'quota' def check_quota(uid, cpu, memory, num_vms) @@ -45,15 +46,15 @@ describe 'Quota' do end it 'should let add and retrieve quotas' do - @quota.add(0, 10.0, 1024, 10) - @quota.add(1, 20.0, 2048, 20) - @quota.add(2, 40.0, 4096, 40) + @quota.set(0, 10.0, 1024, 10) + @quota.set(1, 20.0, 2048, 20) + @quota.set(2, 40.0, 4096, 40) check_quota(0, 10.0, 1024, 10) check_quota(1, 20.0, 2048, 20) check_quota(2, 40.0, 4096, 40) - @quota.get(3).should == nil + @quota.get(3).should == @quota.defaults end it 'should check for quotas' do @@ -77,6 +78,18 @@ describe 'Quota' do @quota.check(1).should == true end + it 'should let update limits' do + @quota.set(0, nil, nil, nil) + check_quota(0, nil, nil, nil) + end + + it 'should understand unlimited quotas' do + vms=@quota.get_user(0) + vms[7]=VmUsage.new(9999999999.0, 99999999999) + @quota.check(0).should == true + @quota.check(0, VmUsage.new(999999999.0, 99999999)).should == true + end + end diff --git a/src/authm_mad/spec/spec_common.rb b/src/authm_mad/spec/spec_common.rb index aad9fa1fc1..7fffd828f0 100644 --- a/src/authm_mad/spec/spec_common.rb +++ b/src/authm_mad/spec/spec_common.rb @@ -19,6 +19,7 @@ require 'pp' require 'db_helpers' $: << '../oca/ruby' +$: << './spec' From fcb7ac0a23f99f663ac34a7902dfa4ba0eff5a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Tue, 13 Jul 2010 13:49:16 +0200 Subject: [PATCH 44/64] feature #203: Changed RM to use log functions --- include/RequestManager.h | 58 +++++++++++++++---- src/rm/RequestManagerAction.cc | 11 ++-- src/rm/RequestManagerAllocate.cc | 6 +- src/rm/RequestManagerCancel.cc | 8 ++- src/rm/RequestManagerClusterAdd.cc | 12 ++-- src/rm/RequestManagerClusterAllocate.cc | 10 ++-- src/rm/RequestManagerClusterDelete.cc | 9 +-- src/rm/RequestManagerClusterInfo.cc | 6 +- src/rm/RequestManagerClusterPoolInfo.cc | 8 ++- src/rm/RequestManagerClusterRemove.cc | 23 ++++---- src/rm/RequestManagerDeploy.cc | 17 ++++-- src/rm/RequestManagerHostAllocate.cc | 8 ++- src/rm/RequestManagerHostDelete.cc | 10 ++-- src/rm/RequestManagerHostEnable.cc | 12 ++-- src/rm/RequestManagerHostInfo.cc | 7 ++- src/rm/RequestManagerHostPoolInfo.cc | 8 ++- src/rm/RequestManagerImageAllocate.cc | 13 ++--- src/rm/RequestManagerImageDelete.cc | 11 ++-- src/rm/RequestManagerImageEnable.cc | 11 ++-- src/rm/RequestManagerImageInfo.cc | 9 +-- src/rm/RequestManagerImagePoolInfo.cc | 9 +-- src/rm/RequestManagerImagePublish.cc | 9 +-- src/rm/RequestManagerImageRemoveAttribute.cc | 12 ++-- src/rm/RequestManagerImageUpdate.cc | 12 ++-- src/rm/RequestManagerInfo.cc | 8 ++- src/rm/RequestManagerMigrate.cc | 15 +++-- src/rm/RequestManagerPoolInfo.cc | 6 +- src/rm/RequestManagerUserAllocate.cc | 11 ++-- src/rm/RequestManagerUserDelete.cc | 22 +++---- src/rm/RequestManagerUserPoolInfo.cc | 7 ++- .../RequestManagerVirtualNetworkAllocate.cc | 12 ++-- src/rm/RequestManagerVirtualNetworkDelete.cc | 10 ++-- src/rm/RequestManagerVirtualNetworkInfo.cc | 9 +-- .../RequestManagerVirtualNetworkPoolInfo.cc | 12 ++-- src/rm/RequestManagerVirtualNetworkPublish.cc | 9 +-- 35 files changed, 253 insertions(+), 167 deletions(-) diff --git a/include/RequestManager.h b/include/RequestManager.h index d59c0b9ed4..6b118cc454 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -24,8 +24,6 @@ #include "VirtualNetworkPool.h" #include "ImagePool.h" -#include "NebulaLog.h" - #include #include #include @@ -183,10 +181,17 @@ private: { ostringstream oss; oss << "[" << method << "]" << " User [" << uid << "] not authorized " - << " to perform " << action << " on " << object - << " [" << id << "]"; - - NebulaLog::log("RM",Log::ERROR,oss); + << " to perform " << action << " on " << object; + + + if ( id != (int)NULL ) + { + oss << " [" << id << "]."; + } + else + { + oss << " Pool"; + } return oss.str(); } @@ -196,9 +201,8 @@ private: ostringstream oss; oss << "[" << method << "]" << " User couldn't be authenticated," << - " aborting call"; - NebulaLog::log("RM",Log::ERROR,oss); - + " aborting call."; + return oss.str(); } @@ -209,7 +213,41 @@ private: ostringstream oss; oss << "[" << method << "]" << " Error getting " << - object << " [" << id << "]"; + object; + + if ( id != (int)NULL ) + { + oss << " [" << id << "]."; + } + else + { + oss << " Pool."; + } + + return oss.str(); + } + + static string action_error (const string& method, + const string &action, + const string &object, + int id, + int rc) + { + ostringstream oss; + + oss << "[" << method << "]" << " Error trying to " << action << " " + << object; + + if ( id != (int)NULL ) + { + oss << " [" << id << "]."; + } + + if ( rc != (int)NULL ) + { + oss << " .Returned error code [" << rc << "]."; + } + NebulaLog::log("RM",Log::ERROR,oss); return oss.str(); diff --git a/src/rm/RequestManagerAction.cc b/src/rm/RequestManagerAction.cc index cb9032de67..11ff2f1b9e 100644 --- a/src/rm/RequestManagerAction.cc +++ b/src/rm/RequestManagerAction.cc @@ -42,6 +42,8 @@ void RequestManager::VirtualMachineAction::execute( VirtualMachine * vm; ostringstream oss; + + const string method_name = "VirtualMachineAction"; NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineAction invoked"); @@ -150,23 +152,24 @@ error_operation: goto error_common; error_vm_get: - oss << "The virtual machine " << vid << " does not exists"; + oss.str(get_error(method_name, "VM", vid)); goto error_common; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to perform VM operation"; + oss.str(authorization_error(method_name, "MANAGE", "VM", rc, vid)); goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); arrayData.push_back(xmlrpc_c::value_string(oss.str())); xmlrpc_c::value_array arrayresult_error(arrayData); + + NebulaLog::log("ReM",Log::ERROR,oss); *retval = arrayresult_error; diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 84f5556838..f91ffde242 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -28,6 +28,8 @@ void RequestManager::VirtualMachineAllocate::execute( { string session; string vm_template; + + const string method_name = "VirtualMachineAllocate"; int vid; int rc; @@ -75,11 +77,11 @@ void RequestManager::VirtualMachineAllocate::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_allocate: - oss << "Error inserting VM in the database, check oned.log"; + oss.str(action_error(method_name, "CREATE", "VM", NULL, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerCancel.cc b/src/rm/RequestManagerCancel.cc index 91178be808..85322e3e0f 100644 --- a/src/rm/RequestManagerCancel.cc +++ b/src/rm/RequestManagerCancel.cc @@ -29,6 +29,8 @@ void RequestManager::VirtualMachineCancel::execute( // of the vid to retrieve the information for int vid; int uid; + + const string method_name = "VirtualMachineCancel"; VirtualMachine * vm; @@ -95,15 +97,15 @@ void RequestManager::VirtualMachineCancel::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to canel VM"; + oss.str(authorization_error(method_name, "MANAGE", "VM", uid, vid)); goto error_common; error_vm_get: - oss << "Error getting vm for cancelling with VID = " << vid; + oss.str(get_error(method_name, "VM", vid)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterAdd.cc b/src/rm/RequestManagerClusterAdd.cc index 3f75b2c97e..199928e6e4 100644 --- a/src/rm/RequestManagerClusterAdd.cc +++ b/src/rm/RequestManagerClusterAdd.cc @@ -31,6 +31,8 @@ void RequestManager::ClusterAdd::execute( int hid; int clid; int rc; + + const string method_name = "ClusterAdd"; Host * host; @@ -101,22 +103,20 @@ void RequestManager::ClusterAdd::execute( return; error_authenticate: - oss << "User not authorized to add hosts to clusters"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to manage HOST"; + oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, hid)); goto error_common; error_host_get: - oss << "The host " << hid << " does not exists"; + oss.str(get_error(method_name, "HOST", hid)); goto error_common; error_cluster_add: host->unlock(); - - oss << "Can not add host " << hid << " to cluster " << clid << - ", returned error code [" << rc << "]"; + oss.str(action_error(method_name, "MANAGE", "HOST", hid, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterAllocate.cc b/src/rm/RequestManagerClusterAllocate.cc index c64b72b9c4..05dbb647f2 100644 --- a/src/rm/RequestManagerClusterAllocate.cc +++ b/src/rm/RequestManagerClusterAllocate.cc @@ -29,8 +29,9 @@ void RequestManager::ClusterAllocate::execute( string session; string clustername; - int id; + + const string method_name = "ClusterAllocate"; int rc; ostringstream oss; @@ -86,16 +87,15 @@ void RequestManager::ClusterAllocate::execute( return; error_authenticate: - oss << "User not authorized to add new clusters"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to manage HOST"; + oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, -1)); goto error_common; error_cluster_allocate: - oss << "Can not allocate cluster " << clustername << - " in the ClusterPool, returned error code [" << rc << "]"; + oss.str(action_error(method_name, "CREATE", "CLUSTER", NULL, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterDelete.cc b/src/rm/RequestManagerClusterDelete.cc index 23c407fc09..e840dd6689 100644 --- a/src/rm/RequestManagerClusterDelete.cc +++ b/src/rm/RequestManagerClusterDelete.cc @@ -32,6 +32,8 @@ void RequestManager::ClusterDelete::execute( int clid; ostringstream oss; int rc; + + const string method_name = "ClusterDelete"; /* -- RPC specific vars -- */ vector arrayData; @@ -83,16 +85,15 @@ void RequestManager::ClusterDelete::execute( return; error_authenticate: - oss << "User not authorized to delete clusters"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to manage HOST"; + oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, -1)); goto error_common; error_cluster_delete: - oss << "Can not delete cluster with CLID " << clid << - " from the ClusterPool, returned error code [" << rc << "]"; + oss.str(action_error(method_name, "DELETE", "CLUSTER", clid, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterInfo.cc b/src/rm/RequestManagerClusterInfo.cc index 4c088636e1..c11208b56f 100644 --- a/src/rm/RequestManagerClusterInfo.cc +++ b/src/rm/RequestManagerClusterInfo.cc @@ -31,6 +31,8 @@ void RequestManager::ClusterInfo::execute( int clid; int rc; + + const string method_name = "ClusterInfo"; ostringstream oss; @@ -73,11 +75,11 @@ void RequestManager::ClusterInfo::execute( return; error_authenticate: - oss << "User not authenticated, ClusterInfo call aborted."; + oss.str(authenticate_error(method_name)); goto error_common; error_cluster: - oss << "Error getting cluster with CLID = " << clid; + oss.str(get_error(method_name, "CLUSTER", clid)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterPoolInfo.cc b/src/rm/RequestManagerClusterPoolInfo.cc index b3ec68a89a..46d6a28d52 100755 --- a/src/rm/RequestManagerClusterPoolInfo.cc +++ b/src/rm/RequestManagerClusterPoolInfo.cc @@ -28,7 +28,9 @@ void RequestManager::ClusterPoolInfo::execute( { string session; ostringstream oss; - int rc; + int rc; + + const string method_name = "ClusterPoolInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -70,11 +72,11 @@ void RequestManager::ClusterPoolInfo::execute( return; error_authenticate: - oss << "User not authenticated, RequestManagerClusterPoolInfo aborted."; + oss.str(authenticate_error(method_name)); goto error_common; error_dump: - oss << "Error getting Cluster pool"; + oss.str(get_error(method_name, "CLUSTER", NULL)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterRemove.cc b/src/rm/RequestManagerClusterRemove.cc index 91df8d0a79..573e1aeb00 100644 --- a/src/rm/RequestManagerClusterRemove.cc +++ b/src/rm/RequestManagerClusterRemove.cc @@ -26,12 +26,14 @@ void RequestManager::ClusterRemove::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) { - string session; + string session; + + int hid; + int rc; + + const string method_name = "ClusterRemove"; - int hid; - int rc; - - Host * host; + Host * host; ostringstream oss; @@ -99,26 +101,23 @@ void RequestManager::ClusterRemove::execute( return; error_authenticate: - oss << "User not authorized to remove hosts from clusters"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to manage HOST"; + oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, -1)); goto error_common; error_host_get: - oss << "The host " << hid << " does not exists"; + oss.str(get_error(method_name, "HOST", hid)); goto error_common; error_cluster_remove: host->unlock(); - - oss << "Can not remove host " << hid << " from its cluster, " - << "returned error code [" << rc << "]"; + oss.str(action_error(method_name, "MANAGE", "HOST", hid, rc)); goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerDeploy.cc b/src/rm/RequestManagerDeploy.cc index d31a5d9460..97b272a1d1 100644 --- a/src/rm/RequestManagerDeploy.cc +++ b/src/rm/RequestManagerDeploy.cc @@ -36,6 +36,8 @@ void RequestManager::VirtualMachineDeploy::execute( string vmm_mad; string tm_mad; string vmdir; + + const string method_name = "VirtualMachineDeploy"; VirtualMachine * vm; Host * host; @@ -138,27 +140,28 @@ void RequestManager::VirtualMachineDeploy::execute( return; error_host_get: - oss << "The host " << hid << " does not exists"; + oss.str(get_error(method_name, "HOST", hid)); goto error_common; error_vm_get: - oss << "The virtual machine " << vid << " does not exists"; + oss.str(get_error(method_name, "VM", vid)); goto error_common; error_state: - oss << "Can not deploy VM, wrong state"; + oss.str(action_error(method_name, "MANAGE", "VM", vid, rc)); + oss << " Reason: VM in wrong state."; goto error_common_lock; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common_lock; error_authorize: - oss << "User not authorized to deploy VM on host"; + oss.str(authorization_error(method_name, "MANAGE", "VM", rc, vid)); goto error_common_lock; error_history: - oss << "Can not insert history to deploy VM"; + oss.str(action_error(method_name, "INSERT HISTORY", "VM", vid, rc)); goto error_common_lock; error_common_lock: @@ -167,6 +170,8 @@ error_common_lock: error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + NebulaLog::log("ReM",Log::ERROR,oss); xmlrpc_c::value_array arrayresult_error(arrayData); diff --git a/src/rm/RequestManagerHostAllocate.cc b/src/rm/RequestManagerHostAllocate.cc index c78971497f..5fcbcc91c2 100644 --- a/src/rm/RequestManagerHostAllocate.cc +++ b/src/rm/RequestManagerHostAllocate.cc @@ -32,6 +32,8 @@ void RequestManager::HostAllocate::execute( string im_mad_name; string vmm_mad_name; string tm_mad_name; + + const string method_name = "HostAllocate"; int hid; @@ -95,15 +97,15 @@ void RequestManager::HostAllocate::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to allocate a new HOST"; + oss.str(authorization_error(method_name, "CREATE", "HOST", rc, NULL)); goto error_common; error_host_allocate: - oss << "Error inserting HOST in the database, check oned.log"; + oss.str(action_error(method_name, "CREATE", "HOST", NULL, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerHostDelete.cc b/src/rm/RequestManagerHostDelete.cc index 4bd0c50333..0a453a8f56 100644 --- a/src/rm/RequestManagerHostDelete.cc +++ b/src/rm/RequestManagerHostDelete.cc @@ -32,7 +32,9 @@ void RequestManager::HostDelete::execute( int hid; Host * host; ostringstream oss; - int rc; + int rc; + + const string method_name = "HostDelete"; /* -- RPC specific vars -- */ vector arrayData; @@ -89,15 +91,15 @@ void RequestManager::HostDelete::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to delete HOST"; + oss.str(authorization_error(method_name, "DELETE", "HOST", rc, hid)); goto error_common; error_host_get: - oss << "Error getting host with HID = " < arrayData; + vector arrayData; xmlrpc_c::value_array * arrayresult; NebulaLog::log("ReM",Log::DEBUG,"HostEnable method invoked"); @@ -97,15 +99,15 @@ void RequestManager::HostEnable::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to enable HOST"; + oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, hid)); goto error_common; error_host_get: - oss << "Error getting host with HID = " << hid; + oss.str(get_error(method_name, "HOST", hid)); goto error_common; error_common: diff --git a/src/rm/RequestManagerHostInfo.cc b/src/rm/RequestManagerHostInfo.cc index 3bff4b530c..c94b73c24f 100644 --- a/src/rm/RequestManagerHostInfo.cc +++ b/src/rm/RequestManagerHostInfo.cc @@ -31,6 +31,8 @@ void RequestManager::HostInfo::execute( Host * host; ostringstream oss; + + const string method_name = "HostInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -75,15 +77,14 @@ void RequestManager::HostInfo::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_host_get: - oss << "Error getting host with HID = " << hid; + oss.str(get_error(method_name, "HOST", hid)); goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerHostPoolInfo.cc b/src/rm/RequestManagerHostPoolInfo.cc index 945bbe6471..babb66b745 100755 --- a/src/rm/RequestManagerHostPoolInfo.cc +++ b/src/rm/RequestManagerHostPoolInfo.cc @@ -26,7 +26,9 @@ void RequestManager::HostPoolInfo::execute( { string session; ostringstream oss; - int rc; + int rc; + + const string method_name = "HostPoolInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -68,11 +70,11 @@ void RequestManager::HostPoolInfo::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_dump: - oss << "Error getting host pool"; + oss.str(get_error(method_name, "HOST", -1)); goto error_common; error_common: diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 64f8d064c4..10f47a047c 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -36,6 +36,8 @@ void RequestManager::ImageAllocate::execute( int rc; ostringstream oss; + + const string method_name = "ImageAllocate"; vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -78,18 +80,11 @@ void RequestManager::ImageAllocate::execute( return; error_authenticate: - oss << "User not authenticated, aborting ImageAllocate call."; + oss.str(authenticate_error(method_name)); goto error_common; error_allocate: - if (rc == -1) - { - oss << "Error allocating image, check oned.log"; - } - else - { - oss << "Error parsing image template"; - } + oss.str(action_error(method_name, "CREATE", "IMAGE", NULL, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerImageDelete.cc b/src/rm/RequestManagerImageDelete.cc index 403833f137..846e77109b 100644 --- a/src/rm/RequestManagerImageDelete.cc +++ b/src/rm/RequestManagerImageDelete.cc @@ -37,6 +37,8 @@ void RequestManager::ImageDelete::execute( Image * image; ostringstream oss; + + const string method_name = "ImageDelete"; vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -101,19 +103,20 @@ void RequestManager::ImageDelete::execute( return; error_authenticate: - oss << "User not authenticated, aborting ImageDelete call."; + oss.str(authenticate_error(method_name)); goto error_common; error_image_get: - oss << "Error getting image with ID = " << iid; + oss.str(get_error(method_name, "IMAGE", iid)); goto error_common; error_authorize: - oss << "User not authorized to delete image, aborting ImageDelete call."; + oss.str(authorization_error(method_name, "DELETE", "IMAGE", uid, iid)); goto error_common; error_delete: - oss << "Cannot delete image, VMs might be running on it."; + oss.str(action_error(method_name, "DELETE", "IMAGE", iid, rc)); + oss << " VMs might be running on it."; image->unlock(); goto error_common; diff --git a/src/rm/RequestManagerImageEnable.cc b/src/rm/RequestManagerImageEnable.cc index 2f16ee6417..e21110b42c 100644 --- a/src/rm/RequestManagerImageEnable.cc +++ b/src/rm/RequestManagerImageEnable.cc @@ -38,6 +38,8 @@ void RequestManager::ImageEnable::execute( Image * image; ostringstream oss; + + const string method_name = "ImageEnable"; vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -104,20 +106,19 @@ void RequestManager::ImageEnable::execute( return; error_authenticate: - oss << "[ImageEnable] User not authenticated, aborting call."; + oss.str(authenticate_error(method_name)); goto error_common; error_image_get: - oss << "[ImageEnable] Error getting image with ID = " << iid; + oss.str(get_error(method_name, "IMAGE", iid)); goto error_common; error_authorize: - oss << "[ImageEnable] User not authorized to enable/disable image" << - " attributes, aborting call."; + oss.str(authorization_error(method_name, "MANAGE", "IMAGE", uid, iid)); goto error_common; error_enable: - oss << "[ImageEnable] Cannot enable/disable image [" << iid << "]"; + oss.str(action_error(method_name, "ENABLE/DISABLE", "IMAGE", iid, rc)); image->unlock(); goto error_common; diff --git a/src/rm/RequestManagerImageInfo.cc b/src/rm/RequestManagerImageInfo.cc index a21adaee2f..cb3c838de4 100644 --- a/src/rm/RequestManagerImageInfo.cc +++ b/src/rm/RequestManagerImageInfo.cc @@ -34,6 +34,8 @@ void RequestManager::ImageInfo::execute( Image * image; ostringstream oss; + + const string method_name = "ImageInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -97,17 +99,16 @@ void RequestManager::ImageInfo::execute( return; error_image_get: - oss << "Error getting image with ID = " << iid; + oss.str(get_error(method_name, "IMAGE", iid)); goto error_common; error_authenticate: -oss << "Cannot authenticate user, aborting ImageInfo call."; + oss.str(authenticate_error(method_name)); image->unlock(); goto error_common; error_authorize: - oss << "User not authorized to use image with " << - "ID = " << iid << " , ImageInfo call aborted."; + oss.str(authorization_error(method_name, "USE", "IMAGE", rc, iid)); image->unlock(); goto error_common; diff --git a/src/rm/RequestManagerImagePoolInfo.cc b/src/rm/RequestManagerImagePoolInfo.cc index e1880b4fc7..b0c3b4e64d 100755 --- a/src/rm/RequestManagerImagePoolInfo.cc +++ b/src/rm/RequestManagerImagePoolInfo.cc @@ -32,6 +32,8 @@ void RequestManager::ImagePoolInfo::execute( int rc; int uid; int filter_flag; + + const string method_name = "ImagePoolInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -109,11 +111,11 @@ void RequestManager::ImagePoolInfo::execute( return; error_authenticate: - oss << "User not authenticated, ImagePoolInfo call aborted."; + oss.str(authenticate_error(method_name)); goto error_common; error_authorization: - oss << "User not authorized to perform this operation."; + oss.str(authorization_error(method_name, "USE", "IMAGE", uid, NULL)); goto error_common; error_filter_flag: @@ -121,11 +123,10 @@ error_filter_flag: goto error_common; error_dump: - oss << "Error getting image pool"; + oss.str(get_error(method_name, "IMAGE", -1)); goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerImagePublish.cc b/src/rm/RequestManagerImagePublish.cc index cfd0bc8bd9..ad3d1c899b 100644 --- a/src/rm/RequestManagerImagePublish.cc +++ b/src/rm/RequestManagerImagePublish.cc @@ -38,6 +38,8 @@ void RequestManager::ImagePublish::execute( Image * image; ostringstream oss; + + const string method_name = "ImagePublish"; vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -102,16 +104,15 @@ void RequestManager::ImagePublish::execute( return; error_authenticate: - oss << "[ImagePublish] User not authenticated, aborting call."; + oss.str(authenticate_error(method_name)); goto error_common; error_image_get: - oss << "[ImagePublish] Error getting image with ID = " << iid; + oss.str(get_error(method_name, "IMAGE", iid)); goto error_common; error_authorize: - oss << "[ImagePublish] User not authorized to publish/unpublish image" << - ", aborting call."; + oss.str(authorization_error(method_name, "MANAGE", "IMAGE", uid, iid)); goto error_common; error_common: diff --git a/src/rm/RequestManagerImageRemoveAttribute.cc b/src/rm/RequestManagerImageRemoveAttribute.cc index b00c88c1ef..2aba7ffc7d 100644 --- a/src/rm/RequestManagerImageRemoveAttribute.cc +++ b/src/rm/RequestManagerImageRemoveAttribute.cc @@ -38,6 +38,8 @@ void RequestManager::ImageRemoveAttribute::execute( Image * image; ostringstream oss; + + const string method_name = "ImageRemoveAttribute"; vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -105,21 +107,19 @@ void RequestManager::ImageRemoveAttribute::execute( return; error_authenticate: - oss << "[ImageRemoveAttribute] User not authenticated, aborting call."; + oss.str(authenticate_error(method_name)); goto error_common; error_image_get: - oss << "[ImageRemoveAttribute] Error getting image with ID = " << iid; + oss.str(get_error(method_name, "IMAGE", iid)); goto error_common; error_authorize: - oss << "[ImageRemoveAttribute] User not authorized to remove image" << - " attributes aborting call."; + oss.str(authorization_error(method_name, "MANAGE", "IMAGE", uid, iid)); goto error_common; error_remove_attribute: - oss << "[ImageRemoveAttribute] Cannot remove attribute with name = " - << name << " for image [" << iid << "]"; + oss.str(action_error(method_name, "PUBLISH/UNPUBLISH", "IMAGE", iid, rc)); image->unlock(); goto error_common; diff --git a/src/rm/RequestManagerImageUpdate.cc b/src/rm/RequestManagerImageUpdate.cc index 4af4a054a7..5783c7dad7 100644 --- a/src/rm/RequestManagerImageUpdate.cc +++ b/src/rm/RequestManagerImageUpdate.cc @@ -42,7 +42,8 @@ void RequestManager::ImageUpdate::execute( vector arrayData; xmlrpc_c::value_array * arrayresult; - + + const string method_name = "ImageUpdate"; NebulaLog::log("ReM",Log::DEBUG,"ImageUpdate invoked"); @@ -109,20 +110,19 @@ void RequestManager::ImageUpdate::execute( return; error_authenticate: - oss << "User not authenticated, aborting ImageUpdate call."; + oss.str(authenticate_error(method_name)); goto error_common; error_image_get: - oss << "Error getting image with ID = " << iid; + oss.str(get_error(method_name, "IMAGE", iid)); goto error_common; error_authorize: - oss << "User not authorized to modify image attributes " << - ", aborting ImageUpdate call."; + oss.str(authorization_error(method_name, "MANAGE", "IMAGE", uid, iid)); goto error_common; error_update: - oss << "Cannot modify image [" << iid << "] attribute with name = " << name; + oss.str(action_error(method_name, "UPDATE ATTRIBUTE", "IMAGE", iid, rc)); image->unlock(); goto error_common; diff --git a/src/rm/RequestManagerInfo.cc b/src/rm/RequestManagerInfo.cc index d53624efe8..1fed884046 100644 --- a/src/rm/RequestManagerInfo.cc +++ b/src/rm/RequestManagerInfo.cc @@ -29,7 +29,9 @@ void RequestManager::VirtualMachineInfo::execute( int vid, rc; VirtualMachine * vm; - ostringstream oss; + ostringstream oss; + + const string method_name = "VirtualMachineInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -74,11 +76,11 @@ void RequestManager::VirtualMachineInfo::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_vm_get: - oss << "Error getting VM " << vid; + oss.str(get_error(method_name, "VM", vid)); goto error_common; error_common: diff --git a/src/rm/RequestManagerMigrate.cc b/src/rm/RequestManagerMigrate.cc index 11088a6ddd..5658020871 100644 --- a/src/rm/RequestManagerMigrate.cc +++ b/src/rm/RequestManagerMigrate.cc @@ -48,6 +48,8 @@ void RequestManager::VirtualMachineMigrate::execute( xmlrpc_c::value_array * arrayresult; ostringstream oss; + + const string method_name = "VirtualMachineMigrate"; NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineMigrate invoked"); @@ -146,27 +148,28 @@ void RequestManager::VirtualMachineMigrate::execute( error_host_get: - oss << "The host " << hid << " does not exists"; + oss.str(get_error(method_name, "HOST", hid)); goto error_common; error_vm_get: - oss << "The virtual machine " << vid << " does not exists"; + oss.str(get_error(method_name, "VM", vid)); goto error_common; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common_lock; error_authorize: - oss << "User not authorized to migrate VM on host"; + oss.str(authorization_error(method_name, "MANAGE", "VM", uid, vid)); goto error_common_lock; error_history: - oss << "Can not insert history to migrate VM"; + oss.str(action_error(method_name, "INSERT HISTORY", "VM", vid, rc)); goto error_common_lock; error_state: - oss << "Can not migrate VM, wrong state"; + oss.str(action_error(method_name, "MANAGE", "VM", vid, rc)); + oss << " Reason: VM in wrong state."; goto error_common_lock; error_common_lock: diff --git a/src/rm/RequestManagerPoolInfo.cc b/src/rm/RequestManagerPoolInfo.cc index 6196da19c7..ba4d4dfd5c 100644 --- a/src/rm/RequestManagerPoolInfo.cc +++ b/src/rm/RequestManagerPoolInfo.cc @@ -37,6 +37,8 @@ void RequestManager::VirtualMachinePoolInfo::execute( /* -- RPC specific vars -- */ vector arrayData; xmlrpc_c::value_array * arrayresult; + + const string method_name = "VirtualMachinePoolInfo"; NebulaLog::log("ReM",Log::DEBUG,"VirtualMachinePoolInfo method invoked"); @@ -86,11 +88,11 @@ void RequestManager::VirtualMachinePoolInfo::execute( return; error_authenticate: - oss << "Error in user authentication"; + oss.str(authenticate_error(method_name)); goto error_common; error_dump: - oss << "Error getting the pool info"; + oss.str(get_error(method_name, "VM", -1)); goto error_common; error_common: diff --git a/src/rm/RequestManagerUserAllocate.cc b/src/rm/RequestManagerUserAllocate.cc index e38bb4ee45..e31759295d 100644 --- a/src/rm/RequestManagerUserAllocate.cc +++ b/src/rm/RequestManagerUserAllocate.cc @@ -37,6 +37,8 @@ void RequestManager::UserAllocate::execute( ostringstream oss; User * user; + + const string method_name = "UserAllocate"; /* -- RPC specific vars -- */ vector arrayData; @@ -103,20 +105,21 @@ void RequestManager::UserAllocate::execute( return; error_authenticate: - oss << "User not authenticated, aborting UserAllocate call."; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to add new users, aborting UserAllocate call."; + oss.str(authorization_error(method_name, "CREATE", "USER", rc, NULL)); goto error_common; error_duplicate: - oss << "Existing user, cannot duplicate."; + oss.str(action_error(method_name, "CREATE", "USER", NULL, NULL)); + oss << " Reason: Existing user, cannot duplicate."; goto error_common; error_allocate: - oss << "Error allocating user."; + oss.str(action_error(method_name, "CREATE", "USER", NULL, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerUserDelete.cc b/src/rm/RequestManagerUserDelete.cc index d1a388ea67..c6817d2b00 100644 --- a/src/rm/RequestManagerUserDelete.cc +++ b/src/rm/RequestManagerUserDelete.cc @@ -26,13 +26,15 @@ void RequestManager::UserDelete::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) { - string session; + string session; - int uid; - User * user; + int uid; + User * user; - int rc; + int rc; ostringstream oss; + + const string method_name = "UserDelete"; /* -- RPC specific vars -- */ vector arrayData; @@ -103,24 +105,24 @@ void RequestManager::UserDelete::execute( return; error_oneadmin_deletion: - oss << "User oneadmin cannot be deleted"; + oss.str(action_error(method_name, "DELETE", "USER", uid, NULL)); + oss << " Reason: Oneadmin cannot be deleted."; goto error_common; error_authenticate: - oss << "User not authenticated, aborting UserDelete call."; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to delete user with uid " << uid - << ", aborting UserDelete call."; + oss.str(authorization_error(method_name, "DELETE", "USER", rc, uid)); goto error_common; error_get_user: - oss << "Error retrieving user " << uid; + oss.str(get_error(method_name, "USER", uid)); goto error_common; error_delete: - oss << "Error deleting user " << uid; + oss.str(action_error(method_name, "DELETE", "USER", uid, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerUserPoolInfo.cc b/src/rm/RequestManagerUserPoolInfo.cc index 96bd11f977..7138a4a3a2 100644 --- a/src/rm/RequestManagerUserPoolInfo.cc +++ b/src/rm/RequestManagerUserPoolInfo.cc @@ -28,6 +28,8 @@ void RequestManager::UserPoolInfo::execute( int rc; ostringstream oss; + + const string method_name = "UserPoolInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -67,15 +69,14 @@ void RequestManager::UserPoolInfo::execute( return; error_authenticate: - oss << "User not authorized to pull user pool info. Error code: " << rc; + oss.str(authenticate_error(method_name)); goto error_common; error_dumping: - oss << "Error dumping pool info"; + oss.str(get_error(method_name, "IMAGE", -1)); goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 3c97f902b2..1be91fdb19 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -39,6 +39,8 @@ void RequestManager::VirtualNetworkAllocate::execute( User * user; ostringstream oss; + + const string method_name = "VirtualNetworkAllocate"; /* -- RPC specific vars -- */ vector arrayData; @@ -52,7 +54,7 @@ void RequestManager::VirtualNetworkAllocate::execute( if ( User::split_secret(session,username,password) != 0 ) { - goto error_session; + goto error_authenticate; } // Now let's get the user @@ -86,16 +88,16 @@ void RequestManager::VirtualNetworkAllocate::execute( return; -error_session: - oss << "Session information malformed, cannot allocate VirtualNetwork"; +error_authenticate: + oss.str(authenticate_error(method_name)); goto error_common; error_get_user: - oss << "User not recognized, cannot allocate VirtualNetwork"; + oss.str(get_error(method_name, "USER", -1)); goto error_common; error_vn_allocate: - oss << "Error allocating VN with template: " << endl << stemplate; + oss.str(action_error(method_name, "ALLOCATE", "NET", NULL, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerVirtualNetworkDelete.cc b/src/rm/RequestManagerVirtualNetworkDelete.cc index 6ffe35e113..d365f36ca3 100644 --- a/src/rm/RequestManagerVirtualNetworkDelete.cc +++ b/src/rm/RequestManagerVirtualNetworkDelete.cc @@ -36,6 +36,8 @@ void RequestManager::VirtualNetworkDelete::execute( int rc; ostringstream oss; + + const string method_name = "VirtualNetworkDelete"; /* -- RPC specific vars -- */ vector arrayData; @@ -94,19 +96,19 @@ void RequestManager::VirtualNetworkDelete::execute( return; error_authenticate: - oss << "User not authenticated, aborting VirtualNetworkDelete call"; + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: - oss << "User not authorized to delete Virtual Network with NID = " << nid; + oss.str(authorization_error(method_name, "DELETE", "NET", rc, nid)); goto error_common; error_vn_get: - oss << "Error getting Virtual Network with NID = " << nid; + oss.str(get_error(method_name, "NET", nid)); goto error_common; error_common: - NebulaLog::log ("Rem",Log::ERROR,oss); + NebulaLog::log ("ReM",Log::ERROR,oss); arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerVirtualNetworkInfo.cc b/src/rm/RequestManagerVirtualNetworkInfo.cc index 5649c96b35..0ce88949e6 100644 --- a/src/rm/RequestManagerVirtualNetworkInfo.cc +++ b/src/rm/RequestManagerVirtualNetworkInfo.cc @@ -35,6 +35,8 @@ void RequestManager::VirtualNetworkInfo::execute( VirtualNetwork * vn; ostringstream oss; + + const string method_name = "VirtualNetworkInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -95,17 +97,16 @@ void RequestManager::VirtualNetworkInfo::execute( return; error_authenticate: - oss << "User not authenticated, VirtualNetworkInfo call aborted."; + oss.str(authenticate_error(method_name)); goto error_common; error_vn_get: - oss << "Error getting Virtual Network with NID = " << nid; + oss.str(get_error(method_name, "NET", nid)); goto error_common; error_authorize: vn->unlock(); - oss << "User not authorized to view VirtualNetwork" << - ", VirtualNetworkInfo call aborted."; + oss.str(authorization_error(method_name, "USE", "NET", rc, nid)); goto error_common; error_common: diff --git a/src/rm/RequestManagerVirtualNetworkPoolInfo.cc b/src/rm/RequestManagerVirtualNetworkPoolInfo.cc index ac1294d203..216d3d82f5 100755 --- a/src/rm/RequestManagerVirtualNetworkPoolInfo.cc +++ b/src/rm/RequestManagerVirtualNetworkPoolInfo.cc @@ -31,11 +31,13 @@ void RequestManager::VirtualNetworkPoolInfo::execute( ostringstream oss; ostringstream where_string; - int rc; + int rc; int filter_flag; User * user; - + + const string method_name = "VirtualNetworkPoolInfo"; + /* -- RPC specific vars -- */ vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -105,15 +107,15 @@ void RequestManager::VirtualNetworkPoolInfo::execute( return; error_authenticate: - oss << "User not authenticated, NetworkPoolInfo call aborted."; + oss.str(authenticate_error(method_name)); goto error_common; error_get_user: - oss << "Error getting user, aborting NetworkPoolInfo call"; + oss.str(get_error(method_name, "USER", -1)); goto error_common; error_dump: - oss << "Error getting virtual network pool"; + oss.str(get_error(method_name, "HOST", -1)); goto error_common; error_common: diff --git a/src/rm/RequestManagerVirtualNetworkPublish.cc b/src/rm/RequestManagerVirtualNetworkPublish.cc index f60bb538eb..6d6874f52b 100644 --- a/src/rm/RequestManagerVirtualNetworkPublish.cc +++ b/src/rm/RequestManagerVirtualNetworkPublish.cc @@ -39,6 +39,8 @@ void RequestManager::VirtualNetworkPublish::execute( ostringstream oss; + const string method_name = "VirtualNetworkPublish"; + vector arrayData; xmlrpc_c::value_array * arrayresult; @@ -102,16 +104,15 @@ void RequestManager::VirtualNetworkPublish::execute( return; error_authenticate: - oss << "[VirtualNetworkPublish] User not authenticated, aborting call."; + oss.str(authenticate_error(method_name)); goto error_common; error_vn_get: - oss << "[VirtualNetworkPublish] Error getting VN with ID = " << nid; + oss.str(get_error(method_name, "NET", nid)); goto error_common; error_authorize: - oss << "[VirtualNetworkPublish] User not authorized to (un)publish VN" << - ", aborting call."; + oss.str(authorization_error(method_name, "MANAGE", "NET", uid, nid)); vn->unlock(); goto error_common; From ccabd55000335d51fed69dc39a37cda1275cf358 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 13 Jul 2010 13:50:35 +0200 Subject: [PATCH 45/64] feature #203: Added tests for simple_permissions and quota setup --- src/authm_mad/simple_permissions.rb | 8 +- src/authm_mad/spec/simple_permissions_spec.rb | 91 +++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 src/authm_mad/spec/simple_permissions_spec.rb diff --git a/src/authm_mad/simple_permissions.rb b/src/authm_mad/simple_permissions.rb index 347205aa33..87952c7013 100644 --- a/src/authm_mad/simple_permissions.rb +++ b/src/authm_mad/simple_permissions.rb @@ -1,8 +1,10 @@ +require 'quota' + class SimplePermissions - def initialize(database=nil) - # initialize quota + def initialize(database, client, conf={}) + @quota=Quota.new(database, client, conf[:quota] || {}) end def auth_message(result, message) @@ -22,7 +24,7 @@ class SimplePermissions end def auth_object(uid, object, id, action, owner, pub) - return true if uid==0 + return true if uid=='0' case object when 'VM' diff --git a/src/authm_mad/spec/simple_permissions_spec.rb b/src/authm_mad/spec/simple_permissions_spec.rb new file mode 100644 index 0000000000..67f6401c05 --- /dev/null +++ b/src/authm_mad/spec/simple_permissions_spec.rb @@ -0,0 +1,91 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed 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. # +#--------------------------------------------------------------------------- # + +require 'spec_common' + +require 'client_mock' +require 'simple_permissions' + +def gen_tokens(user_, action_, options={}) + user=user_.to_s + action=action_.to_s.upcase + + options={ + :vm => true, + :host => true + }.merge!(options) + + pub=(options[:public] ? '1' : '0') + id=(options[:id] ? options[:id].to_s : '1') + #owner=(options[:owner] ? options[:owner].to_s : '1') + + if action=='CREATE' + vmowner='-' + vmid='-' + else + vmowner=user + vmid=user + end + + tokens=[] + tokens<<"VM:#{vmid}:#{action}:#{vmowner}:#{pub}" if options[:vm] + tokens+=[ + "NET:#{id}:#{action}:#{user}:#{pub}", + "IMAGE:#{id}:#{action}:#{user}:#{pub}" + ] + tokens<<"HOST:#{id}:#{action}:#{user}:#{pub}" if options[:host] + + #pp tokens + + tokens +end + +describe SimplePermissions do + before(:all) do + @db=Sequel.sqlite + mock_data=YAML::load(File.read('spec/oca_vms.yaml')) + client=ClientMock.new(mock_data) + @perm=SimplePermissions.new(@db, client) + end + + it 'should let root manage everything' do + @perm.auth(0, gen_tokens(1, :create)).should == true + @perm.auth(0, gen_tokens(1, :delete)).should == true + @perm.auth(0, gen_tokens(1, :manage)).should == true + @perm.auth(0, gen_tokens(1, :use)).should == true + end + + it 'should let anyone use public objects' do + @perm.auth(2, gen_tokens(1, :delete, :public => true)). + should_not == true + @perm.auth(2, gen_tokens(1, :manage, :public => true)). + should_not == true + + # take out vm create (the first token) as it can not be "used" by + # other users even if public + @perm.auth(2, gen_tokens(1, :use, :public => true, :vm => false)). + should == true + end + + it 'should let users use their own objects' do + @perm.auth(1, gen_tokens(1, :create, :host => false)).should == true + @perm.auth(1, gen_tokens(1, :delete, :host => false)).should == true + @perm.auth(1, gen_tokens(1, :manage, :host => false)).should == true + @perm.auth(1, gen_tokens(1, :use)).should == true + end +end + + From 0659115f705e3dbb1722a4387cf6cf4903b3835d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Tue, 13 Jul 2010 18:16:05 +0200 Subject: [PATCH 46/64] feature #203: Finishing RM authorization task --- include/AuthManager.h | 12 ++-- src/authm/AuthManager.cc | 19 +++++-- src/rm/RequestManagerClusterAdd.cc | 7 ++- src/rm/RequestManagerClusterAllocate.cc | 4 +- src/rm/RequestManagerClusterDelete.cc | 4 +- src/rm/RequestManagerHostInfo.cc | 10 ++-- src/rm/RequestManagerImageAllocate.cc | 6 +- src/rm/RequestManagerImageDelete.cc | 26 +++++++-- src/rm/RequestManagerImageEnable.cc | 26 +++++++-- src/rm/RequestManagerImageInfo.cc | 34 ++--------- src/rm/RequestManagerImagePoolInfo.cc | 20 +------ src/rm/RequestManagerImagePublish.cc | 57 ++++++++++++------- src/rm/RequestManagerImageRemoveAttribute.cc | 24 ++++++-- src/rm/RequestManagerImageUpdate.cc | 24 ++++++-- src/rm/RequestManagerUserPoolInfo.cc | 35 ++++++++++-- src/rm/RequestManagerVirtualNetworkInfo.cc | 22 ------- .../RequestManagerVirtualNetworkPoolInfo.cc | 2 + src/rm/RequestManagerVirtualNetworkPublish.cc | 6 +- 18 files changed, 195 insertions(+), 143 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index f9c7afdd94..d29a163560 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -276,10 +276,11 @@ public: */ enum Operation { - CREATE, /** Authorization to create an object (host, vm, net, image)*/ - DELETE, /** Authorization to delete an object */ - USE, /** Authorization to use an object */ - MANAGE /** Authorization to manage an object */ + CREATE, /** Authorization to create an object (host, vm, net, image)*/ + DELETE, /** Authorization to delete an object */ + USE, /** Authorization to use an object */ + MANAGE, /** Authorization to manage an object */ + INFO /** Authorization to view an object */ }; /** @@ -291,7 +292,8 @@ public: HOST, NET, IMAGE, - USER + USER, + CLUSTER }; /** diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 4704c2031a..d3ccf6a6d4 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -80,11 +80,12 @@ void AuthRequest::add_auth(Object ob, switch (ob) { - case VM: oss << "VM:" ; break; - case HOST: oss << "HOST:" ; break; - case NET: oss << "NET:" ; break; - case IMAGE: oss << "IMAGE:" ; break; - case USER: oss << "USER:" ; break; + case VM: oss << "VM:" ; break; + case HOST: oss << "HOST:" ; break; + case NET: oss << "NET:" ; break; + case IMAGE: oss << "IMAGE:" ; break; + case USER: oss << "USER:" ; break; + case CLUSTER: oss << "CLUSTER:" ; break; } if (op == CREATE) //encode the ob_id, it is a template @@ -123,6 +124,10 @@ void AuthRequest::add_auth(Object ob, case MANAGE: oss << "MANAGE:" ; break; + + case INFO: + oss << "INFO:" ; + break; } oss << owner << ":" << pub; @@ -166,6 +171,10 @@ void AuthRequest::add_auth(Object ob, case MANAGE: auth = owner == uid; break; + + case INFO: // This is for completeness, as the only INFO existing + // is for UserPool, and just oneadmin can see it + break; } } diff --git a/src/rm/RequestManagerClusterAdd.cc b/src/rm/RequestManagerClusterAdd.cc index 199928e6e4..b42f156e61 100644 --- a/src/rm/RequestManagerClusterAdd.cc +++ b/src/rm/RequestManagerClusterAdd.cc @@ -61,8 +61,9 @@ void RequestManager::ClusterAdd::execute( if ( rc != 0 ) // rc == 0 means oneadmin { AuthRequest ar(rc); - + ar.add_auth(AuthRequest::HOST,hid,AuthRequest::MANAGE,0,false); + ar.add_auth(AuthRequest::CLUSTER,clid,AuthRequest::USE,0,false); if (UserPool::authorize(ar) == -1) { @@ -107,7 +108,7 @@ error_authenticate: goto error_common; error_authorize: - oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, hid)); + oss.str(authorization_error(method_name, "USE", "CLUSTER", rc, clid)); goto error_common; error_host_get: @@ -116,7 +117,7 @@ error_host_get: error_cluster_add: host->unlock(); - oss.str(action_error(method_name, "MANAGE", "HOST", hid, rc)); + oss.str(action_error(method_name, "USE", "CLUSTER", clid, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterAllocate.cc b/src/rm/RequestManagerClusterAllocate.cc index 05dbb647f2..72e154e6e5 100644 --- a/src/rm/RequestManagerClusterAllocate.cc +++ b/src/rm/RequestManagerClusterAllocate.cc @@ -59,7 +59,7 @@ void RequestManager::ClusterAllocate::execute( { AuthRequest ar(rc); - ar.add_auth(AuthRequest::HOST,-1,AuthRequest::MANAGE,0,false); + ar.add_auth(AuthRequest::CLUSTER,-1,AuthRequest::CREATE,0,false); if (UserPool::authorize(ar) == -1) { @@ -91,7 +91,7 @@ error_authenticate: goto error_common; error_authorize: - oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, -1)); + oss.str(authorization_error(method_name, "CREATE", "CLUSTER", rc, -1)); goto error_common; error_cluster_allocate: diff --git a/src/rm/RequestManagerClusterDelete.cc b/src/rm/RequestManagerClusterDelete.cc index e840dd6689..ddfcee56e3 100644 --- a/src/rm/RequestManagerClusterDelete.cc +++ b/src/rm/RequestManagerClusterDelete.cc @@ -58,7 +58,7 @@ void RequestManager::ClusterDelete::execute( { AuthRequest ar(rc); - ar.add_auth(AuthRequest::HOST,-1,AuthRequest::MANAGE,0,false); + ar.add_auth(AuthRequest::CLUSTER,clid,AuthRequest::DELETE,0,false); if (UserPool::authorize(ar) == -1) { @@ -89,7 +89,7 @@ error_authenticate: goto error_common; error_authorize: - oss.str(authorization_error(method_name, "MANAGE", "HOST", rc, -1)); + oss.str(authorization_error(method_name, "DELETE", "CLUSTER", rc, clid)); goto error_common; error_cluster_delete: diff --git a/src/rm/RequestManagerHostInfo.cc b/src/rm/RequestManagerHostInfo.cc index c94b73c24f..f6b351b2d6 100644 --- a/src/rm/RequestManagerHostInfo.cc +++ b/src/rm/RequestManagerHostInfo.cc @@ -24,11 +24,11 @@ void RequestManager::HostInfo::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) { - string session; - - int hid; - int rc; - Host * host; + string session; + + int hid; + int rc; + Host * host; ostringstream oss; diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 10f47a047c..74357cf73b 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -51,15 +51,13 @@ void RequestManager::ImageAllocate::execute( // First, we need to authenticate the user - rc = ImageAllocate::upool->authenticate(session); + uid = ImageAllocate::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } - uid = rc; - rc = ImageAllocate::ipool->allocate(uid,image_template,&iid); if ( rc < 0 ) diff --git a/src/rm/RequestManagerImageDelete.cc b/src/rm/RequestManagerImageDelete.cc index 846e77109b..6d7813063c 100644 --- a/src/rm/RequestManagerImageDelete.cc +++ b/src/rm/RequestManagerImageDelete.cc @@ -33,6 +33,9 @@ void RequestManager::ImageDelete::execute( int iid; int uid; int rc; + + int image_owner; + bool is_public; Image * image; @@ -51,21 +54,36 @@ void RequestManager::ImageDelete::execute( // First, we need to authenticate the user - rc = ImageDelete::upool->authenticate(session); + uid = ImageDelete::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } + + // Get image from the ImagePool + image = ImageDelete::ipool->get(iid,true); - uid = rc; + if ( image == 0 ) + { + goto error_image_get; + } + + image_owner = image->get_uid(); + is_public = image->isPublic(); + + image->unlock(); //Authorize the operation if ( uid != 0 ) // uid == 0 means oneadmin { AuthRequest ar(uid); - ar.add_auth(AuthRequest::IMAGE,iid,AuthRequest::DELETE,0,false); + ar.add_auth(AuthRequest::IMAGE, + iid, + AuthRequest::DELETE, + image_owner, + is_public); if (UserPool::authorize(ar) == -1) { diff --git a/src/rm/RequestManagerImageEnable.cc b/src/rm/RequestManagerImageEnable.cc index e21110b42c..498875fb28 100644 --- a/src/rm/RequestManagerImageEnable.cc +++ b/src/rm/RequestManagerImageEnable.cc @@ -35,6 +35,9 @@ void RequestManager::ImageEnable::execute( int uid; int rc; + int image_owner; + bool is_public; + Image * image; ostringstream oss; @@ -52,21 +55,36 @@ void RequestManager::ImageEnable::execute( enable_flag = xmlrpc_c::value_boolean(paramList.getBoolean(2)); // First, we need to authenticate the user - rc = ImageEnable::upool->authenticate(session); + uid = ImageEnable::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } - uid = rc; + // Get image from the ImagePool + image = ImageEnable::ipool->get(iid,true); + + if ( image == 0 ) + { + goto error_image_get; + } + + image_owner = image->get_uid(); + is_public = image->isPublic(); + + image->unlock(); //Authorize the operation if ( uid != 0 ) // uid == 0 means oneadmin { AuthRequest ar(uid); - ar.add_auth(AuthRequest::IMAGE,iid,AuthRequest::MANAGE,0,false); + ar.add_auth(AuthRequest::IMAGE, + iid, + AuthRequest::MANAGE, + image_owner, + is_public); if (UserPool::authorize(ar) == -1) { diff --git a/src/rm/RequestManagerImageInfo.cc b/src/rm/RequestManagerImageInfo.cc index cb3c838de4..9359460887 100644 --- a/src/rm/RequestManagerImageInfo.cc +++ b/src/rm/RequestManagerImageInfo.cc @@ -28,14 +28,14 @@ void RequestManager::ImageInfo::execute( { string session; - int iid; - int uid; // Image owner user id - int rc; // Requesting user id - Image * image; - + int iid; + int uid; // Image owner user id + int rc; // Requesting user id + Image * image; + ostringstream oss; - const string method_name = "ImageInfo"; + const string method_name = "ImageInfo"; /* -- RPC specific vars -- */ vector arrayData; @@ -65,23 +65,6 @@ void RequestManager::ImageInfo::execute( goto error_authenticate; } - //Authorize the operation - if ( rc != 0 ) // rc == 0 means oneadmin - { - AuthRequest ar(rc); - - ar.add_auth(AuthRequest::IMAGE, - iid, - AuthRequest::USE, - 0, - image->isPublic()); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - oss << *image; image->unlock(); @@ -107,11 +90,6 @@ error_authenticate: image->unlock(); goto error_common; -error_authorize: - oss.str(authorization_error(method_name, "USE", "IMAGE", rc, iid)); - image->unlock(); - goto error_common; - error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerImagePoolInfo.cc b/src/rm/RequestManagerImagePoolInfo.cc index b0c3b4e64d..d634243c3e 100755 --- a/src/rm/RequestManagerImagePoolInfo.cc +++ b/src/rm/RequestManagerImagePoolInfo.cc @@ -46,14 +46,12 @@ void RequestManager::ImagePoolInfo::execute( filter_flag = xmlrpc_c::value_int(paramList.getInt(1)); // Check if it is a valid user - rc = ImagePoolInfo::upool->authenticate(session); + uid = ImagePoolInfo::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } - - uid = rc; where_string.str(""); @@ -70,21 +68,13 @@ void RequestManager::ImagePoolInfo::execute( switch(filter_flag) { case -2: - if ( uid != 0 ) - { - goto error_authorization; - } + // TODO define authentication bug #278 // where remains empty. break; case -1: where_string << "UID=" << uid << " OR public = 'YES'"; break; default: - // Only oneadmin or the user can list a specific user's images. - if ( uid != 0 && uid != filter_flag ) - { - goto error_authorization; - } where_string << "UID=" << filter_flag; } @@ -114,10 +104,6 @@ error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; -error_authorization: - oss.str(authorization_error(method_name, "USE", "IMAGE", uid, NULL)); - goto error_common; - error_filter_flag: oss << "Incorrect filter_flag, must be >= -2."; goto error_common; diff --git a/src/rm/RequestManagerImagePublish.cc b/src/rm/RequestManagerImagePublish.cc index ad3d1c899b..9137766983 100644 --- a/src/rm/RequestManagerImagePublish.cc +++ b/src/rm/RequestManagerImagePublish.cc @@ -33,7 +33,9 @@ void RequestManager::ImagePublish::execute( int iid; bool publish_flag; int uid; - int rc; + + int image_owner; + bool is_public; Image * image; @@ -52,32 +54,13 @@ void RequestManager::ImagePublish::execute( publish_flag = xmlrpc_c::value_boolean(paramList.getBoolean(2)); // First, we need to authenticate the user - rc = ImagePublish::upool->authenticate(session); + uid = ImagePublish::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } - uid = rc; - - //Authorize the operation - if ( uid != 0 ) // uid == 0 means oneadmin - { - AuthRequest ar(uid); - - ar.add_auth(AuthRequest::IMAGE, - iid, - AuthRequest::MANAGE, - 0, - image->isPublic()); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - // Get image from the ImagePool image = ImagePublish::ipool->get(iid,true); @@ -86,6 +69,36 @@ void RequestManager::ImagePublish::execute( goto error_image_get; } + image_owner = image->get_uid(); + is_public = image->isPublic(); + + image->unlock(); + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::IMAGE, + iid, + AuthRequest::MANAGE, + image_owner, + is_public); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + // Get the image locked again + image = ImagePublish::ipool->get(iid,true); + + if ( image == 0 ) + { + goto error_image_get; + } + image->publish(publish_flag); ImagePublish::ipool->update(image); diff --git a/src/rm/RequestManagerImageRemoveAttribute.cc b/src/rm/RequestManagerImageRemoveAttribute.cc index 2aba7ffc7d..a86d05882b 100644 --- a/src/rm/RequestManagerImageRemoveAttribute.cc +++ b/src/rm/RequestManagerImageRemoveAttribute.cc @@ -34,6 +34,9 @@ void RequestManager::ImageRemoveAttribute::execute( int iid; int uid; int rc; + + int image_owner; + bool is_public; Image * image; @@ -52,14 +55,25 @@ void RequestManager::ImageRemoveAttribute::execute( name = xmlrpc_c::value_string(paramList.getString(2)); // First, we need to authenticate the user - rc = ImageRemoveAttribute::upool->authenticate(session); + uid = ImageRemoveAttribute::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } + + // Get image from the ImagePool + image = ImageRemoveAttribute::ipool->get(iid,true); - uid = rc; + if ( image == 0 ) + { + goto error_image_get; + } + + image_owner = image->get_uid(); + is_public = image->isPublic(); + + image->unlock(); //Authorize the operation if ( uid != 0 ) // uid == 0 means oneadmin @@ -69,8 +83,8 @@ void RequestManager::ImageRemoveAttribute::execute( ar.add_auth(AuthRequest::IMAGE, iid, AuthRequest::MANAGE, - 0, - image->isPublic()); + image_owner, + is_public); if (UserPool::authorize(ar) == -1) { diff --git a/src/rm/RequestManagerImageUpdate.cc b/src/rm/RequestManagerImageUpdate.cc index 5783c7dad7..acc6eef587 100644 --- a/src/rm/RequestManagerImageUpdate.cc +++ b/src/rm/RequestManagerImageUpdate.cc @@ -35,6 +35,9 @@ void RequestManager::ImageUpdate::execute( string name; string value; int rc; + + int image_owner; + bool is_public; Image * image; @@ -53,14 +56,25 @@ void RequestManager::ImageUpdate::execute( value = xmlrpc_c::value_string(paramList.getString(3)); // First, we need to authenticate the user - rc = ImageUpdate::upool->authenticate(session); + uid = ImageUpdate::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } + + // Get image from the ImagePool + image = ImageUpdate::ipool->get(iid,true); - uid = rc; + if ( image == 0 ) + { + goto error_image_get; + } + + image_owner = image->get_uid(); + is_public = image->isPublic(); + + image->unlock(); //Authorize the operation if ( uid != 0 ) // uid == 0 means oneadmin @@ -70,8 +84,8 @@ void RequestManager::ImageUpdate::execute( ar.add_auth(AuthRequest::IMAGE, iid, AuthRequest::MANAGE, - 0, - image->isPublic()); + image_owner, + is_public); if (UserPool::authorize(ar) == -1) { diff --git a/src/rm/RequestManagerUserPoolInfo.cc b/src/rm/RequestManagerUserPoolInfo.cc index 7138a4a3a2..70c9434b90 100644 --- a/src/rm/RequestManagerUserPoolInfo.cc +++ b/src/rm/RequestManagerUserPoolInfo.cc @@ -17,6 +17,8 @@ #include "RequestManager.h" #include "NebulaLog.h" +#include "AuthManager.h" + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -26,7 +28,8 @@ void RequestManager::UserPoolInfo::execute( { string session; - int rc; + int rc; + int uid; ostringstream oss; const string method_name = "UserPoolInfo"; @@ -41,15 +44,31 @@ void RequestManager::UserPoolInfo::execute( session = xmlrpc_c::value_string(paramList.getString(0)); // Only oneadmin can list the whole user pool - rc = UserPoolInfo::upool->authenticate(session); + uid = UserPoolInfo::upool->authenticate(session); - if ( rc != 0 ) + if ( uid == -1 ) { goto error_authenticate; } - // Now let's get the info - + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::USER, + -1, + AuthRequest::INFO, + 0, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + // Now let's get the info rc = UserPoolInfo::upool->dump(oss,""); if ( rc != 0 ) @@ -71,9 +90,13 @@ void RequestManager::UserPoolInfo::execute( error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; + +error_authorize: + oss.str(authorization_error(method_name, "INFO", "USER", uid, -1)); + goto error_common; error_dumping: - oss.str(get_error(method_name, "IMAGE", -1)); + oss.str(get_error(method_name, "USER", -1)); goto error_common; error_common: diff --git a/src/rm/RequestManagerVirtualNetworkInfo.cc b/src/rm/RequestManagerVirtualNetworkInfo.cc index 0ce88949e6..664cf4fdc4 100644 --- a/src/rm/RequestManagerVirtualNetworkInfo.cc +++ b/src/rm/RequestManagerVirtualNetworkInfo.cc @@ -63,23 +63,6 @@ void RequestManager::VirtualNetworkInfo::execute( goto error_vn_get; } - //Authorize the operation - if ( rc != 0 ) // rc == 0 means oneadmin - { - AuthRequest ar(rc); - - ar.add_auth(AuthRequest::NET, - nid, - AuthRequest::USE, - 0, - vn->isPublic()); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - oss << *vn; vn->unlock(); @@ -104,11 +87,6 @@ error_vn_get: oss.str(get_error(method_name, "NET", nid)); goto error_common; -error_authorize: - vn->unlock(); - oss.str(authorization_error(method_name, "USE", "NET", rc, nid)); - goto error_common; - error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerVirtualNetworkPoolInfo.cc b/src/rm/RequestManagerVirtualNetworkPoolInfo.cc index 216d3d82f5..ba36bffaa5 100755 --- a/src/rm/RequestManagerVirtualNetworkPoolInfo.cc +++ b/src/rm/RequestManagerVirtualNetworkPoolInfo.cc @@ -63,6 +63,8 @@ void RequestManager::VirtualNetworkPoolInfo::execute( * -1 :: User's VMs * >=0 :: UID User's VMs **/ + + // TODO define authorization (bug #278) if (filter_flag == -1) { User::split_secret(session,username,password); diff --git a/src/rm/RequestManagerVirtualNetworkPublish.cc b/src/rm/RequestManagerVirtualNetworkPublish.cc index 6d6874f52b..4ec9c3d414 100644 --- a/src/rm/RequestManagerVirtualNetworkPublish.cc +++ b/src/rm/RequestManagerVirtualNetworkPublish.cc @@ -52,15 +52,13 @@ void RequestManager::VirtualNetworkPublish::execute( publish_flag = xmlrpc_c::value_boolean(paramList.getBoolean(2)); // First, we need to authenticate the user - rc = VirtualNetworkPublish::upool->authenticate(session); + uid = VirtualNetworkPublish::upool->authenticate(session); - if ( rc == -1 ) + if ( uid == -1 ) { goto error_authenticate; } - uid = rc; - // Get virtual network from the VirtualNetworkPool vn = VirtualNetworkPublish::vnpool->get(nid,true); From f10b7aff5735a88c54ed1d29d76e4e1724aab466 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 13 Jul 2010 18:17:25 +0200 Subject: [PATCH 47/64] feature #203: added auth mad config file --- install.sh | 3 ++- src/authm_mad/auth.conf | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 src/authm_mad/auth.conf diff --git a/install.sh b/install.sh index aa709e0ab7..733ab6bb53 100755 --- a/install.sh +++ b/install.sh @@ -451,7 +451,8 @@ HM_ETC_FILES="src/hm_mad/hmrc" # Hook Manager driver config. files, to be installed under $ETC_LOCATION/hm #------------------------------------------------------------------------------- -AUTH_ETC_FILES="src/authm_mad/auth_mad" +AUTH_ETC_FILES="src/authm_mad/auth_mad \ + src/authm_mad/auth.conf" #------------------------------------------------------------------------------- # Sample files, to be installed under $SHARE_LOCATION/examples diff --git a/src/authm_mad/auth.conf b/src/authm_mad/auth.conf new file mode 100644 index 0000000000..633bed5302 --- /dev/null +++ b/src/authm_mad/auth.conf @@ -0,0 +1,5 @@ +:database: sqlite://auth.db +:quota: + :defaults: + :cpu: 10.0 + :memory: 1048576 \ No newline at end of file From 213029ad26228fd5749a429e349008f86400c464 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 13 Jul 2010 18:18:23 +0200 Subject: [PATCH 48/64] feature #203: simple permissions check for quota --- src/authm_mad/simple_permissions.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/authm_mad/simple_permissions.rb b/src/authm_mad/simple_permissions.rb index 87952c7013..62f418b51b 100644 --- a/src/authm_mad/simple_permissions.rb +++ b/src/authm_mad/simple_permissions.rb @@ -1,5 +1,6 @@ require 'quota' +require 'base64' class SimplePermissions @@ -11,7 +12,24 @@ class SimplePermissions result ? true : message end + def get_vm_usage(data) + vm_xml=Base64::decode64(data) + vm=OpenNebula::VirtualMachine.new(vm_xml) + vm_hash=vm.to_hash + + # Should set more sensible defaults or get driver configuration + cpu=vm_hash['TEMPLATE']['CPU'] + cpu||=1.0 + + memory=vm_hash['TEMPLATE']['MEMORY'] + memory||=64 + + VmUsage.new(cpu, memory) + end + def auth(uid, tokens) + STDERR.puts [uid, tokens].inspect + result=true tokens.each do |token| @@ -39,8 +57,12 @@ class SimplePermissions def auth_vm(uid, object, id, action, owner, pub) case action when 'CREATE' - # add quota here - return true + STDERR.puts "create vm" + if @quota.check(uid, get_vm_usage(id)) + return true + else + return "Quota exceeded" + end else auth_message(uid==owner, "You cannot manage VM #{id}") end From dace9afa07742fc978bdce116cd11709c6ff3779 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 13 Jul 2010 18:20:04 +0200 Subject: [PATCH 49/64] feature #203: configuration loading and debug --- src/authm_mad/one_auth_mad.rb | 15 ++++++++++++++- src/authm_mad/quota.rb | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/authm_mad/one_auth_mad.rb b/src/authm_mad/one_auth_mad.rb index a32bbf1c5d..eb4deefb5d 100755 --- a/src/authm_mad/one_auth_mad.rb +++ b/src/authm_mad/one_auth_mad.rb @@ -30,16 +30,29 @@ $: << RUBY_LIB_LOCATION require 'pp' +require 'rubygems' require 'OpenNebulaDriver' require 'simple_auth' require 'simple_permissions' +require 'yaml' +require 'sequel' class AuthorizationManager < OpenNebulaDriver def initialize super(1, false) + config_data=File.read(ETC_LOCATION+'/auth/auth.conf') + STDERR.puts(config_data) + @config=YAML::load(config_data) + + STDERR.puts @config.inspect + + database_url=@config[:database] + @db=Sequel.connect(database_url) + @authenticate=SimpleAuth.new - @permissions=SimplePermissions.new + @permissions=SimplePermissions.new(@db, OpenNebula::Client.new, + @config) register_action(:AUTHENTICATE, method('action_authenticate')) register_action(:AUTHORIZE, method('action_authorize')) diff --git a/src/authm_mad/quota.rb b/src/authm_mad/quota.rb index 23eb021e3e..f1e7a02d1f 100644 --- a/src/authm_mad/quota.rb +++ b/src/authm_mad/quota.rb @@ -36,6 +36,9 @@ class Quota } }.merge(conf) + STDERR.puts conf.inspect + STDERR.puts @conf.inspect + @defaults=@conf[:defaults] @usage=OneUsage.new(@client) @@ -93,6 +96,8 @@ class Quota usage.memory+=new_vm.memory end + STDERR.puts [user_quota, usage, new_vm].inspect + (!user_quota[:cpu] || usage.cpu<=user_quota[:cpu]) && (!user_quota[:memory] || usage.memory<=user_quota[:memory]) end From c322e7c3ea6023359fc4b1e763bb35c69104f09d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Tue, 13 Jul 2010 18:57:27 +0200 Subject: [PATCH 50/64] feature #203: Solving newline on some RM log messages --- src/oca/ruby/OpenNebula.rb | 2 -- src/rm/RequestManagerDeploy.cc | 4 ++-- src/rm/RequestManagerMigrate.cc | 4 ++-- src/rm/RequestManagerUserAllocate.cc | 6 ++---- src/rm/RequestManagerUserDelete.cc | 4 ++-- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/oca/ruby/OpenNebula.rb b/src/oca/ruby/OpenNebula.rb index 34360f0832..84336c191a 100644 --- a/src/oca/ruby/OpenNebula.rb +++ b/src/oca/ruby/OpenNebula.rb @@ -19,8 +19,6 @@ require 'OpenNebula/User' require 'OpenNebula/UserPool' require 'OpenNebula/Host' require 'OpenNebula/HostPool' -require 'OpenNebula/Cluster' -require 'OpenNebula/ClusterPool' module OpenNebula diff --git a/src/rm/RequestManagerDeploy.cc b/src/rm/RequestManagerDeploy.cc index 97b272a1d1..95b1c01cb1 100644 --- a/src/rm/RequestManagerDeploy.cc +++ b/src/rm/RequestManagerDeploy.cc @@ -148,8 +148,8 @@ error_vm_get: goto error_common; error_state: - oss.str(action_error(method_name, "MANAGE", "VM", vid, rc)); - oss << " Reason: VM in wrong state."; + oss << action_error(method_name, "MANAGE", "VM", vid, rc) + << ". Reason: VM in wrong state."; goto error_common_lock; error_authenticate: diff --git a/src/rm/RequestManagerMigrate.cc b/src/rm/RequestManagerMigrate.cc index 5658020871..cd03ab3e67 100644 --- a/src/rm/RequestManagerMigrate.cc +++ b/src/rm/RequestManagerMigrate.cc @@ -168,8 +168,8 @@ error_history: goto error_common_lock; error_state: - oss.str(action_error(method_name, "MANAGE", "VM", vid, rc)); - oss << " Reason: VM in wrong state."; + oss << action_error(method_name, "MANAGE", "VM", vid, rc) + << ". Reason: VM in wrong state."; goto error_common_lock; error_common_lock: diff --git a/src/rm/RequestManagerUserAllocate.cc b/src/rm/RequestManagerUserAllocate.cc index e31759295d..7fa13d4aad 100644 --- a/src/rm/RequestManagerUserAllocate.cc +++ b/src/rm/RequestManagerUserAllocate.cc @@ -113,17 +113,15 @@ error_authorize: goto error_common; error_duplicate: - oss.str(action_error(method_name, "CREATE", "USER", NULL, NULL)); - oss << " Reason: Existing user, cannot duplicate."; + oss << action_error(method_name, "CREATE", "USER", NULL, NULL) + << ". Reason: Existing user, cannot duplicate."; goto error_common; - error_allocate: oss.str(action_error(method_name, "CREATE", "USER", NULL, rc)); goto error_common; error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); diff --git a/src/rm/RequestManagerUserDelete.cc b/src/rm/RequestManagerUserDelete.cc index c6817d2b00..f01c091548 100644 --- a/src/rm/RequestManagerUserDelete.cc +++ b/src/rm/RequestManagerUserDelete.cc @@ -105,8 +105,8 @@ void RequestManager::UserDelete::execute( return; error_oneadmin_deletion: - oss.str(action_error(method_name, "DELETE", "USER", uid, NULL)); - oss << " Reason: Oneadmin cannot be deleted."; + oss << action_error(method_name, "DELETE", "USER", uid, NULL) + << ". Reason: Oneadmin cannot be deleted."; goto error_common; error_authenticate: From 24ec057a9cc823da3c793df47522cd1bc8dd84b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Tue, 13 Jul 2010 19:26:43 +0200 Subject: [PATCH 51/64] feature #203: Changing RM log message codes --- include/RequestManager.h | 14 ++++++++------ src/rm/RequestManagerAllocate.cc | 2 +- src/rm/RequestManagerClusterAllocate.cc | 2 +- src/rm/RequestManagerClusterPoolInfo.cc | 2 +- src/rm/RequestManagerHostAllocate.cc | 4 ++-- src/rm/RequestManagerImageDelete.cc | 4 ++-- src/rm/RequestManagerUserAllocate.cc | 6 +++--- src/rm/RequestManagerVirtualNetworkAllocate.cc | 2 +- src/rm/RequestManagerVirtualNetworkPublish.cc | 1 - 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/include/RequestManager.h b/include/RequestManager.h index 6b118cc454..9687853ce8 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -184,7 +184,7 @@ private: << " to perform " << action << " on " << object; - if ( id != (int)NULL ) + if ( id != -1 ) { oss << " [" << id << "]."; } @@ -215,7 +215,7 @@ private: oss << "[" << method << "]" << " Error getting " << object; - if ( id != (int)NULL ) + if ( id != -1 ) { oss << " [" << id << "]."; } @@ -238,18 +238,20 @@ private: oss << "[" << method << "]" << " Error trying to " << action << " " << object; - if ( id != (int)NULL ) + if ( id != -1 ) { oss << " [" << id << "]."; } + else + { + oss << " Pool."; + } if ( rc != (int)NULL ) { - oss << " .Returned error code [" << rc << "]."; + oss << " Returned error code [" << rc << "]."; } - NebulaLog::log("RM",Log::ERROR,oss); - return oss.str(); } diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index f91ffde242..795ec0e7da 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -81,7 +81,7 @@ error_authenticate: goto error_common; error_allocate: - oss.str(action_error(method_name, "CREATE", "VM", NULL, rc)); + oss.str(action_error(method_name, "CREATE", "VM", -1, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterAllocate.cc b/src/rm/RequestManagerClusterAllocate.cc index 72e154e6e5..d369bcf9dc 100644 --- a/src/rm/RequestManagerClusterAllocate.cc +++ b/src/rm/RequestManagerClusterAllocate.cc @@ -95,7 +95,7 @@ error_authorize: goto error_common; error_cluster_allocate: - oss.str(action_error(method_name, "CREATE", "CLUSTER", NULL, rc)); + oss.str(action_error(method_name, "CREATE", "CLUSTER", -1, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterPoolInfo.cc b/src/rm/RequestManagerClusterPoolInfo.cc index 46d6a28d52..7974fca010 100755 --- a/src/rm/RequestManagerClusterPoolInfo.cc +++ b/src/rm/RequestManagerClusterPoolInfo.cc @@ -76,7 +76,7 @@ error_authenticate: goto error_common; error_dump: - oss.str(get_error(method_name, "CLUSTER", NULL)); + oss.str(get_error(method_name, "CLUSTER", -1)); goto error_common; error_common: diff --git a/src/rm/RequestManagerHostAllocate.cc b/src/rm/RequestManagerHostAllocate.cc index 5fcbcc91c2..eefabab437 100644 --- a/src/rm/RequestManagerHostAllocate.cc +++ b/src/rm/RequestManagerHostAllocate.cc @@ -101,11 +101,11 @@ error_authenticate: goto error_common; error_authorize: - oss.str(authorization_error(method_name, "CREATE", "HOST", rc, NULL)); + oss.str(authorization_error(method_name, "CREATE", "HOST", rc, -1)); goto error_common; error_host_allocate: - oss.str(action_error(method_name, "CREATE", "HOST", NULL, rc)); + oss.str(action_error(method_name, "CREATE", "HOST", -1, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerImageDelete.cc b/src/rm/RequestManagerImageDelete.cc index 6d7813063c..20875bf5f7 100644 --- a/src/rm/RequestManagerImageDelete.cc +++ b/src/rm/RequestManagerImageDelete.cc @@ -133,8 +133,8 @@ error_authorize: goto error_common; error_delete: - oss.str(action_error(method_name, "DELETE", "IMAGE", iid, rc)); - oss << " VMs might be running on it."; + oss << action_error(method_name, "DELETE", "IMAGE", iid, rc) + << ". Reason: VMs might be running on it."; image->unlock(); goto error_common; diff --git a/src/rm/RequestManagerUserAllocate.cc b/src/rm/RequestManagerUserAllocate.cc index 7fa13d4aad..6c41cfdd52 100644 --- a/src/rm/RequestManagerUserAllocate.cc +++ b/src/rm/RequestManagerUserAllocate.cc @@ -109,16 +109,16 @@ error_authenticate: goto error_common; error_authorize: - oss.str(authorization_error(method_name, "CREATE", "USER", rc, NULL)); + oss.str(authorization_error(method_name, "CREATE", "USER", rc, -1)); goto error_common; error_duplicate: - oss << action_error(method_name, "CREATE", "USER", NULL, NULL) + oss << action_error(method_name, "CREATE", "USER", -1, NULL) << ". Reason: Existing user, cannot duplicate."; goto error_common; error_allocate: - oss.str(action_error(method_name, "CREATE", "USER", NULL, rc)); + oss.str(action_error(method_name, "CREATE", "USER", -1, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 1be91fdb19..f12199c847 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -97,7 +97,7 @@ error_get_user: goto error_common; error_vn_allocate: - oss.str(action_error(method_name, "ALLOCATE", "NET", NULL, rc)); + oss.str(action_error(method_name, "ALLOCATE", "NET", -1, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerVirtualNetworkPublish.cc b/src/rm/RequestManagerVirtualNetworkPublish.cc index 4ec9c3d414..c8d0befb6e 100644 --- a/src/rm/RequestManagerVirtualNetworkPublish.cc +++ b/src/rm/RequestManagerVirtualNetworkPublish.cc @@ -33,7 +33,6 @@ void RequestManager::VirtualNetworkPublish::execute( int nid; bool publish_flag; int uid; - int rc; VirtualNetwork * vn; From 9ed94ae7f4a570e7ed31e9056879fff849bb04a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Wed, 14 Jul 2010 16:10:18 +0200 Subject: [PATCH 52/64] feature #203: Readding the cluster to OpenNebula.rb --- src/oca/ruby/OpenNebula.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/oca/ruby/OpenNebula.rb b/src/oca/ruby/OpenNebula.rb index 84336c191a..34360f0832 100644 --- a/src/oca/ruby/OpenNebula.rb +++ b/src/oca/ruby/OpenNebula.rb @@ -19,6 +19,8 @@ require 'OpenNebula/User' require 'OpenNebula/UserPool' require 'OpenNebula/Host' require 'OpenNebula/HostPool' +require 'OpenNebula/Cluster' +require 'OpenNebula/ClusterPool' module OpenNebula From 1035fe77221c626c8e67c974952e766e4d9f87df Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 14 Jul 2010 16:21:41 +0200 Subject: [PATCH 53/64] feature #203: Removed Auth from allocate (VM, IMAGE & NET). Added NETWORK and NETWORK_ID for NICs and IMAGE and IMAGE_ID for DISKS --- include/ImagePool.h | 4 +--- include/VirtualMachine.h | 6 ++--- include/VirtualNetworkPool.h | 4 +--- src/image/Image.cc | 34 +++------------------------- src/image/ImagePool.cc | 38 +++++++++++++++++++------------ src/vm/VirtualMachine.cc | 42 +++++++---------------------------- src/vnm/VirtualNetwork.cc | 37 +++++------------------------- src/vnm/VirtualNetworkPool.cc | 39 +++++++++++++++++++++----------- 8 files changed, 70 insertions(+), 134 deletions(-) diff --git a/include/ImagePool.h b/include/ImagePool.h index 79b46705b4..61c14cbcb7 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -29,8 +29,6 @@ using namespace std; -class AuthRequest; - /** * The Image Pool class. */ @@ -170,7 +168,7 @@ public: * @param disk the disk to be generated * @return 0 on success, -1 error, -2 not using the pool */ - int disk_attribute(VectorAttribute * disk, int * index, AuthRequest * ar); + int disk_attribute(VectorAttribute * disk, int * index); static const string& source_prefix() { diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 3f942c8bda..b56f4bb8ef 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -31,8 +31,6 @@ using namespace std; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -class AuthRequest; - /** * The Virtual Machine class. It represents a VM... */ @@ -704,7 +702,7 @@ public: * Get all network leases for this Virtual Machine * @return 0 if success */ - int get_network_leases(AuthRequest *ar); + int get_network_leases(); /** * Releases all network leases taken by this Virtual Machine @@ -715,7 +713,7 @@ public: * Get all disk images for this Virtual Machine * @return 0 if success */ - int get_disk_images(AuthRequest *ar); + int get_disk_images(); /** * Releases all disk images taken by this Virtual Machine diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index 57e7db8d3f..5547d40c93 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -24,8 +24,6 @@ using namespace std; -class AuthRequest; - /** * The Virtual Network Pool class. ... */ @@ -88,7 +86,7 @@ public: * @param vid of the VM requesting the lease * @return 0 on success, -1 error, -2 not using the pool */ - int nic_attribute(VectorAttribute * nic, int vid, AuthRequest *ar); + int nic_attribute(VectorAttribute * nic, int vid); /** * Updates the template of a VN, adding a new attribute (replacing it if diff --git a/src/image/Image.cc b/src/image/Image.cc index fe7b0f0943..f5a6fec6f6 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -210,27 +210,6 @@ int Image::insert(SqlDB *db) source = tmp_sourcestream.str(); - // ------------------------------------------------------------------------ - // Authorize this request - // ------------------------------------------------------------------------ - - if ( uid != 0 ) // uid == 0 means oneadmin - { - string t64; - AuthRequest ar(uid); - - ar.add_auth(AuthRequest::IMAGE, - image_template.to_xml(t64), - AuthRequest::CREATE, - uid, - public_img); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - // ------------ INSERT THE TEMPLATE -------------------- if ( image_template.id == -1 ) @@ -270,10 +249,6 @@ error_type: NebulaLog::log("IMG", Log::ERROR, "Incorrect TYPE in image template"); goto error_common; -error_authorize: - NebulaLog::log("IMG", Log::ERROR, "Error authorizing Image creation"); - goto error_common; - error_common: return -1; } @@ -560,12 +535,10 @@ int Image::disk_attribute(VectorAttribute * disk, int * index) { string overwrite; string saveas; - string name; string bus; ostringstream iid; - name = disk->vector_value("NAME"); overwrite = disk->vector_value("OVERWRITE"); saveas = disk->vector_value("SAVE_AS"); bus = disk->vector_value("BUS"); @@ -595,10 +568,9 @@ int Image::disk_attribute(VectorAttribute * disk, int * index) map new_disk; - new_disk.insert(make_pair("NAME",name)); - new_disk.insert(make_pair("IID", iid.str())); - - new_disk.insert(make_pair("SOURCE", source)); + new_disk.insert(make_pair("IMAGE", name)); + new_disk.insert(make_pair("IMAGE_ID", iid.str())); + new_disk.insert(make_pair("SOURCE", source)); if (!overwrite.empty()) { diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 367409cf56..21cdaeb0db 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -19,7 +19,6 @@ /* ************************************************************************** */ #include "ImagePool.h" -#include "AuthManager.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -190,20 +189,37 @@ int ImagePool::dump(ostringstream& oss, const string& where) /* -------------------------------------------------------------------------- */ int ImagePool::disk_attribute(VectorAttribute * disk, - int * index, - AuthRequest * ar) + int * index) { string source; - Image * img; + Image * img = 0; - source = disk->vector_value("NAME"); + source = disk->vector_value("IMAGE"); if (source.empty()) { - return -2; - } + istringstream is; + int image_id; - img = get(source,true); + source = disk->vector_value("IMAGE_ID"); + + if (source.empty()) + { + return -2; + } + + is.str(source); + is >> image_id; + + if( !is.fail() ) + { + img = get(image_id,true); + } + } + else + { + img = get(source,true); + } if (img == 0) { @@ -212,12 +228,6 @@ int ImagePool::disk_attribute(VectorAttribute * disk, int rc = img->disk_attribute(disk,index); - ar->add_auth(AuthRequest::IMAGE, - img->get_iid(), - AuthRequest::USE, - img->get_uid(), - img->isPublic()); - img->unlock(); return rc; diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index d538f12eb5..032a29403f 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -26,7 +26,6 @@ #include "VirtualMachine.h" #include "VirtualNetworkPool.h" #include "NebulaLog.h" -#include "AuthManager.h" #include "Nebula.h" @@ -268,8 +267,6 @@ int VirtualMachine::insert(SqlDB * db) string value; ostringstream oss; - AuthRequest ar(uid); - // ----------------------------------------------------------------------- // Set a template ID if it wasn't already assigned // ------------------------------------------------------------------------ @@ -306,7 +303,7 @@ int VirtualMachine::insert(SqlDB * db) // Get network leases // ------------------------------------------------------------------------ - rc = get_network_leases(&ar); + rc = get_network_leases(); if ( rc != 0 ) { @@ -317,7 +314,7 @@ int VirtualMachine::insert(SqlDB * db) // Get disk images // ------------------------------------------------------------------------ - rc = get_disk_images(&ar); + rc = get_disk_images(); if ( rc != 0 ) { @@ -344,26 +341,6 @@ int VirtualMachine::insert(SqlDB * db) parse_graphics(); - // ------------------------------------------------------------------------ - // Authorize this request - // ------------------------------------------------------------------------ - - if ( uid != 0 ) // uid == 0 means oneadmin - { - string t64; - - ar.add_auth(AuthRequest::VM, - vm_template.to_xml(t64), - AuthRequest::CREATE, - uid, - false); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - // ------------------------------------------------------------------------ // Insert the template first, so we get a valid template ID. Then the VM // ------------------------------------------------------------------------ @@ -410,9 +387,6 @@ error_requirements: NebulaLog::log("ONE",Log::ERROR, "Could not parse REQUIREMENTS for VM"); goto error_common; -error_authorize: - NebulaLog::log("ONE",Log::ERROR, "Error authorizing VM creation"); - error_common: release_network_leases(); release_disk_images(); @@ -819,7 +793,7 @@ void VirtualMachine::get_requirements (int& cpu, int& memory, int& disk) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualMachine::get_disk_images(AuthRequest *ar) +int VirtualMachine::get_disk_images() { int num_disks, rc; vector disks; @@ -841,7 +815,7 @@ int VirtualMachine::get_disk_images(AuthRequest *ar) continue; } - rc = ipool->disk_attribute(disk, &index, ar); + rc = ipool->disk_attribute(disk, &index); if (rc == -1) // 0 OK, -2 not using the Image pool { @@ -879,7 +853,7 @@ void VirtualMachine::release_disk_images() continue; } - iid = disk->vector_value("IID"); + iid = disk->vector_value("IMAGE_ID"); if ( iid.empty() ) { @@ -902,7 +876,7 @@ void VirtualMachine::release_disk_images() /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualMachine::get_network_leases(AuthRequest *ar) +int VirtualMachine::get_network_leases() { int num_nics, rc; vector nics; @@ -923,7 +897,7 @@ int VirtualMachine::get_network_leases(AuthRequest *ar) continue; } - rc = vnpool->nic_attribute(nic, oid, ar); + rc = vnpool->nic_attribute(nic, oid); if (rc == -1) { @@ -962,7 +936,7 @@ void VirtualMachine::release_network_leases() continue; } - vnid = nic->vector_value("VNID"); + vnid = nic->vector_value("NETWORK_ID"); if ( vnid.empty() ) { diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index fdcf678e00..5020b3fb5b 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -317,27 +317,6 @@ int VirtualNetwork::insert(SqlDB * db) vn_template.erase("PUBLIC"); - // ------------------------------------------------------------------------ - // Authorize this request - // ------------------------------------------------------------------------ - - if ( uid != 0 ) // uid == 0 means oneadmin - { - string t64; - AuthRequest ar(uid); - - ar.add_auth(AuthRequest::NET, - vn_template.to_xml(t64), - AuthRequest::CREATE, - uid, - public_vnet); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - // ------------ INSERT THE TEMPLATE -------------------- if ( vn_template.id == -1 ) @@ -440,10 +419,6 @@ error_bridge: ose << "No BRIDGE in template for Virtual Network id " << oid; goto error_common; -error_authorize: - ose << "Error authorizing Virtual Network creation"; - goto error_common; - error_template: ose << "Can not insert in DB template for Virtual Network id " << oid; goto error_common; @@ -640,7 +615,6 @@ int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid) { int rc; - string network; string model; string ip; string mac; @@ -649,7 +623,6 @@ int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid) map new_nic; - network = nic->vector_value("NETWORK"); model = nic->vector_value("MODEL"); ip = nic->vector_value("IP"); vnid << oid; @@ -676,11 +649,11 @@ int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid) // NEW NIC ATTRIBUTES //-------------------------------------------------------------------------- - new_nic.insert(make_pair("NETWORK",network)); - new_nic.insert(make_pair("MAC" ,mac)); - new_nic.insert(make_pair("BRIDGE" ,bridge)); - new_nic.insert(make_pair("VNID" ,vnid.str())); - new_nic.insert(make_pair("IP" ,ip)); + new_nic.insert(make_pair("NETWORK" ,name)); + new_nic.insert(make_pair("MAC" ,mac)); + new_nic.insert(make_pair("BRIDGE" ,bridge)); + new_nic.insert(make_pair("NETWORK_ID",vnid.str())); + new_nic.insert(make_pair("IP" ,ip)); if (!model.empty()) { diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index 513fb138b4..8b53ebf8b9 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -16,7 +16,6 @@ #include "VirtualNetworkPool.h" #include "NebulaLog.h" -#include "AuthManager.h" #include #include @@ -220,20 +219,39 @@ int VirtualNetworkPool::dump(ostringstream& oss, const string& where) /* -------------------------------------------------------------------------- */ int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, - int vid, - AuthRequest * ar) + int vid) { string network; - VirtualNetwork * vnet; + VirtualNetwork * vnet = 0; network = nic->vector_value("NETWORK"); if (network.empty()) { + istringstream is; + int network_id; + + network = nic->vector_value("NETWORK_ID"); + + if(network.empty()) + { + return -2; + } + + is.str(network); + is >> network_id; + + if( !is.fail() ) + { + vnet = get(network_id,true); + } + return -2; } - - vnet = get(network,true); + else + { + vnet = get(network,true); + } if (vnet == 0) { @@ -242,13 +260,8 @@ int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, int rc = vnet->nic_attribute(nic,vid); - ar->add_auth(AuthRequest::NET, - vnet->get_vnid(), - AuthRequest::USE, - vnet->get_uid(), - vnet->isPublic()); - vnet->unlock(); return rc; -} \ No newline at end of file +} + From f19887fab01bcbebf8c589a4513594be2d670dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Wed, 14 Jul 2010 17:28:33 +0200 Subject: [PATCH 54/64] feature #203: RM log funtions, -2 introduced for (allocate, parse, ...) errors --- include/RequestManager.h | 19 +++++++++++-------- src/rm/RequestManagerAllocate.cc | 2 +- src/rm/RequestManagerClusterAllocate.cc | 2 +- src/rm/RequestManagerHostAllocate.cc | 2 +- src/rm/RequestManagerImageAllocate.cc | 2 +- src/rm/RequestManagerUserAllocate.cc | 2 +- .../RequestManagerVirtualNetworkAllocate.cc | 2 +- 7 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/RequestManager.h b/include/RequestManager.h index 9687853ce8..5e4b47fe45 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -237,16 +237,19 @@ private: oss << "[" << method << "]" << " Error trying to " << action << " " << object; - - if ( id != -1 ) + + switch(id) { - oss << " [" << id << "]."; + case -2: + break; + case -1: + oss << "Pool."; + break; + default: + oss << " [" << id << "]."; + break; } - else - { - oss << " Pool."; - } - + if ( rc != (int)NULL ) { oss << " Returned error code [" << rc << "]."; diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 795ec0e7da..519dd32760 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -81,7 +81,7 @@ error_authenticate: goto error_common; error_allocate: - oss.str(action_error(method_name, "CREATE", "VM", -1, rc)); + oss.str(action_error(method_name, "CREATE", "VM", -2, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerClusterAllocate.cc b/src/rm/RequestManagerClusterAllocate.cc index d369bcf9dc..be09bdf110 100644 --- a/src/rm/RequestManagerClusterAllocate.cc +++ b/src/rm/RequestManagerClusterAllocate.cc @@ -95,7 +95,7 @@ error_authorize: goto error_common; error_cluster_allocate: - oss.str(action_error(method_name, "CREATE", "CLUSTER", -1, rc)); + oss.str(action_error(method_name, "CREATE", "CLUSTER", -2, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerHostAllocate.cc b/src/rm/RequestManagerHostAllocate.cc index eefabab437..e94296624c 100644 --- a/src/rm/RequestManagerHostAllocate.cc +++ b/src/rm/RequestManagerHostAllocate.cc @@ -105,7 +105,7 @@ error_authorize: goto error_common; error_host_allocate: - oss.str(action_error(method_name, "CREATE", "HOST", -1, rc)); + oss.str(action_error(method_name, "CREATE", "HOST", -2, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 74357cf73b..47315cacf1 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -82,7 +82,7 @@ error_authenticate: goto error_common; error_allocate: - oss.str(action_error(method_name, "CREATE", "IMAGE", NULL, rc)); + oss.str(action_error(method_name, "CREATE", "IMAGE", -2, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerUserAllocate.cc b/src/rm/RequestManagerUserAllocate.cc index 6c41cfdd52..5f15ea56c6 100644 --- a/src/rm/RequestManagerUserAllocate.cc +++ b/src/rm/RequestManagerUserAllocate.cc @@ -118,7 +118,7 @@ error_duplicate: goto error_common; error_allocate: - oss.str(action_error(method_name, "CREATE", "USER", -1, rc)); + oss.str(action_error(method_name, "CREATE", "USER", -2, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index f12199c847..ddc1e880d8 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -97,7 +97,7 @@ error_get_user: goto error_common; error_vn_allocate: - oss.str(action_error(method_name, "ALLOCATE", "NET", -1, rc)); + oss.str(action_error(method_name, "CREATE", "NET", -2, rc)); goto error_common; error_common: From 34b2ed6c5cd4f2fa89ad88cf15301e627c4d3f1e Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 14 Jul 2010 18:11:29 +0200 Subject: [PATCH 55/64] feature #203: Authorization method for VMs now in RM --- include/DispatchManager.h | 102 +++++++++++------------ include/ImagePool.h | 9 ++ include/VirtualMachine.h | 18 ++-- include/VirtualMachinePool.h | 10 +-- include/VirtualNetworkPool.h | 9 ++ src/dm/DispatchManagerActions.cc | 12 --- src/image/ImagePool.cc | 55 +++++++++++- src/rm/RequestManagerAllocate.cc | 115 +++++++++++++++++++++++--- src/rm/RequestManagerDeploy.cc | 6 +- src/rm/RequestManagerImageAllocate.cc | 8 +- src/rm/RequestManagerUserAllocate.cc | 60 +++++++------- src/rm/RequestManagerUserDelete.cc | 48 +++++------ src/vm/VirtualMachine.cc | 50 ++++++----- src/vm/VirtualMachinePool.cc | 25 +----- src/vnm/VirtualNetworkPool.cc | 54 +++++++++++- 15 files changed, 376 insertions(+), 205 deletions(-) diff --git a/include/DispatchManager.h b/include/DispatchManager.h index dfb388fd1c..789acd1ba2 100644 --- a/include/DispatchManager.h +++ b/include/DispatchManager.h @@ -55,7 +55,7 @@ public: * Triggers specific actions to the Dispatch Manager. This function * wraps the ActionManager trigger function. * @param action the DM action - * @param vid VM unique id. This is the argument of the passed to the + * @param vid VM unique id. This is the argument of the passed to the * invoked action. */ void trigger( @@ -63,12 +63,12 @@ public: int _vid); /** - * This functions creates a new thread for the Dispatch Manager. This + * This functions creates a new thread for the Dispatch Manager. This * thread will wait in an action loop till it receives ACTION_FINALIZE. * @return 0 on success. */ int start(); - + /** * Gets the thread identification. * @return pthread_t for the manager thread (that in the action loop). @@ -81,132 +81,122 @@ public: //-------------------------------------------------------------------------- // DM Actions, the RM and the Scheduler will invoke this methods //-------------------------------------------------------------------------- - - /** - * Allocates a new virtual machine - * @return 0 on success - */ - int allocate ( - int uid, - const string& stemplate, - int * oid); - /** * Deploys a VM. A new history record MUST be added before calling this * function. Also the VM MUST have its mutex locked. If the function fails * the calling funtion is responsible for recovering from the error. * @param vm pointer to a VirtualMachine with its mutex locked. - * @return 0 on success + * @return 0 on success */ int deploy ( VirtualMachine * vm); /** - * Migrates a VM. The following actions must be performed before calling - * this function: + * Migrates a VM. The following actions must be performed before calling + * this function: * - Lock the VM mutex. * - Update the History statistics of the current host. - * - Add a new History record with the new host. - * If the function fails the calling funtion is responsible for recovering + * - Add a new History record with the new host. + * If the function fails the calling funtion is responsible for recovering * from the error. * @param vm pointer to a VirtualMachine with its mutex locked. - * @return 0 on success + * @return 0 on success */ int migrate( VirtualMachine * vm); /** - * Migrates a VM. The following actions must be performed before calling - * this function: + * Migrates a VM. The following actions must be performed before calling + * this function: * - Lock the VM mutex. * - Update the History statistics of the current host. - * - Add a new History record with the new host. - * If the function fails the calling funtion is responsible for recovering + * - Add a new History record with the new host. + * If the function fails the calling funtion is responsible for recovering * from the error. * @param vm pointer to a VirtualMachine with its mutex locked. - * @return 0 on success + * @return 0 on success */ int live_migrate( VirtualMachine * vm); - + /** * Shuts down a VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int shutdown ( int vid); /** * Holds a VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int hold( int vid); /** * Releases a VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int release( int vid); /** * Stops a VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int stop( int vid); /** * Cancels a VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int cancel( int vid); /** * Suspends a VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int suspend( int vid); /** * Resumes a VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int resume( int vid); - + /** * Restart a previusly deployed VM. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int restart( int vid); /** * Ends a VM life cycle inside ONE. * @param vid VirtualMachine identification - * @return 0 on success, -1 if the VM does not exits or -2 if the VM is - * in a wrong a state - */ + * @return 0 on success, -1 if the VM does not exits or -2 if the VM is + * in a wrong a state + */ int finalize( int vid); @@ -232,7 +222,7 @@ private: ActionManager am; /** - * Function to execute the Manager action loop method within a new pthread + * Function to execute the Manager action loop method within a new pthread * (requires C linkage) */ friend void * dm_action_loop(void *arg); @@ -249,14 +239,14 @@ private: //-------------------------------------------------------------------------- // DM Actions associated with a VM state transition //-------------------------------------------------------------------------- - + void suspend_success_action(int vid); void stop_success_action(int vid); void done_action(int vid); - void failed_action(int vid); + void failed_action(int vid); }; #endif /*DISPATCH_MANAGER_H*/ diff --git a/include/ImagePool.h b/include/ImagePool.h index 61c14cbcb7..709f6bcdf3 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -27,6 +27,8 @@ #include #include +class AuthRequest; + using namespace std; /** @@ -170,6 +172,13 @@ public: */ int disk_attribute(VectorAttribute * disk, int * index); + /** + * Generates an Authorization token for the DISK attribute + * @param disk the disk to be authorized + * @param ar the AuthRequest + */ + void authorize_disk(VectorAttribute * disk, AuthRequest * ar); + static const string& source_prefix() { return _source_prefix; diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index b56f4bb8ef..af7c76e551 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -553,7 +553,7 @@ public: string& name, vector& values) const { - return vm_template.get(name,values); + return vm_template->get(name,values); }; /** @@ -567,7 +567,7 @@ public: vector& values) const { string str=name; - return vm_template.get(str,values); + return vm_template->get(str,values); }; /** @@ -581,7 +581,7 @@ public: string& value) const { string str=name; - vm_template.get(str,value); + vm_template->get(str,value); } /** @@ -595,7 +595,7 @@ public: int& value) const { string str=name; - vm_template.get(str,value); + vm_template->get(str,value); } /** @@ -604,7 +604,7 @@ public: */ void template_to_xml(string &xml) const { - vm_template.to_xml(xml); + vm_template->to_xml(xml); } /** @@ -771,7 +771,7 @@ private: /** * The Virtual Machine template, holds the VM attributes. */ - VirtualMachineTemplate vm_template; + VirtualMachineTemplate* vm_template; // Dynamic state of the Virtual Machine @@ -930,7 +930,7 @@ private: int rc; sattr = new SingleAttribute(name,value); - rc = vm_template.replace_attribute(db,sattr); + rc = vm_template->replace_attribute(db,sattr); if (rc != 0) { @@ -949,7 +949,7 @@ private: */ int insert_template_attribute(SqlDB * db, Attribute * attribute) { - return vm_template.insert_attribute(db,attribute); + return vm_template->insert_attribute(db,attribute); } // ------------------------------------------------------------------------- @@ -987,7 +987,7 @@ protected: // Constructor //************************************************************************** - VirtualMachine(int id=-1); + VirtualMachine(int id=-1, VirtualMachineTemplate * _vm_template = 0); virtual ~VirtualMachine(); diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index 685179e76a..fe8ca25bae 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -39,7 +39,7 @@ public: /** * Function to allocate a new VM object * @param uid user id (the owner of the VM) - * @param stemplate a string describing the VM + * @param vm_template a VM Template object describing the VM * @param oid the id assigned to the VM (output) * @param on_hold flag to submit on hold * @return oid on success, -1 error inserting in DB or -2 error parsing @@ -47,7 +47,7 @@ public: */ int allocate ( int uid, - const string& stemplate, + VirtualMachineTemplate *vm_template, int * oid, bool on_hold = false); @@ -99,9 +99,9 @@ public: * @return 0 on success */ int update_template_attribute( - VirtualMachine * vm, - string& name, - string& value) + VirtualMachine * vm, + string& name, + string& value) { return vm->update_template_attribute(db,name,value); } diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index 5547d40c93..633ff9aebc 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -22,6 +22,8 @@ #include +class AuthRequest; + using namespace std; /** @@ -88,6 +90,13 @@ public: */ int nic_attribute(VectorAttribute * nic, int vid); + /** + * Generates an Authorization token for a NIC attribute + * @param nic the nic to be authorized + * @param ar the AuthRequest + */ + void authorize_nic(VectorAttribute * nic, AuthRequest * ar); + /** * Updates the template of a VN, adding a new attribute (replacing it if * already defined), the VN's mutex SHOULD be locked diff --git a/src/dm/DispatchManagerActions.cc b/src/dm/DispatchManagerActions.cc index b23eaa8fca..a82485f14e 100644 --- a/src/dm/DispatchManagerActions.cc +++ b/src/dm/DispatchManagerActions.cc @@ -18,18 +18,6 @@ #include "NebulaLog.h" #include "Nebula.h" -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int DispatchManager::allocate ( - int uid, - const string& stemplate, - int * oid) -{ - NebulaLog::log("DiM",Log::DEBUG,"Allocating a new VM"); - - return vmpool->allocate(uid,stemplate,oid); -}; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 21cdaeb0db..7d89ccf4c5 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -19,6 +19,7 @@ /* ************************************************************************** */ #include "ImagePool.h" +#include "AuthManager.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -188,8 +189,7 @@ int ImagePool::dump(ostringstream& oss, const string& where) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int ImagePool::disk_attribute(VectorAttribute * disk, - int * index) +int ImagePool::disk_attribute(VectorAttribute * disk, int * index) { string source; Image * img = 0; @@ -231,4 +231,53 @@ int ImagePool::disk_attribute(VectorAttribute * disk, img->unlock(); return rc; -} \ No newline at end of file +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void ImagePool::authorize_disk(VectorAttribute * disk, AuthRequest * ar) +{ + string source; + Image * img = 0; + + source = disk->vector_value("IMAGE"); + + if (source.empty()) + { + istringstream is; + int image_id; + + source = disk->vector_value("IMAGE_ID"); + + if (source.empty()) + { + return; + } + + is.str(source); + is >> image_id; + + if( !is.fail() ) + { + img = get(image_id,true); + } + } + else + { + img = get(source,true); + } + + if (img == 0) + { + return; + } + + ar->add_auth(AuthRequest::IMAGE, + img->get_iid(), + AuthRequest::USE, + img->get_uid(), + img->isPublic()); + + img->unlock(); +} diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 795ec0e7da..a0861fa500 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -27,36 +27,110 @@ void RequestManager::VirtualMachineAllocate::execute( xmlrpc_c::value * const retval) { string session; - string vm_template; - + string str_template; + const string method_name = "VirtualMachineAllocate"; - int vid; + int vid, uid; int rc; - Nebula& nd = Nebula::instance(); - DispatchManager * dm = nd.get_dm(); + Nebula& nd = Nebula::instance(); + ImagePool * ipool = nd.get_ipool(); + VirtualNetworkPool* vnpool= nd.get_vnpool(); + VirtualMachinePool* vmpool= nd.get_vmpool(); ostringstream oss; vector arrayData; xmlrpc_c::value_array * arrayresult; + VirtualMachineTemplate * vm_template; + char * error_msg = 0; + + int num; + vector vectors; + VectorAttribute * vector; + NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineAllocate invoked"); - session = xmlrpc_c::value_string(paramList.getString(0)); - vm_template = xmlrpc_c::value_string(paramList.getString(1)); - vm_template += "\n"; + session = xmlrpc_c::value_string(paramList.getString(0)); + str_template = xmlrpc_c::value_string(paramList.getString(1)); + str_template += "\n"; - //Authenticate the user - rc = VirtualMachineAllocate::upool->authenticate(session); + //-------------------------------------------------------------------------- + // Authenticate the user + //-------------------------------------------------------------------------- + uid = VirtualMachineAllocate::upool->authenticate(session); - if (rc == -1) + if (uid == -1) { goto error_authenticate; } - rc = dm->allocate(rc,vm_template,&vid); + //-------------------------------------------------------------------------- + // Authorize this request + //-------------------------------------------------------------------------- + + vm_template = new VirtualMachineTemplate; + + rc = vm_template->parse(str_template,&error_msg); + + if ( rc != 0 ) + { + goto error_parse; + } + + if ( uid != 0 ) + { + AuthRequest ar(uid); + string t64; + + num = vm_template->get("DISK",vectors); + + for(int i=0; i(vectors[i]); + + if ( vector == 0 ) + { + continue; + } + + ipool->authorize_disk(vector,&ar); + } + + num = vm_template->get("NIC",vectors); + + for(int i=0; i(vectors[i]); + + if ( vector == 0 ) + { + continue; + } + + vnpool->authorize_nic(vector,&ar); + } + + ar.add_auth(AuthRequest::VM, + vm_template->to_xml(t64), + AuthRequest::CREATE, + uid, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + //-------------------------------------------------------------------------- + // Allocate the VirtualMAchine + //-------------------------------------------------------------------------- + + rc = vmpool->allocate(uid,vm_template,&vid,false); if ( rc < 0 ) { @@ -73,13 +147,28 @@ void RequestManager::VirtualMachineAllocate::execute( delete arrayresult; // and get rid of the original - return; error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; +error_authorize: + oss.str(authorization_error(method_name, "CREATE", "VM", uid, -1)); + delete vm_template; + goto error_common; + +error_parse: + oss.str(action_error(method_name, "PARSE", "VM TEMPLATE",-2,rc)); + if (error_msg != 0) + { + oss << "Reason: " << error_msg; + free(error_msg); + } + + delete vm_template; + goto error_common; + error_allocate: oss.str(action_error(method_name, "CREATE", "VM", -1, rc)); goto error_common; diff --git a/src/rm/RequestManagerDeploy.cc b/src/rm/RequestManagerDeploy.cc index 95b1c01cb1..a921a85d7d 100644 --- a/src/rm/RequestManagerDeploy.cc +++ b/src/rm/RequestManagerDeploy.cc @@ -36,7 +36,7 @@ void RequestManager::VirtualMachineDeploy::execute( string vmm_mad; string tm_mad; string vmdir; - + const string method_name = "VirtualMachineDeploy"; VirtualMachine * vm; @@ -148,7 +148,7 @@ error_vm_get: goto error_common; error_state: - oss << action_error(method_name, "MANAGE", "VM", vid, rc) + oss << action_error(method_name, "MANAGE", "VM", vid, -1) << ". Reason: VM in wrong state."; goto error_common_lock; @@ -170,7 +170,7 @@ error_common_lock: error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); arrayData.push_back(xmlrpc_c::value_string(oss.str())); - + NebulaLog::log("ReM",Log::ERROR,oss); xmlrpc_c::value_array arrayresult_error(arrayData); diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 74357cf73b..21d88698a1 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -36,7 +36,7 @@ void RequestManager::ImageAllocate::execute( int rc; ostringstream oss; - + const string method_name = "ImageAllocate"; vector arrayData; @@ -57,7 +57,7 @@ void RequestManager::ImageAllocate::execute( { goto error_authenticate; } - + rc = ImageAllocate::ipool->allocate(uid,image_template,&iid); if ( rc < 0 ) @@ -80,9 +80,9 @@ void RequestManager::ImageAllocate::execute( error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; - + error_allocate: - oss.str(action_error(method_name, "CREATE", "IMAGE", NULL, rc)); + oss.str(action_error(method_name, "CREATE", "IMAGE", -2, rc)); goto error_common; error_common: diff --git a/src/rm/RequestManagerUserAllocate.cc b/src/rm/RequestManagerUserAllocate.cc index 6c41cfdd52..b1646e2e7e 100644 --- a/src/rm/RequestManagerUserAllocate.cc +++ b/src/rm/RequestManagerUserAllocate.cc @@ -25,19 +25,19 @@ void RequestManager::UserAllocate::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; string username; - string password; - + string password; + int uid; - int rc; + int rc; ostringstream oss; User * user; - + const string method_name = "UserAllocate"; /* -- RPC specific vars -- */ @@ -48,17 +48,17 @@ void RequestManager::UserAllocate::execute( // Get the parameters session = xmlrpc_c::value_string(paramList.getString(0)); - + username = xmlrpc_c::value_string(paramList.getString(1)); password = xmlrpc_c::value_string(paramList.getString(2)); - + rc = UserAllocate::upool->authenticate(session); - - if ( rc == -1 ) - { - goto error_authenticate; + + if ( rc == -1 ) + { + goto error_authenticate; } - + //Authorize the operation if ( rc != 0 ) // rc == 0 means oneadmin { @@ -83,37 +83,37 @@ void RequestManager::UserAllocate::execute( { goto error_duplicate; } - + // Now let's add the user rc = UserAllocate::upool->allocate(&uid,username,password,true); - - if ( rc == -1 ) - { - goto error_allocate; - } - - // All nice, return the new uid to client + + if ( rc == -1 ) + { + goto error_allocate; + } + + // All nice, return the new uid to client arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS arrayData.push_back(xmlrpc_c::value_int(uid)); // Copy arrayresult into retval mem space arrayresult = new xmlrpc_c::value_array(arrayData); *retval = *arrayresult; - + delete arrayresult; // and get rid of the original return; error_authenticate: - oss.str(authenticate_error(method_name)); + oss.str(authenticate_error(method_name)); goto error_common; - + error_authorize: oss.str(authorization_error(method_name, "CREATE", "USER", rc, -1)); - goto error_common; - + goto error_common; + error_duplicate: - oss << action_error(method_name, "CREATE", "USER", -1, NULL) + oss << action_error(method_name, "CREATE", "USER", -2, -1) << ". Reason: Existing user, cannot duplicate."; goto error_common; @@ -124,13 +124,13 @@ error_allocate: error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - + + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/rm/RequestManagerUserDelete.cc b/src/rm/RequestManagerUserDelete.cc index f01c091548..6b798bee7f 100644 --- a/src/rm/RequestManagerUserDelete.cc +++ b/src/rm/RequestManagerUserDelete.cc @@ -25,15 +25,15 @@ void RequestManager::UserDelete::execute( xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval) -{ +{ string session; int uid; User * user; - int rc; + int rc; ostringstream oss; - + const string method_name = "UserDelete"; /* -- RPC specific vars -- */ @@ -51,14 +51,14 @@ void RequestManager::UserDelete::execute( { goto error_oneadmin_deletion; } - + rc = UserDelete::upool->authenticate(session); - + if ( rc == -1 ) { goto error_authenticate; } - + //Authorize the operation if ( rc != 0 ) // rc == 0 means oneadmin { @@ -76,24 +76,24 @@ void RequestManager::UserDelete::execute( } } - // Now let's get the user + // Now let's get the user user = UserDelete::upool->get(uid,true); - - if ( user == 0) + + if ( user == 0) { goto error_get_user; } - + rc = UserDelete::upool->drop(user); user->unlock(); - - if ( rc != 0 ) - { - goto error_delete; - } - - // All nice, return the new uid to client + + if ( rc != 0 ) + { + goto error_delete; + } + + // All nice, return the new uid to client arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS // Copy arrayresult into retval mem space @@ -105,18 +105,18 @@ void RequestManager::UserDelete::execute( return; error_oneadmin_deletion: - oss << action_error(method_name, "DELETE", "USER", uid, NULL) + oss << action_error(method_name, "DELETE", "USER", uid, -1) << ". Reason: Oneadmin cannot be deleted."; goto error_common; error_authenticate: - oss.str(authenticate_error(method_name)); + oss.str(authenticate_error(method_name)); goto error_common; error_authorize: oss.str(authorization_error(method_name, "DELETE", "USER", rc, uid)); goto error_common; - + error_get_user: oss.str(get_error(method_name, "USER", uid)); goto error_common; @@ -128,13 +128,13 @@ error_delete: error_common: arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - + + NebulaLog::log("ReM",Log::ERROR,oss); + xmlrpc_c::value_array arrayresult_error(arrayData); *retval = arrayresult_error; - + return; } diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 032a29403f..42549a8e09 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -36,12 +36,11 @@ /* Virtual Machine :: Constructor/Destructor */ /* ************************************************************************** */ -VirtualMachine::VirtualMachine(int id): +VirtualMachine::VirtualMachine(int id, VirtualMachineTemplate * _vm_template): PoolObjectSQL(id), uid(-1), last_poll(0), name(""), - vm_template(), state(INIT), lcm_state(LCM_INIT), stime(time(0)), @@ -56,6 +55,14 @@ VirtualMachine::VirtualMachine(int id): previous_history(0), _log(0) { + if (_vm_template != 0) + { + vm_template = _vm_template; + } + else + { + vm_template = new VirtualMachineTemplate; + } } VirtualMachine::~VirtualMachine() @@ -74,6 +81,11 @@ VirtualMachine::~VirtualMachine() { delete _log; } + + if ( vm_template != 0 ) + { + delete vm_template; + } } /* ************************************************************************** */ @@ -140,7 +152,7 @@ int VirtualMachine::select_cb(void *nil, int num, char **values, char **names) last_seq = atoi(values[LAST_SEQ]); // Virtual Machine template ID is the VM ID - vm_template.id = oid; + vm_template->id = oid; return 0; } @@ -176,7 +188,7 @@ int VirtualMachine::select(SqlDB * db) } //Get the template - rc = vm_template.select(db); + rc = vm_template->select(db); if (rc != 0) { @@ -270,9 +282,9 @@ int VirtualMachine::insert(SqlDB * db) // ----------------------------------------------------------------------- // Set a template ID if it wasn't already assigned // ------------------------------------------------------------------------ - if ( vm_template.id == -1 ) + if ( vm_template->id == -1 ) { - vm_template.id = oid; + vm_template->id = oid; } // ----------------------------------------------------------------------- @@ -283,7 +295,7 @@ int VirtualMachine::insert(SqlDB * db) attr = new SingleAttribute("VMID",value); - vm_template.set(attr); + vm_template->set(attr); get_template_attribute("NAME",name); @@ -294,7 +306,7 @@ int VirtualMachine::insert(SqlDB * db) name = oss.str(); attr = new SingleAttribute("NAME",name); - vm_template.set(attr); + vm_template->set(attr); } this->name = name; @@ -345,7 +357,7 @@ int VirtualMachine::insert(SqlDB * db) // Insert the template first, so we get a valid template ID. Then the VM // ------------------------------------------------------------------------ - rc = vm_template.insert(db); + rc = vm_template->insert(db); if ( rc != 0 ) { @@ -363,7 +375,7 @@ int VirtualMachine::insert(SqlDB * db) error_update: NebulaLog::log("ONE",Log::ERROR, "Can not update VM in the database"); - vm_template.drop(db); + vm_template->drop(db); goto error_common; error_template: @@ -406,7 +418,7 @@ int VirtualMachine::parse_context() string * str; string parsed; - num = vm_template.remove("CONTEXT", array_context); + num = vm_template->remove("CONTEXT", array_context); if ( num == 0 ) { @@ -438,7 +450,7 @@ int VirtualMachine::parse_context() context_parsed = new VectorAttribute("CONTEXT"); context_parsed->unmarshall(parsed," @^_^@ "); - vm_template.set(context_parsed); + vm_template->set(context_parsed); } /* --- Delete old context attributes --- */ @@ -464,7 +476,7 @@ void VirtualMachine::parse_graphics() vector array_graphics; VectorAttribute * graphics; - num = vm_template.get("GRAPHICS", array_graphics); + num = vm_template->get("GRAPHICS", array_graphics); if ( num == 0 ) { @@ -511,7 +523,7 @@ int VirtualMachine::parse_requirements() string parsed; - num = vm_template.remove("REQUIREMENTS", array_reqs); + num = vm_template->remove("REQUIREMENTS", array_reqs); if ( num == 0 ) { @@ -533,7 +545,7 @@ int VirtualMachine::parse_requirements() SingleAttribute * reqs_parsed; reqs_parsed = new SingleAttribute("REQUIREMENTS",parsed); - vm_template.set(reqs_parsed); + vm_template->set(reqs_parsed); } /* --- Delete old requirements attributes --- */ @@ -803,7 +815,7 @@ int VirtualMachine::get_disk_images() Nebula& nd = Nebula::instance(); ipool = nd.get_ipool(); - num_disks = vm_template.get("DISK",disks); + num_disks = vm_template->get("DISK",disks); for(int i=0, index=0; iget("NIC",nics); for(int i=0; i" << net_tx << "" << "" << net_rx << "" << "" << last_seq << "" - << vm_template.to_xml(template_xml); + << vm_template->to_xml(template_xml); if ( hasHistory() ) { @@ -1163,7 +1175,7 @@ string& VirtualMachine::to_str(string& str) const << "NET TX : " << net_tx << endl << "NET RX : " << net_rx << endl << "LAST SEQ : " << last_seq << endl - << "Template" << endl << vm_template.to_str(template_str) << endl; + << "Template" << endl << vm_template->to_str(template_str) << endl; if ( hasHistory() ) { diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index 73a8365d64..bc1a744c24 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -151,19 +151,16 @@ VirtualMachinePool::VirtualMachinePool(SqlDB * db, int VirtualMachinePool::allocate ( int uid, - const string& stemplate, + VirtualMachineTemplate * vm_template, int * oid, bool on_hold) { VirtualMachine * vm; - char * error_msg; - int rc; - // ------------------------------------------------------------------------ // Build a new Virtual Machine object // ------------------------------------------------------------------------ - vm = new VirtualMachine; + vm = new VirtualMachine(-1,vm_template); if (on_hold == true) { @@ -176,24 +173,6 @@ int VirtualMachinePool::allocate ( vm->uid = uid; - // ------------------------------------------------------------------------ - // Parse template - // ------------------------------------------------------------------------ - rc = vm->vm_template.parse(stemplate,&error_msg); - - if ( rc != 0 ) - { - ostringstream oss; - - oss << error_msg; - NebulaLog::log("ONE", Log::ERROR, oss); - free(error_msg); - - delete vm; - - return -1; - } - // ------------------------------------------------------------------------ // Insert the Object in the pool // ------------------------------------------------------------------------ diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index 8b53ebf8b9..a6e38ee46a 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -17,6 +17,7 @@ #include "VirtualNetworkPool.h" #include "NebulaLog.h" +#include "AuthManager.h" #include #include @@ -218,8 +219,7 @@ int VirtualNetworkPool::dump(ostringstream& oss, const string& where) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, - int vid) +int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, int vid) { string network; VirtualNetwork * vnet = 0; @@ -245,8 +245,6 @@ int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, { vnet = get(network_id,true); } - - return -2; } else { @@ -265,3 +263,51 @@ int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, return rc; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void VirtualNetworkPool::authorize_nic(VectorAttribute * nic, AuthRequest * ar) +{ + string network; + VirtualNetwork * vnet = 0; + + network = nic->vector_value("NETWORK"); + + if (network.empty()) + { + istringstream is; + int network_id; + + network = nic->vector_value("NETWORK_ID"); + + if(network.empty()) + { + return; + } + + is.str(network); + is >> network_id; + + if( !is.fail() ) + { + vnet = get(network_id,true); + } + } + else + { + vnet = get(network,true); + } + + if (vnet == 0) + { + return; + } + + ar->add_auth(AuthRequest::NET, + vnet->get_vnid(), + AuthRequest::USE, + vnet->get_uid(), + vnet->isPublic()); + + vnet->unlock(); +} From 0dbb3330fe007b52d427f05ac142bb9cf4f20726 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 14 Jul 2010 19:37:43 +0200 Subject: [PATCH 56/64] feature #203: authorization now in RM.*allocate for IMAGEs and NETs --- include/Image.h | 14 ++-- include/ImagePool.h | 10 +-- include/VirtualNetwork.h | 16 ++-- include/VirtualNetworkPool.h | 9 +-- src/image/Image.cc | 43 +++++++---- src/image/ImagePool.cc | 36 ++------- src/rm/RequestManagerAllocate.cc | 2 - src/rm/RequestManagerImageAllocate.cc | 67 ++++++++++++++-- .../RequestManagerVirtualNetworkAllocate.cc | 76 ++++++++++++++----- src/vnm/VirtualNetwork.cc | 40 +++++++--- src/vnm/VirtualNetworkPool.cc | 24 +----- 11 files changed, 204 insertions(+), 133 deletions(-) diff --git a/include/Image.h b/include/Image.h index aa87243c2a..63feb4f996 100644 --- a/include/Image.h +++ b/include/Image.h @@ -221,7 +221,7 @@ public: string& name, vector& values) const { - return image_template.get(name,values); + return image_template->get(name,values); }; /** @@ -235,7 +235,7 @@ public: vector& values) const { string str=name; - return image_template.get(str,values); + return image_template->get(str,values); }; /** @@ -248,7 +248,7 @@ public: string& value) const { string str=name; - image_template.get(str,value); + image_template->get(str,value); } /** @@ -261,7 +261,7 @@ public: int& value) const { string str=name; - image_template.get(str,value); + image_template->get(str,value); } /** @@ -270,7 +270,7 @@ public: */ int remove_template_attribute(SqlDB * db, const string& name) { - return image_template.remove_attribute(db, name); + return image_template->remove_attribute(db, name); } private: @@ -332,7 +332,7 @@ private: /** * The Image template, holds the Image attributes. */ - ImageTemplate image_template; + ImageTemplate * image_template; // ************************************************************************* @@ -382,7 +382,7 @@ protected: // Constructor // ************************************************************************* - Image(int id=-1); + Image(int uid=-1, ImageTemplate *img_template = 0); virtual ~Image(); diff --git a/include/ImagePool.h b/include/ImagePool.h index 709f6bcdf3..968188178d 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -55,9 +55,9 @@ public: * -2 in case of template parse failure */ int allocate ( - int uid, - const string& stemplate, - int * oid); + int uid, + ImageTemplate * img_template, + int * oid); /** * Function to get a Image from the pool, if the object is not in memory @@ -133,7 +133,7 @@ public: { SingleAttribute * sattr = new SingleAttribute(name,value); - return image->image_template.replace_attribute(db,sattr); + return image->image_template->replace_attribute(db,sattr); } /** Delete an image attribute in the template (Image MUST be locked) @@ -145,7 +145,7 @@ public: Image * image, const string& name) { - return image->image_template.remove_attribute(db, name); + return image->image_template->remove_attribute(db, name); } /** diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 5d8ac3c8a7..cf37a96753 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -195,7 +195,7 @@ public: string& name, vector& values) const { - return vn_template.get(name,values); + return vn_template->get(name,values); }; /** @@ -209,7 +209,7 @@ public: vector& values) const { string str=name; - return vn_template.get(str,values); + return vn_template->get(str,values); }; /** @@ -222,7 +222,7 @@ public: string& value) const { string str=name; - vn_template.get(str,value); + vn_template->get(str,value); } /** @@ -235,7 +235,7 @@ public: int& value) const { string str=name; - vn_template.get(str,value); + vn_template->get(str,value); } private: @@ -293,7 +293,7 @@ private: /** * The Virtual Network template, holds the VNW attributes. */ - VirtualNetworkTemplate vn_template; + VirtualNetworkTemplate * vn_template; // ************************************************************************* // DataBase implementation (Private) @@ -355,7 +355,7 @@ private: int rc; sattr = new SingleAttribute(name,value); - rc = vn_template.replace_attribute(db,sattr); + rc = vn_template->replace_attribute(db,sattr); if (rc != 0) { @@ -371,7 +371,7 @@ protected: // Constructor //************************************************************************** - VirtualNetwork(); + VirtualNetwork(VirtualNetworkTemplate * _vn_template = 0); ~VirtualNetwork(); @@ -428,7 +428,7 @@ protected: { int rc; - rc = vn_template.drop(db); + rc = vn_template->drop(db); rc += leases->drop(db); diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index 633ff9aebc..f57c53037b 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -40,16 +40,15 @@ public: ~VirtualNetworkPool(){}; /** - * Function to allocate a new VN object + * Function to allocate a new VNET object * @param uid user identifier - * @param stemplate a string describing the VN + * @param vn_template a VirtualNetworkTemplate describing the VNET * @param oid the id assigned to the VM (output) - * @return oid on success, -1 error inserting in DB,-2 error parsing - * the template, -3 wrong attributes in template + * @return oid on success, -1 error */ int allocate ( int uid, - const string& stemplate, + VirtualNetworkTemplate * vn_template, int * oid); /** diff --git a/src/image/Image.cc b/src/image/Image.cc index f5a6fec6f6..fd983e6d7b 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -32,7 +32,7 @@ /* Image :: Constructor/Destructor */ /* ************************************************************************ */ -Image::Image(int _uid): +Image::Image(int _uid, ImageTemplate * _image_template): PoolObjectSQL(-1), uid(_uid), name(""), @@ -41,9 +41,24 @@ Image::Image(int _uid): source(""), state(INIT), running_vms(0) - {}; +{ + if (_image_template != 0) + { + image_template = _image_template; + } + else + { + image_template = new ImageTemplate; + } +}; -Image::~Image(){}; +Image::~Image() +{ + if (image_template != 0) + { + delete image_template; + } +}; /* ************************************************************************ */ /* Image :: Database Access Functions */ @@ -93,7 +108,7 @@ int Image::select_cb(void * nil, int num, char **values, char ** names) running_vms = atoi(values[RUNNING_VMS]); - image_template.id = oid; + image_template->id = oid; return 0; } @@ -123,7 +138,7 @@ int Image::select(SqlDB *db) // Get the template - rc = image_template.select(db); + rc = image_template->select(db); if ( rc != 0 ) { @@ -182,7 +197,7 @@ int Image::insert(SqlDB *db) // ------------ PUBLIC -------------------- get_template_attribute("PUBLIC", public_attr); - image_template.erase("PUBLIC"); + image_template->erase("PUBLIC"); transform (public_attr.begin(), public_attr.end(), public_attr.begin(), (int(*)(int))toupper); @@ -198,7 +213,7 @@ int Image::insert(SqlDB *db) SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX", ImagePool::default_dev_prefix()); - image_template.set(dev_att); + image_template->set(dev_att); } // ------------ SOURCE (path to store the image)-------------------- @@ -212,14 +227,14 @@ int Image::insert(SqlDB *db) // ------------ INSERT THE TEMPLATE -------------------- - if ( image_template.id == -1 ) + if ( image_template->id == -1 ) { - image_template.id = oid; + image_template->id = oid; } state = DISABLED; - rc = image_template.insert(db); + rc = image_template->insert(db); if ( rc != 0 ) { @@ -234,7 +249,7 @@ int Image::insert(SqlDB *db) if ( rc != 0 ) { - image_template.drop(db); + image_template->drop(db); return rc; } @@ -374,7 +389,7 @@ int Image::drop(SqlDB * db) return -1; } - image_template.drop(db); + image_template->drop(db); oss << "DELETE FROM " << table << " WHERE oid=" << oid; @@ -424,7 +439,7 @@ string& Image::to_xml(string& xml) const "" << source << "" << "" << state << "" << "" << running_vms << "" << - image_template.to_xml(template_xml) << + image_template->to_xml(template_xml) << ""; xml = oss.str(); @@ -452,7 +467,7 @@ string& Image::to_str(string& str) const "STATE = " << state << endl << "RUNNING_VMS = " << running_vms << endl << "TEMPLATE" << endl - << image_template.to_str(template_str) + << image_template->to_str(template_str) << endl; str = os.str(); diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 7d89ccf4c5..6982dda21b 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -89,55 +89,31 @@ ImagePool::ImagePool( SqlDB * db, int ImagePool::allocate ( int uid, - const string& stemplate, + ImageTemplate* img_template, int * oid) { - int rc; Image * img; - string name; - char * error_msg; // --------------------------------------------------------------------- // Build a new Image object // --------------------------------------------------------------------- - img = new Image(uid); - - // --------------------------------------------------------------------- - // Parse template - // --------------------------------------------------------------------- - rc = img->image_template.parse(stemplate, &error_msg); - - if ( rc != 0 ) - { - ostringstream oss; - oss << "ImagePool template parse error: " << error_msg; - NebulaLog::log("IMG", Log::ERROR, oss); - - free(error_msg); - delete img; - - return -1; - } + img = new Image(uid,img_template); img->get_template_attribute("NAME", name); // --------------------------------------------------------------------- // Insert the Object in the pool // --------------------------------------------------------------------- - *oid = PoolSQL::allocate(img); - if ( *oid == -1 ) - { - return -1; - } - // --------------------------------------------------------------------- // Add the image name to the map of image_names // --------------------------------------------------------------------- - - image_names.insert(make_pair(name, *oid)); + if ( *oid != -1 ) + { + image_names.insert(make_pair(name, *oid)); + } return *oid; } diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 6d4bb4289d..c9fd655bcd 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -70,7 +70,6 @@ void RequestManager::VirtualMachineAllocate::execute( //-------------------------------------------------------------------------- // Authorize this request //-------------------------------------------------------------------------- - vm_template = new VirtualMachineTemplate; rc = vm_template->parse(str_template,&error_msg); @@ -129,7 +128,6 @@ void RequestManager::VirtualMachineAllocate::execute( //-------------------------------------------------------------------------- // Allocate the VirtualMAchine //-------------------------------------------------------------------------- - rc = vmpool->allocate(uid,vm_template,&vid,false); if ( rc < 0 ) diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 21d88698a1..30f11a105d 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -29,11 +29,14 @@ void RequestManager::ImageAllocate::execute( xmlrpc_c::value * const retval) { string session; - string image_template; + string str_template; + + ImageTemplate * img_template; int iid; int uid; int rc; + char * error_msg = 0; ostringstream oss; @@ -42,15 +45,15 @@ void RequestManager::ImageAllocate::execute( vector arrayData; xmlrpc_c::value_array * arrayresult; - NebulaLog::log("ReM",Log::DEBUG,"ImageAllocate invoked"); - session = xmlrpc_c::value_string(paramList.getString(0)); - image_template = xmlrpc_c::value_string(paramList.getString(1)); - image_template += "\n"; + session = xmlrpc_c::value_string(paramList.getString(0)); + str_template = xmlrpc_c::value_string(paramList.getString(1)); + str_template += "\n"; - - // First, we need to authenticate the user + //-------------------------------------------------------------------------- + // Authorize this request + //-------------------------------------------------------------------------- uid = ImageAllocate::upool->authenticate(session); if ( uid == -1 ) @@ -58,7 +61,39 @@ void RequestManager::ImageAllocate::execute( goto error_authenticate; } - rc = ImageAllocate::ipool->allocate(uid,image_template,&iid); + //-------------------------------------------------------------------------- + // Authorize this request + //-------------------------------------------------------------------------- + img_template = new ImageTemplate; + + rc = img_template->parse(str_template,&error_msg); + + if ( rc != 0 ) + { + goto error_parse; + } + + if ( uid != 0 ) + { + AuthRequest ar(uid); + string t64; + + ar.add_auth(AuthRequest::IMAGE, + img_template->to_xml(t64), + AuthRequest::CREATE, + uid, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + //-------------------------------------------------------------------------- + // Allocate the Image + //-------------------------------------------------------------------------- + rc = ImageAllocate::ipool->allocate(uid,img_template,&iid); if ( rc < 0 ) { @@ -81,6 +116,22 @@ error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; +error_authorize: + oss.str(authorization_error(method_name, "CREATE", "IMAGE", uid, -1)); + delete img_template; + goto error_common; + +error_parse: + oss.str(action_error(method_name, "PARSE", "IMAGE TEMPLATE",-2,rc)); + if (error_msg != 0) + { + oss << "Reason: " << error_msg; + free(error_msg); + } + + delete img_template; + goto error_common; + error_allocate: oss.str(action_error(method_name, "CREATE", "IMAGE", -2, rc)); goto error_common; diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index ddc1e880d8..0592098cdb 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -27,19 +27,18 @@ void RequestManager::VirtualNetworkAllocate::execute( xmlrpc_c::value * const retval) { string session; - string username; - string password; string name; - string stemplate; + string str_template; + + VirtualNetworkTemplate * vn_template; int nid; int uid; int rc; - - User * user; + char * error_msg = 0; ostringstream oss; - + const string method_name = "VirtualNetworkAllocate"; /* -- RPC specific vars -- */ @@ -49,27 +48,52 @@ void RequestManager::VirtualNetworkAllocate::execute( NebulaLog::log("ReM",Log::DEBUG,"VirtualNetworkAllocate method invoked"); // Get the parameters & host - session = xmlrpc_c::value_string(paramList.getString(0)); - stemplate = xmlrpc_c::value_string(paramList.getString(1)); + session = xmlrpc_c::value_string(paramList.getString(0)); + str_template = xmlrpc_c::value_string(paramList.getString(1)); - if ( User::split_secret(session,username,password) != 0 ) + //-------------------------------------------------------------------------- + // Authorize this request + //-------------------------------------------------------------------------- + uid = VirtualNetworkAllocate::upool->authenticate(session); + + if ( uid == -1 ) { goto error_authenticate; } - // Now let's get the user - user = VirtualNetworkAllocate::upool->get(username,true); + //-------------------------------------------------------------------------- + // Authorize this request + //-------------------------------------------------------------------------- + vn_template = new VirtualNetworkTemplate; - if ( user == 0 ) + rc = vn_template->parse(str_template,&error_msg); + + if ( rc != 0 ) { - goto error_get_user; + goto error_parse; } - uid = user->get_uid(); + if ( uid != 0 ) + { + AuthRequest ar(uid); + string t64; - user->unlock(); - - rc = vnpool->allocate(uid,stemplate,&nid); + ar.add_auth(AuthRequest::NET, + vn_template->to_xml(t64), + AuthRequest::CREATE, + uid, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + //-------------------------------------------------------------------------- + // Allocate the Virtual Network + //-------------------------------------------------------------------------- + rc = vnpool->allocate(uid,vn_template,&nid); if ( rc < 0 ) { @@ -92,10 +116,22 @@ error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; -error_get_user: - oss.str(get_error(method_name, "USER", -1)); +error_authorize: + oss.str(authorization_error(method_name, "CREATE", "VNET", uid, -1)); + delete vn_template; goto error_common; - + +error_parse: + oss.str(action_error(method_name, "PARSE", "VNET TEMPLATE",-2,rc)); + if (error_msg != 0) + { + oss << "Reason: " << error_msg; + free(error_msg); + } + + delete vn_template; + goto error_common; + error_vn_allocate: oss.str(action_error(method_name, "CREATE", "NET", -2, rc)); goto error_common; diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 5020b3fb5b..c7a04e1670 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -29,13 +29,23 @@ /* Virtual Network :: Constructor/Destructor */ /* ************************************************************************** */ -VirtualNetwork::VirtualNetwork(): +VirtualNetwork::VirtualNetwork(VirtualNetworkTemplate *_vn_template): PoolObjectSQL(-1), name(""), uid(-1), bridge(""), type(UNINITIALIZED), - leases(0){}; + leases(0) +{ + if (_vn_template != 0) + { + vn_template = _vn_template; + } + else + { + vn_template = new VirtualNetworkTemplate; + } +}; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -46,6 +56,11 @@ VirtualNetwork::~VirtualNetwork() { delete leases; } + + if (vn_template != 0) + { + delete vn_template; + } } /* ************************************************************************** */ @@ -89,7 +104,7 @@ int VirtualNetwork::select_cb(void * nil, int num, char **values, char **names) public_vnet = atoi(values[PUBLIC]); // Virtual Network template ID is the Network ID - vn_template.id = oid; + vn_template->id = oid; return 0; } @@ -128,7 +143,7 @@ int VirtualNetwork::select(SqlDB * db) } //Get the template - rc = vn_template.select(db); + rc = vn_template->select(db); if (rc != 0) { @@ -315,16 +330,16 @@ int VirtualNetwork::insert(SqlDB * db) public_vnet = (pub == "YES"); - vn_template.erase("PUBLIC"); + vn_template->erase("PUBLIC"); // ------------ INSERT THE TEMPLATE -------------------- - if ( vn_template.id == -1 ) + if ( vn_template->id == -1 ) { - vn_template.id = oid; + vn_template->id = oid; } - rc = vn_template.insert(db); + rc = vn_template->insert(db); if ( rc != 0 ) { @@ -425,7 +440,7 @@ error_template: error_update: ose << "Can not update Virtual Network id " << oid; - vn_template.drop(db); + vn_template->drop(db); goto error_common; error_addr: @@ -506,7 +521,7 @@ int VirtualNetwork::vn_drop(SqlDB * db) ostringstream oss; int rc; - vn_template.drop(db); + vn_template->drop(db); if ( leases != 0 ) { @@ -558,7 +573,7 @@ string& VirtualNetwork::to_xml(string& xml) const "" << type << "" << "" << bridge << "" << "" << public_vnet << "" << - vn_template.to_xml(template_xml); + vn_template->to_xml(template_xml); if (leases) { os << leases->to_xml(leases_xml); @@ -596,7 +611,8 @@ string& VirtualNetwork::to_str(string& str) const os << "Bridge : " << bridge << endl; os << "Public : " << public_vnet << endl << endl; - os << "....: Template :...." << vn_template.to_str(template_str) << endl << endl; + os << "....: Template :...." << vn_template->to_str(template_str) << endl << +endl; if (leases) { diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index a6e38ee46a..2e6d755e11 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -71,35 +71,15 @@ VirtualNetworkPool::VirtualNetworkPool(SqlDB * db, int VirtualNetworkPool::allocate ( int uid, - const string& stemplate, + VirtualNetworkTemplate * vn_template, int * oid) { VirtualNetwork * vn; - char * error_msg; - int rc; - // Build a new Virtual Network object - vn = new VirtualNetwork(); + vn = new VirtualNetwork(vn_template); vn->uid = uid; - rc = vn->vn_template.parse(stemplate,&error_msg); - - if ( rc != 0 ) - { - ostringstream oss; - - oss << error_msg; - NebulaLog::log("VNM", Log::ERROR, oss); - free(error_msg); - - delete vn; - - return -1; - } - - // Insert the VN in the pool so we have a valid OID - *oid = PoolSQL::allocate(vn); return *oid; From 498102b786ed497c893f5d41b8572d4478dfa6a2 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 14 Jul 2010 19:49:16 +0200 Subject: [PATCH 57/64] feature #203: Better public in Allocate Methods --- src/rm/RequestManagerImageAllocate.cc | 7 ++++++- src/rm/RequestManagerVirtualNetworkAllocate.cc | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 30f11a105d..8fc05ee328 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -77,12 +77,17 @@ void RequestManager::ImageAllocate::execute( { AuthRequest ar(uid); string t64; + string pub; + string pub_name = "PUBLIC"; + + img_template->get(pub_name, pub); + transform (pub.begin(), pub.end(), pub.begin(),(int(*)(int))toupper); ar.add_auth(AuthRequest::IMAGE, img_template->to_xml(t64), AuthRequest::CREATE, uid, - false); + (pub == "YES")); if (UserPool::authorize(ar) == -1) { diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 0592098cdb..761c47baa3 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -77,12 +77,17 @@ void RequestManager::VirtualNetworkAllocate::execute( { AuthRequest ar(uid); string t64; + string pub; + string pub_name = "PUBLIC"; + + vn_template->get(pub_name, pub); + transform (pub.begin(), pub.end(), pub.begin(),(int(*)(int))toupper); ar.add_auth(AuthRequest::NET, vn_template->to_xml(t64), AuthRequest::CREATE, uid, - false); + (pub == "YES")); if (UserPool::authorize(ar) == -1) { From f3fc078b6c6a845c9fb319a611a6dfe485c5373b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Thu, 15 Jul 2010 12:43:27 +0200 Subject: [PATCH 58/64] feature #203: Adding comments, fixing pools accesors in RM --- include/RequestManager.h | 53 ++++++++++++++++--- src/rm/RequestManager.cc | 2 +- src/rm/RequestManagerAllocate.cc | 15 ++---- src/rm/RequestManagerImageAllocate.cc | 4 +- .../RequestManagerVirtualNetworkAllocate.cc | 4 +- 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/include/RequestManager.h b/include/RequestManager.h index 5e4b47fe45..f76af653d8 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -173,6 +173,16 @@ private: // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- + + /** + * Logs authorization errors + * @param method name of the RM method where the error arose + * @param action authorization action + * @param object object that needs to be authorized + * @param uid user that is authorized + * @param id id of the object, -1 for Pool + * @returns string for logging + */ static string authorization_error (const string& method, const string &action, const string &object, @@ -180,7 +190,7 @@ private: int id) { ostringstream oss; - oss << "[" << method << "]" << " User [" << uid << "] not authorized " + oss << "[" << method << "]" << " User [" << uid << "] not authorized" << " to perform " << action << " on " << object; @@ -196,6 +206,11 @@ private: return oss.str(); } + /** + * Logs authenticate errors + * @param method name of the RM method where the error arose + * @returns string for logging + */ static string authenticate_error (const string& method) { ostringstream oss; @@ -205,7 +220,14 @@ private: return oss.str(); } - + + /** + * Logs get object errors + * @param method name of the RM method where the error arose + * @param object over which the get failed + * @param id of the object over which the get failed + * @returns string for logging + */ static string get_error (const string& method, const string &object, int id) @@ -227,6 +249,16 @@ private: return oss.str(); } + /** + * Logs action errors + * @param method name of the RM method where the error arose + * @param action that triggered the error + * @param object over which the action was applied + * @param id id of the object, -1 for Pool, -2 for no-id objects + * (allocate error, parse error) + * @param rc returned error code (NULL to ignore) + * @returns string for logging + */ static string action_error (const string& method, const string &action, const string &object, @@ -271,8 +303,14 @@ private: { public: VirtualMachineAllocate( - UserPool * _upool): - upool(_upool) + VirtualMachinePool * _vmpool, + VirtualNetworkPool * _vnpool, + ImagePool * _ipool, + UserPool * _upool): + vmpool(_vmpool), + vnpool(_vnpool), + ipool(_ipool), + upool(_upool) { _signature="A:ss"; _help="Allocates a virtual machine in the pool"; @@ -284,6 +322,9 @@ private: xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retval); private: + VirtualMachinePool * vmpool; + VirtualNetworkPool * vnpool; + ImagePool * ipool; UserPool * upool; }; @@ -294,8 +335,8 @@ private: public: VirtualMachineDeploy( VirtualMachinePool * _vmpool, - HostPool * _hpool, - UserPool * _upool): + HostPool * _hpool, + UserPool * _upool): vmpool(_vmpool), hpool(_hpool), upool(_upool) diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index cf3fb033e0..a3f4b3af39 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -214,7 +214,7 @@ void RequestManager::do_action( void RequestManager::register_xml_methods() { xmlrpc_c::methodPtr vm_allocate(new - RequestManager::VirtualMachineAllocate(upool)); + RequestManager::VirtualMachineAllocate(vmpool, vnpool, ipool, upool)); xmlrpc_c::methodPtr vm_deploy(new RequestManager::VirtualMachineDeploy(vmpool,hpool,upool)); diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index c9fd655bcd..f51d7c8470 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -34,11 +34,6 @@ void RequestManager::VirtualMachineAllocate::execute( int vid, uid; int rc; - Nebula& nd = Nebula::instance(); - ImagePool * ipool = nd.get_ipool(); - VirtualNetworkPool* vnpool= nd.get_vnpool(); - VirtualMachinePool* vmpool= nd.get_vmpool(); - ostringstream oss; vector arrayData; @@ -96,7 +91,7 @@ void RequestManager::VirtualMachineAllocate::execute( continue; } - ipool->authorize_disk(vector,&ar); + VirtualMachineAllocate::ipool->authorize_disk(vector,&ar); } num = vm_template->get("NIC",vectors); @@ -110,7 +105,7 @@ void RequestManager::VirtualMachineAllocate::execute( continue; } - vnpool->authorize_nic(vector,&ar); + VirtualMachineAllocate::vnpool->authorize_nic(vector,&ar); } ar.add_auth(AuthRequest::VM, @@ -128,7 +123,7 @@ void RequestManager::VirtualMachineAllocate::execute( //-------------------------------------------------------------------------- // Allocate the VirtualMAchine //-------------------------------------------------------------------------- - rc = vmpool->allocate(uid,vm_template,&vid,false); + rc = VirtualMachineAllocate::vmpool->allocate(uid,vm_template,&vid,false); if ( rc < 0 ) { @@ -157,10 +152,10 @@ error_authorize: goto error_common; error_parse: - oss.str(action_error(method_name, "PARSE", "VM TEMPLATE",-2,rc)); + oss << action_error(method_name, "PARSE", "VM TEMPLATE",-2,rc); if (error_msg != 0) { - oss << "Reason: " << error_msg; + oss << ". Reason: " << error_msg; free(error_msg); } diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 8fc05ee328..84e1fd287f 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -127,10 +127,10 @@ error_authorize: goto error_common; error_parse: - oss.str(action_error(method_name, "PARSE", "IMAGE TEMPLATE",-2,rc)); + oss << action_error(method_name, "PARSE", "IMAGE TEMPLATE",-2,rc); if (error_msg != 0) { - oss << "Reason: " << error_msg; + oss << ". Reason: " << error_msg; free(error_msg); } diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 761c47baa3..0856d29d81 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -127,10 +127,10 @@ error_authorize: goto error_common; error_parse: - oss.str(action_error(method_name, "PARSE", "VNET TEMPLATE",-2,rc)); + oss << action_error(method_name, "PARSE", "VNET TEMPLATE",-2,rc); if (error_msg != 0) { - oss << "Reason: " << error_msg; + oss << ". Reason: " << error_msg; free(error_msg); } From d68855a4c686e5e4b0e7e4f87c4fb811d7905e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 15 Jul 2010 12:51:05 +0200 Subject: [PATCH 59/64] feature #192: New tests for ImagePool::disk_attribute and tests fixed for new ImagePool::allocate parameters (#203). --- src/image/test/ImagePoolTest.cc | 130 ++++++++++++++++++++++++++++++-- 1 file changed, 123 insertions(+), 7 deletions(-) diff --git a/src/image/test/ImagePoolTest.cc b/src/image/test/ImagePoolTest.cc index a883c070d0..2f2198caeb 100644 --- a/src/image/test/ImagePoolTest.cc +++ b/src/image/test/ImagePoolTest.cc @@ -65,6 +65,40 @@ const string xml_dump_where = const string replacement = "0000000000"; +class ImagePoolFriend : public ImagePool +{ +public: + ImagePoolFriend(SqlDB * db, + const string& _source_prefix, + const string& _default_type, + const string& _default_dev_prefix): + + ImagePool( db, + _source_prefix, + _default_type, + _default_dev_prefix){}; + + + int allocate(const int& uid, const std::string& stemplate, int* oid) + { + ImageTemplate * img_template; + char * error_msg = 0; + int rc; + + img_template = new ImageTemplate; + rc = img_template->parse(stemplate,&error_msg); + + if( rc == 0 ) + { + return ImagePool::allocate(uid, img_template, oid); + } + else + { + return -2; + } + }; +}; + /* ************************************************************************* */ /* ************************************************************************* */ @@ -86,6 +120,7 @@ class ImagePoolTest : public PoolTest CPPUNIT_TEST ( bus_source_assignment ); CPPUNIT_TEST ( public_attribute ); CPPUNIT_TEST ( disk_overwrite ); + CPPUNIT_TEST ( imagepool_disk_attribute ); CPPUNIT_TEST ( dump ); CPPUNIT_TEST ( dump_where ); @@ -100,13 +135,15 @@ protected: PoolSQL* create_pool(SqlDB* db) { - return new ImagePool(db, "source_prefix", "OS", "hd"); + ImagePoolFriend * imp = + new ImagePoolFriend(db, "source_prefix", "OS", "hd"); + return imp; }; int allocate(int index) { int oid; - return ((ImagePool*)pool)->allocate(uids[index], + return ((ImagePoolFriend*)pool)->allocate(uids[index], templates[index], &oid); @@ -320,7 +357,7 @@ public: void duplicates() { int rc, oid; - ImagePool * imp = static_cast(pool); + ImagePoolFriend * imp = static_cast(pool); // Allocate an image. rc = imp->allocate(uids[0], templates[0], &oid); @@ -371,7 +408,7 @@ public: void wrong_templates() { int rc; - ImagePool * imp = static_cast(pool); + ImagePoolFriend * imp = static_cast(pool); string templates[] = { @@ -410,7 +447,7 @@ public: void target_generation() { - ImagePool * imp = static_cast(pool); + ImagePoolFriend * imp = static_cast(pool); Image * img; VectorAttribute * disk; @@ -682,13 +719,85 @@ public: delete disk; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + + void imagepool_disk_attribute() + { + ImagePoolFriend * imp = static_cast(pool); + Image * img; + + VectorAttribute * disk; + int oid_0, oid_1; + string value; + + // --------------------------------------------------------------------- + // Allocate 2 images, with different dev_prefix + + string template_0 = "NAME = \"Image 0\"\n" + "DEV_PREFIX = \"hd\"\n"; + + string template_1 = "NAME = \"Image 1\"\n" + "DEV_PREFIX = \"sd\"\n"; + + + imp->allocate(0, template_0, &oid_0); + CPPUNIT_ASSERT( oid_0 == 0 ); + + imp->allocate(0, template_1, &oid_1); + CPPUNIT_ASSERT( oid_1 == 1 ); + + + img = imp->get(oid_0, false); + CPPUNIT_ASSERT( img != 0 ); + img->enable(true); + + img = imp->get(oid_1, false); + CPPUNIT_ASSERT( img != 0 ); + img->enable(true); + + + // Disk using image 0 + disk = new VectorAttribute("DISK"); + disk->replace("IMAGE", "Image 0"); + + ((ImagePool*)imp)->disk_attribute(disk, 0); + + value = ""; + value = disk->vector_value("TARGET"); + CPPUNIT_ASSERT( value == "hda" ); + + value = ""; + value = disk->vector_value("IMAGE_ID"); + CPPUNIT_ASSERT( value == "0" ); + + delete disk; + + + // Disk using image 1 index + disk = new VectorAttribute("DISK"); + disk->replace("IMAGE_ID", "1"); + + ((ImagePool*)imp)->disk_attribute(disk, 0); + + value = ""; + value = disk->vector_value("TARGET"); + CPPUNIT_ASSERT( value == "sda" ); + + value = ""; + value = disk->vector_value("IMAGE"); + CPPUNIT_ASSERT( value == "Image 1" ); + + delete disk; + } + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void public_attribute() { int oid; - ImagePool * imp = static_cast(pool); + ImagePoolFriend * imp = static_cast(pool); Image * img; string templates[] = @@ -754,7 +863,14 @@ public: img = imp->get( oid, false ); CPPUNIT_ASSERT( img != 0 ); //cout << endl << i << " : exp. " << results[i] << " got " << img->is_public(); - CPPUNIT_ASSERT( img->is_public() == results[i] ); + +// ============================================================================= +// ============================================================================= +// Branch feature 203 doesn't have is_public method, but this test is +// still valid for master. +// CPPUNIT_ASSERT( img->is_public() == results[i] ); +// ============================================================================= +// ============================================================================= i++; } From 57e5c9598d52876862ef940ed036e3f595d6d0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 15 Jul 2010 12:51:27 +0200 Subject: [PATCH 60/64] feature #192: Tests fixed for new VirtualNetworkPool::allocate parameters (#203). --- src/vnm/test/VirtualNetworkPoolTest.cc | 162 +++++++++++++++++++++---- 1 file changed, 138 insertions(+), 24 deletions(-) diff --git a/src/vnm/test/VirtualNetworkPoolTest.cc b/src/vnm/test/VirtualNetworkPoolTest.cc index 8809cbb60b..267f2da00e 100644 --- a/src/vnm/test/VirtualNetworkPoolTest.cc +++ b/src/vnm/test/VirtualNetworkPoolTest.cc @@ -85,6 +85,41 @@ const string xml_dump_where = /* ************************************************************************* */ /* ************************************************************************* */ +class VirtualNetworkPoolFriend : public VirtualNetworkPool +{ +public: + + VirtualNetworkPoolFriend( SqlDB * db, + const string& str_mac_prefix, + int default_size): + + VirtualNetworkPool( db, str_mac_prefix, default_size) + {}; + + + int allocate(const int& uid, const std::string& stemplate, int* oid) + { + VirtualNetworkTemplate * vn_template; + char * error_msg = 0; + int rc; + + vn_template = new VirtualNetworkTemplate; + rc = vn_template->parse(stemplate,&error_msg); + + if( rc == 0 ) + { + return VirtualNetworkPool::allocate(uid, vn_template, oid); + } + else + { + return -1; + } + }; +}; + +/* ************************************************************************* */ +/* ************************************************************************* */ + class VirtualNetworkPoolTest : public PoolTest { CPPUNIT_TEST_SUITE (VirtualNetworkPoolTest); @@ -107,6 +142,7 @@ class VirtualNetworkPoolTest : public PoolTest CPPUNIT_TEST (overlapping_leases_rr); CPPUNIT_TEST (drop_leases); CPPUNIT_TEST (public_attribute); + CPPUNIT_TEST (vnpool_nic_attribute); CPPUNIT_TEST_SUITE_END (); @@ -127,7 +163,7 @@ protected: int allocate(int index) { int oid; - return ((VirtualNetworkPool*)pool)->allocate(uids[index], + return ((VirtualNetworkPoolFriend*)pool)->allocate(uids[index], templates[index], &oid); }; @@ -181,9 +217,10 @@ public: void allocate_rcs() { - VirtualNetworkPool * vnpool = static_cast(pool); + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); VirtualNetwork * vn; - int rc; + int rc; // Check case rc = allocate(2); @@ -207,6 +244,9 @@ public: CPPUNIT_ASSERT( rc == -1 ); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void get_using_name() { VirtualNetworkPool * vnpool = static_cast(pool); @@ -235,6 +275,9 @@ public: check(1, vn); }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void wrong_get_name() { VirtualNetworkPool * vnpool = static_cast(pool); @@ -252,16 +295,23 @@ public: CPPUNIT_ASSERT( vn == 0 ); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void update() { // TODO }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void size() { - VirtualNetworkPool * vnpool = static_cast(pool); + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); - int rc; + int rc; VirtualNetwork* vnet; string templ[] = { @@ -338,10 +388,14 @@ public: } } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void duplicates() { int rc, oid_0, oid_1; - VirtualNetworkPool * vnpool = static_cast(pool); + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); VirtualNetwork * vnet; // Allocate a vnet @@ -382,9 +436,13 @@ public: check(0, vnet); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void dump() { - VirtualNetworkPool * vnpool = static_cast(pool); + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); int oid, rc; ostringstream oss; @@ -401,9 +459,13 @@ public: CPPUNIT_ASSERT( result == xml_dump ); }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void dump_where() { - VirtualNetworkPool * vnpool = static_cast(pool); + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); int oid, rc; ostringstream oss; @@ -421,8 +483,13 @@ public: CPPUNIT_ASSERT( result == xml_dump_where ); }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void fixed_leases() { + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); int rc, oid; VirtualNetwork *vn; @@ -435,7 +502,7 @@ public: oid = allocate(0); CPPUNIT_ASSERT( oid != -1 ); - vn = ((VirtualNetworkPool*)pool)->get(oid, false); + vn = vnpool->get(oid, false); CPPUNIT_ASSERT( vn != 0 ); // Ask for an IP @@ -454,7 +521,7 @@ public: pool->clean(); // Read the Vnet from the DB - vn = ((VirtualNetworkPool*)pool)->get(oid, false); + vn = vnpool->get(oid, false); CPPUNIT_ASSERT( vn != 0 ); // Ask for another IP @@ -495,8 +562,13 @@ public: CPPUNIT_ASSERT( bridge == "br1" ); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void ranged_leases() { + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); int rc, oid; VirtualNetwork *vn; @@ -512,10 +584,10 @@ public: "NETWORK_ADDRESS = 192.168.50.0\n"; - ((VirtualNetworkPool*)pool)->allocate(45, tmpl, &oid); + vnpool->allocate(45, tmpl, &oid); CPPUNIT_ASSERT( oid != -1 ); - vn = ((VirtualNetworkPool*)pool)->get(oid, false); + vn = vnpool->get(oid, false); CPPUNIT_ASSERT( vn != 0 ); // Ask for an IP @@ -533,7 +605,7 @@ public: pool->clean(); // Read the Vnet from the DB - vn = ((VirtualNetworkPool*)pool)->get(oid, false); + vn = vnpool->get(oid, false); CPPUNIT_ASSERT( vn != 0 ); // Ask for the IP already in use @@ -587,8 +659,13 @@ public: CPPUNIT_ASSERT( bridge == "bridge0" ); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void wrong_leases() { + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); int rc, oid_0, oid_1; VirtualNetwork *vn; @@ -601,7 +678,7 @@ public: oid_0 = allocate(0); CPPUNIT_ASSERT( oid_0 != -1 ); - vn = ((VirtualNetworkPool*)pool)->get(oid_0, false); + vn = vnpool->get(oid_0, false); CPPUNIT_ASSERT( vn != 0 ); // Ask for a non-defined IP @@ -616,7 +693,7 @@ public: oid_1 = allocate(1); CPPUNIT_ASSERT( oid_1 != -1 ); - vn = ((VirtualNetworkPool*)pool)->get(oid_1, false); + vn = vnpool->get(oid_1, false); CPPUNIT_ASSERT( vn != 0 ); // Ask for an out of range IP @@ -635,6 +712,9 @@ public: CPPUNIT_ASSERT( rc == -1 ); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void overlapping_leases_ff() { // It is a different Vnet from #0: different user, name, and bridge. @@ -649,6 +729,9 @@ public: overlapping_leases(templates[0], tmpl_B); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void overlapping_leases_fr() { // Now use a ranged network, also containig the IP 130.10.0.1 @@ -662,6 +745,9 @@ public: overlapping_leases(templates[0], tmpl_B); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void overlapping_leases_rf() { // It is a different Vnet from #0: different user, name, and bridge. @@ -676,6 +762,9 @@ public: overlapping_leases(tmpl_A, templates[0]); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void overlapping_leases_rr() { // It is a different Vnet from #0: different user, name, and bridge. @@ -697,10 +786,14 @@ public: overlapping_leases(tmpl_A, tmpl_B); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ // Runs a test using the given templates void overlapping_leases(string tmpl_A, string tmpl_B) { + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); int rc, oid_0, oid_new; VirtualNetwork *vn; @@ -712,15 +805,15 @@ public: // First VNet template - ((VirtualNetworkPool*)pool)->allocate(13, tmpl_A, &oid_0); + vnpool->allocate(13, tmpl_A, &oid_0); CPPUNIT_ASSERT( oid_0 != -1 ); // Second VNet - ((VirtualNetworkPool*)pool)->allocate(45, tmpl_B, &oid_new); + vnpool->allocate(45, tmpl_B, &oid_new); CPPUNIT_ASSERT( oid_new != -1 ); // Get this second VNet - vn = ((VirtualNetworkPool*)pool)->get(oid_new, false); + vn = vnpool->get(oid_new, false); CPPUNIT_ASSERT( vn != 0 ); // Set a lease. Ask for the IP that both Vnets have @@ -742,7 +835,7 @@ public: // Now check that the first VNet has that IP available - vn = ((VirtualNetworkPool*)pool)->get(oid_0, false); + vn = vnpool->get(oid_0, false); CPPUNIT_ASSERT( vn != 0 ); // Ask the first VNet for the IP that both Vnets have @@ -759,7 +852,7 @@ public: // Get again the second VNet - vn = ((VirtualNetworkPool*)pool)->get(oid_new, false); + vn = vnpool->get(oid_new, false); CPPUNIT_ASSERT( vn != 0 ); // Ask for the IP, should be still used @@ -778,8 +871,13 @@ public: CPPUNIT_ASSERT(results.size() == 1); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void drop_leases() { + VirtualNetworkPoolFriend * vnpool = + static_cast(pool); int rc, oid; VirtualNetwork *vn; @@ -793,7 +891,7 @@ public: oid = allocate(0); CPPUNIT_ASSERT( oid != -1 ); - vn = ((VirtualNetworkPool*)pool)->get(oid, false); + vn = vnpool->get(oid, false); CPPUNIT_ASSERT( vn != 0 ); // Drop the VNet @@ -808,11 +906,14 @@ public: CPPUNIT_ASSERT(results.size() == 0); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ void public_attribute() { int oid; - VirtualNetworkPool * vnp = static_cast(pool); + VirtualNetworkPoolFriend * vnp = + static_cast(pool); VirtualNetwork * vn; string templates[] = @@ -898,11 +999,24 @@ public: //cout << endl << i << ":expected " << results[i] << " got " << vn->is_public(); - CPPUNIT_ASSERT( vn->is_public() == results[i] ); - +// ============================================================================= +// ============================================================================= +// Branch feature 203 doesn't have is_public method, but this test is +// still valid for master. +// CPPUNIT_ASSERT( vn->is_public() == results[i] ); +// ============================================================================= +// ============================================================================= i++; } } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + + void vnpool_nic_attribute() + { + } + }; /* ************************************************************************* */ From d0ded1ca82717655ddf18243a6b133c4405277e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 15 Jul 2010 13:04:58 +0200 Subject: [PATCH 61/64] feature #192: New tests for VirtualNetworkPool::nic_attribute --- src/vnm/test/VirtualNetworkPoolTest.cc | 85 ++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/vnm/test/VirtualNetworkPoolTest.cc b/src/vnm/test/VirtualNetworkPoolTest.cc index 267f2da00e..ba14a660cb 100644 --- a/src/vnm/test/VirtualNetworkPoolTest.cc +++ b/src/vnm/test/VirtualNetworkPoolTest.cc @@ -1015,6 +1015,91 @@ public: void vnpool_nic_attribute() { + VirtualNetworkPoolFriend * vnp = + static_cast(pool); + + VectorAttribute * disk; + int oid_0, oid_1; + string value; + + // --------------------------------------------------------------------- + // Allocate 2 vnets + + string template_0 = "NAME = \"Net 0\"\n" + "TYPE = FIXED\n" + "BRIDGE = br1\n" + "LEASES = [IP=130.10.0.1, MAC=50:20:20:20:20:20]"; + + string template_1 = "NAME = \"Net 1\"\n" + "TYPE = FIXED\n" + "BRIDGE = br2\n" + "LEASES = [IP=130.10.0.5, MAC=50:20:20:20:20:25]"; + + + vnp->allocate(0, template_0, &oid_0); + CPPUNIT_ASSERT( oid_0 == 0 ); + + vnp->allocate(0, template_1, &oid_1); + CPPUNIT_ASSERT( oid_1 == 1 ); + + + // Disk using network 0 + disk = new VectorAttribute("DISK"); + disk->replace("NETWORK", "Net 0"); + + ((VirtualNetworkPool*)vnp)->nic_attribute(disk, 0); + + + value = ""; + value = disk->vector_value("NETWORK"); + CPPUNIT_ASSERT( value == "Net 0" ); + + value = ""; + value = disk->vector_value("MAC"); + CPPUNIT_ASSERT( value == "50:20:20:20:20:20" ); + + value = ""; + value = disk->vector_value("BRIDGE"); + CPPUNIT_ASSERT( value == "br1" ); + + value = ""; + value = disk->vector_value("NETWORK_ID"); + CPPUNIT_ASSERT( value == "0" ); + + value = ""; + value = disk->vector_value("IP"); + CPPUNIT_ASSERT( value == "130.10.0.1" ); + + delete disk; + + + // Disk using network 1 index + disk = new VectorAttribute("DISK"); + disk->replace("NETWORK_ID", "1"); + + ((VirtualNetworkPool*)vnp)->nic_attribute(disk, 0); + + value = ""; + value = disk->vector_value("NETWORK"); + CPPUNIT_ASSERT( value == "Net 1" ); + + value = ""; + value = disk->vector_value("MAC"); + CPPUNIT_ASSERT( value == "50:20:20:20:20:25" ); + + value = ""; + value = disk->vector_value("BRIDGE"); + CPPUNIT_ASSERT( value == "br2" ); + + value = ""; + value = disk->vector_value("NETWORK_ID"); + CPPUNIT_ASSERT( value == "1" ); + + value = ""; + value = disk->vector_value("IP"); + CPPUNIT_ASSERT( value == "130.10.0.5" ); + + delete disk; } }; From b6178c1ccef17a90423f2e454f34f340c224375c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 15 Jul 2010 14:36:59 +0200 Subject: [PATCH 62/64] feature #192: Tests fixed for new VirtualMachinePool::allocate parameters (#203). --- src/vm/test/VirtualMachinePoolTest.cc | 60 ++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/vm/test/VirtualMachinePoolTest.cc b/src/vm/test/VirtualMachinePoolTest.cc index e9a61d9e4d..04ce70247e 100644 --- a/src/vm/test/VirtualMachinePoolTest.cc +++ b/src/vm/test/VirtualMachinePoolTest.cc @@ -80,6 +80,40 @@ const string xml_history_dump = const string replacement = "0000000000"; +/* ************************************************************************* */ +/* ************************************************************************* */ + +class VirtualMachinePoolFriend : public VirtualMachinePool +{ +public: + VirtualMachinePoolFriend(SqlDB * db, vector hook_mads): + VirtualMachinePool(db, hook_mads) + {}; + + + int allocate ( + int uid, + const string& stemplate, + int * oid, + bool on_hold = false) + { + VirtualMachineTemplate * vm_template; + char * error_msg = 0; + int rc; + + vm_template = new VirtualMachineTemplate; + rc = vm_template->parse(stemplate,&error_msg); + + if( rc == 0 ) + { + return VirtualMachinePool::allocate(uid, vm_template, oid, on_hold); + } + else + { + return -1; + } + }; +}; /* ************************************************************************* */ /* ************************************************************************* */ @@ -114,15 +148,15 @@ protected: { // The VM pool needs a vector containing the vm hooks vector vm_hooks; - return new VirtualMachinePool(db, vm_hooks); + return new VirtualMachinePoolFriend(db, vm_hooks); }; int allocate(int index) { int oid; - return ((VirtualMachinePool*)pool)->allocate(uids[index], - templates[index], - &oid, false); + return ((VirtualMachinePoolFriend*)pool)->allocate( uids[index], + templates[index], + &oid, false); }; void check(int index, PoolObjectSQL* obj) @@ -231,7 +265,8 @@ public: void dump() { - VirtualMachinePool * vmp = static_cast(pool); + VirtualMachinePoolFriend * vmp = + static_cast(pool); set_up_user_pool(); @@ -253,7 +288,8 @@ public: void dump_where() { - VirtualMachinePool * vmp = static_cast(pool); + VirtualMachinePoolFriend * vmp = + static_cast(pool); set_up_user_pool(); @@ -275,7 +311,8 @@ public: void dump_history() { - VirtualMachinePool * vmp = static_cast(pool); + VirtualMachinePoolFriend * vmp = + static_cast(pool); VirtualMachine* vm; string hostnames[] = {"A_hostname", "B_hostname", "C_hostname"}; @@ -374,8 +411,9 @@ public: void history() { - VirtualMachine * vm; - VirtualMachinePool * vmp = static_cast(pool); + VirtualMachine * vm; + VirtualMachinePoolFriend * vmp = + static_cast(pool); int rc, oid; @@ -395,7 +433,7 @@ public: // Add a history item vm->add_history(0, hostname, vm_dir, vmm_mad, tm_mad); - rc = vmp->update(vm); + rc = vmp->update(vm); CPPUNIT_ASSERT( rc == 0 ); rc = vmp->update_history(vm); @@ -403,7 +441,7 @@ public: vm->add_history(0, new_hostname, vm_dir, vmm_mad, tm_mad); - rc = vmp->update(vm); + rc = vmp->update(vm); CPPUNIT_ASSERT( rc == 0 ); vm->set_reason(History::USER); From db4f66742d1b426b509527344bc3a5bc8e2c0b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 15 Jul 2010 15:00:09 +0200 Subject: [PATCH 63/64] feature #201: Better error log for cluster allocate. --- src/host/ClusterPool.cc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/host/ClusterPool.cc b/src/host/ClusterPool.cc index 350dc0e01a..ff91cc98e2 100644 --- a/src/host/ClusterPool.cc +++ b/src/host/ClusterPool.cc @@ -15,6 +15,7 @@ /* -------------------------------------------------------------------------- */ #include "ClusterPool.h" +#include "NebulaLog.h" const char * ClusterPool::table = "cluster_pool"; @@ -31,10 +32,11 @@ const string ClusterPool::DEFAULT_CLUSTER_NAME = "default"; int ClusterPool::allocate(int * clid, string name, SqlDB *db) { - int rc; - + int rc; map::iterator it; + ostringstream oss; + // Return error if name already exists for(it=cluster_names.begin();it!=cluster_names.end();it++) { @@ -56,10 +58,20 @@ int ClusterPool::allocate(int * clid, string name, SqlDB *db) return *clid; -// TODO: LOG ERRORS + error_existing_name: + oss << "Could not allocate new cluster: Name \"" + << name << "\" already exists."; + + goto error_common; error_db: + oss << "Could not allocate new cluster \"" << name + << "\": Database returned error code " << rc << "."; + goto error_common; + error_common: + NebulaLog::log("CLUSTER", Log::ERROR, oss); + *clid = -1; return *clid; } From c2dab091592be24c7d697197577c37e1b5058b82 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 15 Jul 2010 16:37:58 +0200 Subject: [PATCH 64/64] feature #203: Added some more checks to tests and build options --- share/test/do_tests.sh | 6 ++++-- src/image/test/ImagePoolTest.cc | 8 +------- src/vnm/test/VirtualNetworkPoolTest.cc | 9 +-------- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/share/test/do_tests.sh b/share/test/do_tests.sh index 55b6ca0d3d..8d5c766099 100755 --- a/share/test/do_tests.sh +++ b/share/test/do_tests.sh @@ -62,8 +62,10 @@ done if [ "$MYSQL" = "yes" ] ; then TEST_ARGS="-m" + BUILD_ARGS="mysql=yes sqlite=no" else TEST_ARGS="-s" + BUILD_ARGS="mysql=no sqlite=yes" fi if [ "$LOGS" = "yes" ] ; then @@ -88,9 +90,9 @@ for i in $TESTS ; do if [ "$CLEAR" = "yes" ] ; then scons -c - rm -f callgrind.out* test.db* test.log* memgrid.out* + rm -f callgrind.out* test.db* *.log* memgrid.out* elif [ "$BUILD" = "yes" ] ; then - scons + scons $BUILD_ARGS else for j in `ls test*` ; do $CALLER ./$j $TEST_ARGS diff --git a/src/image/test/ImagePoolTest.cc b/src/image/test/ImagePoolTest.cc index 2f2198caeb..b7b097bbaa 100644 --- a/src/image/test/ImagePoolTest.cc +++ b/src/image/test/ImagePoolTest.cc @@ -864,13 +864,7 @@ public: CPPUNIT_ASSERT( img != 0 ); //cout << endl << i << " : exp. " << results[i] << " got " << img->is_public(); -// ============================================================================= -// ============================================================================= -// Branch feature 203 doesn't have is_public method, but this test is -// still valid for master. -// CPPUNIT_ASSERT( img->is_public() == results[i] ); -// ============================================================================= -// ============================================================================= + CPPUNIT_ASSERT( img->isPublic() == results[i] ); i++; } diff --git a/src/vnm/test/VirtualNetworkPoolTest.cc b/src/vnm/test/VirtualNetworkPoolTest.cc index ba14a660cb..7c3239a17e 100644 --- a/src/vnm/test/VirtualNetworkPoolTest.cc +++ b/src/vnm/test/VirtualNetworkPoolTest.cc @@ -998,14 +998,7 @@ public: CPPUNIT_ASSERT( vn != 0 ); //cout << endl << i << ":expected " << results[i] << " got " << vn->is_public(); - -// ============================================================================= -// ============================================================================= -// Branch feature 203 doesn't have is_public method, but this test is -// still valid for master. -// CPPUNIT_ASSERT( vn->is_public() == results[i] ); -// ============================================================================= -// ============================================================================= + CPPUNIT_ASSERT( vn->isPublic() == results[i] ); i++; } }