From 8018cbe20e4649141d5842cc2d44e04ad96fb527 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Mon, 5 Mar 2018 11:53:17 +0100
Subject: [PATCH] perform sanity check later, so as to gracefully fails if
 needed

---
 bin/access-check-manager.cgi.in | 10 ------
 lib/AccountManager/App.pm       | 58 ++++++++++++++++++++++++++++-----
 2 files changed, 50 insertions(+), 18 deletions(-)

diff --git a/bin/access-check-manager.cgi.in b/bin/access-check-manager.cgi.in
index 577d25c..75c0d21 100755
--- a/bin/access-check-manager.cgi.in
+++ b/bin/access-check-manager.cgi.in
@@ -13,16 +13,6 @@ my $configuration_file =
     $ENV{ACCOUNTMANAGER_CONFIG} || '@sysconfdir@/manager.conf';
 my $configuration = Config::Tiny->read($configuration_file);
 
-# configuration sanity check
-die "no database defined in configuration, aborting\n"
-    unless $configuration->{database};
-die "no mailer defined in configuration, aborting\n"
-    unless $configuration->{mailer};
-die "no idp defined in configuration, aborting\n"
-    unless $configuration->{idp};
-warn "no logger in configuration, logging disabled\n"
-    unless $configuration->{logger};
-
 my $app = AccountManager::App->new(
     configuration => $configuration
 );
diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index 1a8bbc4..fa66bf8 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -54,20 +54,62 @@ sub new {
             $self->{configuration}->{logger}->{file},
             log_level => $self->{configuration}->{logger}->{level}
         );
+    } else {
+        warn "no logger in configuration, logging disabled\n";
     }
 
     $self->{logger} = Log::Any->get_logger();
 
-    AccountManager::DB->register_db(
-        driver   => $self->{configuration}->{database}->{type},
-        database => $self->{configuration}->{database}->{name},
-        host     => $self->{configuration}->{database}->{host},
-        username => $self->{configuration}->{database}->{username},
-        password => $self->{configuration}->{database}->{password},
-    );
+    $self->{cgi} = CGI->new();
+
+    if (!$self->{configuration}->{mailer}) {
+        $self->{logger}->fatal(
+            "No mailer defined in configuration, aborting"
+        );
+        $self->respond(
+            template => 'index.tt2.html',
+            data     => {
+                content => 'errors.tt2.html',
+                errors  => [ 'internal' ]
+            }
+        );
+    }
+
+    if (!$self->{configuration}->{idp}) {
+        $self->{logger}->fatal(
+            "No IDP defined in configuration, aborting"
+        );
+        $self->respond(
+            template => 'index.tt2.html',
+            data     => {
+                content => 'errors.tt2.html',
+                errors  => [ 'internal' ]
+            }
+        );
+    }
+
+    if ($self->{configuration}->{logger}) {
+        AccountManager::DB->register_db(
+            driver   => $self->{configuration}->{database}->{type},
+            database => $self->{configuration}->{database}->{name},
+            host     => $self->{configuration}->{database}->{host},
+            username => $self->{configuration}->{database}->{username},
+            password => $self->{configuration}->{database}->{password},
+        );
+    } else {
+        $self->{logger}->fatal(
+            "No database defined in configuration, aborting"
+        );
+        $self->respond(
+            template => 'index.tt2.html',
+            data     => {
+                content => 'errors.tt2.html',
+                errors => [ 'internal' ]
+            }
+        );
+    }
 
     $self->{db} = AccountManager::DB->new();
-    $self->{cgi} = CGI->new();
 
     bless $self, $pkg;
 
-- 
GitLab