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

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 68 CC 3 SCAR 13.9
CC
3
Cov
0%
new_from_json
12.0
valid_fields
2.0
secret_access_key
2.0
CRAP 4.0
1 package VM::EC2::Security::Credentials;
2  
3 =head1 NAME
4  
5 VM::EC2::Security::Credentials -- Temporary security credentials for EC2
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->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 # create a copy of the token from its JSON representation (e.g. as returned
36 # from instance metadata of an instance that is assigned an IAM role
37 my $token = VM::EC2::Security::Credentials->new_from_json($json);
38  
39 # open a new EC2 connection with this token. User will be
40 # able to run all the methods specified in the policy.
41 my $ec2 = VM::EC2->new(-security_token => $token);
42 print $ec2->describe_images(-owner=>'self');
43  
44 # convenience routine; will return a VM::EC2 object authorized
45 # to use the current token
46 my $ec2 = $token->new_ec2;
47 print $ec2->describe_images(-owner=>'self');
48
49  
50 =head1 DESCRIPTION
51  
52 The VM::EC2::Security::Credentials object is returned by the
53 VM::EC2::Security::Token->credentials() method, which in turn is
54 generated by calls to VM::EC2->get_federation_token() and
55 VM::EC2->get_session_token(). The Credentials object contains
56 time-limited EC2 authentication information, including access key ID,
57 secret access key, and a temporary authentication session token.
58  
59 A Credentials object can be passed to VM::EC2->new() via the
60 -security_token parameter, in which case the -access_key and
61 -secret_key parameters can be omitted.
62  
63 As Credentials typically need to be transmitted from a process being
64 run by an AWS account holder to a process being run by another user,
65 the object provides serialization methods that allow the object to be
66 transmitted as a simple string.
67  
68 =head1 DATA ACCESS METHODS
69  
70 accessKeyId() -- The temporary access key ID
71 secretAccessKey() -- The secret access key
72 sessionToken() -- The temporary security token, as a long
73 opaque string
74 expiration() -- The expiration time of these credentials, as a
75 DateTime string.
76  
77 As in all VM::EC2 classes, mixedCase() and
78 broken_out_with_underscores() names may be used interchangeably.
79  
80 =head1 SERIALIZATION METHODS
81  
82 These two methods allow you to serialize the credentials into a string
83 suitable for sending via SSL, S/MIME or another secure channel, and
84 then reconstructing the object at the other end. For sending the
85 credentials to a non-perl process, you can simply retrieve each
86 individual field (access key, etc) and send them individually.
87  
88 =head2 $serialized = $credentials->serialize()
89  
90 Return a serialized form of the object as a base64-encoded
91 string. Note that the serialized form contains the secret access key
92 and session token in unencrypted, but very slightly obfuscated, form.
93  
94 =head2 $credentials = VM::EC2::Security::Credentials->new_from_serialized($serialized)
95  
96 Given a previously-serialized Credentials object, unserialize it and
97 return a copy.
98  
99 =head1 CONVENIENCE METHODS
100  
101 These are convenience methods.
102  
103 =head2 $ec2 = $credentials->new_ec2(@args)
104  
105 Create a new VM::EC2 object which is authorized using the security
106 token contained in the credentials object. You may pass all the
107 arguments, such as -endpoint, that are accepted by VM::EC2->new(), but
108 -access_key and -secret_access_key will be ignored.
109  
110 =head1 STRING OVERLOADING
111  
112 When used in a string context, this object will interpolate the
113  
114 =head1 SEE ALSO
115  
116 L<VM::EC2>
117 L<VM::EC2::Generic>
118  
119 =head1 AUTHOR
120  
121 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
122  
123 Copyright (c) 2011 Ontario Institute for Cancer Research
124  
125 This package and its accompanying libraries is free software; you can
126 redistribute it and/or modify it under the terms of the GPL (either
127 version 1, or at your option, any later version) or the Artistic
128 License 2.0. Refer to LICENSE for the full license text. In addition,
129 please see DISCLAIMER.txt for disclaimers of warranty.
130  
131 =cut
132  
133 use strict;
134 use base 'VM::EC2::Generic';
135 use Storable 'nfreeze','thaw';
136 use MIME::Base64 'encode_base64','decode_base64';
137  
138 sub valid_fields {
139 my $self = shift;
140 return qw(AccessKeyId Expiration SecretAccessKey SessionToken);
141 }
142  
143 # because of AWS inconsistencies and limitations on the autoloaded can() method.
144 sub secret_access_key { shift->{data}{SecretAccessKey} }
145 sub access_key_id { shift->{data}{AccessKeyId} }
146 sub session_token { shift->{data}{SessionToken} }
147  
148 sub new_ec2 {
149 my $self = shift;
150 my @args = @_;
151 return VM::EC2->new(-security_token=>$self,
152 @args);
153 }
154  
155 # serialize the credentials in a packed form
156 sub serialize {
157 my $self = shift;
158 my $data = nfreeze($self);
159 return encode_base64($data);
160 }
161  
162 sub new_from_serialized {
163 my $class = shift;
164 my $data = shift;
165 my $obj = thaw(decode_base64($data));
166 return bless $obj,ref $class || $class;
167 }
168  
169 sub new_from_json {
170 my $class = shift;
171 my ($data,$endpoint) = @_;
172 eval "require JSON; 1" or die "no JSON module installed: $@"
173 unless JSON->can('decode');
174 my $hash = JSON::from_json($data);
175  
176 my $payload = {AccessKeyId => $hash->{AccessKeyId},
177 SecretAccessKey => $hash->{SecretAccessKey},
178 SessionToken => $hash->{Token}, # note inconsistency here, which is why we are copying
179 Expiration => $hash->{Expiration}
180 };
181  
182 my $self = $class->new($payload,undef);
183 my $ec2 = $self->new_ec2(-endpoint => $endpoint);
184 $self->ec2($ec2) unless $self->ec2;
185 return $self;
186 }
187  
188 sub short_name {shift->access_key_id}
189  
190 1;