diff --git a/include/AddressRange.h b/include/AddressRange.h index 68846f1d43..9315e8a3ac 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -333,7 +333,7 @@ public: * @param name of the attribute * @return the value of the attribute if found, empty otherwise */ - string get_attribute(const char *name) const + string get_attribute(const string& name) const { return attr->vector_value(name); } @@ -344,7 +344,7 @@ public: * @param value of the attribute * @return 0 on success */ - int get_attribute(const char *name, int& value) const + int get_attribute(const string& name, int& value) const { return attr->vector_value(name, value); } @@ -383,7 +383,7 @@ public: void get_security_groups(set& sgs) { std::set::const_iterator it; - + for (it = security_groups.begin(); it != security_groups.end(); ++it) { sgs.insert(*it); diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 9630a9824f..c204f0e3b8 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -291,7 +291,7 @@ public: * @param value of the attribute * @param ar_id to get the attribute from */ - void get_attribute(const char * name, string& value, int ar_id) const; + void get_attribute(const string& name, string& value, int ar_id) const; /** * Gets an attribute from the Address Range, int version @@ -300,7 +300,7 @@ public: * @param ar_id to get the attribute from * @return 0 on success */ - int get_attribute(const char * name, int& value, int ar_id) const; + int get_attribute(const string& name, int& value, int ar_id) const; /** * Gets a reference to a the security group set of an AR diff --git a/include/BitMap.h b/include/BitMap.h index aba0e4b5f1..832a26e0a6 100644 --- a/include/BitMap.h +++ b/include/BitMap.h @@ -304,7 +304,7 @@ private: return -1; } - char * ezipped64 = db->escape_str(zipped->c_str()); + char * ezipped64 = db->escape_str(*zipped); if (replace) diff --git a/include/Cluster.h b/include/Cluster.h index b282646831..607760375e 100644 --- a/include/Cluster.h +++ b/include/Cluster.h @@ -94,7 +94,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -102,7 +102,16 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; + + /** + * Child classes can process the new template set with replace_template or + * append_template with this method + * @param error string describing the error if any + * @return 0 on success + * - encrypt secret attributes. + */ + int post_update_template(string& error) override; private: @@ -147,7 +156,7 @@ private: Cluster(int id, const string& name, ClusterTemplate* cl_template, const VectorAttribute& vnc_conf); - virtual ~Cluster(){}; + virtual ~Cluster() = default; // ************************************************************************* // Attributes (Private) @@ -209,7 +218,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str) + int insert(SqlDB *db, string& error_str) override { int rc; @@ -228,7 +237,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; @@ -250,7 +259,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int select(SqlDB *db) + int select(SqlDB *db) override { int rc = PoolObjectSQL::select(db); @@ -267,7 +276,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int select(SqlDB *db, const string& _name, int _uid) + int select(SqlDB *db, const string& _name, int _uid) override { int rc = PoolObjectSQL::select(db, _name, _uid); @@ -291,7 +300,7 @@ private: /** * Factory method for cluster templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new ClusterTemplate; } diff --git a/include/Datastore.h b/include/Datastore.h index 86590ae8c7..2b31509f7c 100644 --- a/include/Datastore.h +++ b/include/Datastore.h @@ -80,7 +80,7 @@ public: */ static string state_to_str(DatastoreState state) { - switch(state) + switch (state) { case READY: return "READY"; break; case DISABLED: return "DISABLED"; break; @@ -93,7 +93,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -101,7 +101,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Adds this image's ID to the set. @@ -341,7 +341,7 @@ private: DatastoreTemplate* ds_template, const set &cluster_ids); - virtual ~Datastore(); + virtual ~Datastore() = default; /** * Sets the DISK_TYPE attribute for the datastore. This function will @@ -390,14 +390,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the Datastore's data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); @@ -406,7 +406,7 @@ private: /** * Factory method for datastore templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new DatastoreTemplate; } @@ -429,9 +429,9 @@ private: * append_template with this method * @param error string describing the error if any * @return 0 on success - * - encrypt VCENTER_PASSWORD attribute. + * - encrypt secret attributes. */ - int post_update_template(string& error); + int post_update_template(string& error) override; }; #endif /*DATASTORE_H_*/ diff --git a/include/Document.h b/include/Document.h index 0f7acde352..544f9c6f6e 100644 --- a/include/Document.h +++ b/include/Document.h @@ -35,7 +35,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; // ------------------------------------------------------------------------ // Template Contents @@ -44,7 +44,7 @@ public: /** * Factory method for document templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new Template; } @@ -116,7 +116,7 @@ private: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; protected: @@ -132,7 +132,7 @@ protected: int type, Template * _template_contents); - ~Document(); + virtual ~Document() = default; // ************************************************************************* // DataBase implementation @@ -150,14 +150,14 @@ protected: * @param error_str Returns the error reason, if any * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the Document data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string err; return insert_replace(db, true, err); diff --git a/include/Group.h b/include/Group.h index fcd4605ab3..5fbf6f9c09 100644 --- a/include/Group.h +++ b/include/Group.h @@ -37,7 +37,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Function to print the Group object into a string in @@ -53,7 +53,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Adds this user's ID to the set. @@ -120,7 +120,7 @@ public: /** * Factory method for Group templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new Template; } @@ -161,7 +161,7 @@ private: obj_template = new Template; }; - virtual ~Group(){}; + virtual ~Group() = default; // ************************************************************************* // Administrators @@ -215,7 +215,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int select(SqlDB * db); + int select(SqlDB * db) override; /** * Reads the Group (identified with its OID) from the database. @@ -225,7 +225,7 @@ private: * * @return 0 on success */ - int select(SqlDB * db, const string& name, int uid); + int select(SqlDB * db, const string& name, int uid) override; /** * Reads the Group quotas from the database. @@ -239,14 +239,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int drop(SqlDB *db); + int drop(SqlDB *db) override; /** * Writes the Group in the database. * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the Group's data fields in the database. This method does @@ -254,7 +254,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); diff --git a/include/Host.h b/include/Host.h index 3bf565bd36..857c146dc3 100644 --- a/include/Host.h +++ b/include/Host.h @@ -64,7 +64,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -72,7 +72,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Checks if the host is a remote public cloud @@ -342,16 +342,16 @@ public: /** * Factory method for host templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new HostTemplate; } /** * Executed after an update operation to process the new template - * - encrypt VCENTER_PASSWORD attribute. + * - encrypt secret attributes. */ - int post_update_template(string& error); + int post_update_template(string& error) override; /** * Returns the rediscovered VMs (from poff to running) in the previous @@ -452,7 +452,7 @@ private: int cluster_id, const string& cluster_name); - virtual ~Host(); + virtual ~Host() = default; // ************************************************************************* // DataBase implementation (Private) @@ -501,7 +501,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str) + int insert(SqlDB *db, string& error_str) override { return insert_replace(db, false, error_str); }; @@ -511,7 +511,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); diff --git a/include/Image.h b/include/Image.h index 5d000c6837..9bbd91542a 100644 --- a/include/Image.h +++ b/include/Image.h @@ -146,7 +146,7 @@ public: */ static string state_to_str(ImageState state) { - switch(state) + switch (state) { case INIT: return "INIT"; break; case READY: return "READY"; break; @@ -172,7 +172,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -180,7 +180,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Returns true if the image is persistent @@ -320,7 +320,7 @@ public: /* Access Image Counters (running vms and cloning operations ) */ /* ---------------------------------------------------------------------- */ - int dec_running (int vm_id) + int dec_running(int vm_id) { if ( vm_collection.del(vm_id) == 0 ) { @@ -431,7 +431,7 @@ public: return -1; } - switch(state) + switch (state) { case USED: case CLONE: @@ -483,7 +483,7 @@ public: /** * Factory method for image templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new ImageTemplate; } @@ -726,7 +726,7 @@ protected: int umask, ImageTemplate* img_template); - virtual ~Image(); + virtual ~Image() = default; // ************************************************************************* // DataBase implementation @@ -743,14 +743,14 @@ protected: * @param db pointer to the db * @return 0 on success */ - virtual int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the Images data fields in the database. * @param db pointer to the db * @return 0 on success */ - virtual int update(SqlDB *db); + int update(SqlDB *db) override; }; #endif /*IMAGE_H_*/ diff --git a/include/MarketPlace.h b/include/MarketPlace.h index a21f88af6a..be243b8eb8 100644 --- a/include/MarketPlace.h +++ b/include/MarketPlace.h @@ -35,7 +35,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -43,7 +43,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Adds this marketplace app's ID to the set. @@ -184,7 +184,7 @@ private: int umask, MarketPlaceTemplate* mp_template); - virtual ~MarketPlace(); + virtual ~MarketPlace() = default; // ************************************************************************* // DataBase implementation (Private) @@ -229,14 +229,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the MarketPlace's data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); @@ -245,7 +245,7 @@ private: /** * Factory method for marketplace templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new MarketPlaceTemplate; } @@ -262,7 +262,7 @@ private: * @param error string describing the error if any * @return 0 on success */ - int post_update_template(string& error); + int post_update_template(string& error) override; }; #endif /*MARKETPLACE_H*/ diff --git a/include/MarketPlaceApp.h b/include/MarketPlaceApp.h index bbc24c0af9..6a5a1009a0 100644 --- a/include/MarketPlaceApp.h +++ b/include/MarketPlaceApp.h @@ -81,7 +81,7 @@ public: */ static string state_to_str(State state) { - switch(state) + switch (state) { case INIT: return "INIT"; break; case READY: return "READY"; break; @@ -132,7 +132,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(std::string& xml) const; + string& to_xml(std::string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -140,7 +140,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const std::string &xml_str); + int from_xml(const std::string &xml_str) override; /** * Rebuilds the object from base64 encoded template representation @@ -355,7 +355,7 @@ private: int umask, MarketPlaceAppTemplate* app_template); - virtual ~MarketPlaceApp(); + virtual ~MarketPlaceApp() = default; // ************************************************************************* // DataBase implementation (Private) @@ -400,14 +400,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, std::string& error_str); + int insert(SqlDB *db, std::string& error_str) override; /** * Writes/updates the MarketPlace's data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { std::string error_str; return insert_replace(db, true, error_str); @@ -419,12 +419,12 @@ private: * @param error string describing the error if any * @return 0 on success */ - int post_update_template(std::string& error); + int post_update_template(std::string& error) override; /** * Factory method for marketplace app templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new MarketPlaceAppTemplate; } diff --git a/include/NebulaUtil.h b/include/NebulaUtil.h index 2ef83019f4..a1b5a7948c 100644 --- a/include/NebulaUtil.h +++ b/include/NebulaUtil.h @@ -23,7 +23,6 @@ #include #include #include -#include #include @@ -61,7 +60,7 @@ namespace one_util /** * Base 64 encoding * @param in the string to encoded - * @return a pointer to the encoded string (must be freed) or 0 in case of + * @return a pointer to the encoded string (must be freed) or nullptr in case of * error */ std::string * base64_encode(const std::string& in); @@ -69,7 +68,7 @@ namespace one_util /** * Base 64 decoding * @param in the string to decode - * @return a pointer to the decoded string (must be freed) or 0 in case of + * @return a pointer to the decoded string (must be freed) or nullptr in case of * error */ std::string * base64_decode(const std::string& in); @@ -78,10 +77,19 @@ namespace one_util * AES256 encryption * @param in the string to encrypt * @param password to encrypt data - * @return a pointer to the encrypted string (must be freed) or 0 in case of + * @return a pointer to the encrypted string (must be freed) or nullptr in case of * error */ - std::string * aes256cbc_encrypt(const std::string& in, const std::string password); + std::string * aes256cbc_encrypt(const std::string& in, const std::string& password); + + /** + * AES256 decryption + * @param in the base64 string to decrypt + * @param password to decrypt data + * @return a pointer to the decrypted string (must be freed) or nullptr in case of + * error + */ + std::string * aes256cbc_decrypt(const std::string& in, const std::string& password); /** * Creates a random number, using time(0) as seed, and performs an sha1 hash @@ -180,7 +188,7 @@ namespace one_util } std::vector split(const std::string& st, char delim, - bool clean_empty=true); + bool clean_empty = true); /** * Splits a string, using the given delimiter @@ -231,7 +239,7 @@ namespace one_util { std::ostringstream oss; - for(Iterator it = first; it != last; it++) + for (Iterator it = first; it != last; it++) { if (it != first) { @@ -380,6 +388,6 @@ namespace one_util static std::vector vmutex; }; -}; +} // namespace one_util #endif /* _NEBULA_UTIL_H_ */ diff --git a/include/ObjectSQL.h b/include/ObjectSQL.h index 540fc9db4e..561d6c716c 100644 --- a/include/ObjectSQL.h +++ b/include/ObjectSQL.h @@ -30,9 +30,9 @@ class ObjectSQL : public Callbackable { public: - ObjectSQL(){}; + ObjectSQL() = default; - virtual ~ObjectSQL(){}; + virtual ~ObjectSQL() = default; protected: /** diff --git a/include/ObjectXML.h b/include/ObjectXML.h index 5d453072dc..441faaa912 100644 --- a/include/ObjectXML.h +++ b/include/ObjectXML.h @@ -37,7 +37,7 @@ public: // ---------------------- Constructors ------------------------------------ - ObjectXML():xml(0),ctx(0){}; + ObjectXML():xml(0), ctx(0) {} /** * Constructs an object using a XML document @@ -65,7 +65,7 @@ public: xmlNodePtr cur; xmlChar * str_ptr; - obj=xmlXPathEvalExpression(reinterpret_cast(expr),ctx); + obj = xmlXPathEvalExpression(reinterpret_cast(expr),ctx); if (obj == 0) { @@ -79,7 +79,7 @@ public: break; case XPATH_NODESET: - for(int i = 0; i < obj->nodesetval->nodeNr ; ++i) + for (int i = 0; i < obj->nodesetval->nodeNr ; ++i) { cur = obj->nodesetval->nodeTab[i]; diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 34a84d162d..f5c955e199 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -41,8 +41,11 @@ class PoolObjectSQL : public ObjectSQL, public ObjectXML public: /* ---------------------------------------------------------------------- */ /* Class Constructors & Constants */ + /* Initialized during the startup phase by Nebula::start() */ /* ---------------------------------------------------------------------- */ + static const set ENCRYPTED_ATTRIBUTES; + /** * OpenNebula objects. This definitions are used by other core components * like the AuthZ/AuthN module @@ -88,50 +91,50 @@ public: { switch (ob) { - case VM: return "VM" ; break; - case HOST: return "HOST" ; break; - case NET: return "NET" ; break; - case IMAGE: return "IMAGE" ; break; - case USER: return "USER" ; break; - case TEMPLATE: return "TEMPLATE" ; break; - case GROUP: return "GROUP" ; break; - case ACL: return "ACL" ; break; - case DATASTORE: return "DATASTORE" ; break; - case CLUSTER: return "CLUSTER" ; break; - case DOCUMENT: return "DOCUMENT" ; break; - case ZONE: return "ZONE" ; break; - case SECGROUP: return "SECGROUP" ; break; - case VDC: return "VDC" ; break; - case VROUTER: return "VROUTER" ; break; - case MARKETPLACE: return "MARKETPLACE" ; break; - case MARKETPLACEAPP: return "MARKETPLACEAPP" ; break; - case VMGROUP: return "VMGROUP" ; break; + case VM: return "VM"; break; + case HOST: return "HOST"; break; + case NET: return "NET"; break; + case IMAGE: return "IMAGE"; break; + case USER: return "USER"; break; + case TEMPLATE: return "TEMPLATE"; break; + case GROUP: return "GROUP"; break; + case ACL: return "ACL"; break; + case DATASTORE: return "DATASTORE"; break; + case CLUSTER: return "CLUSTER"; break; + case DOCUMENT: return "DOCUMENT"; break; + case ZONE: return "ZONE"; break; + case SECGROUP: return "SECGROUP"; break; + case VDC: return "VDC"; break; + case VROUTER: return "VROUTER"; break; + case MARKETPLACE: return "MARKETPLACE"; break; + case MARKETPLACEAPP: return "MARKETPLACEAPP"; break; + case VMGROUP: return "VMGROUP"; break; case VNTEMPLATE: return "VNTEMPLATE"; break; default: return ""; } }; - static ObjectType str_to_type(string type) + static ObjectType str_to_type(const string& type) { - if ( type == "VM" ) return VM ; - else if ( type == "HOST" ) return HOST ; - else if ( type == "NET" ) return NET ; - else if ( type == "IMAGE" ) return IMAGE ; - else if ( type == "USER" ) return USER ; - else if ( type == "TEMPLATE" ) return TEMPLATE ; - else if ( type == "GROUP" ) return GROUP ; - else if ( type == "ACL" ) return ACL ; - else if ( type == "DATASTORE" ) return DATASTORE ; - else if ( type == "CLUSTER" ) return CLUSTER ; - else if ( type == "DOCUMENT" ) return DOCUMENT ; - else if ( type == "ZONE" ) return ZONE ; - else if ( type == "SECGROUP" ) return SECGROUP ; - else if ( type == "VDC" ) return VDC ; - else if ( type == "VROUTER" ) return VROUTER ; - else if ( type == "MARKETPLACE" ) return MARKETPLACE ; - else if ( type == "MARKETPLACEAPP" ) return MARKETPLACEAPP ; - else if ( type == "VMGROUP" ) return VMGROUP ; - else if ( type == "VNTEMPLATE" ) return VNTEMPLATE ; + if ( type == "VM" ) return VM; + else if ( type == "HOST" ) return HOST; + else if ( type == "NET" ) return NET; + else if ( type == "IMAGE" ) return IMAGE; + else if ( type == "USER" ) return USER; + else if ( type == "TEMPLATE" ) return TEMPLATE; + else if ( type == "GROUP" ) return GROUP; + else if ( type == "ACL" ) return ACL; + else if ( type == "DATASTORE" ) return DATASTORE; + else if ( type == "CLUSTER" ) return CLUSTER; + else if ( type == "DOCUMENT" ) return DOCUMENT; + else if ( type == "ZONE" ) return ZONE; + else if ( type == "SECGROUP" ) return SECGROUP; + else if ( type == "VDC" ) return VDC; + else if ( type == "VROUTER" ) return VROUTER; + else if ( type == "MARKETPLACE" ) return MARKETPLACE; + else if ( type == "MARKETPLACEAPP" ) return MARKETPLACEAPP; + else if ( type == "VMGROUP" ) return VMGROUP; + else if ( type == "VNTEMPLATE" ) return VNTEMPLATE; else return NONE; }; @@ -139,10 +142,10 @@ public: { switch (ob) { - case ST_NONE: return "NONE" ; break; - case ST_USE: return "USE" ; break; - case ST_MANAGE: return "MANAGE" ; break; - case ST_ADMIN: return "ADMIN" ; break; + case ST_NONE: return "NONE"; break; + case ST_USE: return "USE"; break; + case ST_MANAGE: return "MANAGE"; break; + case ST_ADMIN: return "ADMIN"; break; default: return ""; } }; @@ -379,15 +382,15 @@ public: * @return the number of values */ template - int get_template_attribute(const char * name, vector& values) const + int get_template_attribute(const string& name, vector& values) const { - return obj_template->get(name,values); + return obj_template->get(name, values); }; template - int get_template_attribute(const char * name, vector& values) const + int get_template_attribute(const string& name, vector& values) const { - return obj_template->get(name,values); + return obj_template->get(name, values); }; /** @@ -400,16 +403,10 @@ public: * @return true if the attribute was found and is a valid type for the * target value */ - template - bool get_template_attribute(const char * name, T& value) const - { - return obj_template->get(name,value); - } - template bool get_template_attribute(const string& name, T& value) const { - return obj_template->get(name,value); + return obj_template->get(name, value); } /** @@ -420,9 +417,9 @@ public: * @return the number of attributes erased */ template - int erase_template_attribute(const char * name, T& value) + int erase_template_attribute(const string& name, T& value) { - obj_template->get(name,value); + obj_template->get(name, value); return obj_template->erase(name); } @@ -568,6 +565,14 @@ public: return locked; } + /** + * Decrypt all secret attributes + */ + virtual void decrypt_all_secrets() + { + decrypt_all_secrets(obj_template); + } + protected: /** @@ -592,7 +597,7 @@ protected: * @param db pointer to the db * @return 0 on success */ - virtual int select(SqlDB *db); + int select(SqlDB *db) override; /** * Reads the PoolObjectSQL (identified by its OID) from the database. @@ -627,7 +632,7 @@ protected: * @param db pointer to the db * @return 0 on success */ - virtual int drop(SqlDB *db); + int drop(SqlDB *db) override; /** * Function to output a pool object into a stream in XML format @@ -720,6 +725,33 @@ protected: */ int lock_db_from_xml(); + /** + * Crypt string using aes256cbc, use ONE_KEY as key + * If key doesn't exists out = in + * @param in plain text + * @param out crypted text encoded as base64 + */ + static void encrypt(const std::string& in, std::string& out); + + /** + * Decrypt input text encoded as base64, using ONE_KEY as key + * If key doesn't exists, + * @param in base64 text crypted by aes256cbc + * @param out plain text, if decryption succesfull. + * @return true, if text was decrypted, false otherwise + */ + static bool decrypt(const std::string& in, std::string& out); + + /** + * Encrypt all secret attributes + */ + void encrypt_all_secrets(Template *tmpl); + + /** + * Decrypt all secret attributes + */ + void decrypt_all_secrets(Template *tmpl); + /** * The object's unique ID */ diff --git a/include/Request.h b/include/Request.h index aad5915c9b..e8b6db586d 100644 --- a/include/Request.h +++ b/include/Request.h @@ -224,7 +224,7 @@ protected: leader_only = true; }; - virtual ~Request(){}; + virtual ~Request() = default; /* ---------------------------------------------------------------------- */ /* Methods to execute the request when received at the server */ diff --git a/include/RequestManagerInfo.h b/include/RequestManagerInfo.h index e29ecf0613..61a5ccd7dd 100644 --- a/include/RequestManagerInfo.h +++ b/include/RequestManagerInfo.h @@ -38,8 +38,6 @@ protected: leader_only = false; }; - ~RequestManagerInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute(xmlrpc_c::paramList const& _paramList, @@ -69,8 +67,6 @@ public: auth_object = PoolObjectSQL::VM; }; - ~VirtualMachineInfo(){}; - /* -------------------------------------------------------------------- */ void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str) override @@ -94,8 +90,6 @@ public: auth_object = PoolObjectSQL::TEMPLATE; }; - ~TemplateInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute(xmlrpc_c::paramList const& _paramList, @@ -118,8 +112,6 @@ public: auth_object = PoolObjectSQL::NET; }; - ~VirtualNetworkInfo(){}; - /* -------------------------------------------------------------------- */ void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str) override; @@ -141,8 +133,6 @@ public: auth_object = PoolObjectSQL::VNTEMPLATE; }; - ~VirtualNetworkTemplateInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute(xmlrpc_c::paramList const& _paramList, @@ -165,7 +155,6 @@ public: auth_object = PoolObjectSQL::IMAGE; }; - ~ImageInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -182,8 +171,6 @@ public: pool = nd.get_hpool(); auth_object = PoolObjectSQL::HOST; }; - - ~HostInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -201,8 +188,6 @@ public: auth_object = PoolObjectSQL::GROUP; }; - ~GroupInfo(){}; - /* -------------------------------------------------------------------- */ void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str) override @@ -226,8 +211,6 @@ public: auth_object = PoolObjectSQL::USER; }; - ~UserInfo(){}; - /* -------------------------------------------------------------------- */ void to_xml(RequestAttributes& att, PoolObjectSQL * object, string& str) override @@ -250,8 +233,6 @@ public: pool = nd.get_dspool(); auth_object = PoolObjectSQL::DATASTORE; }; - - ~DatastoreInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -268,8 +249,6 @@ public: pool = nd.get_clpool(); auth_object = PoolObjectSQL::CLUSTER; }; - - ~ClusterInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -286,8 +265,6 @@ public: pool = nd.get_docpool(); auth_object = PoolObjectSQL::DOCUMENT; }; - - ~DocumentInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -304,8 +281,6 @@ public: pool = nd.get_zonepool(); auth_object = PoolObjectSQL::ZONE; }; - - ~ZoneInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -322,8 +297,6 @@ public: pool = nd.get_secgrouppool(); auth_object = PoolObjectSQL::SECGROUP; }; - - ~SecurityGroupInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -340,8 +313,6 @@ public: pool = nd.get_vdcpool(); auth_object = PoolObjectSQL::VDC; }; - - ~VdcInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -358,8 +329,6 @@ public: pool = nd.get_vrouterpool(); auth_object = PoolObjectSQL::VROUTER; }; - - ~VirtualRouterInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -376,8 +345,6 @@ public: pool = nd.get_marketpool(); auth_object = PoolObjectSQL::MARKETPLACE; }; - - ~MarketPlaceInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -394,8 +361,6 @@ public: pool = nd.get_apppool(); auth_object = PoolObjectSQL::MARKETPLACEAPP; }; - - ~MarketPlaceAppInfo(){}; }; /* -------------------------------------------------------------------------- */ @@ -412,8 +377,6 @@ public: pool = nd.get_vmgrouppool(); auth_object = PoolObjectSQL::VMGROUP; }; - - ~VMGroupInfo(){}; }; #endif diff --git a/include/RequestManagerPoolInfoFilter.h b/include/RequestManagerPoolInfoFilter.h index d4ba612b74..034bab9b7b 100644 --- a/include/RequestManagerPoolInfoFilter.h +++ b/include/RequestManagerPoolInfoFilter.h @@ -73,8 +73,6 @@ protected: extended = false; }; - ~RequestManagerPoolInfoFilter(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -137,8 +135,6 @@ public: auth_object = PoolObjectSQL::VM; }; - ~VirtualMachinePoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -160,7 +156,6 @@ public: extended = true; }; - ~VirtualMachinePoolInfoExtended(){}; }; /* ------------------------------------------------------------------------- */ @@ -180,8 +175,6 @@ public: auth_object = PoolObjectSQL::VM; }; - ~VirtualMachinePoolAccounting(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -205,8 +198,6 @@ public: auth_object = PoolObjectSQL::VM; }; - ~VirtualMachinePoolShowback(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -230,8 +221,6 @@ public: auth_object = PoolObjectSQL::VM; }; - ~VirtualMachinePoolMonitoring(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -253,8 +242,6 @@ public: pool = nd.get_tpool(); auth_object = PoolObjectSQL::TEMPLATE; }; - - ~TemplatePoolInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -273,8 +260,6 @@ public: auth_object = PoolObjectSQL::NET; }; - ~VirtualNetworkPoolInfo(){}; - void request_execute( xmlrpc_c::paramList const& paramList, RequestAttributes& att) override; }; @@ -294,9 +279,6 @@ public: pool = nd.get_vntpool(); auth_object = PoolObjectSQL::VNTEMPLATE; }; - - ~VirtualNetworkTemplatePoolInfo(){}; - }; /* ------------------------------------------------------------------------- */ @@ -314,8 +296,6 @@ public: pool = nd.get_ipool(); auth_object = PoolObjectSQL::IMAGE; }; - - ~ImagePoolInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -334,8 +314,6 @@ public: auth_object = PoolObjectSQL::HOST; }; - ~HostPoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -359,8 +337,6 @@ public: auth_object = PoolObjectSQL::HOST; }; - ~HostPoolMonitoring(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -383,8 +359,6 @@ public: auth_object = PoolObjectSQL::GROUP; }; - ~GroupPoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -407,8 +381,6 @@ public: auth_object = PoolObjectSQL::USER; }; - ~UserPoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -431,8 +403,6 @@ public: auth_object = PoolObjectSQL::DATASTORE; }; - ~DatastorePoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -455,8 +425,6 @@ public: auth_object = PoolObjectSQL::CLUSTER; }; - ~ClusterPoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -479,8 +447,6 @@ public: auth_object = PoolObjectSQL::DOCUMENT; }; - ~DocumentPoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -503,8 +469,6 @@ public: auth_object = PoolObjectSQL::ZONE; }; - ~ZonePoolInfo(){}; - /* -------------------------------------------------------------------- */ void request_execute( @@ -526,8 +490,6 @@ public: pool = nd.get_secgrouppool(); auth_object = PoolObjectSQL::SECGROUP; }; - - ~SecurityGroupPoolInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -546,8 +508,6 @@ public: auth_object = PoolObjectSQL::VDC; }; - ~VdcPoolInfo(){}; - void request_execute(xmlrpc_c::paramList const& pl, RequestAttributes& att) override; }; @@ -566,8 +526,6 @@ public: pool = nd.get_vrouterpool(); auth_object = PoolObjectSQL::VROUTER; }; - - ~VirtualRouterPoolInfo(){}; }; /* -------------------------------------------------------------------------- */ @@ -586,8 +544,6 @@ public: auth_object = PoolObjectSQL::MARKETPLACE; }; - ~MarketPlacePoolInfo(){}; - void request_execute(xmlrpc_c::paramList const& pl, RequestAttributes& att) override; }; @@ -606,8 +562,6 @@ public: pool = nd.get_apppool(); auth_object = PoolObjectSQL::MARKETPLACEAPP; }; - - ~MarketPlaceAppPoolInfo(){}; }; /* ------------------------------------------------------------------------- */ @@ -625,8 +579,6 @@ public: pool = nd.get_vmgrouppool(); auth_object = PoolObjectSQL::VMGROUP; }; - - ~VMGroupPoolInfo(){}; }; #endif diff --git a/include/SecurityGroup.h b/include/SecurityGroup.h index 031aab0ad6..c03ac452cb 100644 --- a/include/SecurityGroup.h +++ b/include/SecurityGroup.h @@ -34,7 +34,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -42,7 +42,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Returns a copy of the Template @@ -173,7 +173,7 @@ private: int _umask, Template* sgroup_template); - ~SecurityGroup(); + virtual ~SecurityGroup() = default; /** * Check that a rule is valid @@ -188,7 +188,7 @@ private: * @param error string describing the error if any * @return 0 on success */ - int post_update_template(string& error); + int post_update_template(string& error) override; // ************************************************************************* // DataBase implementation (Private) @@ -225,14 +225,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the SecurityGroup's data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); @@ -241,7 +241,7 @@ private: /** * Factory method for SecurityGroup templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new Template; } diff --git a/include/Template.h b/include/Template.h index d87a26528a..05844e7ab0 100644 --- a/include/Template.h +++ b/include/Template.h @@ -47,7 +47,7 @@ public: const char * _xml_root = "TEMPLATE"): replace_mode(_replace_mode), separator(_separator), - xml_root(_xml_root){}; + xml_root(_xml_root){} Template(const Template& t) { @@ -152,7 +152,7 @@ public: * @param str_tempalte string that hold the template * @param delim to separate attributes */ - void marshall(string &str, const char delim='\n'); + void marshall(string &str, const char delim = '\n'); /** * Writes the template in a simple xml string: @@ -321,7 +321,7 @@ public: return __get(n, v); } - inline virtual int get( const string& n, vector& v) + inline virtual int get(const string& n, vector& v) { return __get(n, v); } @@ -331,7 +331,7 @@ public: return __get(n, s); } - inline virtual int get( const string& n, vector& s) + inline virtual int get(const string& n, vector& s) { return __get(n, s); } @@ -624,7 +624,7 @@ private: { typename vector::iterator it; - for(it = values.begin(); it != values.end(); it++ ) + for (it = values.begin(); it != values.end(); it++ ) { set(*it); } diff --git a/include/User.h b/include/User.h index 4720c45549..ffee8aa897 100644 --- a/include/User.h +++ b/include/User.h @@ -52,7 +52,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Function to print the User object into a string in @@ -154,7 +154,7 @@ public: /** * Factory method for image templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new UserTemplate; } @@ -207,7 +207,7 @@ public: */ int del_group(int group_id) { - if( group_id == gid ) + if (group_id == gid) { return -2; } @@ -317,7 +317,7 @@ private: * @param db pointer to the db * @return 0 on success */ - int select(SqlDB * db); + int select(SqlDB * db) override; /** * Reads the User (identified with its OID) from the database. @@ -327,14 +327,14 @@ private: * * @return 0 on success */ - int select(SqlDB * db, const string& name, int uid); + int select(SqlDB * db, const string& name, int uid) override; /** * Drops the user from the database * @param db pointer to the db * @return 0 on success */ - int drop(SqlDB *db); + int drop(SqlDB *db) override; /** * Rebuilds the object from an xml formatted string @@ -342,7 +342,7 @@ private: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Function to print the User object into a string in @@ -377,7 +377,7 @@ protected: obj_template = new UserTemplate; }; - virtual ~User(){}; + virtual ~User() = default; // ************************************************************************* // DataBase implementation @@ -394,7 +394,7 @@ protected: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the User data fields in the database. This method does @@ -402,7 +402,7 @@ protected: * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); diff --git a/include/VMGroup.h b/include/VMGroup.h index 3d48b600af..03a018cb0b 100644 --- a/include/VMGroup.h +++ b/include/VMGroup.h @@ -56,14 +56,14 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string * @param xml_str The xml-formatted string * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Returns a copy of the Template @@ -113,7 +113,7 @@ private: VMGroup(int _uid, int _gid, const string& _uname, const string& _gname, int _umask, Template * group_template); - ~VMGroup(); + virtual ~VMGroup() = default; // ------------------------------------------------------------------------- // Role Management @@ -174,14 +174,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the VMGroup's data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); @@ -192,12 +192,12 @@ private: * @param error string describing the error if any * @return 0 on success */ - int post_update_template(string& error); + int post_update_template(string& error) override; /** * Factory method for VMGroup templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new Template; } diff --git a/include/VMTemplate.h b/include/VMTemplate.h index 537028756f..a54f3e8f9f 100644 --- a/include/VMTemplate.h +++ b/include/VMTemplate.h @@ -35,7 +35,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** @@ -55,7 +55,7 @@ public: /** * Factory method for virtual machine templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new VirtualMachineTemplate; } @@ -146,7 +146,7 @@ private: * @param error Returns the error reason, if any * @return 0 one success */ - int post_update_template(string& error); + int post_update_template(string& error) override; /** * Bootstraps the database table(s) associated to the VMTemplate @@ -165,7 +165,7 @@ private: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * This method removes sched_action DONE/MESSAGE attributes @@ -185,7 +185,7 @@ protected: int umask, VirtualMachineTemplate * _template_contents); - ~VMTemplate(); + virtual ~VMTemplate() = default; // ************************************************************************* // DataBase implementation @@ -203,14 +203,14 @@ protected: * @param error_str Returns the error reason, if any * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the VMTemplate data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string err; return insert_replace(db, true, err); diff --git a/include/VNTemplate.h b/include/VNTemplate.h index 6f243ef69d..ff3f70987b 100644 --- a/include/VNTemplate.h +++ b/include/VNTemplate.h @@ -35,7 +35,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; // ------------------------------------------------------------------------ // Template Contents @@ -44,7 +44,7 @@ public: /** * Factory method for virtual machine templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new VirtualNetworkTemplate; } @@ -59,6 +59,15 @@ public: *(static_cast(obj_template))); }; + /** + * Child classes can process the new template set with replace_template or + * append_template with this method + * @param error string describing the error if any + * @return 0 on success + * - encrypt secret attributes. + */ + int post_update_template(string& error) override; + private: // ------------------------------------------------------------------------- // Friends @@ -105,7 +114,7 @@ private: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; protected: @@ -120,7 +129,7 @@ protected: int umask, VirtualNetworkTemplate * _template_contents); - ~VNTemplate(); + virtual ~VNTemplate() = default; // ************************************************************************* // DataBase implementation @@ -138,14 +147,14 @@ protected: * @param error_str Returns the error reason, if any * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the VNTemplate data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string err; return insert_replace(db, true, err); diff --git a/include/Vdc.h b/include/Vdc.h index 984e62427e..21fb1dac13 100644 --- a/include/Vdc.h +++ b/include/Vdc.h @@ -162,7 +162,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -170,7 +170,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Adds a Group to the VDC. ACL Rules are updated only for this group. @@ -324,7 +324,7 @@ private: Vdc(int id, Template* vdc_template); - ~Vdc(); + virtual ~Vdc() = default; // ************************************************************************* // Attributes (Private) @@ -372,14 +372,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the Vdc's data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); @@ -390,12 +390,12 @@ private: * @param db pointer to the db * @return 0 on success */ - int drop(SqlDB *db); + int drop(SqlDB *db) override; /** * Factory method for Vdc templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new Template; } diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index c01dd2a32f..209507d6ff 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -870,7 +870,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const + string& to_xml(string& xml) const override { return to_xml_extended(xml, 1); } @@ -900,12 +900,12 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Factory method for virtual machine templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new VirtualMachineTemplate; } @@ -929,7 +929,7 @@ public: * @return 0 on success */ int replace_template(const string& tmpl_str, bool keep_restricted, - string& error); + string& error) override; /** * Append new attributes to the *user template*. @@ -940,23 +940,23 @@ public: * @return 0 on success */ int append_template(const string& tmpl_str, bool keep_restricted, - string& error); + string& error) override; /** * This function gets an attribute from the user template * @param name of the attribute * @param value of the attribute */ - void get_user_template_attribute(const char * name, string& value) const + void get_user_template_attribute(const string& name, string& value) const { - user_obj_template->get(name,value); + user_obj_template->get(name, value); } /** * Sets an error message with timestamp in the template * @param message Message string */ - void set_template_error_message(const string& message); + void set_template_error_message(const string& message) override; /** * Sets an error message with timestamp in the template @@ -968,7 +968,7 @@ public: /** * Deletes the error message from the template */ - void clear_template_error_message(); + void clear_template_error_message() override; /** * Sets an error message with timestamp in the template (ERROR_MONITOR) @@ -1674,6 +1674,15 @@ public: */ int check_tm_mad_disks(const string& tm_mad, string& error); + /** + * Decrypt all secret attributes + */ + void decrypt_all_secrets() override + { + PoolObjectSQL::decrypt_all_secrets(obj_template); + PoolObjectSQL::decrypt_all_secrets(user_obj_template); + } + private: // ------------------------------------------------------------------------- @@ -2183,6 +2192,15 @@ private: */ int parse_sched_action(string& error_str); + /** + * Child classes can process the new template set with replace_template or + * append_template with this method + * @param error string describing the error if any + * @return 0 on success + * - encrypt secret attributes. + */ + int post_update_template(string& error) override; + protected: //************************************************************************** @@ -2226,21 +2244,21 @@ protected: * @param db pointer to the db * @return 0 on success */ - int select(SqlDB * db); + int select(SqlDB * db) override; /** * Writes the Virtual Machine and its associated template in the database. * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB * db, string& error_str); + int insert(SqlDB * db, string& error_str) override; /** * Writes/updates the Virtual Machine data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB * db) + int update(SqlDB * db) override { string error_str; return insert_replace(db, true, error_str); @@ -2251,7 +2269,7 @@ protected: * @param db pointer to the db * @return -1 */ - int drop(SqlDB * db) + int drop(SqlDB * db) override { NebulaLog::log("ONE",Log::ERROR, "VM Drop not implemented!"); return -1; diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index e8a5bcf300..bfcc0e094e 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -187,8 +187,8 @@ public: * @param error_str describing the error * @return 0 on success -1 otherwise */ - static int parse_phydev_vlans(const Template* tmpl, const string& vn_mad, const string& phydev, - const string& bridge, const bool auto_id, const string& vlan_id, + static int parse_phydev_vlans(const Template* tmpl, const string& vn_mad, const string& phydev, + const string& bridge, const bool auto_id, const string& vlan_id, const bool auto_outer, const string& outer_id, string& estr); // ************************************************************************* @@ -198,7 +198,7 @@ public: /** * Factory method for virtual network templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new VirtualNetworkTemplate; } @@ -209,7 +209,7 @@ public: * reservations. * @param auths to be filled */ - void get_permissions(PoolObjectAuth& auths); + void get_permissions(PoolObjectAuth& auths) override; // ************************************************************************* // Address Range management interface @@ -497,7 +497,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Function to print the VirtualNetwork object into a string in @@ -523,13 +523,13 @@ public: * not a single attribute * @param ar_id of the address attribute. */ - void get_template_attribute(const char * name, string& value, int ar_id) const; + void get_template_attribute(const string& name, string& value, int ar_id) const; /** * int version of get_template_attribute * @return 0 on success */ - int get_template_attribute(const char * name, int& value, int ar_id) const; + int get_template_attribute(const string& name, int& value, int ar_id) const; /** * Adds the security group of the VNet and its ARs to the given set @@ -759,14 +759,14 @@ private: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Updates the BRIDGE, PHYDEV, and VLAN_ID attributes. * @param error string describing the error if any * @return 0 on success */ - int post_update_template(string& error); + int post_update_template(string& error) override; //************************************************************************** // Constructor @@ -781,7 +781,7 @@ private: const set &_cluster_ids, VirtualNetworkTemplate * _vn_template = 0); - ~VirtualNetwork(); + virtual ~VirtualNetwork() = default; // ************************************************************************* // DataBase implementation @@ -798,14 +798,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB * db, string& error_str); + int insert(SqlDB * db, string& error_str) override; /** * Writes/updates the Virtual Network data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB * db) + int update(SqlDB * db) override { string error_str; return insert_replace(db, true, error_str); diff --git a/include/VirtualRouter.h b/include/VirtualRouter.h index 8591af7931..002b528b17 100644 --- a/include/VirtualRouter.h +++ b/include/VirtualRouter.h @@ -41,7 +41,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; int add_vmid(int vmid) { @@ -72,7 +72,7 @@ public: /** * Factory method for VirtualRouter templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new Template; } @@ -97,7 +97,7 @@ public: * @param error string describing the error if any * @return 0 on success */ - int replace_template(const string& tmpl_str, bool keep_restricted, string& error); + int replace_template(const string& tmpl_str, bool keep_restricted, string& error) override; /** * Append new attributes to this object's template. Object should be updated @@ -108,7 +108,7 @@ public: * @param error string describing the error if any * @return 0 on success */ - int append_template(const string& tmpl_str, bool keep_restricted, string& error); + int append_template(const string& tmpl_str, bool keep_restricted, string& error) override; /** * Set the template ID to instantiate new VMs @@ -228,7 +228,7 @@ private: int umask, Template * _template_contents); - ~VirtualRouter(); + virtual ~VirtualRouter() = default; // ************************************************************************* // DataBase implementation @@ -246,21 +246,21 @@ private: * @param error_str Returns the error reason, if any * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Drops object from the database * @param db pointer to the db * @return 0 on success */ - virtual int drop(SqlDB *db); + virtual int drop(SqlDB *db) override; /** * Writes/updates the VirtualRouter data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string err; return insert_replace(db, true, err); diff --git a/include/Zone.h b/include/Zone.h index 7b7043c88d..2ae6e6bb88 100644 --- a/include/Zone.h +++ b/include/Zone.h @@ -36,7 +36,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + string& to_xml(string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -44,7 +44,7 @@ public: * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str); + int from_xml(const string &xml_str) override; /** * Add servers to this zone @@ -96,7 +96,7 @@ private: // ------------------------------------------------------------------------- Zone(int id, Template* zone_template); - ~Zone(); + virtual ~Zone(); // ------------------------------------------------------------------------- // Zone servers @@ -139,14 +139,14 @@ private: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str); + int insert(SqlDB *db, string& error_str) override; /** * Writes/updates the Zone's data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) + int update(SqlDB *db) override { string error_str; return insert_replace(db, true, error_str); @@ -155,7 +155,7 @@ private: /** * Factory method for Zone templates */ - Template * get_new_template() const + Template * get_new_template() const override { return new Template; } @@ -166,7 +166,7 @@ private: * @param error string describing the error if any * @return 0 on success */ - int post_update_template(string& error); + int post_update_template(string& error) override; }; #endif /*ZONE_H_*/ diff --git a/share/etc/oned.conf b/share/etc/oned.conf index c89c723a95..34bff7c34d 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -60,8 +60,8 @@ # std to use the default log stream (stderr) to use with systemd # debug_level: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG # -# VM_SUBMIT_ON_HOLD: Forces VMs to be created on hold state instead of pending. -# Values: YES or NO. +# ENCRYPTED_ATTRIBUTES: These template attributes are encrypted before storing +# them in the database. #******************************************************************************* LOG = [ @@ -109,6 +109,9 @@ VNC_PORTS = [ # RESERVED = "6800, 6801, 6810:6820, 9869" ] +ENCRYPTED_ATTRIBUTES = "EC2_ACCESS, EC2_SECRET, AZ_ID, AZ_CERT, VCENTER_PASSWORD, + NSX_PASSWORD, ONE_PASSWORD" + #******************************************************************************* # API configuration attributes #------------------------------------------------------------------------------- diff --git a/src/cluster/Cluster.cc b/src/cluster/Cluster.cc index 7d6033ee9b..1ae1a13f91 100644 --- a/src/cluster/Cluster.cc +++ b/src/cluster/Cluster.cc @@ -167,14 +167,14 @@ int Cluster::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Cluster - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -314,3 +314,9 @@ int Cluster::from_xml(const string& xml) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int Cluster::post_update_template(string& error_str) +{ + encrypt_all_secrets(obj_template); + + return 0; +} diff --git a/src/common/NebulaUtil.cc b/src/common/NebulaUtil.cc index 48961e4cf1..42520845f3 100644 --- a/src/common/NebulaUtil.cc +++ b/src/common/NebulaUtil.cc @@ -80,9 +80,9 @@ string one_util::log_time() /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -std::string one_util::xml_escape(const std::string& in) +string one_util::xml_escape(const string& in) { - std::string result = in; + string result = in; result = one_util::gsub(result, "&", "&"); result = one_util::gsub(result, "<", "<"); @@ -116,7 +116,7 @@ string * one_util::base64_encode(const string& in) if (BIO_flush(bio_64) != 1) { - return 0; + return nullptr; } size = BIO_get_mem_data(bio_mem,&encoded_c); @@ -154,7 +154,7 @@ string * one_util::base64_decode(const string& in) BIO_write(bio_mem_in, in.c_str(), in.length()); - while((inlen = BIO_read(bio_64, inbuf, 512)) > 0) + while ((inlen = BIO_read(bio_64, inbuf, 512)) > 0) { BIO_write(bio_mem_out, inbuf, inlen); } @@ -199,7 +199,7 @@ string one_util::sha1_digest(const string& in) EVP_MD_CTX_free(mdctx); #endif - for(unsigned int i = 0; i crypted(base64_decode(in)); + + if (!crypted || crypted->empty()) + { + return nullptr; + } + + bool success = true; + EVP_CIPHER_CTX *ctx; + + const unsigned char *key = (unsigned char*) password.c_str(); + const unsigned char *in_data = (unsigned char*) crypted->c_str(); + + unsigned char out[in.length() + AES_BLOCK_SIZE]; + + int outlen1, outlen2; + +#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) + ctx = (EVP_CIPHER_CTX*) malloc(sizeof(EVP_CIPHER_CTX)); + EVP_CIPHER_CTX_init(ctx); +#else + ctx = EVP_CIPHER_CTX_new(); +#endif + + EVP_DecryptInit(ctx, EVP_aes_256_cbc(), key, NULL); + EVP_DecryptUpdate(ctx, out, &outlen1, in_data, crypted->length()); + if (1 != EVP_DecryptFinal(ctx, out + outlen1, &outlen2)) + { + success = false; + } + +#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) + EVP_CIPHER_CTX_cleanup(ctx); + free(ctx); +#else + EVP_CIPHER_CTX_free(ctx); +#endif + + if (!success) + { + return nullptr; + } + + return new string((char*)out, (size_t)(outlen1 + outlen2)); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string one_util::random_password() { return sha256_digest(std::to_string(random())); @@ -341,17 +392,17 @@ static bool not_space(int c) return std::isspace(c) == 0; }; -std::string one_util::trim(const std::string& str) +string one_util::trim(const string& str) { - std::string::const_iterator wfirst; - std::string::const_reverse_iterator rwlast; + string::const_iterator wfirst; + string::const_reverse_iterator rwlast; wfirst = find_if(str.begin(), str.end(), not_space); rwlast = find_if(str.rbegin(),str.rend(),not_space); - std::string::const_iterator wlast(rwlast.base()); + string::const_iterator wlast(rwlast.base()); - std::string tstr(wfirst, wlast); + string tstr(wfirst, wlast); return tstr; } @@ -359,19 +410,19 @@ std::string one_util::trim(const std::string& str) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -std::string one_util::gsub(const std::string& st, const std::string& sfind, - const std::string& srepl) +string one_util::gsub(const string& st, const string& sfind, + const string& srepl) { - std::string result = st; + string result = st; - std::string::size_type pos = 0; + string::size_type pos = 0; size_t srepl_len = srepl.length(); size_t sfind_len = sfind.length(); pos = result.find(sfind, pos); - while(pos != std::string::npos) + while (pos != string::npos) { result.replace(pos, sfind_len , srepl); pos += srepl_len; @@ -385,9 +436,9 @@ std::string one_util::gsub(const std::string& st, const std::string& sfind, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void one_util::escape_json(const std::string& str, std::ostringstream& s) +void one_util::escape_json(const string& str, ostringstream& s) { - std::string::const_iterator it; + string::const_iterator it; s << "\""; @@ -413,9 +464,9 @@ void one_util::escape_json(const std::string& str, std::ostringstream& s) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void one_util::escape_token(const std::string& str, std::ostringstream& s) +void one_util::escape_token(const string& str, ostringstream& s) { - std::string::const_iterator it; + string::const_iterator it; for (it = str.begin(); it != str.end(); ++it) { @@ -436,21 +487,18 @@ void one_util::escape_token(const std::string& str, std::ostringstream& s) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -namespace one_util -{ template<> -void split_unique(const std::string& st, char delim, std::set& res) +void one_util::split_unique(const string& st, char delim, set& res) { - std::vector::const_iterator it; + vector::const_iterator it; - std::vector strings = split(st, delim, true); + vector strings = split(st, delim, true); for (it = strings.begin(); it != strings.end(); it++) { res.insert(*it); } } -} /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -460,18 +508,18 @@ void split_unique(const std::string& st, char delim, std::set& res) */ #define ZBUFFER 16384 -std::string * one_util::zlib_compress(const std::string& in, bool base64) +string * one_util::zlib_compress(const string& in, bool base64) { z_stream zs; - std::ostringstream oss; + ostringstream oss; unsigned char out[ZBUFFER]; - std::string * zstr; + string * zstr; if ( in.empty() ) { - return 0; + return nullptr; } zs.zalloc = Z_NULL; @@ -480,7 +528,7 @@ std::string * one_util::zlib_compress(const std::string& in, bool base64) if ( deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK ) { - return 0; + return nullptr; } zs.avail_in = in.size(); @@ -494,7 +542,7 @@ std::string * one_util::zlib_compress(const std::string& in, bool base64) if ( deflate(&zs, Z_FINISH) == Z_STREAM_ERROR ) { deflateEnd(&zs); - return 0; + return nullptr; } oss.write((const char *)out, ZBUFFER - zs.avail_out); @@ -508,7 +556,7 @@ std::string * one_util::zlib_compress(const std::string& in, bool base64) } else { - zstr = new std::string(oss.str()); + zstr = new string(oss.str()); } return zstr; @@ -517,20 +565,20 @@ std::string * one_util::zlib_compress(const std::string& in, bool base64) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -std::string * one_util::zlib_decompress(const std::string& in, bool base64) +string * one_util::zlib_decompress(const string& in, bool base64) { int rc; z_stream zs; - std::ostringstream oss; + ostringstream oss; unsigned char out[ZBUFFER]; - std::string * in64; + string * in64; if ( in.empty() ) { - return 0; + return nullptr; } zs.zalloc = Z_NULL; @@ -542,7 +590,7 @@ std::string * one_util::zlib_decompress(const std::string& in, bool base64) if ( inflateInit(&zs) != Z_OK) { - return 0; + return nullptr; } if ( base64 ) @@ -553,7 +601,7 @@ std::string * one_util::zlib_decompress(const std::string& in, bool base64) { inflateEnd(&zs); - return 0; + return nullptr; } zs.avail_in = in64->size(); @@ -574,7 +622,7 @@ std::string * one_util::zlib_decompress(const std::string& in, bool base64) delete in64; } - return 0; + return nullptr; } do @@ -593,7 +641,7 @@ std::string * one_util::zlib_decompress(const std::string& in, bool base64) delete in64; } - return 0; + return nullptr; } oss.write((const char *)out, ZBUFFER - zs.avail_out); @@ -606,7 +654,7 @@ std::string * one_util::zlib_decompress(const std::string& in, bool base64) delete in64; } - return new std::string(oss.str()); + return new string(oss.str()); } /* -------------------------------------------------------------------------- */ @@ -634,11 +682,11 @@ extern "C" unsigned long one_util::sslmutex_id_callback() one_util::SSLMutex * one_util::SSLMutex::ssl_mutex; -std::vector one_util::SSLMutex::vmutex; +vector one_util::SSLMutex::vmutex; void one_util::SSLMutex::initialize() { - if ( ssl_mutex == 0 ) + if ( ssl_mutex == nullptr ) { ssl_mutex = new SSLMutex(); } diff --git a/src/datastore/Datastore.cc b/src/datastore/Datastore.cc index 0115535848..1d84bea923 100644 --- a/src/datastore/Datastore.cc +++ b/src/datastore/Datastore.cc @@ -68,8 +68,6 @@ Datastore::Datastore( group_u = 1; } -Datastore::~Datastore(){}; - /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ @@ -164,8 +162,8 @@ void Datastore::disk_attribute( for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++) { - current_val = disk->vector_value((*it).c_str()); - get_template_attribute((*it).c_str(), inherit_val); + current_val = disk->vector_value(*it); + get_template_attribute(*it, inherit_val); if ( current_val.empty() && !inherit_val.empty() ) { @@ -193,7 +191,7 @@ void Datastore::disk_attribute( { get_template_attribute("DRIVER", st); - if(!st.empty() && disk->vector_value("DRIVER").empty()) + if (!st.empty() && disk->vector_value("DRIVER").empty()) { disk->replace("DRIVER", st); } @@ -292,7 +290,7 @@ int Datastore::set_ds_mad(std::string &mad, std::string &error_str) required_attr = one_util::trim(required_attr); one_util::toupper(required_attr); - get_template_attribute(required_attr.c_str(), value); + get_template_attribute(required_attr, value); if ( value.empty() ) { @@ -680,14 +678,14 @@ int Datastore::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Datastore - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -936,35 +934,9 @@ int Datastore::post_update_template(string& error_str) } /* ---------------------------------------------------------------------- */ - /* Encrypt VCENTER_PASSWORD attribute */ + /* Encrypt attributes */ /* ---------------------------------------------------------------------- */ - - get_template_attribute("VCENTER_PASSWORD", vcenter_password); - - if (!vcenter_password.empty() && vcenter_password.size() <= 22) - { - erase_template_attribute("VCENTER_PASSWORD", vcenter_password); - - Nebula& nd = Nebula::instance(); - string one_key; - string * encrypted; - - nd.get_configuration_attribute("ONE_KEY", one_key); - - if (!one_key.empty()) - { - encrypted = one_util::aes256cbc_encrypt(vcenter_password, one_key); - - add_template_attribute("VCENTER_PASSWORD", *encrypted); - - delete encrypted; - } - else - { - add_template_attribute("VCENTER_PASSWORD", vcenter_password); - } - } - + encrypt_all_secrets(obj_template); /* ---------------------------------------------------------------------- */ /* Set the TM_MAD of the Datastore (class & template) */ diff --git a/src/document/Document.cc b/src/document/Document.cc index 295eeb5428..01e598edf9 100644 --- a/src/document/Document.cc +++ b/src/document/Document.cc @@ -42,11 +42,6 @@ Document::Document( int id, set_umask(_umask); } -/* ------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------ */ - -Document::~Document(){}; - /* ************************************************************************ */ /* Document :: Database Access Functions */ /* ************************************************************************ */ @@ -110,14 +105,14 @@ int Document::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Object - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { diff --git a/src/group/Group.cc b/src/group/Group.cc index 9410879407..da5856154c 100644 --- a/src/group/Group.cc +++ b/src/group/Group.cc @@ -120,14 +120,14 @@ int Group::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Group - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { diff --git a/src/host/Host.cc b/src/host/Host.cc index 98f6ffa647..a0a6556efa 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -51,8 +51,6 @@ Host::Host( replace_template_attribute("VM_MAD", vmm_mad_name); } -Host::~Host(){}; - /* ************************************************************************ */ /* Host :: Database Access Functions */ /* ************************************************************************ */ @@ -94,14 +92,14 @@ int Host::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Host - sql_hostname = db->escape_str(name.c_str()); + sql_hostname = db->escape_str(name); if ( sql_hostname == 0 ) { goto error_hostname; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -113,7 +111,7 @@ int Host::insert_replace(SqlDB *db, bool replace, string& error_str) goto error_xml; } - if(replace) + if (replace) { oss << "UPDATE " << table << " SET " << "name = '" << sql_hostname << "', " @@ -390,7 +388,7 @@ int Host::update_info(Template &tmpl, } } - for(map_it = found.begin(); map_it != found.end(); ) + for (map_it = found.begin(); map_it != found.end(); ) { if ( one_util::regex_match("STATE=.",map_it->second.c_str()) != 0 ) { @@ -403,7 +401,7 @@ int Host::update_info(Template &tmpl, } } - for(set_it = tmp_lost_vms->begin(); set_it != tmp_lost_vms->end(); set_it++) + for (set_it = tmp_lost_vms->begin(); set_it != tmp_lost_vms->end(); set_it++) { // Reported as lost at least 2 times? if (prev_tmp_lost.count(*set_it) == 1) @@ -541,7 +539,7 @@ int Host::update_monitoring(SqlDB * db) string error_str; char * sql_xml; - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -669,7 +667,7 @@ int Host::from_xml(const string& xml) return -1; } - rc += host_share.from_xml_node( content[0] ); + rc += host_share.from_xml_node(content[0]); ObjectXML::free_nodes(content); @@ -679,12 +677,12 @@ int Host::from_xml(const string& xml) ObjectXML::get_nodes("/HOST/TEMPLATE", content); - if( content.empty()) + if (content.empty()) { return -1; } - rc += obj_template->from_xml_node( content[0] ); + rc += obj_template->from_xml_node(content[0]); ObjectXML::free_nodes(content); @@ -704,62 +702,13 @@ int Host::from_xml(const string& xml) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -static void nebula_crypt(const std::string in, std::string& out) -{ - Nebula& nd = Nebula::instance(); - string one_key; - string * encrypted; - - nd.get_configuration_attribute("ONE_KEY", one_key); - - if (!one_key.empty()) - { - encrypted = one_util::aes256cbc_encrypt(in, one_key); - - out = *encrypted; - - delete encrypted; - } - else - { - out = in; - } -} - -/* -------------------------------------------------------------------------- */ - -static const map MAX_HOST_VAR_SIZES = { - {"EC2_ACCESS", 21}, - {"EC2_SECRET", 41}, - {"AZ_ID", 41}, - {"AZ_CERT", 3130}, - {"VCENTER_PASSWORD", 22}, - {"NSX_PASSWORD", 22}, - {"ONE_PASSWORD", 22} -}; - int Host::post_update_template(string& error) { string new_im_mad; string new_vm_mad; string cpu_ids; - map::const_iterator it; - - for (it = MAX_HOST_VAR_SIZES.begin(); it != MAX_HOST_VAR_SIZES.end() ; ++it) - { - string att; - string crypted; - - get_template_attribute(it->first.c_str(), att); - - if (!att.empty() && att.size() <= it->second) - { - nebula_crypt(att, crypted); - - replace_template_attribute(it->first, crypted); - } - } + encrypt_all_secrets(obj_template); get_template_attribute("IM_MAD", new_im_mad); get_template_attribute("VM_MAD", new_vm_mad); diff --git a/src/image/Image.cc b/src/image/Image.cc index 8dd295a388..76b88b498c 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -75,8 +75,6 @@ Image::Image(int _uid, set_umask(_umask); } -Image::~Image(){}; - /* ************************************************************************ */ /* Image :: Database Access Functions */ /* ************************************************************************ */ @@ -143,7 +141,7 @@ int Image::insert(SqlDB *db, string& error_str) get_template_attribute("DEV_PREFIX", dev_prefix); - if( dev_prefix.empty() ) + if (dev_prefix.empty()) { if (type == CDROM) { @@ -270,14 +268,14 @@ int Image::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Image - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -289,7 +287,7 @@ int Image::insert_replace(SqlDB *db, bool replace, string& error_str) goto error_xml; } - if(replace) + if (replace) { oss << "UPDATE " << table << " SET " << "name = '" << sql_name << "', " @@ -602,7 +600,7 @@ void Image::disk_attribute(VirtualMachineDisk * disk, } //Image is being copied/cloned - if ( state == Image::LOCKED_USED || state == Image::LOCKED_USED_PERS + if ( state == Image::LOCKED_USED || state == Image::LOCKED_USED_PERS || state == Image::LOCKED ) { disk->replace("CLONING", "YES"); @@ -615,7 +613,7 @@ void Image::disk_attribute(VirtualMachineDisk * disk, //-------------------------------------------------------------------------- // TYPE attribute //-------------------------------------------------------------------------- - switch(type) + switch (type) { case OS: case DATABLOCK: @@ -625,7 +623,7 @@ void Image::disk_attribute(VirtualMachineDisk * disk, case CDROM: //Always use CDROM type for these ones DiskType new_disk_type; - switch(disk_type) + switch (disk_type) { case RBD: new_disk_type = RBD_CDROM; @@ -663,7 +661,7 @@ void Image::disk_attribute(VirtualMachineDisk * disk, for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++) { - get_template_attribute((*it).c_str(), inherit_val); + get_template_attribute(*it, inherit_val); if (!inherit_val.empty()) { @@ -841,11 +839,12 @@ void Image::set_state(ImageState _state) LifeCycleManager* lcm = Nebula::instance().get_lcm(); const set& vms = vm_collection.get_collection(); - for(set::iterator i = vms.begin(); i != vms.end(); i++) + for (set::iterator i = vms.begin(); i != vms.end(); i++) { lcm->trigger(LCMAction::DISK_LOCK_FAILURE, *i); } - } else if( _state == LOCKED) + } + else if (state == LOCKED) { lock_db(-1,-1, PoolObjectSQL::LockStates::ST_USE); } @@ -889,7 +888,7 @@ void Image::set_state_unlock() } else { - if(is_persistent()) + if (is_persistent()) { set_state(USED_PERS); } @@ -910,7 +909,7 @@ void Image::set_state_unlock() { const set& vms = vm_collection.get_collection(); - for(set::iterator i = vms.begin(); i != vms.end(); i++) + for (set::iterator i = vms.begin(); i != vms.end(); i++) { lcm->trigger(LCMAction::DISK_LOCK_SUCCESS, *i); } diff --git a/src/market/MarketPlace.cc b/src/market/MarketPlace.cc index ec613b2eb9..59d6cf99b2 100644 --- a/src/market/MarketPlace.cc +++ b/src/market/MarketPlace.cc @@ -65,8 +65,6 @@ MarketPlace::MarketPlace( set_umask(umask); }; -MarketPlace::~MarketPlace(){}; - /* *************************************************************************** */ /* MartketPlace :: Database Access Functions */ /* *************************************************************************** */ @@ -107,7 +105,7 @@ int MarketPlace::set_market_mad(std::string &mad, std::string &error_str) required_attr = one_util::trim(required_attr); one_util::toupper(required_attr); - get_template_attribute(required_attr.c_str(), value); + get_template_attribute(required_attr, value); if ( value.empty() ) { @@ -179,14 +177,14 @@ int MarketPlace::insert_replace(SqlDB *db, bool replace, string& error_str) char * sql_name; char * sql_xml; - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { diff --git a/src/market/MarketPlaceApp.cc b/src/market/MarketPlaceApp.cc index 433a85714f..d455ef6202 100644 --- a/src/market/MarketPlaceApp.cc +++ b/src/market/MarketPlaceApp.cc @@ -69,8 +69,6 @@ MarketPlaceApp::MarketPlaceApp( set_umask(umask); }; -MarketPlaceApp::~MarketPlaceApp(){}; - /* ************************************************************************ */ /* MartketPlaceApp:: Database Access Functions */ /* ************************************************************************ */ @@ -155,14 +153,14 @@ int MarketPlaceApp::insert_replace(SqlDB *db, bool replace, string& error_str) char * sql_name; char * sql_xml; - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -392,7 +390,7 @@ MarketPlaceApp::Type MarketPlaceApp::str_to_type(string& str_type) int MarketPlaceApp::enable(bool enable, string& error_str) { - switch(state) + switch (state) { case INIT: case LOCKED: diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index 4084998ad7..0e214e1c17 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -531,6 +531,16 @@ void Nebula::start(bool bootstrap_only) // ------------------------------------------------------------------------- // Pools // ------------------------------------------------------------------------- + string encrypted_attributes; + set * po_ea = const_cast *>(&PoolObjectSQL::ENCRYPTED_ATTRIBUTES); + + nebula_configuration->get("ENCRYPTED_ATTRIBUTES", encrypted_attributes); + + for (const auto& att_name : one_util::split(encrypted_attributes, ',')) + { + po_ea->insert(one_util::trim(att_name)); + } + try { /* -------------------------- Cluster Pool -------------------------- */ diff --git a/src/nebula/NebulaTemplate.cc b/src/nebula/NebulaTemplate.cc index 5420e50a7a..21abaa961d 100644 --- a/src/nebula/NebulaTemplate.cc +++ b/src/nebula/NebulaTemplate.cc @@ -452,6 +452,8 @@ void OpenNebulaTemplate::set_conf_default() set_conf_single("SCRIPTS_REMOTE_DIR", "/var/tmp/one"); set_conf_single("VM_SUBMIT_ON_HOLD", "NO"); set_conf_single("API_LIST_ORDER", "DESC"); + set_conf_single("ENCRYPTED_ATTRIBUTES", "EC2_ACCESS, EC2_SECRET, AZ_ID, AZ_CERT," + "VCENTER_PASSWORD, NSX_PASSWORD, ONE_PASSWORD"); //DB CONFIGURATION vvalue.insert(make_pair("BACKEND","sqlite")); diff --git a/src/nebula/SystemDB.cc b/src/nebula/SystemDB.cc index ca3d94878f..f1c8b16355 100644 --- a/src/nebula/SystemDB.cc +++ b/src/nebula/SystemDB.cc @@ -273,7 +273,7 @@ int SystemDB::insert_replace( int rc; char * sql_xml; - sql_xml = db->escape_str(xml_attr.c_str()); + sql_xml = db->escape_str(xml_attr); if ( sql_xml == 0 ) { diff --git a/src/oca/ruby/opennebula/cluster.rb b/src/oca/ruby/opennebula/cluster.rb index 1e3e5b019e..5778cd28d0 100644 --- a/src/oca/ruby/opennebula/cluster.rb +++ b/src/oca/ruby/opennebula/cluster.rb @@ -64,8 +64,8 @@ module OpenNebula ####################################################################### # Retrieves the information of the given Cluster. - def info() - super(CLUSTER_METHODS[:info], 'CLUSTER') + def info(decrypt = false) + super(CLUSTER_METHODS[:info], 'CLUSTER', decrypt) end alias_method :info!, :info diff --git a/src/oca/ruby/opennebula/datastore.rb b/src/oca/ruby/opennebula/datastore.rb index 7670b5a6d5..cad9bf7228 100644 --- a/src/oca/ruby/opennebula/datastore.rb +++ b/src/oca/ruby/opennebula/datastore.rb @@ -76,8 +76,8 @@ module OpenNebula ####################################################################### # Retrieves the information of the given Datastore. - def info() - super(DATASTORE_METHODS[:info], 'DATASTORE') + def info(decrypt = false) + super(DATASTORE_METHODS[:info], 'DATASTORE', decrypt) end alias_method :info!, :info diff --git a/src/oca/ruby/opennebula/host.rb b/src/oca/ruby/opennebula/host.rb index fb63e0dd47..ff31ebf4a3 100644 --- a/src/oca/ruby/opennebula/host.rb +++ b/src/oca/ruby/opennebula/host.rb @@ -86,8 +86,8 @@ module OpenNebula ####################################################################### # Retrieves the information of the given Host. - def info() - super(HOST_METHODS[:info], 'HOST') + def info(decrypt = false) + super(HOST_METHODS[:info], 'HOST', decrypt) end alias_method :info!, :info diff --git a/src/oca/ruby/opennebula/pool_element.rb b/src/oca/ruby/opennebula/pool_element.rb index c033f5591d..9108e3882f 100644 --- a/src/oca/ruby/opennebula/pool_element.rb +++ b/src/oca/ruby/opennebula/pool_element.rb @@ -65,10 +65,10 @@ module OpenNebula # # @return [nil, OpenNebula::Error] nil in case of success, Error # otherwise - def info(xml_method, root_element) + def info(xml_method, root_element, decrypt = false) return Error.new('ID not defined') if !@pe_id - rc = @client.call(xml_method, @pe_id) + rc = @client.call(xml_method, @pe_id, decrypt) if !OpenNebula.is_error?(rc) initialize_xml(rc, root_element) diff --git a/src/oca/ruby/opennebula/template.rb b/src/oca/ruby/opennebula/template.rb index 16457ee2ba..82f84f9341 100644 --- a/src/oca/ruby/opennebula/template.rb +++ b/src/oca/ruby/opennebula/template.rb @@ -69,10 +69,10 @@ module OpenNebula # Retrieves the information of the given Template. # @param extended [true,false] optional flag to process the template and # include extended information, such as the SIZE for each DISK - def info(extended=false) + def info(extended=false, decrypt=false) return Error.new('ID not defined') if !@pe_id - rc = @client.call(TEMPLATE_METHODS[:info], @pe_id, extended) + rc = @client.call(TEMPLATE_METHODS[:info], @pe_id, extended, decrypt) if !OpenNebula.is_error?(rc) initialize_xml(rc, 'VMTEMPLATE') @@ -98,10 +98,10 @@ module OpenNebula end # Deletes the Template - # + # # @param recursive [true,false] optional, deletes the template plus # any image defined in DISK. - # + # # @return [nil, OpenNebula::Error] nil in case of success, Error # otherwise def delete(recursive=false) diff --git a/src/oca/ruby/opennebula/virtual_machine.rb b/src/oca/ruby/opennebula/virtual_machine.rb index 2a4a1b6245..ee0042c943 100644 --- a/src/oca/ruby/opennebula/virtual_machine.rb +++ b/src/oca/ruby/opennebula/virtual_machine.rb @@ -277,8 +277,8 @@ module OpenNebula ####################################################################### # Retrieves the information of the given VirtualMachine. - def info() - super(VM_METHODS[:info], 'VM') + def info(decrypt = false) + super(VM_METHODS[:info], 'VM', decrypt) end alias_method :info!, :info @@ -468,7 +468,7 @@ module OpenNebula # overcommited. Defaults to false # @param ds_id [Integer] The System Datastore where to migrate the VM. # To use the current one, set it to -1 - # @param mtype [Integer] How to perform the cold migration: + # @param mtype [Integer] How to perform the cold migration: # - 0: save - restore, # - 1: power off - boot # - 2: power off hard - boot diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index 197a8d975d..449effa7f4 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -70,8 +70,8 @@ module OpenNebula ####################################################################### # Retrieves the information of the given VirtualNetwork. - def info() - super(VN_METHODS[:info], 'VNET') + def info(decrypt = false) + super(VN_METHODS[:info], 'VNET', decrypt) end alias_method :info!, :info diff --git a/src/oca/ruby/opennebula/vntemplate.rb b/src/oca/ruby/opennebula/vntemplate.rb index a8de76b678..43d51ea4ad 100644 --- a/src/oca/ruby/opennebula/vntemplate.rb +++ b/src/oca/ruby/opennebula/vntemplate.rb @@ -68,10 +68,10 @@ module OpenNebula # Retrieves the information of the given VNTemplate. # include extended information, such as the SIZE for each DISK - def info() + def info(decrypt = false) return Error.new('ID not defined') if !@pe_id - rc = @client.call(TEMPLATE_METHODS[:info], @pe_id, false) + rc = @client.call(TEMPLATE_METHODS[:info], @pe_id, decrypt) if !OpenNebula.is_error?(rc) initialize_xml(rc, 'VNTEMPLATE') @@ -97,7 +97,7 @@ module OpenNebula end # Deletes the Template - # + # # @return [nil, OpenNebula::Error] nil in case of success, Error # otherwise def delete() diff --git a/src/parsers/vm_var_syntax.y b/src/parsers/vm_var_syntax.y index 6f0a53350b..243f3ceed1 100644 --- a/src/parsers/vm_var_syntax.y +++ b/src/parsers/vm_var_syntax.y @@ -44,7 +44,7 @@ int vm_var_parse (VirtualMachine * vm, ostringstream * parsed, char ** errmsg, #define YYERROR_VERBOSE -void vm_var_error(YYLTYPE * llocp, mem_collector * mc, VirtualMachine * vm, +void vm_var_error(YYLTYPE * llocp, mem_collector * mc, VirtualMachine * vm, ostringstream * parsed, char ** errmsg, yyscan_t scanner, const char * str); int vm_var_lex(YYSTYPE *lvalp, YYLTYPE *llocp, mem_collector * mc, @@ -98,7 +98,7 @@ void get_image_attribute(VirtualMachine * vm, for (int i=0; i < num ;i++) { - if ( disks[i]->vector_value(img_name.c_str()) == img_value ) + if ( disks[i]->vector_value(img_name) == img_value ) { string iid_str = disks[i]->vector_value("IMAGE_ID"); istringstream iss(iid_str); @@ -135,7 +135,7 @@ void get_image_attribute(VirtualMachine * vm, } else { - img->get_template_attribute(attr_name.c_str(), attr_value); + img->get_template_attribute(attr_name, attr_value); } img->unlock(); @@ -175,7 +175,7 @@ void get_network_attribute(VirtualMachine * vm, for (int i=0; i < num ;i++) { - if ( nets[i]->vector_value(net_name.c_str()) == net_value ) + if ( nets[i]->vector_value(net_name) == net_value ) { if (nets[i]->vector_value("NETWORK_ID", vnet_id) != 0) { @@ -240,7 +240,7 @@ void get_network_attribute(VirtualMachine * vm, } else { - vn->get_template_attribute(attr_name.c_str(), attr_value, ar_id); + vn->get_template_attribute(attr_name, attr_value, ar_id); } vn->unlock(); @@ -273,7 +273,7 @@ void get_user_attribute(VirtualMachine * vm, } else { - user->get_template_attribute(attr_name.c_str(),attr_value); + user->get_template_attribute(attr_name, attr_value); } user->unlock(); @@ -316,11 +316,11 @@ void insert_single(VirtualMachine * vm, else { - vm->get_template_attribute(name.c_str(),value); + vm->get_template_attribute(name, value); if (value.empty()) { - vm->get_user_template_attribute(name.c_str(),value); + vm->get_user_template_attribute(name, value); } } @@ -387,7 +387,7 @@ void insert_vector(VirtualMachine * vm, } else { - if ( ( num = vm->get_template_attribute(name.c_str(), values) ) <= 0 ) + if ( ( num = vm->get_template_attribute(name, values) ) <= 0 ) { return; } @@ -400,7 +400,7 @@ void insert_vector(VirtualMachine * vm, { for (int i=0 ; i < num ; i++) { - if (values[i]->vector_value(vvar.c_str()) == vval) + if (values[i]->vector_value(vvar) == vval) { vattr = values[i]; break; @@ -410,7 +410,7 @@ void insert_vector(VirtualMachine * vm, if ( vattr != 0 ) { - parsed << vattr->vector_value(vname.c_str()); + parsed << vattr->vector_value(vname); } } } diff --git a/src/pool/PoolObjectSQL.cc b/src/pool/PoolObjectSQL.cc index 781c522ca0..6caffa8dfc 100644 --- a/src/pool/PoolObjectSQL.cc +++ b/src/pool/PoolObjectSQL.cc @@ -20,6 +20,8 @@ #include "Nebula.h" #include "Clusterable.h" +const set PoolObjectSQL::ENCRYPTED_ATTRIBUTES{}; + const string PoolObjectSQL::INVALID_NAME_CHARS = "&|:\\\";/'#{}$<>"; const int PoolObjectSQL::LOCK_DB_EXPIRATION = 120; @@ -86,7 +88,7 @@ int PoolObjectSQL::select(SqlDB *db) int PoolObjectSQL::select_oid(SqlDB *db, const char * _table, const string& _name, int _uid) { - char * sql_name = db->escape_str(_name.c_str()); + char * sql_name = db->escape_str(_name); if ( sql_name == 0 ) { @@ -164,7 +166,7 @@ int PoolObjectSQL::select(SqlDB *db, const string& _name, int _uid) int rc; char * sql_name; - sql_name = db->escape_str(_name.c_str()); + sql_name = db->escape_str(_name); if ( sql_name == 0 ) { @@ -451,7 +453,7 @@ void PoolObjectSQL::get_permissions(PoolObjectAuth& auth) Clusterable* cl = dynamic_cast(this); - if(cl != 0) + if (cl != 0) { auth.cids = cl->get_cluster_ids(); } @@ -459,7 +461,7 @@ void PoolObjectSQL::get_permissions(PoolObjectAuth& auth) { ClusterableSingle* cls = dynamic_cast(this); - if(cls != 0) + if (cls != 0) { auth.cids.insert(cls->get_cluster_id()); } @@ -469,16 +471,16 @@ void PoolObjectSQL::get_permissions(PoolObjectAuth& auth) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int PoolObjectSQL::set_permissions( int _owner_u, - int _owner_m, - int _owner_a, - int _group_u, - int _group_m, - int _group_a, - int _other_u, - int _other_m, - int _other_a, - string& error_str) +int PoolObjectSQL::set_permissions(int _owner_u, + int _owner_m, + int _owner_a, + int _group_u, + int _group_m, + int _group_a, + int _other_u, + int _other_m, + int _other_a, + string& error_str) { if ( _owner_u < -1 || _owner_u > 1 ) goto error_value; if ( _owner_m < -1 || _owner_m > 1 ) goto error_value; @@ -680,3 +682,148 @@ int PoolObjectSQL::lock_db_from_xml() return rc; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void PoolObjectSQL::encrypt(const std::string& in, std::string& out) +{ + Nebula& nd = Nebula::instance(); + string one_key; + + nd.get_configuration_attribute("ONE_KEY", one_key); + + if (!one_key.empty()) + { + string * encrypted = one_util::aes256cbc_encrypt(in, one_key); + + out = *encrypted; + + delete encrypted; + } + else + { + out = in; + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +bool PoolObjectSQL::decrypt(const std::string& in, std::string& out) +{ + Nebula& nd = Nebula::instance(); + string one_key; + + nd.get_configuration_attribute("ONE_KEY", one_key); + + if (one_key.empty()) + { + out = in; + return true; + } + + string * plain = one_util::aes256cbc_decrypt(in, one_key); + + if (plain == nullptr) + { + return false; + } + + out = *plain; + + delete plain + + return true; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void PoolObjectSQL::encrypt_all_secrets(Template *tmpl) +{ + for (const string& att_name : ENCRYPTED_ATTRIBUTES) + { + string att; + string encrypted; + string tmp; + + auto path = one_util::split(att_name, '/'); + + if (path.size() > 1) + { + auto vatt = tmpl->get(path[0]); + + if (vatt == nullptr) + { + continue; + } + + att = vatt->vector_value(path[1]); + + if (!att.empty() && !decrypt(att, tmp)) + { + // Nested attribute present, but not encrypted, crypt it + encrypt(att, encrypted); + + vatt->replace(path[1], encrypted); + } + } + else + { + tmpl->get(att_name, att); + + if (!att.empty() && !decrypt(att, tmp)) + { + // Simple attribute present, but not encrypted, crypt it + encrypt(att, encrypted); + + tmpl->replace(att_name, encrypted); + } + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void PoolObjectSQL::decrypt_all_secrets(Template *tmpl) +{ + for (const string& att_name : ENCRYPTED_ATTRIBUTES) + { + string att; + string plain; + + auto path = one_util::split(att_name, '/'); + + if (path.size() > 1) + { + auto vatt = tmpl->get(path[0]); + + if (vatt == nullptr) + { + continue; + } + + ostringstream oss; + + vatt->to_json(oss); + + att = vatt->vector_value(path[1]); + + if (!att.empty() && decrypt(att, plain)) + { + vatt->replace(path[1], plain); + } + } + else + { + tmpl->get(att_name, att); + + if (!att.empty() && decrypt(att, plain)) + { + tmpl->replace(att_name, plain); + } + } + } +} diff --git a/src/rm/RequestManagerInfo.cc b/src/rm/RequestManagerInfo.cc index 718b01fcf3..edb62a69af 100644 --- a/src/rm/RequestManagerInfo.cc +++ b/src/rm/RequestManagerInfo.cc @@ -49,13 +49,25 @@ void RequestManagerInfo::request_execute(xmlrpc_c::paramList const& paramList, object = pool->get_ro(oid); - if ( object == 0 ) + if ( object == nullptr ) { att.resp_id = oid; failure_response(NO_EXISTS, att); return; } + // Check optional parameter - decrypt + bool decrypt = false; + if (att.is_admin() && paramList.size() > 2) + { + decrypt = xmlrpc_c::value_boolean(paramList.getBoolean(2)); + } + + if (decrypt) + { + object->decrypt_all_secrets(); + } + to_xml(att, object, str); object->unlock(); @@ -71,15 +83,17 @@ void RequestManagerInfo::request_execute(xmlrpc_c::paramList const& paramList, void TemplateInfo::request_execute(xmlrpc_c::paramList const& paramList, RequestAttributes& att) { - VMTemplatePool * tpool = static_cast(pool); - VirtualMachineTemplate * extended_tmpl = 0; - VMTemplate * vm_tmpl; + VMTemplatePool * tpool = static_cast(pool); + VMTemplate * vm_tmpl; + + VirtualMachineTemplate * extended_tmpl = nullptr; PoolObjectAuth perms; - int oid = xmlrpc_c::value_int(paramList.getInt(1)); - bool extended = false; - string str; + int oid = xmlrpc_c::value_int(paramList.getInt(1)); + bool extended = false; + + string str; if ( paramList.size() > 2 ) { @@ -88,7 +102,7 @@ void TemplateInfo::request_execute(xmlrpc_c::paramList const& paramList, vm_tmpl = tpool->get_ro(oid); - if ( vm_tmpl == 0 ) + if ( vm_tmpl == nullptr ) { att.resp_id = oid; failure_response(NO_EXISTS, att); @@ -126,7 +140,7 @@ void TemplateInfo::request_execute(xmlrpc_c::paramList const& paramList, vm_tmpl = tpool->get_ro(oid); - if ( vm_tmpl == 0 ) + if ( vm_tmpl == nullptr ) { att.resp_id = oid; failure_response(NO_EXISTS, att); @@ -135,17 +149,30 @@ void TemplateInfo::request_execute(xmlrpc_c::paramList const& paramList, return; } + // Check optional parameter - decrypt + bool decrypt = false; + + if (att.is_admin() && paramList.size() > 3) + { + decrypt = xmlrpc_c::value_boolean(paramList.getBoolean(3)); + } + + if (decrypt) + { + vm_tmpl->decrypt_all_secrets(); + } + if (extended) { vm_tmpl->to_xml(str, extended_tmpl); - - delete extended_tmpl; } else { vm_tmpl->to_xml(str); } + delete extended_tmpl; + vm_tmpl->unlock(); success_response(str, att); @@ -159,18 +186,17 @@ void TemplateInfo::request_execute(xmlrpc_c::paramList const& paramList, void VirtualNetworkTemplateInfo::request_execute(xmlrpc_c::paramList const& paramList, RequestAttributes& att) { - VNTemplatePool * tpool = static_cast(pool); - VirtualNetworkTemplate * extended_tmpl = 0; - VNTemplate * vn_tmpl; + VNTemplatePool * tpool = static_cast(pool); + VNTemplate * vn_tmpl; PoolObjectAuth perms; - int oid = xmlrpc_c::value_int(paramList.getInt(1)); - string str; + int oid = xmlrpc_c::value_int(paramList.getInt(1)); + string str; vn_tmpl = tpool->get_ro(oid); - if ( vn_tmpl == 0 ) + if ( vn_tmpl == nullptr ) { att.resp_id = oid; failure_response(NO_EXISTS, att); @@ -190,21 +216,32 @@ void VirtualNetworkTemplateInfo::request_execute(xmlrpc_c::paramList const& para att.resp_msg = ar.message; failure_response(AUTHORIZATION, att); - delete extended_tmpl; return; } vn_tmpl = tpool->get_ro(oid); - if ( vn_tmpl == 0 ) + if ( vn_tmpl == nullptr ) { att.resp_id = oid; failure_response(NO_EXISTS, att); - delete extended_tmpl; return; } + // Check optional parameter - decrypt + bool decrypt = false; + + if (att.is_admin() && paramList.size() > 2) + { + decrypt = xmlrpc_c::value_boolean(paramList.getBoolean(2)); + } + + if (decrypt) + { + vn_tmpl->decrypt_all_secrets(); + } + vn_tmpl->to_xml(str); vn_tmpl->unlock(); diff --git a/src/rm/RequestManagerPoolInfoFilter.cc b/src/rm/RequestManagerPoolInfoFilter.cc index d173879cd9..c0891dde4e 100644 --- a/src/rm/RequestManagerPoolInfoFilter.cc +++ b/src/rm/RequestManagerPoolInfoFilter.cc @@ -122,7 +122,7 @@ void VirtualMachinePoolInfo::request_execute( return; } - switch(state) + switch (state) { case VirtualMachinePoolInfo::ALL_VM: break; @@ -428,7 +428,7 @@ void RequestManagerPoolInfoFilter::where_filter( if (!oid_str.empty()) { - filter << "(" << oid_str << ")" ; + filter << "(" << oid_str << ")"; empty = false; } diff --git a/src/secgroup/SecurityGroup.cc b/src/secgroup/SecurityGroup.cc index c5aee76b83..eb789ab9a4 100644 --- a/src/secgroup/SecurityGroup.cc +++ b/src/secgroup/SecurityGroup.cc @@ -60,11 +60,6 @@ SecurityGroup::SecurityGroup( set_umask(_umask); } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -SecurityGroup::~SecurityGroup(){}; - /* ************************************************************************ */ /* SecurityGroup :: Database Access Functions */ /* ************************************************************************ */ @@ -123,14 +118,14 @@ int SecurityGroup::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the security group - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { diff --git a/src/sql/LogDB.cc b/src/sql/LogDB.cc index 6bcc77589f..9362a54b61 100644 --- a/src/sql/LogDB.cc +++ b/src/sql/LogDB.cc @@ -285,7 +285,7 @@ int LogDB::update_raft_state(std::string name, std::string& raft_xml) { std::ostringstream oss; - char * sql_db = db->escape_str(raft_xml.c_str()); + char * sql_db = db->escape_str(raft_xml); if ( sql_db == 0 ) { @@ -317,7 +317,7 @@ int LogDB::insert(uint64_t index, unsigned int term, const std::string& sql, return -1; } - char * sql_db = db->escape_str(zsql->c_str()); + char * sql_db = db->escape_str(*zsql); delete zsql; diff --git a/src/tm/TransferManager.cc b/src/tm/TransferManager.cc index e6ad36f754..340a382a3b 100644 --- a/src/tm/TransferManager.cc +++ b/src/tm/TransferManager.cc @@ -476,7 +476,7 @@ static string prolog_os_transfer_commands( { string base_ds = base + "_DS"; - string name_ds = os_attr->vector_value(base_ds.c_str()); + string name_ds = os_attr->vector_value(base_ds); if ( name_ds.empty() ) { @@ -487,9 +487,9 @@ static string prolog_os_transfer_commands( string base_ds_id = base + "_DS_DSID"; string base_tm = base + "_DS_TM"; - string source = os_attr->vector_value(base_source.c_str()); - string ds_id = os_attr->vector_value(base_ds_id.c_str()); - string tm_mad = os_attr->vector_value(base_tm.c_str()); + string source = os_attr->vector_value(base_source); + string ds_id = os_attr->vector_value(base_ds_id); + string tm_mad = os_attr->vector_value(base_tm); if ( source.empty() || ds_id.empty() || tm_mad.empty() ) { @@ -1150,7 +1150,7 @@ void TransferManager::epilog_transfer_command( { tm_mad_system = "." + tsys; } - + //MVDS(.tm_mad_system) tm_mad hostname:remote_system_dir/disk.0 vmid dsid xfr << "MVDS" << tm_mad_system << " " << tm_mad << " " diff --git a/src/um/Quota.cc b/src/um/Quota.cc index b068428fde..455ee55721 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -73,7 +73,7 @@ void Quota::add_to_quota(VectorAttribute * attr, const string& va_name, float nu istringstream iss; float total; - iss.str(attr->vector_value(va_name.c_str())); + iss.str(attr->vector_value(va_name)); iss >> total; @@ -216,7 +216,7 @@ bool Quota::check_quota(const string& qid, } q->vector_value(metrics[i], limit); - q->vector_value(metrics_used.c_str(), usage); + q->vector_value(metrics_used, usage); if ( limit == DEFAULT ) { @@ -345,7 +345,7 @@ void Quota::cleanup_quota(const string& qid) metrics_used += "_USED"; q->vector_value(metrics[i], limit); - q->vector_value(metrics_used.c_str(), usage); + q->vector_value(metrics_used, usage); if ( usage != 0 || limit != implicit_limit ) { diff --git a/src/um/QuotasSQL.cc b/src/um/QuotasSQL.cc index b42b76e598..f401d44368 100644 --- a/src/um/QuotasSQL.cc +++ b/src/um/QuotasSQL.cc @@ -108,7 +108,7 @@ int QuotasSQL::insert_replace(SqlDB *db, bool replace, string& error_str) // Quota fields - sql_quota_xml = db->escape_str(to_xml_db(xml_quota).c_str()); + sql_quota_xml = db->escape_str(to_xml_db(xml_quota)); if ( sql_quota_xml == 0 ) { diff --git a/src/um/User.cc b/src/um/User.cc index 5b669eda15..29a9cb9706 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -129,14 +129,14 @@ int User::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the User - sql_username = db->escape_str(name.c_str()); + sql_username = db->escape_str(name); if ( sql_username == 0 ) { goto error_username; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -149,7 +149,7 @@ int User::insert_replace(SqlDB *db, bool replace, string& error_str) } // Construct the SQL statement to Insert or Replace - if(replace) + if (replace) { oss << "UPDATE " << table << " SET " << "name = '" << sql_username << "', " @@ -327,7 +327,7 @@ int User::split_secret(const string secret, string& user, string& pass) size_t pos; int rc = -1; - pos=secret.find(":"); + pos = secret.find(":"); if (pos != string::npos) { @@ -407,7 +407,7 @@ int User::get_umask() const get_template_attribute("UMASK", umask_st); - if(umask_st.empty()) + if (umask_st.empty()) { Nebula::instance().get_configuration_attribute("DEFAULT_UMASK",umask_st); } diff --git a/src/vdc/Vdc.cc b/src/vdc/Vdc.cc index 353599919e..83cd71c733 100644 --- a/src/vdc/Vdc.cc +++ b/src/vdc/Vdc.cc @@ -52,11 +52,6 @@ Vdc::Vdc(int id, Template* vdc_template): } } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -Vdc::~Vdc(){}; - /* ************************************************************************ */ /* Vdc :: Database Access Functions */ /* ************************************************************************ */ @@ -107,14 +102,14 @@ int Vdc::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the vdc - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { diff --git a/src/vm/History.cc b/src/vm/History.cc index 96daee3bcc..75eec7cb46 100644 --- a/src/vm/History.cc +++ b/src/vm/History.cc @@ -168,7 +168,7 @@ int History::insert_replace(SqlDB *db, bool replace) return 0; } - sql_xml = db->escape_str(to_db_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_db_xml(xml_body)); if ( sql_xml == 0 ) { diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 866b3d6c58..c8ec580a7a 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -1681,14 +1681,14 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace, string& error_str) char * sql_short_xml; char * sql_text; - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_generic; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -1700,7 +1700,7 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace, string& error_str) goto error_xml; } - sql_short_xml = db->escape_str(to_xml_short(short_xml_body).c_str()); + sql_short_xml = db->escape_str(to_xml_short(short_xml_body)); if ( sql_short_xml == 0 ) { @@ -1712,7 +1712,7 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace, string& error_str) goto error_xml_short; } - sql_text = db->escape_str(to_token(text).c_str()); + sql_text = db->escape_str(to_token(text)); if ( sql_text == 0 ) { @@ -1795,7 +1795,7 @@ int VirtualMachine::update_search(SqlDB * db) std::ostringstream oss; std::string text; - char * sql_text = db->escape_str(to_token(text).c_str()); + char * sql_text = db->escape_str(to_token(text)); if ( sql_text == 0 ) { @@ -1840,7 +1840,7 @@ int VirtualMachine::update_monitoring(SqlDB * db) << "" << ""; - sql_xml = db->escape_str(oss.str().c_str()); + sql_xml = db->escape_str(oss.str()); if ( sql_xml == 0 ) { @@ -2706,9 +2706,19 @@ int VirtualMachine::replace_template( return -1; } - delete user_obj_template; + auto old_user_tmpl = user_obj_template; + user_obj_template = new_tmpl; - user_obj_template = new_tmpl; + if (post_update_template(error) == -1) + { + delete user_obj_template; + + user_obj_template = old_user_tmpl; + + return -1; + } + + delete old_user_tmpl; return 0; } @@ -2752,14 +2762,20 @@ int VirtualMachine::append_template( /* ---------------------------------------------------------------------- */ /* Append new_tmpl to the current user_template */ /* ---------------------------------------------------------------------- */ + auto old_user_tmpl = new VirtualMachineTemplate(*user_obj_template); + if (user_obj_template != 0) { if (keep_restricted && new_tmpl->check_restricted(rname, user_obj_template)) { error ="User Template includes a restricted attribute " + rname; + delete new_tmpl; + delete old_user_tmpl; + return -1; } + user_obj_template->merge(new_tmpl); delete new_tmpl; @@ -2769,12 +2785,27 @@ int VirtualMachine::append_template( if (keep_restricted && new_tmpl->check_restricted(rname)) { error ="User Template includes a restricted attribute " + rname; + delete new_tmpl; + delete old_user_tmpl; + return -1; } + user_obj_template = new_tmpl; } + if (post_update_template(error) == -1) + { + delete user_obj_template; + + user_obj_template = old_user_tmpl; + + return -1; + } + + delete old_user_tmpl; + return 0; } @@ -3662,6 +3693,17 @@ int VirtualMachine::parse_sched_action(string& error_str) /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ +int VirtualMachine::post_update_template(string& error_str) +{ + encrypt_all_secrets(obj_template); + encrypt_all_secrets(user_obj_template); + + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + int VirtualMachine::check_tm_mad_disks(const string& tm_mad, string& error) { string tm_mad_sys; diff --git a/src/vm/VirtualMachineParser.cc b/src/vm/VirtualMachineParser.cc index 563cea3c81..e20722d9a3 100644 --- a/src/vm/VirtualMachineParser.cc +++ b/src/vm/VirtualMachineParser.cc @@ -92,7 +92,7 @@ int VirtualMachine::set_os_file(VectorAttribute* os, const string& base_name, string type_str; - attr = os->vector_value(base_name_ds.c_str()); + attr = os->vector_value(base_name_ds); if ( attr.empty() ) { @@ -522,7 +522,7 @@ void VirtualMachine::parse_well_known_attributes() * TOPOLOGY * NUMA_NODE */ - std::vector names = {"INPUT", "FEATURES", "RAW", + std::vector names = {"INPUT", "FEATURES", "RAW", "CLONING_TEMPLATE_ID", "TOPOLOGY", "NUMA_NODE"}; for (auto it = names.begin(); it != names.end() ; ++it) diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index 3ae1eb314d..bf2d4fc266 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -217,7 +217,7 @@ int VirtualMachinePool::insert_index(const string& deploy_id, int vmid, bool replace) { ostringstream oss; - char * deploy_name = db->escape_str(deploy_id.c_str()); + char * deploy_name = db->escape_str(deploy_id); if (deploy_name == 0) { @@ -246,7 +246,7 @@ int VirtualMachinePool::insert_index(const string& deploy_id, int vmid, void VirtualMachinePool::drop_index(const string& deploy_id) { ostringstream oss; - char * deploy_name = db->escape_str(deploy_id.c_str()); + char * deploy_name = db->escape_str(deploy_id); if (deploy_name == 0) { @@ -525,7 +525,7 @@ int VirtualMachinePool::get_vmid (const string& deploy_id) cb.set_callback(&vmid); oss << "SELECT vmid FROM " << import_table - << " WHERE deploy_id = '" << db->escape_str(deploy_id.c_str()) << "'"; + << " WHERE deploy_id = '" << db->escape_str(deploy_id) << "'"; rc = db->exec_rd(oss, &cb); @@ -926,7 +926,7 @@ int VirtualMachinePool::calculate_showback( vm_month_it->second.to_xml(body) << ""; - sql_body = db->escape_str(body.str().c_str()); + sql_body = db->escape_str(body.str()); if ( sql_body == 0 ) { diff --git a/src/vm_group/VMGroup.cc b/src/vm_group/VMGroup.cc index de10d29013..5dcc5877fe 100644 --- a/src/vm_group/VMGroup.cc +++ b/src/vm_group/VMGroup.cc @@ -52,8 +52,6 @@ VMGroup::VMGroup(int _uid, int _gid, const string& _uname, const string& _gname, set_umask(_umask); } -VMGroup::~VMGroup(){}; - /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -155,14 +153,14 @@ int VMGroup::insert_replace(SqlDB *db, bool replace, string& error_str) char * sql_name; char * sql_xml; - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { diff --git a/src/vm_template/VMTemplate.cc b/src/vm_template/VMTemplate.cc index a344a8f1a8..b2f9df5749 100644 --- a/src/vm_template/VMTemplate.cc +++ b/src/vm_template/VMTemplate.cc @@ -43,11 +43,6 @@ VMTemplate::VMTemplate(int id, set_umask(umask); } -/* ------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------ */ - -VMTemplate::~VMTemplate(){}; - /* ************************************************************************ */ /* VMTemplate :: Database Access Functions */ /* ************************************************************************ */ @@ -103,14 +98,14 @@ int VMTemplate::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Object - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -193,6 +188,8 @@ int VMTemplate::post_update_template(string& error) return rc; } + encrypt_all_secrets(obj_template); + return 0; } diff --git a/src/vmm_mad/remotes/az/az_driver.rb b/src/vmm_mad/remotes/az/az_driver.rb index ebb5dd93b9..e5a644cb76 100755 --- a/src/vmm_mad/remotes/az/az_driver.rb +++ b/src/vmm_mad/remotes/az/az_driver.rb @@ -196,7 +196,10 @@ class AzureDriver pool.info objects=pool.select {|object| object.name==@host } - objects.first + host_id = objects.first.id + xmlhost = OpenNebula::Host.new_with_id(host_id, client) + xmlhost.info(true) + xmlhost end # Check the current template to retrieve @@ -206,23 +209,14 @@ class AzureDriver client = OpenNebula::Client.new xmlhost = get_host_info(client) - system = OpenNebula::System.new(client) - config = system.get_configuration - raise "Error getting oned configuration : #{config.message}" if OpenNebula.is_error?(config) - - token = config["ONE_KEY"] - conn_opts = { :cert => xmlhost["TEMPLATE/AZ_CERT"], :id => xmlhost["TEMPLATE/AZ_ID"] } - #conn_opts = OpenNebula.encrypt(conn_opts, token) - conn_opts = OpenNebula.decrypt(conn_opts, token) conn_opts[:region] = xmlhost["TEMPLATE/REGION_NAME"] conn_opts[:endpoint] = xmlhost["TEMPLATE/AZ_ENDPOINT"] - return conn_opts end diff --git a/src/vmm_mad/remotes/ec2/ec2_driver.rb b/src/vmm_mad/remotes/ec2/ec2_driver.rb index 35b7833b33..367211a342 100755 --- a/src/vmm_mad/remotes/ec2/ec2_driver.rb +++ b/src/vmm_mad/remotes/ec2/ec2_driver.rb @@ -239,7 +239,7 @@ class EC2Driver @instance_types = PUBLIC_CLOUD_EC2_CONF['instance_types'] - conn_opts = get_connect_info(host) + conn_opts = get_connect_info(host, host_id) access_key = conn_opts[:access] secret_key = conn_opts[:secret] region_name = conn_opts[:region] @@ -275,16 +275,21 @@ class EC2Driver # Check the current template of host # to retrieve connection information # needed for Amazon - def get_connect_info(host) + def get_connect_info(host, host_id) conn_opts={} client = OpenNebula::Client.new if host.is_a?(String) - pool = OpenNebula::HostPool.new(client) - pool.info - objects=pool.select {|object| object.name==host } - xmlhost = objects.first + if !host_id + pool = OpenNebula::HostPool.new(client) + pool.info + objects = pool.select {|object| object.name==host } + host_id = objects.first.id + @host_id = host_id + end + xmlhost = OpenNebula::Host.new_with_id(host_id, client) + xmlhost.info(true) else xmlhost = host end @@ -293,8 +298,6 @@ class EC2Driver config = system.get_configuration raise "Error getting oned configuration : #{config.message}" if OpenNebula.is_error?(config) - token = config["ONE_KEY"] - if xmlhost["TEMPLATE/PROVISION"] @tmplBase = 'TEMPLATE/PROVISION' else @@ -309,17 +312,10 @@ class EC2Driver conn_opts = { :access => xmlhost["#{@tmplBase}/EC2_ACCESS"], - :secret => xmlhost["#{@tmplBase}/EC2_SECRET"] + :secret => xmlhost["#{@tmplBase}/EC2_SECRET"], + :region => xmlhost["#{@tmplBase}/REGION_NAME"] } - begin - conn_opts = OpenNebula.decrypt(conn_opts, token) - - conn_opts[:region] = xmlhost["#{@tmplBase}/REGION_NAME"] - rescue - raise "HOST: #{host} must have ec2 credentials and region in order to work properly" - end - return conn_opts end @@ -340,7 +336,6 @@ class EC2Driver # DEPLOY action, also sets ports and ip if needed def deploy(id, host, xml_text, lcm_state, deploy_id) - # Restore if we need to if lcm_state != "BOOT" && lcm_state != "BOOT_FAILURE" restore(deploy_id) diff --git a/src/vmm_mad/remotes/ec2/poll b/src/vmm_mad/remotes/ec2/poll index 3aeeac3b82..6f53b52eaf 100755 --- a/src/vmm_mad/remotes/ec2/poll +++ b/src/vmm_mad/remotes/ec2/poll @@ -43,6 +43,6 @@ id = ARGV[2] begin ec2_drv = EC2Driver.new(host) ec2_drv.poll(id, deploy_id) -rescue Excetion => e +rescue Exception => e OpenNebula.handle_driver_exception("Poll", e, host, deploy_id, id) end diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/vi_client.rb b/src/vmm_mad/remotes/lib/vcenter_driver/vi_client.rb index 87bffd71f3..e757bb7ccd 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/vi_client.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/vi_client.rb @@ -124,29 +124,17 @@ class VIClient begin client = OpenNebula::Client.new host = OpenNebula::Host.new_with_id(host_id, client) - rc = host.info + rc = host.info(true) if OpenNebula.is_error?(rc) raise "Could not get host info for ID: #{host_id} - #{rc.message}" end - password = host["TEMPLATE/VCENTER_PASSWORD"] - - system = OpenNebula::System.new(client) - config = system.get_configuration - if OpenNebula.is_error?(config) - raise "Error getting oned configuration : #{config.message}" - end - - token = config["ONE_KEY"] - - password = VIClient::decrypt(password, token) - connection = { :host => host["TEMPLATE/VCENTER_HOST"], :user => host["TEMPLATE/VCENTER_USER"], :rp => host["TEMPLATE/VCENTER_RESOURCE_POOL"], :ccr => host["TEMPLATE/VCENTER_CCR_REF"], - :password => password + :password => host["TEMPLATE/VCENTER_PASSWORD"] } self.new(connection, host_id) @@ -176,24 +164,16 @@ class VIClient password = "" host_pool.each do |host| if host["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_id - user = host["TEMPLATE/VCENTER_USER"] - password = host["TEMPLATE/VCENTER_PASSWORD"] + host_decrypted = OpenNebula::Host.new_with_id(host["ID"], client) + host_decrypted.info(true) + user = host_decrypted["TEMPLATE/VCENTER_USER"] + password = host_decrypted["TEMPLATE/VCENTER_PASSWORD"] end end if password.empty? or user.empty? raise "Error getting credentials for datastore #{datastore_id}" end - system = OpenNebula::System.new(client) - config = system.get_configuration - if OpenNebula.is_error?(config) - raise "Error getting oned configuration : #{config.message}" - end - - token = config["ONE_KEY"] - - password = VIClient::decrypt(password, token) - connection = { :host => datastore["TEMPLATE/VCENTER_HOST"], :user => user, diff --git a/src/vmm_mad/remotes/one/opennebula_driver.rb b/src/vmm_mad/remotes/one/opennebula_driver.rb index 56e5c503da..2d0e05a7ce 100755 --- a/src/vmm_mad/remotes/one/opennebula_driver.rb +++ b/src/vmm_mad/remotes/one/opennebula_driver.rb @@ -70,13 +70,12 @@ class OpenNebulaDriver host_pool = OpenNebula::HostPool.new(client) host_pool.info objects=host_pool.select {|object| object.name==host_name } - xmlhost = objects.first - host_id = xmlhost["ID"].to_i + host_id = objects.first.id end host = OpenNebula::Host.new_with_id(host_id, client) - host.info - + host.info(true) + region = {} ["user", "password", "endpoint", "capacity"].each do |key| @@ -98,21 +97,7 @@ class OpenNebulaDriver end end - system = OpenNebula::System.new(client) - config = system.get_configuration - raise "Error getting oned configuration : #{config.message}" if OpenNebula.is_error?(config) - - token = config["ONE_KEY"] - conn_opts = { - :password => host["/HOST/TEMPLATE/ONE_PASSWORD"], - } - - begin - conn_opts = OpenNebula.decrypt(conn_opts, token) - region["password"] = conn_opts[:password] - rescue - raise "HOST: #{host} must have remote host password to work properly" - end + region["password"] = host["/HOST/TEMPLATE/ONE_PASSWORD"], secret = "#{region['user']}:#{region['password']}" @@ -399,22 +384,22 @@ class OpenNebulaDriver vms_info << "VM=[\n" vms_info << " ID=\"#{vmid}\",\n" - vms_info << " DEPLOY_ID=\"#{deploy_id}\",\n" - vms_info << " VM_NAME=#{vm.name},\n" - vms_info << " IMPORT_TEMPLATE=\"#{vm_template_to_one}\",\n" - vms_info << " POLL=\"#{poll_data}\" ]\n" - end - - host_info << "USEDMEMORY=#{usedmemory}\n" - host_info << "USEDCPU=#{usedcpu}\n" - host_info << "FREEMEMORY=#{(totalmemory - usedmemory)}\n" - host_info << "FREECPU=#{(totalcpu - usedcpu)}\n" - - puts host_info - puts vms_info + vms_info << " DEPLOY_ID=\"#{deploy_id}\",\n" + vms_info << " VM_NAME=#{vm.name},\n" + vms_info << " IMPORT_TEMPLATE=\"#{vm_template_to_one}\",\n" + vms_info << " POLL=\"#{poll_data}\" ]\n" end - private + host_info << "USEDMEMORY=#{usedmemory}\n" + host_info << "USEDCPU=#{usedcpu}\n" + host_info << "FREEMEMORY=#{(totalmemory - usedmemory)}\n" + host_info << "FREECPU=#{(totalcpu - usedcpu)}\n" + + puts host_info + puts vms_info + end + + private # Get the OpenNebula hybrid section of the template. With more than one section # the HOST element is used and matched with the host diff --git a/src/vn_template/VNTemplate.cc b/src/vn_template/VNTemplate.cc index ef7bbcdb80..583165296f 100644 --- a/src/vn_template/VNTemplate.cc +++ b/src/vn_template/VNTemplate.cc @@ -43,11 +43,6 @@ VNTemplate::VNTemplate(int id, set_umask(umask); } -/* ------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------ */ - -VNTemplate::~VNTemplate(){}; - /* ************************************************************************ */ /* VNTemplate :: Database Access Functions */ /* ************************************************************************ */ @@ -135,14 +130,14 @@ int VNTemplate::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the Object - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -154,7 +149,7 @@ int VNTemplate::insert_replace(SqlDB *db, bool replace, string& error_str) goto error_xml; } - if(replace) + if (replace) { oss << "UPDATE " << table << " SET " << "name = '" << sql_name << "', " @@ -282,3 +277,10 @@ int VNTemplate::from_xml(const string& xml) /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ + +int VNTemplate::post_update_template(string& error_str) +{ + encrypt_all_secrets(obj_template); + + return 0; +} diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 360992cf32..bd41fc7b3c 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -1265,7 +1265,7 @@ void AddressRange::set_vnet(VectorAttribute *nic, const vector &inherit) for (it = inherit.begin(); it != inherit.end(); it++) { - string inherit_val = attr->vector_value((*it).c_str()); + string inherit_val = attr->vector_value(*it); if (!inherit_val.empty()) { diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 132691954d..94d4c64a59 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -514,7 +514,7 @@ int AddressRangePool::free_addr_by_range(unsigned int arid, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRangePool::get_attribute(const char * name, string& value, +void AddressRangePool::get_attribute(const string& name, string& value, int ar_id) const { map::const_iterator it = ar_pool.find(ar_id); @@ -530,7 +530,7 @@ void AddressRangePool::get_attribute(const char * name, string& value, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int AddressRangePool::get_attribute(const char * name, int& value, +int AddressRangePool::get_attribute(const string& name, int& value, int ar_id) const { map::const_iterator it = ar_pool.find(ar_id); diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 54ed9545e8..bae9b910f0 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -67,11 +67,6 @@ VirtualNetwork::VirtualNetwork(int _uid, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -VirtualNetwork::~VirtualNetwork(){}; - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - void VirtualNetwork::get_permissions(PoolObjectAuth& auths) { PoolObjectSQL::get_permissions(auths); @@ -463,6 +458,8 @@ int VirtualNetwork::post_update_template(string& error) one_util::split_unique(sg_str, ',', security_groups); + encrypt_all_secrets(obj_template); + return 0; } @@ -480,14 +477,14 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace, string& error_str) char * sql_xml; - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -855,7 +852,7 @@ int VirtualNetwork::nic_attribute( for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++) { - PoolObjectSQL::get_template_attribute((*it).c_str(), inherit_val); + PoolObjectSQL::get_template_attribute(*it, inherit_val); if (!inherit_val.empty()) { @@ -1225,7 +1222,7 @@ int VirtualNetwork::free_leases(VirtualNetworkTemplate * leases_template, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void VirtualNetwork::get_template_attribute(const char * name, +void VirtualNetwork::get_template_attribute(const string& name, string& value, int ar_id) const { ar_pool.get_attribute(name, value, ar_id); @@ -1239,7 +1236,7 @@ void VirtualNetwork::get_template_attribute(const char * name, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetwork::get_template_attribute(const char * name, int& value, +int VirtualNetwork::get_template_attribute(const string& name, int& value, int ar_id) const { int rc = ar_pool.get_attribute(name, value, ar_id); diff --git a/src/vrouter/VirtualRouter.cc b/src/vrouter/VirtualRouter.cc index 5847aa5c6f..fc40d54780 100644 --- a/src/vrouter/VirtualRouter.cc +++ b/src/vrouter/VirtualRouter.cc @@ -26,7 +26,7 @@ static void vrouter_prefix(VectorAttribute* nic, const string& attr) { string val; - if (nic->vector_value(attr.c_str(), val) == 0) + if (nic->vector_value(attr, val) == 0) { nic->remove(attr); nic->replace("VROUTER_"+attr, val); @@ -89,11 +89,6 @@ VirtualRouter::VirtualRouter( int id, set_umask(_umask); } -/* ------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------ */ - -VirtualRouter::~VirtualRouter(){}; - /* ************************************************************************ */ /* VirtualRouter :: Database Access Functions */ /* ************************************************************************ */ @@ -220,7 +215,7 @@ int VirtualRouter::get_network_leases(string& estr) int num_nics = obj_template->get("NIC",nics); - for(int i=0; iescape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) { @@ -271,7 +266,7 @@ int VirtualRouter::insert_replace(SqlDB *db, bool replace, string& error_str) goto error_xml; } - if(replace) + if (replace) { oss << "UPDATE " << table << " SET " << "name = '" << sql_name << "', " @@ -412,7 +407,7 @@ void VirtualRouter::release_network_leases() int num_nics = get_template_attribute("NIC",nics); - for(int i=0; iget("NIC",nics); - for(int i=0; iclone(); @@ -600,7 +595,7 @@ VectorAttribute * VirtualRouter::attach_nic( obj_template->get("NIC", nics); - for(it = nics.begin(); it != nics.end(); it++) + for (it = nics.begin(); it != nics.end(); it++) { (*it)->vector_value("NIC_ID", nic_id); @@ -687,7 +682,7 @@ VectorAttribute* VirtualRouter::get_nic(int nic_id) const obj_template->get("NIC", nics); - for(nic_it = nics.begin(); nic_it != nics.end(); nic_it++) + for (nic_it = nics.begin(); nic_it != nics.end(); nic_it++) { (*nic_it)->vector_value("NIC_ID", tnic_id); @@ -709,7 +704,7 @@ void VirtualRouter::set_auth_request(int uid, AuthRequest& ar, Template *tmpl, VirtualMachineNics::nic_iterator nic; VirtualMachineNics tnics(tmpl); - for( nic = tnics.begin(); nic != tnics.end(); ++nic) + for (nic = tnics.begin(); nic != tnics.end(); ++nic) { (*nic)->authorize_vrouter(uid, &ar, check_lock); } diff --git a/src/zone/Zone.cc b/src/zone/Zone.cc index 4bbc3c3599..7713969651 100644 --- a/src/zone/Zone.cc +++ b/src/zone/Zone.cc @@ -126,14 +126,14 @@ int Zone::insert_replace(SqlDB *db, bool replace, string& error_str) // Update the zone - sql_name = db->escape_str(name.c_str()); + sql_name = db->escape_str(name); if ( sql_name == 0 ) { goto error_name; } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); + sql_xml = db->escape_str(to_xml(xml_body)); if ( sql_xml == 0 ) {