ListCertificates.php 3.0 KB

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