Files
ubicloud/prog/resolve_globally_blocked_dnsnames.rb
Furkan Sahin d3b4b70193 Add prog to update ip addresses of globally blocked dns names
This commit introduces a new prog that will periodically check the ip
addresses of globally blocked dnsnames. This way, the list will be kept
fresh and the new VMs will be provisioned with the new list. In future,
we might introduce a system here to trigger a firewall rule update for
existing VMs as well.
2024-03-13 10:50:40 +01:00

28 lines
720 B
Ruby

# frozen_string_literal: true
require "socket"
require "open-uri"
require "net/http"
class Prog::ResolveGloballyBlockedDnsnames < Prog::Base
label def wait
GloballyBlockedDnsname.each do |globally_blocked_dnsname|
dns_name = globally_blocked_dnsname.dns_name
begin
addr_info = Socket.getaddrinfo(dns_name, nil)
rescue SocketError
Clog.emit("Failed to resolve blocked dns name") { {dns_name: dns_name} }
next
end
ip_list = addr_info.map do |info|
info[3]
end.uniq
globally_blocked_dnsname.update(ip_list: Sequel.lit("ARRAY[#{ip_list.map { |ip| "'#{ip}'::inet" }.join(",")}]"), last_check_at: Time.now)
end
nap 60 * 60
end
end