GeniAuthority.pm.in 14.6 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;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
111
112
113
114
115
116
117
118
119
120
121
    
    return $self;
}

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

124
    return "[GeniAuthority: $urn]";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
125
126
127
128
129
}

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

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

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

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

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

164
165
166
    # Delete from cache, since we use replace above. 
    delete($authorities{$certificate->uuid()});

167
168
    return GeniAuthority->Lookup( defined( $certificate->urn() ) ?
	$certificate->urn() : $certificate->uuid() );
Leigh B. Stoller's avatar
Leigh B. Stoller committed
169
170
171
172
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'AUTHORITY'}->{$_[1]}); }
sub uuid($)		{ return field($_[0], "uuid"); }
173
sub expires($)		{ return field($_[0], "expires"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
174
sub uuid_prefix($)	{ return field($_[0], "uuid_prefix"); }
175
sub urn($)		{ return field($_[0], "urn"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
176
177
sub url($)		{ return field($_[0], "url"); }
sub hrn($)		{ return field($_[0], "hrn"); }
178
sub type($)		{ return field($_[0], "type"); }
179
sub disabled($)		{ return field($_[0], "disabled"); }
180
181
182
183
184
185
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"}; }
186
187
sub cert($)		{ return $_[0]->{'CERT'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERT'}; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
188

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#
# Expired?
#
sub IsExpired($)
{
    my ($self)  = @_;
    my $expires = $self->expires();

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

    return ($when < time());
}

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#
# 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;

224
    # Delete from cache. 
225
226
227
228
    delete($authorities{$uuid});
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
229
#
230
# Check to see if there is an existing authority with the same urn.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
231
#
232
sub CheckExisting($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
233
{
234
235
    my ($class, $certificate) = @_;
    my $urn = $certificate->urn();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
236

237
238
239
240
    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
241
242

    my $query_result =
243
244
	DBQueryWarn("select urn,type from geni_authorities ".
		    "where urn=$safe_urn");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
245
246
247
248
249
250

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

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

256
	# Same urn, different type.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
257
	return 1
258
	    if ($urn eq $DBurn && $type ne $DBtype);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
259

260
	# Different urn, same type.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
261
	return 1
262
	    if ($urn ne $DBurn && $type eq $DBtype);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
263
264
265
266
    }
    return 0;
}

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

276
277
278
279
    my $authority = GeniAuthority->Lookup($name);
    return $authority
	if (defined($authority) && $authority->urn());

Leigh B. Stoller's avatar
Leigh B. Stoller committed
280
281
282
283
    my $clearinghouse = GeniRegistry::ClearingHouse->Create();
    return undef
	if (!defined($clearinghouse));

284
285
    my $blob;
    return undef
286
	if ($clearinghouse->Resolve($name, $type, \$blob) != 0);
287
288
289
290
291

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

292
293
294
295
296
297
298
299
300
301
302
303
304
305
    #
    # 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'});
306
307
308
309
310
311
    $certificate->Delete()
	if (!defined($authority));

    return $authority;
}

312
313
314
#
# Get Version. Ask the Authority what version it is running. 
#
315
sub VersionInfo($)
316
317
318
{
    my ($self) = @_;

319
320
    return $self->_version_info()
	if (defined($self->_version_info()));
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

    #
    # 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;
    }
337
338
    my $ref = {"versiondata" => $response->value() };
    
339
    if (ref($response->value())) {
340
341
	# Look for the AM interface.
	if (exists($response->value()->{'geni_api'})) {
342
	    $ref->{'version'}  = $response->value()->{'geni_api'};
343
	    # This was wrong; it should be 2.0 not 1.0
344
345
346
347
	    $ref->{'version'}  = 2.0 if ($ref->{'version'} == 1.0);
	    $ref->{'apilevel'} = 0;
	    $ref->{'api'}      = "AM";
	    $ref->{'issfa'}    = exists($response->value()->{'sfa'});
348
349
	}
	else {
350
351
352
353
	    $ref->{'version'}  = $response->value()->{'api'};
	    $ref->{'apilevel'} = $response->value()->{'level'};
	    $ref->{'api'}      = "CM";
	    $ref->{'issfa'}    = 0;
354
	}
355
356
    }
    else {
357
358
359
360
	$ref->{'version'}  = $response->value();
	$ref->{'apilevel'} = 1;
	$ref->{'api'}      = "CM";
	$ref->{'issfa'}    = 0;
361
    }
362
363
    $self->{'_version_info'} = $ref;
    return $ref;
364
}
365
sub Version($)
366
367
368
{
    my ($self) = @_;

369
370
371
372
373
374
375
376
    return undef
	if (!defined($self->VersionInfo()));

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

    return undef
379
	if (!defined($self->VersionInfo()));
380
381
382
383
384
385
386
387

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

    return undef
388
	if (!defined($self->VersionInfo()));
389
390
391

    return $self->api();
}
392
393
394
395
396
397
398
399
400
sub IsSFA($)
{
    my ($self) = @_;

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

    return $self->issfa();
}
401

402
#
403
404
# Check that the authority is the issuer of the given certificate.
# This check is not quite complete yet.
405
#
406
sub CheckValidIssuer($$)
407
{
408
    my ($self, $certificate) = @_;
409

410
411
    my ($hisauthority, undef, undef) = GeniHRN::Parse($self->urn());
    my ($herauthority, undef, undef) = GeniHRN::Parse($certificate->urn());
412
    return 0
413
414
	if (! (defined($hisauthority) && defined($herauthority) &&
	       $hisauthority eq $herauthority));
415

416
    return 1;
417
418
}

419
420
421
422
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
#
# 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;
}

448
449
450
451
452
453
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
#
# 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);
}

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#
# 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;
}

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#
# 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();
516
517
518
519
520
521
    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);
522
    if (!defined($credential)) {
523
	print STDERR "Could not create self credential for $me\n";
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
	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();
}

550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
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
596
597
598
599
600
601
602
603
#
# Discover resources at an authority (CM).
#
sub DiscoverResources($$)
{
    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();
    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()];
	$method_args->{'available'}   = 1;
    }
    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
604
605
606
# _Always_ make sure that this 1 is at the end of the file...
1;