GeniAuthority.pm.in 18.3 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2013 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
# 
# {{{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.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#
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 Stoller's avatar
Leigh Stoller committed
44
use GeniRegistry;
45
use GeniHRN;
46 47
use Genixmlrpc;
use GeniResponse;
48
use emutil qw(TBGetUniqueIndex);
Leigh Stoller's avatar
Leigh Stoller committed
49 50 51
use English;
use overload ('""' => 'Stringify');
use XML::Simple;
52
use Date::Parse;
53
use Data::Dumper;
Leigh Stoller's avatar
Leigh Stoller committed
54 55 56 57 58 59 60 61 62 63

# 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";
64
my $PROTOGENI_URL  = "@PROTOGENI_URL@";
Leigh Stoller's avatar
Leigh Stoller committed
65 66 67

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

#
71
# Lookup by URN (and also UUID, for compatibility).
Leigh Stoller's avatar
Leigh Stoller committed
72 73 74 75 76
#
sub Lookup($$)
{
    my ($class, $token) = @_;
    my $query_result;
Leigh Stoller's avatar
Leigh Stoller committed
77
    my $uuid;
Leigh Stoller's avatar
Leigh Stoller committed
78

79 80 81
    if (GeniHRN::IsValid($token)) {
	$query_result =
	    DBQueryWarn("select uuid from geni_authorities ".
82
			"where urn='$token'");
83

84 85 86 87 88 89 90
	if( $query_result && $query_result->numrows ) {
	    ($uuid) = $query_result->fetchrow_array();
	} else {
	    # A lexically valid name, which isn't in the authority
	    # database.  First, we check if the authority corresponds
	    # to a valid local project: we generate those on demand.
	    my ($auth, $type, $id) = GeniHRN::Parse( $token );
91

92 93 94 95 96 97 98 99
	    if( $type eq "authority" && $auth =~ /^@OURDOMAIN@:([-\w]+)$/ ) {
		my $pid = $1;
		
		require Project;

		my $project = Project->Lookup($pid);
		return undef if (!defined($project));

100
		my $url = "$PROTOGENI_URL/project/$pid/$id";
101 102 103 104 105 106 107 108 109 110 111 112 113

		my $cert = GeniCertificate->Create(
		    { "urn" => GeniHRN::Generate( "@OURDOMAIN@:$pid",
						  "authority", $id ),
		      "url" => $url,
		      "email" => "@TBOPSEMAIL@",
		      "hrn" => "@OURDOMAIN@.$pid.$id" } );

		return GeniAuthority->Create( $cert, $url, $id );
	    }

	    return undef;
	}
114 115
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
Leigh Stoller's avatar
Leigh Stoller committed
116
	$uuid = $token;
117
    }
118
    elsif ($token =~ /^P([\w]+)$/) {
119 120
	print STDERR "Deprecated authority lookup by prefix\n";
	return undef;
121
    }
122 123
    elsif ($token =~ /^[\w\.]*$/) {
	$query_result =
Leigh Stoller's avatar
Leigh Stoller committed
124
	    DBQueryWarn("select uuid from geni_authorities ".
125
			"where hrn='$token'");
Leigh Stoller's avatar
Leigh Stoller committed
126 127
	return undef
	    if (! $query_result || !$query_result->numrows);
128

Leigh Stoller's avatar
Leigh Stoller committed
129
	($uuid) = $query_result->fetchrow_array();
130
    }
Leigh Stoller's avatar
Leigh Stoller committed
131 132 133 134
    else {
	return undef;
    }
    # Look in cache first
Leigh Stoller's avatar
Leigh Stoller committed
135 136
    return $authorities{"$uuid"}
        if (exists($authorities{"$uuid"}));
Leigh Stoller's avatar
Leigh Stoller committed
137 138

    $query_result =
Leigh Stoller's avatar
Leigh Stoller committed
139
	DBQueryWarn("select * from geni_authorities where uuid='$uuid'");
Leigh Stoller's avatar
Leigh Stoller committed
140 141 142 143 144 145
    
    return undef
	if (!$query_result || !$query_result->numrows);

    my $self              = {};
    $self->{'AUTHORITY'}  = $query_result->fetchrow_hashref();
146
    $self->{'_version_info'} = undef;
Leigh Stoller's avatar
Leigh Stoller committed
147 148 149 150 151
    bless($self, $class);

    #
    # Grab the certificate, since we will probably want it.
    #
152 153
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
Leigh Stoller's avatar
Leigh Stoller committed
154
	print STDERR "Could not find certificate for authority $uuid\n";
Leigh Stoller's avatar
Leigh Stoller committed
155 156
	return undef;
    }
157
    $self->{'CERT'} = $certificate;
Leigh Stoller's avatar
Leigh Stoller committed
158 159
    
    # Add to cache. 
Leigh Stoller's avatar
Leigh Stoller committed
160
    $authorities{$self->{'AUTHORITY'}->{'uuid'}} = $self;
161 162 163 164

    # Get the domain as a convenience.
    my ($domain,undef,undef) = GeniHRN::Parse($self->{'AUTHORITY'}->{'urn'});
    $self->{'DOMAIN'} = $domain;
Leigh Stoller's avatar
Leigh Stoller committed
165 166 167 168 169 170 171 172 173 174 175
    
    return $self;
}

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

178
    return "[GeniAuthority: $urn]";
Leigh Stoller's avatar
Leigh Stoller committed
179 180 181 182 183
}

#
# Create a Geni authority in the DB.
#
Leigh Stoller's avatar
Leigh Stoller committed
184
sub Create($$$$)
Leigh Stoller's avatar
Leigh Stoller committed
185
{
186
    my ($class, $certificate, $url, $type) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
187 188

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

191
    my $safe_hrn    = DBQuoteSpecial($certificate->hrn());
192
    my $safe_urn    = DBQuoteSpecial($certificate->URN());
Leigh Stoller's avatar
Leigh Stoller committed
193
    my $safe_url    = DBQuoteSpecial($url);
194
    my $safe_uuid   = DBQuoteSpecial($certificate->uuid());
Leigh Stoller's avatar
Leigh Stoller committed
195
    my $safe_prefix = DBQuoteSpecial($prefix);
Leigh Stoller's avatar
Leigh Stoller committed
196
    my $safe_type   = DBQuoteSpecial(lc($type));
Leigh Stoller's avatar
Leigh Stoller committed
197 198 199 200
    
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "hrn=$safe_hrn");
201
    push(@insert_data, "urn=$safe_urn");
Leigh Stoller's avatar
Leigh Stoller committed
202 203
    push(@insert_data, "url=$safe_url");
    push(@insert_data, "uuid=$safe_uuid");
Leigh Stoller's avatar
Leigh Stoller committed
204
    push(@insert_data, "uuid_prefix=$safe_prefix");
205
    push(@insert_data, "type=$safe_type");
Leigh Stoller's avatar
Leigh Stoller committed
206

207
    if ($certificate->Store() != 0) {
208
	print STDERR "Could not store certificate for new authority.\n";
Leigh Stoller's avatar
Leigh Stoller committed
209 210 211
	return undef;
    }

212 213
    # Insert into DB. Use "replace" here since we reload the auth info
    # periodically, and do not want to cause a race by deleting it. 
214 215 216 217
    return undef
	if (!DBQueryWarn("replace into geni_authorities set " .
			 join(",", @insert_data)));

218 219 220
    # Delete from cache, since we use replace above. 
    delete($authorities{$certificate->uuid()});

221 222
    return GeniAuthority->Lookup( defined( $certificate->urn() ) ?
	$certificate->urn() : $certificate->uuid() );
Leigh Stoller's avatar
Leigh Stoller committed
223 224 225 226
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'AUTHORITY'}->{$_[1]}); }
sub uuid($)		{ return field($_[0], "uuid"); }
227
sub expires($)		{ return field($_[0], "expires"); }
228
sub created($)		{ return field($_[0], "created"); }
Leigh Stoller's avatar
Leigh Stoller committed
229
sub uuid_prefix($)	{ return field($_[0], "uuid_prefix"); }
230
sub urn($)		{ return field($_[0], "urn"); }
Leigh Stoller's avatar
Leigh Stoller committed
231 232
sub url($)		{ return field($_[0], "url"); }
sub hrn($)		{ return field($_[0], "hrn"); }
233
sub type($)		{ return field($_[0], "type"); }
234
sub disabled($)		{ return field($_[0], "disabled"); }
235 236
# Convenience. 
sub domain($)     	{ return $_[0]->{"DOMAIN"}; }
237 238 239 240 241 242
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"}; }
243 244
sub cert($)		{ return $_[0]->{'CERT'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERT'}; }
Leigh Stoller's avatar
Leigh Stoller committed
245

246 247 248 249 250 251
#
# Expired?
#
sub IsExpired($)
{
    my ($self)  = @_;
252
    my $created = $self->created();
253

254 255 256 257
    #
    # We are not storing an expires field yet, so lets just age things
    # out periodically.
    #
258
    return 1
259
	if (!defined($created) || $created eq "");
260

261 262 263
    return 1
	if (str2time($created) < (time() - (14 * 24 * 3600)));
    return 0;
264 265
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
#
# 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;

285
    # Delete from cache. 
286 287 288 289
    delete($authorities{$uuid});
    return 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
290
#
291
# Check to see if there is an existing authority with the same urn.
Leigh Stoller's avatar
Leigh Stoller committed
292
#
293
sub CheckExisting($$)
Leigh Stoller's avatar
Leigh Stoller committed
294
{
295 296
    my ($class, $certificate) = @_;
    my $urn = $certificate->urn();
297
    my $hrn = $certificate->hrn();
Leigh Stoller's avatar
Leigh Stoller committed
298

299 300 301 302
    my (undef, undef, $type) = GeniHRN::Parse($urn);
    return -1
	if (!defined($type));
    my $safe_urn = DBQuoteSpecial($urn);
303
    my $safe_hrn = DBQuoteSpecial($hrn);
Leigh Stoller's avatar
Leigh Stoller committed
304 305

    my $query_result =
306 307
	DBQueryWarn("select urn,type from geni_authorities ".
		    "where urn=$safe_urn");
Leigh Stoller's avatar
Leigh Stoller committed
308 309 310 311 312 313

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

314
    while (my ($DBurn,$DBtype) = $query_result->fetchrow_array()) {
Leigh Stoller's avatar
Leigh Stoller committed
315 316
	# Look for an exact match, which means its just a replacement.
	next
317
	    if ($urn eq $DBurn && $type eq $DBtype);
Leigh Stoller's avatar
Leigh Stoller committed
318

319
	# Same urn, different type.
Leigh Stoller's avatar
Leigh Stoller committed
320
	return 1
321
	    if ($urn eq $DBurn && $type ne $DBtype);
Leigh Stoller's avatar
Leigh Stoller committed
322

323
	# Different urn, same type.
Leigh Stoller's avatar
Leigh Stoller committed
324
	return 1
325
	    if ($urn ne $DBurn && $type eq $DBtype);
Leigh Stoller's avatar
Leigh Stoller committed
326
    }
327 328 329 330 331 332 333 334 335 336 337 338

    #
    # Look for a duplicate hrn.
    # 
    $query_result =
	DBQueryWarn("select urn from geni_authorities ".
		    "where hrn=$safe_hrn and urn!=$safe_urn");

    return -1
	if (!$query_result);

    return $query_result->numrows;
Leigh Stoller's avatar
Leigh Stoller committed
339 340
}

341 342
#
# Create authority from the ClearingHouse, by looking up the info.
343 344
# All authorities are currently stored in the Utah Emulab ClearingHouse,
# at least until we can deal with multiple clearinghouses/registries.
345 346 347
#
sub CreateFromRegistry($$$)
{
348
    my ($class, $type, $name) = @_;
349

350
    my $authority = GeniAuthority->Lookup($name);
351 352 353 354 355 356 357 358 359 360 361 362
    if (defined($authority)) {
	#
	# Check for expiration.
	#
	if (!$authority->urn() || $authority->IsExpired()) {
	    print STDERR "Aged out stale or expired $authority ...\n";
	    $authority->Delete();
	    $authority = undef;
	}
	return $authority
	    if (defined($authority));
    }
Leigh Stoller's avatar
Leigh Stoller committed
363 364 365 366
    my $clearinghouse = GeniRegistry::ClearingHouse->Create();
    return undef
	if (!defined($clearinghouse));

367 368
    my $blob;
    return undef
369
	if ($clearinghouse->Resolve($name, $type, \$blob) != 0);
370 371 372 373 374

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

375 376 377 378 379 380 381 382 383 384 385 386 387 388
    #
    # 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'});
389 390 391 392 393 394
    $certificate->Delete()
	if (!defined($authority));

    return $authority;
}

395 396 397
#
# Get Version. Ask the Authority what version it is running. 
#
398
sub VersionInfo($)
399 400 401
{
    my ($self) = @_;

402 403
    return $self->_version_info()
	if (defined($self->_version_info()));
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

    #
    # 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;
    }
420 421
    my $ref = {"versiondata" => $response->value() };
    
422
    if (ref($response->value())) {
423 424
	# Look for the AM interface.
	if (exists($response->value()->{'geni_api'})) {
425
	    $ref->{'version'}  = $response->value()->{'geni_api'};
426
	    # This was wrong; it should be 2.0 not 1.0
427 428 429 430
	    $ref->{'version'}  = 2.0 if ($ref->{'version'} == 1.0);
	    $ref->{'apilevel'} = 0;
	    $ref->{'api'}      = "AM";
	    $ref->{'issfa'}    = exists($response->value()->{'sfa'});
431 432
	}
	else {
433 434 435 436
	    $ref->{'version'}  = $response->value()->{'api'};
	    $ref->{'apilevel'} = $response->value()->{'level'};
	    $ref->{'api'}      = "CM";
	    $ref->{'issfa'}    = 0;
437
	}
438 439
    }
    else {
440 441 442 443
	$ref->{'version'}  = $response->value();
	$ref->{'apilevel'} = 1;
	$ref->{'api'}      = "CM";
	$ref->{'issfa'}    = 0;
444
    }
445 446
    $self->{'_version_info'} = $ref;
    return $ref;
447
}
448
sub Version($)
449 450 451
{
    my ($self) = @_;

452 453 454 455 456 457 458 459
    return undef
	if (!defined($self->VersionInfo()));

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

    return undef
462
	if (!defined($self->VersionInfo()));
463 464 465 466 467 468 469 470

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

    return undef
471
	if (!defined($self->VersionInfo()));
472 473 474

    return $self->api();
}
475 476 477 478 479 480 481 482 483
sub IsSFA($)
{
    my ($self) = @_;

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

    return $self->issfa();
}
484

485
#
486 487
# Check that the authority is the issuer of the given certificate.
# This check is not quite complete yet.
488
#
489
sub CheckValidIssuer($$)
490
{
491
    my ($self, $certificate) = @_;
492

493 494
    my ($hisauthority, undef, undef) = GeniHRN::Parse($self->urn());
    my ($herauthority, undef, undef) = GeniHRN::Parse($certificate->urn());
495
    return 0
496 497
	if (! (defined($hisauthority) && defined($herauthority) &&
	       $hisauthority eq $herauthority));
498

499
    return 1;
500 501
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
#
# 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;
}

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 556 557 558 559 560 561 562 563 564
#
# 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);
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
#
# 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;
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
#
# Update the certificate, as when an authority regenerates.
#
sub UpdateCertificate($$)
{
    my ($self, $certificate) = @_;
    my $oldcert = $self->GetCertificate();

    if ($oldcert->Delete()) {
	print STDERR "Could not delete $self\n";
	return -1;
    }
    if ($certificate->Store()) {
	print STDERR "Could not store $certificate\n";
	return -1;
    }
    return 0;
}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
#
# 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;
    }
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

    #
    # Gack, if this is an SFA, hand off to the registry module, since it 
    # knows how to get to the registry.
    #
    if ($self->IsSFA()) {
	my $registry = GeniRegistry::PLC->CreateFromAuthority($self);
	if (!defined($registry)) {
	    print "Could not create an SFA registry from $self\n";
	    return undef;
	}
	return $registry->Resolve($urn);
    }

    #
    # Otherwise we assume a PG authority, and all PG servers do Resolve.
    #
634
    my $context = Genixmlrpc->GetContext();
635 636 637 638 639 640
    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);
641
    if (!defined($credential)) {
642
	print STDERR "Could not create self credential for $me\n";
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	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();
}

669 670 671
#
# Discover resources at an authority (CM).
#
672
sub DiscoverResources($$;$$)
673
{
674 675 676 677 678 679 680 681
    my ($self, $urn, $available, $compress) = @_;

    if (! defined($available)) {
	$available = 1;
    }
    if (! defined($compress)) {
	$compress = 0;
    }
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

    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()];
709 710
	$method_args->{'available'}   = $available;
	$method_args->{'compress'}    = $compress;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    }
    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 Stoller's avatar
Leigh Stoller committed
731 732 733
# _Always_ make sure that this 1 is at the end of the file...
1;