| 1 | package VM::EC2::REST::keys; | ||||||||||||||||||||||||||||
| 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 | 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 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
describe_key_pairs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | my %args = $self->args(-key_name=>@_); | |||||||||||||||||||||||||||
| 58 | 0 | my @params = $self->list_parm('KeyName',\%args); | |||||||||||||||||||||||||||
| 59 | 0 | push @params,$self->filter_parm(\%args); | |||||||||||||||||||||||||||
| 60 | 0 | 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 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
create_key_pair: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $name = shift or croak "Usage: create_key_pair(\$name)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $name =~ /^[\w _-]+$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | or croak "Invalid keypair name: must contain only alphanumerics, spaces, dashes and underscores"; | ||||||||||||||||||||||||||||
| 83 | 0 | my @params = (KeyName=>$name); | |||||||||||||||||||||||||||
| 84 | 0 | $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 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
import_key_pair: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | 0 | my %args; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (@_ == 2 && $_[0] !~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 2 and not $_[0] =~ /^-/
MC/DC0%0/2
@_ == 2 and not $_[0] =~ /^-/
@_ == 2
not $_[0] =~ /^-/
Truth table0%0/3
A and not(B)
A= @_ == 2B= not $_[0] =~ /^-/
| |||||||||||||||||||||||||||||
| 134 | 0 | %args = (-key_name => shift, | |||||||||||||||||||||||||||
| 135 | -public_key_material => shift); | ||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 0 | %args = @_; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my $name = $args{-key_name} || $args{-name} or croak "-key_name argument required"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{'-key_name'} || $args{'-name'}
MC/DC0%0/2
$args{'-key_name'} || $args{'-name'}
$args{'-key_name'}
$args{'-name'}
Truth table0%0/3
A || B
A= $args{'-key_name'}B= $args{'-name'}
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | my $pkm = $args{-public_key_material}|| $args{-public_key} or croak "-public_key_material argument required"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{'-public_key_material'} || $args{'-public_key'}
MC/DC0%0/2
$args{'-public_key_material'} || $args{'-public_key'}
$args{'-public_key_material'}
$args{'-public_key'}
Truth table0%0/3
A || B
A= $args{'-public_key_material'}B= $args{'-public_key'}
| |||||||||||||||||||||||||||||
| 141 | 0 | my @params = (KeyName => $name,PublicKeyMaterial=>encode_base64($pkm)); | |||||||||||||||||||||||||||
| 142 | 0 | $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 | 0 | ▶ | my $self = shift; my $name = shift or croak "Usage: delete_key_pair(\$name)"; | ||||||||||||||||||||||||||
|
Subroutine
delete_key_pair: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $name =~ /^[\w _-]+$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | or croak "Invalid keypair name: must contain only alphanumerics, spaces, dashes and underscores"; | ||||||||||||||||||||||||||||
| 156 | 0 | my @params = (KeyName=>$name); | |||||||||||||||||||||||||||
| 157 | 0 | $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 | 7 | 1; | |||||||||||||||||||||||||||