I have some code which looks up domains in a database via DNS to get the SPF and DMARC records - the code works flawlessly on cloud9, but when deployed to an EC2 instance on AWS (created using Op works) the same code throws the error below - I am now stuck, I have tried everything to ensure everything is UTF-8 encoded. Mysql DB is definitely utf-8 encoded and I have tried to force utf-8 encoding of the variables as you can see form the code - maybe I am not understanding the error message properly?
Code
<h1>Domain Update scan</h1>
<p>RUNNING</p>
<% Resolv::DNS.open do |dns| %>
<% @domains.each do |domain| %>
<% host = domain.domain %>
<% host.force_encoding('UTF-8') %>
<% records = dns.getresources(host, Resolv::DNS::Resource::IN::TXT) %>
<% records.each do |record| %>
<% txt = record.strings.join %>
<% txt.force_encoding('UTF-8') %>
<% if txt.include?('v=spf1') %>
<% domain.update_attribute(:spf, txt) %>
<% domain.save %>
<% end %>
<% end %>
<% hostd = "_dmarc.#{host}" %>
<% recordds = dns.getresources(hostd, Resolv::DNS::Resource::IN::TXT) %>
<% recordds.each do |recordd| %>
<% txtd = recordd.strings.join %>
<% txtd.force_encoding('UTF-8') %>
<% if txtd.include?('v=DMARC1;')%>
<% domain.update_attribute(:dmarc, txtd) %>
<% domain.save %>
<% end %>
<% end %>
<% end %>
<% end %>
<p>Finished</p>
<%= link_to 'Show Domain', domains_path %>
<%= link_to 'Run Scan', domains_scans_path %>
ERROR MESSAGE FROM EC2
I, [2017-10-21T16:02:49.832304 #6039] INFO -- : Started GET "/domains/scanonedomain" for 84.92.56.31 at 2017-10-21 16:02:49 +0000
I, [2017-10-21T16:02:49.834128 #6039] INFO -- : Processing by DomainsController#scanonedomain as HTML
D, [2017-10-21T16:02:49.835289 #6039] DEBUG -- : User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
D, [2017-10-21T16:02:49.837043 #6039] DEBUG -- : Domain Load (0.7ms) SELECT `domains`.* FROM `domains`
I, [2017-10-21T16:02:49.845188 #6039] INFO -- : Rendered domains/scanonedomain.html.erb within layouts/application (9.0ms)
I, [2017-10-21T16:02:49.845440 #6039] INFO -- : Completed 500 Internal Server Error in 11ms (ActiveRecord: 1.1ms)
F, [2017-10-21T16:02:49.846542 #6039] FATAL -- :
ActionView::Template::Error (incompatible character encodings: UTF-8 and ASCII-8BIT):
5: <% @domains.each do |domain| %>
6: <% host = domain.domain %>
7: <% host.force_encoding('UTF-8') %>
8: <% records = dns.getresources(host, Resolv::DNS::Resource::IN::TXT) %>
9: <% records.each do |record| %>
10: <% txt = record.strings.join %>
11: <% txt.force_encoding('UTF-8') %>
app/views/domains/scanonedomain.html.erb:8:in `block (2 levels) in _app_views_domains_scanonedomain_html_erb___1736845507882839174_20151860'
app/views/domains/scanonedomain.html.erb:5:in `block in _app_views_domains_scanonedomain_html_erb___1736845507882839174_20151860'
app/views/domains/scanonedomain.html.erb:4:in `_app_views_domains_scanonedomain_html_erb___1736845507882839174_20151860'