File Coverage

blib/lib/PerlIO/via/GnuPG.pm
Criterion Covered Total %
statement 53 56 94.6
branch 11 14 78.5
condition 3 6 50.0
subroutine 11 12 91.6
pod 0 2 0.0
total 78 90 86.6


line stmt bran cond sub pod time code
1             #
2             # This file is part of PerlIO-via-GnuPG
3             #
4             # This software is Copyright (c) 2013 by Chris Weyl.
5             #
6             # This is free software, licensed under:
7             #
8             # The GNU Lesser General Public License, Version 2.1, February 1999
9             #
10             package PerlIO::via::GnuPG;
11             BEGIN {
12 1     1   32553 $PerlIO::via::GnuPG::AUTHORITY = 'cpan:RSRCHBOY';
13             }
14             # git description: 0.003-7-gb91cfad
15             $PerlIO::via::GnuPG::VERSION = '0.004';
16              
17             # ABSTRACT: Layer to try to decrypt on read
18              
19 1     1   10 use strict;
  1         2  
  1         29  
20 1     1   6 use warnings::register qw{ unencrypted };
  1         1  
  1         254  
21             #use warnings::register;
22 1     1   5 use warnings;
  1         2  
  1         31  
23              
24 1     1   859 use autodie 2.25;
  1         29116  
  1         6  
25              
26 1     1   6789 use IPC::Open3 'open3';
  1         4662  
  1         64  
27 1     1   8 use Symbol 'gensym';
  1         2  
  1         36  
28 1     1   971 use List::AllUtils 'part';
  1         2964  
  1         603  
29              
30             # gpg --decrypt -q --status-file aksdja --no-tty
31             # gpg --decrypt -q --status-file aksdja --no-tty .pause.gpg
32              
33             sub PUSHED {
34 3     3 0 11132 my ($class, $mode) = @_;
35              
36 3         185 return bless { }, $class;
37             }
38              
39 0     0   0 sub _passthrough_unencrypted { 0 }
40              
41             sub FILL {
42 9     9 0 59 my ($self, $fh) = @_;
43              
44 9 100       44 return shift @{ $self->{buffer} }
  6         67  
45             if exists $self->{buffer};
46              
47             ### pull in all of fh and try to decrypt it...
48 3         7 my $maybe_encrypted = do { local $/; <$fh> };
  3         11  
  3         238  
49              
50             ### $maybe_encrypted
51 3         21 my ($in, $out, $error) = (gensym, gensym, gensym);
52 3         203 my $run = 'gpg -qd --no-tty --command-fd 0';
53 3         19 my $pid = open3($in, $out, $error, $run);
54              
55             ### $pid
56 3         24683 print $in $maybe_encrypted;
57 3         59 close $in;
58 3         67234 my @output = <$out>;
59 3         241 my @errors = <$error>;
60              
61 3         176 waitpid $pid, 0;
62              
63             ### @output
64             ### @errors
65              
66             ### filter warnings out...
67 3         19 chomp @errors;
68 3 100   5   113 my ($errors, $warnings) = map { $_ || [] } part { /WARNING:/ ? 1 : 0 } @errors;
  6 100       52  
  5         242  
69              
70             ### $warnings
71 3 50 33     1800 warnings::warnif(@$warnings)
72             if !!$warnings && @$warnings;
73              
74 3 100 66     946 if (!!$errors && @$errors) {
75              
76 1         8 my $not_encrypted = scalar grep { /no valid OpenPGP data found/ } @$errors;
  2         16  
77              
78             ### $not_encrypted
79             ### passthrough: $self->_passthrough_unencrypted
80 1 50       15 if ($not_encrypted) {
81              
82 1 50       17 if ($self->_passthrough_unencrypted) {
83 1         66 warnings::warnif(
84             'PerlIO::via::GnuPG::unencrypted',
85             'File does not appear to be encrypted!',
86             );
87 1         9 @output = ($maybe_encrypted);
88             }
89             else {
90 0         0 die "File does not appear to be encrypted!";
91             }
92             }
93             else {
94              
95             # "@errors" here is intentional -- show the warnings, too
96 0         0 die "Errors while attempting decryption: @errors";
97             }
98             }
99              
100 3         27 $self->{buffer} = [ @output ];
101 3         48 return shift @{ $self->{buffer} };
  3         305  
102             }
103              
104             !!42;
105              
106             __END__
107              
108             =pod
109              
110             =encoding UTF-8
111              
112             =for :stopwords Chris Weyl decrypt
113              
114             =head1 NAME
115              
116             PerlIO::via::GnuPG - Layer to try to decrypt on read
117              
118             =head1 VERSION
119              
120             This document describes version 0.004 of PerlIO::via::GnuPG - released April 14, 2014 as part of PerlIO-via-GnuPG.
121              
122             =head1 SYNOPSIS
123              
124             use PerlIO::via::GnuPG;
125              
126             # dies on error, and if the file is not encrypted
127             open(my $fh, '<:via(GnuPG)', 'secret.txt.asc')
128             or die "cannot open! $!";
129              
130             my @in = <$fh>; # or whatever...
131              
132             =head1 DESCRIPTION
133              
134             This is a L<PerlIO> module to decrypt files transparently. It's pretty
135             simple and does not support writing, but works.
136              
137             ...and if it doesn't, please file an issue :)
138              
139             =for Pod::Coverage FILL PUSHED
140              
141             =head1 CUSTOM WARNING CATEGORIES
142              
143             This package emits warnings from time to time. To disable warnings generated
144             when passing through unencrypted data:
145              
146             no warnings 'PerlIO::via::GnuPG::unencrypted';
147              
148             Likewise, to disable all warnings issued by this package:
149              
150             no warnings 'PerlIO::via::GnuPG';
151              
152             =head1 SEE ALSO
153              
154             Please see those modules/websites for more information related to this module.
155              
156             =over 4
157              
158             =item *
159              
160             L<PerlIO::via::GnuPG::Maybe|PerlIO::via::GnuPG::Maybe>
161              
162             =item *
163              
164             L<PerlIO|PerlIO>
165              
166             =item *
167              
168             L<PerlIO::via|PerlIO::via>
169              
170             =back
171              
172             =head1 SOURCE
173              
174             The development version is on github at L<http://https://github.com/RsrchBoy/PerlIO-via-GnuPG>
175             and may be cloned from L<git://https://github.com/RsrchBoy/PerlIO-via-GnuPG.git>
176              
177             =head1 BUGS
178              
179             Please report any bugs or feature requests on the bugtracker website
180             https://github.com/RsrchBoy/PerlIO-via-GnuPG/issues
181              
182             When submitting a bug or request, please include a test-file or a
183             patch to an existing test-file that illustrates the bug or desired
184             feature.
185              
186             =head1 AUTHOR
187              
188             Chris Weyl <cweyl@alumni.drew.edu>
189              
190             =head1 COPYRIGHT AND LICENSE
191              
192             This software is Copyright (c) 2013 by Chris Weyl.
193              
194             This is free software, licensed under:
195              
196             The GNU Lesser General Public License, Version 2.1, February 1999
197              
198             =cut