Repository navigation
Clean up after the inbound registration revert #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ccabaf7
a0c2ae1
8359050
1902951
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,3 +61,21 @@ php backfill-guid.php --seconds=15 | |
| ``` | ||
|
|
||
| `--table` is one of `lti_user`, `lti_context`, `lti_key`, or `lti_link`. Omit it to process all four. Each batch is its own transaction and holds locks on the updated rows until it commits. A later run skips rows that already have a guid. | ||
|
|
||
| ## When to run `undo-registration-migration.php` | ||
|
|
||
| A short-lived inbound registration added `lti_registration` and five columns on `lti_key`: `lti_registration_id`, `deployment_status`, `deployment_approved_at`, `platform_guid`, and `dynamic_registration_unlocked_at`. `admin/upgrade.php` does not create them and does not drop them. This script drops the foreign key, then those columns, then the table. | ||
|
|
||
| Run it once, from a checkout that no longer contains that registration code. A later `php admin/upgrade.php` on that checkout will not put the table back. Each statement auto-commits. A second run finds nothing left and exits. | ||
|
|
||
| The script stops if `lti_registration` still has rows, if a key still points at a registration, or if one of those columns holds a value. Copy the issuer, client id, and URLs back onto `lti_key` and delete the registration rows first. The script does not copy them. | ||
|
|
||
| From the site's Tsugi checkout: | ||
|
|
||
| ``` | ||
| cd admin/lti-maint | ||
| php undo-registration-migration.php | ||
| php undo-registration-migration.php apply | ||
| ``` | ||
|
|
||
| The first command is a dry run. `apply` prints a warning and waits ten seconds, then drops. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require operators to stop registration writers before applying. If an old worker inserts a registration during the ten-second wait, the script drops the table without checking again. That loses the new row. Require operators to stop all registration writers before the checks and keep them stopped until the drops finish. (raw.githubusercontent.com)
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Drop the inbound lti_registration table and the lti_key columns added with it. | ||
| * | ||
| * Not called from the web, from admin/upgrade.php, or from database setup. | ||
| * A dry run is the default. apply drops the foreign key, then the columns, | ||
| * then the table. Each statement auto-commits. A second apply finds nothing | ||
| * left and exits. | ||
| * | ||
| * The script stops when lti_registration still has rows, when a key still | ||
| * points at a registration, or when one of the added columns holds a value. | ||
| * Copy those values back onto lti_key and delete the registration rows first. | ||
| * | ||
| * php admin/lti-maint/undo-registration-migration.php | ||
| * php admin/lti-maint/undo-registration-migration.php apply | ||
| */ | ||
|
|
||
| use Tsugi\Core\LTIX; | ||
| use Tsugi\Util\U; | ||
|
|
||
| if ( PHP_SAPI !== 'cli' | ||
| || ! isset($_SERVER['SCRIPT_FILENAME']) | ||
| || realpath($_SERVER['SCRIPT_FILENAME']) !== realpath(__FILE__) ) { | ||
| fwrite(STDERR, "undo-registration-migration.php must be run from the CLI\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| if ( ! defined('COOKIE_SESSION') ) define('COOKIE_SESSION', true); | ||
| require_once __DIR__ . '/../../config.php'; | ||
|
|
||
| if ( ! U::isCli() ) { | ||
| fwrite(STDERR, "undo-registration-migration.php must be run from the CLI\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| if ( realpath($CFG->dirroot) !== realpath(__DIR__ . '/../..') ) { | ||
| fwrite(STDERR, "dirroot is {$CFG->dirroot}, expected this checkout.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $apply = false; | ||
| foreach ( array_slice($argv, 1) as $arg ) { | ||
| if ( $arg === 'apply' ) { | ||
| $apply = true; | ||
| continue; | ||
| } | ||
| if ( $arg === 'dry' || $arg === 'dry-run' || $arg === '--dry-run' ) { | ||
| continue; | ||
| } | ||
| fwrite(STDERR, "Unknown argument: {$arg}\n"); | ||
| fwrite(STDERR, "Usage: php undo-registration-migration.php [apply]\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| LTIX::getConnection(); | ||
|
|
||
| if ( $PDOX->inTransaction() ) { | ||
| fwrite(STDERR, "Refusing to run inside a transaction.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $p = $CFG->dbprefix; | ||
| $keyTable = $p.'lti_key'; | ||
| $regTable = $p.'lti_registration'; | ||
| $fkName = $p.'lti_key_ibfk_1'; | ||
| $columns = array( | ||
| 'lti_registration_id', | ||
| 'deployment_status', | ||
| 'deployment_approved_at', | ||
| 'platform_guid', | ||
| 'dynamic_registration_unlocked_at', | ||
| ); | ||
|
|
||
| /** | ||
| * @param string $line | ||
| */ | ||
| function undo_registration_out($line) | ||
| { | ||
| echo $line."\n"; | ||
| flush(); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $sql | ||
| * @param array<string, mixed> $params | ||
| * @return array<string, mixed>|null | ||
| */ | ||
| function undo_registration_row($PDOX, $sql, $params = array()) | ||
| { | ||
| $q = $PDOX->queryReturnError($sql, $params); | ||
| if ( ! $q->success ) { | ||
| fwrite(STDERR, $q->errorImplode."\n"); | ||
| exit(1); | ||
| } | ||
| $row = $q->fetch(\PDO::FETCH_ASSOC); | ||
| $q->closeCursor(); | ||
| if ( $row === false ) return null; | ||
| return $row; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $sql | ||
| */ | ||
| function undo_registration_exec($PDOX, $sql) | ||
| { | ||
| undo_registration_out($sql); | ||
| $q = $PDOX->queryReturnError($sql); | ||
| if ( ! $q->success ) { | ||
| fwrite(STDERR, $q->errorImplode."\n"); | ||
| exit(1); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| $regExists = $PDOX->metadata($regTable) !== false; | ||
| $keyExists = $PDOX->metadata($keyTable) !== false; | ||
| if ( ! $keyExists ) { | ||
| fwrite(STDERR, "{$keyTable} is missing.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $present = array(); | ||
| foreach ( $columns as $column ) { | ||
| if ( $PDOX->columnExists($column, $keyTable) ) { | ||
| $present[] = $column; | ||
| } | ||
| } | ||
|
|
||
| $fk = undo_registration_row( | ||
| $PDOX, | ||
| "SELECT CONSTRAINT_NAME | ||
| FROM information_schema.TABLE_CONSTRAINTS | ||
| WHERE TABLE_SCHEMA = DATABASE() | ||
| AND TABLE_NAME = :TABLE_NAME | ||
| AND CONSTRAINT_NAME = :CONSTRAINT_NAME | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match the registration foreign key by column and referenced table, not by name. The lookup selects only Select the constraint by its column and referenced table. This still finds the registration foreign key when it exists, and it does not match the issuer constraint. Suggested fix
|
||
| AND CONSTRAINT_TYPE = 'FOREIGN KEY'", | ||
| array(':TABLE_NAME' => $keyTable, ':CONSTRAINT_NAME' => $fkName) | ||
| ); | ||
| $fkExists = is_array($fk); | ||
|
|
||
| $blockers = array(); | ||
| if ( $regExists ) { | ||
| $countRow = undo_registration_row($PDOX, "SELECT COUNT(*) AS n FROM {$regTable}"); | ||
| $regCount = (int) ($countRow['n'] ?? 0); | ||
| undo_registration_out("{$regTable} rows: {$regCount}"); | ||
| if ( $regCount > 0 ) { | ||
| $blockers[] = "{$regTable} has {$regCount} row(s). Copy issuer, client id, and URLs back onto {$keyTable}, delete those rows, then run this again."; | ||
| } | ||
| } else { | ||
| undo_registration_out("{$regTable} is already absent"); | ||
| } | ||
|
|
||
| foreach ( $present as $column ) { | ||
| $countRow = undo_registration_row( | ||
| $PDOX, | ||
| "SELECT COUNT(*) AS n FROM {$keyTable} WHERE {$column} IS NOT NULL" | ||
| ); | ||
| $filled = (int) ($countRow['n'] ?? 0); | ||
| undo_registration_out("{$keyTable}.{$column}: present, {$filled} non-null"); | ||
| if ( $filled > 0 ) { | ||
| $blockers[] = "{$keyTable}.{$column} is set on {$filled} key(s). This script does not keep that value."; | ||
| } | ||
| } | ||
|
|
||
| foreach ( $columns as $column ) { | ||
| if ( ! in_array($column, $present, true) ) { | ||
| undo_registration_out("{$keyTable}.{$column} is already absent"); | ||
| } | ||
| } | ||
|
|
||
| if ( $fkExists ) { | ||
| undo_registration_out("foreign key {$fkName} is present"); | ||
| } else { | ||
| undo_registration_out("foreign key {$fkName} is already absent"); | ||
| } | ||
|
|
||
| if ( count($blockers) > 0 ) { | ||
| foreach ( $blockers as $blocker ) { | ||
| fwrite(STDERR, $blocker."\n"); | ||
| } | ||
| exit(1); | ||
| } | ||
|
|
||
| $work = $fkExists || count($present) > 0 || $regExists; | ||
| if ( ! $work ) { | ||
| undo_registration_out("Nothing to drop."); | ||
| exit(0); | ||
| } | ||
|
|
||
| if ( ! $apply ) { | ||
| if ( $fkExists ) undo_registration_out("Would drop foreign key {$fkName}"); | ||
| foreach ( $present as $column ) { | ||
| undo_registration_out("Would drop {$keyTable}.{$column}"); | ||
| } | ||
| if ( $regExists ) undo_registration_out("Would drop table {$regTable}"); | ||
| undo_registration_out("Dry run. php admin/lti-maint/undo-registration-migration.php apply"); | ||
| exit(0); | ||
| } | ||
|
|
||
| undo_registration_out("This IS NOT A DRILL. Dropping the registration table and columns in 10 seconds."); | ||
| sleep(10); | ||
|
|
||
| if ( $fkExists ) { | ||
| undo_registration_exec($PDOX, "ALTER TABLE `{$keyTable}` DROP FOREIGN KEY `{$fkName}`"); | ||
| } | ||
| foreach ( $present as $column ) { | ||
| undo_registration_exec($PDOX, "ALTER TABLE `{$keyTable}` DROP COLUMN `{$column}`"); | ||
| } | ||
| if ( $regExists ) { | ||
| undo_registration_exec($PDOX, "DROP TABLE `{$regTable}`"); | ||
| } | ||
|
|
||
| undo_registration_out("Done."); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,9 @@ | |
| $key_key = U::get($_POST,'key_key'); | ||
| $deploy_key = U::get($_POST,'deploy_key'); | ||
|
|
||
| $old_keyset_url = null; | ||
| $saw_old_keyset = false; | ||
|
|
||
| // Check the complex validation | ||
| if ( count($_POST) > 0 && U::get($_POST,'doUpdate') && !empty($key_id) ) { | ||
| $row = $PDOX->rowDie( "SELECT * FROM {$CFG->dbprefix}lti_key | ||
|
|
@@ -87,6 +90,8 @@ | |
|
|
||
| $old_key_key = $row['key_key']; | ||
| $old_deploy_key = $row['deploy_key']; | ||
| $old_keyset_url = $row['lms_keyset_url']; | ||
| $saw_old_keyset = true; | ||
| $lms_issuer_val = array_key_exists('lms_issuer', $_POST) ? $_POST['lms_issuer'] : $row['lms_issuer']; | ||
| $lms_client_val = array_key_exists('lms_client', $_POST) ? $_POST['lms_client'] : $row['lms_client']; | ||
| $posted_secret = array_key_exists('secret', $_POST) ? $_POST['secret'] : $row['secret']; | ||
|
|
@@ -121,6 +126,14 @@ | |
| $query_fields, $allow_edit, $allow_delete); | ||
|
|
||
| if ( $row === CrudForm::CRUD_FAIL || $row === CrudForm::CRUD_SUCCESS ) { | ||
| if ( $row === CrudForm::CRUD_SUCCESS && isset($_POST['doUpdate']) && $saw_old_keyset | ||
| && array_key_exists('lms_keyset_url', $_POST) ) { | ||
| $posted_keyset = trim((string) $_POST['lms_keyset_url']); | ||
| $stored_keyset = trim((string) $old_keyset_url); | ||
| if ( $posted_keyset !== $stored_keyset ) { | ||
| LTIX::clearKeysetCache($key_id); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commit the keyset URL update and its cache clear together. The update in Wrap the update and the clear in one transaction. Begin the transaction before
|
||
| } | ||
| } | ||
| header('Location: '.$from_location); | ||
| return; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1151,6 +1151,26 @@ public static function displayNameFromClaim($claim) { | |
| return $retval; | ||
| } | ||
|
|
||
| /** | ||
| * Drop a stored platform keyset. The next launch fetches it again. | ||
| * | ||
| * Call this when the keyset URL on the key changes. A launch's login | ||
| * timestamp and the cache fill itself are not URL changes. | ||
| * | ||
| * @param mixed $key_id | ||
| */ | ||
| public static function clearKeysetCache($key_id) { | ||
| global $PDOX, $CFG; | ||
| $id = (int) $key_id; | ||
| if ( $id < 1 || ! isset($PDOX) || ! $PDOX || ! isset($CFG) ) return; | ||
| $PDOX->queryDie( | ||
| "UPDATE {$CFG->dbprefix}lti_key | ||
| SET lms_cache_keyset = NULL, lms_cache_pubkey = NULL, lms_cache_kid = NULL | ||
| WHERE key_id = :ID", | ||
|
Comment on lines
+1168
to
+1169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the keyset cache writes conditional on the URL they were fetched from.
Make both cache writes conditional on the URL that was fetched. For example, add
|
||
| array(':ID' => $id) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Load the data from our lti_ tables using one long LEFT JOIN | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explain how to clear the remaining column values.
If
lti_key.platform_guidis non-null, copying the registration fields and deleting the registration rows does not clear it. The script still refuses to run. Document how to preserve and clear each remaining value before retrying. (raw.githubusercontent.com)