Skip to content

Instantly share code, notes, and snippets.

@rdubya

rdubya/base.rb Secret

Created July 30, 2019 12:56
Show Gist options
  • Save rdubya/e18e1860b9482a20d7c11e494b56e8ee to your computer and use it in GitHub Desktop.
Save rdubya/e18e1860b9482a20d7c11e494b56e8ee to your computer and use it in GitHub Desktop.
private method unable to be called from child class
module Services
module Cloudflare
# Interacts with the Cloudflare gem to manage custom hostnames
class Base
# @param [String] hostname the domain name to work with
def initialize
@tenant = ClientInfo.id # We have to cache this because the calls happen in other threads
end
private
# Override this to supply extra data when requests fail
# @return [Hash]
def error_params
{}
end
# Helper for dealing with the asynchronous calls
# @param [Proc] block the work that should be done with the connection
# @return [Object] the result of the work done
# @raise [StandardError] any errors that happen inside the block are passed along
def with_connection(&block)
task = ::Cloudflare.connect(Settings.cloudflare.credentials, &block)
result = task.wait
raise result if task.failed?
result
rescue => e
NewRelic::Agent.notice_error(e, custom_params: error_params.merge(tenant: @tenant))
raise
end
end
end
end
# frozen_string_literal: true
module Services
module Cloudflare
# Interacts with the Cloudflare gem to manage custom hostnames
class Hostname < Base
# @param [String] hostname the domain name to work with
def initialize(hostname, redirect_domain: nil)
super()
@hostname = hostname
@redirect_domain = redirect_domain
end
# Delete the hostname record from cloudflare
def delete
with_connection do |conn|
hostname_record(conn)&.delete
end
end
# Make sure the record exists in cloudflare and is configured correctly
# @return [Cloudflare::CustomHostname] the new record
def ensure_existence_and_correctness
with_connection do |conn| ########################### This line breaks
hostnames = custom_hostnames_representation(conn)
record = hostnames.find_by_hostname(@hostname) # rubocop:disable Rails/DynamicFindBy
if record
# We already have a domain, so just ensure that it has the correct settings
changes = {}
# These checks should happen infrequently (and fail less than that) so it should be ok to build the objects twice
changes[:metadata] = metadata unless record.custom_metadata == metadata
changes[:origin] = origin unless record.custom_origin == origin
record.update_settings(changes) unless changes.empty?
else
record = hostnames.create(@hostname, metadata: metadata, origin: origin)
end
record
end
end
# Get the record from cloudflare
# @return [Cloudflare::CustomHostname|nil] the requested record
def fetch
with_connection(&method(:hostname_record))
end
# Check if the ssl cert is active for this hostname
# @return [Boolean|nil] true if it is activated, false if not, nil if the hostname record does not exist
def ssl_active? # rubocop:disable Metrics/MethodLength
with_connection do |conn|
record = hostname_record(conn)
if record
if record.ssl_active?
true
else
cname = Services::DNSLookup.cname(@hostname)
if cname.present? && cname.downcase == "#{@tenant}.finalsite.com"
sleep(1) # Give DNS an extra second buffer
record.ssl_active?(true)
else
false
end
end
end
end
end
private
# @param [Cloudflare::Connection] conn
def custom_hostnames_representation(conn)
conn.zones.find_by_id(Settings.cloudflare.zone_id).custom_hostnames # rubocop:disable Rails/DynamicFindBy
end
# Record the hostname when a request fails
# @return [Hash]
def error_params
{ hostname: @hostname }
end
# @param [Cloudflare::Connection] conn
# @return [Cloudflare::CustomHostname|nil]
def hostname_record(conn)
custom_hostnames_representation(conn).find_by_hostname(@hostname) # rubocop:disable Rails/DynamicFindBy
end
# The expected metadata stored in cloudflare
def metadata
md = { dc: Settings.cluster_name, tenant_name: @tenant }
md[:redirect_domain] = @redirect_domain if @redirect_domain
md
end
# The origin that should be set for this hostname
def origin
"#{Settings.cluster_name}.#{Settings.cloudflare.origin_base_domain}"
end
end
end
end
@rdubya
Copy link
Author

rdubya commented Jul 30, 2019

The issue happens on line 27 of hostname.rb. The way it is it gives a method not defined error. If I put "self." in front of it it complains that I'm trying to call a private method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment