Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Cfssl
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 2
182
0.00% covered (danger)
0.00%
0 / 1
 configure
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Command\Configure;
10
11use InvalidArgumentException;
12use OCA\Libresign\Command\Base;
13use Symfony\Component\Console\Input\InputInterface;
14use Symfony\Component\Console\Input\InputOption;
15use Symfony\Component\Console\Output\OutputInterface;
16
17class Cfssl extends Base {
18    #[\Override]
19    protected function configure(): void {
20        $this
21            ->setName('libresign:configure:cfssl')
22            ->setDescription('Configure Cfssl')
23            ->addOption(
24                name: 'cn',
25                shortcut: null,
26                mode: InputOption::VALUE_REQUIRED,
27                description: 'Comon name'
28            )
29            ->addOption(
30                name: 'ou',
31                shortcut: null,
32                mode: InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
33                description: 'Organization unit (can be specified multiple times)'
34            )
35            ->addOption(
36                name: 'o',
37                shortcut: 'o',
38                mode: InputOption::VALUE_REQUIRED,
39                description: 'Organization'
40            )
41            ->addOption(
42                name: 'c',
43                shortcut: 'c',
44                mode: InputOption::VALUE_REQUIRED,
45                description: 'Country name'
46            )
47            ->addOption(
48                name: 'st',
49                shortcut: 's',
50                mode: InputOption::VALUE_REQUIRED,
51                description: 'State'
52            )
53            ->addOption(
54                name: 'l',
55                shortcut: 'l',
56                mode: InputOption::VALUE_REQUIRED,
57                description: 'Locality'
58            )
59            ->addOption(
60                name: 'config-path',
61                shortcut: null,
62                mode: InputOption::VALUE_REQUIRED,
63                description: 'Config path'
64            )
65            ->addOption(
66                name: 'cfssl-uri',
67                shortcut: null,
68                mode: InputOption::VALUE_REQUIRED,
69                description: 'CFSSL URI'
70            );
71    }
72
73    #[\Override]
74    protected function execute(InputInterface $input, OutputInterface $output): int {
75        if (!$this->installService->isCfsslBinInstalled()) {
76            throw new InvalidArgumentException('CFSSL binary not found! run libresign:istall --cfssl first.');
77        }
78        $names = [];
79        if (!$commonName = $input->getOption('cn')) {
80            throw new InvalidArgumentException('Invalid Comon Name');
81        }
82        if ($input->getOption('ou')) {
83            $names['OU'] = ['value' => $input->getOption('ou')];
84        }
85        if ($input->getOption('o')) {
86            $names['O'] = ['value' => $input->getOption('o')];
87        }
88        if ($input->getOption('c')) {
89            $names['C'] = ['value' => $input->getOption('c')];
90        }
91        if ($input->getOption('l')) {
92            $names['L'] = ['value' => $input->getOption('l')];
93        }
94        if ($input->getOption('st')) {
95            $names['ST'] = ['value' => $input->getOption('st')];
96        }
97
98        if (PHP_OS_FAMILY === 'Windows') {
99            throw new InvalidArgumentException('Incompatible with Windows');
100        }
101        if ($cfsslUri = $input->getOption('cfssl-uri')) {
102            if (!filter_var($cfsslUri, FILTER_VALIDATE_URL)) {
103                throw new InvalidArgumentException('Invalid CFSSL API URI');
104            }
105            if ($input->getOption('config-path')) {
106                throw new InvalidArgumentException('Config path is not necessary');
107            }
108        }
109        $configPath = $input->getOption('config-path');
110
111        $this->installService->generate(
112            (string)$commonName,
113            'cfssl',
114            $names,
115            [
116                'configPath' => $configPath,
117                'cfsslUri' => $cfsslUri,
118            ]
119        );
120        return 0;
121    }
122}