| 1 | package VM::EC2::REST::security_token; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 7 | ▶ | use VM::EC2 ''; # important not to import anything! | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | package VM::EC2; # add methods to VM::EC2 | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 7 | VM::EC2::Dispatch->register( | |||||||||||||||||||
| 8 | GetFederationToken => 'fetch_one,GetFederationTokenResult,VM::EC2::Security::Token', | ||||||||||||||||||||
| 9 | GetSessionToken => 'fetch_one,GetSessionTokenResult,VM::EC2::Security::Token', | ||||||||||||||||||||
| 10 | ); | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub sts_call { | ||||||||||||||||||||
| 13 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
sts_call: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 14 | 0 | local $self->{endpoint} = 'https://sts.amazonaws.com'; | |||||||||||||||||||
| 15 | 0 | local $self->{version} = '2011-06-15'; | |||||||||||||||||||
| 16 | 0 | $self->call(@_); | |||||||||||||||||||
| 17 | } | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NAME VM::EC2::REST::security_token | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | use VM::EC2 qw(:standard); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 EC2 REGIONS AND AVAILABILITY ZONES | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | AWS security tokens provide a way to grant temporary access to | ||||||||||||||||||||
| 28 | resources in your EC2 space without giving them permanent | ||||||||||||||||||||
| 29 | accounts. They also provide the foundation for mobile services and | ||||||||||||||||||||
| 30 | multifactor authentication devices (MFA). | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | Used in conjunction with VM::EC2::Security::Policy and | ||||||||||||||||||||
| 33 | VM::EC2::Security::Credentials, you can create a temporary user who is | ||||||||||||||||||||
| 34 | authenticated for a limited length of time and pass the credentials to | ||||||||||||||||||||
| 35 | him or her via a secure channel. He or she can then create a | ||||||||||||||||||||
| 36 | credentials object to access your AWS resources. | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Here is an example: | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # on your side of the connection | ||||||||||||||||||||
| 41 | $ec2 = VM::EC2->new(...); # as usual | ||||||||||||||||||||
| 42 | my $policy = VM::EC2::Security::Policy->new; | ||||||||||||||||||||
| 43 | $policy->allow('DescribeImages','RunInstances'); | ||||||||||||||||||||
| 44 | my $token = $ec2->get_federation_token(-name => 'TemporaryUser', | ||||||||||||||||||||
| 45 | -duration => 60*60*3, # 3 hrs, as seconds | ||||||||||||||||||||
| 46 | -policy => $policy); | ||||||||||||||||||||
| 47 | my $serialized = $token->credentials->serialize; | ||||||||||||||||||||
| 48 | send_data_to_user_somehow($serialized); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # on the temporary user's side of the connection | ||||||||||||||||||||
| 51 | my $serialized = get_data_somehow(); | ||||||||||||||||||||
| 52 | my $token = VM::EC2::Security::Credentials->new_from_serialized($serialized); | ||||||||||||||||||||
| 53 | my $ec2 = VM::EC2->new(-security_token => $token); | ||||||||||||||||||||
| 54 | print $ec2->describe_images(-owner=>'self'); | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | For temporary users who are not using the Perl VM::EC2 API, you can | ||||||||||||||||||||
| 57 | transmit the required fields individually: | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | my $credentials = $token->credentials; | ||||||||||||||||||||
| 60 | my $access_key_id = $credentials->accessKeyId; | ||||||||||||||||||||
| 61 | my $secret_key = $credentials->secretKey; | ||||||||||||||||||||
| 62 | my $session_token = $credentials->sessionToken; | ||||||||||||||||||||
| 63 | send_data_to_user_somehow($session_token, | ||||||||||||||||||||
| 64 | $access_key_id, | ||||||||||||||||||||
| 65 | $secret_key); | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Calls to get_federation_token() return a VM::EC2::Security::Token | ||||||||||||||||||||
| 68 | object. This object contains two sub-objects, a | ||||||||||||||||||||
| 69 | VM::EC2::Security::Credentials object, and a | ||||||||||||||||||||
| 70 | VM::EC2::Security::FederatedUser object. The Credentials object | ||||||||||||||||||||
| 71 | contains a temporary access key ID, secret access key, and session | ||||||||||||||||||||
| 72 | token which together can be used to authenticate to the EC2 API. The | ||||||||||||||||||||
| 73 | FederatedUser object contains the temporary user account name and ID. | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | See L<VM::EC2::Security::Token>, L<VM::EC2::Security::FederatedUser>, | ||||||||||||||||||||
| 76 | L<VM::EC2::Security::Credentials>, and L<VM::EC2::Security::Policy>. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Implemented: | ||||||||||||||||||||
| 79 | GetFederationToken | ||||||||||||||||||||
| 80 | GetSessionToken | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Unimplemented: | ||||||||||||||||||||
| 83 | (none) | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head2 $token = $ec2->get_federation_token($username) | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head2 $token = $ec2->get_federation_token(-name=>$username,@args) | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | This method creates a new temporary user under the provided username | ||||||||||||||||||||
| 92 | and returns a VM::EC2::Security::Token object that contains temporary | ||||||||||||||||||||
| 93 | credentials for the user, as well as information about the user's | ||||||||||||||||||||
| 94 | account. Other options allow you to control the duration for which the | ||||||||||||||||||||
| 95 | credentials will be valid, and the policy the controls what resources | ||||||||||||||||||||
| 96 | the user is allowed to access. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =over 4 | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =item Required arguments: | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | -name The username | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | The username must comply with the guidelines described in | ||||||||||||||||||||
| 105 | http://docs.amazonwebservices.com/IAM/latest/UserGuide/LimitationsOnEntities.html: | ||||||||||||||||||||
| 106 | essentially all alphanumeric plus the characters [+=,.@-]. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =item Optional arguments: | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | -duration_seconds Length of time the session token will be valid for, | ||||||||||||||||||||
| 111 | expressed in seconds. | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | -duration Same thing, faster to type. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | -policy A VM::EC2::Security::Policy object, or a JSON string | ||||||||||||||||||||
| 116 | complying with the IAM policy syntax. | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | The duration must be no shorter than 1 hour (3600 seconds) and no | ||||||||||||||||||||
| 119 | longer than 36 hours (129600 seconds). If no duration is specified, | ||||||||||||||||||||
| 120 | Amazon will default to 12 hours. If no policy is provided, then the | ||||||||||||||||||||
| 121 | user will not be able to execute B<any> actions. | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | Note that if the temporary user wishes to create a VM::EC2 object and | ||||||||||||||||||||
| 124 | specify a region name at create time | ||||||||||||||||||||
| 125 | (e.g. VM::EC2->new(-region=>'us-west-1'), then the user must have | ||||||||||||||||||||
| 126 | access to the DescribeRegions action: | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | $policy->allow('DescribeRegions') | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | Otherwise the call to new() will fail. | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =back | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | sub get_federation_token { | ||||||||||||||||||||
| 137 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
get_federation_token: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 138 | 0 | my %args = $self->args('-name',@_); | |||||||||||||||||||
| 139 | 0 | ▶ | $args{-name} or croak "Usage: get_federation_token(-name=>\$name,\@more_args)"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 0 | ▶ | $args{-duration_seconds} ||= $args{-duration}; | ||||||||||||||||||
|
Condition0%0/3
$args{"-duration_seconds"} ||= $args{"-duration"}
MC/DC0%0/2
$args{"-duration_seconds"} ||= $args{"-duration"}
$args{"-duration_seconds"}
$args{"-duration"}
Truth table0%0/3
A ||= B
A= $args{"-duration_seconds"}B= $args{"-duration"}
| |||||||||||||||||||||
| 141 | 0 | my @p = map {$self->single_parm($_,\%args)} qw(Name DurationSeconds Policy); | |||||||||||||||||||
| 142 | 0 | return $self->sts_call('GetFederationToken',@p); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head2 $token = $ec2->get_session_token(%args) | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | This method creates a temporary VM::EC2::Security::Token object for an | ||||||||||||||||||||
| 148 | anonymous user. The token has no policy associated with it, and can be | ||||||||||||||||||||
| 149 | used to run any of the EC2 actions available to the user who created | ||||||||||||||||||||
| 150 | the token. Optional arguments allow the session token to be used in | ||||||||||||||||||||
| 151 | conjunction with MFA devices. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =over 4 | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =item Required arguments: | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | none | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =item Optional arguments: | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | -duration_seconds Length of time the session token will be valid for, | ||||||||||||||||||||
| 162 | expressed in seconds. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | -duration Same thing, faster to type. | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | -serial_number The identification number of the user's MFA device, | ||||||||||||||||||||
| 167 | if any. | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | -token_code The code provided by the MFA device, if any. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | If no duration is specified, Amazon will default to 12 hours. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | See | ||||||||||||||||||||
| 174 | http://docs.amazonwebservices.com/IAM/latest/UserGuide/Using_ManagingMFA.html | ||||||||||||||||||||
| 175 | for information on using AWS in conjunction with MFA devices. | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =back | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | sub get_session_token { | ||||||||||||||||||||
| 182 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
get_session_token: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 183 | 0 | my %args = @_; | |||||||||||||||||||
| 184 | 0 | my @p = map {$self->single_parm($_,\%args)} qw(SerialNumber DurationSeconds TokenCode); | |||||||||||||||||||
| 185 | 0 | return $self->sts_call('GetSessionToken',@p); | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head1 SEE ALSO | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | L<VM::EC2> | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head1 AUTHOR | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | This package and its accompanying libraries is free software; you can | ||||||||||||||||||||
| 199 | redistribute it and/or modify it under the terms of the GPL (either | ||||||||||||||||||||
| 200 | version 1, or at your option, any later version) or the Artistic | ||||||||||||||||||||
| 201 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||||||||||||||||||||
| 202 | please see DISCLAIMER.txt for disclaimers of warranty. | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 7 | 1; | |||||||||||||||||||