Skip to content

Commit

Permalink
Fix instance actor being incorrectly created when running migrations (m…
Browse files Browse the repository at this point in the history
…astodon#18109)

* Add migration test about instance actor key

* Fix old migration

* Work around incorrect database state
  • Loading branch information
ClearlyClaire authored and single-right-quote committed May 5, 2022
1 parent ecd9e30 commit a4f40dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ def emojis
before_validation :prepare_username, on: :create
before_destroy :clean_feed_manager

def ensure_keys!
return unless local? && private_key.blank? && public_key.blank?
generate_keys
save!
end

private

def prepare_contents
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/account_finder_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def find_local_or_remote!(username, domain)
end

def representative
Account.find(-99)
Account.find(-99).tap(&:ensure_keys!)
rescue ActiveRecord::RecordNotFound
Account.create!(id: -99, actor_type: 'Application', locked: true, username: Rails.configuration.x.local_domain)
end
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20190715164535_add_instance_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ class AddInstanceActor < ActiveRecord::Migration[5.2]
class Account < ApplicationRecord
# Dummy class, to make migration possible across version changes
validates :username, uniqueness: { scope: :domain, case_sensitive: false }

before_create :generate_keys

def generate_keys
keypair = OpenSSL::PKey::RSA.new(2048)
self.private_key = keypair.to_pem
self.public_key = keypair.public_key.to_pem
end
end

def up
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/tests.rake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ namespace :tests do
puts 'AccountConversation records not created as expected'
exit(1)
end

if Account.find(-99).private_key.blank?
puts 'Instance actor does not have a private key'
exit(1)
end
end

desc 'Populate the database with test data for 2.4.0'
Expand Down

0 comments on commit a4f40dc

Please sign in to comment.