blib/lib/VM/EC2/Security/Token.pm untested

filter: Statementstmt 0.0% 0 / 27 Branchbran n/a Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 55 CC 1 SCAR 6.9
CC
1
Cov
0%
valid_fields
2.0
new_ec2
2.0
credentials
2.0
CRAP 2.0
1 package VM::EC2::Security::Token;
2  
3 =head1 NAME
4  
5 VM::EC2::Security::Token - Temporary security token object
6  
7 =head1 SYNOPSIS
8  
9 use VM::EC2;
10 use VM::EC2::Security::Policy
11  
12 # under your account
13 $ec2 = VM::EC2->new(...); # as usual
14 my $policy = VM::EC2::Security::Policy->new;
15 $policy->allow('DescribeImages','RunInstances');
16 my $token = $ec2->get_federation_token(-name => 'TemporaryUser',
17 -duration => 60*60*3, # 3 hrs, as seconds
18 -policy => $policy);
19 print $token->sessionToken,"\n";
20 print $token->accessKeyId,"\n";
21 print $token->secretAccessKey,"\n";
22 print $token->federatedUser,"\n";
23  
24 my $serialized = $token->credentials->serialize;
25  
26 # get the serialized token to the temporary user
27 send_data_to_user_somehow($serialized);
28  
29 # under the temporary user's account
30 my $serialized = get_data_somehow();
31  
32 # create a copy of the token from its serialized form
33 my $token = VM::EC2::Security::Credentials->new_from_serialized($serialized);
34  
35 # open a new EC2 connection with this token. User will be
36 # able to run all the methods specified in the policy.
37 my $ec2 = VM::EC2->new(-security_token => $token);
38 print $ec2->describe_images(-owner=>'self');
39  
40 # convenience routine; will return a VM::EC2 object authorized
41 # to use the current token
42 my $ec2 = $token->new_ec2;
43 print $ec2->describe_images(-owner=>'self');
44  
45 =head1 DESCRIPTION
46  
47 VM::EC2::Security::Token objects allow you to grant a user access to
48 some or all of your EC2 resources for a limited period of time. The
49 user does not have to have his own AWS account.
50  
51 Token objects are returned by calls to VM::EC2->get_federation_token()
52 and get_session_token(). The former call is used to create a temporary
53 user with privileges restricted to those listed in the accompanying
54 policy (a VM::EC2::Security::Policy object). The latter call is used
55 in conjunction with multi-factor authentication devices, such as smart
56 cards. The tokens returned by get_session_token() are not associated
57 with a user account nor a policy, and grant privileges to all EC2
58 actions and resources. Both federation and session tokens have an
59 expiry time between a few seconds and 36 hours.
60  
61 A VM::EC2::Security::Credentials object contained within the token
62 contains the temporary secret access key, acess key ID, and a session
63 token string that unlocks the access key. The credentials object can
64 be serialized into a form suitable for sending to a user via a secure
65 channel, such as SSL or S/MIME e-mail, and unserialized at the
66 receiving end into a copy of the original credentials object.
67  
68 Either the token object, or its contained credentials object can be
69 used passed to VM::EC2->new() via the B<-security_token> parameter in
70 order to gain access to EC2 resources.
71  
72 =head1 METHODS
73  
74 credentials() -- The VM::EC2::Security::Credentials object
75 that contains the session token, access key ID,
76 and secret key.
77  
78 federatedUser() -- the VM::EC2::Security::FederatedUser object that
79 contains information about the temporary user
80 account.
81  
82 packedPolicySize() -- A percentage value indicating the size of the policy in
83 packed form relative to the maximum allowed size.
84 Policies in excess of 100% will be rejected by the
85 service.
86  
87 secret_access_key()-- Convenience method that calls the credentials object's
88 secret_access_key() method.
89  
90 access_key_id() -- Convenience method that calls the credentials object's
91 access_key_id() method.
92  
93 session_token() -- Convenience method that calls the credentials object's
94 session_token() method.
95  
96 new_ec2(@args) -- Convenience method that returns a VM::EC2 object authorized
97 with the current token. You may pass any of the arguments
98 accepted by VM::EC2->new(), except that -access_key and
99 -secret_key will be ignored if present.
100  
101 =head1 STRING OVERLOADING
102  
103 When used in a string context, this object will interpolate as the
104 session token, and can be used for the -security_token parameter in
105 VM::EC2->new().
106  
107 =head1 SEE ALSO
108  
109 L<VM::EC2>
110 L<VM::EC2::Generic>
111 L<VM::EC2::Security::Credentials>
112 L<VM::EC2::Security::FederatedUser>
113  
114 =head1 AUTHOR
115  
116 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
117  
118 Copyright (c) 2011 Ontario Institute for Cancer Research
119  
120 This package and its accompanying libraries is free software; you can
121 redistribute it and/or modify it under the terms of the GPL (either
122 version 1, or at your option, any later version) or the Artistic
123 License 2.0. Refer to LICENSE for the full license text. In addition,
124 please see DISCLAIMER.txt for disclaimers of warranty.
125  
126 =cut
127  
128 use strict;
129 use base 'VM::EC2::Generic';
130 use VM::EC2::Security::FederatedUser;
131 use VM::EC2::Security::Credentials;
132  
133 sub valid_fields {
134 my $self = shift;
135 return qw(Credentials FederatedUser PackedPolicySize);
136 }
137  
138 sub new_ec2 {
139 shift->credentials->new_ec2(@_);
140 }
141  
142 sub credentials {
143 my $self = shift;
144 return VM::EC2::Security::Credentials->new($self->SUPER::credentials,undef);
145 }
146  
147 sub federated_user {
148 my $self = shift;
149 my $user = $self->SUPER::federated_user or return;
150 return VM::EC2::Security::FederatedUser->new($user,undef);
151 }
152  
153 sub secret_access_key { shift->credentials->SecretAccessKey }
154 sub access_key_id { shift->credentials->AccessKeyId }
155 sub session_token { shift->credentials->SessionToken }
156 sub secretAccessKey { shift->secret_access_key }
157 sub accessKeyId { shift->access_key_id }
158 sub sessionToken { shift->session_token }
159  
160 sub short_name { shift->session_token; }
161  
162 1;