Pipeline/Stages/Taps.pm

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