Merge remote-tracking branch 'origin/master' into 1763-registrar-bulk-renew

This commit is contained in:
Karl Erik Õunapuu 2020-12-21 14:36:49 +02:00
commit 00a65b3b01
No known key found for this signature in database
GPG key ID: C9DD647298A34764
40 changed files with 818 additions and 160 deletions

View file

@ -0,0 +1,30 @@
module Admin
class BouncedMailAddressesController < BaseController
before_action :set_bounced_mail_address, only: %i[show destroy]
load_and_authorize_resource
# GET /bounced_mail_addresses
def index
@bounced_mail_addresses = BouncedMailAddress.all.order(created_at: :desc)
end
# GET /bounced_mail_addresses/1
def show; end
# DELETE /bounced_mail_addresses/1
def destroy
@bounced_mail_address.destroy
redirect_to(
admin_bounced_mail_addresses_url,
notice: 'Bounced mail address was successfully destroyed.'
)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_bounced_mail_address
@bounced_mail_address = BouncedMailAddress.find(params[:id])
end
end
end

View file

@ -10,6 +10,11 @@ module Api
head :unauthorized unless ip_allowed
end
def authenticate_shared_key
api_key = "Basic #{ENV['api_shared_key']}"
head(:unauthorized) unless api_key == request.authorization
end
def not_found_error
uuid = params['uuid']
json = { error: 'Not Found', uuid: uuid, message: 'Record not found' }

View file

@ -0,0 +1,25 @@
module Api
module V1
class BouncesController < BaseController
before_action :authenticate_shared_key
# POST api/v1/bounces/
def create
return head(:bad_request) unless bounce_params[:bounce][:bouncedRecipients].any?
BouncedMailAddress.record(bounce_params)
head(:created)
rescue ActionController::ParameterMissing
head(:bad_request)
end
def bounce_params
params.require(:data).require(:bounce).require(:bouncedRecipients).each do |r|
r.require(:emailAddress)
end
params.require(:data)
end
end
end
end

View file

@ -19,15 +19,16 @@ module Api
end
contacts = current_user_contacts.limit(limit).offset(offset)
serialized_contacts = contacts.collect { |contact| serialize_contact(contact) }
serialized_contacts = contacts.collect { |contact| serialize_contact(contact, false) }
render json: serialized_contacts
end
def show
contact = current_user_contacts.find_by(uuid: params[:uuid])
links = params[:links] == 'true'
if contact
render json: serialize_contact(contact)
render json: serialize_contact(contact, links)
else
render json: { errors: [{ base: ['Contact not found'] }] }, status: :not_found
end
@ -85,7 +86,7 @@ module Api
contact.registrar.notify(action)
end
render json: serialize_contact(contact)
render json: serialize_contact(contact, false)
end
private
@ -96,8 +97,8 @@ module Api
current_registrant_user.direct_contacts
end
def serialize_contact(contact)
Serializers::RegistrantApi::Contact.new(contact).to_json
def serialize_contact(contact, links)
Serializers::RegistrantApi::Contact.new(contact, links).to_json
end
end
end

View file

@ -7,6 +7,7 @@ module Api
def index
limit = params[:limit] || 200
offset = params[:offset] || 0
simple = params[:simple] == 'true' || false
if limit.to_i > 200 || limit.to_i < 1
render(json: { errors: [{ limit: ['parameter is out of range'] }] },
@ -18,21 +19,20 @@ module Api
status: :bad_request) && return
end
@domains = current_user_domains.limit(limit).offset(offset)
serialized_domains = @domains.map do |item|
serializer = Serializers::RegistrantApi::Domain.new(item)
domains = current_user_domains
serialized_domains = domains.limit(limit).offset(offset).map do |item|
serializer = Serializers::RegistrantApi::Domain.new(item, simplify: simple)
serializer.to_json
end
render json: serialized_domains
render json: { count: domains.count, domains: serialized_domains }
end
def show
@domain = current_user_domains.find_by(uuid: params[:uuid])
if @domain
serializer = Serializers::RegistrantApi::Domain.new(@domain)
serializer = Serializers::RegistrantApi::Domain.new(@domain, simplify: false)
render json: serializer.to_json
else
render json: { errors: [{ base: ['Domain not found'] }] }, status: :not_found

View file

@ -62,6 +62,7 @@ class Registrar
def find_user_by_idc_and_allowed(idc)
return User.new unless idc
possible_users = ApiUser.where(identity_code: idc) || User.new
possible_users.each do |selected_user|
if selected_user.registrar.white_ips.registrar_area.include_ip?(request.ip)

View file

@ -0,0 +1,53 @@
module Domains
module DeleteConfirm
class Base < ActiveInteraction::Base
object :domain,
class: Domain,
description: 'Domain to confirm release'
string :action
string :initiator,
default: nil
validates :domain, :action, presence: true
validates :action, inclusion: { in: [RegistrantVerification::CONFIRMED,
RegistrantVerification::REJECTED] }
def raise_errors!(domain)
return unless domain.errors.any?
message = "domain #{domain.name} failed with errors #{domain.errors.full_messages}"
throw message
end
def notify_registrar(message_key)
domain.registrar.notifications.create!(
text: "#{I18n.t(message_key)}: #{domain.name}",
attached_obj_id: domain.id,
attached_obj_type: domain.class.to_s
)
end
def preclean_pendings
domain.registrant_verification_token = nil
domain.registrant_verification_asked_at = nil
end
def clean_pendings!
domain.is_admin = true
domain.registrant_verification_token = nil
domain.registrant_verification_asked_at = nil
domain.pending_json = {}
clear_statuses
domain.save
end
def clear_statuses
domain.statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION)
domain.statuses.delete(DomainStatus::PENDING_UPDATE)
domain.statuses.delete(DomainStatus::PENDING_DELETE)
domain.status_notes[DomainStatus::PENDING_UPDATE] = ''
domain.status_notes[DomainStatus::PENDING_DELETE] = ''
end
end
end
end

View file

@ -0,0 +1,17 @@
module Domains
module DeleteConfirm
class ProcessAction < Base
def execute
::PaperTrail.request.whodunnit = "interaction - #{self.class.name} - #{action} by"\
" #{initiator}"
case action
when RegistrantVerification::CONFIRMED
compose(ProcessDeleteConfirmed, inputs)
when RegistrantVerification::REJECTED
compose(ProcessDeleteRejected, inputs)
end
end
end
end
end

View file

@ -0,0 +1,50 @@
module Domains
module DeleteConfirm
class ProcessDeleteConfirmed < Base
def execute
notify_registrar(:poll_pending_delete_confirmed_by_registrant)
domain.apply_pending_delete!
raise_errors!(domain)
end
def apply_pending_delete!
preclean_pendings
clean_pendings!
DomainDeleteMailer.accepted(domain).deliver_now
domain.set_pending_delete!
end
def set_pending_delete!
unless domain.pending_deletable?
add_epp_error
return
end
domain.delete_date = delete_date
domain.statuses << DomainStatus::PENDING_DELETE
set_server_hold if server_holdable?
domain.save(validate: false)
end
def set_server_hold
domain.statuses << DomainStatus::SERVER_HOLD
domain.outzone_at = Time.current
end
def server_holdable?
return false if domain.statuses.include?(DomainStatus::SERVER_HOLD)
return false if domain.statuses.include?(DomainStatus::SERVER_MANUAL_INZONE)
true
end
def delete_date
Time.zone.today + Setting.redemption_grace_period.days + 1.day
end
def add_epp_error
domain.add_epp_error('2304', nil, nil, I18n.t(:object_status_prohibits_operation))
end
end
end
end

View file

@ -0,0 +1,32 @@
module Domains
module DeleteConfirm
class ProcessDeleteRejected < Base
def execute
domain.cancel_pending_delete
notify_registrar(:poll_pending_delete_rejected_by_registrant)
domain.save(validate: false)
raise_errors!(domain)
send_domain_delete_rejected_email
end
def send_domain_delete_rejected_email
if domain.registrant_verification_token.blank?
warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{domain.name}"
elsif domain.registrant_verification_asked_at.blank?
warn "EMAIL NOT DELIVERED: registrant_verification_asked_at is missing for #{domain.name}"
else
send_email
end
end
def warn(message)
Rails.logger.warn(message)
end
def send_email
DomainDeleteMailer.rejected(domain).deliver_now
end
end
end
end

View file

@ -1,5 +1,5 @@
module Domains
module DeleteConfirm
module DeleteConfirmEmail
class SendRequest < ActiveInteraction::Base
object :domain,
class: Domain,

View file

@ -0,0 +1,48 @@
module Whois
class DeleteRecord < ActiveInteraction::Base
string :name
string :type
validates :type, inclusion: { in: %w[reserved blocked domain disputed zone] }
def execute
send "delete_#{type}", name
end
# 1. deleting own
# 2. trying to regenerate reserved in order domain is still in the list
def delete_domain(name)
WhoisRecord.where(name: name).destroy_all
BlockedDomain.find_by(name: name).try(:generate_data)
ReservedDomain.find_by(name: name).try(:generate_data)
Dispute.active.find_by(domain_name: name).try(:generate_data)
end
def delete_reserved(name)
remove_status_from_whois(domain_name: name, domain_status: 'Reserved')
end
def delete_blocked(name)
delete_reserved(name)
end
def delete_disputed(name)
return if Dispute.active.find_by(domain_name: name).present?
remove_status_from_whois(domain_name: name, domain_status: 'disputed')
end
def delete_zone(name)
WhoisRecord.where(name: name).destroy_all
Whois::Record.where(name: name).destroy_all
end
def remove_status_from_whois(domain_name:, domain_status:)
Whois::Record.where(name: domain_name).each do |r|
r.json['status'] = r.json['status'].delete_if { |status| status == domain_status }
r.json['status'].blank? ? r.destroy : r.save
end
end
end
end

View file

@ -0,0 +1,39 @@
module Whois
class Update < ActiveInteraction::Base
array :names
string :type
validates :type, inclusion: { in: %w[reserved blocked domain disputed zone] }
def execute
::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{type}"
klass = determine_class
Array(names).each do |name|
record = find_record(klass, name)
if record
Whois::UpdateRecord.run(record: record, type: type)
else
Whois::DeleteRecord.run(name: name, type: type)
end
end
end
private
def determine_class
case type
when 'reserved' then ReservedDomain
when 'blocked' then BlockedDomain
when 'domain' then Domain
when 'disputed' then Dispute.active
else DNS::Zone
end
end
def find_record(klass, name)
klass == DNS::Zone ? klass.find_by(origin: name) : klass.find_by(name: name)
end
end
end

View file

@ -0,0 +1,32 @@
module Whois
class UpdateRecord < ActiveInteraction::Base
interface :record
string :type
validates :type, inclusion: { in: %w[reserved blocked domain disputed zone] }
def execute
send "update_#{type}", record
end
def update_domain(domain)
domain.whois_record ? domain.whois_record.save : domain.create_whois_record
end
def update_reserved(record)
record.generate_data
end
def update_blocked(record)
update_reserved(record)
end
def update_disputed(record)
update_reserved(record)
end
def update_zone(record)
update_reserved(record)
end
end
end

View file

@ -1,39 +1,11 @@
class DomainDeleteConfirmJob < Que::Job
def run(domain_id, action, initiator = nil)
::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{action} by #{initiator}"
# it's recommended to keep transaction against job table as short as possible.
ActiveRecord::Base.transaction do
domain = Epp::Domain.find(domain_id)
class DomainDeleteConfirmJob < ApplicationJob
queue_as :default
case action
when RegistrantVerification::CONFIRMED
domain.notify_registrar(:poll_pending_delete_confirmed_by_registrant)
domain.apply_pending_delete!
raise_errors!(domain)
def perform(domain_id, action, initiator = nil)
domain = Epp::Domain.find(domain_id)
when RegistrantVerification::REJECTED
domain.statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION)
domain.notify_registrar(:poll_pending_delete_rejected_by_registrant)
domain.cancel_pending_delete
domain.save(validate: false)
raise_errors!(domain)
if domain.registrant_verification_token.blank?
Rails.logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{domain.name}"
elsif domain.registrant_verification_asked_at.blank?
Rails.logger.warn "EMAIL NOT DELIVERED: registrant_verification_asked_at is missing for #{domain.name}"
else
DomainDeleteMailer.rejected(domain).deliver_now
end
end
destroy # it's best to destroy the job in the same transaction
end
end
def raise_errors!(domain)
throw "domain #{domain.name} failed with errors #{domain.errors.full_messages}" if domain.errors.any?
Domains::DeleteConfirm::ProcessAction.run(domain: domain,
action: action,
initiator: initiator)
end
end

View file

@ -1,87 +1,5 @@
class UpdateWhoisRecordJob < Que::Job
def run(names, type)
::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{type}"
klass = determine_class(type)
Array(names).each do |name|
record = find_record(klass, name)
if record
send "update_#{type}", record
else
send "delete_#{type}", name
end
end
end
def find_record(klass, name)
klass == DNS::Zone ? klass.find_by(origin: name) : klass.find_by(name: name)
end
def determine_class(type)
case type
when 'reserved' then ReservedDomain
when 'blocked' then BlockedDomain
when 'domain' then Domain
when 'disputed' then Dispute.active
when 'zone' then DNS::Zone
end
end
def update_domain(domain)
domain.whois_record ? domain.whois_record.save : domain.create_whois_record
end
def update_reserved(record)
record.generate_data
end
def update_blocked(record)
update_reserved(record)
end
def update_disputed(record)
update_reserved(record)
end
def update_zone(record)
update_reserved(record)
end
# 1. deleting own
# 2. trying to regenerate reserved in order domain is still in the list
def delete_domain(name)
WhoisRecord.where(name: name).destroy_all
BlockedDomain.find_by(name: name).try(:generate_data)
ReservedDomain.find_by(name: name).try(:generate_data)
Dispute.active.find_by(domain_name: name).try(:generate_data)
end
def delete_reserved(name)
remove_status_from_whois(domain_name: name, domain_status: 'Reserved')
end
def delete_blocked(name)
delete_reserved(name)
end
def delete_disputed(name)
return if Dispute.active.find_by(domain_name: name).present?
remove_status_from_whois(domain_name: name, domain_status: 'disputed')
end
def delete_zone(name)
WhoisRecord.where(name: name).destroy_all
Whois::Record.where(name: name).destroy_all
end
def remove_status_from_whois(domain_name:, domain_status:)
Whois::Record.where(name: domain_name).each do |r|
r.json['status'] = r.json['status'].delete_if { |status| status == domain_status }
r.json['status'].blank? ? r.destroy : r.save
end
Whois::Update.run(names: [names].flatten, type: type)
end
end

View file

@ -109,6 +109,7 @@ class Ability
can :destroy, :pending
can :create, :zonefile
can :access, :settings_menu
can :manage, BouncedMailAddress
end
def static_registrant

View file

@ -0,0 +1,28 @@
class BouncedMailAddress < ApplicationRecord
validates :email, :message_id, :bounce_type, :bounce_subtype, :action, :status, presence: true
def bounce_reason
"#{action} (#{status} #{diagnostic})"
end
def self.record(json)
bounced_records = json['bounce']['bouncedRecipients']
bounced_records.each do |record|
bounce_record = BouncedMailAddress.new(params_from_json(json, record))
bounce_record.save
end
end
def self.params_from_json(json, bounced_record)
{
email: bounced_record['emailAddress'],
message_id: json['mail']['messageId'],
bounce_type: json['bounce']['bounceType'],
bounce_subtype: json['bounce']['bounceSubType'],
action: bounced_record['action'],
status: bounced_record['status'],
diagnostic: bounced_record['diagnosticCode'],
}
end
end

View file

@ -347,19 +347,24 @@ class Contact < ApplicationRecord
@desc = {}
registrant_domains.each do |dom|
@desc[dom.name] ||= []
@desc[dom.name] << :registrant
@desc[dom.name] ||= { id: dom.uuid, roles: [] }
@desc[dom.name][:roles] << :registrant
end
domain_contacts.each do |dc|
@desc[dc.domain.name] ||= []
@desc[dc.domain.name] << dc.name.downcase.to_sym
@desc[dc.domain.name] ||= { id: dc.domain.uuid, roles: [] }
@desc[dc.domain.name][:roles] << dc.name.downcase.to_sym
@desc[dc.domain.name] = @desc[dc.domain.name].compact
end
@desc
end
def related_domains
a = related_domain_descriptions
a.keys.map { |d| { name: d, id: a[d][:id], roles: a[d][:roles] } }
end
def status_notes_array=(notes)
self.status_notes = {}
notes ||= []

View file

@ -421,7 +421,7 @@ class Domain < ApplicationRecord
pending_delete_confirmation!
save(validate: false) # should check if this did succeed
Domains::DeleteConfirm::SendRequest.run(domain: self)
Domains::DeleteConfirmEmail::SendRequest.run(domain: self)
end
def cancel_pending_delete

View file

@ -30,12 +30,12 @@ class RegistrantVerification < ApplicationRecord
def domain_registrant_delete_confirm!(initiator)
self.action_type = DOMAIN_DELETE
self.action = CONFIRMED
DomainDeleteConfirmJob.enqueue domain.id, CONFIRMED, initiator if save
DomainDeleteConfirmJob.perform_later domain.id, CONFIRMED, initiator if save
end
def domain_registrant_delete_reject!(initiator)
self.action_type = DOMAIN_DELETE
self.action = REJECTED
DomainDeleteConfirmJob.enqueue domain.id, REJECTED, initiator if save
DomainDeleteConfirmJob.perform_later domain.id, REJECTED, initiator if save
end
end

View file

@ -33,6 +33,7 @@
%li= link_to t('.blocked_domains'), admin_blocked_domains_path
%li= link_to t('.reserved_domains'), admin_reserved_domains_path
%li= link_to t('.disputed_domains'), admin_disputes_path
%li= link_to t('.bounced_email_addresses'), admin_bounced_mail_addresses_path
%li= link_to t('.epp_log'), admin_epp_logs_path(created_after: 'today')
%li= link_to t('.repp_log'), admin_repp_logs_path(created_after: 'today')
%li= link_to t('.que'), '/admin/que'

View file

@ -0,0 +1,36 @@
<h1>Bounced Mail Addresses</h1>
<div class="row">
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-hover table-bordered table-condensed">
<thead>
<tr>
<th>Email</th>
<th>Action</th>
<th>Status</th>
<th>Diagnostic</th>
<th>Tracked</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
<% @bounced_mail_addresses.each do |mail_addr| %>
<tr>
<td><%= mail_addr.email %></td>
<td><%= mail_addr.action %></td>
<td><%= mail_addr.status %></td>
<td><%= mail_addr.diagnostic %></td>
<td><%= mail_addr.created_at %></td>
<td><%= link_to 'Detailed', admin_bounced_mail_address_path(mail_addr) %></td>
<td><%= link_to 'Destroy', admin_bounced_mail_address_path(mail_addr), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>

View file

@ -0,0 +1,27 @@
<p>
<strong>Email:</strong>
<%= @bounced_mail_address.email %>
</p>
<p>
<strong>Bounced message ID:</strong>
<%= @bounced_mail_address.message_id %>
</p>
<p>
<strong>Overall bounce type:</strong>
<%= @bounced_mail_address.bounce_type %> (<%= @bounced_mail_address.bounce_subtype %> )
</p>
<p>
<strong>Bounced recipient status:</strong>
<%= @bounced_mail_address.action %> (<%= @bounced_mail_address.status %>)
</p>
<p>
<strong>Bounced recipient diagnostic:</strong>
<pre><%= @bounced_mail_address.diagnostic %></pre>
</p>
<%= link_to 'Back', admin_bounced_mail_addresses_path %>
<%= link_to 'Destroy', admin_bounced_mail_address_path(@bounced_mail_address), method: :delete, data: { confirm: 'Are you sure?' } %></td>