summaryrefslogtreecommitdiffstats
path: root/library/X509/ProvidedHook/ServicesImportSource.php
blob: 7b87cd863d8d071b856ea169d1c77ddcc6c20524 (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
<?php

// Icinga Web 2 X.509 Module | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\X509\ProvidedHook;

use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Job;
use Icinga\Module\X509\Model\X509CertificateSubjectAltName;
use Icinga\Module\X509\Model\X509Target;
use ipl\Sql;

class ServicesImportSource extends X509ImportSource
{
    public function fetchData()
    {
        $conn = Database::get();
        $targets = X509Target::on($conn)
            ->with([
                'chain',
                'chain.certificate',
                'chain.certificate.dn',
                'chain.certificate.issuer_certificate'
            ])
            ->columns([
                'ip',
                'host_name'        => 'hostname',
                'host_port'        => 'port',
                'cert_subject'     => 'chain.certificate.subject',
                'cert_issuer'      => 'chain.certificate.issuer',
                'cert_trusted'     => 'chain.certificate.trusted',
                'cert_valid_from'  => 'chain.certificate.valid_from',
                'cert_valid_to'    => 'chain.certificate.valid_to',
                'cert_self_signed' => new Sql\Expression('COALESCE(%s, %s)', [
                    'chain.certificate.issuer_certificate.self_signed',
                    'chain.certificate.self_signed'
                ])
            ]);

        $targets->getWith()['target.chain.certificate.issuer_certificate']->setJoinType('LEFT');
        $targets
            ->getSelectBase()
            ->where(new Sql\Expression('target_chain_link.order = 0'))
            ->groupBy(['ip, hostname, port']);

        $certAltName = X509CertificateSubjectAltName::on($conn);
        $certAltName
            ->getSelectBase()
            ->where(new Sql\Expression('certificate_id = target_chain_certificate.id'))
            ->groupBy(['alt_name.certificate_id']);

        if ($conn->getAdapter() instanceof Sql\Adapter\Pgsql) {
            $targets
                ->withColumns([
                    'cert_fingerprint' => new Sql\Expression("ENCODE(%s, 'hex')", [
                        'chain.certificate.fingerprint'
                    ]),
                    'cert_dn'          => new Sql\Expression(
                        "ARRAY_TO_STRING(ARRAY_AGG(CONCAT(%s, '=', %s)), ',')",
                        [
                            'chain.certificate.dn.key',
                            'chain.certificate.dn.value'
                        ]
                    )
                ])
                ->getSelectBase()
                ->groupBy(['target_chain_certificate.id', 'target_chain_certificate_issuer_certificate.id']);

            $certAltName->columns([
                new Sql\Expression("ARRAY_TO_STRING(ARRAY_AGG(CONCAT(%s, ':', %s)), ',')", ['type', 'value'])
            ]);
        } else {
            $targets->withColumns([
                'cert_fingerprint' => new Sql\Expression('HEX(%s)', ['chain.certificate.fingerprint']),
                'cert_dn'          => new Sql\Expression(
                    "GROUP_CONCAT(CONCAT(%s, '=', %s) SEPARATOR ',')",
                    [
                        'chain.certificate.dn.key',
                        'chain.certificate.dn.value'
                    ]
                )
            ]);

            $certAltName->columns([
                new Sql\Expression("GROUP_CONCAT(CONCAT(%s, ':', %s) SEPARATOR ',')", ['type', 'value'])
            ]);
        }

        list($select, $values) = $certAltName->dump();
        $targets->withColumns(['cert_subject_alt_name' => new Sql\Expression("$select", null, ...$values)]);

        $results = [];
        /** @var X509Target $target */
        foreach ($targets as $target) {
            $isV6 = Job::isIPV6($target->ip);
            $target->host_ip = $target->ip;
            $target->host_address = $isV6 ? null : $target->ip;
            $target->host_address6 = $isV6 ? $target->ip : null;

            $target->host_name_ip_and_port = sprintf(
                '%s/%s:%d',
                $target->host_name,
                $target->host_ip,
                $target->host_port
            );

            // Target ip is now obsolete and must not be included in the results.
            // The relation is only used to utilize the query and must not be in the result set as well.
            unset($target->ip);
            unset($target->chain);

            $results[$target->host_name_ip_and_port] = (object) iterator_to_array($target);
        }

        return $results;
    }

    public function listColumns()
    {
        return [
            'host_name_ip_and_port',
            'host_ip',
            'host_name',
            'host_port',
            'host_address',
            'host_address6',
            'cert_subject',
            'cert_issuer',
            'cert_self_signed',
            'cert_trusted',
            'cert_valid_from',
            'cert_valid_to',
            'cert_fingerprint',
            'cert_dn',
            'cert_subject_alt_name'
        ];
    }

    public static function getDefaultKeyColumnName()
    {
        return 'host_name_ip_and_port';
    }
}