Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/UPC/ravada
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Dec 9, 2020
2 parents 8303382 + 9764d1e commit fa6e5de
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 20 deletions.
14 changes: 13 additions & 1 deletion lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,11 @@ sub _sql_insert_defaults($self){
,name => 'delay_migrate_back'
,value => 600
}
,{
id_parent => $id_backend
,name => 'display_password'
,value => 1
}
]
);
my %field = ( settings => 'name' );
Expand Down Expand Up @@ -4465,7 +4470,7 @@ Returns the value of a configuration setting
=cut

sub setting($self, $name) {
sub setting($self, $name, $new_value=undef) {
my $sth = $CONNECTOR->dbh->prepare(
"SELECT id,value "
." FROM settings "
Expand All @@ -4482,6 +4487,13 @@ sub setting($self, $name) {

$id_parent = $id;
}
if (defined $new_value && $new_value ne $value ) {
$sth = $CONNECTOR->dbh->prepare(
"UPDATE settings set value=? WHERE id=?"
);
$sth->execute($new_value , $id);
return $new_value;
}
return $value;
}

Expand Down
13 changes: 8 additions & 5 deletions lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,14 @@ sub _around_start($orig, $self, @arg) {
if (!defined $listen_ip) {
my $display_ip;
if ($remote_ip) {
my $set_password = 0;
my $network = Ravada::Network->new(address => $remote_ip);
$set_password = 1 if $network->requires_password();
$display_ip = $self->_listen_ip($remote_ip);
$arg{set_password} = $set_password;
if ( Ravada::setting(undef,"/backend/display_password") ) {
# We'll see if we set it from the network, defaults to 0 meanwhile
my $set_password = 0;
my $network = Ravada::Network->new(address => $remote_ip);
$set_password = 1 if $network->requires_password();
$display_ip = $self->_listen_ip($remote_ip);
$arg{set_password} = $set_password;
}
} else {
$display_ip = $self->_listen_ip();
}
Expand Down
12 changes: 9 additions & 3 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -712,15 +712,15 @@ sub start {
my $listen_ip = ( delete $arg{listen_ip} or $self->_listen_ip);
my $set_password = delete $arg{set_password};

$self->_set_spice_ip($set_password, $listen_ip);

my $is_active = 0;
eval { $is_active = $self->domain->is_active };
warn $@ if $@;
if (!$is_active) {
if (!$is_active && !$self->is_hibernated) {
$self->_check_qcow_format($request);
$self->_set_volumes_backing_store();
$self->_detect_disks_driver();
$self->_set_spice_ip($set_password, $listen_ip);
}

$self->status('starting');
Expand Down Expand Up @@ -1617,6 +1617,8 @@ sub rename_volumes {

sub _set_spice_ip($self, $set_password, $ip=undef) {

return if $self->is_hibernated() || $self->domain->is_active;

my $doc = XML::LibXML->load_xml(string
=> $self->domain->get_xml_description);
my @graphics = $doc->findnodes('/domain/devices/graphics');
Expand All @@ -1642,7 +1644,11 @@ sub _set_spice_ip($self, $set_password, $ip=undef) {
# we should consider in the future add a new listen if it ain't one
next if !$listen;
$listen->setAttribute('address' => ($ip or $self->_vm->listen_ip));
$self->domain->update_device($graphics);
$self->domain->update_device($graphics, Sys::Virt::Domain::DEVICE_MODIFY_CONFIG);

$self->domain->update_device($graphics, Sys::Virt::Domain::DEVICE_MODIFY_LIVE)
if $self->domain->is_active;

}
}

Expand Down
10 changes: 6 additions & 4 deletions lib/Ravada/VM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,13 @@ sub display_ip($self, $value=undef) {
}

sub _set_display_ip($self, $value) {
my %ip_address = $self->_list_ip_address();
if (defined $value && length $value ) {
my %ip_address = $self->_list_ip_address();

confess "Error: $value is not in any interface in node ".$self->name
.". Found ".Dumper(\%ip_address)
if !exists $ip_address{$value};
confess "Error: $value is not in any interface in node ".$self->name
.". Found ".Dumper(\%ip_address)
if !exists $ip_address{$value};
}

$self->_data( display_ip => $value );
}
Expand Down
41 changes: 34 additions & 7 deletions t/kvm/p10_password.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use Test::More;
use lib 't/lib';
use Test::Ravada;

use_ok('Ravada');
no warnings "experimental::signatures";
use feature qw(signatures);

init();
my @VMS = vm_names();
Expand Down Expand Up @@ -113,14 +114,17 @@ sub test_domain_password2 {
return $domain;
}

sub test_domain_password1 {
my $vm_name = shift;
sub test_domain_password1($vm_name, $requires_password=1) {
my $vm = rvd_back->search_vm($vm_name);

my $net2 = Ravada::Network->new(address => '10.0.0.1/32');

ok($net2->requires_password,"Expecting net requires password ")
or return;

if (!$requires_password) {
rvd_back->setting("/backend/display_password" => 0);
}
my $domain = $vm->create_domain( name => new_domain_name
, disk => 1024 * 1024
, id_iso => search_id_iso('Alpine') , id_owner => $USER->id);
Expand All @@ -130,10 +134,15 @@ sub test_domain_password1 {
my $vm2 = rvd_back->search_vm($vm_name);
my $domain2 = $vm2->search_domain($domain->name);
my $password = $domain2->spice_password();
like($password,qr/./,"Expecting a password, got '".($password or '')."'") or die $domain2->name;
if ($requires_password) {
like($password,qr/./,"Expecting a password, got '".($password or '')."'") or die $domain2->name;

$password = $domain->spice_password();
like($password,qr/./,"Expecting a password, got '".($password or '')."'");
} else {
is($password, undef);
}

$password = $domain->spice_password();
like($password,qr/./,"Expecting a password, got '".($password or '')."'");

my $domain_f = rvd_front()->search_domain($domain->name);
my $password_f;
Expand All @@ -142,10 +151,28 @@ sub test_domain_password1 {
is($password_f , $password,"Expecting password : '".($password or '')."'"
." got : '".($password_f or '')."'");

my $domain3 = Ravada::Domain->open($domain->id);
test_password_xml($domain3,$password);

$domain->shutdown_now($USER);

# default is display password = 1
rvd_back->setting("/backend/display_password" => 1);
return $domain;
}

sub test_password_xml($domain, $exp_password) {
my $xml = XML::LibXML->load_xml(string => $domain->domain->get_xml_description(Sys::Virt::Domain::XML_SECURE));
my $found = 0;
for my $graphics ( $xml->findnodes("/domain/devices/graphics") ) {
next if $graphics->getAttribute('type') ne 'spice';
$found++;
is($graphics->getAttribute('passwd'),$exp_password,$domain->name) or exit;
}
ok($found,"Expecting a graphics type='spice' found in ".$domain->name);
}


sub test_any_network_password {
my $vm_name = shift;
my $vm = rvd_back->search_vm($vm_name);
Expand Down Expand Up @@ -373,7 +400,7 @@ SKIP: {
skip($msg,10) if !$vm;

add_network_10();
my $domain1 = test_domain_password1($vm_name);
my $domain1 = test_domain_password1($vm_name, 0);
my $domain2 = test_domain_password2($vm_name);
remove_network_10();

Expand Down
1 change: 1 addition & 0 deletions t/nodes/10_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ sub test_display_ip($vm, $node, $set_localhost_dp=0) {

$node->_data(display_ip => '');
rvd_back->display_ip('') if $set_localhost_dp;
$vm->display_ip('') if $set_localhost_dp;
}

sub test_nat($vm, $node, $set_localhost_natip=0) {
Expand Down
1 change: 1 addition & 0 deletions t/vm/n10_nat.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sub test_route($vm) {
my %route = ( '127.0.0.0/24', '127.0.0.1');
my $routes = `ip route`;
for my $line ( split /\n/, $routes ) {
next if $line =~ / dev tun\d+ /;
my ($network,$ip) = $line =~ /(^[\d+\.\/]+).*src ([\d+\.]+)/;
next if !$network || !$ip;
$route{$network} = $ip;
Expand Down
13 changes: 13 additions & 0 deletions templates/main/admin_settings.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@
<%= l 'Debug' %>
</div>
</div>
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-2">
<label for="debug">Display Password</label>
</div>
<div class="col-md-6">
<input name="debug" ng-model="settings.backend.display_password.value"
ng-true-value="1" ng-false-value="0"
type="checkbox">
<%= l 'Enables display password when available' %>
</div>
</div>


<hr>

Expand Down

0 comments on commit fa6e5de

Please sign in to comment.