1
0

Version31000Date20240101084401.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Migrations;
  8. use Closure;
  9. use OCP\DB\ISchemaWrapper;
  10. use OCP\DB\Types;
  11. use OCP\Migration\Attributes\AddIndex;
  12. use OCP\Migration\Attributes\CreateTable;
  13. use OCP\Migration\Attributes\IndexType;
  14. use OCP\Migration\IOutput;
  15. use OCP\Migration\SimpleMigrationStep;
  16. /**
  17. * @since 31.0.0
  18. */
  19. #[CreateTable(
  20. table: 'sec_signatory',
  21. columns: ['id', 'key_id_sum', 'key_id', 'host', 'provider_id', 'account', 'public_key', 'metadata', 'type', 'status', 'creation', 'last_updated'],
  22. description: 'new table to store remove public/private key pairs'
  23. )]
  24. #[AddIndex(
  25. table: 'sec_signatory',
  26. type: IndexType::PRIMARY
  27. )]
  28. #[AddIndex(
  29. table: 'sec_signatory',
  30. type: IndexType::UNIQUE,
  31. description: 'confirm uniqueness per host, provider and account'
  32. )]
  33. #[AddIndex(
  34. table: 'sec_signatory',
  35. type: IndexType::INDEX,
  36. description: 'to search on key and provider'
  37. )]
  38. class Version31000Date20240101084401 extends SimpleMigrationStep {
  39. public function description(): string {
  40. return "creating new table 'sec_signatory' to store remote signatories";
  41. }
  42. public function name(): string {
  43. return 'create sec_signatory';
  44. }
  45. /**
  46. * @param IOutput $output
  47. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  48. * @param array $options
  49. * @return null|ISchemaWrapper
  50. */
  51. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  52. /** @var ISchemaWrapper $schema */
  53. $schema = $schemaClosure();
  54. if (!$schema->hasTable('sec_signatory')) {
  55. $table = $schema->createTable('sec_signatory');
  56. $table->addColumn('id', Types::BIGINT, [
  57. 'notnull' => true,
  58. 'length' => 64,
  59. 'autoincrement' => true,
  60. 'unsigned' => true,
  61. ]);
  62. // key_id_sum will store a hash version of the key_id, more appropriate for search/index
  63. $table->addColumn('key_id_sum', Types::STRING, [
  64. 'notnull' => true,
  65. 'length' => 127,
  66. ]);
  67. $table->addColumn('key_id', Types::STRING, [
  68. 'notnull' => true,
  69. 'length' => 512
  70. ]);
  71. // host/provider_id/account will help generate a unique entry, not based on key_id
  72. // this way, a spoofed instance cannot publish a new key_id for same host+provider_id
  73. // account will be used only to stored multiple keys for the same provider_id/host
  74. $table->addColumn('host', Types::STRING, [
  75. 'notnull' => true,
  76. 'length' => 512
  77. ]);
  78. $table->addColumn('provider_id', Types::STRING, [
  79. 'notnull' => true,
  80. 'length' => 31,
  81. ]);
  82. $table->addColumn('account', Types::STRING, [
  83. 'notnull' => false,
  84. 'length' => 127,
  85. 'default' => ''
  86. ]);
  87. $table->addColumn('public_key', Types::TEXT, [
  88. 'notnull' => true,
  89. 'default' => ''
  90. ]);
  91. $table->addColumn('metadata', Types::TEXT, [
  92. 'notnull' => true,
  93. 'default' => '[]'
  94. ]);
  95. // type+status are informative about the trustability of remote instance and status of the signatory
  96. $table->addColumn('type', Types::SMALLINT, [
  97. 'notnull' => true,
  98. 'length' => 2,
  99. 'default' => 9
  100. ]);
  101. $table->addColumn('status', Types::SMALLINT, [
  102. 'notnull' => true,
  103. 'length' => 2,
  104. 'default' => 0,
  105. ]);
  106. $table->addColumn('creation', Types::INTEGER, [
  107. 'notnull' => false,
  108. 'length' => 4,
  109. 'default' => 0,
  110. 'unsigned' => true,
  111. ]);
  112. $table->addColumn('last_updated', Types::INTEGER, [
  113. 'notnull' => false,
  114. 'length' => 4,
  115. 'default' => 0,
  116. 'unsigned' => true,
  117. ]);
  118. $table->setPrimaryKey(['id'], 'sec_sig_id');
  119. $table->addUniqueIndex(['provider_id', 'host', 'account'], 'sec_sig_unic');
  120. $table->addIndex(['key_id_sum', 'provider_id'], 'sec_sig_key');
  121. return $schema;
  122. }
  123. return null;
  124. }
  125. }