Add DomainResource class to wrap MX lookup/normalize (#32864)
				
					
				
			This commit is contained in:
		
							parent
							
								
									e8b6607ece
								
							
						
					
					
						commit
						62d65504f6
					
				| @ -58,10 +58,7 @@ module Admin | |||||||
|     private |     private | ||||||
| 
 | 
 | ||||||
|     def set_resolved_records |     def set_resolved_records | ||||||
|       Resolv::DNS.open do |dns| |       @resolved_records = DomainResource.new(@email_domain_block.domain).mx | ||||||
|         dns.timeouts = 5 |  | ||||||
|         @resolved_records = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a |  | ||||||
|       end |  | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     def resource_params |     def resource_params | ||||||
|  | |||||||
							
								
								
									
										22
									
								
								app/lib/domain_resource.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/lib/domain_resource.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | |||||||
|  | # frozen_string_literal: true | ||||||
|  | 
 | ||||||
|  | class DomainResource | ||||||
|  |   attr_reader :domain | ||||||
|  | 
 | ||||||
|  |   RESOLVE_TIMEOUT = 5 | ||||||
|  | 
 | ||||||
|  |   def initialize(domain) | ||||||
|  |     @domain = domain | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def mx | ||||||
|  |     Resolv::DNS.open do |dns| | ||||||
|  |       dns.timeouts = RESOLVE_TIMEOUT | ||||||
|  |       dns | ||||||
|  |         .getresources(domain, Resolv::DNS::Resource::IN::MX) | ||||||
|  |         .to_a | ||||||
|  |         .map { |mx| mx.exchange.to_s } | ||||||
|  |         .compact_blank | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
| @ -457,13 +457,7 @@ class User < ApplicationRecord | |||||||
| 
 | 
 | ||||||
|     # Doing this conditionally is not very satisfying, but this is consistent |     # Doing this conditionally is not very satisfying, but this is consistent | ||||||
|     # with the MX records validations we do and keeps the specs tractable. |     # with the MX records validations we do and keeps the specs tractable. | ||||||
|     unless self.class.skip_mx_check? |     records = DomainResource.new(domain).mx unless self.class.skip_mx_check? | ||||||
|       Resolv::DNS.open do |dns| |  | ||||||
|         dns.timeouts = 5 |  | ||||||
| 
 |  | ||||||
|         records = dns.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }.compact_blank |  | ||||||
|       end |  | ||||||
|     end |  | ||||||
| 
 | 
 | ||||||
|     EmailDomainBlock.requires_approval?(records + [domain], attempt_ip: sign_up_ip) |     EmailDomainBlock.requires_approval?(records + [domain], attempt_ip: sign_up_ip) | ||||||
|   end |   end | ||||||
|  | |||||||
| @ -30,12 +30,12 @@ | |||||||
|             %label.batch-table__row__select.batch-table__row__select--aligned.batch-checkbox |             %label.batch-table__row__select.batch-table__row__select--aligned.batch-checkbox | ||||||
|               = f.input_field :other_domains, |               = f.input_field :other_domains, | ||||||
|                               as: :boolean, |                               as: :boolean, | ||||||
|                               checked_value: record.exchange.to_s, |                               checked_value: record, | ||||||
|                               include_hidden: false, |                               include_hidden: false, | ||||||
|                               multiple: true |                               multiple: true | ||||||
|             .batch-table__row__content.pending-account |             .batch-table__row__content.pending-account | ||||||
|               .pending-account__header |               .pending-account__header | ||||||
|                 %samp= record.exchange.to_s |                 %samp= record | ||||||
|                 %br |                 %br | ||||||
|                 = t('admin.email_domain_blocks.dns.types.mx') |                 = t('admin.email_domain_blocks.dns.types.mx') | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -45,12 +45,7 @@ module Mastodon::CLI | |||||||
|         end |         end | ||||||
| 
 | 
 | ||||||
|         other_domains = [] |         other_domains = [] | ||||||
|         if options[:with_dns_records] |         other_domains = DomainResource.new(domain).mx if options[:with_dns_records] | ||||||
|           Resolv::DNS.open do |dns| |  | ||||||
|             dns.timeouts = 5 |  | ||||||
|             other_domains = dns.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }.compact_blank |  | ||||||
|           end |  | ||||||
|         end |  | ||||||
| 
 | 
 | ||||||
|         email_domain_block = EmailDomainBlock.new(domain: domain, other_domains: other_domains) |         email_domain_block = EmailDomainBlock.new(domain: domain, other_domains: other_domains) | ||||||
|         email_domain_block.save! |         email_domain_block.save! | ||||||
|  | |||||||
							
								
								
									
										19
									
								
								spec/lib/domain_resource_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								spec/lib/domain_resource_spec.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | |||||||
|  | # frozen_string_literal: true | ||||||
|  | 
 | ||||||
|  | require 'rails_helper' | ||||||
|  | 
 | ||||||
|  | RSpec.describe DomainResource do | ||||||
|  |   describe '#mx' do | ||||||
|  |     subject { described_class.new(domain) } | ||||||
|  | 
 | ||||||
|  |     let(:domain) { 'example.host' } | ||||||
|  |     let(:exchange) { 'mx.host' } | ||||||
|  | 
 | ||||||
|  |     before { configure_mx(domain: domain, exchange: exchange) } | ||||||
|  | 
 | ||||||
|  |     it 'returns array of hostnames' do | ||||||
|  |       expect(subject.mx) | ||||||
|  |         .to eq([exchange]) | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user