Repository navigation
Drop the plaintext personal access token column (D3 of 3: drop identity_access_tokens.token) #3203
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
Draft
jeremy
wants to merge
6
commits into
oauth-token-digests-d2
Choose a base branch
from
oauth-token-digests-d3
base: oauth-token-digests-d2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
36fd94e
Drop the plaintext personal access token column
jeremy 99ba98c
Record step 3 of token digests in the conformance record and deployme…
jeremy fecaa4a
Rebuild the table when dropping the plaintext on MySQL, and name the …
jeremy 6d1e545
Say where plaintext tokens from before the upgrade may linger
jeremy 9b670d8
Name the code that must be running before the plaintext is dropped
jeremy a420fae
Drop the plaintext refresh token column beside token
jeremy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
db/migrate/20261017130000_drop_identity_access_token_plaintext.rb
This file contains hidden or 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,37 @@ | ||
| # Drops the plaintext personal access tokens that BackfillTokenDigests and | ||
| # BackfillRemainingTokenDigests left beside their digests, and the plaintext | ||
| # refresh token column, empty since BackfillTokenDigests. | ||
| # | ||
| # Run it only once every process runs the code that shipped with | ||
| # BackfillRemainingTokenDigests, or later. That code finds tokens only by | ||
| # digest and ignores both columns, so it never names them. Code from before it | ||
| # still writes personal access tokens' plaintext, and falls back to reading | ||
| # it, so it fails once the column is gone. Code from before | ||
| # DropPlaintextOauthCredentials, which doesn't ignore refresh_token, names it | ||
| # on every INSERT. | ||
| # | ||
| # Undoing this restores both columns empty and nullable, as they were before | ||
| # any token was written. The tokens they held can't be recovered from their | ||
| # digests, so code that reads only plaintext can't authenticate them again. | ||
| class DropIdentityAccessTokenPlaintext < ActiveRecord::Migration[8.2] | ||
| def up | ||
| if connection.adapter_name.in?(%w[ Mysql2 Trilogy ]) | ||
| # MySQL would drop the columns INSTANT, from the data dictionary alone, | ||
| # leaving each row's plaintext in the table's pages until that row is | ||
| # next rewritten, which for most tokens is never. Rebuilding the table | ||
| # writes every row without them, still without blocking reads or writes. | ||
| execute "ALTER TABLE identity_access_tokens DROP INDEX index_identity_access_tokens_on_refresh_token, " \ | ||
| "DROP COLUMN refresh_token, DROP COLUMN token, ALGORITHM=INPLACE, LOCK=NONE" | ||
| else | ||
| remove_index :identity_access_tokens, :refresh_token, unique: true | ||
| remove_column :identity_access_tokens, :refresh_token | ||
| remove_column :identity_access_tokens, :token | ||
| end | ||
| end | ||
|
|
||
| def down | ||
| add_column :identity_access_tokens, :token, :string | ||
| add_column :identity_access_tokens, :refresh_token, :string | ||
| add_index :identity_access_tokens, :refresh_token, unique: true | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
103 changes: 0 additions & 103 deletions
103
test/migrations/backfill_remaining_token_digests_test.rb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
test/migrations/drop_identity_access_token_plaintext_test.rb
This file contains hidden or 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,50 @@ | ||
| require "test_helper" | ||
| require Rails.root.join("db/migrate/20261017130000_drop_identity_access_token_plaintext") | ||
|
|
||
| # DropIdentityAccessTokenPlaintext runs while processes still serve the code | ||
| # that shipped with BackfillRemainingTokenDigests, which ignores both plaintext | ||
| # columns. With partial_inserts off, a process names every column it has cached | ||
| # on each INSERT, so one that loaded identity_access_tokens' columns before the | ||
| # migration must still create tokens after it. | ||
| class DropIdentityAccessTokenPlaintextTest < ActiveSupport::TestCase | ||
| # Each step is DDL, which MySQL commits on its own. | ||
| self.use_transactional_tests = false | ||
|
|
||
| # identity_access_tokens as the code before this migration models it. | ||
| class PreviousAccessToken < ApplicationRecord | ||
| self.table_name = "identity_access_tokens" | ||
| self.ignored_columns = %w[ token refresh_token ] | ||
| end | ||
|
|
||
| setup do | ||
| migrate :down | ||
| PreviousAccessToken.reset_column_information.then { PreviousAccessToken.columns } | ||
| end | ||
|
|
||
| teardown do | ||
| migrate :up if ActiveRecord::Base.connection.column_exists?(:identity_access_tokens, :token) | ||
| [ PreviousAccessToken, Identity::AccessToken ].each(&:reset_column_information) | ||
| PreviousAccessToken.where(description: "Cached before the migration").delete_all | ||
| end | ||
|
|
||
| test "drops both plaintext columns" do | ||
| migrate :up | ||
|
|
||
| assert_not ActiveRecord::Base.connection.column_exists?(:identity_access_tokens, :token) | ||
| assert_not ActiveRecord::Base.connection.column_exists?(:identity_access_tokens, :refresh_token) | ||
| end | ||
|
|
||
| test "a process that cached identity_access_tokens' columns before it creates tokens after it" do | ||
| migrate :up | ||
|
|
||
| assert_difference -> { PreviousAccessToken.count } do | ||
| PreviousAccessToken.create! identity_id: identities(:david).id, permission: "write", description: "Cached before the migration", | ||
| token_digest: TokenDigester.digest(SecureRandom.base58(24)) | ||
| end | ||
| end | ||
|
|
||
| private | ||
| def migrate(direction) | ||
| DropIdentityAccessTokenPlaintext.new.tap { |migration| migration.verbose = false }.migrate(direction) | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.