summaryrefslogtreecommitdiffstats
path: root/application/controllers/DashboardController.php
blob: a48bb98ebea4cc4686fe7b73e1c7ddedbf6e7f19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
// Icinga Web 2 X.509 Module | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\X509\Controllers;

use Icinga\Exception\ConfigurationError;
use Icinga\Module\X509\CertificateUtils;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Donut;
use Icinga\Web\Url;
use ipl\Html\Html;
use ipl\Sql\Select;

class DashboardController extends Controller
{
    public function indexAction()
    {
        $this->setTitle($this->translate('Certificate Dashboard'));

        try {
            $db = $this->getDb();
        } catch (ConfigurationError $_) {
            $this->render('missing-resource', null, true);
            return;
        }

        $byCa = $db->select(
            (new Select())
                ->from('x509_certificate i')
                ->columns(['i.subject', 'cnt' => 'COUNT(*)'])
                ->join('x509_certificate c', ['c.issuer_hash = i.subject_hash', 'i.ca = ?' => 'yes'])
                ->groupBy(['i.id'])
                ->orderBy('cnt', SORT_DESC)
                ->limit(5)
        );

        $this->view->byCa = (new Donut())
            ->setHeading($this->translate('Certificates by CA'), 2)
            ->setData($byCa)
            ->setLabelCallback(function ($data) {
                return Html::tag(
                    'a',
                    [
                        'href' => Url::fromPath('x509/certificates', ['issuer' => $data['subject']])->getAbsoluteUrl()
                    ],
                    $data['subject']
                );
            });

        $duration = $db->select(
            (new Select())
                ->from('x509_certificate')
                ->columns([
                    'duration' => 'valid_to - valid_from',
                    'cnt' => 'COUNT(*)'
                ])
                ->where(['ca = ?' => 'no'])
                ->groupBy(['duration'])
                ->orderBy('cnt', SORT_DESC)
                ->limit(5)
        );

        $this->view->duration = (new Donut())
            ->setHeading($this->translate('Certificates by Duration'), 2)
            ->setData($duration)
            ->setLabelCallback(function ($data) {
                return Html::tag(
                    'a',
                    [
                        'href' => Url::fromPath(
                            "x509/certificates?duration={$data['duration']}&ca=no"
                        )->getAbsoluteUrl()
                    ],
                    CertificateUtils::duration($data['duration'])
                );
            });

        $keyStrength = $db->select(
            (new Select())
                ->from('x509_certificate')
                ->columns(['pubkey_algo', 'pubkey_bits', 'cnt' => 'COUNT(*)'])
                ->groupBy(['pubkey_algo', 'pubkey_bits'])
                ->orderBy('cnt', SORT_DESC)
                ->limit(5)
        );

        $this->view->keyStrength = (new Donut())
            ->setHeading($this->translate('Key Strength'), 2)
            ->setData($keyStrength)
            ->setLabelCallback(function ($data) {
                return Html::tag(
                    'a',
                    [
                        'href' => Url::fromPath(
                            'x509/certificates',
                            [
                                'pubkey_algo' => $data['pubkey_algo'],
                                'pubkey_bits' => $data['pubkey_bits']
                            ]
                        )->getAbsoluteUrl()
                    ],
                    "{$data['pubkey_algo']} {$data['pubkey_bits']} bits"
                );
            });

        $sigAlgos = $db->select(
            (new Select())
                ->from('x509_certificate')
                ->columns(['signature_algo', 'signature_hash_algo', 'cnt' => 'COUNT(*)'])
                ->groupBy(['signature_algo', 'signature_hash_algo'])
                ->orderBy('cnt', SORT_DESC)
                ->limit(5)
        );

        $this->view->sigAlgos = (new Donut())
            ->setHeading($this->translate('Signature Algorithms'), 2)
            ->setData($sigAlgos)
            ->setLabelCallback(function ($data) {
                return Html::tag(
                    'a',
                    [
                        'href' => Url::fromPath(
                            'x509/certificates',
                            [
                                'signature_hash_algo' => $data['signature_hash_algo'],
                                'signature_algo' => $data['signature_algo']
                            ]
                        )->getAbsoluteUrl()
                    ],
                    "{$data['signature_hash_algo']} with {$data['signature_algo']}"
                );
            });
    }
}