Sha256: 05807394607d12efb1a1325e7a6175ec24e9e5f4c2106ab8a428b130882d89e1

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require "dnsruby"
require "public_suffix"

module GitHubPages
  module HealthCheck
    class CAA
      attr_reader :host
      attr_reader :error

      def initialize(host)
        raise ArgumentError, "host cannot be nil" if host.nil?

        @host = host
      end

      def errored?
        records # load the records first
        !error.nil?
      end

      def lets_encrypt_allowed?
        return false if errored?
        return true unless records_present?
        records.any? { |r| r.property_value == "letsencrypt.org" }
      end

      def records_present?
        return false if errored?
        records && !records.empty?
      end

      def records
        @records ||= (get_caa_records(host) | get_caa_records(PublicSuffix.domain(host)))
      end

      private

      def get_caa_records(domain)
        return [] if domain.nil?
        query(domain).select { |r| issue_caa_record?(r) }
      end

      def issue_caa_record?(record)
        record.type == Dnsruby::Types::CAA && record.property_tag == "issue"
      end

      def query(domain)
        GitHubPages::HealthCheck::Resolver.new(domain).query(Dnsruby::Types::CAA)
      rescue Dnsruby::ResolvError, Dnsruby::ResolvTimeout => e
        @error = e
        []
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
github-pages-health-check-1.9.0 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.8.1 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.8.0 lib/github-pages-health-check/caa.rb