Repository navigation
Store personal access tokens only as digests (D2 of 3: rerun the backfill, stop the plaintext) #3202
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
Choose a base branch
from
oauth-token-digests-d2
base: oauth-token-digests
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
Store personal access tokens only as digests (D2 of 3: rerun the backfill, stop the plaintext) #3202
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ccfc8a5
Store personal access tokens only as digests, and stop reading plaintext
jeremy 0da26eb
Record step 2 of token digests in the conformance record
jeremy ecc24d7
Point the conformance record at #3202
jeremy fdf36db
Keep the personal access client's tokens' plaintext in the rerun, and…
jeremy 5415432
Use the personal access client where it already exists in the rerun's…
jeremy 3ac03cc
Present the scheme pins' fixture token from memory, as digests require
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
73 changes: 73 additions & 0 deletions
73
db/migrate/20261017120000_backfill_remaining_token_digests.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,73 @@ | ||
| # Digests every access token still without one, as BackfillTokenDigests did, | ||
| # before code that finds tokens only by digest is deployed. Until then, | ||
| # production code that stores only plaintext kept writing personal access | ||
| # tokens, and each was digested only when it was first used. One never used | ||
| # since is digested here, or it would stop authenticating. | ||
| # | ||
| # MySQL and SQLite have no HMAC, so this runs in Ruby, a batch at a time, | ||
| # outside one long transaction. update_all writes only the digest: a grant's | ||
| # updated_at is its idle-lapse clock (Identity::AccessToken::REFRESH_IDLE_LIMIT), | ||
| # and backfilling must not restart it. | ||
| # | ||
| # Personal access tokens keep their plaintext, as BackfillTokenDigests left | ||
| # it, until the column is dropped, so rolling back to code that reads only | ||
| # plaintext still finds them. A personal access token is one without a | ||
| # client, or one issued by the personal access client where that client | ||
| # exists. OAuth access tokens are digest-only, so any plaintext one is | ||
| # cleared once digested. | ||
| # | ||
| # Rerunnable: it only touches rows without a digest, and does nothing once the | ||
| # plaintext column is gone. Its down is a no-op, so | ||
| # `db:migrate:redo:primary VERSION=20261017120000` runs it again. | ||
| class BackfillRemainingTokenDigests < ActiveRecord::Migration[8.2] | ||
| disable_ddl_transaction! | ||
|
|
||
| BATCH_SIZE = 500 | ||
|
|
||
| # Identity::AccessToken ignores the plaintext column, so this reads it | ||
| # through a model of its own. | ||
| class AccessToken < ActiveRecord::Base | ||
| self.table_name = "identity_access_tokens" | ||
| end | ||
|
|
||
| class Client < ActiveRecord::Base | ||
| self.table_name = "oauth_clients" | ||
| end | ||
|
|
||
| PERSONAL_ACCESS_CLIENT_ID = "fizzy-personal-access" | ||
|
|
||
| def up | ||
| if column_exists?(:identity_access_tokens, :token) | ||
| refuse_duplicate_tokens | ||
| personal_client_ids = [ nil, *Client.where(client_id: PERSONAL_ACCESS_CLIENT_ID).pluck(:id) ] | ||
|
|
||
| undigested.in_batches(of: BATCH_SIZE) do |batch| | ||
| batch.select(:id, :token, :oauth_client_id).each do |row| | ||
| AccessToken.where(id: row.id, token_digest: nil).update_all \ | ||
| token_digest: TokenDigester.digest(row.token), token: (row.oauth_client_id.in?(personal_client_ids) ? row.token : nil) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def down | ||
| # Digests are derived from what's still stored: nothing to undo. | ||
| end | ||
|
|
||
| private | ||
| def undigested | ||
| AccessToken.where(token_digest: nil).where.not(token: nil) | ||
| end | ||
|
|
||
| # Two rows sharing a token would share a digest, which the unique index | ||
| # refuses partway through. Stop before writing anything instead, naming | ||
| # the rows but never the tokens. | ||
| def refuse_duplicate_tokens | ||
| duplicated = AccessToken.where.not(token: nil).group(:token).having("COUNT(*) > 1").pluck(:token) | ||
|
|
||
| if duplicated.any? | ||
| ids = AccessToken.where(token: duplicated).pluck(:id) | ||
| raise "identity_access_tokens.token has #{duplicated.size} duplicated value(s), in rows #{ids.join(", ")}. Resolve them before digesting." | ||
| end | ||
| 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| jasons_api_token: | ||
| identity: jason | ||
| token: 018cf1425682700098f24f0799e3fe20 | ||
| token_digest: <%= TokenDigester.digest("018cf1425682700098f24f0799e3fe20") %> | ||
| token_digest: <%= TokenDigester.digest(AccessTokenTestHelper::RAW_FIXTURE_TOKENS.fetch(:jasons_api_token)) %> | ||
| description: My Superscript | ||
| permission: read | ||
|
|
||
| davids_api_token: | ||
| identity: david | ||
| token: x18cf1425682700098f24f0799e3fe20 | ||
| token_digest: <%= TokenDigester.digest("x18cf1425682700098f24f0799e3fe20") %> | ||
| token_digest: <%= TokenDigester.digest(AccessTokenTestHelper::RAW_FIXTURE_TOKENS.fetch(:davids_api_token)) %> | ||
| description: My Superscript | ||
| permission: write |
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
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.