Skip to content

Commit

Permalink
Merge pull request #2483 from internetee/add-xml-console-endpoints
Browse files Browse the repository at this point in the history
Added xml epp console required endpoints and sample xml files
  • Loading branch information
vohmar authored Nov 30, 2022
2 parents aae4740 + 48616ec commit 553339f
Show file tree
Hide file tree
Showing 24 changed files with 620 additions and 47 deletions.
63 changes: 63 additions & 0 deletions app/controllers/concerns/epp_requestable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module EppRequestable
extend ActiveSupport::Concern

included do
# before_action :validate_epp_user, only: :create
end

def create
result = server.request(request_params[:payload])
render_success(data: { xml: result.force_encoding('UTF-8') })
rescue StandardError
handle_non_epp_errors(nil, I18n.t('errors.messages.epp_conn_error'))
end

private

# def validate_epp_user
# return unless handle_hello_request

# handle_login_request
# server.close_connection
# rescue OpenSSL::SSL::SSLError => e
# Rails.logger.error "INVALID CERT: #{e}"
# Rails.logger.error "INVALID CERT DEBUG INFO: epp_hostname: #{ENV['epp_hostname']}," \
# "port: #{ENV['epp_port']}, cert_path: #{ENV['cert_path']}, key_path: #{ENV['key_path']}"
# handle_non_epp_errors(nil, I18n.t('errors.messages.invalid_cert'))
# end

# def handle_hello_request
# res = server.open_connection
# unless Nokogiri::XML(res).css('greeting')
# server.close_connection # just in case
# handle_non_epp_errors(nil, I18n.t('errors.messages.failed_epp_conn')) and return false
# end
# true
# end

# def handle_login_request
# tag = current_user.username
# ex = EppXml::Session.new(cl_trid_prefix: tag)
# xml = ex.login(clID: { value: tag }, pw: { value: current_user.plain_text_password })
# res = server.send_request(xml)

# return if Nokogiri::XML(res).css('result').first['code'] == '1000'

# handle_non_epp_errors(nil, Nokogiri::XML(res).css('result').text)
# end

def server
client_cert = File.read(ENV['cert_path'])
client_key = File.read(ENV['key_path'])
port = ENV['epp_port'] || 700
@server ||= Epp::Server.new({ server: ENV['epp_hostname'], tag: current_user.username,
password: current_user.plain_text_password,
port: port,
cert: OpenSSL::X509::Certificate.new(client_cert),
key: OpenSSL::PKey::RSA.new(client_key) })
end

def request_params
params.require(:xml_console).permit(:payload)
end
end
55 changes: 55 additions & 0 deletions app/controllers/repp/v1/registrar/xml_console_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Repp
module V1
module Registrar
class XmlConsoleController < BaseController
include EppRequestable

THROTTLED_ACTIONS = %i[load_xml].freeze
include Shunter::Integration::Throttle

PREFS = %w[domain-ee contact-ee eis epp-ee].freeze

SCHEMA_VERSIONS = {
'epp-ee' => '1.0',
'eis' => '1.0',
'contact-ee' => '1.1',
'default' => '1.2',
}.freeze

def load_xml
cl_trid = "#{current_user.username}-#{Time.zone.now.to_i}"
obj = ActionController::Base.helpers.sanitize(params[:obj])
epp_action = ActionController::Base.helpers.sanitize(params[:epp_action])
xml_dir_path = Rails.root.join('app/views/epp/sample_requests').to_s
xml = File.read("#{xml_dir_path}/#{obj}/#{epp_action}.xml")
xml = prepare_payload(xml, cl_trid)

render_success(data: { xml: xml })
end

private

def prepare_payload(xml, cl_trid)
PREFS.map do |pref|
xml = load_schema_by_prefix(pref, xml)
end

xml.gsub!('<clTRID>ABC-12345</clTRID>', "<clTRID>#{cl_trid}</clTRID>")
xml
end

def load_schema_by_prefix(pref, xml)
version = version_by_prefix(pref)
xml.gsub!("\"#{pref}\"",
"\"#{Xsd::Schema.filename(for_prefix: pref.to_s, for_version: version)}\"")
xml
end

def version_by_prefix(pref)
key = SCHEMA_VERSIONS.key?(pref) ? pref : 'default'
SCHEMA_VERSIONS[key]
end
end
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/repp/v1/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def set_zero_values!(cur, prev)

def calculate_market_share(domains_by_rar)
sum = domains_by_rar.values.sum
return domains_by_rar if sum.zero?

domains_by_rar.transform_values do |v|
value = v.to_f / sum * 100.0
value < 0.1 ? value.round(3) : value.round(1)
Expand Down
1 change: 0 additions & 1 deletion app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def registrant_change_prohibited?
statuses.include? DomainStatus::SERVER_REGISTRANT_CHANGE_PROHIBITED
end


# NB! contacts, admin_contacts, tech_contacts are empty for a new record
has_many :domain_contacts, dependent: :destroy
has_many :contacts, through: :domain_contacts, source: :contact
Expand Down
94 changes: 51 additions & 43 deletions app/models/legal_document.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class LegalDocument < ApplicationRecord
class LegalDocument < ApplicationRecord # rubocop:disable Metrics/ClassLength
include EppErrors
MIN_BODY_SIZE = (1.37 * 3.kilobytes).ceil
MAX_BODY_SIZE = 8.megabytes
Expand All @@ -14,7 +14,7 @@ class LegalDocument < ApplicationRecord

belongs_to :documentable, polymorphic: true

validate :val_body_length, if: ->(file) { file.path.blank? }
validate :val_body_length, if: ->(file) { file.path.blank? && (Rails.env.production? || Rails.env.test?) }

before_create :add_creator
before_save :save_to_filesystem, if: :body
Expand All @@ -24,7 +24,7 @@ def epp_code_map
'2308' => [
%i[body length_more_than],
%i[body length_less_than],
]
],
}
end

Expand All @@ -41,12 +41,13 @@ def save_to_filesystem
digest = Digest::SHA1.new.update(binary).to_s

loop do
rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i == 0 || rand.length < 4
dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
FileUtils.mkdir_p(dir, mode: 0775)
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
break unless File.file?(path)
rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i.zero? || rand.length < 4

dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
FileUtils.mkdir_p(dir, mode: 0775)
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
break unless File.file?(path)
end

File.open(path, 'wb') { |f| f.write(binary) } unless Rails.env.test?
Expand All @@ -69,50 +70,57 @@ def self.remove_duplicates
start = Time.zone.now.to_f
Rails.logger.info '-----> Removing legal documents duplicates'
count = 0
modified = Array.new
modified = []

LegalDocument.where(documentable_type: "Domain").where.not(checksum: [nil, ""]).find_each do |orig_legal|
LegalDocument.where(documentable_type: 'Domain')
.where.not(checksum: [nil, ''])
.find_each do |orig_legal|
next if modified.include?(orig_legal.checksum)
next if !File.exist?(orig_legal.path)
next unless File.exist?(orig_legal.path)

modified.push(orig_legal.checksum)

LegalDocument.where(documentable_type: "Domain", documentable_id: orig_legal.documentable_id).
where(checksum: orig_legal.checksum).
where.not(id: orig_legal.id).where.not(path: orig_legal.path).each do |new_legal|
unless modified.include?(orig_legal.id)
File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
Rails.logger.info "File #{new_legal.path} has been removed by Domain "\
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
end
LegalDocument.where(documentable_type: 'Domain', documentable_id: orig_legal.documentable_id)
.where(checksum: orig_legal.checksum)
.where.not(id: orig_legal.id)
.where.not(path: orig_legal.path).each do |new_legal|
next if modified.include?(orig_legal.id)

File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
Rails.logger.info "File #{new_legal.path} has been removed by Domain "\
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
end

contact_ids = Version::DomainVersion.where(item_id: orig_legal.documentable_id).distinct.
pluck("object->>'registrant_id'", "object_changes->>'registrant_id'",
"children->>'tech_contacts'", "children->>'admin_contacts'").flatten.uniq
contact_ids = contact_ids.map{|id|
contact_ids = Version::DomainVersion.where(item_id: orig_legal.documentable_id).distinct
.pluck("object->>'registrant_id'",
"object_changes->>'registrant_id'",
"children->>'tech_contacts'",
"children->>'admin_contacts'")
.flatten.uniq
contact_ids = contact_ids.map do |id|
case id
when Hash
id["id"]
when String
JSON.parse(id) rescue id.to_i
else
id
end
}.flatten.compact.uniq
LegalDocument.where(documentable_type: "Contact", documentable_id: contact_ids).
where(checksum: orig_legal.checksum).where.not(path: orig_legal.path).each do |new_legal|
unless modified.include?(orig_legal.id)
File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
Rails.logger.info "File #{new_legal.path} has been removed by Contact "\
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
when Hash
id['id']
when String
JSON.parse(id) rescue id.to_i
else
id
end
end.flatten.compact.uniq
LegalDocument.where(documentable_type: 'Contact', documentable_id: contact_ids)
.where(checksum: orig_legal.checksum)
.where.not(path: orig_legal.path).each do |new_legal|
next if modified.include?(orig_legal.id)

File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
Rails.logger.info "File #{new_legal.path} has been removed by Contact "\
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
end
end
Rails.logger.info "-----> Duplicates fixed for #{count} rows in #{(Time.zone.now.to_f - start).round(2)} seconds"

end
end
12 changes: 12 additions & 0 deletions app/views/epp/sample_requests/contact/check.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="epp-ee">
<command>
<check>
<contact:check
xmlns:contact="contact-ee">
<contact:id>sh8013</contact:id>
</contact:check>
</check>
<clTRID>ABC-12345</clTRID>
</command>
</epp>
14 changes: 14 additions & 0 deletions app/views/epp/sample_requests/contact/check_multiple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="epp-ee">
<command>
<check>
<contact:check
xmlns:contact="contact-ee">
<contact:id>sh8013</contact:id>
<contact:id>sh13</contact:id>
<contact:id>vsdfvq</contact:id>
</contact:check>
</check>
<clTRID>ABC-12345</clTRID>
</command>
</epp>
32 changes: 32 additions & 0 deletions app/views/epp/sample_requests/contact/create.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="epp-ee">
<command>
<create>
<contact:create xmlns:contact="contact-ee">
<contact:postalInfo>
<contact:name>Sillius Soddus</contact:name>
<contact:addr>
<contact:street>123 Example Dr.</contact:street>
<contact:street>Suite 100</contact:street>
<contact:street/>
<contact:city>Megaton</contact:city>
<contact:sp>F3 </contact:sp>
<contact:pc>201-33</contact:pc>
<contact:cc>EE</contact:cc>
</contact:addr>
</contact:postalInfo>
<contact:voice>+372.1234567</contact:voice>
<contact:email>[email protected]</contact:email>
</contact:create>
</create>
<extension>
<eis:extdata xmlns:eis="eis">
<eis:ident type="org" cc="EE">123</eis:ident>
<eis:legalDocument type="pdf">
dGVzdCBmYWlsCg==
</eis:legalDocument>
</eis:extdata>
</extension>
<clTRID>ABC-12345</clTRID>
</command>
</epp>
22 changes: 22 additions & 0 deletions app/views/epp/sample_requests/contact/delete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="epp-ee">
<command>
<delete>
<contact:delete
xmlns:contact="contact-ee">
<contact:id>sh8013</contact:id>
<contact:authInfo>
<contact:pw>wrong-one</contact:pw>
</contact:authInfo>
</contact:delete>
</delete>
<extension>
<eis:extdata xmlns:eis="eis">
<eis:legalDocument type="pdf">
dGVzdCBmYWlsCg==
</eis:legalDocument>
</eis:extdata>
</extension>
<clTRID>ABC-12345</clTRID>
</command>
</epp>
14 changes: 14 additions & 0 deletions app/views/epp/sample_requests/contact/info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="epp-ee">
<command>
<info>
<contact:info xmlns:contact="contact-ee">
<contact:id>sh8013</contact:id>
<contact:authInfo>
<contact:pw>Aas34fq</contact:pw>
</contact:authInfo>
</contact:info>
</info>
<clTRID>ABC-12345</clTRID>
</command>
</epp>
Loading

0 comments on commit 553339f

Please sign in to comment.