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

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

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

# Must come after package declaration!
use GeniDB;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
21
use GeniRegistry;
22
use GeniHRN;
23
24
use Genixmlrpc;
use GeniResponse;
25
use emutil qw(TBGetUniqueIndex);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
27
28
use English;
use overload ('""' => 'Stringify');
use XML::Simple;
29
use Date::Parse;
30
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
32
33
34
35
36
37
38
39
40
41
42
43

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
my $SIGNCRED	   = "$TB/sbin/signgenicred";
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";

# Cache of instances to avoid regenerating them.
my %authorities    = ();
44
BEGIN { use GeniUtil; GeniUtil::AddCache(\%authorities); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
46

#
47
# Lookup by URN (and also UUID, for compatibility).
Leigh B. Stoller's avatar
Leigh B. Stoller committed
48
49
50
51
52
#
sub Lookup($$)
{
    my ($class, $token) = @_;
    my $query_result;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
    my $uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54

55
56
57
    if (GeniHRN::IsValid($token)) {
	$query_result =
	    DBQueryWarn("select uuid from geni_authorities ".
58
			"where urn='$token'");
59
60
61
62
63

	return undef
	    if (! $query_result || !$query_result->numrows);

	($uuid) = $query_result->fetchrow_array();
64
65
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
	$uuid = $token;
67
    }
68
    elsif ($token =~ /^P([\w]+)$/) {
69
70
	print STDERR "Deprecated authority lookup by prefix\n";
	return undef;
71
    }
72
73
    elsif ($token =~ /^[\w\.]*$/) {
	$query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
74
	    DBQueryWarn("select uuid from geni_authorities ".
75
			"where hrn='$token'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76
77
	return undef
	    if (! $query_result || !$query_result->numrows);
78

Leigh B. Stoller's avatar
Leigh B. Stoller committed
79
	($uuid) = $query_result->fetchrow_array();
80
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
81
82
83
84
    else {
	return undef;
    }
    # Look in cache first
Leigh B. Stoller's avatar
Leigh B. Stoller committed
85
86
    return $authorities{"$uuid"}
        if (exists($authorities{"$uuid"}));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
87
88

    $query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
89
	DBQueryWarn("select * from geni_authorities where uuid='$uuid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
90
91
92
93
94
95
    
    return undef
	if (!$query_result || !$query_result->numrows);

    my $self              = {};
    $self->{'AUTHORITY'}  = $query_result->fetchrow_hashref();
96
    $self->{'_version_info'} = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
97
98
99
100
101
    bless($self, $class);

    #
    # Grab the certificate, since we will probably want it.
    #
102
103
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
104
	print STDERR "Could not find certificate for authority $uuid\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
105
106
	return undef;
    }
107
    $self->{'CERT'} = $certificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
108
109
    
    # Add to cache. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
110
    $authorities{$self->{'AUTHORITY'}->{'uuid'}} = $self;
111
112
113
114

    # Get the domain as a convenience.
    my ($domain,undef,undef) = GeniHRN::Parse($self->{'AUTHORITY'}->{'urn'});
    $self->{'DOMAIN'} = $domain;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
115
116
117
118
119
120
121
122
123
124
125
    
    return $self;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
126
    my $urn = $self->urn();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
127

128
    return "[GeniAuthority: $urn]";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
129
130
131
132
133
}

#
# Create a Geni authority in the DB.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
134
sub Create($$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
135
{
136
    my ($class, $certificate, $url, $type) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
137
138

    my @insert_data = ();
139
    my ($prefix) = ($certificate->uuid() =~ /^\w+\-\w+\-\w+\-\w+\-(\w+)$/);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
140

141
    my $safe_hrn    = DBQuoteSpecial($certificate->hrn());
142
    my $safe_urn    = DBQuoteSpecial($certificate->URN());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143
    my $safe_url    = DBQuoteSpecial($url);
144
    my $safe_uuid   = DBQuoteSpecial($certificate->uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
145
    my $safe_prefix = DBQuoteSpecial($prefix);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
146
    my $safe_type   = DBQuoteSpecial(lc($type));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
147
148
149
150
    
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "hrn=$safe_hrn");
151
    push(@insert_data, "urn=$safe_urn");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
152
153
    push(@insert_data, "url=$safe_url");
    push(@insert_data, "uuid=$safe_uuid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
154
    push(@insert_data, "uuid_prefix=$safe_prefix");
155
    push(@insert_data, "type=$safe_type");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
156

157
    if ($certificate->Store() != 0) {
158
	print STDERR "Could not store certificate for new authority.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
159
160
161
	return undef;
    }

162
163
    # Insert into DB. Use "replace" here since we reload the auth info
    # periodically, and do not want to cause a race by deleting it. 
164
165
166
167
    return undef
	if (!DBQueryWarn("replace into geni_authorities set " .
			 join(",", @insert_data)));

168
169
170
    # Delete from cache, since we use replace above. 
    delete($authorities{$certificate->uuid()});

171
172
    return GeniAuthority->Lookup( defined( $certificate->urn() ) ?
	$certificate->urn() : $certificate->uuid() );
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173
174
175
176
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'AUTHORITY'}->{$_[1]}); }
sub uuid($)		{ return field($_[0], "uuid"); }
177
sub expires($)		{ return field($_[0], "expires"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
178
sub uuid_prefix($)	{ return field($_[0], "uuid_prefix"); }
179
sub urn($)		{ return field($_[0], "urn"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
180
181
sub url($)		{ return field($_[0], "url"); }
sub hrn($)		{ return field($_[0], "hrn"); }
182
sub type($)		{ return field($_[0], "type"); }
183
sub disabled($)		{ return field($_[0], "disabled"); }
184
185
# Convenience. 
sub domain($)     	{ return $_[0]->{"DOMAIN"}; }
186
187
188
189
190
191
sub _version_info($)	{ return $_[0]->{"_version_info"}; }
sub version($)		{ return $_[0]->VersionInfo()->{"version"}; }
sub apilevel($)		{ return $_[0]->VersionInfo()->{"apilevel"}; }
sub api($)		{ return $_[0]->VersionInfo()->{"api"}; }
sub issfa($)		{ return $_[0]->VersionInfo()->{"issfa"}; }
sub versiondata($)	{ return $_[0]->VersionInfo()->{"versiondata"}; }
192
193
sub cert($)		{ return $_[0]->{'CERT'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERT'}; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
194

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#
# Expired?
#
sub IsExpired($)
{
    my ($self)  = @_;
    my $expires = $self->expires();

    return 1
	if (!defined($expires) || $expires eq "");
    
    my $when = strptime($expires);

    return ($when < time());
}

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#
# Delete from the DB.
#
sub Delete($)
{
    my ($self) = @_;

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

    if ($self->GetCertificate()->Delete() != 0) {
	print STDERR "Could not delete certificate for $self\n";
	return -1;
    }
    my $uuid = $self->uuid();
    DBQueryWarn("delete from geni_authorities ".
		"where uuid='$uuid'")
	or return -1;

230
    # Delete from cache. 
231
232
233
234
    delete($authorities{$uuid});
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
235
#
236
# Check to see if there is an existing authority with the same urn.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
237
#
238
sub CheckExisting($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
239
{
240
241
    my ($class, $certificate) = @_;
    my $urn = $certificate->urn();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
242

243
244
245
246
    my (undef, undef, $type) = GeniHRN::Parse($urn);
    return -1
	if (!defined($type));
    my $safe_urn = DBQuoteSpecial($urn);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
247
248

    my $query_result =
249
250
	DBQueryWarn("select urn,type from geni_authorities ".
		    "where urn=$safe_urn");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251
252
253
254
255
256

    return -1
	if (!$query_result);
    return 0
	if (!$query_result->numrows);

257
    while (my ($DBurn,$DBtype) = $query_result->fetchrow_array()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
258
259
	# Look for an exact match, which means its just a replacement.
	next
260
	    if ($urn eq $DBurn && $type eq $DBtype);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
261

262
	# Same urn, different type.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
263
	return 1
264
	    if ($urn eq $DBurn && $type ne $DBtype);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
265

266
	# Different urn, same type.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
267
	return 1
268
	    if ($urn ne $DBurn && $type eq $DBtype);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269
270
271
272
    }
    return 0;
}

273
274
#
# Create authority from the ClearingHouse, by looking up the info.
275
276
# All authorities are currently stored in the Utah Emulab ClearingHouse,
# at least until we can deal with multiple clearinghouses/registries.
277
278
279
#
sub CreateFromRegistry($$$)
{
280
    my ($class, $type, $name) = @_;
281

282
283
284
285
    my $authority = GeniAuthority->Lookup($name);
    return $authority
	if (defined($authority) && $authority->urn());

Leigh B. Stoller's avatar
Leigh B. Stoller committed
286
287
288
289
    my $clearinghouse = GeniRegistry::ClearingHouse->Create();
    return undef
	if (!defined($clearinghouse));

290
291
    my $blob;
    return undef
292
	if ($clearinghouse->Resolve($name, $type, \$blob) != 0);
293
294
295
296
297

    my $certificate = GeniCertificate->LoadFromString($blob->{'gid'});
    return undef
	if (!defined($certificate));

298
299
300
301
302
303
304
305
306
307
308
309
310
311
    #
    # At this point, we do not support non-urn sites. They must re-register.
    #
    my $urn = $certificate->urn();
    if (!defined($urn)) {
	print STDERR "GeniAuthority::CreateFromRegistry: ".
	    "$certificate does not have a urn.\n";
	$certificate->Delete();
	return undef;
    }

    $authority = GeniAuthority->Create($certificate,
				       $blob->{'url'},
				       $blob->{'type'});
312
313
314
315
316
317
    $certificate->Delete()
	if (!defined($authority));

    return $authority;
}

318
319
320
#
# Get Version. Ask the Authority what version it is running. 
#
321
sub VersionInfo($)
322
323
324
{
    my ($self) = @_;

325
326
    return $self->_version_info()
	if (defined($self->_version_info()));
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

    #
    # The caller had to set up the xmlrpc context.
    #
    my $response =
	Genixmlrpc::CallMethod($self->url(), undef, "GetVersion");
    
    if (!defined($response)) {
	print STDERR "*** Internal error getting version for $self\n";
	return undef;
    }
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	print STDERR "Could not get version for $self Error: ";
	print STDERR "  " . $response->output() . "\n";
	return undef;
    }
343
344
    my $ref = {"versiondata" => $response->value() };
    
345
    if (ref($response->value())) {
346
347
	# Look for the AM interface.
	if (exists($response->value()->{'geni_api'})) {
348
	    $ref->{'version'}  = $response->value()->{'geni_api'};
349
	    # This was wrong; it should be 2.0 not 1.0
350
351
352
353
	    $ref->{'version'}  = 2.0 if ($ref->{'version'} == 1.0);
	    $ref->{'apilevel'} = 0;
	    $ref->{'api'}      = "AM";
	    $ref->{'issfa'}    = exists($response->value()->{'sfa'});
354
355
	}
	else {
356
357
358
359
	    $ref->{'version'}  = $response->value()->{'api'};
	    $ref->{'apilevel'} = $response->value()->{'level'};
	    $ref->{'api'}      = "CM";
	    $ref->{'issfa'}    = 0;
360
	}
361
362
    }
    else {
363
364
365
366
	$ref->{'version'}  = $response->value();
	$ref->{'apilevel'} = 1;
	$ref->{'api'}      = "CM";
	$ref->{'issfa'}    = 0;
367
    }
368
369
    $self->{'_version_info'} = $ref;
    return $ref;
370
}
371
sub Version($)
372
373
374
{
    my ($self) = @_;

375
376
377
378
379
380
381
382
    return undef
	if (!defined($self->VersionInfo()));

    return $self->version();
}
sub ApiLevel($)
{
    my ($self) = @_;
383
384

    return undef
385
	if (!defined($self->VersionInfo()));
386
387
388
389
390
391
392
393

    return $self->apilevel();
}
sub Api($)
{
    my ($self) = @_;

    return undef
394
	if (!defined($self->VersionInfo()));
395
396
397

    return $self->api();
}
398
399
400
401
402
403
404
405
406
sub IsSFA($)
{
    my ($self) = @_;

    return undef
	if (!defined($self->VersionInfo()));

    return $self->issfa();
}
407

408
#
409
410
# Check that the authority is the issuer of the given certificate.
# This check is not quite complete yet.
411
#
412
sub CheckValidIssuer($$)
413
{
414
    my ($self, $certificate) = @_;
415

416
417
    my ($hisauthority, undef, undef) = GeniHRN::Parse($self->urn());
    my ($herauthority, undef, undef) = GeniHRN::Parse($certificate->urn());
418
    return 0
419
420
	if (! (defined($hisauthority) && defined($herauthority) &&
	       $hisauthority eq $herauthority));
421

422
    return 1;
423
424
}

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#
# List all authorities.
#
sub ListAll($$)
{
    my ($class, $pref) = @_;
    my @result = ();
    @$pref = ();

    my $query = "select uuid from geni_authorities";
    my $query_result = DBQueryWarn($query);

    return -1
	if (! $query_result);
    return 0
	if (!$query_result->numrows);

    while (my ($uuid) = $query_result->fetchrow_array()) {
	my $authority = GeniAuthority->Lookup($uuid);
	if (!defined($authority)) {
	    print STDERR "Could not lookup authority $uuid\n";
	    return -1;
	}
	push(@result, $authority);
    }
    @$pref = @result;
    return 0;
}

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#
# Find an authority by looking for the prefix. This will eventually go
# away when we fully switch to URNs
#
# Note tha only SAs are looked up this way.
#
sub LookupByPrefix($$)
{
    my ($class, $uuid) = @_;
    my $prefix;
    
    if ($uuid =~ /^\w+\-\w+\-\w+\-\w+\-(\w+)$/) {
	$prefix = $1;
    }
    elsif ($uuid =~ /^(\w+)$/) {
	$prefix = $1;
    }
    else {
	print STDERR "Could not parse uuid for prefix\n";
	return undef;
    }
    
    my $query_result =
	DBQueryWarn("select uuid from geni_authorities ".
		    "where uuid_prefix='$prefix' and type='sa'");

    return undef
	if (! $query_result || !$query_result->numrows);

    ($uuid) = $query_result->fetchrow_array();

    return GeniAuthority->Lookup($uuid);
}

488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#
# Set the disabled bit.
#
sub Disable($)
{
    my ($self) = @_;
    my $urn = $self->urn();
    
    DBQueryWarn("update geni_authorities set disabled=1 ".
		"where urn='$urn'")
	or return -1;

    return 0;
}

503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#
# Resolve something at an authority
#
sub Resolve($$)
{
    my ($self, $urn) = @_;

    my $manager_version = $self->Version();
    return undef
	if (!defined($manager_version));

    #
    # Need a credential to make this call. 
    #
    if (!defined(Genixmlrpc->GetContext())) {
	print STDERR "Need an RPC context to generate a self credential\n";
	return undef;
    }
    my $context = Genixmlrpc->GetContext();
522
523
524
525
526
527
    my $me      = GeniAuthority->Lookup($context->certificate()->uuid());
    if (!defined($me)) {
	print STDERR "Could not find my own authority for $context\n";
	return undef;
    }
    my $credential = GeniCredential->GetSelfCredential($me);
528
    if (!defined($credential)) {
529
	print STDERR "Could not create self credential for $me\n";
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
	return undef;
    }

    my $method_args;
    if ($manager_version == 2.0) {
	$method_args->{'credentials'} = [$credential->asString()];
	$method_args->{'urn'}         = $urn;
    }
    else {
	print STDERR "GeniAuthority::Resolve: Unknown version at $self\n";
	return undef;
    }
    my $response =
	Genixmlrpc::CallMethod($self->url(), undef, "Resolve", $method_args);
    if (!defined($response)) {
	print STDERR "*** Internal error Resolving $urn at $self\n";
	return undef;
    }
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	print STDERR "Could not resolve $urn at $self Error:";
	print STDERR " " . $response->output() . "\n";
	return undef;
    }
    return $response->value();
}

556
557
558
#
# Discover resources at an authority (CM).
#
559
sub DiscoverResources($$;$$)
560
{
561
562
563
564
565
566
567
568
    my ($self, $urn, $available, $compress) = @_;

    if (! defined($available)) {
	$available = 1;
    }
    if (! defined($compress)) {
	$compress = 0;
    }
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

    my $manager_version = $self->Version();
    return undef
	if (!defined($manager_version));

    #
    # Need a credential to make this call. 
    #
    if (!defined(Genixmlrpc->GetContext())) {
	print STDERR "Need an RPC context to generate a self credential\n";
	return undef;
    }
    my $context = Genixmlrpc->GetContext();
    my $me      = GeniAuthority->Lookup($context->certificate()->uuid());
    if (!defined($me)) {
	print STDERR "Could not find my own authority for $context\n";
	return undef;
    }
    my $credential = GeniCredential->GetSelfCredential($me);
    if (!defined($credential)) {
	print STDERR "Could not create self credential for $me\n";
	return undef;
    }

    my $method_args;
    if ($manager_version == 2.0) {
	$method_args->{'credentials'} = [$credential->asString()];
596
597
	$method_args->{'available'}   = $available;
	$method_args->{'compress'}    = $compress;
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
    }
    else {
	print STDERR "GeniAuthority::Discover: Unknown version at $self\n";
	return undef;
    }
    my $response =
	Genixmlrpc::CallMethod($self->url(), undef,
			       "DiscoverResources", $method_args);
    if (!defined($response)) {
	print STDERR "*** Internal error discovering resources at $self\n";
	return undef;
    }
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	print STDERR "Could not discover resources at $self Error:";
	print STDERR " " . $response->output() . "\n";
	return undef;
    }
    return $response->value();
}

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