F #1401: Configurable VM_*_OPERATIONS per group and user. Restructure of the API

authorization levels and initialization logic.

Co-authored-by: Pavel Czerny <pczerny@opennebula.systems>
This commit is contained in:
Ruben S. MonteroandPavel Czerny committed 2019-09-09 13:13:52 +02:00
1 parent 332560d38f
commit 500e45dbd6
58 files changed
+1442 -1130

No files matched your search

+87 -96
View File
@@ -30,8 +30,7 @@ bool RequestManagerVirtualMachine::vm_authorization(
RequestAttributes& att,
PoolObjectAuth * host_perm,
PoolObjectAuth * ds_perm,
PoolObjectAuth * img_perm,
AuthRequest::Operation op)
PoolObjectAuth * img_perm)
{
PoolObjectSQL * object;
PoolObjectAuth vm_perms;
@@ -52,7 +51,7 @@ bool RequestManagerVirtualMachine::vm_authorization(
AuthRequest ar(att.uid, att.group_ids);
ar.add_auth(op, vm_perms);
ar.add_auth(att.auth_op, vm_perms);
if (host_perm != nullptr)
{
@@ -496,8 +495,7 @@ void VirtualMachineAction::request_execute(xmlrpc_c::paramList const& paramList,
ostringstream oss;
string error;
AuthRequest::Operation op;
History::VMAction action;
VMActions::Action action;
VirtualMachineTemplate quota_tmpl;
@@ -513,11 +511,12 @@ void VirtualMachineAction::request_execute(xmlrpc_c::paramList const& paramList,
action_st = "terminate";
}
History::action_from_str(action_st, action);
VMActions::action_from_str(action_st, action);
op = nd.get_vm_auth_op(action);
// Update the authorization level for the action
att.set_auth_op(action);
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -527,21 +526,7 @@ void VirtualMachineAction::request_execute(xmlrpc_c::paramList const& paramList,
return;
}
vm->get_template_attribute("MEMORY", memory);
vm->get_template_attribute("CPU", cpu);
quota_tmpl.add("RUNNING_MEMORY", memory);
quota_tmpl.add("RUNNING_CPU", cpu);
quota_tmpl.add("RUNNING_VMS", 1);
quota_tmpl.add("VMS", 0);
quota_tmpl.add("MEMORY", 0);
quota_tmpl.add("CPU", 0);
RequestAttributes& att_aux(att);
att_aux.uid = vm->get_uid();
att_aux.gid = vm->get_gid();
// Check if the action is supported for imported VMs
if (vm->is_imported() && !vm->is_imported_action_supported(action))
{
att.resp_msg = "Action \"" + action_st + "\" is not supported for "
@@ -553,11 +538,30 @@ void VirtualMachineAction::request_execute(xmlrpc_c::paramList const& paramList,
return;
}
// Generate quota information for resume action
RequestAttributes& att_aux(att);
if (action == VMActions::RESUME_ACTION)
{
vm->get_template_attribute("MEMORY", memory);
vm->get_template_attribute("CPU", cpu);
quota_tmpl.add("RUNNING_MEMORY", memory);
quota_tmpl.add("RUNNING_CPU", cpu);
quota_tmpl.add("RUNNING_VMS", 1);
quota_tmpl.add("VMS", 0);
quota_tmpl.add("MEMORY", 0);
quota_tmpl.add("CPU", 0);
att_aux.uid = vm->get_uid();
att_aux.gid = vm->get_gid();
}
vm->unlock();
if ( action == History::RESUME_ACTION &&
!quota_authorization(&quota_tmpl, Quotas::VIRTUALMACHINE, att_aux,
att.resp_msg) )
if (action == VMActions::RESUME_ACTION && !quota_authorization(&quota_tmpl,
Quotas::VIRTUALMACHINE, att_aux, att.resp_msg))
{
failure_response(ACTION, att);
return;
@@ -565,49 +569,49 @@ void VirtualMachineAction::request_execute(xmlrpc_c::paramList const& paramList,
switch (action)
{
case History::TERMINATE_ACTION:
case VMActions::TERMINATE_ACTION:
rc = dm->terminate(id, false, att, error);
break;
case History::TERMINATE_HARD_ACTION:
case VMActions::TERMINATE_HARD_ACTION:
rc = dm->terminate(id, true, att, error);
break;
case History::HOLD_ACTION:
case VMActions::HOLD_ACTION:
rc = dm->hold(id, att, error);
break;
case History::RELEASE_ACTION:
case VMActions::RELEASE_ACTION:
rc = dm->release(id, att, error);
break;
case History::STOP_ACTION:
case VMActions::STOP_ACTION:
rc = dm->stop(id, att, error);
break;
case History::SUSPEND_ACTION:
case VMActions::SUSPEND_ACTION:
rc = dm->suspend(id, att, error);
break;
case History::RESUME_ACTION:
case VMActions::RESUME_ACTION:
rc = dm->resume(id, att, error);
break;
case History::REBOOT_ACTION:
case VMActions::REBOOT_ACTION:
rc = dm->reboot(id, false, att, error);
break;
case History::REBOOT_HARD_ACTION:
case VMActions::REBOOT_HARD_ACTION:
rc = dm->reboot(id, true, att, error);
break;
case History::RESCHED_ACTION:
case VMActions::RESCHED_ACTION:
rc = dm->resched(id, true, att, error);
break;
case History::UNRESCHED_ACTION:
case VMActions::UNRESCHED_ACTION:
rc = dm->resched(id, false, att, error);
break;
case History::POWEROFF_ACTION:
case VMActions::POWEROFF_ACTION:
rc = dm->poweroff(id, false, att, error);
break;
case History::POWEROFF_HARD_ACTION:
case VMActions::POWEROFF_HARD_ACTION:
rc = dm->poweroff(id, true, att, error);
break;
case History::UNDEPLOY_ACTION:
case VMActions::UNDEPLOY_ACTION:
rc = dm->undeploy(id, false, att, error);
break;
case History::UNDEPLOY_HARD_ACTION:
case VMActions::UNDEPLOY_HARD_ACTION:
rc = dm->undeploy(id, true, att, error);
break;
default:
@@ -702,7 +706,7 @@ int set_vnc_port(VirtualMachine *vm, int cluster_id, RequestAttributes& att)
{
return 0;
}
else if (vm->hasHistory() && vm->get_action()==History::STOP_ACTION)
else if (vm->hasHistory() && vm->get_action()==VMActions::STOP_ACTION)
{
return 0;
}
@@ -761,6 +765,7 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
bool auth = false;
bool check_nic_auto = false;
// ------------------------------------------------------------------------
// Get request parameters and information about the target host
// ------------------------------------------------------------------------
@@ -815,9 +820,9 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
}
if (vm->hasHistory() &&
(vm->get_action() == History::STOP_ACTION ||
vm->get_action() == History::UNDEPLOY_ACTION ||
vm->get_action() == History::UNDEPLOY_HARD_ACTION))
(vm->get_action() == VMActions::STOP_ACTION ||
vm->get_action() == VMActions::UNDEPLOY_ACTION ||
vm->get_action() == VMActions::UNDEPLOY_HARD_ACTION))
{
ds_id = vm->get_ds_id();
@@ -924,13 +929,11 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
return;
}
auth = vm_authorization(id, 0, &tmpl, att, &host_perms, auth_ds_perms,0,
auth_op);
auth = vm_authorization(id, 0, &tmpl, att, &host_perms, auth_ds_perms,0);
}
else
{
auth = vm_authorization(id, 0, 0, att, &host_perms, auth_ds_perms, 0,
auth_op);
auth = vm_authorization(id, 0, 0, att, &host_perms, auth_ds_perms, 0);
}
if (auth == false)
@@ -1068,7 +1071,7 @@ void VirtualMachineMigrate::request_execute(xmlrpc_c::paramList const& paramList
string error;
History::VMAction action;
VMActions::Action action;
// ------------------------------------------------------------------------
// Get request parameters and information about the target host
@@ -1134,8 +1137,7 @@ void VirtualMachineMigrate::request_execute(xmlrpc_c::paramList const& paramList
// ------------------------------------------------------------------------
// Authorize request
// ------------------------------------------------------------------------
auth = vm_authorization(id, 0, 0, att, &host_perms, auth_ds_perms, 0, auth_op);
auth = vm_authorization(id, 0, 0, att, &host_perms, auth_ds_perms, 0);
if (auth == false)
{
@@ -1174,7 +1176,7 @@ void VirtualMachineMigrate::request_execute(xmlrpc_c::paramList const& paramList
if (live)
{
action = History::LIVE_MIGRATE_ACTION;
action = VMActions::LIVE_MIGRATE_ACTION;
if ( vm->is_pinned() )
{
@@ -1187,7 +1189,7 @@ void VirtualMachineMigrate::request_execute(xmlrpc_c::paramList const& paramList
}
else
{
action = History::MIGRATE_ACTION;
action = VMActions::MIGRATE_ACTION;
}
if (vm->is_imported() && !vm->is_imported_action_supported(action))
@@ -1589,7 +1591,7 @@ void VirtualMachineDiskSaveas::request_execute(
// -------------------------------------------------------------------------
// Authorize the operation & check quotas
// -------------------------------------------------------------------------
rc_auth = vm_authorization(id, itemplate, 0, att, 0,&ds_perms,0,auth_op);
rc_auth = vm_authorization(id, itemplate, 0, att, 0, &ds_perms, 0);
if ( rc_auth == true )
{
@@ -1720,7 +1722,7 @@ void VirtualMachineMonitoring::request_execute(
string oss;
bool auth = vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op);
bool auth = vm_authorization(id, 0, 0, att, 0, 0, 0);
if ( auth == false )
{
@@ -1829,8 +1831,7 @@ Request::ErrorCode VirtualMachineAttach::request_execute(int id,
// -------------------------------------------------------------------------
// Authorize the operation & check quotas
// -------------------------------------------------------------------------
if ( vm_authorization(id, 0, &tmpl, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, &tmpl, att, 0, 0, 0) == false)
{
return AUTHORIZATION;
}
@@ -1914,8 +1915,7 @@ void VirtualMachineDetach::request_execute(xmlrpc_c::paramList const& paramList,
// -------------------------------------------------------------------------
// Authorize the operation
// -------------------------------------------------------------------------
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2038,7 +2038,7 @@ void VirtualMachineResize::request_execute(xmlrpc_c::paramList const& paramList,
/* ---------------------------------------------------------------------- */
/* Authorize the operation & restricted attributes */
/* ---------------------------------------------------------------------- */
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2217,8 +2217,7 @@ void VirtualMachineSnapshotCreate::request_execute(
// -------------------------------------------------------------------------
// Authorize the operation
// -------------------------------------------------------------------------
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2255,8 +2254,7 @@ void VirtualMachineSnapshotRevert::request_execute(
// -------------------------------------------------------------------------
// Authorize the operation
// -------------------------------------------------------------------------
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2293,8 +2291,7 @@ void VirtualMachineSnapshotDelete::request_execute(
// -------------------------------------------------------------------------
// Authorize the operation
// -------------------------------------------------------------------------
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2560,23 +2557,21 @@ void VirtualMachineRecover::request_execute(
Nebula& nd = Nebula::instance();
DispatchManager * dm = nd.get_dm();
AuthRequest::Operation aop;
switch (op)
{
case 0: //recover-failure
case 1: //recover-success
aop = nd.get_vm_auth_op(History::RECOVER_ACTION);
att.set_auth_op(VMActions::RECOVER_ACTION);
break;
case 2: //retry
aop = nd.get_vm_auth_op(History::RETRY_ACTION);
att.set_auth_op(VMActions::RETRY_ACTION);
break;
case 3: //delete
case 4: //delete-recreate set same as delete in OpenNebulaTemplate
case 5: //delete-db
aop = nd.get_vm_auth_op(History::DELETE_ACTION);
att.set_auth_op(VMActions::DELETE_ACTION);
break;
default:
@@ -2585,7 +2580,7 @@ void VirtualMachineRecover::request_execute(
return;
}
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, aop) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2751,14 +2746,10 @@ void VirtualMachineDiskSnapshotCreate::request_execute(
}
/* ---------- Attributes for quota update requests ---------------------- */
RequestAttributes img_att_quota;
RequestAttributes vm_att_quota;
PoolObjectAuth img_perms;
if (img_ds_quota)
{
PoolObjectAuth img_perms;
Image* img = ipool->get_ro(img_id);
if (img == nullptr)
@@ -2774,19 +2765,21 @@ void VirtualMachineDiskSnapshotCreate::request_execute(
img->unlock();
if (vm_authorization(id, 0, 0, att, 0, 0, &img_perms, auth_op) == false)
if (vm_authorization(id, 0, 0, att, 0, 0, &img_perms) == false)
{
return;
}
img_att_quota = RequestAttributes(img_perms.uid, img_perms.gid, att);
}
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
vm_att_quota = RequestAttributes(vm_perms.uid, vm_perms.gid, att);
RequestAttributes vm_att_quota = RequestAttributes(vm_perms.uid,
vm_perms.gid, att);
RequestAttributes img_att_quota = RequestAttributes(img_perms.uid,
img_perms.gid, att);
/* ---------------------------------------------------------------------- */
/* Check quotas for the new size in image/system datastoress */
@@ -2874,7 +2867,7 @@ void VirtualMachineDiskSnapshotRevert::request_execute(
int did = xmlrpc_c::value_int(paramList.getInt(2));
int snap_id = xmlrpc_c::value_int(paramList.getInt(3));
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2955,12 +2948,12 @@ void VirtualMachineDiskSnapshotDelete::request_execute(
img->unlock();
if (vm_authorization(id, 0, 0, att, 0, 0, &img_perms, auth_op) == false)
if (vm_authorization(id, 0, 0, att, 0, 0, &img_perms) == false)
{
return;
}
}
else if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
else if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -2998,7 +2991,7 @@ void VirtualMachineDiskSnapshotRename::request_execute(xmlrpc_c::paramList const
int snap_id = xmlrpc_c::value_int(paramList.getInt(3));
string new_snap_name = xmlrpc_c::value_string(paramList.getString(4));
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -3072,7 +3065,7 @@ void VirtualMachineUpdateConf::request_execute(
/* ---------------------------------------------------------------------- */
/* Authorize the operation & restricted attributes */
/* ---------------------------------------------------------------------- */
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
@@ -3260,13 +3253,10 @@ void VirtualMachineDiskResize::request_execute(
/* ---------------------------------------------------------------------- */
/* Authorize the request for VM and IMAGE for persistent disks */
/* ---------------------------------------------------------------------- */
RequestAttributes img_att_quota;
RequestAttributes vm_att_quota;
PoolObjectAuth img_perms;
if ( img_ds_quota )
{
PoolObjectAuth img_perms;
if ( img_id != -1 )
{
Image* img = ipool->get_ro(img_id);
@@ -3285,23 +3275,24 @@ void VirtualMachineDiskResize::request_execute(
img->unlock();
}
if (vm_authorization(id, 0, 0, att, 0, 0, &img_perms, auth_op) == false)
if (vm_authorization(id, 0, 0, att, 0, 0, &img_perms) == false)
{
return;
}
img_att_quota = RequestAttributes(img_perms.uid, img_perms.gid, att);
}
if ( vm_ds_quota )
{
if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false )
if (vm_authorization(id, 0, 0, att, 0, 0, 0) == false)
{
return;
}
}
vm_att_quota = RequestAttributes(vm_perms.uid, vm_perms.gid, att);
RequestAttributes img_att_quota = RequestAttributes(img_perms.uid,
img_perms.gid, att);
RequestAttributes vm_att_quota = RequestAttributes(vm_perms.uid,
vm_perms.gid, att);
/* ---------------------------------------------------------------------- */
/* Check quotas for the new size in image/system datastoress */