secureimageget.in 6.82 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008-2017, 2019 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Date::Parse;
use Data::Dumper;

#
37 38 39 40 41 42 43 44 45
# Use a Geni XMLrpc call to download an image. This is a specially
# crafted RPC on the server side; instead of returning an XML response,
# it just starts streams the data out, we have to capture it and
# write to a file.
#
# The credential is for the image (or dataset) at the remote cluster.
# We have to tell the remote cluster what version we want. We use the
# origin_name in the image descriptor to generate one.
#
46 47 48 49 50 51
sub usage()
{
    print "Usage: secureimageget [-n] [-d] [-u user] ".
	"[-c credfile] -o outfile imageid\n";
    exit(1);
}
52
my $optlist  = "ndvc:u:o:D";
53
my $impotent = 0;
54
my $debug    = 1;
55
my $verbose  = 0;
56
my $devtree  = 0;
57 58 59 60 61
my $credfile;
my $outfile;
my $outfp;
my $user;
my $credential;
62
my $image_urn;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";
my $PGENIDOMAIN   = "@PROTOGENI_DOMAIN@";
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
my $PGENILOCALUSER= @PROTOGENI_LOCALUSER@;
my $CMCERT	  = "$TB/etc/genicm.pem";
my $OURDOMAIN     = "@OURDOMAIN@";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Protos
sub fatal($);
sub GetServerCredential();
sub PostImageInfo($);

#
# Turn off line buffering on output
#
$| = 1; 

# Now we can load the libraries after setting the proper DB.
use lib '@prefix@/lib';
use emutil;
use GeniDB;
use EmulabConstants;
use GeniCertificate;
use GeniAuthority;
use GeniUser;
use GeniHRN;
use Genixmlrpc;
use libEmulab;
101
use OSImage;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
use User;
use Group;

if (!$PGENISUPPORT) {
    print STDERR "You are not a protogeni site\n";
    exit(0);
}
#
# Check args.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"d"})) {
    $debug = 1;
}
122 123 124
if (defined($options{"D"})) {
    $devtree = 1;
}
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
if (defined($options{"v"})) {
    $verbose = 1;
}
if (defined($options{"c"})) {
    $credfile = $options{"c"};
}
if (defined($options{"o"})) {
    $outfile = $options{"o"};
}
if (defined($options{"u"})) {
    $user = $options{"u"};
}
usage()
    if (! (@ARGV && $outfile));

140
my $image = OSImage->Lookup($ARGV[0]);
141 142 143
fatal("No such image")
    if (!defined($image));

144 145 146 147 148 149 150
#
# This is supposed to be set, bail if not.
#
if (!defined($image->origin_name()) || $image->origin_name() eq "") {
    fatal("No origin name in the descriptor");
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
if (defined($user)) {
    my $local_user = User->Lookup($user);
    fatal("No such user")
	if (!defined($local_user));
    
    $local_user = GeniUser->CreateFromLocal($local_user);
    my $context = Genixmlrpc->UserContext($local_user);
    if (!defined($context)) {
	fatal("Could not create context to talk to image server");
    }
    Genixmlrpc->SetContext($context);
}
else {
    #
    # Load the CM cert to act as caller context.
    #
    my $certificate = GeniCertificate->LoadFromFile($CMCERT);
    if (!defined($certificate)) {
	fatal("Could not load certificate from $CMCERT\n");
    }
    my $context = Genixmlrpc->Context($certificate);
    if (!defined($context)) {
	fatal("Could not create context to talk to image server");
    }
    Genixmlrpc->SetContext($context);
}
if (defined($credfile)) {
    $credential = GeniCredential->LoadFromFile($credfile);
}
else {
    $credential = $image->GetCredential();
}
fatal("Could not load credential")
    if (!defined($credential));

fatal("Not allowed to write to output file")
    if (-e $outfile && !-w $outfile);
open(FD, ">$outfile")
    or fatal("Could not open output file for writing: $!");
$outfp = *FD;

#
# Derive the authority where the image lives, from the credential.
#
my ($signer_cert) = @{ $credential->signer_certs() };
fatal("Could not find signer certificate for credential")
    if (!defined($signer_cert));
my $signer = GeniCertificate->LoadFromString($signer_cert);
fatal("Could not load signer certificate for credential")
    if (!defined($signer));
my $authority = GeniAuthority->CreateFromRegistry("cm", $signer->urn());
fatal("Could not lookup authority: " . $signer->urn())
    if (!defined($authority));
my $cmurl = $authority->url();
205
$cmurl =~ s/protogeni/protogeni\/stoller/ if ($devtree);
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
#
# Convert this to image urn at the target. Version is important for OS
# images, also need to generate a dataset URN if appropriate.
#
if ($image->origin_name() =~ /^([^\/]+)\/([^:]+):(\d+)$/) {
    my $pid  = $1;
    my $name = $2;
    my $vers = $3;
    
    if ($image->isdataset()) {
	$image_urn = GeniHRN::GenerateDataset($authority->urn()->domain(),
					      "imdataset", $pid, $name, $vers);
    }
    else {
	$image_urn = GeniHRN::GenerateImage($authority->urn()->domain(),
					    $pid, $name, $vers);
    }
}
else {
    fatal("Invalid origin_name in image");
}

if ($debug) {
    print "$image_urn, $cmurl\n";
}

233 234 235 236 237
#
# Suck over some bits.
#
my $response =
    Genixmlrpc::CallMethodStream($cmurl, undef, $outfp, "SecureImageDownload",
238 239
				 {"credentials" => [$credential->asString()],
				  "image_urn"   => $image_urn});
240
if (!defined($response) || $response->code()) {
241 242 243
    if ($debug && defined($response)) {
	print Dumper $response . "\n";
    }
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    my $msg = "SecureImageDownload failed for image " . $image->versname() . " :";
    
    if (!defined($response)) {
	# Bail, we will try again later.
	print STDERR "$msg RPC error\n";
	exit(-1);
    }
    elsif (defined($response->output())) {
	print STDERR "$msg " . $response->output() . "\n";
	exit(-1);
    }
    else {
	print STDERR "$msg " . $response->code() . "\n";
	exit(-1);
    }
}
close($outfp);
exit(0);

sub fatal($)
{
    my ($msg) = @_;

    print STDERR "*** $0:\n".
	         "    $msg\n";
    # exit value important.
    exit(-1);
}