]> cat aescling's git repositories - mastodon.git/commitdiff
Fix instance actor being incorrectly created when running migrations (#18109)
authorClaire <claire.github-309c@sitedethib.com>
Tue, 26 Apr 2022 19:22:09 +0000 (21:22 +0200)
committersingle-right-quote <11325618-aescling@users.noreply.gitlab.com>
Thu, 5 May 2022 03:56:01 +0000 (23:56 -0400)
* Add migration test about instance actor key

* Fix old migration

* Work around incorrect database state

app/models/account.rb
app/models/concerns/account_finder_concern.rb
db/migrate/20190715164535_add_instance_actor.rb
lib/tasks/tests.rake

index 1966c5a48bfd431406a9088eea5af5005bb27069..068ee7ae9c6fe431b2a31c9fb52b47d69abd8daa 100644 (file)
@@ -561,6 +561,12 @@ class Account < ApplicationRecord
   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
index 7e165d3ab92cfe4ef1f4be6fd71c2f1a1a956071..c4f255b73b08238c10c257aa47b3db3962232eb2 100644 (file)
@@ -17,7 +17,7 @@ module AccountFinderConcern
     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
index 8c0301d69d8bf3ec1fd7c15634bb37f51a0a14b6..0ae53199acbd52e779ff18e9a5d0484baba350ff 100644 (file)
@@ -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
index 8082f32fb4ca6b9341f4a9ac98b6824e661cfa2f..0f3b44a7443f3dd8d1caad10b49bdc88394e02cb 100644 (file)
@@ -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'