Pipeline/Stages/Taps.pm

filter: Statementstmt 100% 107 / 107 Branchbran 100% 40 / 40 Conditioncond 93.3% 14 / 15 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 27 / 27 Podpod 100% 4 / 4 totaltotal 98.5% 200 / 203 CC 27 SCAR 12.5
CC
27
Cov
99%
_find_redactor
8.0
_redacts
6.0
_redactor
5.0
CRAP 3.5
« Pipeline/JSON.pm ↑ Summary Pipeline/Feed.pm »
1 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Stages::Taps;
4  
5 6 ▶use Data::Dumper qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Ref::Util qw(is_coderef is_plain_scalarref);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util qw(openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use Ekorn::Pipeline qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Ekorn::Pipeline::Gather qw(gather);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Ekorn::Pipeline::Stage qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 our @EXPORT = qw(peek count_into); ## no critic (Modules::ProhibitAutomaticExportation)
18  
19 6 our @EXPORT_OK = (@EXPORT, qw(dump_to tee));
20 6 our %EXPORT_TAGS = (all => \@EXPORT_OK);
21  
22 # Croak at the line that called Ekorn::Pipeline.
23 6 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
24  
25 # True, dump_to writes secrets as they are, without redacting them. A
26 # package variable, so that it can be localised, as Data::Dumper's are.
27 6 our $DumpSecrets; ## no critic (Variables::ProhibitPackageVars)
28  
29 # What dump_to redacts with, looked for at its first dump: undef until
30 # then; then Text::Redact::Secrets' redact, '' when the module isn't
31 # installed, or why it's there but can't be used.
32 6 my $redactor;
33  
34 # Return a stage calling the block given with each value, for its side
35 # effects, and passing the value on.
36 5 ▶sub peek : prototype(&) ($block) {
Subroutine peek: called CC=2 SCAR=6.9
Pod documented
37 5 ▶ croakf 'Expected a code reference' unless is_coderef($block);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($block) 1 4 5
38 6 ▶ return Ekorn::Pipeline::Stage->new(sub ($value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 6 $block->($value);
40 6 return $value;
41 4 });
42 }
43  
44 # Return a stage counting the values into the scalar given, from 0 on
45 # each run, and passing them on.
46 6 ▶sub count_into ($count) {
Subroutine count_into: called CC=2 SCAR=6.9
Pod documented
47 6 ▶ croakf 'Expected a reference to a scalar'
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_scalarref($count) 4 2 6
48 unless is_plain_scalarref($count);
49 3 ▶ return gather {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 3 $$count = 0;
51  
52 # Without a signature, as it's called for each value.
53 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 3 $$count++;
55 3 return $_[0];
56 3 };
57 2 };
58 }
59  
60 # Return a stage running each value through a branch of the stages
61 # given, one run of its own beside the main run, fed by the stage, and
62 # passing the value on unchanged. What comes out of the branch is
63 # dropped; a stop in it ends only it; its flushes are called in the
64 # stage's flush, which closes it, and it's finished in the stage's
65 # end, if the main run ended first. A branch with nothing to flush or
66 # end needs neither, so the stage has none, and the main run can take
67 # the plain loop.
68 31 ▶sub tee (@stages) {
Subroutine tee: called CC=1 SCAR=0.0
Pod documented
69 31 my $branch = Ekorn::Pipeline->new(@stages);
70 32 ▶ return gather {
Subroutine __ANON__: called CC=3 SCAR=11.0
71 32 my $feed = $branch->feed;
72 # The feed's own code for push and ended, called directly, as
73 # they're called for each value: the branch can't reach its feed,
74 # so the checks push makes of it, that it's open and not pushed
75 # from inside, are made here, by $open, or can't fail.
76 32 my($push, $ended) = @$feed{qw(push ended)};
77 32 my $open = 1;
78 # The step, without a signature, as it's called for each value:
79 # into the branch while it's open, and on.
80 77 ▶ my $step = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
81 77 ▶ if ($open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open) 65 12 77
82 65 $push->($_[0]);
83 61 $open = !defined $ended->();
84 }
85 73 return $_[0];
86 32 };
87 32 ▶ return $step if $feed->{plain};
Branch: 1 branch
BranchTrueFalseTotal
if $feed->{"plain"} 13 19 32
88 # The flush: the branch's input has run out.
89 15 ▶ my $flush = sub () {
Subroutine __ANON__: called CC=2 SCAR=6.9
90 15 ▶ $feed->close if $open;
Branch: 1 branch
BranchTrueFalseTotal
if $open 11 4 15
91 14 $open = 0;
92 14 return;
93 19 };
94 19 ▶ return ($step, $flush) unless $feed->{ends};
Branch: 1 branch
BranchTrueFalseTotal
unless $feed->{"ends"} 9 10 19
95 # The end: the branch, if still open, is told it stopped.
96 10 ▶ return ($step, $flush, sub ($how) { $feed->finish });
Subroutine __ANON__: called CC=1 SCAR=0.0
97 30 };
98 }
99  
100  
101 # Return a stage writing each value, with Data::Dumper, to the filehandle
102 # given, or STDERR, and passing it on, with its secrets redacted when
103 # Text::Redact::Secrets is installed, unless $DumpSecrets is true.
104 37 ▶sub dump_to ($fh = \*STDERR) {
Subroutine dump_to: called CC=3 SCAR=11.0
Pod documented
105 37 ▶ croakf 'Expected an open filehandle' unless defined openhandle($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless defined &openhandle($fh) 3 34 37
106 # Without a signature, as it's called for each value.
107 34 ▶ return Ekorn::Pipeline::Stage->new(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
108 34 my $dump = _dump($_[0]);
109 34 ▶ unless ($DumpSecrets) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Ekorn::Pipeline::Stages::Taps::DumpSecrets) 32 2 34
110 32 my $redact = _redactor();
111 10 ▶ $dump = $redact->($dump) if $redact;
Branch: 1 branch
BranchTrueFalseTotal
if $redact 8 2 10
112 }
113 12 print { $fh } $dump;
114 12 return $_[0];
115 34 });
116 }
117  
118  
119 43 ▶sub _dump ($value) {
Subroutine _dump: called CC=1 SCAR=0.0
120 43 local $Data::Dumper::Indent = 1;
121 43 local $Data::Dumper::Sortkeys = 1;
122 43 local $Data::Dumper::Terse = 1;
123 43 return Data::Dumper::Dumper($value);
124 }
125  
126 # Return Text::Redact::Secrets' redact, or nothing when it isn't
127 # installed; croak when it's there but can't be used. It's looked for
128 # once, and the answer kept.
129 32 ▶sub _redactor () {
Subroutine _redactor: called CC=5 SCAR=16.1
130 32 ▶ $redactor //= _find_redactor();
Condition66%2/3
$redactor //= _find_redactor()
l!l&&r!l&&!r
17150
MC/DC0%0/2
$redactor //= _find_redactor()
$redactor _find_redactor()
Truth table66%2/3
A //= B
A= $redactorB= _find_redactor()
ABresult
1X1
011
000
131 32 ▶ croakf 'Text::Redact::Secrets %s, so dump_to won\'t write a dump that'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $redactor or $redactor eq "" 22 10 32
Condition100%3/3
ref $redactor or $redactor eq ""
l!l&&r!l&&!r
8222
MC/DC100%2/2
ref $redactor or $redactor eq ""
ref $redactor $redactor eq ""
Truth table100%3/3
A or B
A= ref $redactorB= $redactor eq ""
ABresult
1X1
011
000
132 . ' may hold secrets; local $Ekorn::Pipeline::Stages::Taps::DumpSecrets'
133 . ' = 1 dumps them without redacting', $redactor
134 unless ref $redactor || $redactor eq '';
135 10 ▶ return ref $redactor ? $redactor : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $redactor ? : 8 2 10
136 }
137  
138 # Load Text::Redact::Secrets, check its version and that it redacts, and
139 # return its redact, '' when it isn't installed, or why it can't be used.
140 15 ▶sub _find_redactor () {
Subroutine _find_redactor: called CC=8 SCAR=20.8
141 15 my $loaded = eval { require Text::Redact::Secrets; 1 };
142 15 ▶ unless ($loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 2 13 15
143 2 my $error = $@;
144 2 ▶ return ''
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ m[ \A Can't\ locate\ Text/Redact/Secrets\.pm\ in\ \@INC ]ux 1 1 2
145 if $error =~ m( \A Can't\ locate\ Text/Redact/Secrets\.pm\ in\ \@INC )x;
146 1 return 'failed to load: ' . ($error =~ s( \s* \n .* )()xsr);
147 }
148 13 my $version = eval { version->parse(Text::Redact::Secrets->VERSION) };
149 13 ▶ return 'has no version we can read' unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 1 12 13
150 12 ▶ return "is $version, not v1.x" if $version < v1.0.0 || $version >= v2.0.0;
Branch: 1 branch
BranchTrueFalseTotal
if $version < v1.0.0 or $version >= v2.0.0 2 10 12
Condition100%3/3
$version < v1.0.0 or $version >= v2.0.0
l!l&&r!l&&!r
1110
MC/DC100%2/2
$version < v1.0.0 or $version >= v2.0.0
$version < v1.0.0 $version >= v2.0.0
Truth table100%3/3
A or B
A= $version < v1.0.0B= $version >= v2.0.0
ABresult
1X1
011
000
151 10 my $redact = Text::Redact::Secrets->can('redact');
152 10 ▶ return 'has no redact' unless $redact;
Branch: 1 branch
BranchTrueFalseTotal
unless $redact 1 9 10
153 9 ▶ return 'failed its self-test' unless _redacts($redact);
Branch: 1 branch
BranchTrueFalseTotal
unless _redacts($redact) 6 3 9
154 3 return $redact;
155 }
156  
157 # Whether redact takes each of a few made-up secrets out of a dump. Each
158 # is built from pieces, so that no line holds one whole for a scanner.
159 9 ▶sub _redacts ($redact) {
Subroutine _redacts: called CC=6 SCAR=17.9
160 9 my $password = join '', 'Zq8vR2mX', 'kP4wL9nT';
161 9 my $key_id = join '', 'AKIA', 'QYLPMN5H', 'HHFPZAM2';
162 9 my $dump = _dump({
163 password => $password,
164 dsn => "postgres://app:$password\@db/app",
165 key_id => $key_id,
166 });
167 9 my $redacted = eval { $redact->($dump) };
168 9 ▶ return 0 if !defined $redacted || ref $redacted;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $redacted or ref $redacted 2 7 9
Condition100%3/3
not defined $redacted or ref $redacted
l!l&&r!l&&!r
117
MC/DC100%2/2
not defined $redacted or ref $redacted
not defined $redacted ref $redacted
Truth table100%3/3
not(A) or B
A= not defined $redactedB= ref $redacted
ABresult
1X1
011
000
169 7 ▶ return 0 if index($redacted, $password) >= 0 || index($redacted, $key_id) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($redacted, $password) != -1 or index($redacted, $key_id) != -1 3 4 7
Condition100%3/3
index($redacted, $password) != -1 or index($redacted, $key_id) != -1
l!l&&r!l&&!r
214
MC/DC100%2/2
index($redacted, $password) != -1 or index($redacted, $key_id) != -1
index($redacted, $password) != -1 index($redacted, $key_id) != -1
Truth table100%3/3
A or B
A= index($redacted, $password) != -1B= index($redacted, $key_id) != -1
ABresult
1X1
011
000
170 4 ▶ return 0 if index($redacted, '*REDACTED*') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($redacted, "*REDACTED*") == -1 1 3 4
171 3 return 1;
172 }
173  
174 # Forget what was found, so the next dump looks again. For the tests.
175 30 ▶sub _forget_redactor () { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
Subroutine _forget_redactor: called CC=1 SCAR=0.0
176 30 undef $redactor;
177 30 return;
178 }
179  
180 6 1;
181 __END__
« Pipeline/JSON.pm ↑ Summary Pipeline/Feed.pm »