scripts/importMetadata

filter: Statementstmt 74.5% 217 / 291 Branchbran 62.5% 55 / 88 Conditioncond 82.9% 39 / 47 MC/DCmcdc 56.5% 13 / 23 Subroutinesub 91.3% 21 / 23 Podpod n/a totaltotal 73.0% 345 / 472 CC 80 SCAR 35.1
CC
80
Cov
73%
get_config
132.0
main
42.0
transform_config
39.4
CRAP 33.6
« blib/lib/Lemonldap/NG/Common/Util.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Notifications/XML.pm »
1 #!/usr/bin/perl
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Getopt::Long qw/GetOptionsFromArray/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Lemonldap::NG::Common::Conf;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 476 ▶sub toEntityIDkey {
Subroutine toEntityIDkey: called CC=1 SCAR=0.0
13 476 my ( $prefix, $entityID ) = @_;
14  
15 476 my $entityIDKey = $entityID;
16 476 $entityIDKey =~ s/^https?:\/\///;
17 476 $entityIDKey =~ s/[^a-zA-Z0-9]/-/g;
18 476 $entityIDKey =~ s/-+$//g;
19 476 return ( $prefix . $entityIDKey );
20 }
21  
22 # Get configuration from CLI opts and config file
23 # CLI arguments take priority
24 # Multi value arguments are extended instead of replaced
25 0 ▶sub get_config {
Subroutine get_config: not called CC=11 SCAR=48.8
26 0 my ($array) = @_;
27  
28 0 my %opts;
29  
30 # Get command line options
31 0 my $result = GetOptionsFromArray(
32 $array, \%opts,
33 'metadata|m=s', 'verbose|v',
34 'help|h', 'spconfprefix|s=s',
35 'idpconfprefix|i=s', 'remove|r',
36 'nagios|a', 'ignore-sp=s@',
37 'ignore-idp=s@', 'dry-run|n',
38 'configfile|c=s',
39 );
40  
41 0 ▶ pod2usage(1) if $opts{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"help"} 0 0 0
42  
43 0 my $config;
44 0 ▶ if ( my $configfile = $opts{configfile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $configfile = $opts{"configfile"}) 0 0 0
45 0 my %iniconfig;
46 0 tie %iniconfig, 'Config::IniFiles',
47 ( -file => $configfile, -allowempty => 1 );
48 0 for my $section ( keys %iniconfig ) {
49  
50 # Copy tied hash into normal hash
51 0 $config->{$section} = { %{ $iniconfig{$section} } };
52 }
53  
54 }
55  
56 # Handle scalar options
57 0 for my $option (
58 qw/verbose spconfprefix remove dry-run metadata idpconfprefix nagios/)
59 {
60 0 ▶ if ( defined $opts{$option} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$option}) 0 0 0
61 0 $config->{main}->{$option} = $opts{$option};
62 }
63 }
64  
65 # Handle arrayref options by appending values from file to values from CLI
66 0 for my $option (qw/ignore-sp ignore-idp/) {
67 0 ▶ my $value_from_cmdline = $opts{$option} || [];
Condition0%0/2
$opts{$option} || []
l!l
00
MC/DC0%0/1
$opts{$option} || []
$opts{$option}
Truth table0%0/2
A
A= $opts{$option}
Aresult
11
00
68 0 my $value_from_conf = $config->{main}->{$option};
69 0 ▶ if ( ref($value_from_conf) ne "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value_from_conf ne "ARRAY") 0 0 0
70 0 ▶ if ($value_from_conf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_from_conf) { } 0 0 0
71 0 $config->{main}->{$option} = [$value_from_conf];
72 }
73 else {
74 0 $config->{main}->{$option} = [];
75 }
76 }
77 0 ▶ if ( defined $value_from_cmdline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_from_cmdline) 0 0 0
78 0 push @{ $config->{main}->{$option} }, @$value_from_cmdline;
79 }
80 }
81  
82 0 return $config;
83 }
84  
85 #==============================================================================
86 # Default values
87 #==============================================================================
88  
89 # Set here attributes that are declared for your SP in the federation
90 # They will be set as exported attributes for all IDP
91 #
92 2 my $exportedAttributes = {
93 'cn' => '0;cn',
94 'eduPersonPrincipalName' => '0;eduPersonPrincipalName',
95 'givenName' => '0;givenName',
96 'surname' => '0;surname',
97 'displayName' => '0;displayName',
98 'eduPersonAffiliation' => '0;eduPersonAffiliation',
99 'eduPersonPrimaryAffiliation' => '0;eduPersonPrimaryAffiliation',
100 'mail' => '0;mail',
101 'supannListeRouge' => '0;supannListeRouge',
102 'supannEtuCursusAnnee' => '0;supannEtuCursusAnnee',
103 };
104  
105 # Set here options that are applied on all SP from the federation
106 2 my $spOptions = {
107 'samlSPMetaDataOptionsCheckSLOMessageSignature' => 1,
108 'samlSPMetaDataOptionsCheckSSOMessageSignature' => 1,
109 'samlSPMetaDataOptionsEnableIDPInitiatedURL' => 0,
110 'samlSPMetaDataOptionsEncryptionMode' => 'none',
111 'samlSPMetaDataOptionsForceUTF8' => 1,
112 'samlSPMetaDataOptionsNameIDFormat' => '',
113 'samlSPMetaDataOptionsNotOnOrAfterTimeout' => 72000,
114 'samlSPMetaDataOptionsOneTimeUse' => 0,
115 'samlSPMetaDataOptionsSessionNotOnOrAfterTimeout' => 72000,
116 'samlSPMetaDataOptionsSignSLOMessage' => 1,
117 'samlSPMetaDataOptionsSignSSOMessage' => 1
118 };
119  
120 # Set here options that are applied on all IDP from the federation
121 2 my $idpOptions = {
122 'samlIDPMetaDataOptionsAdaptSessionUtime' => 0,
123 'samlIDPMetaDataOptionsAllowLoginFromIDP' => 0,
124 'samlIDPMetaDataOptionsCheckAudience' => 1,
125 'samlIDPMetaDataOptionsCheckSLOMessageSignature' => 1,
126 'samlIDPMetaDataOptionsCheckSSOMessageSignature' => 1,
127 'samlIDPMetaDataOptionsCheckTime' => 1,
128 'samlIDPMetaDataOptionsEncryptionMode' => 'none',
129 'samlIDPMetaDataOptionsForceAuthn' => 0,
130 'samlIDPMetaDataOptionsForceUTF8' => 0,
131 'samlIDPMetaDataOptionsIsPassive' => 0,
132 'samlIDPMetaDataOptionsNameIDFormat' => 'transient',
133 'samlIDPMetaDataOptionsRelayStateURL' => 0,
134 'samlIDPMetaDataOptionsSignSLOMessage' => -1,
135 'samlIDPMetaDataOptionsSignSSOMessage' => -1,
136 'samlIDPMetaDataOptionsStoreSAMLToken' => 0
137 };
138  
139 #==============================================================================
140 # Main
141 #==============================================================================
142  
143 2 our $verbose = 0;
144  
145 3136 ▶sub printlog {
Subroutine printlog: called CC=2 SCAR=9.2
146 3136 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::verbose) 0 3136 3136
147 0 print @_;
148 }
149 }
150  
151 20 ▶sub printlogerr {
Subroutine printlogerr: called CC=2 SCAR=9.2
152 20 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::verbose) 0 20 20
153 0 print STDERR @_;
154 }
155 }
156  
157 478 ▶sub register_saml_sp {
Subroutine register_saml_sp: called CC=2 SCAR=6.9
158 478 my ( $config, $lastConf, $confKey, $entityID, $partner_metadata,
159 $requestedAttributes )
160 = @_;
161  
162 my $old_provider = {
163 xml => $lastConf->{samlSPMetaDataXML}->{$confKey}->{samlSPMetaDataXML},
164 attributes => $lastConf->{samlSPMetaDataExportedAttributes}->{$confKey},
165 478 options => $lastConf->{samlSPMetaDataOptions}->{$confKey},
166 };
167  
168 # Update metadata
169 $lastConf->{samlSPMetaDataXML}->{$confKey}->{samlSPMetaDataXML} =
170 478 $partner_metadata;
171  
172 478 my $requested_attributes_lmconf =
173 _compute_requested_attributes( $config, $entityID, $requestedAttributes );
174  
175 # Update attributes
176 478 $lastConf->{samlSPMetaDataExportedAttributes}->{$confKey} =
177 $requested_attributes_lmconf;
178  
179 478 $lastConf->{samlSPMetaDataOptions}->{$confKey} =
180 _compute_sp_options( $config, $entityID );
181  
182 # handle eduPersonTargetedID
183 478 ▶ if ( $lastConf->{samlSPMetaDataExportedAttributes}->{$confKey}
Branch: 1 branch
BranchTrueFalseTotal
if ($lastConf->{"samlSPMetaDataExportedAttributes"}{$confKey}{"eduPersonTargetedID"}) 59 419 478
184 ->{eduPersonTargetedID} )
185 {
186 delete $lastConf->{samlSPMetaDataExportedAttributes}->{$confKey}
187 59 ->{eduPersonTargetedID};
188 $lastConf->{samlSPMetaDataOptions}->{$confKey}
189 59 ->{samlSPMetaDataOptionsNameIDFormat} = 'persistent';
190 }
191  
192 478 return _provider_equal( $lastConf, $old_provider, $confKey, "SP" );
193 }
194  
195 118 ▶sub _compute_idp_options {
Subroutine _compute_idp_options: called CC=1 SCAR=0.0
196 118 my ( $config, $entityID ) = @_;
197 118 return _compute_options( $config, $entityID, "samlIDPMetaDataOptions",
198 $idpOptions );
199 }
200  
201 478 ▶sub _compute_sp_options {
Subroutine _compute_sp_options: called CC=1 SCAR=0.0
202 478 my ( $config, $entityID ) = @_;
203 478 return _compute_options( $config, $entityID, "samlSPMetaDataOptions",
204 $spOptions );
205 }
206  
207 596 ▶sub _compute_options {
Subroutine _compute_options: called CC=5 SCAR=16.1
208 596 my ( $config, $entityID, $option_prefix, $defaultOptions ) = @_;
209  
210 596 my $provider_options = {%$defaultOptions};
211 596 ▶ my $global_provider_options = $config->{"ALL"} || {};
Condition50%1/2
$config->{'ALL'} || {}
l!l
5960
MC/DC0%0/1
$config->{'ALL'} || {}
$config->{'ALL'}
Truth table50%1/2
A
A= $config->{'ALL'}
Aresult
11
00
212 596 for my $key ( grep /^$option_prefix/, keys %$global_provider_options ) {
213 60 $provider_options->{$key} = $global_provider_options->{$key};
214 }
215 596 ▶ my $entityid_provider_options = $config->{$entityID} || {};
Condition100%2/2
$config->{$entityID} || {}
l!l
480116
MC/DC100%1/1
$config->{$entityID} || {}
$config->{$entityID}
Truth table100%2/2
A
A= $config->{$entityID}
Aresult
11
00
216 596 for my $key ( grep /^$option_prefix/, keys %$entityid_provider_options ) {
217 2 $provider_options->{$key} = $entityid_provider_options->{$key};
218 }
219 596 return $provider_options;
220 }
221  
222 478 ▶sub _compute_requested_attributes {
Subroutine _compute_requested_attributes: called CC=9 SCAR=22.1
223 478 my ( $config, $entityID, $requestedAttributes ) = @_;
224 478 my $result = {};
225  
226 478 for my $name ( keys %$requestedAttributes ) {
227 2551 my $attrconf = $requestedAttributes->{$name};
228 my $is_required = $config->{"$entityID"}->{"attribute_required_$name"}
229 // $config->{"$entityID"}->{"attribute_required"}
230 // $config->{"ALL"}->{"attribute_required_$name"}
231 2551 ▶ // $config->{"ALL"}->{"attribute_required"} // $attrconf->{required};
Condition83%10/12
((($$config{"$entityID"}{"attribute_required_$name"} // $$config{"$entityID"}{"attribute_required"}) // $config->{"ALL"}{"attribute_required_$name"}) // $config->{"ALL"}{"attribute_required"}) // $attrconf->{"required"}
l!l&&r!l&&!r
25722940
(($$config{"$entityID"}{"attribute_required_$name"} // $$config{"$entityID"}{"attribute_required"}) // $config->{"ALL"}{"attribute_required_$name"}) // $config->{"ALL"}{"attribute_required"}
l!l&&r!l&&!r
132442294
($$config{"$entityID"}{"attribute_required_$name"} // $$config{"$entityID"}{"attribute_required"}) // $config->{"ALL"}{"attribute_required_$name"}
l!l&&r!l&&!r
1302538
$$config{"$entityID"}{"attribute_required_$name"} // $$config{"$entityID"}{"attribute_required"}
l!l&&r!l&&!r
1122538
MC/DC0%0/5
((($$config{"$entityID"}{"attribute_required_$name"} // $$config{"$entityID"}{"attribute_required"}) // $config->{"ALL"}{"attribute_required_$name"}) // $config->{"ALL"}{"attribute_required"}) // $attrconf->{"required"}
$$config{"$entityID"}{"attribute_required_$name"} $$config{"$entityID"}{"attribute_required"} $config->{"ALL"}{"attribute_required_$name"} $config->{"ALL"}{"attribute_required"} $attrconf->{"required"}
Truth table66%4/6
A // B // C // D // E
A= $$config{"$entityID"}{"attribute_required_$name"}B= $$config{"$entityID"}{"attribute_required"}C= $config->{"ALL"}{"attribute_required_$name"}D= $config->{"ALL"}{"attribute_required"}E= $attrconf->{"required"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
232 my $req_attr_str = join( ';',
233 ( $is_required // 0 ),
234 $attrconf->{name},
235 ( $attrconf->{name_format} || '' ),
236 2551 ▶ ( $attrconf->{friendly_name} || '' ),
Condition83%5/6
$is_required // 0
l!l
25510
$attrconf->{'name_format'} || ''
l!l
2401150
$attrconf->{'friendly_name'} || ''
l!l
2401150
MC/DC0%0/1
$is_required // 0
$is_required
MC/DC100%1/1
$attrconf->{'name_format'} || ''
$attrconf->{'name_format'}
MC/DC100%1/1
$attrconf->{'friendly_name'} || ''
$attrconf->{'friendly_name'}
Truth table50%1/2
A
A= $is_required
Aresult
11
00
Truth table100%2/2
A
A= $attrconf->{'name_format'}
Aresult
11
00
Truth table100%2/2
A
A= $attrconf->{'friendly_name'}
Aresult
11
00
237 );
238 2551 $result->{$name} = $req_attr_str;
239 }
240 478 return $result;
241 }
242  
243 118 ▶sub register_saml_idp {
Subroutine register_saml_idp: called CC=1 SCAR=0.0
244 118 my ( $config, $lastConf, $confKey, $entityID, $partner_metadata ) = @_;
245  
246 my $old_provider = {
247 xml =>
248 $lastConf->{samlIDPMetaDataXML}->{$confKey}->{samlIDPMetaDataXML},
249 attributes =>
250 $lastConf->{samlIDPMetaDataExportedAttributes}->{$confKey},
251 118 options => $lastConf->{samlIDPMetaDataOptions}->{$confKey},
252 };
253  
254 # Set Metadata
255 $lastConf->{samlIDPMetaDataXML}->{$confKey}->{samlIDPMetaDataXML} =
256 118 $partner_metadata;
257  
258 # Set attributes
259 118 $lastConf->{samlIDPMetaDataExportedAttributes}->{$confKey} =
260 _compute_idp_exported_attributes( $config, $entityID );
261  
262 # Set options
263 118 $lastConf->{samlIDPMetaDataOptions}->{$confKey} =
264 _compute_idp_options( $config, $entityID );
265  
266 118 return _provider_equal( $lastConf, $old_provider, $confKey, "IDP" );
267 }
268  
269 118 ▶sub _compute_idp_exported_attributes {
Subroutine _compute_idp_exported_attributes: called CC=4 SCAR=13.9
270 118 my ( $config, $entityID ) = @_;
271 118 my $result;
272  
273 # Use default exported attributes from config or from hardcoded list
274 118 ▶ if ( $config->{exportedAttributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'exportedAttributes'}) { } 12 106 118
275 12 $result = { %{ $config->{exportedAttributes} } };
276 }
277 else {
278 106 $result = {%$exportedAttributes};
279 }
280  
281 # Override according to configuration
282 118 ▶ my $idp_config = $config->{"$entityID"} || {};
Condition100%2/2
$$config{"$entityID"} || {}
l!l
2116
MC/DC100%1/1
$$config{"$entityID"} || {}
$$config{"$entityID"}
Truth table100%2/2
A
A= $$config{"$entityID"}
Aresult
11
00
283 118 for my $key ( grep /^exported_attribute_/, keys(%$idp_config) ) {
284 1 my ($attribute_name) = $key =~ m/exported_attribute_(.*)$/;
285 1 $result->{$attribute_name} = $idp_config->{$key};
286 }
287 118 return $result;
288 }
289  
290 596 ▶sub _provider_equal {
Subroutine _provider_equal: called CC=3 SCAR=11.0
291 596 my ( $lastConf, $old_provider, $confKey, $type ) = @_;
292  
293 my $xml_equal =
294 $old_provider->{xml} eq $lastConf->{"saml${type}MetaDataXML"}->{$confKey}
295 596 ->{"saml${type}MetaDataXML"};
296 my $attributes_equal = _hash_equal( $old_provider->{attributes},
297 596 $lastConf->{"saml${type}MetaDataExportedAttributes"}->{$confKey} );
298 my $options_equal = _hash_equal( $old_provider->{options},
299 596 $lastConf->{"saml${type}MetaDataOptions"}->{$confKey} );
300  
301 596 ▶ return ( $xml_equal and $attributes_equal and $options_equal );
Condition83%5/6
$xml_equal && $attributes_equal && $options_equal
!ll&&!rl&&r
4762118
$xml_equal and $attributes_equal
!ll&&!rl&&r
4760120
MC/DC66%2/3
$xml_equal && $attributes_equal && $options_equal
$xml_equal $attributes_equal $options_equal
Truth table75%3/4
A and B && C
A= $xml_equalB= $attributes_equalC= $options_equal
ABCresult
0XX0
10X0
1100
1111
302 }
303  
304 1192 ▶sub _hash_equal {
Subroutine _hash_equal: called CC=8 SCAR=20.9
305 1192 my ( $hash1, $hash2 ) = @_;
306 1192 ▶ return 0 unless $hash1;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash1 952 240 1192
307 240 ▶ return 0 unless $hash2;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash2 0 240 240
308 240 ▶ return 0 unless ( %$hash1 == %$hash2 );
Branch: 1 branch
BranchTrueFalseTotal
unless %$hash1 == %$hash2 0 240 240
309 240 for my $key ( keys %$hash1 ) {
310 2140 ▶ return 0 unless ( $hash1->{$key} || '' ) eq ( $hash2->{$key} || '' );
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash1->{$key} || "") eq ($hash2->{$key} || "") 2 2138 2140
Condition100%4/4
$hash1->{$key} || ''
l!l
1701439
$hash2->{$key} || ''
l!l
1701439
MC/DC100%1/1
$hash1->{$key} || ''
$hash1->{$key}
MC/DC100%1/1
$hash2->{$key} || ''
$hash2->{$key}
Truth table100%2/2
A
A= $hash1->{$key}
Aresult
11
00
Truth table100%2/2
A
A= $hash2->{$key}
Aresult
11
00
311 }
312 238 return 1;
313 }
314  
315 10 ▶sub transform_config {
Subroutine transform_config: called CC=38 SCAR=36.7
316 10 my ( $config, $lastConf, $xml_metadata ) = @_;
317  
318 10 ▶ my $spConfKeyPrefix = $config->{main}->{spconfprefix} || "sp-";
Condition100%2/2
$config->{'main'}{'spconfprefix'} || 'sp-'
l!l
19
MC/DC100%1/1
$config->{'main'}{'spconfprefix'} || 'sp-'
$config->{'main'}{'spconfprefix'}
Truth table100%2/2
A
A= $config->{'main'}{'spconfprefix'}
Aresult
11
00
319 10 ▶ my $idpConfKeyPrefix = $config->{main}->{idpconfprefix} || "idp-";
Condition100%2/2
$config->{'main'}{'idpconfprefix'} || 'idp-'
l!l
19
MC/DC100%1/1
$config->{'main'}{'idpconfprefix'} || 'idp-'
$config->{'main'}{'idpconfprefix'}
Truth table100%2/2
A
A= $config->{'main'}{'idpconfprefix'}
Aresult
11
00
320  
321 # BlockList initialisation
322 10 ▶ my @spIgnorelist = @{ $config->{main}->{'ignore-sp'} || [] };
Condition100%2/2
$config->{'main'}{'ignore-sp'} || []
l!l
19
MC/DC100%1/1
$config->{'main'}{'ignore-sp'} || []
$config->{'main'}{'ignore-sp'}
Truth table100%2/2
A
A= $config->{'main'}{'ignore-sp'}
Aresult
11
00
323 10 ▶ my @idpIgnorelist = @{ $config->{main}->{'ignore-idp'} || [] };
Condition100%2/2
$config->{'main'}{'ignore-idp'} || []
l!l
19
MC/DC100%1/1
$config->{'main'}{'ignore-idp'} || []
$config->{'main'}{'ignore-idp'}
Truth table100%2/2
A
A= $config->{'main'}{'ignore-idp'}
Aresult
11
00
324  
325 # Stats initialization
326 10 my $idpCounter = {
327 'found' => 0,
328 'updated' => 0,
329 'created' => 0,
330 'rejected' => 0,
331 'removed' => 0,
332 'ignored' => 0
333 };
334 10 my $spCounter = {
335 'found' => 0,
336 'updated' => 0,
337 'created' => 0,
338 'rejected' => 0,
339 'removed' => 0,
340 'ignored' => 0,
341 };
342  
343 # IDP and SP lists
344 10 my ( $allIdpList, $allSpList, $mdIdpList, $mdSpList, $matchingIdpList,
345 $matchingSpList );
346  
347 # List current SAML partners
348 10 foreach my $spConfKey ( keys %{ $lastConf->{samlSPMetaDataXML} } ) {
349 my ( $tmp, $entityID ) =
350 ( $lastConf->{samlSPMetaDataXML}->{$spConfKey}->{samlSPMetaDataXML}
351 98 =~ /entityID=(['"])(.+?)\1/si );
352 98 $allSpList->{$entityID} = $spConfKey;
353 98 ▶ if ( $spConfKey =~ /^$spConfKeyPrefix/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spConfKey =~ /^$spConfKeyPrefix/) 97 1 98
354 97 $matchingSpList->{$entityID} = $spConfKey;
355 }
356 98 printlog("Existing SAML partner found: [SP] $entityID ($spConfKey)\n");
357 }
358  
359 10 foreach my $idpConfKey ( keys %{ $lastConf->{samlIDPMetaDataXML} } ) {
360 my ( $tmp, $entityID ) =
361 ( $lastConf->{samlIDPMetaDataXML}->{$idpConfKey}->{samlIDPMetaDataXML}
362 26 =~ /entityID=(['"])(.+?)\1/si );
363 26 $allIdpList->{$entityID} = $idpConfKey;
364 26 ▶ if ( $idpConfKey =~ /^$idpConfKeyPrefix/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idpConfKey =~ /^$idpConfKeyPrefix/) 25 1 26
365 25 $matchingIdpList->{$entityID} = $idpConfKey;
366 }
367 printlog(
368 26 "Existing SAML partner found: [IDP] $entityID ($idpConfKey)\n");
369 }
370  
371 10 my $dom = XML::LibXML->load_xml( string => $xml_metadata );
372  
373 # Browse all partners
374 10 foreach my $partner (
375 $dom->findnodes('/md:EntitiesDescriptor/md:EntityDescriptor') )
376 {
377 620 my $entityID = $partner->getAttribute('entityID');
378  
379 # Add required XML namespaces
380 620 $partner->setNamespace( "urn:oasis:names:tc:SAML:2.0:metadata",
381 "md", 0 );
382 620 $partner->setNamespace( "urn:oasis:names:tc:SAML:metadata:attribute",
383 "mdattr", 0 );
384 620 $partner->setNamespace( "urn:oasis:names:tc:SAML:2.0:assertion",
385 "saml", 0 );
386 620 $partner->setNamespace( "http://www.w3.org/2000/09/xmldsig#", "ds", 0 );
387  
388 # Parse subject-id:req extension
389 620 my $requested_subject_id = "none";
390 620 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjectid = $partner->findnodes("./md:Extensions/mdattr:EntityAttributes/saml:Attribute[\@Name=\"urn:oasis:names:tc:SAML:profiles:subject-id:req\"]/saml:AttributeValue[1]/text()")->shift) 10 610 620
391 my $subjectid = $partner->findnodes(
392 './md:Extensions'
393 . '/mdattr:EntityAttributes'
394 . '/saml:Attribute[@Name="urn:oasis:names:tc:SAML:profiles:subject-id:req"]'
395 . '/saml:AttributeValue[1]'
396 . '/text()'
397 )->shift()
398 )
399 {
400 10 $requested_subject_id = $subjectid->toString;
401 }
402  
403 # Remove other extensions
404 620 foreach ( $partner->findnodes('.//md:Extensions') ) { $_->unbindNode; }
405  
406 # Check IDP or SP
407 620 ▶ if ( my $idp = $partner->findnodes('./md:IDPSSODescriptor') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $idp = $partner->findnodes("./md:IDPSSODescriptor")) 130 490 620
408 130 $idpCounter->{found}++;
409 130 $mdIdpList->{$entityID} = 1;
410  
411 # Check if SAML 2.0 is supported
412 130 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($partner->findnodes('./md:IDPSSODescriptor/md:SingleSignOnService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]')) { } 120 10 130
413 $partner->findnodes(
414 './md:IDPSSODescriptor/md:SingleSignOnService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]'
415 )
416 )
417 {
418  
419 # Read metadata
420 120 my $partner_metadata = $partner->toString;
421 120 $partner_metadata =~ s/\n//g;
422 120 utf8::encode($partner_metadata);
423  
424 # test if IDP entityID is inside the block list
425  
426 120 ▶ if ( grep { $entityID eq $_ } @idpIgnorelist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$entityID eq $_;} @idpIgnorelist) { } 2 118 120
427 2 printlog("IDP $entityID won't be update/added \n");
428 2 $idpCounter->{ignored}++;
429 }
430 else {
431 # Check if entityID already in configuration
432 118 ▶ if ( defined $matchingIdpList->{$entityID} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $matchingIdpList->{$entityID}) { } 24 94 118
elsif (not defined $allIdpList->{$entityID}) { } 94 0 94
433  
434 24 my $confKey = $matchingIdpList->{$entityID};
435 24 my $equal =
436 register_saml_idp( $config, $lastConf, $confKey,
437 $entityID, $partner_metadata );
438  
439 24 ▶ if ($equal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($equal) { } 23 1 24
440 23 printlog("IDP $entityID has not changed\n");
441 }
442 else {
443 1 printlog("Update IDP $entityID in configuration\n");
444 1 $idpCounter->{updated}++;
445 }
446 }
447 elsif ( not defined $allIdpList->{$entityID} ) {
448  
449 # Create a new partner
450 94 my $confKey =
451 toEntityIDkey( $idpConfKeyPrefix, $entityID );
452  
453 94 register_saml_idp( $config, $lastConf, $confKey,
454 $entityID, $partner_metadata );
455  
456 94 printlog( "Declare new IDP $entityID"
457 . " (configuration key $confKey)\n" );
458 94 $idpCounter->{created}++;
459 }
460 else {
461 0 my $confKey = $allIdpList->{$entityID};
462 0 printlog( "Skipping existing IDP $entityID"
463 . " (configuration key $confKey)\n" );
464 0 $idpCounter->{ignored}++;
465 }
466 }
467  
468 }
469 else {
470 10 printlogerr(
471 "[WARN] IDP $entityID is not compatible with SAML 2.0,"
472 . " it will not be imported.\n" );
473 10 $idpCounter->{rejected}++;
474 }
475 }
476 620 ▶ if ( my $sp = $partner->findnodes('./md:SPSSODescriptor') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sp = $partner->findnodes("./md:SPSSODescriptor")) 490 130 620
477 490 $spCounter->{found}++;
478 490 $mdSpList->{$entityID} = 1;
479  
480 # Check if SAML 2.0 is supported
481 490 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($partner->findnodes('./md:SPSSODescriptor/md:AssertionConsumerService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]')) { } 480 10 490
482 $partner->findnodes(
483 './md:SPSSODescriptor/md:AssertionConsumerService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]'
484 )
485 )
486 {
487  
488 # Read requested attributes
489 480 my $requestedAttributes = {};
490 480 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($partner->findnodes('./md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute')) { } 430 50 480
491 $partner->findnodes(
492 './md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute'
493 )
494 )
495 {
496 430 foreach my $requestedAttribute (
497 $partner->findnodes(
498 './md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute'
499 )
500 )
501 {
502 2440 my $name = $requestedAttribute->getAttribute("Name");
503 2440 my $friendlyname =
504 $requestedAttribute->getAttribute("FriendlyName");
505 2440 my $nameformat =
506 $requestedAttribute->getAttribute("NameFormat");
507 2440 ▶ my $required =
Branch: 1 branch
BranchTrueFalseTotal
$requestedAttribute->getAttribute('isRequired') =~ /true/i ? : 1420 1020 2440
508 ( $requestedAttribute->getAttribute("isRequired") =~
509 /true/i ) ? 1 : 0;
510 2440 ▶ if ($friendlyname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($friendlyname) 2410 30 2440
511 2410 $requestedAttributes->{$friendlyname} = {
512 required => $required,
513 name => $name,
514 name_format => $nameformat,
515 friendly_name => $friendlyname
516 };
517 2410 printlog( "Attribute $friendlyname ($name)"
518 . " requested by SP $entityID\n" );
519 }
520 }
521 }
522 else {
523 50 $requestedAttributes = {
524 cn => { required => 1, name => 'cn' },
525 uid => { required => 1, name => 'uid' },
526 mail => { required => 1, name => 'mail' },
527 };
528 }
529 480 ▶ if ( $requested_subject_id eq "any"
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_subject_id eq "any" or $requested_subject_id eq "subject-id") 10 470 480
Condition66%2/3
$requested_subject_id eq "any" or $requested_subject_id eq "subject-id"
l!l&&r!l&&!r
100470
MC/DC50%1/2
$requested_subject_id eq "any" or $requested_subject_id eq "subject-id"
$requested_subject_id eq "any" $requested_subject_id eq "subject-id"
Truth table66%2/3
A or B
A= $requested_subject_id eq "any"B= $requested_subject_id eq "subject-id"
ABresult
1X1
011
000
530 or $requested_subject_id eq "subject-id" )
531 {
532 10 $requestedAttributes->{"subjectId"} = {
533 required => 0,
534 name => "urn:oasis:names:tc:SAML:attribute:subject-id",
535 name_format =>
536 "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
537 friendly_name => "subject-id",
538 };
539 }
540  
541 # Remove AttributeConsumingService node
542 480 foreach (
543 $partner->findnodes(
544 './md:SPSSODescriptor/md:AttributeConsumingService')
545 )
546 {
547 430 $_->unbindNode;
548 }
549  
550 # Read metadata
551 480 my $partner_metadata = $partner->toString;
552 480 $partner_metadata =~ s/\n//g;
553 480 utf8::encode($partner_metadata);
554  
555 # test if IDP entityID is inside the block list
556  
557 480 ▶ if ( grep { $entityID eq $_ } @spIgnorelist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$entityID eq $_;} @spIgnorelist) { } 2 478 480
558 2 printlog("SP $entityID won't be update/added \n");
559 2 $spCounter->{ignored}++;
560 }
561 else {
562 # Check if entityID already in configuration
563 478 my $confKey;
564 478 ▶ if ( defined $matchingSpList->{$entityID} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $matchingSpList->{$entityID}) { } 96 382 478
elsif (not defined $allSpList->{$entityID}) { } 382 0 382
565 96 $confKey = $matchingSpList->{$entityID};
566  
567 96 my $equal =
568 register_saml_sp( $config, $lastConf, $confKey,
569 $entityID,
570 $partner_metadata, $requestedAttributes );
571  
572 96 ▶ if ($equal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($equal) { } 95 1 96
573 95 printlog("SP $entityID has not changed\n");
574 }
575 else {
576 1 printlog("Update SP $entityID in configuration\n");
577 1 $spCounter->{updated}++;
578 }
579 }
580 elsif ( not defined $allSpList->{$entityID} ) {
581  
582 # Create a new partner
583 382 $confKey = toEntityIDkey( $spConfKeyPrefix, $entityID );
584  
585 382 register_saml_sp( $config, $lastConf, $confKey,
586 $entityID,
587 $partner_metadata, $requestedAttributes );
588  
589 382 printlog( "Declare new SP $entityID"
590 . " (configuration key $confKey)\n" );
591 382 $spCounter->{created}++;
592 }
593 else {
594 0 my $entityID = $allSpList->{$entityID};
595 0 printlog( "Skipping existing SP $entityID "
596 . "(configuration key $confKey)\n" );
597 0 $spCounter->{ignored}++;
598 }
599  
600 }
601  
602 }
603 else {
604 10 printlogerr(
605 "[WARN] SP $entityID is not compatible with SAML 2.0,"
606 . " it will not be imported.\n" );
607 10 $spCounter->{rejected}++;
608 }
609  
610 }
611  
612 }
613  
614 # Remove partners
615 10 ▶ if ( $config->{main}->{remove} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"main"}{"remove"}) 1 9 10
616 1 foreach my $entityID ( keys %$matchingIdpList ) {
617 1 my $idpConfKey = $matchingIdpList->{$entityID};
618 1 ▶ unless ( defined $mdIdpList->{$entityID} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mdIdpList->{$entityID}) 1 0 1
619 0 ▶ if ( grep { $entityID eq $_ } @idpIgnorelist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$entityID eq $_;} @idpIgnorelist) { } 0 1 1
620 0 $idpCounter->{ignored}++;
621 0 printlog("IDP $idpConfKey won't be deleted \n");
622 }
623 else {
624 1 delete $lastConf->{samlIDPMetaDataXML}->{$idpConfKey};
625 delete $lastConf->{samlIDPMetaDataExportedAttributes}
626 1 ->{$idpConfKey};
627 1 delete $lastConf->{samlIDPMetaDataOptions}->{$idpConfKey};
628 1 $idpCounter->{removed}++;
629 1 printlog("Remove IDP $idpConfKey\n");
630 }
631 }
632 }
633  
634 1 foreach my $entityID ( keys %$matchingSpList ) {
635 1 my $spConfKey = $matchingSpList->{$entityID};
636 1 ▶ unless ( defined $mdSpList->{$entityID} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mdSpList->{$entityID}) 1 0 1
637 0 ▶ if ( grep { $entityID eq $_ } @spIgnorelist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$entityID eq $_;} @spIgnorelist) { } 0 1 1
638 0 $spCounter->{ignored}++;
639 0 printlog("SP $spConfKey won't be deleted \n");
640 }
641 else {
642 1 delete $lastConf->{samlSPMetaDataXML}->{$spConfKey};
643 delete $lastConf->{samlSPMetaDataExportedAttributes}
644 1 ->{$spConfKey};
645 1 delete $lastConf->{samlSPMetaDataOptions}->{$spConfKey};
646 1 $spCounter->{removed}++;
647 1 printlog("Remove SP $spConfKey\n");
648 }
649 }
650 }
651 }
652 10 return ( $spCounter, $idpCounter );
653 }
654  
655 0 ▶sub main {
Subroutine main: not called CC=6 SCAR=37.4
656  
657 0 my $config = get_config( \@ARGV );
658  
659 # Get merged config
660 0 my %opts = %{ $config->{main} };
661 0 $verbose = $opts{verbose};
662  
663 pod2usage( -message => "Missing metadata URL (-m)", -exitval => 2 )
664 0 ▶ if !$opts{metadata};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"metadata"} 0 0 0
665  
666 0 my $conf = Lemonldap::NG::Common::Conf->new();
667 0 my $lastConf = $conf->getConf();
668  
669 0 printlog( "Read configuration " . $lastConf->{cfgNum} . "\n" );
670  
671 # Download metadata file
672 0 my $ua = LWP::UserAgent->new;
673 0 $ua->timeout(10);
674 0 $ua->env_proxy;
675  
676 0 my $metadata_file = $opts{metadata};
677  
678 0 printlog("Try to download metadata file at $metadata_file\n");
679 0 my $response = $ua->get($metadata_file);
680  
681 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
682 0 printlog("Metadata file found\n");
683 }
684 else {
685 0 die $response->status_line;
686 }
687  
688 0 my $xml_metadata = $response->decoded_content;
689  
690 0 my ( $spCounter, $idpCounter ) =
691 transform_config( $config, $lastConf, $xml_metadata );
692  
693 0 my $numConf = "DRY-RUN";
694 0 my $exitCode = 0;
695  
696 0 ▶ if ( !$opts{'dry-run'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'dry-run'}) { } 0 0 0
697  
698 # Register configuration
699 0 printlog("[INFO] run mod EntityID will be inserted\n");
700 0 $numConf = $conf->saveConf( $lastConf, ( cfgNumFixed => 1 ) );
701 0 printlog("[OK] Configuration $numConf saved\n");
702 0 ▶ unless ( $numConf > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($numConf > 0) 0 0 0
703 0 print "[ERROR] Unable to save configuration\n";
704 0 $exitCode = 1;
705 }
706 }
707 else {
708 0 printlog("[INFO] Dry-run mod no EntityID inserted\n");
709 }
710  
711 0 ▶ if ( $opts{nagios} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'nagios'}) { } 0 0 0
712 print "Metadata loaded inside Conf: ["
713 . $numConf
714 . "]|idp_found="
715 . $idpCounter->{found}
716 . ", idp_updated="
717 . $idpCounter->{updated}
718 . ", idp_created="
719 . $idpCounter->{created}
720 . ", idp_removed="
721 . $idpCounter->{removed}
722 . ", idp_rejected="
723 . $idpCounter->{rejected}
724 . ", idp_ignored="
725 . $idpCounter->{ignored}
726 . ", sp_found="
727 . $spCounter->{found}
728 . ", sp_updated="
729 . $spCounter->{updated}
730 . ", sp_created="
731 . $spCounter->{created}
732 . ", sp_removed="
733 . $spCounter->{removed}
734 . ", sp_rejected="
735 . $spCounter->{rejected}
736 . ", sp_ignored="
737 0 . $spCounter->{ignored} . "\n";
738 }
739 else {
740 print "[IDP]\tFound: "
741 . $idpCounter->{found}
742 . "\tUpdated: "
743 . $idpCounter->{updated}
744 . "\tCreated: "
745 . $idpCounter->{created}
746 . "\tRemoved: "
747 . $idpCounter->{removed}
748 . "\tRejected: "
749 . $idpCounter->{rejected}
750 . "\tIgnored: "
751 0 . $idpCounter->{ignored} . "\n";
752 print "[SP]\tFound: "
753 . $spCounter->{found}
754 . "\tUpdated: "
755 . $spCounter->{updated}
756 . "\tCreated: "
757 . $spCounter->{created}
758 . "\tRemoved: "
759 . $spCounter->{removed}
760 . "\tRejected: "
761 . $spCounter->{rejected}
762 . "\tIgnored: "
763 0 . $spCounter->{ignored} . "\n";
764 }
765  
766 0 exit $exitCode;
767 }
768  
769 # If run as a script
770 2 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 2 2
771 0 main();
772 }
773  
774 __END__
« blib/lib/Lemonldap/NG/Common/Util.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Notifications/XML.pm »