mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 17:59:47 +02:00
Story 108521790 importing dnskey
This commit is contained in:
parent
d41488472a
commit
ac6eccb24c
3 changed files with 85 additions and 59 deletions
|
@ -4,5 +4,50 @@ module Legacy
|
||||||
|
|
||||||
belongs_to :object_registry, foreign_key: :id
|
belongs_to :object_registry, foreign_key: :id
|
||||||
belongs_to :object, foreign_key: :id
|
belongs_to :object, foreign_key: :id
|
||||||
|
has_one :object_history, foreign_key: :historyid, primary_key: :historyid
|
||||||
|
|
||||||
|
|
||||||
|
def self.for_at(keysetid, time)
|
||||||
|
return [] unless keysetid
|
||||||
|
|
||||||
|
sql = %Q{select distinct dh.id, dh.keysetid, dh.flags, dh.protocol, dh.alg, dh.key,
|
||||||
|
first_value(history.valid_from) OVER (PARTITION BY key ORDER BY history.valid_from ASC NULLS FIRST) valid_from,
|
||||||
|
first_value(history.valid_to) OVER (PARTITION BY key ORDER BY history.valid_to DESC NULLS FIRST) valid_to
|
||||||
|
FROM dnskey_history dh JOIN history ON dh.historyid=history.id
|
||||||
|
WHERE dh.keysetid IN (#{keysetid})
|
||||||
|
AND (valid_from is null or valid_from <= '#{time.to_s}'::TIMESTAMPTZ)
|
||||||
|
AND (valid_to is null or valid_to >= '#{time}'::TIMESTAMPTZ)
|
||||||
|
ORDER BY dh.id;}
|
||||||
|
find_by_sql(sql)
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_object_hash(old_domain, new_domain)
|
||||||
|
new_object_mains(new_domain).merge(
|
||||||
|
creator_str: old_domain.object_registry.try(:registrar).try(:name),
|
||||||
|
updator_str: old_domain.object_history.try(:registrar).try(:name) || old_domain.object_registry.try(:registrar).try(:name),
|
||||||
|
legacy_domain_id: old_domain.id,
|
||||||
|
legacy_keyset_id: keysetid,
|
||||||
|
updated_at: (!object_registry.try(:object_history) || object_registry.try(:object_history).read_attribute(:update).nil?) ? (try(:crdate)||Time.zone.now) : object_registry.try(:object_history).read_attribute(:update)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_object_mains(new_domain)
|
||||||
|
@new_object_mains ||= {
|
||||||
|
domain_id: new_domain.id,
|
||||||
|
flags: flags,
|
||||||
|
protocol: protocol,
|
||||||
|
alg: alg,
|
||||||
|
public_key: key
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def historical_data(old_domain, new_domain, time_attr = :valid_from)
|
||||||
|
{
|
||||||
|
whodunnit: old_domain.history_domain.user.try(:id),
|
||||||
|
object: nil,
|
||||||
|
object_changes: new_object_hash(old_domain, new_domain).each_with_object({}){|(k,v), h| h[k] = [nil, v]},
|
||||||
|
created_at: [try(time_attr), old_domain.try(time_attr)].max
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,7 @@ module Legacy
|
||||||
class DomainHistory < Db
|
class DomainHistory < Db
|
||||||
self.table_name = :domain_history
|
self.table_name = :domain_history
|
||||||
self.primary_key = :id
|
self.primary_key = :id
|
||||||
|
class_attribute :dnssecs
|
||||||
|
|
||||||
belongs_to :object_registry, foreign_key: :id
|
belongs_to :object_registry, foreign_key: :id
|
||||||
belongs_to :object, foreign_key: :id
|
belongs_to :object, foreign_key: :id
|
||||||
|
@ -123,6 +124,44 @@ module Legacy
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# returns imported dnskey ids
|
||||||
|
def import_dnskeys_history(new_domain, time)
|
||||||
|
self.class.dnssecs ||= {}
|
||||||
|
self.class.dnssecs[id] ||= {}
|
||||||
|
ids = []
|
||||||
|
Legacy::DnskeyHistory.for_at(keyset, time).each do |dns|
|
||||||
|
# checking if we have create history for dnskey (cache)
|
||||||
|
if val = self.class.dnssecs[id][dns]
|
||||||
|
ids << val
|
||||||
|
else # if not found we should check current dnssec and historical if changes were done
|
||||||
|
# if current object wan't changed
|
||||||
|
if item=::Dnskey.where(dns.new_object_mains(new_domain)).first
|
||||||
|
item.versions.where(event: :create).first_or_create!(dns.historical_data(self, new_domain))
|
||||||
|
self.class.dnssecs[id][dns] = item.id
|
||||||
|
ids << item.id
|
||||||
|
# if current object was changed
|
||||||
|
elsif (versions = ::DnskeyVersion.where("object->>'legacy_domain_id'='#{id}'").to_a).any?
|
||||||
|
versions.each do |v|
|
||||||
|
if v.object.slice(*dns.new_object_mains(new_domain).stringify_keys.keys) == dns.new_object_mains(new_domain).keys
|
||||||
|
self.class.dnssecs[id][dns] = v.item_id
|
||||||
|
ids << v.item_id
|
||||||
|
v.item.versions.where(event: :create).first_or_create!(dns.historical_data(self, new_domain))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
item=::Dnskey.new(id: ::Dnskey.next_id)
|
||||||
|
DnskeyVersion.where(item_type: ::Dnskey.to_s, item_id: item.id).where(event: :create).first_or_create!(dns.historical_data(self, new_domain))
|
||||||
|
DnskeyVersion.where(item_type: ::Dnskey.to_s, item_id: item.id).where(event: :destroy).first_or_create!(dns.historical_data(self, new_domain), :valid_to) if dns.valid_to
|
||||||
|
self.class.dnssecs[id][dns] = item.id
|
||||||
|
ids << item.id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ids
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def changes_dates_for domain_id
|
def changes_dates_for domain_id
|
||||||
sql = %Q{SELECT dh.*, valid_from
|
sql = %Q{SELECT dh.*, valid_from
|
||||||
|
|
|
@ -2,7 +2,6 @@ namespace :import do
|
||||||
desc 'Import all history'
|
desc 'Import all history'
|
||||||
task history_all: :environment do
|
task history_all: :environment do
|
||||||
Rake::Task['import:history_contacts'].invoke
|
Rake::Task['import:history_contacts'].invoke
|
||||||
Rake::Task['import:history_dnskeys'].invoke
|
|
||||||
Rake::Task['import:history_domains'].invoke
|
Rake::Task['import:history_domains'].invoke
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,63 +77,6 @@ namespace :import do
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
desc 'Import contact history'
|
|
||||||
task history_dnskeys: :environment do
|
|
||||||
Legacy::DnskeyHistory.uniq.pluck(:id).each do |legacy_dnskey_id|
|
|
||||||
Dnskey.transaction do
|
|
||||||
contact = Dnskey.find_by(legacy_id: legacy_dnskey_id)
|
|
||||||
version_dns = DnskeytVersion.where("object->>'legacy_id' = '#{legacy_dnskey_id}'").select(:item_id).first
|
|
||||||
contact ||= Dnskey.new(id: version_dns.item_id, legacy_id: legacy_dnskey_id) if version_dns
|
|
||||||
contact ||= Dnskey.new(id: ::Contact.next_id, legacy_id: legacy_dnskey_id)
|
|
||||||
next if contact.versions.where(event: :create).any?
|
|
||||||
# add here to skip domains whith create history
|
|
||||||
|
|
||||||
last_changes = nil
|
|
||||||
history = Legacy::ContactHistory.changes_dates_for(legacy_dnskey_id)
|
|
||||||
last_contact_action = history.sort.last[1].last # need to identify if we delete
|
|
||||||
|
|
||||||
keys = history.keys.compact.sort
|
|
||||||
i = 0
|
|
||||||
keys.each_with_index do |time|
|
|
||||||
history[time].each do |orig_history_klass|
|
|
||||||
changes = {}
|
|
||||||
responder = orig_history_klass[:klass].get_record_at(legacy_dnskey_id, orig_history_klass[:id])
|
|
||||||
new_attrs = responder.get_current_contact_object(time, orig_history_klass[:param])
|
|
||||||
new_attrs[:id] = contact.id
|
|
||||||
|
|
||||||
event = :update
|
|
||||||
event = :create if i == 0
|
|
||||||
if orig_history_klass == last_contact_action && responder.valid_to.present?
|
|
||||||
event = :destroy
|
|
||||||
new_attrs = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
new_attrs.each do |k, v|
|
|
||||||
if (old_val = last_changes.to_h[k]) != v then changes[k] = [old_val, v] end
|
|
||||||
end
|
|
||||||
next if changes.blank? && event != :destroy
|
|
||||||
obj_his = Legacy::ObjectHistory.find_by(historyid: responder.historyid)
|
|
||||||
user = Registrar.find_by(legacy_id: obj_his.upid || obj_his.clid).try(:api_users).try(:first)
|
|
||||||
|
|
||||||
hash = {
|
|
||||||
item_type: Dnskey.to_s,
|
|
||||||
item_id: contact.id,
|
|
||||||
event: event,
|
|
||||||
whodunnit: user.try(:id),
|
|
||||||
object: last_changes,
|
|
||||||
object_changes: changes,
|
|
||||||
created_at: time
|
|
||||||
}
|
|
||||||
DnskeyVersion.create!(hash)
|
|
||||||
|
|
||||||
last_changes = new_attrs
|
|
||||||
i += 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
desc 'Import domain history'
|
desc 'Import domain history'
|
||||||
task history_domains: :environment do
|
task history_domains: :environment do
|
||||||
|
@ -200,7 +142,7 @@ namespace :import do
|
||||||
admin_contacts: responder.history_domain.get_admin_contact_new_ids,
|
admin_contacts: responder.history_domain.get_admin_contact_new_ids,
|
||||||
tech_contacts: responder.history_domain.get_tech_contact_new_ids,
|
tech_contacts: responder.history_domain.get_tech_contact_new_ids,
|
||||||
nameservers: [],
|
nameservers: [],
|
||||||
dnskeys: [],
|
dnskeys: responder.history_domain.import_dnskeys_history(domain, time),
|
||||||
registrant: [responder.history_domain.new_registrant_id]
|
registrant: [responder.history_domain.new_registrant_id]
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue