APT_Geni.pm.in 15.7 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2018 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 29 30 31 32 33 34 35 36 37 38 39
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
package APT_Geni;

use strict;
use English;
use Data::Dumper;
use Carp;
use Exporter;
use vars qw(@ISA @EXPORT $AUTOLOAD);

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

# Must come after package declaration!
use EmulabConstants;
use emdb;
use libtestbed;
40
use GeniHRN;
41 42
use GeniCertificate;
use GeniCredential;
43 44
use GeniAuthority;
use GeniResponse;
45 46 47 48 49

# Configure variables
my $TB		  = "@prefix@";
my $MAINSITE      = @TBMAINSITE@;
my $TBOPS         = "@TBOPSEMAIL@";
50 51
my $OURDOMAIN     = "@OURDOMAIN@";
my $PGENIDOMAIN   = "@PROTOGENI_DOMAIN@";
52
my $SACERT	  = "$TB/etc/genisa.pem";
53 54
my $EMCERT	  = "$TB/etc/emulab.pem";
my $EMKEY	  = "$TB/etc/emulab.key";
55

56 57 58 59 60
# Caches so we do not keep regenerating down inside the libraries that
# make the XMLRPC calls.
my %credcache  = ();
my %certcache  = ();
my %speakcache = ();
61

62 63 64
# Use real abac credentials (which means we can do speaks-for at ALS2).
my $USEABACCREDS  = 0;

65 66 67
# Debugging;
my $usemydevtree  = 0;

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 124 125 126 127 128 129 130 131 132 133 134
#
# Check credential/certificate status early, looking for expired certs.
#
sub VerifyCredentials($$)
{
    my ($geniuser, $perrmsg) = @_;
    my $urn = $geniuser->urn();
    my $code = -1;
    my $errmsg;
    
    #
    # If a local user account, but a nonlocal id, then we should
    # have a speaksfor credential stored, as well as a certificate
    # for the user.
    #
    if ($geniuser->IsLocal() && $geniuser->emulab_user()->IsNonLocal()) {
	#
	# If we already have these cached, they are okay.
	#
	return 0
	    if (exists($speakcache{$urn}));
	    
	my ($speaksfor_string, $certificate_string) =
	    $geniuser->emulab_user()->GetStoredCredential();
	
	if (! (defined($speaksfor_string) &&
	       defined($certificate_string))) {
	    $errmsg = "No stored speaksfor/certificate";
	    goto bad;
	}
	my $speaksfor = GeniCredential->CreateFromSigned($speaksfor_string, 1);
	if (!defined($speaksfor)) {
	    $errmsg = "Could not create speaksfor credential from string";
	    goto bad;
	}
	my $certificate = GeniCertificate->LoadFromString($certificate_string);
	if (!defined($certificate)) {
	    $errmsg = "Could not load certificate from string";
	    goto bad;
	}
	if ($certificate->IsExpired()) {
	    $errmsg = "User certificate has expired";
	    $code   = 1;
	    goto bad;
	}
	if ($speaksfor->IsExpired()) {
	    $errmsg = "User speaksfor credential has expired";
	    $code   = 1;
	    goto bad;
	}
	$speakcache{$urn} = $speaksfor;
	$certcache{$urn}  = $certificate;
    }
    else {
	# No need to cache this, GeniUser already does.
	if ($geniuser->GetCertificate()->IsExpired()) {
	    $errmsg = "User certificate has expired";
	    $code   = 1;
	    goto bad;
	}
    }
    return 0;
  bad:
    $$perrmsg = $errmsg;
    return $code;
}

135 136 137
#
# Generate the credentials we need. 
#
138
sub GenCredentials($$;$$)
139
{
140
    my ($target, $geniuser, $privs, $allowexpired) = @_;
141
    my ($speaksfor, $credential, $certificate, $oldexpires);
142 143
    # If the caller does not want a speaksfor, do not generate.
    my $wantspeaksfor = wantarray;
144 145 146 147 148 149 150 151 152 153

    my $speaker_signer = $GeniCredential::LOCALSA_FLAG;
    #
    # Utah; Guest users use the apt CA, and so we must sign the speaksfor
    # credential with the APT SA as well so that the target of the
    # speaksfor credential is in the same namespace as the signer.
    #
    if (!$geniuser->IsLocal() && $MAINSITE) {
	$speaker_signer = "/usr/testbed/etc/utah-apt.sa";
    }
154 155 156 157 158 159 160 161
    my $cachetag = $target->urn() . "::" . $geniuser->urn();
    
    #
    # If the target is a slice, and expired, change the expiration so
    # that the credential we generate is not also expired (and invalid).
    #
    if (ref($target) eq "GeniSlice" && $target->IsExpired()) {
	$oldexpires = $target->expires();
162
	$target->SetExpiration(time() + (24 * 3600));
163 164 165
	delete($credcache{$cachetag})
	    if (exists($credcache{$cachetag}));
    }
166
    #
167 168
    # Check caches. We save a lot of time by not regenerating all this
    # stuff every time in a long running poll!
169 170
    #
    if (exists($credcache{$cachetag})) {
171 172 173 174 175 176 177
	$credential = $credcache{$cachetag};
    }
    if (exists($speakcache{$geniuser->urn()})) {
	$speaksfor = $speakcache{$geniuser->urn()};
    }
    if (exists($certcache{$geniuser->urn()})) {
	$certificate = $certcache{$geniuser->urn()};
178 179
    }

180 181 182 183 184 185
    #
    # If a local user account, but a nonlocal id, then we should
    # have a speaksfor credential stored, as well as a certificate
    # for the user.
    #
    if ($geniuser->IsLocal() && $geniuser->emulab_user()->IsNonLocal()) {
186 187 188 189 190 191 192 193
	if (! (defined($certificate) && defined($speaksfor))) {
	    my ($speaksfor_string, $certificate_string) =
		$geniuser->emulab_user()->GetStoredCredential();
	    if (! (defined($speaksfor_string) &&
		   defined($certificate_string))) {
		print STDERR "No stored speaksfor/certificate for $geniuser\n";
		goto bad;
	    }
194
	    $speaksfor = GeniCredential->CreateFromSigned($speaksfor_string, 1);
195 196 197 198
	    if (!defined($speaksfor)) {
		print STDERR "Could not create speaksfor credential\n";
		goto bad;
	    }
199 200 201 202 203 204 205
	    $certificate = GeniCertificate->LoadFromString($certificate_string);
	    if (!defined($certificate)) {
		print STDERR "Could not load certificate from string\n";
		goto bad;
	    }
	    $speakcache{$geniuser->urn()} = $speaksfor;
	    $certcache{$geniuser->urn()}  = $certificate;
206
	}
207
	#
208 209 210
	# We need to generate an SA credential if either the speaksfor or
	# the user certificate is expired, and the caller is allowing the
	# use of an SA credential instead (as for terminate, etc). 
211
	#
212 213 214 215 216 217 218 219 220 221
	my $gensacert = 0;

	if ($certificate->IsExpired()) {
	    print STDERR "certificate for $geniuser has expired\n";
	    goto bad
		if (!$allowexpired);
	    
	    $gensacert = 1;
	}
	if ($wantspeaksfor && $speaksfor->IsExpired()) {
222 223
	    print STDERR "speaksfor credential for $geniuser has expired\n";
	    goto bad
224
		if (!$allowexpired);
225

226 227 228
	    $gensacert = 1;
	}
	if ($gensacert) {
229 230
	    # Be careful not to return this.
	    $speaksfor = undef;
231 232 233 234 235 236 237
	    
	    print STDERR "-> Generating an SA credential instead\n";
	    $credential = APT_Geni::GenAuthCredential($target, $privs);
	    if (!defined($credential)) {
		print STDERR "-> Could not generate SA credential!\n";
		goto bad;
	    }
238
	    # Would it make sense to put this in the cache?
239 240
	    goto cached;
	}
241 242 243
	else {
	    $credential = GeniCredential->Create($target, $certificate);
	}
244 245
    }
    else {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	if ($wantspeaksfor) {
	    #
	    # The Utah SA is always the speaker, even if the user is a guest
	    # with the alternate CA.
	    #
	    my $sa_certificate = GeniCertificate->LoadFromFile($SACERT);
	    if (!defined($sa_certificate)) {
		print STDERR "Could not load certificate from $SACERT\n";
		goto bad;
	    }
	    my $sa_authority = GeniAuthority->Lookup($sa_certificate->urn());
	    if (!defined($sa_authority)) {
		prnt STDERR "Could not load SA authority object\n";
		goto bad;
	    }
261 262 263 264 265

	    if ($USEABACCREDS) {
		$speaksfor = GenABACCredential($geniuser, $sa_authority);
		goto bad
		    if (!defined($speaksfor));
266
	    }
267 268 269 270 271 272 273 274 275 276 277
	    else {
		$speaksfor = GeniCredential->Create($geniuser, $sa_authority);
		if (!defined($speaksfor)) {
		    print STDERR "Could not create speaksfor credential\n";
		    goto bad;
		}
		$speaksfor->SetType("speaksfor");
		if ($speaksfor->Sign($speaker_signer)) {
		    print STDERR "Could not sign speaksfor credential\n";
		    goto bad;
		}
278
	    }
279
	    $speakcache{$geniuser->urn()} = $speaksfor;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	}
	$credential = GeniCredential->Create($target, $geniuser);
    }
    if (!defined($credential)) {
	print STDERR "Could not create credential for $target\n";
	goto bad;
    }
    # Add optional privs.
    if (defined($privs)) {
	foreach my $priv (@{ $privs }) {
	    $credential->AddCapability($priv, 0);
	}
    }
    # And sign it.
    if ($credential->Sign($GeniCredential::LOCALSA_FLAG) != 0) {
	$credential->Delete();
	print STDERR "Could not sign $target credential\n";
	goto bad;
    }
299
    $credcache{$cachetag} = $credential;
300
  cached:
301 302
    $target->SetExpiration($oldexpires)
	if (defined($oldexpires));
303 304 305 306
    if (wantarray) {
	return ($credential, $speaksfor);
    }
    return $credential;
307
  bad:
308 309
    $target->SetExpiration($oldexpires)
	if (defined($oldexpires));
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    return ();
}

#
# Return the authority object for a URN.
#
sub GetAuthority($)
{
    my ($urn) = @_;

    my $cm_authority = GeniAuthority->Lookup($urn);
    if (!defined($cm_authority)) {
	$cm_authority = GeniAuthority->CreateFromRegistry("cm", $urn);
	if (!defined($cm_authority)) {
	    print STDERR "Could not load CM authority object\n";
	    return undef;
	}
    }
    return $cm_authority;
}

#
# Load the context operate as; always the same for APT.
#
sub GeniContext()
{
    my $certificate = GeniCertificate->LoadFromFile($SACERT);
    if (!defined($certificate)) {
	print STDERR "Could not load certificate from $SACERT\n";
	return undef;
    }
    return Genixmlrpc->Context($certificate);
}

344 345 346 347 348
sub RootContext()
{
    return Genixmlrpc->Context($EMCERT, $EMKEY);
}

349 350 351 352 353 354
#
# Generate an SA credential (sorta like an admin credential).
#
sub GenAuthCredential($;$)
{
    my ($target, $privs) = @_;
355
    my $oldexpires;
Leigh Stoller's avatar
Leigh Stoller committed
356 357 358

    my $owner = GeniCertificate->LoadFromFile($SACERT);
    if (!defined($owner)) {
359 360 361
	print STDERR "Could not load certificate from $SACERT\n";
	return undef;
    }
362 363 364 365 366 367 368 369
    #
    # If the target is a slice, and expired, change the expiration so
    # that the credential we generate is not also expired (and invalid).
    #
    if (ref($target) eq "GeniSlice" && $target->IsExpired()) {
	$oldexpires = $target->expires();
	$target->SetExpiration(time() + 600);
    }
Leigh Stoller's avatar
Leigh Stoller committed
370
    my $credential = GeniCredential->Create($target, $owner);
371 372
    if (!defined($credential)) {
	print STDERR "Could not create credential for $target\n";
373
	goto bad;
374 375 376 377 378 379 380 381 382 383 384
    }
    # Add optional privs.
    if (defined($privs)) {
	foreach my $priv (@{ $privs }) {
	    $credential->AddCapability($priv, 0);
	}
    }
    # And sign it.
    if ($credential->Sign($GeniCredential::LOCALSA_FLAG) != 0) {
	$credential->Delete();
	print STDERR "Could not sign $target credential\n";
385
	goto bad;
386
    }
387 388
    $target->SetExpiration($oldexpires)
	if (defined($oldexpires));
389
    return $credential;
390 391 392 393
  bad:
    $target->SetExpiration($oldexpires)
	if (defined($oldexpires));
    return undef;
394 395
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 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 448
#
# Generate a real ABAC credential
#
sub GenABACCredential($$)
{
    my ($geniuser, $speaker) = @_;
    require ABAC;
    import ABAC;

    my $userfile = $geniuser->GetCertificate()->WriteToFile(1);
    if (!defined($userfile)) {
	print STDERR "Could not write user cert/key to file!\n";
	return undef;
    }
    my $speakerfile = $speaker->GetCertificate()->WriteToFile(1);
    if (!defined($speakerfile)) {
	print STDERR "Could not write speaker cert/key to file!\n";
	return undef;
    }
    my $abacuser = ABAC::ID->new($userfile);
    if (!defined($abacuser)) {
	print STDERR "Could not create user ABAC:ID for $geniuser\n";
	return undef;
    }
    $abacuser->load_privkey($userfile);

    my $abactool = ABAC::ID->new($speakerfile);
    if (!defined($abactool)) {
	print STDERR "Could not create speaker ABAC:ID\n";
	return undef;
    }

    my $abacattr = ABAC::Attribute->new($abacuser,
					"speaks_for_" . $abacuser->keyid(),
					30 * 24 * 60 * 60);
    if (!defined($abacattr)) {
	print STDERR "Could not create ABAC::Attribute\n";
	return undef;
    }
    
    $abacattr->principal($abactool->keyid());
    $abacattr->bake();
    my $xml  = $abacattr->cert_chunk();
    my $cred = GeniCredential->CreateFromSigned($xml);
    if (!defined($cred)) {
	print STDERR "Could not create ABAC credential from $xml\n";
	return undef;
    }
    $cred->SetTargetCert($geniuser->GetCertificate());
    $cred->SetOwnerCert($speaker->GetCertificate());
    return $cred;
}

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 482 483 484 485 486 487 488 489
#
# Generate a project credential for a user. 
#
sub GenProjectCredential($$)
{
    my ($project, $geniuser) = @_;
    my $pid    = $project->pid();
    my $urn    = $project->urn();
    my $error;

    my $certificate =
	GeniCertificate->Create({'urn'  => $urn,
				 'hrn'  => "${PGENIDOMAIN}.project.${pid}",
				 'email'=> $TBOPS,
				 "nostore" => 1,
				}, \$error);
    if (!defined($certificate)) {
	print STDERR "Could not create new certificate for $urn\n";
	return undef;
    }
    my ($credential, $speaksfor) = GenCredentials($certificate, $geniuser);
    return undef
	if (!defined($credential));

    return ($credential, $speaksfor);
}

#
# Generate a user (self) credential for a user.
#
sub GenUserCredential($)
{
    my ($geniuser) = @_;

    my ($credential, $speaksfor) = GenCredentials($geniuser, $geniuser);
    return undef
	if (!defined($credential));

    return ($credential, $speaksfor);
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
#
# Create a user at a cluster via the Cluster RPC Server. The point
# of this is so that the admin user we are operating as exists at
# the cluster when we make the admin level call via using the root
# certificate. Not the best approach, but best I could think of.
#
sub CreatePortalUser($$)
{
    my ($authority, $geniuser) =  @_;
    my $context = APT_Geni::GeniContext();

    my ($credential,$speaksfor) =
	APT_Geni::GenUserCredential($geniuser);
    return -1
	if (!defined($credential));

    my $credentials = [$credential->asString()];
    if (defined($speaksfor)) {
	$credentials = [@$credentials, $speaksfor->asString()];
    }
    my $args = {"credentials" => $credentials};
    my $response = PortalRPC($authority, $context, "CreateUser", $args);
    return $response->code();
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
#
# RPC to the Cluster RPC server.
#
sub PortalRPC($$$@)
{
    my ($authority, $context, $method, @args) = @_;
    my $cmurl = $authority->url();
    $cmurl =~ s/\/cm$/\/cluster/;
    if ($usemydevtree) {
	$cmurl =~ s/protogeni/protogeni\/stoller/;
    }
    #
    # We use the root context to talk to the Cluster RPC server
    #
    if (!defined($context)) {
	$context = RootContext();
	if (!defined($context)) {
532
	    return GeniResponse->Create(GENIRESPONSE_ERROR(), undef,
533 534 535 536 537 538
					"Could not get root context for RPC");
	}
    }
    my $response = Genixmlrpc::CallMethod($cmurl, $context, $method, @args);
    if ($response->code() != GENIRESPONSE_SUCCESS()) {
	if (!defined($response->output())) {
539
	    $response->output(GENIRESPONSE_STRING($response->code()));
540 541 542 543 544
	}
    }
    return $response;
}

545 546 547 548 549 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
#
# Ping an aggregate to see if its alive, using getversion.
#
sub PingAggregate($$;$)
{
    my ($aggregate, $perrmsg, $portalrpc) = @_;
    my $authority   = GetAuthority($aggregate->urn());
    my $context     = RootContext();

    if (!defined($authority)) {
	$$perrmsg = "Could not lookup authority for $aggregate!";
	return -1;
    }
    if (!defined($context)) {
	$$perrmsg = "Could generate root context!";
	return -1;
    }
    my $cmurl = $authority->url();
    if (defined($portalrpc)) {
	$cmurl =~ s/\/cm$/\/cluster/;
    }
    if ($usemydevtree) {
	$cmurl =~ s/protogeni/protogeni\/stoller/;
    }
    my $oldto = Genixmlrpc->SetTimeout(10);
    my $response = Genixmlrpc::CallMethod($cmurl, $context, "GetVersion");
    Genixmlrpc->SetTimeout($oldto);

    # Success is good!
    return 0
	if ($response->code() == GENIRESPONSE_SUCCESS());

    #print STDERR Dumper($response);

    $$perrmsg = $response->error();
    return $response->code();
}

583 584
# _Always_ make sure that this 1 is at the end of the file...
1;