Commit 2c44ff48 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

enforcin user for start and shutdown domain

parent 597ee2f2
......@@ -713,7 +713,11 @@ sub _cmd_start {
my $name = $request->args('name');
my $domain = $self->search_domain($name);
die "Unknown domain '$name'" if !$domain;
$domain->start();
my $uid = $request->args('uid');
my $user = Ravada::Auth::SQL->search_by_id($uid);
$domain->start($user);
$request->status('done');
......@@ -743,6 +747,7 @@ sub _cmd_shutdown {
my $request = shift;
$request->status('working');
my $uid = $request->args('uid');
my $name = $request->args('name');
my $timeout = ($request->args('timeout') or 60);
......@@ -750,7 +755,9 @@ sub _cmd_shutdown {
$domain = $self->search_domain($name);
die "Unknown domain '$name'\n" if !$domain;
$domain->shutdown(timeout => $timeout);
my $user = Ravada::Auth::SQL->search_by_id( $uid);
$domain->shutdown(timeout => $timeout, name => $name, user => $user);
}
......
......@@ -101,7 +101,8 @@ sub shutdown {
sub shutdown_now {
my $self = shift;
return $self->shutdown(@_);
my $user = shift;
return $self->shutdown(user => $user);
}
sub start {
......
......@@ -255,7 +255,7 @@ sub domdisplay {
my $req = Ravada::Request->domdisplay($name, $user->id);
$self->wait_request($req, 10);
return if $req->status() ne 'done';
return $req if $req->status() ne 'done';
my $result = $req->result();
return $result->{display};
......@@ -264,8 +264,9 @@ sub domdisplay {
sub start_domain {
my $self = shift;
my $name = shift;
my $user = shift;
return Ravada::Request->start_domain($name);
return Ravada::Request->start_domain(name => $name, uid => $user->id);
}
1;
......@@ -21,6 +21,8 @@ Request a command to the ravada backend
our %FIELD = map { $_ => 1 } qw(error);
our %FIELD_RO = map { $_ => 1 } qw(id name);
our $args_manage = { name => 1 , uid => 1 };
our %VALID_ARG = (
create_domain => {
vm => 1
......@@ -30,15 +32,10 @@ our %VALID_ARG = (
,id_owner => 1
,id_template => 1
}
,remove_domain => {
name => 1
,uid => 1
}
,prepare_base => {
name => 1
,uid => 1
}
,remove_domain => $args_manage
,prepare_base => $args_manage
,shutdown_domain => { name => 1, uid => 1, timeout => 2 }
,start_domain => $args_manage
);
our $CONNECTOR;
......@@ -162,7 +159,7 @@ sub remove_domain {
Requests to start a domain
my $req = Ravada::Request->start_domain( name => 'name' );
my $req = Ravada::Request->start_domain( name => 'name', uid => $user->id );
=cut
......@@ -170,23 +167,38 @@ sub start_domain {
my $proto = shift;
my $class=ref($proto) || $proto;
my $name = shift;
$name = $name->name if ref($name) =~ /Domain/;
my %args = ( name => $name ) or confess "Missing domain name";
my $args = _check_args('start_domain', @_);
my $self = {};
bless($self,$class);
return $self->_new_request(command => 'start' , args => encode_json({ name => $name }));
return $self->_new_request(command => 'start' , args => encode_json($args));
}
sub _check_args {
my $sub = shift;
my $args = { @_ };
for (keys %{$args}) {
confess "Invalid argument $_" if !$VALID_ARG{$sub}->{$_};
}
for (keys %{$VALID_ARG{$sub}}) {
next if $VALID_ARG{$sub}->{$_} == 2; # optional arg
confess "Missing argument $_" if !exists $args->{$_};
}
return $args;
}
=head2 shutdown_domain
Requests to stop a domain
my $req = Ravada::Request->shutdown_domain( 'name' );
my $req = Ravada::Request->shutdown_domain( 'name' , $timeout );
my $req = Ravada::Request->shutdown_domain( name => 'name' , uid => $user->id );
my $req = Ravada::Request->shutdown_domain( name => 'name' , uid => $user->id
,timeout => $timeout );
=cut
......@@ -194,16 +206,14 @@ sub shutdown_domain {
my $proto = shift;
my $class=ref($proto) || $proto;
my $name = shift;
$name = $name->name if ref($name) =~ /Domain/;
my $timeout = ( shift or 10 );
my $args = _check_args('shutdown_domain', @_ );
my %args = ( name => $name, timeout => $timeout ) or confess "Missing domain name";
$args->{timeout} = 10 if !exists $args->{timeout};
my $self = {};
bless($self,$class);
return $self->_new_request(command => 'shutdown' , args => encode_json(\%args));
return $self->_new_request(command => 'shutdown' , args => encode_json($args));
}
=head2 prepare_base
......
......@@ -13,6 +13,7 @@ use Test::Ravada;
my $test = Test::SQL::Data->new(config => 't/etc/sql.conf');
my $RAVADA = rvd_back($test->connector, 't/etc/ravada.conf');
my $USER = create_user('foo','bar', 1);
my @ARG_CREATE_DOM;
......@@ -68,7 +69,8 @@ sub test_start {
my $req = Ravada::Request->start_domain(
"does not exists"
name => "does not exists"
,uid => $USER->id
);
$RAVADA->_process_requests_dont_fork();
......@@ -86,11 +88,11 @@ sub test_start {
my $domain = $RAVADA->search_domain($name);
ok(!$domain->is_active,"Domain $name should be inactive") or return;
my $req2 = Ravada::Request->start_domain($name);
my $req2 = Ravada::Request->start_domain(name => $name, uid => $USER->id);
$RAVADA->process_requests();
ok($req2->status eq 'done');
$domain->start() if !$domain->is_active();
$domain->start($USER) if !$domain->is_active();
ok($domain->is_active);
......@@ -103,14 +105,15 @@ sub test_start {
#
# stop
my $req3 = Ravada::Request->shutdown_domain($name);
my $req3 = Ravada::Request->shutdown_domain(name => $name, uid => $USER->id);
$RAVADA->process_requests();
ok($req3->status eq 'done');
ok(!$req3->error,"Error shutting down domain $name , expecting ''. Got '".$req3->error);
ok(!$domain->is_active);
ok(!$domain->is_active, "Domain $name should not be active");
my $domain3 = $RAVADA->search_domain($name);
ok(!$domain3->is_active);
ok(!$domain3->is_active,"Domain $name should not be active");
return $domain3;
......@@ -126,11 +129,11 @@ my $vmm;
eval {
$vmm = $RAVADA->search_vm('kvm');
@ARG_CREATE_DOM = ( id_iso => 1, vm => 'kvm', id_owner => 1 ) if $vmm;
@ARG_CREATE_DOM = ( id_iso => 1, vm => 'kvm', id_owner => $USER->id ) if $vmm;
if (!$vmm) {
$vmm = $RAVADA->search_vm('lxc');
@ARG_CREATE_DOM = ( id_template => 1, vm => 'LXC', id_owner => 1 );
@ARG_CREATE_DOM = ( id_template => 1, vm => 'LXC', id_owner => $USER->id );
}
} if $RAVADA;
......@@ -144,7 +147,7 @@ SKIP: {
remove_old_disks();
my $domain = test_start();
$domain->shutdown_now() if $domain;
$domain->shutdown_now($USER) if $domain;
$domain->remove(user_admin()) if $domain;
};
done_testing();
......
......@@ -103,13 +103,18 @@ for my $vm_name ('kvm','lxc') {
$domain->name eq $name,"[$vm_name] Expecting domain name $name, got "
.($domain->name or '<UNDEF>'));
$RVD_FRONT->start_domain($name);
$req = $RVD_FRONT->start_domain($name, $USER);
$RVD_FRONT->wait_request($req,10);
ok($req->status('done'),"Request ".$req->status);
ok(!$req->error,"[$vm_name] Request start domain expecting no error, got '".$req->error
."'") or exit;
ok($domain->is_active,"[$vm_name] Expecting domain $name active, got ".$domain->is_active)
or exit;
my $display = $RVD_FRONT->domdisplay($name, $USER);
ok($display,"No display for domain $name found. Is it active ?");
ok($display && $display =~ m{\w+://.*?:\d+},"Expecting display a URL, it is '"
ok($display,"[$vm_name] No display for domain $name found. Is it active ?");
ok($display && $display =~ m{\w+://.*?:\d+},"[$vm_name] Expecting display a URL, it is '"
.($display or '<UNDEF>')
."'");
......
......@@ -71,12 +71,12 @@ sub test_start_domain {
my $domain_f = $RVD_FRONT->search_domain($name);
ok($domain_f,"Domain $name should be in frontend");
eval { $domain_b->shutdown};
eval { $domain_b->shutdown(user => $USER)};
ok(!$@,"[$vm_name] Start domain $name expecting error: '' , got $@");
ok(!$domain_f->is_active);
eval { $domain_f->start( ) };
eval { $domain_f->start($USER ) };
ok($@,"[$vm_name] Start should be denied from front ");
ok(!$domain_f->is_active,"[$vm_name] Domain should be active");
......@@ -87,7 +87,7 @@ sub test_start_domain {
ok($@,"[$vm_name] domain->create should be denied from front ");
}
eval { $domain_b->start() };
eval { $domain_b->start($USER) };
ok(!$@,$@);
ok($domain_f->is_active);# && !$domain_f->is_active);
......@@ -123,7 +123,7 @@ sub test_shutdown_domain {
ok($@,"[$vm_name] Shutdown should be denied from front ");
}
eval { $domain_b->shutdown(force => 1) };
eval { $domain_b->shutdown(user => $USER,force => 1) };
ok(!$@,$@);
ok(!$domain_f->is_active);# && !$domain_f->is_active);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment