1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

Merge branch 'feature-200' of opennebula.org:one into feature-200

This commit is contained in:
Jaime Melis 2010-07-01 13:26:29 +02:00
commit d150ea5d67
14 changed files with 319 additions and 163 deletions

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "ImageTemplate.h"
#include "NebulaLog.h"
using namespace std;
@ -367,6 +368,14 @@ private:
db->exec(oss_templ);
};
/**
* "Encrypts" the password with SHA1 digest
* @param password
* @return sha1 encrypted password
*/
string sha1_digest(const string& pass);
protected:
// *************************************************************************

View File

@ -194,26 +194,47 @@ public:
return rc;
}
private:
/**
* This map stores the association between IIDs and Image names
*/
map<string, int> image_names;
static const string& source_prefix()
{
return _source_prefix;
};
static const string& default_type()
{
return _default_type;
};
static const string& default_dev_prefix()
{
return _default_dev_prefix;
};
private:
//--------------------------------------------------------------------------
// Configuration Attributes for Images
// -------------------------------------------------------------------------
/**
* Path to the image repository
**/
string source_prefix;
static string _source_prefix;
/**
* Default image type
**/
string default_type;
static string _default_type;
/**
* Default device prefix
**/
string default_dev_prefix;
static string _default_dev_prefix;
//--------------------------------------------------------------------------
// Pool Attributes
// -------------------------------------------------------------------------
/**
* This map stores the association between IIDs and Image names
*/
map<string, int> image_names;
/**
* Factory method to produce Image objects
@ -243,12 +264,6 @@ private:
*/
int init_cb(void *nil, int num, char **values, char **names);
/**
* "Encrypts" the password with SHA1 digest
* @param password
* @return sha1 encrypted password
*/
string sha1_digest(const string& pass);
};
#endif /*IMAGE_POOL_H_*/

View File

@ -19,6 +19,8 @@
#include <iostream>
#include <sstream>
#include <openssl/evp.h>
#include <iomanip>
#include "Image.h"
#include "ImagePool.h"
@ -137,6 +139,76 @@ int Image::insert(SqlDB *db)
{
int rc;
string source_att;
string type_att;
string public_attr;
string dev_prefix;
ostringstream tmp_hashstream;
ostringstream tmp_sourcestream;
// ---------------------------------------------------------------------
// Check default image attributes
// ---------------------------------------------------------------------
// ------------ NAME --------------------
get_template_attribute("NAME", name);
if ( name.empty() == true )
{
goto error_name;
}
// ------------ TYPE --------------------
get_template_attribute("TYPE", type_att);
transform (type_att.begin(), type_att.end(), type_att.begin(),
(int(*)(int))toupper);
if ( type_att.empty() == true )
{
type_att = ImagePool::default_type();
}
if (set_type(type_att) != 0)
{
goto error_type;
}
// ------------ PUBLIC --------------------
get_template_attribute("PUBLIC", public_attr);
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
(int(*)(int))toupper);
public_img = (public_attr == "YES");
// ------------ PREFIX --------------------
get_template_attribute("DEV_PREFIX", dev_prefix);
if( dev_prefix.empty() )
{
SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX",
ImagePool::default_dev_prefix());
image_template.set(dev_att);
}
// ------------ SOURCE (path to store the image)--------------------
tmp_hashstream << uid << ":" << name;
tmp_sourcestream << ImagePool::source_prefix() << "/";
tmp_sourcestream << sha1_digest(tmp_hashstream.str());
source = tmp_sourcestream.str();
// Set up the template ID, to insert it
if ( image_template.id == -1 )
{
@ -164,6 +236,15 @@ int Image::insert(SqlDB *db)
}
return 0;
error_name:
NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template");
goto error_common;
error_type:
NebulaLog::log("IMG", Log::ERROR, "Incorrect TYPE in image template");
goto error_common;
error_common:
return -1;
}
/* ------------------------------------------------------------------------ */
@ -173,14 +254,6 @@ int Image::update(SqlDB *db)
{
int rc;
// Update the Template
rc = image_template.update(db);
if ( rc != 0 )
{
return rc;
}
rc = insert_replace(db, true);
if ( rc != 0 )
@ -591,6 +664,33 @@ int Image::disk_attribute(VectorAttribute * disk, int * index)
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string Image::sha1_digest(const string& pass)
{
EVP_MD_CTX mdctx;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
ostringstream oss;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, EVP_sha1(), NULL);
EVP_DigestUpdate(&mdctx, pass.c_str(), pass.length());
EVP_DigestFinal_ex(&mdctx,md_value,&md_len);
EVP_MD_CTX_cleanup(&mdctx);
for(unsigned int i = 0; i<md_len; i++)
{
oss << setfill('0') << setw(2) << hex << nouppercase
<< (unsigned short) md_value[i];
}
return oss.str();
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

View File

@ -19,11 +19,12 @@
/* ************************************************************************** */
#include "ImagePool.h"
#include <openssl/evp.h>
#include <iomanip>
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string ImagePool::_source_prefix;
string ImagePool::_default_type;
string ImagePool::_default_dev_prefix;
int ImagePool::init_cb(void *nil, int num, char **values, char **names)
{
@ -40,19 +41,22 @@ int ImagePool::init_cb(void *nil, int num, char **values, char **names)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ImagePool::ImagePool( SqlDB * db,
const string& _source_prefix,
const string& _default_type,
const string& _default_dev_prefix):
PoolSQL(db,Image::table),
source_prefix(_source_prefix),
default_type(_default_type),
default_dev_prefix(_default_dev_prefix)
ImagePool::ImagePool( SqlDB * db,
const string& __source_prefix,
const string& __default_type,
const string& __default_dev_prefix):
PoolSQL(db,Image::table)
{
ostringstream sql;
int rc;
// Init static defaults
_source_prefix = __source_prefix;
_default_type = __default_type;
_default_dev_prefix = __default_dev_prefix;
// Set default type
if (_default_type != "OS" &&
_default_type != "CDROM" &&
@ -60,7 +64,7 @@ ImagePool::ImagePool( SqlDB * db,
{
NebulaLog::log("IMG", Log::ERROR,
"Bad default for image type, setting OS");
default_type = "OS";
_default_type = "OS";
}
// Read from the DB the existing images, and build the ID:Name map
@ -87,18 +91,11 @@ int ImagePool::allocate (
const string& stemplate,
int * oid)
{
Image * img;
string name;
string source;
string type;
string public_attr;
string dev_prefix;
char * error_msg;
int rc;
Image * img;
ostringstream tmp_hashstream;
ostringstream tmp_sourcestream;
string name;
char * error_msg;
// ---------------------------------------------------------------------
// Build a new Image object
@ -115,68 +112,8 @@ int ImagePool::allocate (
goto error_parse;
}
// ---------------------------------------------------------------------
// Check default image attributes
// ---------------------------------------------------------------------
// ------------ NAME --------------------
img->get_template_attribute("NAME", name);
if ( name.empty() == true )
{
goto error_name;
}
img->name = name;
// ------------ TYPE --------------------
img->get_template_attribute("TYPE", type);
transform (type.begin(), type.end(), type.begin(),
(int(*)(int))toupper);
if ( type.empty() == true )
{
type = default_type;
}
if (img->set_type(type) != 0)
{
goto error_type;
}
// ------------ PUBLIC --------------------
img->get_template_attribute("PUBLIC", public_attr);
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
(int(*)(int))toupper);
img->public_img = (public_attr == "YES");
// ------------ PREFIX --------------------
img->get_template_attribute("DEV_PREFIX", dev_prefix);
if( dev_prefix.empty() )
{
SingleAttribute * dev_att =
new SingleAttribute("DEV_PREFIX", default_dev_prefix);
img->image_template.set(dev_att);
}
// ------------ SOURCE (path to store the image)--------------------
tmp_hashstream << uid << ":" << name;
tmp_sourcestream << source_prefix << "/";
tmp_sourcestream << sha1_digest(tmp_hashstream.str());
img->source = tmp_sourcestream.str();
// ---------------------------------------------------------------------
// Insert the Object in the pool
// ---------------------------------------------------------------------
@ -196,17 +133,6 @@ int ImagePool::allocate (
return *oid;
error_name:
NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template");
goto error_common;
error_type:
NebulaLog::log("IMG", Log::ERROR, "Incorrect TYPE in image template");
goto error_common;
error_common:
delete img;
*oid = -1;
return -1;
error_parse:
ostringstream oss;
oss << "ImagePool template parse error: " << error_msg;
@ -263,30 +189,3 @@ int ImagePool::dump(ostringstream& oss, const string& where)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string ImagePool::sha1_digest(const string& pass)
{
EVP_MD_CTX mdctx;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
ostringstream oss;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, EVP_sha1(), NULL);
EVP_DigestUpdate(&mdctx, pass.c_str(), pass.length());
EVP_DigestFinal_ex(&mdctx,md_value,&md_len);
EVP_MD_CTX_cleanup(&mdctx);
for(unsigned int i = 0; i<md_len; i++)
{
oss << setfill('0') << setw(2) << hex << nouppercase
<< (unsigned short) md_value[i];
}
return oss.str();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -85,6 +85,7 @@ class ImagePoolTest : public PoolTest
CPPUNIT_TEST ( target_generation );
CPPUNIT_TEST ( bus_source_assignment );
CPPUNIT_TEST ( public_attribute );
CPPUNIT_TEST ( disk_overwrite );
CPPUNIT_TEST ( dump );
CPPUNIT_TEST ( dump_where );
@ -156,6 +157,7 @@ protected:
delete user_pool;
};
public:
ImagePoolTest(){};
@ -292,6 +294,8 @@ public:
check(1, obj);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void wrong_get_name()
{
@ -548,6 +552,136 @@ public:
delete disk;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void disk_overwrite()
{
ImagePool * imp = static_cast<ImagePool *>(pool);
Image * img;
VectorAttribute * disk;
int oid, rc;
string value;
int index = 0;
// ---------------------------------------------------------------------
// Allocate an OS type image
oid = allocate(0);
CPPUNIT_ASSERT( oid > -1 );
img = imp->get(oid, false);
// Disk with overwrite=yes, save_as empty
disk = new VectorAttribute("DISK");
disk->replace("OVERWRITE", "yes");
img->enable(true);
rc = img->disk_attribute(disk, &index);
CPPUNIT_ASSERT( rc == 0 );
value = disk->vector_value("OVERWRITE");
CPPUNIT_ASSERT( value == "YES" );
value = "";
value = disk->vector_value("SAVE_AS");
CPPUNIT_ASSERT( value == "" );
value = "";
value = disk->vector_value("CLONE");
CPPUNIT_ASSERT( value == "NO" );
value = "";
value = disk->vector_value("SAVE");
CPPUNIT_ASSERT( value == "YES" );
value = "";
value = disk->vector_value("READONLY");
CPPUNIT_ASSERT( value == "NO" );
// clean up
delete disk;
// ---------------------------------------------------------------------
// Allocate an OS type image
oid = allocate(1);
CPPUNIT_ASSERT( oid > -1 );
img = imp->get(oid, false);
// Disk with overwrite=no, save_as not empty
disk = new VectorAttribute("DISK");
disk->replace("OVERWRITE", "NO");
disk->replace("SAVE_AS", "path_to_save");
img->enable(true);
rc = img->disk_attribute(disk, &index);
CPPUNIT_ASSERT( rc == 0 );
value = "";
value = disk->vector_value("OVERWRITE");
CPPUNIT_ASSERT( value == "NO" );
value = "";
value = disk->vector_value("SAVE_AS");
CPPUNIT_ASSERT( value == "path_to_save" );
value = "";
value = disk->vector_value("CLONE");
CPPUNIT_ASSERT( value == "YES" );
value = "";
value = disk->vector_value("SAVE");
CPPUNIT_ASSERT( value == "YES" );
value = "";
value = disk->vector_value("READONLY");
CPPUNIT_ASSERT( value == "NO" );
// clean up
delete disk;
// ---------------------------------------------------------------------
// Allocate an OS type image
oid = allocate(2);
CPPUNIT_ASSERT( oid > -1 );
img = imp->get(oid, false);
// Disk with overwrite=no, save_as not present
disk = new VectorAttribute("DISK");
disk->replace("OVERWRITE", "NO");
img->enable(true);
rc = img->disk_attribute(disk, &index);
CPPUNIT_ASSERT( rc == 0 );
value = "";
value = disk->vector_value("OVERWRITE");
CPPUNIT_ASSERT( value == "NO" );
value = "";
value = disk->vector_value("SAVE_AS");
CPPUNIT_ASSERT( value == "" );
value = "";
value = disk->vector_value("CLONE");
CPPUNIT_ASSERT( value == "YES" );
value = "";
value = disk->vector_value("SAVE");
CPPUNIT_ASSERT( value == "NO" );
value = "";
value = disk->vector_value("READONLY");
CPPUNIT_ASSERT( value == "NO" );
// clean up
delete disk;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -104,10 +104,12 @@ error_image_get:
error_authorization:
oss << "[ImageEnable] User not authorized to enable/disable image" <<
" attributes, aborting call.";
image->unlock();
goto error_common;
error_enable:
oss << "[ImageEnable] Cannot enable/disable image [" << iid << "]";
image->unlock();
goto error_common;
error_common:

View File

@ -84,6 +84,7 @@ error_image_get:
error_authenticate:
oss << "User doesn't exist, or not authorized to use image with " <<
"ID = " << iid << " , ImageInfo call aborted.";
image->unlock();
goto error_common;
error_common:

View File

@ -98,6 +98,7 @@ error_image_get:
error_authorization:
oss << "[ImagePublish] User not authorized to publish/unpublish image" <<
", aborting call.";
image->unlock();
goto error_common;
error_common:

View File

@ -103,11 +103,13 @@ error_image_get:
error_authorization:
oss << "[ImageRemoveAttribute] User not authorized to remove image" <<
" attributes aborting call.";
image->unlock();
goto error_common;
error_remove_attribute:
oss << "[ImageRemoveAttribute] Cannot remove attribute with name = "
<< name << " for image [" << iid << "]";
image->unlock();
goto error_common;
error_common:

View File

@ -109,10 +109,12 @@ error_image_get:
error_authorization:
oss << "User not authorized to modify image attributes " <<
", aborting ImageUpdate call.";
image->unlock();
goto error_common;
error_update:
oss << "Cannot modify image [" << iid << "] attribute with name = " << name;
image->unlock();
goto error_common;
error_common:

View File

@ -98,6 +98,7 @@ error_vn_get:
error_authorization:
oss << "[VirtualNetworkPublish] User not authorized to (un)publish VN" <<
", aborting call.";
vn->unlock();
goto error_common;
error_common:

View File

@ -50,19 +50,8 @@ int TemplateSQL::insert_cb(void *nil, int num, char **values, char **names)
int TemplateSQL::insert(SqlDB * db)
{
ostringstream oss;
int rc;
// Get next id from the DB table
set_callback(
static_cast<Callbackable::Callback>(&TemplateSQL::insert_cb));
oss << "SELECT MAX(id) FROM " << table;
rc = db->exec(oss,this);
unset_callback();
if ( rc != 0 )
{
return -1;

View File

@ -254,11 +254,23 @@ int VirtualNetwork::insert(SqlDB * db)
{
ostringstream ose;
int rc;
string public_attr;
if ( vn_template.id == -1 )
{
vn_template.id = oid;
}
// ------------ PUBLIC --------------------
get_template_attribute("PUBLIC", public_attr);
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
(int(*)(int))toupper);
public_vnet = (public_attr == "YES");
vn_template.erase("PUBLIC");
// Insert the template first
rc = vn_template.insert(db);

View File

@ -72,7 +72,6 @@ int VirtualNetworkPool::allocate (
string name;
string bridge;
string public_attr;
string s_type;
@ -122,16 +121,6 @@ int VirtualNetworkPool::allocate (
vn->get_template_attribute("BRIDGE",bridge);
vn->bridge = bridge;
// ------------ PUBLIC --------------------
vn->get_template_attribute("PUBLIC", public_attr);
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
(int(*)(int))toupper);
vn->public_vnet = (public_attr == "YES");
vn->vn_template.erase("PUBLIC");
// Insert the VN in the pool so we have a valid OID
*oid = PoolSQL::allocate(vn);