ListCertificates.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command\Security;
  24. use OC\Core\Command\Base;
  25. use OCP\ICertificate;
  26. use OCP\ICertificateManager;
  27. use OCP\IL10N;
  28. use Symfony\Component\Console\Helper\Table;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class ListCertificates extends Base {
  32. public function __construct(
  33. protected ICertificateManager $certificateManager,
  34. protected IL10N $l,
  35. ) {
  36. parent::__construct();
  37. }
  38. protected function configure() {
  39. $this
  40. ->setName('security:certificates')
  41. ->setDescription('list trusted certificates');
  42. parent::configure();
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output): int {
  45. $outputType = $input->getOption('output');
  46. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  47. $certificates = array_map(function (ICertificate $certificate) {
  48. return [
  49. 'name' => $certificate->getName(),
  50. 'common_name' => $certificate->getCommonName(),
  51. 'organization' => $certificate->getOrganization(),
  52. 'expire' => $certificate->getExpireDate()->format(\DateTimeInterface::ATOM),
  53. 'issuer' => $certificate->getIssuerName(),
  54. 'issuer_organization' => $certificate->getIssuerOrganization(),
  55. 'issue_date' => $certificate->getIssueDate()->format(\DateTimeInterface::ATOM)
  56. ];
  57. }, $this->certificateManager->listCertificates());
  58. if ($outputType === self::OUTPUT_FORMAT_JSON) {
  59. $output->writeln(json_encode(array_values($certificates)));
  60. } else {
  61. $output->writeln(json_encode(array_values($certificates), JSON_PRETTY_PRINT));
  62. }
  63. } else {
  64. $table = new Table($output);
  65. $table->setHeaders([
  66. 'File Name',
  67. 'Common Name',
  68. 'Organization',
  69. 'Valid Until',
  70. 'Issued By'
  71. ]);
  72. $rows = array_map(function (ICertificate $certificate) {
  73. return [
  74. $certificate->getName(),
  75. $certificate->getCommonName(),
  76. $certificate->getOrganization(),
  77. $this->l->l('date', $certificate->getExpireDate()),
  78. $certificate->getIssuerName()
  79. ];
  80. }, $this->certificateManager->listCertificates());
  81. $table->setRows($rows);
  82. $table->render();
  83. }
  84. return 0;
  85. }
  86. }