| 1 | package Net::Amazon::S3::Client::GPG; | ||
| 2 | use Moose; | ||
| 3 | use MooseX::StrictConstructor; | ||
| 4 | use Moose::Util::TypeConstraints; | ||
| 5 | use Carp qw(confess); | ||
| 6 | use File::Temp qw(tempfile); | ||
| 7 | use Net::Amazon::S3; | ||
| 8 | use Net::Amazon::S3::Client; | ||
| 9 | use Net::Amazon::S3::Client::Object; | ||
| 10 | |||
| 11 | our $VERSION = '0.33'; | ||
| 12 | |||
| 13 | extends 'Net::Amazon::S3::Client'; | ||
| 14 | |||
| 15 | has 'passphrase' => ( is => 'ro', isa => 'Str', required => 0 ); | ||
| 16 | has 'gnupg_interface' => | ||
| 17 | ( is => 'ro', isa => 'GnuPG::Interface', required => 1 ); | ||
| 18 | |||
| 19 | __PACKAGE__->meta->make_immutable; | ||
| 20 | |||
| 21 | Net::Amazon::S3::Client::Object->meta->make_mutable(); | ||
| 22 | Net::Amazon::S3::Client::Object->meta->add_method( | ||
| 23 | 'gpg_get' => sub { | ||
| 24 | my $self = shift; | ||
| 25 | my ( $ciphertext_fh, $ciphertext_filename ) = tempfile(); | ||
| 26 | $ciphertext_fh->close || confess "Error closing filehandle: $!"; | ||
| 27 | $self->get_filename($ciphertext_filename); | ||
| 28 | |||
| 29 | my ( $tmp_plaintext_fh, $plaintext_filename ) = tempfile(); | ||
| 30 | $tmp_plaintext_fh->close; | ||
| 31 | |||
| 32 | $self->client->decrypt( $ciphertext_filename, $plaintext_filename ); | ||
| 33 | |||
| 34 | unlink($ciphertext_filename) | ||
| 35 | || confess "Error unlinking $ciphertext_filename: $!"; | ||
| 36 | |||
| 37 | my $plaintext_fh = IO::File->new($plaintext_filename) | ||
| 38 | || confess "Error opening $plaintext_filename: $!"; | ||
| 39 | my $plaintext; | ||
| 40 | until ( $plaintext_fh->eof ) { | ||
| 41 | $plaintext_fh->read( my $chunk, 4096 ) | ||
| 42 | || confess "Error reading from plaintext: $!"; | ||
| 43 | $plaintext .= $chunk; | ||
| 44 | } | ||
| 45 | $plaintext_fh->close; | ||
| 46 | |||
| 47 | unlink($plaintext_filename) | ||
| 48 | || confess "Error unlinking $plaintext_filename: $!"; | ||
| 49 | return $plaintext; | ||
| 50 | } | ||
| 51 | ); | ||
| 52 | Net::Amazon::S3::Client::Object->meta->add_method( | ||
| 53 | 'gpg_get_filename' => sub { | ||
| 54 | my ( $self, $plaintext_filename ) = @_; | ||
| 55 | my ( $ciphertext_fh, $ciphertext_filename ) = tempfile(); | ||
| 56 | $ciphertext_fh->close; | ||
| 57 | $self->get_filename($ciphertext_filename); | ||
| 58 | $self->client->decrypt( $ciphertext_filename, $plaintext_filename ); | ||
| 59 | unlink($ciphertext_filename) | ||
| 60 | || confess "Error unlinking $ciphertext_filename: $!"; | ||
| 61 | } | ||
| 62 | ); | ||
| 63 | Net::Amazon::S3::Client::Object->meta->add_method( | ||
| 64 | 'gpg_put' => sub { | ||
| 65 | my ( $self, $plaintext ) = @_; | ||
| 66 | |||
| 67 | my ( $plaintext_fh, $plaintext_filename ) = tempfile(); | ||
| 68 | $plaintext_fh->print($plaintext) | ||
| 69 | || confess "Error printing the value: $!"; | ||
| 70 | $plaintext_fh->close || confess "Error closing filehandle: $!"; | ||
| 71 | |||
| 72 | my $ciphertext_filename = $self->client->encrypt($plaintext_filename); | ||
| 73 | $self->put_filename($ciphertext_filename); | ||
| 74 | |||
| 75 | unlink($plaintext_filename) | ||
| 76 | || confess "Error unlinking $plaintext_filename: $!"; | ||
| 77 | unlink($ciphertext_filename) | ||
| 78 | || confess "Error unlinking $ciphertext_filename: $!"; | ||
| 79 | } | ||
| 80 | ); | ||
| 81 | Net::Amazon::S3::Client::Object->meta->add_method( | ||
| 82 | 'gpg_put_filename' => sub { | ||
| 83 | my ( $self, $plaintext_filename ) = @_; | ||
| 84 | my $ciphertext_filename = $self->client->encrypt($plaintext_filename); | ||
| 85 | $self->put_filename($ciphertext_filename); | ||
| 86 | unlink($ciphertext_filename) | ||
| 87 | || confess "Error unlinking $ciphertext_filename: $!"; | ||
| 88 | } | ||
| 89 | ); | ||
| 90 | Net::Amazon::S3::Client::Object->meta->make_immutable(); | ||
| 91 | |||
| 92 | sub decrypt { | ||
| 93 | my ( $self, $ciphertext_filename, $plaintext_filename ) = @_; | ||
| 94 | my $gnupg = $self->gnupg_interface; | ||
| 95 | |||
| 96 | my $ciphertext_fh = IO::File->new($ciphertext_filename) | ||
| 97 | || confess "Error opening $ciphertext_filename: $!"; | ||
| 98 | |||
| 99 | my $plaintext_fh = IO::File->new( $plaintext_filename, 'w' ) | ||
| 100 | || confess "Error opening $plaintext_filename: $!"; | ||
| 101 | |||
| 102 | # This time we'll catch the standard error for our perusing | ||
| 103 | # as well as passing in the passphrase manually | ||
| 104 | # as well as the status information given by GnuPG | ||
| 105 | my ( $input, $output, $error, $passphrase_fh, $status_fh ) = ( | ||
| 106 | IO::Handle->new(), IO::Handle->new(), | ||
| 107 | IO::Handle->new(), IO::Handle->new(), | ||
| 108 | IO::Handle->new(), | ||
| 109 | ); | ||
| 110 | |||
| 111 | my $handles = GnuPG::Handles->new( | ||
| 112 | stdin => $input, | ||
| 113 | stdout => $output, | ||
| 114 | stderr => $error, | ||
| 115 | passphrase => $passphrase_fh, | ||
| 116 | status => $status_fh, | ||
| 117 | ); | ||
| 118 | |||
| 119 | # this sets up the communication | ||
| 120 | my $pid = $gnupg->decrypt( handles => $handles ) | ||
| 121 | || confess "Error decrypting: no pid!"; | ||
| 122 | |||
| 123 | # This passes in the passphrase | ||
| 124 | $passphrase_fh->print( $self->passphrase ) | ||
| 125 | || confess "Error printing passphrase: $!"; | ||
| 126 | $passphrase_fh->close || confess "Error closing passphrase: $!"; | ||
| 127 | |||
| 128 | until ( $ciphertext_fh->eof ) { | ||
| 129 | $ciphertext_fh->read( my $chunk, 4096 ) | ||
| 130 | || confess "Error reading from ciphertext: $!"; | ||
| 131 | $input->print($chunk) || confess "Error printing the ciphertext: $!"; | ||
| 132 | } | ||
| 133 | |||
| 134 | $input->close || confess "Error closing input: $!"; | ||
| 135 | $ciphertext_fh->close || confess "Error closing ciphertext: $!"; | ||
| 136 | |||
| 137 | until ( $output->eof ) { | ||
| 138 | $output->read( my $chunk, 4096 ) | ||
| 139 | || confess "Error reading from output: $!"; | ||
| 140 | $plaintext_fh->print($chunk) | ||
| 141 | || confess "Error writing the plaintext: $!"; | ||
| 142 | } | ||
| 143 | $output->close || confess "Error closing output: $!"; | ||
| 144 | $plaintext_fh->close || confess "Error closing plaintext: $!"; | ||
| 145 | |||
| 146 | my $error_output = join '', <$error>; # reading the error | ||
| 147 | my $status_info = join '', <$status_fh>; # read the status info | ||
| 148 | |||
| 149 | # clean up... | ||
| 150 | $error->close || confess "Error closing error: $!"; | ||
| 151 | $status_fh->close || confess "Error closing status: $!"; | ||
| 152 | |||
| 153 | #warn $error_output; | ||
| 154 | #warn $status_info; | ||
| 155 | |||
| 156 | waitpid $pid, 0; # clean up the finished GnuPG process | ||
| 157 | } | ||
| 158 | |||
| 159 | sub encrypt { | ||
| 160 | my ( $self, $plaintext_filename ) = @_; | ||
| 161 | my $gnupg = $self->gnupg_interface; | ||
| 162 | |||
| 163 | my $plaintext_fh = IO::File->new($plaintext_filename) | ||
| 164 | || confess "Error opening $plaintext_filename: $!"; | ||
| 165 | my ( $ciphertext_fh, $ciphertext_filename ) = tempfile(); | ||
| 166 | |||
| 167 | my $input = IO::Handle->new(); | ||
| 168 | my $output = IO::Handle->new(); | ||
| 169 | my $handles = GnuPG::Handles->new( | ||
| 170 | stdin => $input, | ||
| 171 | stdout => $output, | ||
| 172 | ); | ||
| 173 | my $pid = $gnupg->encrypt( handles => $handles ) | ||
| 174 | || confess "Error encrypting: no pid!"; | ||
| 175 | |||
| 176 | until ( $plaintext_fh->eof ) { | ||
| 177 | $plaintext_fh->read( my $chunk, 4096 ) | ||
| 178 | || confess "Error reading from plaintext: $!"; | ||
| 179 | $input->print($chunk) || confess "Error printing the plaintext: $!"; | ||
| 180 | } | ||
| 181 | |||
| 182 | $input->close || confess "Error closing filehandle: $!"; | ||
| 183 | |||
| 184 | until ( $output->eof ) { | ||
| 185 | $output->read( my $chunk, 4096 ) | ||
| 186 | || confess "Error reading from output: $!"; | ||
| 187 | $ciphertext_fh->print($chunk) | ||
| 188 | || confess "Error writing the ciphertext: $!"; | ||
| 189 | } | ||
| 190 | $output->close || confess "Error closing filehandle: $!"; | ||
| 191 | |||
| 192 | waitpid $pid, 0; | ||
| 193 | return $ciphertext_filename; | ||
| 194 | } | ||
| 195 | |||
| 196 | 1; | ||
| 197 | |||
| 198 | __END__ |