Skip to content
Snippets Groups Projects
Commit 5a9192e4 authored by Guillaume ROUSSE's avatar Guillaume ROUSSE
Browse files

use exceptions, and let caller handle logging

parent 10a9399b
Branches
Tags
No related merge requests found
...@@ -7,6 +7,7 @@ use lib qw(lib); ...@@ -7,6 +7,7 @@ use lib qw(lib);
use feature "switch"; use feature "switch";
no warnings 'experimental::smartmatch'; no warnings 'experimental::smartmatch';
use English qw(-no_match_vars);
use Getopt::Long qw(:config auto_help); use Getopt::Long qw(:config auto_help);
use Pod::Usage; use Pod::Usage;
...@@ -150,19 +151,23 @@ sub list_test_accounts { ...@@ -150,19 +151,23 @@ sub list_test_accounts {
sub parse_federation_metadata { sub parse_federation_metadata {
my $federation_metadata = IdPAccountManager::SAMLMetadata->new(); my $federation_metadata = IdPAccountManager::SAMLMetadata->new();
die "unable to load federation metadata\n" eval {
unless $federation_metadata->load( $federation_metadata->load(
federation_metadata_file_path => federation_metadata_file_path =>
$configuration->{federation_metadata_file_path} $configuration->{federation_metadata_file_path}
); );
};
die "unable to load federation metadata: $EVAL_ERROR" if $EVAL_ERROR;
my %args; my %args;
if ($options{sp_entityid}) { if ($options{sp_entityid}) {
$args{filter_entity_id} = $options{sp_entityid}; $args{filter_entity_id} = $options{sp_entityid};
} }
die "unable to parse federation metadata\n" eval {
unless $federation_metadata->parse(%args); $federation_metadata->parse(%args);
};
die "unable to parse federation metadata: $EVAL_ERROR\n" if $EVAL_ERROR;
printf "Document %s parsed\n", printf "Document %s parsed\n",
$configuration->{federation_metadata_file_path}; $configuration->{federation_metadata_file_path};
......
...@@ -7,12 +7,11 @@ use English qw(-no_match_vars); ...@@ -7,12 +7,11 @@ use English qw(-no_match_vars);
use XML::LibXML; use XML::LibXML;
use IdPAccountManager::Tools; use IdPAccountManager::Tools;
use IdPAccountManager::Logger;
sub new { sub new {
my ($pkg, %args) = @_; my ($pkg, %args) = @_;
my $self = { logger => $args{logger} }; my $self = {};
bless $self, $pkg; bless $self, $pkg;
...@@ -23,47 +22,23 @@ sub new { ...@@ -23,47 +22,23 @@ sub new {
sub load { sub load {
my ($self, %args) = @_; my ($self, %args) = @_;
unless ($args{federation_metadata_file_path}) { die "Missing parameter 'federation_metadata_file_path'"
$self->{logger}->log( unless $args{federation_metadata_file_path};
level => LOG_ERROR,
message => "Missing parameter 'federation_metadata_file_path'"
);
return undef;
}
$self->{federation_metadata_file_path} = $self->{federation_metadata_file_path} =
$args{federation_metadata_file_path}; $args{federation_metadata_file_path};
unless (-r $self->{federation_metadata_file_path}) { die "Failed to read $args{federation_metadata_file_path}: $ERRNO"
$self->{logger}->log( unless -r $self->{federation_metadata_file_path};
level => LOG_ERROR,
message =>
"Failed to read $args{federation_metadata_file_path} : $ERRNO"
);
return undef;
}
unless ($self->{federation_metadata_as_xml} = die "Failed to parse file $args{metadata_file}: $ERRNO"
$self->_get_xml_object($args{federation_metadata_file_path})) unless $self->{federation_metadata_as_xml} =
{ $self->_get_xml_object($args{federation_metadata_file_path});
$self->{logger}->log(
level => LOG_ERROR,
message => "Failed to parse file $args{metadata_file} : $ERRNO"
);
return undef;
}
my $root = $self->{federation_metadata_as_xml}->documentElement(); my $root = $self->{federation_metadata_as_xml}->documentElement();
unless ($root->nodeName() =~ /EntitiesDescriptor$/) {
$self->{logger}->( die "Root element of file $args{federation_metadata_file_path} is of type '%s'; should be 'EntitiesDescriptor'", $root->nodeName()
level => LOG_ERROR, unless $root->nodeName() =~ /EntitiesDescriptor$/;
message => sprintf(
"Root element of file $args{federation_metadata_file_path} is of type '%s'; should be 'EntitiesDescriptor'",
$root->nodeName()
)
);
return undef;
}
return 1; return 1;
} }
...@@ -83,13 +58,9 @@ sub parse { ...@@ -83,13 +58,9 @@ sub parse {
$self->{federation_metadata_as_hashref} = $self->{federation_metadata_as_hashref} =
$self->_parse_saml_metadata(%parser_args); $self->_parse_saml_metadata(%parser_args);
unless (defined $self->{federation_metadata_as_hashref}) {
$self->{logger}->log( die "Failed to parse federation metadata"
level => LOG_ERROR, unless defined $self->{federation_metadata_as_hashref};
message => "Failed to parse federation metadata"
);
return undef;
}
return 1; return 1;
} }
...@@ -107,51 +78,25 @@ sub print { ...@@ -107,51 +78,25 @@ sub print {
sub _get_xml_object { sub _get_xml_object {
my ($self, $metadata_file) = @_; my ($self, $metadata_file) = @_;
unless (-f $metadata_file) { die "File $metadata_file not found: $ERRNO"
$self->{logger}->log( unless -f $metadata_file;
level => LOG_ERROR,
message => "File $metadata_file not found: $ERRNO"
);
return undef;
}
unless (open FH, $metadata_file) { die "Failed to open file $metadata_file: $ERRNO"
$self->{logger}->log( unless open FH, $metadata_file;
level => LOG_ERROR,
message => "Failed to open file $metadata_file: $ERRNO"
);
return undef;
}
my $parser; my $parser = XML::LibXML->new();
unless ($parser = XML::LibXML->new()) { die "Failed to initialize XML parser" unless $parser;
$self->{logger}->log(
level => LOG_ERROR,
message => "Failed to initialize XML parser"
);
return undef;
}
$parser->line_numbers(1); $parser->line_numbers(1);
my $doc; my $doc;
## Eval() prevents the parsing from killing the main process ## Eval() prevents the parsing from killing the main process
eval { $doc = $parser->parse_fh(\*FH) }; eval { $doc = $parser->parse_fh(\*FH) };
if ($EVAL_ERROR) { die "Failed to parse file $metadata_file : $EVAL_ERROR"
$self->{logger}->log( if $EVAL_ERROR;
level => LOG_ERROR,
message => "Failed to parse file $metadata_file : $EVAL_ERROR"
);
return undef;
}
unless ($doc) { die "Failed to parse file $metadata_file : $EVAL_ERROR"
$self->{logger}->log( unless $doc;
level => LOG_ERROR,
message => "Failed to parse file $metadata_file : $ERRNO"
);
return undef;
}
return $doc; return $doc;
} }
......
...@@ -231,30 +231,31 @@ sub req_account_wizard { ...@@ -231,30 +231,31 @@ sub req_account_wizard {
my ($self) = @_; my ($self) = @_;
$self->{logger}->log(level => LOG_INFO, message => ""); $self->{logger}->log(level => LOG_INFO, message => "");
my $federation_metadata = IdPAccountManager::SAMLMetadata->new( my $federation_metadata = IdPAccountManager::SAMLMetadata->new();
logger => $self->{logger}
);
unless ( eval {
$federation_metadata->load( $federation_metadata->load(
federation_metadata_file_path => federation_metadata_file_path =>
$self->{configuration}->{federation_metadata_file_path} $self->{configuration}->{federation_metadata_file_path}
) );
) };
{ if ($EVAL_ERROR) {
push @{ $self->{param_out}->{errors} }, "internal"; push @{ $self->{param_out}->{errors} }, "internal";
$self->{logger}->log( $self->{logger}->log(
level => LOG_ERROR, level => LOG_ERROR,
message => "Failed to load federation metadata : $ERRNO" message => "Failed to load federation metadata: $EVAL_ERROR"
); );
return undef; return undef;
} }
unless ($federation_metadata->parse()) { eval {
$federation_metadata->parse());
};
if ($EVAL_ERROR) {
push @{ $self->{param_out}->{errors} }, "internal"; push @{ $self->{param_out}->{errors} }, "internal";
$self->{logger}->log( $self->{logger}->log(
level => LOG_ERROR, level => LOG_ERROR,
message => "Failed to parse federation metadata : $ERRNO" message => "Failed to parse federation metadata: $EVAL_ERROR"
); );
return undef; return undef;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment