1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00

Move account private key to dedicated table

This commit is contained in:
Adam Niedzielski 2024-07-29 13:19:07 +02:00
parent dfd43869c9
commit ee58baf14e
No known key found for this signature in database
GPG key ID: D97FC2C798BAC058
6 changed files with 66 additions and 1 deletions

View file

@ -501,6 +501,19 @@ class Account < ApplicationRecord
save! save!
end end
def private_key
if account_secret
account_secret.private_key
else
super
end
end
def private_key=(value)
self.account_secret ||= AccountSecret.new
account_secret.private_key = value
end
private private
def prepare_contents def prepare_contents

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: account_secrets
#
# id :bigint(8) not null, primary key
# private_key :text
# account_id :bigint(8) not null
# created_at :datetime not null
# updated_at :datetime not null
#
class AccountSecret < ApplicationRecord
belongs_to :account
encrypts :private_key
end

View file

@ -77,5 +77,8 @@ module Account::Associations
# Imports # Imports
has_many :bulk_imports, inverse_of: :account, dependent: :delete_all has_many :bulk_imports, inverse_of: :account, dependent: :delete_all
# Secrets
has_one :account_secret, inverse_of: :account, dependent: :destroy, autosave: true
end end
end end

View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
class CreateAccountSecrets < ActiveRecord::Migration[7.1]
def change
create_table :account_secrets do |t|
t.text :private_key
t.references :account, null: false, foreign_key: true
t.timestamps
end
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_07_24_181224) do ActiveRecord::Schema[7.1].define(version: 2024_07_26_143215) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -103,6 +103,14 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_24_181224) do
t.index ["relationship_severance_event_id"], name: "idx_on_relationship_severance_event_id_403f53e707" t.index ["relationship_severance_event_id"], name: "idx_on_relationship_severance_event_id_403f53e707"
end end
create_table "account_secrets", force: :cascade do |t|
t.text "private_key"
t.bigint "account_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id"], name: "index_account_secrets_on_account_id"
end
create_table "account_stats", force: :cascade do |t| create_table "account_stats", force: :cascade do |t|
t.bigint "account_id", null: false t.bigint "account_id", null: false
t.bigint "statuses_count", default: 0, null: false t.bigint "statuses_count", default: 0, null: false
@ -1274,6 +1282,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_24_181224) do
add_foreign_key "account_pins", "accounts", on_delete: :cascade add_foreign_key "account_pins", "accounts", on_delete: :cascade
add_foreign_key "account_relationship_severance_events", "accounts", on_delete: :cascade add_foreign_key "account_relationship_severance_events", "accounts", on_delete: :cascade
add_foreign_key "account_relationship_severance_events", "relationship_severance_events", on_delete: :cascade add_foreign_key "account_relationship_severance_events", "relationship_severance_events", on_delete: :cascade
add_foreign_key "account_secrets", "accounts"
add_foreign_key "account_stats", "accounts", on_delete: :cascade add_foreign_key "account_stats", "accounts", on_delete: :cascade
add_foreign_key "account_statuses_cleanup_policies", "accounts", on_delete: :cascade add_foreign_key "account_statuses_cleanup_policies", "accounts", on_delete: :cascade
add_foreign_key "account_warnings", "accounts", column: "target_account_id", on_delete: :cascade add_foreign_key "account_warnings", "accounts", column: "target_account_id", on_delete: :cascade

View file

@ -1067,4 +1067,15 @@ RSpec.describe Account do
expect(subject.reload.followers_count).to eq 15 expect(subject.reload.followers_count).to eq 15
end end
end end
describe 'private key' do
it 'encrypts and decrypts the key' do
account = Fabricate(:account)
account.private_key = 'secret'
account.save!
expect(account.reload.private_key).to eq 'secret'
end
end
end end