Skip to content

Commit

Permalink
Fixes #29450 - Use sp-references in used taxonomy lookup
Browse files Browse the repository at this point in the history
Before we relied only on Puppet and Puppet CA proxy features and
searched for hosts which were linked against the proxy in question using
host.puppet_proxy_id and host.puppet_ca_proxy_id fields. When the proxy
didn't have any of these features, the search was unbounded. This meant
we took all the hosts and taxonomy ids from them. This lead to proxy
seemingly being assigned to taxonomies when it really wasn't and not
being able to remove the proxy from such taxonomies.

After this change, we leverage the smart proxy references framework to
really look for hosts reference a given proxy in any way.
  • Loading branch information
adamruzicka authored and ofedoren committed Jun 12, 2024
1 parent 2216a14 commit 94b3416
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/models/concerns/smart_proxy_host_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module SmartProxyHostExtensions
extend ActiveSupport::Concern

included do
scope :with_smart_proxies, -> { eager_load(ProxyReferenceRegistry.smart_proxy_references.select(&:join?).map(&:join_relation)) }
end

module ClassMethods
# registers a reference to a proxy
def smart_proxy_reference(hash)
ProxyReferenceRegistry.add_smart_proxy_reference hash
end

def smart_proxy_ids(hosts_scope = Host::Managed.all)
hosts_scope.eager_load(proxy_join_tables).pluck(proxy_column_list).flatten.uniq.compact
hosts_scope.with_smart_proxies.pluck(proxy_column_list).flatten.uniq.compact
end

# table names containing reference to a proxy
Expand Down
1 change: 1 addition & 0 deletions app/models/host/managed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def build_hooks
smart_proxy_reference :domain => [:dns_id]
smart_proxy_reference :realm => [:realm_proxy_id]
smart_proxy_reference :self => [:puppet_proxy_id, :puppet_ca_proxy_id]
smart_proxy_reference :infrastructure_facet => [:smart_proxy_id]

graphql_type '::Types::Host'

Expand Down
7 changes: 7 additions & 0 deletions app/models/smart_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def ping
!errors.any?
end

def used_taxonomy_ids(type)
return [] if new_record? || !respond_to?(:hosts)

conditions = "#{id} IN (#{Host::Managed.proxy_column_list})"
::Host::Managed.with_smart_proxies.where(conditions).distinct.pluck(type).compact
end

def taxonomy_foreign_conditions
conditions = {}
if has_feature?('Puppet') && has_feature?('Puppet CA')
Expand Down
4 changes: 4 additions & 0 deletions test/unit/concerns/smart_proxy_host_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class SmartProxyHostExtensionsTest < ActiveSupport::TestCase
ProxyReferenceRegistry.references = nil

class ProxyReferrer
# SmartProxyHostExtensions is expected to be included into a ActiveRecord model
def self.scope(...)
end

include SmartProxyHostExtensions
smart_proxy_reference :test_reference => [:test]
smart_proxy_reference :test_reference => [:another_test]
Expand Down

0 comments on commit 94b3416

Please sign in to comment.