GeniTicket.pm.in 7.83 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# All rights reserved.
#
package GeniTicket;

#
# Some simple ticket stuff.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use lib '@prefix@/lib';
use GeniDB;
use libtestbed;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23 24
use Experiment;
use libdb qw(TBGetUniqueIndex);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26 27
use XML::Simple;
use XML::LibXML;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28 29 30 31 32 33 34 35 36 37 38 39
use Data::Dumper;
use File::Temp qw(tempfile);

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
my $GENICENTRAL    = "https://boss/protogeni/xmlrpc";
my $SIGNCRED	   = "$TB/sbin/signgenicred";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41 42

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43
# Create an unsigned ticket object from the raw rspec. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
sub Create($$$$;$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
46
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47 48
    my ($class, $slice_uuid, $owner_uuid,
	$rspec, $ticket_string, $component) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49 50

    my $self = {};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
51 52 53 54 55
    $self->{'rspec'}         = $rspec;
    $self->{'slice_uuid'}    = $slice_uuid;
    $self->{'owner_uuid'}    = $owner_uuid;
    $self->{'ticket_string'} = $ticket_string;
    $self->{'ticket'}        = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56
    $self->{'idx'}           = undef;	# Only set when in DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
57
    $self->{'component'}     = $component;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
58 59 60 61 62 63
    bless($self, $class);

    return $self;
}
# accessors
sub field($$)           { return ($_[0]->{$_[1]}); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
64
sub idx($)		{ return field($_[0], "idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
65
sub rspec($)		{ return field($_[0], "rspec"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66 67 68
sub uuid($)		{ return field($_[0], "slice_uuid"); }
sub slice_uuid($)	{ return field($_[0], "slice_uuid"); }
sub owner_uuid($)	{ return field($_[0], "owner_uuid"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69
sub ticket($)		{ return field($_[0], "ticket"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
70
sub asString($)		{ return field($_[0], "ticket_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71
sub ticket_string($)	{ return field($_[0], "ticket_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114
sub component($)	{ return field($_[0], "component"); }

#
# Create a ticket object from a signed ticket string.
#
sub CreateFromSignedTicket($$)
{
    my ($class, $ticket_string) = @_;

    # Use XML::Simple to convert to something we can mess with.
    my $parser = XML::LibXML->new;
    my $doc    = $parser->parse_string($ticket_string);

    # Dig out the rspec.
    my ($rspec_node) = $doc->getElementsByTagName("rspec");
    return undef
	if (!defined($rspec_node));
    my $rspec = XMLin($rspec_node->toString(), ForceArray => 0);

    # Dig out the slice uuid. Locally, I am not sure if we bother to
    # keep slices in the DB (they are in the DB at geni central).
    my ($uuid_node) = $doc->getElementsByTagName("this_uuid");
    return undef
	if (!defined($uuid_node));
    my $slice_uuid = $uuid_node->to_literal();

    if (! ($slice_uuid =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/)) {
	print STDERR "Invalid slice_uuid in ticket\n";
	return undef;
    }

    # Dig out the owner uuid. Locally, I am not sure if we bother to
    # keep users in the DB (they are in the DB at geni central).
    ($uuid_node) = $doc->getElementsByTagName("owner_uuid");
    return undef
	if (!defined($uuid_node));
    my $owner_uuid = $uuid_node->to_literal();

    if (! ($owner_uuid =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/)) {
	print STDERR "Invalid owner_uuid in ticket\n";
	return undef;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    #
    # We save copies of the tickets we hand out; lets find that record
    # in the DB, just to verify.
    #
    my ($serial_node) = $doc->getElementsByTagName("serial");
    return undef
	if (!defined($serial_node));
    my $serial = $serial_node->to_literal();

    if (! ($serial =~ /^\w+$/)) {
	print STDERR "Invalid serial in ticket\n";
	return undef;
    }
    my $query_result =
	DBQueryWarn("select * from geni_tickets where idx='$serial'");
    if (!$query_result || !$query_result->numrows) {
	print STDERR "Could not find the ticket in te DB\n";
	return undef;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
135 136 137 138 139 140 141
    my $self = {};
    $self->{'rspec'}         = $rspec;
    $self->{'slice_uuid'}    = $slice_uuid;
    $self->{'owner_uuid'}    = $owner_uuid;
    $self->{'ticket_string'} = $ticket_string;
    $self->{'xmlref'}        = $doc;
    $self->{'component'}     = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142
    $self->{'idx'}           = $serial;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143 144 145 146 147 148
    bless($self, $class);

    print Dumper($self);

    return $self;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
149

Leigh B. Stoller's avatar
Leigh B. Stoller committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#
# Might have to delete this fron the DB, as with an error handing out a ticket.
#
sub Delete($)
{
    my ($self) = @_;

    return -1
	if (! ref($self));

    if (defined($self->idx())) {
	my $idx = $self->idx();
	
	DBQueryWarn("delete from geni_tickets where idx='$idx'")
	    or return -1;
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
169 170 171 172 173 174 175 176
#
# Populate the ticket with some stuff, which right now is just the
# number of node we are willing to grant.
#
sub Grant($$)
{
    my ($self, $count) = @_;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
177
    return -1
Leigh B. Stoller's avatar
Leigh B. Stoller committed
178 179 180 181 182 183
	if (! ref($self));

    $self->{'count'} = $count;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
#
# Store the given ticket in the DB. We only do this for signed tickets
# which we hand out, so we have a record of them. 
#
sub Store($$)
{
    my ($self, $idx) = @_;
    my @insert_data  = ();

    my $slice_uuid = $self->slice_uuid();
    my $owner_uuid = $self->owner_uuid();

    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
    push(@insert_data, "slice_uuid='$slice_uuid'");
    push(@insert_data, "owner_uuid='$owner_uuid'");
    
    my $safe_ticket = DBQuoteSpecial($self->ticket_string());
    push(@insert_data, "ticket_string=$safe_ticket");

    # Insert into DB.
    DBQueryWarn("insert into geni_tickets set " . join(",", @insert_data))
	or return -1;

    # If sucessfully stored, set the idx field so we know.
    $self->{'idx'} = $idx;

    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
215 216 217 218 219 220 221 222 223 224
#
# Sign the ticket before returning it. We capture the output, which is
# in XML.
#
sub Sign($)
{
    my ($self) = @_;

    return -1
	if (!ref($self));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225 226 227 228

    # Every Ticket gets a new unique index, which is used in the xml:id below.
    # Also used for the DB insertion below.
    my $idx = TBGetUniqueIndex('next_ticket', 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
229
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
230 231
    my $slice_uuid = $self->slice_uuid();
    my $owner_uuid = $self->owner_uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
232 233
    my $rspec_xml  = XMLout($self->rspec(), "NoAttr" => 1);
    $rspec_xml =~ s/opt\>/rspec\>/g;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
234 235 236 237 238 239

    #
    # Create a template xml file to sign.
    #
    my $template =
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
240
	"<credential xml:id=\"ref1\">\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
241
	" <type>ticket</type>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
242
	" <serial>$idx</serial>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
243 244
	" <owner_uuid>$owner_uuid</owner_uuid>\n".
	" <this_uuid>$slice_uuid</this_uuid>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
245 246
	" <ticket>\n".
	"  <can_delegate>1</can_delegate>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
247
	"  $rspec_xml\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	" </ticket>\n".	
        "</credential>\n";

    my ($fh, $filename) = tempfile(UNLINK => 0);
    return -1
	if (!defined($fh));

    print $fh $template;
    close($fh);

    #
    # Fire up the signer and capture the output. This is the signed ticket
    # that is returned. 
    #
    if (! open(SIGNER, "$SIGNCRED $filename |")) {
	print STDERR "Could not sign $filename\n";
	return -1;
    }
    my $ticket = "";
    while (<SIGNER>) {
	$ticket .= $_;
    }
    close(SIGNER);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
271
    $self->{'ticket_string'} = $ticket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
272

Leigh B. Stoller's avatar
Leigh B. Stoller committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    $self->Store($idx) == 0
	or return -1;

    return 0;
}

#
# Get the experiment for the slice this sliver belongs to.
#
sub GetExperiment($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    return Experiment->Lookup($self->slice_uuid());
}

#
# Release a ticket. Need to release the nodes ...
#
sub Release($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    my $experiment = Experiment->Lookup($self->slice_uuid());
    my $node_id    = $self->rspec()->{'node_id'};
    my $node       = Node->Lookup($node_id);
    return -1
	if (!defined($node));
    my $reservation = Node->Reservation();
    
    if (defined($reservation) && $reservation->SameExperiment($experiment)) {
	my $pid = $experiment->pid();
	my $eid = $experiment->eid();
	
	system("$NFREE $pid $eid $node_id");
    }
    $self->Delete();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
316 317 318
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
319 320
# _Always_ make sure that this 1 is at the end of the file...
1;