blib/lib/VM/EC2/REST/keys.pm untested

filter: Statementstmt 0.0% 0 / 31 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 64 CC 2 SCAR 11.5
CC
2
Cov
0%
import_key_pair
6.0
describe_key_pairs
2.0
create_key_pair
2.0
CRAP 3.1
1 package VM::EC2::REST::keys;
2  
3 use strict;
4 use VM::EC2 ''; # important not to import anything!
5 package VM::EC2; # add methods to VM::EC2
6  
7 VM::EC2::Dispatch->register(
8 DescribeKeyPairs => 'fetch_items,keySet,VM::EC2::KeyPair',
9 CreateKeyPair => 'VM::EC2::KeyPair',
10 ImportKeyPair => 'VM::EC2::KeyPair',
11 DeleteKeyPair => 'boolean',
12 );
13  
14 =head1 NAME VM::EC2::REST::keys
15  
16 =head1 SYNOPSIS
17  
18 use VM::EC2 ':standard';
19  
20 =head1 METHODS
21  
22 Implemented:
23 DescribeKeyPairs
24 CreateKeyPair
25 ImportKeyPair
26 DeleteKeyPair
27  
28 Unimplemented:
29 (none)
30  
31 These methods let you manipulate ssh key pairs.
32  
33 =head2 @keys = $ec2->describe_key_pairs(@names);
34  
35 =head2 @keys = $ec2->describe_key_pairs(\%filters);
36  
37 =head2 @keys = $ec2->describe_key_pairs(-key_name => \@names,
38 -filter => \%filters);
39  
40 Searches for ssh key pairs matching the provided filters and return
41 a series of VM::EC2::KeyPair objects.
42  
43 Optional arguments:
44  
45 -key_name A single key name or an arrayref containing a list
46 of names
47  
48 -filter Filter on tags and other attributes.
49  
50 The full list of key filters can be found at:
51 http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeKeyPairs.html
52  
53 =cut
54  
55 sub describe_key_pairs {
56 my $self = shift;
57 my %args = $self->args(-key_name=>@_);
58 my @params = $self->list_parm('KeyName',\%args);
59 push @params,$self->filter_parm(\%args);
60 return $self->call('DescribeKeyPairs',@params);
61 }
62  
63 =head2 $key = $ec2->create_key_pair($name)
64  
65 Create a new key pair with the specified name (required). If the key
66 pair already exists, returns undef. The contents of the new keypair,
67 including the PEM-encoded private key, is contained in the returned
68 VM::EC2::KeyPair object:
69  
70 my $key = $ec2->create_key_pair('My Keypair');
71 if ($key) {
72 print $key->fingerprint,"\n";
73 print $key->privateKey,"\n";
74 }
75  
76 =cut
77  
78 sub create_key_pair {
79 my $self = shift;
80 my $name = shift or croak "Usage: create_key_pair(\$name)";
81 $name =~ /^[\w _-]+$/
82 or croak "Invalid keypair name: must contain only alphanumerics, spaces, dashes and underscores";
83 my @params = (KeyName=>$name);
84 $self->call('CreateKeyPair',@params);
85 }
86  
87 =head2 $key = $ec2->import_key_pair($name,$public_key)
88  
89 =head2 $key = $ec2->import_key_pair(-key_name => $name,
90 -public_key_material => $public_key)
91  
92 Imports a preexisting public key into AWS under the specified name.
93 If successful, returns a VM::EC2::KeyPair. The public key must be an
94 RSA key of length 1024, 2048 or 4096. The method can be called with
95 two unnamed arguments consisting of the key name and the public key
96 material, or in a named argument form with the following argument
97 names:
98  
99 -key_name -- desired name for the imported key pair (required)
100 -name -- shorter version of -key_name
101  
102 -public_key_material -- public key data (required)
103 -public_key -- shorter version of the above
104  
105 This example uses Net::SSH::Perl::Key to generate a new keypair, and
106 then uploads the public key to Amazon.
107  
108 use Net::SSH::Perl::Key;
109  
110 my $newkey = Net::SSH::Perl::Key->keygen('RSA',1024);
111 $newkey->write_private('.ssh/MyKeypair.rsa'); # save private parts
112  
113 my $key = $ec2->import_key_pair('My Keypair' => $newkey->dump_public)
114 or die $ec2->error;
115 print "My Keypair added with fingerprint ",$key->fingerprint,"\n";
116  
117 Several different formats are accepted for the key, including SSH
118 "authorized_keys" format (generated by L<ssh-keygen> and
119 Net::SSH::Perl::Key), the SSH public keys format, and DER format. You
120 do not need to base64-encode the key or perform any other
121 pre-processing.
122  
123 Note that the algorithm used by Amazon to calculate its key
124 fingerprints differs from the one used by the ssh library, so don't
125 try to compare the key fingerprints returned by Amazon to the ones
126 produced by ssh-keygen or Net::SSH::Perl::Key.
127  
128 =cut
129  
130 sub import_key_pair {
131 my $self = shift;
132 my %args;
133 if (@_ == 2 && $_[0] !~ /^-/) {
134 %args = (-key_name => shift,
135 -public_key_material => shift);
136 } else {
137 %args = @_;
138 }
139 my $name = $args{-key_name} || $args{-name} or croak "-key_name argument required";
140 my $pkm = $args{-public_key_material}|| $args{-public_key} or croak "-public_key_material argument required";
141 my @params = (KeyName => $name,PublicKeyMaterial=>encode_base64($pkm));
142 $self->call('ImportKeyPair',@params);
143 }
144  
145 =head2 $result = $ec2->delete_key_pair($name)
146  
147 Deletes the key pair with the specified name (required). Returns true
148 if successful.
149  
150 =cut
151  
152 sub delete_key_pair {
153 my $self = shift; my $name = shift or croak "Usage: delete_key_pair(\$name)";
154 $name =~ /^[\w _-]+$/
155 or croak "Invalid keypair name: must contain only alphanumerics, spaces, dashes and underscores";
156 my @params = (KeyName=>$name);
157 $self->call('DeleteKeyPair',@params);
158 }
159  
160  
161 =head1 SEE ALSO
162  
163 L<VM::EC2>
164  
165 =head1 AUTHOR
166  
167 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
168  
169 Copyright (c) 2011 Ontario Institute for Cancer Research
170  
171 This package and its accompanying libraries is free software; you can
172 redistribute it and/or modify it under the terms of the GPL (either
173 version 1, or at your option, any later version) or the Artistic
174 License 2.0. Refer to LICENSE for the full license text. In addition,
175 please see DISCLAIMER.txt for disclaimers of warranty.
176  
177 =cut
178  
179  
180 1;