GroupSecret/File.pm

filter: Statementstmt 39.3% 52 / 132 Branchbran 13.6% 9 / 66 Conditioncond 19.0% 8 / 42 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 59.2% 16 / 27 Podpod 100% 18 / 18 totaltotal 33.0% 103 / 312 CC 50 SCAR 33.9
CC
50
Cov
29%
add_key
90.0
find_public_key
56.0
format_key
56.0
CRAP 29.7
« GroupSecret.pm ↑ Summary GroupSecret/Crypt.pm »
1 package App::GroupSecret::File;
2 # ABSTRACT: Reading and writing groupsecret keyfiles
3  
4  
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.304'; # VERSION
9  
10 3 ▶use App::GroupSecret::Crypt qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 generate_secure_random_bytes
12 read_openssh_public_key
13 read_openssh_key_fingerprint
14 decrypt_rsa
15 encrypt_rsa
16 decrypt_aes_256_cbc
17 encrypt_aes_256_cbc
18 3 );
19 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use YAML::Tiny qw(LoadFile DumpFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 our $FILE_VERSION = 1;
25  
26 0 ▶sub _croak { require Carp; Carp::croak(@_) }
Subroutine _croak: not called CC=1 SCAR=6.9
27 0 ▶sub _usage { _croak("Usage: @_\n") }
Subroutine _usage: not called CC=1 SCAR=6.9
28  
29  
30 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
31 3 my $class = shift;
32 3 ▶ my $filepath = shift or _croak(q{App::GroupSecret::File->new($filepath)});
Branch: 1 branch
BranchTrueFalseTotal
my $filepath = shift() or _croak("App::GroupSecret::File->new(\$filepath)") 3 0 3
33 3 return bless {filepath => $filepath}, $class;
34 }
35  
36  
37 5 ▶sub filepath { shift->{filepath} }
Subroutine filepath: called CC=1 SCAR=0.0
Pod documented
38  
39  
40 5 ▶sub info {
Subroutine info: called CC=3 SCAR=11.0
Pod documented
41 5 my $self = shift;
42 5 ▶ return $self->{info} ||= do {
Condition66%2/3
$self->{"info"} ||= do { if (-e $self->filepath) { $self->load; } else { $self->init; } }
l!l&&r!l&&!r
230
MC/DC0%0/2
$self->{"info"} ||= do { if (-e $self->filepath) { $self->load; } else { $self->init; } }
$self->{"info"} do { if (-e $self->filepath) { $self->load; } else { $self->init; } }
Truth table66%2/3
A ||= B
A= $self->{"info"}B= do { if (-e $self->filepath) { $self->load; } else { $self->init; } }
ABresult
1X1
011
000
43 3 ▶ if (-e $self->filepath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->filepath) { } 2 1 3
44 2 $self->load;
45 }
46 else {
47 1 $self->init;
48 }
49 };
50 }
51  
52  
53 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
54 return {
55 1 keys => {},
56 secret => undef,
57 version => $FILE_VERSION,
58 };
59 }
60  
61  
62 2 ▶sub load {
Subroutine load: called CC=4 SCAR=15.0
Pod documented
63 2 my $self = shift;
64 2 ▶ my $filepath = shift || $self->filepath;
Condition33%1/3
shift() || $self->filepath
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() || $self->filepath
shift() $self->filepath
Truth table33%1/3
A || B
A= shift()B= $self->filepath
ABresult
1X1
011
000
65 2 ▶ my $info = LoadFile($filepath) || {};
Condition50%1/2
LoadFile($filepath) || {}
l!l
20
MC/DC0%0/1
LoadFile($filepath) || {}
LoadFile($filepath)
Truth table50%1/2
A
A= LoadFile($filepath)
Aresult
11
00
66 2 $self->check($info);
67 2 ▶ $self->{info} = $info if !$filepath;
Branch: 1 branch
BranchTrueFalseTotal
unless $filepath 0 2 2
68 2 return $info;
69 }
70  
71  
72 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
73 0 my $self = shift;
74 0 ▶ my $filepath = shift || $self->filepath;
Condition0%0/3
shift() || $self->filepath
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->filepath
shift() $self->filepath
Truth table0%0/3
A || B
A= shift()B= $self->filepath
ABresult
1X1
011
000
75 0 DumpFile($filepath, $self->info);
76 0 return $self;
77 }
78  
79  
80 2 ▶sub check {
Subroutine check: called CC=10 SCAR=29.1
Pod documented
81 2 my $self = shift;
82 2 ▶ my $info = shift || $self->info;
Condition33%1/3
shift() || $self->info
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || $self->info
shift() $self->info
Truth table33%1/3
A || B
A= shift()B= $self->info
ABresult
1X1
011
000
83  
84 2 ▶ _croak 'Corrupt file: Bad type for root' if !$info || ref $info ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not $info or ref $info ne "HASH" 0 2 2
Condition33%1/3
not $info or ref $info ne "HASH"
l!l&&r!l&&!r
002
MC/DC0%0/2
not $info or ref $info ne "HASH"
not $info ref $info ne "HASH"
Truth table33%1/3
not(A) or B
A= not $infoB= ref $info ne "HASH"
ABresult
1X1
011
000
85  
86 2 my $version = $info->{version};
87 2 ▶ _croak 'Unknown file version' if !$version || $version !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $version and $version =~ /^\d+$/ 0 2 2
Condition33%1/3
$version and $version =~ /^\d+$/
!ll&&!rl&&r
002
MC/DC0%0/2
$version and $version =~ /^\d+$/
$version $version =~ /^\d+$/
Truth table33%1/3
A and B
A= $versionB= $version =~ /^\d+$/
ABresult
0X0
100
111
88 2 ▶ _croak 'Unsupported file version' if $FILE_VERSION < $version;
Branch: 1 branch
BranchTrueFalseTotal
if $App::GroupSecret::File::FILE_VERSION < $version 0 2 2
89  
90 2 ▶ _croak 'Corrupt file: Bad type for keys' if ref $info->{keys} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $info->{"keys"} ne "HASH" 0 2 2
91  
92 0 ▶ warn "The file has a secret but no keys to access it!\n" if $info->{secret} && !%{$info->{keys}};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"secret"} and not %{$info->{"keys"};} 0 2 2
Condition33%1/3
$info->{"secret"} and not %{$info->{"keys"};}
!ll&&!rl&&r
200
MC/DC0%0/2
$info->{"secret"} and not %{$info->{"keys"};}
$info->{"secret"} not %{$info->{"keys"};}
Truth table33%1/3
A and not(B)
A= $info->{"secret"}B= not %{$info->{"keys"};}
ABresult
0X0
100
111
93  
94 2 return 1;
95 }
96  
97  
98 1 ▶sub keys { shift->info->{keys} }
Subroutine keys: called CC=1 SCAR=0.0
Pod documented
99 1 ▶sub secret { shift->info->{secret} }
Subroutine secret: called CC=1 SCAR=0.0
Pod documented
100 1 ▶sub version { shift->info->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
101  
102  
103 0 ▶sub add_key {
Subroutine add_key: not called CC=9 SCAR=45.0
Pod documented
104 0 my $self = shift;
105 0 ▶ my $public_key = shift or _usage(q{$file->add_key($public_key)});
Branch: 1 branch
BranchTrueFalseTotal
my $public_key = shift() or _usage("\$file->add_key(\$public_key)") 0 0 0
106 0 ▶ my $args = @_ == 1 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
107  
108 0 my $keys = $self->keys;
109  
110 0 ▶ my $info = $args->{fingerprint_info} || read_openssh_key_fingerprint($public_key);
Condition0%0/3
$args->{'fingerprint_info'} || read_openssh_key_fingerprint($public_key)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'fingerprint_info'} || read_openssh_key_fingerprint($public_key)
$args->{'fingerprint_info'} read_openssh_key_fingerprint($public_key)
Truth table0%0/3
A || B
A= $args->{'fingerprint_info'}B= read_openssh_key_fingerprint($public_key)
ABresult
1X1
011
000
111 0 my $fingerprint = $info->{fingerprint};
112  
113 my $key = {
114 comment => $info->{comment},
115 filename => basename($public_key),
116 secret_passphrase => undef,
117 type => $info->{type},
118 0 };
119  
120 0 ▶ if ($args->{embed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"embed"}) 0 0 0
121 0 ▶ open(my $fh, '<', $public_key) or die "open failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $public_key or die "open failed: $!" 0 0 0
122 0 $key->{content} = do { local $/; <$fh> };
123 0 chomp $key->{content};
124 }
125  
126 0 $keys->{$fingerprint} = $key;
127  
128 0 ▶ if ($self->secret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->secret) 0 0 0
129 0 ▶ my $passphrase = $args->{passphrase} || $self->decrypt_secret_passphrase($args->{private_key});
Condition0%0/3
$args->{'passphrase'} || $self->decrypt_secret_passphrase($args->{'private_key'})
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'passphrase'} || $self->decrypt_secret_passphrase($args->{'private_key'})
$args->{'passphrase'} $self->decrypt_secret_passphrase($args->{'private_key'})
Truth table0%0/3
A || B
A= $args->{'passphrase'}B= $self->decrypt_secret_passphrase($args->{'private_key'})
ABresult
1X1
011
000
130 0 my $ciphertext = encrypt_rsa(\$passphrase, $public_key);
131 0 $key->{secret_passphrase} = $ciphertext;
132 }
133  
134 0 ▶ return wantarray ? ($fingerprint => $key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
135 }
136  
137  
138 0 ▶sub delete_key {
Subroutine delete_key: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 my $fingerprint = shift;
141 0 delete $self->keys->{$fingerprint};
142 }
143  
144  
145 0 ▶sub decrypt_secret {
Subroutine decrypt_secret: not called CC=5 SCAR=34.0
Pod documented
146 0 my $self = shift;
147 0 ▶ my $args = @_ == 1 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
148  
149 0 ▶ $args->{passphrase} || $args->{private_key} or _usage(q{$file->decrypt_secret($private_key)});
Branch: 1 branch
BranchTrueFalseTotal
$args->{"passphrase"} or $args->{"private_key"} or _usage("\$file->decrypt_secret(\$private_key)") 0 0 0
Condition0%0/3
$args->{"passphrase"} or $args->{"private_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"passphrase"} or $args->{"private_key"}
$args->{"passphrase"} $args->{"private_key"}
Truth table0%0/3
A or B
A= $args->{"passphrase"}B= $args->{"private_key"}
ABresult
1X1
011
000
150  
151 0 my $passphrase = $args->{passphrase};
152 0 ▶ $passphrase = $self->decrypt_secret_passphrase($args->{private_key}) if !$passphrase;
Branch: 1 branch
BranchTrueFalseTotal
unless $passphrase 0 0 0
153  
154 0 my $ciphertext = $self->secret;
155 0 return decrypt_aes_256_cbc(\$ciphertext, $passphrase);
156 }
157  
158  
159 0 ▶sub decrypt_secret_passphrase {
Subroutine decrypt_secret_passphrase: not called CC=5 SCAR=34.0
Pod documented
160 0 my $self = shift;
161 0 ▶ my $private_key = shift or _usage(q{$file->decrypt_secret_passphrase($private_key)});
Branch: 1 branch
BranchTrueFalseTotal
my $private_key = shift() or _usage("\$file->decrypt_secret_passphrase(\$private_key)") 0 0 0
162  
163 0 ▶ die "Private key '$private_key' not found.\n" unless -e $private_key && !-d $private_key;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $private_key and not -d $private_key 0 0 0
Condition0%0/3
-e $private_key and not -d $private_key
!ll&&!rl&&r
000
MC/DC0%0/2
-e $private_key and not -d $private_key
-e $private_key not -d $private_key
Truth table0%0/3
A and not(B)
A= -e $private_keyB= not -d $private_key
ABresult
0X0
100
111
164  
165 0 my $info = read_openssh_key_fingerprint($private_key);
166 0 my $fingerprint = $info->{fingerprint};
167  
168 0 my $keys = $self->keys;
169 0 ▶ if (my $key = $keys->{$fingerprint}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $keys->{$fingerprint}) 0 0 0
170 0 return decrypt_rsa(\$key->{secret_passphrase}, $private_key);
171 }
172  
173 0 die "Private key '$private_key' not able to decrypt the keyfile.\n";
174 }
175  
176  
177 0 ▶sub encrypt_secret {
Subroutine encrypt_secret: not called CC=3 SCAR=24.8
Pod documented
178 0 my $self = shift;
179 0 ▶ my $secret = shift or _usage(q{$file->encrypt_secret($secret)});
Branch: 1 branch
BranchTrueFalseTotal
my $secret = shift() or _usage("\$file->encrypt_secret(\$secret)") 0 0 0
180 0 ▶ my $passphrase = shift or _usage(q{$file->encrypt_secret($secret)});
Branch: 1 branch
BranchTrueFalseTotal
my $passphrase = shift() or _usage("\$file->encrypt_secret(\$secret)") 0 0 0
181  
182 0 my $ciphertext = encrypt_aes_256_cbc($secret, $passphrase);
183 0 $self->info->{secret} = $ciphertext;
184 }
185  
186  
187 0 ▶sub encrypt_secret_passphrase {
Subroutine encrypt_secret_passphrase: not called CC=4 SCAR=30.0
Pod documented
188 0 my $self = shift;
189 0 ▶ my $passphrase = shift or _usage(q{$file->encrypt_secret_passphrase($passphrase)});
Branch: 1 branch
BranchTrueFalseTotal
my $passphrase = shift() or _usage("\$file->encrypt_secret_passphrase(\$passphrase)") 0 0 0
190  
191 0 ▶ while (my ($fingerprint, $key) = each %{$self->keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->keys;}) 0 0 0
192 0 local $key->{fingerprint} = $fingerprint;
193 0 ▶ my $pubkey = $self->find_public_key($key) or die 'Cannot find public key: ' . $self->format_key($key) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
my $pubkey = $self->find_public_key($key) or die 'Cannot find public key: ' . $self->format_key($key) . "\n" 0 0 0
194 0 my $ciphertext = encrypt_rsa(\$passphrase, $pubkey);
195 0 $key->{secret_passphrase} = $ciphertext;
196 }
197 }
198  
199  
200 0 ▶sub find_public_key {
Subroutine find_public_key: not called CC=7 SCAR=40.3
Pod documented
201 0 my $self = shift;
202 0 ▶ my $key = shift or _usage(q{$file->find_public_key($key)});
Branch: 1 branch
BranchTrueFalseTotal
my $key = shift() or _usage("\$file->find_public_key(\$key)") 0 0 0
203  
204 0 ▶ if ($key->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->{'content'}) { } 0 0 0
205 0 my $temp = File::Temp->new(UNLINK => 1);
206 0 print $temp $key->{content};
207 0 close $temp;
208 0 $self->{"temp:$key->{fingerprint}"} = $temp;
209 0 return $temp->filename;
210 }
211 else {
212 0 ▶ my @dirs = split(/:/, $ENV{GROUPSECRET_PATH} || ".:keys:$ENV{HOME}/.ssh");
Condition0%0/2
$ENV{'GROUPSECRET_PATH'} || ".:keys:$ENV{'HOME'}/.ssh"
l!l
00
MC/DC0%0/1
$ENV{'GROUPSECRET_PATH'} || ".:keys:$ENV{'HOME'}/.ssh"
$ENV{'GROUPSECRET_PATH'}
Truth table0%0/2
A
A= $ENV{'GROUPSECRET_PATH'}
Aresult
11
00
213 0 for my $dir (@dirs) {
214 0 my $filepath = File::Spec->catfile($dir, $key->{filename});
215 0 ▶ return $filepath if -e $filepath && !-d $filepath;
Branch: 1 branch
BranchTrueFalseTotal
if -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
216 }
217 }
218 }
219  
220  
221 0 ▶sub format_key {
Subroutine format_key: not called CC=7 SCAR=40.3
Pod documented
222 0 my $self = shift;
223 0 ▶ my $key = shift or _usage(q{$file->format_key($key)});
Branch: 1 branch
BranchTrueFalseTotal
my $key = shift() or _usage("\$file->format_key(\$key)") 0 0 0
224  
225 0 ▶ my $fingerprint = $key->{fingerprint} or _croak(q{Missing required field in key: fingerprint});
Branch: 1 branch
BranchTrueFalseTotal
my $fingerprint = $key->{"fingerprint"} or _croak("Missing required field in key: fingerprint") 0 0 0
226 0 ▶ my $comment = $key->{comment} || 'uncommented';
Condition0%0/2
$key->{'comment'} || 'uncommented'
l!l
00
MC/DC0%0/1
$key->{'comment'} || 'uncommented'
$key->{'comment'}
Truth table0%0/2
A
A= $key->{'comment'}
Aresult
11
00
227  
228 0 ▶ if ($fingerprint =~ /^[A-Fa-f0-9]{32}$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($fingerprint =~ /^[A-Fa-f0-9]{32}$/) { } 0 0 0
elsif ($fingerprint =~ m[^[A-Za-z0-9/\+]{27}$]) { } 0 0 0
elsif ($fingerprint =~ m[^[A-Za-z0-9/\+]{43}$]) { } 0 0 0
229 0 $fingerprint = 'MD5:' . join(':', ($fingerprint =~ /../g ));
230 }
231 elsif ($fingerprint =~ /^[A-Za-z0-9\/\+]{27}$/) {
232 0 $fingerprint = "SHA1:$fingerprint";
233 }
234 elsif ($fingerprint =~ /^[A-Za-z0-9\/\+]{43}$/) {
235 0 $fingerprint = "SHA256:$fingerprint";
236 }
237  
238 0 return "$fingerprint $comment";
239 }
240  
241 3 1;
242  
243 __END__
« GroupSecret.pm ↑ Summary GroupSecret/Crypt.pm »