GroupSecret.pm

filter: Statementstmt 18.8% 26 / 138 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 44.4% 8 / 18 Podpod 100% 5 / 5 totaltotal 15.4% 39 / 253 CC 38 SCAR 39.4
CC
38
Cov
12%
main
132.0
_action_print_secret
56.0
_action_set_secret
56.0
CRAP 51.2
↑ Summary GroupSecret/File.pm »
1 package App::GroupSecret;
2 # ABSTRACT: A simple tool for maintaining a shared group secret
3  
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.304'; # VERSION
9  
10 1 ▶use App::GroupSecret::Crypt qw(generate_secure_random_bytes read_openssh_key_fingerprint);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use App::GroupSecret::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Getopt::Long 2.38 qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
19 0 my $class = shift;
20 0 return bless {}, $class;
21 }
22  
23  
24 0 ▶sub main {
Subroutine main: not called CC=11 SCAR=48.8
Pod documented
25 0 my $self = shift;
26 0 my @args = @_;
27  
28 0 my $filepath = '';
29 0 my $help = 0;
30 0 my $man = 0;
31 0 my $version = 0;
32 0 my $private_key = '';
33  
34 # Parse options using pass_through so that we can pick out the global
35 # options, wherever they are in the arg list, and leave the rest to be
36 # parsed by each individual command.
37 0 Getopt::Long::Configure('pass_through');
38 0 ▶ GetOptionsFromArray(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@args, "file|f=s", \$filepath, "help|h|?", \$help, "manual|man", \$man, "private-key|k=s", \$private_key, "version|v", \$version) or pod2usage(2) 0 0 0
39 \@args,
40 'file|f=s' => \$filepath,
41 'help|h|?' => \$help,
42 'manual|man' => \$man,
43 'private-key|k=s' => \$private_key,
44 'version|v' => \$version,
45 ) or pod2usage(2);
46 0 Getopt::Long::Configure('default');
47  
48 0 ▶ pod2usage(-exitval => 1, -verbose => 99, -sections => [qw(SYNOPSIS OPTIONS COMMANDS)]) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 0 0
49 0 ▶ pod2usage(-verbose => 2) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 0 0
50 0 ▶ return print "groupsecret ${VERSION}\n" if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 0 0 0
51  
52 0 ▶ $self->{private_key} = $private_key if $private_key;
Branch: 1 branch
BranchTrueFalseTotal
if $private_key 0 0 0
53 0 ▶ $self->{filepath} = $filepath if $filepath;
Branch: 1 branch
BranchTrueFalseTotal
if $filepath 0 0 0
54  
55 0 my %commands = (
56 add_key => 'add_key',
57 add_keys => 'add_key',
58 change_secret => 'set_secret',
59 delete_key => 'delete_key',
60 delete_keys => 'delete_key',
61 list_keys => 'list_keys',
62 print => 'print_secret',
63 print_secret => 'print_secret',
64 remove_key => 'delete_key',
65 remove_keys => 'delete_key',
66 set_secret => 'set_secret',
67 show_secret => 'print_secret',
68 update_secret => 'set_secret',
69 );
70  
71 0 ▶ unshift @args, 'print' if !@args || $args[0] =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if not @args or $args[0] =~ /^-/ 0 0 0
Condition0%0/3
not @args or $args[0] =~ /^-/
l!l&&r!l&&!r
000
MC/DC0%0/2
not @args or $args[0] =~ /^-/
not @args $args[0] =~ /^-/
Truth table0%0/3
not(A) or B
A= not @argsB= $args[0] =~ /^-/
ABresult
1X1
011
000
72  
73 0 my $command = shift @args;
74 0 my $lookup = $command;
75 0 $lookup =~ s/-/_/g;
76 0 ▶ my $method = '_action_' . ($commands{$lookup} || '');
Condition0%0/2
$commands{$lookup} || ''
l!l
00
MC/DC0%0/1
$commands{$lookup} || ''
$commands{$lookup}
Truth table0%0/2
A
A= $commands{$lookup}
Aresult
11
00
77  
78 0 ▶ if (!$self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($method)) 0 0 0
79 0 warn "Unknown command: $command\n";
80 0 pod2usage(2);
81 }
82  
83 0 $self->$method(@args);
84 }
85  
86  
87 0 ▶sub filepath {
Subroutine filepath: not called CC=3 SCAR=24.8
Pod documented
88 0 ▶ shift->{filepath} ||= $ENV{GROUPSECRET_KEYFILE} || 'groupsecret.yml';
Condition0%0/5
+(shift())->{"filepath"} ||= $ENV{"GROUPSECRET_KEYFILE"} || "groupsecret.yml"
l!l&&r!l&&!r
000
$ENV{'GROUPSECRET_KEYFILE'} || 'groupsecret.yml'
l!l
00
MC/DC0%0/2
+(shift())->{"filepath"} ||= $ENV{"GROUPSECRET_KEYFILE"} || "groupsecret.yml"
+(shift())->{"filepath"} $ENV{'GROUPSECRET_KEYFILE'}
Truth table0%0/3
A ||= B
A= +(shift())->{"filepath"}B= $ENV{'GROUPSECRET_KEYFILE'}
ABresult
1X1
011
000
89 }
90  
91  
92 0 ▶sub file {
Subroutine file: not called CC=2 SCAR=17.9
Pod documented
93 0 my $self = shift;
94 0 ▶ return $self->{file} ||= App::GroupSecret::File->new($self->filepath);
Condition0%0/3
$self->{"file"} ||= "App::GroupSecret::File"->new($self->filepath)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"file"} ||= "App::GroupSecret::File"->new($self->filepath)
$self->{"file"} "App::GroupSecret::File"->new($self->filepath)
Truth table0%0/3
A ||= B
A= $self->{"file"}B= "App::GroupSecret::File"->new($self->filepath)
ABresult
1X1
011
000
95 }
96  
97  
98 0 ▶sub private_key {
Subroutine private_key: not called CC=3 SCAR=24.8
Pod documented
99 0 ▶ shift->{private_key} ||= $ENV{GROUPSECRET_PRIVATE_KEY} || "$ENV{HOME}/.ssh/id_rsa";
Condition0%0/5
+(shift())->{"private_key"} ||= $ENV{"GROUPSECRET_PRIVATE_KEY"} || qq[$ENV{"HOME"}/.ssh/id_rsa]
l!l&&r!l&&!r
000
$ENV{'GROUPSECRET_PRIVATE_KEY'} || "$ENV{'HOME'}/.ssh/id_rsa"
l!l
00
MC/DC0%0/2
+(shift())->{"private_key"} ||= $ENV{"GROUPSECRET_PRIVATE_KEY"} || qq[$ENV{"HOME"}/.ssh/id_rsa]
+(shift())->{"private_key"} $ENV{'GROUPSECRET_PRIVATE_KEY'}
Truth table0%0/3
A ||= B
A= +(shift())->{"private_key"}B= $ENV{'GROUPSECRET_PRIVATE_KEY'}
ABresult
1X1
011
000
100 }
101  
102 0 ▶sub _action_print_secret {
Subroutine _action_print_secret: not called CC=7 SCAR=40.3
103 0 my $self = shift;
104  
105 0 my $decrypt = 1;
106 0 ▶ GetOptionsFromArray(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@_, "decrypt!", \$decrypt) or pod2usage(2) 0 0 0
107 \@_,
108 'decrypt!' => \$decrypt,
109 ) or pod2usage(2);
110  
111 0 my $file = $self->file;
112 0 my $filepath = $file->filepath;
113 0 ▶ die "No keyfile '$filepath' exists -- use the \`add-key' command to create one.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filepath and not -d $filepath 0 0 0
Condition0%0/3
-e $filepath and not -d $filepath
!ll&&!rl&&r
000
MC/DC0%0/2
-e $filepath and not -d $filepath
-e $filepath not -d $filepath
Truth table0%0/3
A and not(B)
A= -e $filepathB= not -d $filepath
ABresult
0X0
100
111
114 unless -e $filepath && !-d $filepath;
115 0 ▶ die "No secret in keyfile '$filepath' exists -- use the \`set-secret' command to set one.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $file->secret 0 0 0
116 if !$file->secret;
117  
118 0 ▶ if ($decrypt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decrypt) { } 0 0 0
119 0 my $private_key = $self->private_key;
120 0 ▶ my $secret = $file->decrypt_secret(private_key => $private_key) or die "No secret.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $secret = $file->decrypt_secret("private_key", $private_key) or die "No secret.\n" 0 0 0
121 0 print $secret;
122 }
123 else {
124 0 print $file->secret;
125 }
126 }
127  
128 0 ▶sub _action_set_secret {
Subroutine _action_set_secret: not called CC=7 SCAR=40.3
129 0 my $self = shift;
130  
131 0 my $keep_passphrase = 0;
132 0 ▶ GetOptionsFromArray(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@_, "keep-passphrase!", \$keep_passphrase) or pod2usage(2) 0 0 0
133 \@_,
134 'keep-passphrase!' => \$keep_passphrase,
135 ) or pod2usage(2);
136  
137 0 my $secret_spec = shift;
138 0 ▶ if (!$secret_spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret_spec) 0 0 0
139 0 warn "You must specify a secret to set.\n";
140 0 pod2usage(2);
141 }
142  
143 0 my $passphrase;
144 my $secret;
145  
146 0 ▶ if ($secret_spec =~ /^rand:(\d+)$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($secret_spec =~ /^rand:(\d+)$/i) { } 0 0 0
elsif ($secret_spec eq '-') { } 0 0 0
elsif ($secret_spec =~ /^file:(.*)$/i) { } 0 0 0
147 0 my $rand = encode_base64(generate_secure_random_bytes($1), '');
148 0 $secret = \$rand;
149 }
150 elsif ($secret_spec eq '-') {
151 0 my $in = do { local $/; <STDIN> };
152 0 $secret = \$in;
153 }
154 elsif ($secret_spec =~ /^file:(.*)$/i) {
155 0 $secret = $1;
156 }
157 else {
158 0 $secret = $secret_spec;
159 }
160  
161 0 my $file = $self->file;
162  
163 0 ▶ if ($keep_passphrase) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_passphrase) { } 0 0 0
164 0 my $private_key = $self->private_key;
165 0 $passphrase = $file->decrypt_secret_passphrase($private_key);
166 0 $file->encrypt_secret($secret, $passphrase);
167 }
168 else {
169 0 $passphrase = generate_secure_random_bytes(32);
170 0 $file->encrypt_secret($secret, $passphrase);
171 0 $file->encrypt_secret_passphrase($passphrase);
172 }
173  
174 0 $file->save;
175 }
176  
177 0 ▶sub _action_add_key {
Subroutine _action_add_key: not called CC=7 SCAR=40.3
178 0 my $self = shift;
179  
180 0 my $embed = 0;
181 0 my $update = 0;
182 0 ▶ GetOptionsFromArray(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@_, "embed", \$embed, "update|u", \$update) or pod2usage(2) 0 0 0
183 \@_,
184 'embed' => \$embed,
185 'update|u' => \$update,
186 ) or pod2usage(2);
187  
188 0 my $file = $self->file;
189 0 my $keys = $file->keys;
190  
191 0 my $opts = {embed => $embed};
192  
193 0 for my $public_key (@_) {
194 0 my $info = read_openssh_key_fingerprint($public_key);
195  
196 0 ▶ if ($keys->{$info->{fingerprint}} && !$update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$keys{$info->{"fingerprint"}} and not $update) 0 0 0
Condition0%0/3
$$keys{$info->{"fingerprint"}} and not $update
!ll&&!rl&&r
000
MC/DC0%0/2
$$keys{$info->{"fingerprint"}} and not $update
$$keys{$info->{"fingerprint"}} not $update
Truth table0%0/3
A and not(B)
A= $$keys{$info->{"fingerprint"}}B= not $update
ABresult
0X0
100
111
197 0 my $formatted_key = $file->format_key($info);
198 0 print "SKIP\t$formatted_key\n";
199 0 next;
200 }
201  
202 0 ▶ if ($file->secret && !$opts->{passphrase}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->secret and not $opts->{"passphrase"}) 0 0 0
Condition0%0/3
$file->secret and not $opts->{"passphrase"}
!ll&&!rl&&r
000
MC/DC0%0/2
$file->secret and not $opts->{"passphrase"}
$file->secret not $opts->{"passphrase"}
Truth table0%0/3
A and not(B)
A= $file->secretB= not $opts->{"passphrase"}
ABresult
0X0
100
111
203 0 my $private_key = $self->private_key;
204 0 my $passphrase = $file->decrypt_secret_passphrase($private_key);
205 0 $opts->{passphrase} = $passphrase;
206 }
207  
208 0 local $opts->{fingerprint_info} = $info;
209 0 my ($fingerprint, $key) = $file->add_key($public_key, $opts);
210  
211 0 local $key->{fingerprint} = $fingerprint;
212 0 my $formatted_key = $file->format_key($key);
213 0 print "ADD\t$formatted_key\n";
214 }
215  
216 0 $file->save;
217 }
218  
219 0 ▶sub _action_delete_key {
Subroutine _action_delete_key: not called CC=4 SCAR=30.0
220 0 my $self = shift;
221  
222 0 my $file = $self->file;
223  
224 0 for my $fingerprint (@_) {
225 0 ▶ if ($fingerprint =~ s/^(?:MD5|SHA1|SHA256)://) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fingerprint =~ s/^(?:MD5|SHA1|SHA256)://) { } 0 0 0
226 0 $fingerprint =~ s/://g;
227 }
228 else {
229 0 my $info = read_openssh_key_fingerprint($fingerprint);
230 0 $fingerprint = $info->{fingerprint};
231 }
232  
233 0 my $key = $file->keys->{$fingerprint};
234 0 ▶ $file->delete_key($fingerprint) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
235  
236 0 local $key->{fingerprint} = $fingerprint;
237 0 my $formatted_key = $file->format_key($key);
238 0 print "DELETE\t$formatted_key\n";
239 }
240  
241 0 $file->save;
242 }
243  
244 0 ▶sub _action_list_keys {
Subroutine _action_list_keys: not called CC=2 SCAR=17.9
245 0 my $self = shift;
246  
247 0 my $file = $self->file;
248 0 my $keys = $file->keys;
249  
250 0 ▶ while (my ($fingerprint, $key) = each %$keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$keys) 0 0 0
251 0 local $key->{fingerprint} = $fingerprint;
252 0 my $formatted_key = $file->format_key($key);
253 0 print "$formatted_key\n";
254 }
255 }
256  
257 1 1;
258  
259 __END__
↑ Summary GroupSecret/File.pm »