Sha256: 96255911cfd4d7a4981d2a58d2ebcb0f1c7e845a1d90479595a3d9045c71067e

Contents?: true

Size: 1.64 KB

Versions: 12

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require "dnsruby"
require "public_suffix"
require "github-pages-health-check/resolver"

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

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

        @host = host
        @nameservers = nameservers
      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
        return @records if defined?(@records)

        @records = get_caa_records(host)
        @records = get_caa_records(parent_host) if @records.nil? || @records.empty?

        @records
      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)
        resolver(domain).query(Dnsruby::Types::CAA)
      rescue Dnsruby::ResolvError, Dnsruby::ResolvTimeout => e
        @error = e
        []
      end

      def resolver(domain)
        GitHubPages::HealthCheck::Resolver.new(domain, :nameservers => nameservers)
      end

      def parent_host
        host.split(".").drop(1).join(".")
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
github-pages-health-check-1.18.5 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.18.4 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.18.2 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.18.1 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.18.0 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.17.9 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.17.8 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.17.7 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.17.6 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.17.2 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.17.1 lib/github-pages-health-check/caa.rb
github-pages-health-check-1.17.0 lib/github-pages-health-check/caa.rb