forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge torrust#983: Overhaul tracker keys: allow permanent keys
c5beff5 feat: [torrust#979] permanent keys (Jose Celano) 8d3fe72 chore(deps): [torrust#979] add new cargo dep: serde_with (Jose Celano) Pull request description: This PR introduces a new feature. Tracker keys can be permanent. ### How to use the endpoint #### Upload a pre-generated and expiring key ``` curl -X POST http://localhost:1212/api/v1/keys?token=MyAccessToken \ -H "Content-Type: application/json" \ -d '{ "key": "Xc1L4PbQJSFGlrgSRZl8wxSFAuMa2110", "seconds_valid": 7200 }' ``` #### Upload a pre-generated and permanent key ``` curl -X POST http://localhost:1212/api/v1/keys?token=MyAccessToken \ -H "Content-Type: application/json" \ -d '{ "key": "Xc1L4PbQJSFGlrgSRZl8wxSFAuMa2110", "seconds_valid": null }' ``` #### Generate a random and expiring key You can omit the key, and the tracker will generate a random one. ``` curl -X POST http://localhost:1212/api/v1/keys?token=MyAccessToken \ -H "Content-Type: application/json" \ -d '{ "key": null, "seconds_valid": 7200 }' ``` #### Generate a random and permanent key ``` curl -X POST http://localhost:1212/api/v1/keys?token=MyAccessToken \ -H "Content-Type: application/json" \ -d '{ "key": null, "seconds_valid": null }' ``` ### NOTES - Fields in the json object are mandatory. You cannot omit them. Instead of omitting the field, I've decided to use the `null` value to represent the case where the value is not needed. I prefer to use always the same structure and make it explicit. However, omitting the field could have other advantages, like requiring less maintenance. For example, if we rename a field (breaking change), that would not affect users who are not using that field. - I've introduced manual database migrations. The `second_valid` column in the `keys` table is now nullable (for permanent keys). ACKs for top commit: josecelano: ACK c5beff5 Tree-SHA512: 3680165af0f6269eab88144aa116c7aa1d3d73d5d1322f3e18ff25e7dff10de7bc6bc3cb34bcf7b36df6f9fb8d50c6cccad4bb1c10e2cecc5abdc6a102d20a8b
- Loading branch information
Showing
22 changed files
with
457 additions
and
167 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Database Migrations | ||
|
||
We don't support automatic migrations yet. The tracker creates all the needed tables when it starts. The SQL sentences are hardcoded in each database driver. | ||
|
||
The migrations in this folder were introduced to add some new changes (permanent keys) and to allow users to migrate to the new version. In the future, we will remove the hardcoded SQL and start using a Rust crate for database migrations. For the time being, if you are using the initial schema described in the migration `20240730183000_torrust_tracker_create_all_tables.sql` you will need to run all the subsequent migrations manually. |
21 changes: 21 additions & 0 deletions
21
migrations/mysql/20240730183000_torrust_tracker_create_all_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CREATE TABLE | ||
IF NOT EXISTS whitelist ( | ||
id integer PRIMARY KEY AUTO_INCREMENT, | ||
info_hash VARCHAR(40) NOT NULL UNIQUE | ||
); | ||
|
||
CREATE TABLE | ||
IF NOT EXISTS torrents ( | ||
id integer PRIMARY KEY AUTO_INCREMENT, | ||
info_hash VARCHAR(40) NOT NULL UNIQUE, | ||
completed INTEGER DEFAULT 0 NOT NULL | ||
); | ||
|
||
CREATE TABLE | ||
IF NOT EXISTS `keys` ( | ||
`id` INT NOT NULL AUTO_INCREMENT, | ||
`key` VARCHAR(32) NOT NULL, | ||
`valid_until` INT (10) NOT NULL, | ||
PRIMARY KEY (`id`), | ||
UNIQUE (`key`) | ||
); |
1 change: 1 addition & 0 deletions
1
migrations/mysql/20240730183500_torrust_tracker_keys_valid_until_nullable.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE `keys` CHANGE `valid_until` `valid_until` INT (10); |
19 changes: 19 additions & 0 deletions
19
migrations/sqlite/20240730183000_torrust_tracker_create_all_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
CREATE TABLE | ||
IF NOT EXISTS whitelist ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
info_hash TEXT NOT NULL UNIQUE | ||
); | ||
|
||
CREATE TABLE | ||
IF NOT EXISTS torrents ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
info_hash TEXT NOT NULL UNIQUE, | ||
completed INTEGER DEFAULT 0 NOT NULL | ||
); | ||
|
||
CREATE TABLE | ||
IF NOT EXISTS keys ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
key TEXT NOT NULL UNIQUE, | ||
valid_until INTEGER NOT NULL | ||
); |
12 changes: 12 additions & 0 deletions
12
migrations/sqlite/20240730183500_torrust_tracker_keys_valid_until_nullable.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE | ||
IF NOT EXISTS keys_new ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
key TEXT NOT NULL UNIQUE, | ||
valid_until INTEGER | ||
); | ||
|
||
INSERT INTO keys_new SELECT * FROM `keys`; | ||
|
||
DROP TABLE `keys`; | ||
|
||
ALTER TABLE keys_new RENAME TO `keys`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.