summaryrefslogtreecommitdiffstats
path: root/application/controllers/DashboardController.php
blob: 8b43761ede65e71e7c42f3a44451ca268edf8e61 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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\Common\Database;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Donut;
use Icinga\Module\X509\Model\X509Certificate;
use Icinga\Web\Url;
use ipl\Html\Html;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;

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

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

        $byCa = X509Certificate::on($db)
            ->columns([
                'issuer_certificate.subject',
                'cnt' => new Expression('COUNT(*)')
            ])
            ->orderBy('cnt', SORT_DESC)
            ->orderBy('issuer_certificate.subject')
            ->filter(Filter::equal('issuer_certificate.ca', true))
            ->limit(5);

        $byCa
            ->getSelectBase()
            ->groupBy('certificate_issuer_certificate.id');

        $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->issuer_certificate->subject
                        ])->getAbsoluteUrl()
                    ],
                    $data->issuer_certificate->subject
                );
            });

        $duration = X509Certificate::on($db)
            ->columns([
                'duration',
                'cnt' => new Expression('COUNT(*)')
            ])
            ->filter(Filter::equal('ca', false))
            ->orderBy('cnt', SORT_DESC)
            ->limit(5);

        $duration
            ->getSelectBase()
            ->groupBy('duration');

        $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->getTimestamp()}&ca=n"
                        )->getAbsoluteUrl()
                    ],
                    CertificateUtils::duration($data->duration->getTimestamp())
                );
            });

        $keyStrength = X509Certificate::on($db)
            ->columns([
                'pubkey_algo',
                'pubkey_bits',
                'cnt' => new Expression('COUNT(*)')
            ])
            ->orderBy('cnt', SORT_DESC)
            ->limit(5);

        $keyStrength
            ->getSelectBase()
            ->groupBy(['pubkey_algo', 'pubkey_bits']);

        $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 = X509Certificate::on($db)
            ->columns([
                'signature_algo',
                'signature_hash_algo',
                'cnt' => new Expression('COUNT(*)')
            ])
            ->orderBy('cnt', SORT_DESC)
            ->limit(5);

        $sigAlgos
            ->getSelectBase()
            ->groupBy(['signature_algo', 'signature_hash_algo']);

        $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}"
                );
            });
    }
}