GeniStdSA.pm.in 13.9 KB
Newer Older
Jonathon Duerig's avatar
Jonathon Duerig committed
1 2 3 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
#!/usr/bin/perl -wT
#
# Copyright (c) 2008-2013 University of Utah and the Flux Group.
# 
# {{{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.
# 
# }}}
#
30
package GeniStdSA;
Jonathon Duerig's avatar
Jonathon Duerig committed
31 32 33 34 35 36 37 38 39 40 41 42

#
# The server side of the CM interface on remote sites. Also communicates
# with the GMC interface at Geni Central as a client.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

43
use GeniStd;
Jonathon Duerig's avatar
Jonathon Duerig committed
44
use GeniSA;
45
use GeniSlice;
Jonathon Duerig's avatar
Jonathon Duerig committed
46 47 48 49
use GeniResponse;
use GeniCredential;
use GeniRegistry;
use emutil;
50
use Data::Dumper;
Jonathon Duerig's avatar
Jonathon Duerig committed
51

52 53
my $coder = Frontier::RPC2->new('use_objects' => 1);

Jonathon Duerig's avatar
Jonathon Duerig committed
54 55 56
sub GetVersion()
{
    my $blob = {
57 58 59
	"VERSION" => $coder->string("0.1"),
	"SERVICES" => ["SLICE", "SLIVER_INFO", "SLICE_MEMBER"],
	"CREDENTIAL_TYPES" => ["SFA"], # => [$coder->string("3")]],
Jonathon Duerig's avatar
Jonathon Duerig committed
60 61 62 63 64
	"ROLES" => ["AUTHORITY", "ADMIN", "MEMBER"]
    };
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $blob);
}

65
sub CreateSlice($$)
Jonathon Duerig's avatar
Jonathon Duerig committed
66
{
67 68 69 70 71 72 73 74 75 76
    my ($credential_args, $options) = @_;
    if (! defined($credential_args) ||
	! defined($options) ||
	! exists($options->{'fields'}) ||
	! exists($options->{'fields'}->{'SLICE_NAME'}))
    {
	return GeniResponse->MalformedArgsResponse('Requires a list of credentials, an options field, and a SLICE_NAME in the options field');
    }

    my $credential = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));
Jonathon Duerig's avatar
Jonathon Duerig committed
77 78
    return $credential
	if (GeniResponse::IsResponse($credential));
79
    my $hrn = $options->{'fields'}->{'SLICE_NAME'};
Jonathon Duerig's avatar
Jonathon Duerig committed
80
    my $args = {
81 82 83
	"credential" => $credential->asString(),
	"hrn" => $hrn,
	"type" => "slice"
Jonathon Duerig's avatar
Jonathon Duerig committed
84
    };
85 86 87
    if (exists($options->{'fields'}->{'SLICE_EXPIRATION'})) {
	$args->{'expiration'} = $options->{'fields'}->{'SLICE_EXPIRATION'};
    }
Jonathon Duerig's avatar
Jonathon Duerig committed
88
    my $response = GeniSA::Register($args);
89 90
    if (GeniResponse::IsError($response)) {
	return $response;
Jonathon Duerig's avatar
Jonathon Duerig committed
91
    }
92 93

    my $sliceCred = GeniCredential->CreateFromSigned(GeniResponse::value($response));
94 95 96 97 98 99
    my $slice = GeniSlice->Lookup($sliceCred->target_urn());
    if (exists($options->{'fields'}->{'SLICE_DESCRIPTION'})) {
	my $description = $options->{'fields'}->{'SLICE_DESCRIPTION'};
	$slice->SetDescription($description);
    }

100 101
    my $blob = {
	"SLICE_URN" => $sliceCred->target_urn(),
102 103
	"SLICE_EXPIRATION" => $sliceCred->expires()
#	,"SLICE_CREDENTIAL" => GeniResponse::value($response)
104 105
    };
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $blob);
Jonathon Duerig's avatar
Jonathon Duerig committed
106 107 108 109 110 111
}

sub LookupSlices()
{
    my ($credential_args, $options) = @_;

112
    my $credential = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));
Jonathon Duerig's avatar
Jonathon Duerig committed
113 114 115
    return $credential
	if (GeniResponse::IsResponse($credential));

116
    # TODO: Make sure that slice URN is the same as the credential URN
Jonathon Duerig's avatar
Jonathon Duerig committed
117 118 119 120 121
    $credential->HasPrivilege( "authority" ) or
	$credential->HasPrivilege( "resolve" ) or
	return GeniResponse->Create( GENIRESPONSE_FORBIDDEN, undef,
				     "Insufficient privilege" );

122
    my ($match, $filter) = GeniStd::GetMatchFilter($options);
Jonathon Duerig's avatar
Jonathon Duerig committed
123 124

    my $members = {};
125
    if (defined($match)) {
Jonathon Duerig's avatar
Jonathon Duerig committed
126
	foreach my $key (@{ $match }) {
127
	    my $slice = GeniSlice->Lookup($key);
Jonathon Duerig's avatar
Jonathon Duerig committed
128
	    if (defined($slice)) {
129 130 131 132 133 134 135 136 137
		my $description = '';
		if (defined($slice->description())) {
		    $description = $slice->description();
		}
		my $isExpired = 'False';
		if ($slice->IsExpired()) {
		    $isExpired = 'True';
		}

138 139 140 141 142
		my $completeblob = {
		    "SLICE_URN"         => $slice->urn(),
		    "SLICE_UID"         => $slice->uuid(),
		    "SLICE_CREATION"    => $slice->created(),
		    "SLICE_EXPIRATION"  => $slice->expires(),
143
		    "SLICE_EXPIRED"     => $isExpired,
144
		    "SLICE_NAME"        => $slice->hrn(),
145
		    "SLICE_DESCRIPTION" => $description,
146 147 148 149
		    "SLICE_PROJECT_URN" => "Unimplemented"
		};
		my $blob = GeniStd::FilterFields($completeblob, $filter);
		$members->{$slice->urn()} = $blob;
Jonathon Duerig's avatar
Jonathon Duerig committed
150 151 152 153 154 155 156 157 158
	    }
	}
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $members);
}

sub UpdateSlice()
{
    my ($slice_urn, $credential_args, $options) = @_;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    my $credential = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));
    return $credential
	if (GeniResponse::IsResponse($credential));

    # TODO: Make sure that slice URN is the same as the credential URN
    my $slice = GeniSlice->Lookup($slice_urn);
    my $response;
    if (exists($options->{'fields'}->{'SLICE_DESCRIPTION'})) {
	$slice->SetDescription($options->{'fields'}->{'SLICE_DESCRIPTION'});
    }
    if (exists($options->{'fields'}->{'SLICE_EXPIRES'})) {
	my $args = {
	    "credential" => $credential->asString(),
	    "expiration" => $options->{'fields'}->{'SLICE_EXPIRES'}
	};
	$response = GeniSA::RenewSlice($args);
    }

    return $response
	if (GeniResponse::IsError($response));

    return GeniResponse->Create(GENIRESPONSE_SUCCESS, {});
Jonathon Duerig's avatar
Jonathon Duerig committed
181 182 183 184 185
}

sub GetCredentials()
{
    my ($slice_urn, $credential_args, $options) = @_;
186 187 188 189 190 191
    if (! defined($slice_urn) ||
	! defined($credential_args) ||
	! defined($options))
    {
	return GeniResponse->MalformedArgsResponse('Requires a slice urn, a list of credentials, and an options field');
    }
Jonathon Duerig's avatar
Jonathon Duerig committed
192

193
    my $credential = GeniSA::GetCredential({ "urn" => $slice_urn });
Jonathon Duerig's avatar
Jonathon Duerig committed
194
    return $credential
195
	if (GeniResponse::IsError($credential));
Jonathon Duerig's avatar
Jonathon Duerig committed
196 197

    my $blob = {
198 199 200
	"geni_type" => "geni_sfa",
	"geni_version" => $coder->string("3"),
	"geni_value" => $credential->{"value"}
Jonathon Duerig's avatar
Jonathon Duerig committed
201 202 203 204 205 206 207
    };

    return GeniResponse->Create(GENIRESPONSE_SUCCESS, [$blob]);
}

sub ModifySliceMembership()
{
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    my ($slice_urn, $credential_args, $options) = @_;
    if (! defined($slice_urn) ||
	! defined($credential_args) ||
	! defined($options))
    {
	return GeniResponse->MalformedArgsResponse('Requires a slice urn, a list of credentials, and an options field');
    }

    my ($credential) = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));

    my $adding = $options->{'members_to_add'};
    my $removing = $options->{'members_to_remove'};
    my $changing = $options ->{'members_to_change'};

    if (defined($removing) && scalar(@{ $removing }) > 0) {
	return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				 "Not implemented: Remove members from slice");
	
    }
    if (defined($changing) && scalar(@{ $changing }) > 0) {
	return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				   "Not implemented: Change members in slice");
	
    }
    if (! defined($adding)) {
	return GeniResponse->Create(GENIRESPONSE_SUCCESS, 0);
    }

    my $params = {
	"credential" => $credential->asString()
    };

    my $i = 0;
    foreach my $current (@{ $adding }) {
	if (exists($adding->{'SLICE_MEMBER'})) {
	    $params->{"urn"} = $adding->{'SLICE_MEMBER'};
	    my $result = GeniSA::BindToSlice($params);
	    if (GeniResponse::IsError($result)) {
		return $result;
	    }
	}
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, 0);
Jonathon Duerig's avatar
Jonathon Duerig committed
251 252 253 254
}

sub LookupSliceMembers()
{
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    my ($slice_urn, $credential_args, $options) = @_;
    if (! defined($slice_urn) ||
	! defined($credential_args) ||
	! defined($options))
    {
	return GeniResponse->MalformedArgsResponse('Requires a slice urn, a list of credentials, and an options field');
    }

    my ($cred) = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));
    my $credential = GeniCredential::CheckCredential($cred->asString());
    return $credential
	if (GeniResponse::IsResponse($credential));

    $credential->HasPrivilege( "pi" ) or
	$credential->HasPrivilege( "bind" ) or
	return GeniResponse->Create( GENIRESPONSE_FORBIDDEN, undef,
				     "Insufficient privilege" );
    
    my $this_user = GeniUser->Lookup($ENV{'GENIURN'}, 1);
    if (!defined($this_user)) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Who are you? No local record");
    }

279 280 281 282 283
    # TODO: How do we validate slice urn?
#    if ($credential->target_urn() ne $slice_urn) {
#	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
#				    "Slice URN does not match credential URN");
#    }
284

285
    my $slice = GeniSlice->Lookup($slice_urn);
286 287 288 289 290 291 292 293 294 295 296
    if (!defined($slice)) {
	return GeniResponse->Create(GENIRESPONSE_SEARCHFAILED, undef,
				    "Unknown slice for this credential");
    }

    my $uuids = [];
    my $error = $slice->UserBindings($uuids);
    if ($error != 0) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Failed to lookup member bindings");
    }
297
    push(@{ $uuids }, $this_user->uuid());
298 299 300 301 302 303 304 305 306

    my $result = [];
    foreach my $id (@{ $uuids }) {
	my $user = GeniUser->Lookup($id, 1);
	if (defined($user)) {
	    push(@{ $result }, { 'SLICE_MEMBER' => $user->urn(),
				 'SLICE_ROLE' => 'MEMBER' });
	}
    }
307
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $result);
Jonathon Duerig's avatar
Jonathon Duerig committed
308 309
}

310
sub LookupSlicesForMember($$$)
Jonathon Duerig's avatar
Jonathon Duerig committed
311
{
312
    my ($member_urn, $credential_args, $options) = @_;
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    if (! defined($member_urn) ||
	! defined($credential_args) ||
	! defined($options))
    {
	return GeniResponse->MalformedArgsResponse('Requires a member urn, a list of credentials, and an options field');
    }

    my ($cred) = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));
    my $credential = GeniCredential::CheckCredential($cred->asString());
    return $credential
	if (GeniResponse::IsResponse($credential));

    if ($ENV{'GENIURN'} ne $member_urn) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "You are not allowed to lookup slices for other members");
    }

    my $this_user = GeniUser->Lookup($ENV{'GENIURN'}, 1);
    if (!defined($this_user)) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Who are you? No local record");
    }

    my $result = [];

    my @created = GeniSlice->LookupByCreator($this_user);
    my @bound = GeniSlice->BoundToUser($this_user);

    addSlicesToMemberList(\@created, $result)
	if (defined(@created));
    addSlicesToMemberList(\@bound, $result)
	if (defined(@bound));

    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $result);
}

sub addSlicesToMemberList($$)
{
    my ($slices, $result) = @_;
    
    foreach my $slice (@{ $slices }) {
	my $blob = {
	    'SLICE_URN' => $slice->urn(),
	    'SLICE_ROLE' => 'MEMBER'
	};
	push(@{ $result }, $blob);
    }
Jonathon Duerig's avatar
Jonathon Duerig committed
360 361
}

362
sub CreateSliverInfo($$$$)
Jonathon Duerig's avatar
Jonathon Duerig committed
363
{
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    my ($credential_args, $options) = @_;
    if (! defined($credential_args) ||
	! defined($options))
    {
	return GeniResponse->MalformedArgsResponse('Requires a list of credentials, and an options field');
    }

    my ($credential) = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));
    if (! defined($options->{'SLIVER_INFO_SLICE_URN'}) ||
	! defined($options->{'SLIVER_INFO_URN'}) ||
	! defined($options->{'SLIVER_INFO_AGGREGATE_URN'}) ||
	! defined($options->{'SLIVER_INFO_CREATOR_URN'}) ||
	! defined($options->{'SLIVER_INFO_CREATION'}) ||
	! defined($options->{'SLIVER_INFO_EXPIRATION'})) {
	return GeniResponse->MalformedArgsResponse('Required option is missing. Make sure to include SLIVER_INFO_SLICE_URN, SLIVER_INFO_URN, SLIVER_INFO_AGGREGATE_URN, SLIVER_INFO_CREATOR_URN, CREATION, and EXPIRATION');
    }
    my $params = {
	'credential' => $credential->asString(),
	'slice_urn' => $options->{'SLIVER_INFO_SLICE_URN'},
	'creator_urn' => $options->{'SLIVER_INFO_CREATOR_URN'},
	'urn' => $options->{'SLIVER_INFO_URN'},
	'created' => $options->{'SLIVER_INFO_CREATION'},
	'expires' => $options->{'SLIVER_INFO_EXPIRATION'}
    };
    return GeniSA::RegisterSliver($params);
}

sub UpdateSliverInfo($$)
{
    my ($credential_args, $options) = @_;
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				"Update Sliver Info is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
396 397
}

398
sub DeleteSliverInfo()
Jonathon Duerig's avatar
Jonathon Duerig committed
399
{
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    my ($slice_urn, $aggregate_url, $credential_args, $options) = @_;
    if (! defined($slice_urn) ||
	! defined($aggregate_url) ||
	! defined($credential_args) ||
	! defined($options))
    {
	return GeniResponse->MalformedArgsResponse('Requires a slice urn, an aggregate url, a list of credentials, and an options field');
    }

    my ($credential) = GeniStd::CheckCredentials(GeniStd::FilterCredentials($credential_args));
    my $params = {
	'credential' => $credential->asString(),
	'slice_urn' => $slice_urn
    };
    return GeniSA::UnRegisterSliver($params);
Jonathon Duerig's avatar
Jonathon Duerig committed
415 416
}

417
sub LookupSliverInfo($$)
Jonathon Duerig's avatar
Jonathon Duerig committed
418
{
419 420 421
    my ($credential_args, $options) = @_;
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				"Lookup Sliver Info is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
422 423 424 425
}

sub CreateProject()
{
426 427
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				"Create Project is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
428 429 430 431
}

sub LookupProjects()
{
432 433
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				"Lookup Projects is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
434 435 436 437
}

sub UpdateProject()
{
438 439
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				"Update Project is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
440 441 442 443
}

sub ModifyProjectMembership()
{
444 445
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				"Modify Project is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
446 447 448 449
}

sub LookupProjectMembers()
{
450 451
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
				"Lookup Project is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
452 453 454 455
}

sub LookupProjectsForMember()
{
456 457
    return GeniResponse->Create(GENIRESPONSE_NOT_IMPLEMENTED, undef,
			      "Lookup Projects for Member is not implemented");
Jonathon Duerig's avatar
Jonathon Duerig committed
458
}
459 460 461

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