From 57308da948de7c1e6aef0fa984d7b4da5d1373d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 11 Apr 2012 14:45:46 +0200 Subject: [PATCH 1/3] Bug #834: When Users or Groups are deleted, their ACL rules are also dropped (cherry picked from commit 5a481f23d8aaeaefada65e46a8aebba04fdba586) --- include/AclManager.h | 21 ++++++++++++++ include/RequestManagerDelete.h | 18 ++++++++++++ src/acl/AclManager.cc | 51 +++++++++++++++++++++++++++++++++- src/rm/RequestManagerDelete.cc | 22 ++++++++++++--- 4 files changed, 107 insertions(+), 5 deletions(-) diff --git a/include/AclManager.h b/include/AclManager.h index 37dd9ce455..8887e4c74d 100644 --- a/include/AclManager.h +++ b/include/AclManager.h @@ -93,6 +93,20 @@ public: */ virtual int del_rule(int oid, string& error_str); + /** + * Deletes rules that apply to this user id + * + * @param uid The user id + */ + void del_uid_rules(int uid); + + /** + * Deletes rules that apply to this group id + * + * @param gid The group id + */ + void del_gid_rules(int gid); + /** * Searches what resources of type obj_type the ACL rules set allows * the given user to perform the operation. @@ -199,6 +213,13 @@ private: long long group_obj_type, multimap &tmp_rules); + /** + * Deletes all rules that match the user mask + * + * @param user_req Mask to match + */ + void del_user_matching_rules(long long user_req); + // ---------------------------------------- // Mutex synchronization // ---------------------------------------- diff --git a/include/RequestManagerDelete.h b/include/RequestManagerDelete.h index 0eaf8c6359..447b8e4027 100644 --- a/include/RequestManagerDelete.h +++ b/include/RequestManagerDelete.h @@ -180,11 +180,21 @@ public: { Nebula& nd = Nebula::instance(); pool = nd.get_gpool(); + aclm = nd.get_aclm(); + auth_object = PoolObjectSQL::GROUP; auth_op = AuthRequest::ADMIN; }; ~GroupDelete(){}; + + /* -------------------------------------------------------------------- */ + + AclManager * aclm; + + /* -------------------------------------------------------------------- */ + + int drop(int oid, PoolObjectSQL * object, string& error_msg); }; /* ------------------------------------------------------------------------- */ @@ -198,6 +208,9 @@ public: { Nebula& nd = Nebula::instance(); pool = nd.get_upool(); + gpool = nd.get_gpool(); + aclm = nd.get_aclm(); + auth_object = PoolObjectSQL::USER; auth_op = AuthRequest::ADMIN; }; @@ -206,6 +219,11 @@ public: /* -------------------------------------------------------------------- */ + GroupPool * gpool; + AclManager * aclm; + + /* -------------------------------------------------------------------- */ + int drop(int oid, PoolObjectSQL * object, string& error_msg); }; diff --git a/src/acl/AclManager.cc b/src/acl/AclManager.cc index 8ffd997c8e..0c9f9c6b59 100644 --- a/src/acl/AclManager.cc +++ b/src/acl/AclManager.cc @@ -504,7 +504,6 @@ int AclManager::del_rule(int oid, string& error_str) { found = *rule == *(it->second); - if ( !found ) { it++; @@ -546,6 +545,56 @@ int AclManager::del_rule(int oid, string& error_str) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void AclManager::del_uid_rules(int uid) +{ + long long user_req = AclRule::INDIVIDUAL_ID | uid; + + del_user_matching_rules(user_req); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AclManager::del_gid_rules(int gid) +{ + long long user_req = AclRule::GROUP_ID | gid; + + del_user_matching_rules(user_req); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AclManager::del_user_matching_rules(long long user_req) +{ + multimap::iterator it; + pair::iterator, + multimap::iterator> index; + + vector oids; + vector::iterator oid_it; + string error_str; + + lock(); + + index = acl_rules.equal_range( user_req ); + + for ( it = index.first; it != index.second; it++) + { + oids.push_back(it->second->oid); + } + + unlock(); + + for ( oid_it = oids.begin() ; oid_it < oids.end(); oid_it++ ) + { + del_rule(*oid_it, error_str); + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void AclManager::reverse_search(int uid, int gid, PoolObjectSQL::ObjectType obj_type, diff --git a/src/rm/RequestManagerDelete.cc b/src/rm/RequestManagerDelete.cc index 34ec4d0393..4a0f027127 100644 --- a/src/rm/RequestManagerDelete.cc +++ b/src/rm/RequestManagerDelete.cc @@ -192,6 +192,21 @@ int ImageDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ +int GroupDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) +{ + int rc = RequestManagerDelete::drop(oid, object, error_msg); + + if ( rc == 0 ) + { + aclm->del_gid_rules(oid); + } + + return rc; +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + int UserDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) { User * user = static_cast(object); @@ -211,10 +226,7 @@ int UserDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) if ( rc == 0 ) { - Nebula& nd = Nebula::instance(); - GroupPool * gpool = nd.get_gpool(); - - Group * group = gpool->get(group_id, true); + Group * group = gpool->get(group_id, true); if( group != 0 ) { @@ -223,6 +235,8 @@ int UserDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) group->unlock(); } + + aclm->del_uid_rules(oid); } return rc; From f5ec9483388708f5a9a149169c81c75cbb2b5b83 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 11 Apr 2012 16:56:59 +0200 Subject: [PATCH 2/3] Add XMLUtils tests --- src/oca/ruby/OpenNebula/XMLUtils.rb | 11 +++ src/oca/ruby/test/XMLUtils_spec.rb | 100 ++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/oca/ruby/test/XMLUtils_spec.rb diff --git a/src/oca/ruby/OpenNebula/XMLUtils.rb b/src/oca/ruby/OpenNebula/XMLUtils.rb index 44f1553a4b..bfe04fcc2d 100644 --- a/src/oca/ruby/OpenNebula/XMLUtils.rb +++ b/src/oca/ruby/OpenNebula/XMLUtils.rb @@ -101,6 +101,8 @@ module OpenNebula end end + # Delete an element from the xml + # xpath::_String_ xpath expression that selects the elemnts to be deleted def delete_element(xpath) if NOKOGIRI @xml.xpath(xpath.to_s).remove @@ -109,6 +111,15 @@ module OpenNebula end end + # Add a new element to the xml + # xpath::_String_ xpath xpression where the elemente will be added + # elems::_Hash_ Hash containing the pairs key-value to be included + # Examples: + # add_element('VM', 'NEW_ITEM' => 'NEW_VALUE') + # NEW_VALUE... + # + # add_element('VM/TEMPLATE', 'V1' => {'X1' => 'A1', 'Y2' => 'A2'}) + # def add_element(xpath, elems) elems.each { |key, value| if value.instance_of?(Hash) diff --git a/src/oca/ruby/test/XMLUtils_spec.rb b/src/oca/ruby/test/XMLUtils_spec.rb new file mode 100644 index 0000000000..45b9fdacf6 --- /dev/null +++ b/src/oca/ruby/test/XMLUtils_spec.rb @@ -0,0 +1,100 @@ +$: << '..' + + +require 'rexml/document' +require 'OpenNebula/XMLUtils' + + shared_examples "modifying XML" do + it "add a new element '.', 'B2' => 'bdos'" do + @xml_element.add_element('.', 'B2' => 'bdos') + + @xml_element.to_xml(true).should eql " + auno + bdos +" + end + + it "add a new element '.', 'V2' => {'CC3' => 'cctres'}" do + @xml_element.add_element('.', 'V2' => {'CC3' => 'cctres'}) + + @xml_element.to_xml(true).should eql " + auno + bdos + + cctres + +" + end + + it "add a new element 'V2', 'DD4' => 'ddcuatro'" do + @xml_element.add_element('V2', 'DD4' => 'ddcuatro') + + @xml_element.to_xml(true).should eql " + auno + bdos + + cctres + ddcuatro + +" + end + + it "delete an element 'B2'" do + @xml_element.delete_element('B2') + + @xml_element.to_xml(true).should eql " + auno + + cctres + ddcuatro + +" + end + + it "delete an element 'V2/DD4'" do + @xml_element.delete_element('V2/DD4') + + @xml_element.to_xml(true).should eql " + auno + + cctres + +" + end + + it "delete an element 'V2'" do + @xml_element.delete_element('V2') + + @xml_element.to_xml(true).should eql " + auno +" + end + end + + describe 'NOKOGIRI' do + before :all do + xml = "auno" + + bxml = OpenNebula::XMLElement.build_xml(xml, "V1") + bxml.class.to_s.should eql('Nokogiri::XML::NodeSet') + + @xml_element = OpenNebula::XMLElement.new(bxml) + end + + it_behaves_like "modifying XML" + end + + describe 'REXML' do + before :all do + OpenNebula::NOKOGIRI = false + + xml = "auno" + + bxml = OpenNebula::XMLElement.build_xml(xml, "V1") + bxml.class.to_s.should eql('REXML::Element') + + @xml_element = OpenNebula::XMLElement.new(bxml) + end + + it_behaves_like "modifying XML" + end \ No newline at end of file From a76be303127de68eafbf1b89f60d99b34fa82760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 11 Apr 2012 17:58:57 +0200 Subject: [PATCH 3/3] Feature #1096: Change authorization MAD definition to detect if there is any authZ driver enabled --- include/AuthManager.h | 17 ++++++++++++++++- share/etc/oned.conf | 4 ++-- src/authm/AuthManager.cc | 23 +++++++++++++++++++++++ src/um/UserPool.cc | 2 +- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/include/AuthManager.h b/include/AuthManager.h index 3e47f91550..0d325495eb 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -125,6 +125,16 @@ public: return _time_out; } + /** + * Returns true if there is an authorization driver enabled + * + * @return true if there is an authorization driver enabled + */ + bool is_authz_enabled() + { + return authz_enabled; + }; + private: /** * Thread id for the Transfer Manager @@ -159,7 +169,12 @@ private: /** * Generic name for the Auth driver */ - static const char * auth_driver_name; + static const char * auth_driver_name; + + /** + * True if there is an authorization driver enabled + */ + bool authz_enabled; /** * Returns a pointer to a Auth Manager driver. diff --git a/share/etc/oned.conf b/share/etc/oned.conf index e18ef4d5d4..a2f3890c3f 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -422,8 +422,8 @@ HM_MAD = [ AUTH_MAD = [ executable = "one_auth_mad", - arguments = "--authn ssh,x509,ldap,server_cipher,server_x509" -# arguments = "--authz quota --authn ssh,x509,ldap,server_cipher,server_x509" + authn = "ssh,x509,ldap,server_cipher,server_x509" +# , authz = "quota" ] SESSION_EXPIRATION_TIME = 900 diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 6201ba33e2..a772dca1dc 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -453,6 +453,29 @@ void AuthManager::load_mads(int uid) auth_conf.replace("NAME",auth_driver_name); + oss.str(""); + + string authn = auth_conf.vector_value("AUTHN"); + + if ( !authn.empty() ) + { + oss << "--authn " << authn; + } + + string authz = auth_conf.vector_value("AUTHZ"); + + if ( !authz.empty() ) + { + authz_enabled = true; + oss << " --authz " << authz; + } + else + { + authz_enabled = false; + } + + auth_conf.replace("ARGUMENTS", oss.str()); + authm_driver = new AuthManagerDriver(uid,auth_conf.value(),(uid!=0),this); rc = add(authm_driver); diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index d06e6d90d6..34bbbb17ae 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -735,7 +735,7 @@ int UserPool::authorize(AuthRequest& ar) AuthManager * authm = nd.get_authm(); int rc = -1; - if (authm == 0) + if (authm == 0 || !authm->is_authz_enabled()) { if (ar.core_authorize()) {