GeniTicket.pm.in 4.97 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 23
#!/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;
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24 25
use XML::Simple;
use XML::LibXML;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26 27 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
# Create an unsigned ticket object from the raw rspec. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41 42 43
#
# Should we keep track of tickets locally in the DB?
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
sub Create($$$$;$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
46 47
    my ($class, $slice_uuid, $owner_uuid,
	$rspec, $ticket_string, $component) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
48 49

    my $self = {};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50 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;
    $self->{'component'}     = $component;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56 57 58 59 60 61 62
    bless($self, $class);

    return $self;
}
# accessors
sub field($$)           { return ($_[0]->{$_[1]}); }
sub rspec($)		{ return field($_[0], "rspec"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63 64 65
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
66
sub ticket($)		{ return field($_[0], "ticket"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
sub asString($)		{ return field($_[0], "ticket_string"); }
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;
    }

    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;
    bless($self, $class);

    print Dumper($self);

    return $self;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

#
# 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) = @_;

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

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

#
# 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
151 152 153 154
    my $slice_uuid = $self->slice_uuid();
    my $owner_uuid = $self->owner_uuid();
    my $requested  = $self->rspec()->{'requested'};
    my $granted    = $self->rspec()->{'granted'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
155 156 157 158 159 160 161 162

    #
    # Create a template xml file to sign.
    #
    my $template =
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n".
	"<credential xml:id=\"Ref1\">\n".
	" <type>ticket</type>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
163 164
	" <owner_uuid>$owner_uuid</owner_uuid>\n".
	" <this_uuid>$slice_uuid</this_uuid>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
	" <ticket>\n".
	"  <can_delegate>1</can_delegate>\n".
	"  <rspec>\n".
	"    <requested>$requested</requested>\n".
	"    <granted>$granted</granted>\n".
	"  </rspec>\n".
	" </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
194
    $self->{'ticket_string'} = $ticket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
195 196 197 198

    return 0;
}

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