blib/lib/Lemonldap/NG/Portal/Lib/Key.pm

filter: Statementstmt 86.0% 43 / 50 Branchbran 71.8% 23 / 32 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 69.0% 78 / 113 CC 25 SCAR 31.1
CC
25
Cov
73%
_get_key_config
32.4
get_private_key
4.6
get_public_key
4.0
CRAP 22.5
« blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/OfflineTokens.pm »
1 package Lemonldap::NG::Portal::Lib::Key;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Lemonldap::NG::Portal::Lib::Key - A role for private key management in Portal
8 plugins
9  
10 =head1 SYNOPSIS
11  
12 use Mouse;
13  
14 with 'Lemonldap::NG::Portal::Lib::Key';
15  
16 =head1 DESCRIPTION
17  
18 This role is meant to be composed into portal modules to give them the ability
19 to lookup private/public keys from the General parameters > Keys interface
20  
21 All asymetric key retrieval should be performed through the functions provided
22 here
23  
24 =head1 METHODS
25 =cut
26  
27 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 56 ▶use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 56 our $VERSION = '2.22.0';
31  
32 56 requires qw(conf);
33  
34 =pod
35  
36 =head2 get_private_key
37  
38 This method returns a private key structure containing the following fields
39  
40 =over
41  
42 =item private: PEM private key
43  
44 =item password: optional password for key decryption
45  
46 =item public: PEM public key or certificate
47  
48 =item external_id: optional external key identified (kid)
49  
50 =back
51  
52 =cut
53  
54 12 ▶sub get_private_key {
Subroutine get_private_key: called CC=4 SCAR=15.2
Pod documented
55 12 my ( $self, $key_id ) = @_;
56 12 ▶ my $key_id_log = $key_id // '[undef]' || "[empty]";
Condition50%2/4
($key_id // '[undef]') || '[empty]'
l!l
120
$key_id // "[undef]"
l!l
120
MC/DC0%0/1
($key_id // '[undef]') || '[empty]'
$key_id
Truth table50%1/2
A
A= $key_id
Aresult
11
00
57  
58 12 $self->logger->debug("Looking up private key $key_id_log");
59  
60 12 my $key_config = $self->_get_key_config($key_id);
61  
62 12 ▶ if ($key_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_config) 12 0 12
63 12 $self->logger->debug("Found private key $key_id_log");
64 return {
65 private => $key_config->{keyPrivate},
66 public => $key_config->{keyPublic},
67 external_id => $key_config->{keyId},
68 password => $key_config->{keyPrivatePwd},
69 12 };
70 }
71  
72 0 $self->logger->debug("Private key $key_id_log not found");
73 0 return;
74 }
75  
76 109 ▶sub get_public_key {
Subroutine get_public_key: called CC=4 SCAR=14.0
Pod undocumented
77 109 my ( $self, $key_id ) = @_;
78 109 ▶ my $key_id_log = $key_id // '[undef]' || "[empty]";
Condition50%2/4
($key_id // '[undef]') || '[empty]'
l!l
1090
$key_id // "[undef]"
l!l
1090
MC/DC0%0/1
($key_id // '[undef]') || '[empty]'
$key_id
Truth table50%1/2
A
A= $key_id
Aresult
11
00
79 109 $self->logger->debug("Looking up public key $key_id_log");
80  
81 109 my $key_config = $self->_get_key_config($key_id);
82  
83 109 ▶ if ($key_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_config) 49 60 109
84 49 $self->logger->debug("Found public key $key_id_log");
85 return {
86 public => $key_config->{keyPublic},
87 external_id => $key_config->{keyId},
88 49 };
89 }
90  
91 60 $self->logger->debug("Public key $key_id_log not found");
92 60 return;
93 }
94  
95 121 ▶sub _get_key_config {
Subroutine _get_key_config: called CC=19 SCAR=34.8
96 121 my ( $self, $key_id ) = @_;
97  
98 121 ▶ return unless $key_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_id 0 121 121
99 121 ▶ if ( my $key = $self->conf->{'keys'}->{$key_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $self->conf->{"keys"}{$key_id}) 6 115 121
100 return {
101 keyPrivate => $key->{keyPrivate},
102 keyPrivatePwd => ( $key->{keyPrivatePwd} // "" ),
103 keyPublic => $key->{keyPublic},
104 6 ▶ keyId => ( $key->{keyId} || $key_id ),
Condition40%2/5
$key->{"keyPrivatePwd"} // ""
l!l
06
$key->{'keyId'} || $key_id
l!l&&r!l&&!r
600
MC/DC0%0/1
$key->{"keyPrivatePwd"} // ""
$key->{"keyPrivatePwd"}
MC/DC0%0/2
$key->{'keyId'} || $key_id
$key->{'keyId'} $key_id
Truth table50%1/2
A
A= $key->{"keyPrivatePwd"}
Aresult
11
00
Truth table33%1/3
A || B
A= $key->{'keyId'}B= $key_id
ABresult
1X1
011
000
105 };
106 }
107  
108 115 ▶ if ( $key_id eq "default-saml-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-saml-sig") 0 115 115
109 0 ▶ if ( $self->conf->{samlServicePrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"samlServicePrivateKeySig"}) 0 0 0
110 return {
111 keyPrivate => $self->conf->{samlServicePrivateKeySig},
112 keyPrivatePwd =>
113 ( $self->conf->{samlServicePrivateKeySigPwd} // '' ),
114 keyPublic => $self->conf->{samlServicePublicKeySig},
115 0 ▶ keyId => "default-saml-sig",
Condition0%0/2
$self->conf->{"samlServicePrivateKeySigPwd"} // ""
l!l
00
MC/DC0%0/1
$self->conf->{"samlServicePrivateKeySigPwd"} // ""
$self->conf->{"samlServicePrivateKeySigPwd"}
Truth table0%0/2
A
A= $self->conf->{"samlServicePrivateKeySigPwd"}
Aresult
11
00
116 };
117 }
118 }
119  
120 115 ▶ if ( $key_id eq "default-saml-enc" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-saml-enc") 0 115 115
121 0 ▶ if ( $self->conf->{samlServicePrivateKeyEnc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"samlServicePrivateKeyEnc"}) 0 0 0
122 return {
123 keyPrivate => $self->conf->{samlServicePrivateKeyEnc},
124 keyPrivatePwd =>
125 ( $self->conf->{samlServicePrivateKeyEncPwd} // '' ),
126 keyPublic => $self->conf->{samlServicePublicKeyEnc},
127 0 ▶ keyId => "default-saml-enc",
Condition0%0/2
$self->conf->{"samlServicePrivateKeyEncPwd"} // ""
l!l
00
MC/DC0%0/1
$self->conf->{"samlServicePrivateKeyEncPwd"} // ""
$self->conf->{"samlServicePrivateKeyEncPwd"}
Truth table0%0/2
A
A= $self->conf->{"samlServicePrivateKeyEncPwd"}
Aresult
11
00
128 };
129 }
130 }
131  
132 115 ▶ if ( $key_id eq "default-oidc-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-oidc-sig") 47 68 115
133 47 ▶ if ( $self->conf->{oidcServicePrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServicePrivateKeySig"}) 44 3 47
134 return {
135 keyPrivate => $self->conf->{oidcServicePrivateKeySig},
136 keyPrivatePwd => '',
137 keyPublic => $self->conf->{oidcServicePublicKeySig},
138 keyId => $self->conf->{oidcServiceKeyIdSig},
139 44 };
140 }
141 }
142  
143 71 ▶ if ( $key_id eq "old-oidc-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "old-oidc-sig") 24 47 71
144 24 ▶ if ( $self->conf->{oidcServiceOldPrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServiceOldPrivateKeySig"}) 8 16 24
145 return {
146 keyPrivate => $self->conf->{oidcServiceOldPrivateKeySig},
147 keyPrivatePwd => '',
148 keyPublic => $self->conf->{oidcServiceOldPublicKeySig},
149 keyId => $self->conf->{oidcServiceOldKeyIdSig},
150 8 };
151 }
152 }
153  
154 63 ▶ if ( $key_id eq "new-oidc-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "new-oidc-sig") 21 42 63
155 21 ▶ if ( $self->conf->{oidcServiceNewPrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServiceNewPrivateKeySig"}) 0 21 21
156 return {
157 keyPrivate => $self->conf->{oidcServiceNewPrivateKeySig},
158 keyPrivatePwd => '',
159 keyPublic => $self->conf->{oidcServiceNewPublicKeySig},
160 keyId => $self->conf->{oidcServiceNewKeyIdSig},
161 0 };
162 }
163 }
164  
165 63 ▶ if ( $key_id eq "default-oidc-enc" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-oidc-enc") 23 40 63
166 23 ▶ if ( $self->conf->{oidcServicePrivateKeyEnc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServicePrivateKeyEnc"}) 3 20 23
167 return {
168 keyPrivate => $self->conf->{oidcServicePrivateKeyEnc},
169 keyPrivatePwd => '',
170 keyPublic => $self->conf->{oidcServicePublicKeyEnc},
171 keyId => $self->conf->{oidcServiceKeyIdEnc},
172 3 };
173 }
174 }
175  
176 60 return;
177 }
178  
179 56 1;
180  
181 __END__
« blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/OfflineTokens.pm »