Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

(FACT-2330) Add ssh fact for Windows OpenSSH feature #424

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 213 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-04-13 17:31:41 +0300 using RuboCop version 0.74.0.
# `rubocop --auto-gen-config --exclude-limit 1000`
# on 2020-04-15 10:09:42 +0300 using RuboCop version 0.74.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -9,7 +9,87 @@
# Offense count: 80
# Configuration parameters: CustomTransform, IgnoreMethods.
RSpec/FilePath:
Enabled: false
Exclude:
- 'spec/custom_facts/core/aggregate_spec.rb'
- 'spec/custom_facts/core/directed_graph_spec.rb'
- 'spec/custom_facts/core/execution/fact_manager_spec.rb'
- 'spec/custom_facts/core/execution/posix_spec.rb'
- 'spec/custom_facts/core/execution/windows_spec.rb'
- 'spec/custom_facts/core/execution_spec.rb'
- 'spec/custom_facts/core/logging_spec.rb'
- 'spec/custom_facts/core/resolvable_spec.rb'
- 'spec/custom_facts/core/suitable_spec.rb'
- 'spec/custom_facts/util/collection_spec.rb'
- 'spec/custom_facts/util/config_spec.rb'
- 'spec/custom_facts/util/confine_spec.rb'
- 'spec/custom_facts/util/directory_loader_spec.rb'
- 'spec/custom_facts/util/fact_spec.rb'
- 'spec/custom_facts/util/loader_spec.rb'
- 'spec/custom_facts/util/normalization_spec.rb'
- 'spec/custom_facts/util/parser_spec.rb'
- 'spec/custom_facts/util/resolution_spec.rb'
- 'spec/facter/facts_utils/bytes_to_human_readable_spec.rb'
- 'spec/facter/facts_utils/uptime_parser_spec.rb'
- 'spec/facter/facts_utils/windows_release_finder_spec.rb'
- 'spec/facter/model/fact_collection_spec.rb'
- 'spec/facter/model/resolved_fact_spec.rb'
- 'spec/facter/resolvers/aio_agent_version_spec.rb'
- 'spec/facter/resolvers/aix/architecture_resolver_spec.rb'
- 'spec/facter/resolvers/aix/ffi_helper_spec.rb'
- 'spec/facter/resolvers/aix/hardware_resolver_spec.rb'
- 'spec/facter/resolvers/disk_resolver_spec.rb'
- 'spec/facter/resolvers/dmi_resolver_spec.rb'
- 'spec/facter/resolvers/filesystems_resolver_spec.rb'
- 'spec/facter/resolvers/fips_enabled_resolver_spec.rb'
- 'spec/facter/resolvers/identity_resolver_spec.rb'
- 'spec/facter/resolvers/load_averages_resolver_spec.rb'
- 'spec/facter/resolvers/macosx/dmi_resolver_spec.rb'
- 'spec/facter/resolvers/memory_resolver_spec.rb'
- 'spec/facter/resolvers/mountpoints_resolver_spec.rb'
- 'spec/facter/resolvers/processors_resolver_spec.rb'
- 'spec/facter/resolvers/redhat_release_resolver_spec.rb'
- 'spec/facter/resolvers/selinux_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/current_zone_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/solaris_release_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/zone_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/zpool_resolver_spec.rb'
- 'spec/facter/resolvers/suse_relese_resolver_spec.rb'
- 'spec/facter/resolvers/system_profile_resolver_spec.rb'
- 'spec/facter/resolvers/utils/aix/odm_query_spec.rb'
- 'spec/facter/resolvers/utils/macosx/filesystem_helper_spec.rb'
- 'spec/facter/resolvers/utils/windows/win32ole_spec.rb'
- 'spec/facter/resolvers/windows/dmi_bios_resolver_spec.rb'
- 'spec/facter/resolvers/windows/dmi_computersystem_resolver_spec.rb'
- 'spec/facter/resolvers/windows/hardware_architecture_resolver_spec.rb'
- 'spec/facter/resolvers/windows/identity_resolver_spec.rb'
- 'spec/facter/resolvers/windows/kernel_resolver_spec.rb'
- 'spec/facter/resolvers/windows/memory_resolver_spec.rb'
- 'spec/facter/resolvers/windows/netkvm_resolver_spec.rb'
- 'spec/facter/resolvers/windows/networking_resolver_spec.rb'
- 'spec/facter/resolvers/windows/processors_resolver_spec.rb'
- 'spec/facter/resolvers/windows/product_release_resolver_spec.rb'
- 'spec/facter/resolvers/windows/system32_resolver_spec.rb'
- 'spec/facter/resolvers/windows/virtualization_resolver_spec.rb'
- 'spec/facter/resolvers/windows/win_os_description_resolver_spec.rb'
- 'spec/framework/config/block_list_spec.rb'
- 'spec/framework/config/config_reader_spec.rb'
- 'spec/framework/core/fact/external/external_fact_manager_spec.rb'
- 'spec/framework/core/fact/internal/internal_fact_manager_spec.rb'
- 'spec/framework/core/fact_loaders/class_discoverer_spec.rb'
- 'spec/framework/core/fact_loaders/external_fact_loader_spec.rb'
- 'spec/framework/core/fact_loaders/fact_loader_spec.rb'
- 'spec/framework/core/fact_loaders/internal_fact_loader_spec.rb'
- 'spec/framework/core/fact_manager_spec.rb'
- 'spec/framework/core/options/option_store_spec.rb'
- 'spec/framework/core/options/options_validator_spec.rb'
- 'spec/framework/core/options_spec.rb'
- 'spec/framework/core/session_cache_spec.rb'
- 'spec/framework/formatters/fact_formatter_spec.rb'
- 'spec/framework/formatters/hocon_fact_formatter_spec.rb'
- 'spec/framework/formatters/json_fact_formatter_spec.rb'
- 'spec/framework/formatters/legacy_fact_formatter_spec.rb'
- 'spec/framework/formatters/yaml_fact_formatter_spec.rb'
- 'spec/framework/utils/utils_spec.rb'

# Offense count: 15
# Configuration parameters: AssignmentOnly.
Expand All @@ -33,7 +113,71 @@ RSpec/LeakyConstantDeclaration:
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Enabled: false
Exclude:
- 'spec/custom_facts/core/aggregate_spec.rb'
- 'spec/custom_facts/core/execution/fact_manager_spec.rb'
- 'spec/custom_facts/core/execution/windows_spec.rb'
- 'spec/custom_facts/core/execution_spec.rb'
- 'spec/custom_facts/core/logging_spec.rb'
- 'spec/custom_facts/core/resolvable_spec.rb'
- 'spec/custom_facts/core/suitable_spec.rb'
- 'spec/custom_facts/util/collection_spec.rb'
- 'spec/custom_facts/util/confine_spec.rb'
- 'spec/custom_facts/util/directory_loader_spec.rb'
- 'spec/custom_facts/util/fact_spec.rb'
- 'spec/custom_facts/util/loader_spec.rb'
- 'spec/custom_facts/util/parser_spec.rb'
- 'spec/custom_facts/util/resolution_spec.rb'
- 'spec/facter/facter_spec.rb'
- 'spec/facter/facts/aix/os/name_spec.rb'
- 'spec/facter/facts/aix/os/release_spec.rb'
- 'spec/facter/facts/macosx/is_virtual_spec.rb'
- 'spec/facter/facts/macosx/mountpoints_spec.rb'
- 'spec/facter/facts/macosx/os/architecture_spec.rb'
- 'spec/facter/facts/macosx/os/family_spec.rb'
- 'spec/facter/facts/macosx/os/name_spec.rb'
- 'spec/facter/facts/sles/os/architecture_spec.rb'
- 'spec/facter/facts/sles/os/name_spec.rb'
- 'spec/facter/facts/sles/os/release_spec.rb'
- 'spec/facter/facts/solaris/os/architecture_spec.rb'
- 'spec/facter/facts/solaris/os/name_spec.rb'
- 'spec/facter/facts/solaris/ruby/platform_spec.rb'
- 'spec/facter/facts/solaris/ruby/sitedir_spec.rb'
- 'spec/facter/facts/windows/dmi/manufacturer_spec.rb'
- 'spec/facter/facts/windows/dmi/product/name_spec.rb'
- 'spec/facter/facts/windows/dmi/product/serial_number_spec.rb'
- 'spec/facter/facts/windows/dmi/product/uuid_spec.rb'
- 'spec/facter/facts/windows/fips_enabled_spec.rb'
- 'spec/facter/facts/windows/identity/user_spec.rb'
- 'spec/facter/facts/windows/ipaddress6_interfaces_spec.rb'
- 'spec/facter/facts/windows/ipaddress_interfaces_spec.rb'
- 'spec/facter/facts/windows/macaddress_interfaces_spec.rb'
- 'spec/facter/facts/windows/memory/system/available_bytes_spec.rb'
- 'spec/facter/facts/windows/memory/system/available_spec.rb'
- 'spec/facter/facts/windows/memory/system/total_bytes_spec.rb'
- 'spec/facter/facts/windows/memory/system/total_spec.rb'
- 'spec/facter/facts/windows/mtu_interfaces_spec.rb'
- 'spec/facter/facts/windows/netmask6_interfaces_spec.rb'
- 'spec/facter/facts/windows/netmask_interfaces_spec.rb'
- 'spec/facter/facts/windows/network6_interfaces_spec.rb'
- 'spec/facter/facts/windows/network_interfaces_spec.rb'
- 'spec/facter/facts/windows/os/architecture_spec.rb'
- 'spec/facter/facts/windows/os/family_spec.rb'
- 'spec/facter/facts/windows/os/hardware_spec.rb'
- 'spec/facter/facts/windows/os/name_spec.rb'
- 'spec/facter/facts/windows/os/windows/edition_id_spec.rb'
- 'spec/facter/facts/windows/os/windows/installation_type_spec.rb'
- 'spec/facter/facts/windows/os/windows/product_name_spec.rb'
- 'spec/facter/facts/windows/os/windows/release_id_spec.rb'
- 'spec/facter/facts/windows/os/windows/system32_spec.rb'
- 'spec/facter/facts/windows/processors/count_spec.rb'
- 'spec/facter/facts/windows/processors/isa_spec.rb'
- 'spec/facter/facts/windows/processors/physicalcount_spec.rb'
- 'spec/facter/facts/windows/ruby/platform_spec.rb'
- 'spec/facter/facts/windows/ruby/sitedir_spec.rb'
- 'spec/facter/facts/windows/scope6_interfaces_spec.rb'
- 'spec/facter/resolvers/utils/aix/odm_query_spec.rb'
- 'spec/framework/core/fact_loaders/external_fact_loader_spec.rb'

# Offense count: 26
RSpec/SubjectStub:
Expand All @@ -46,7 +190,70 @@ RSpec/SubjectStub:
- 'spec/custom_facts/util/fact_spec.rb'
- 'spec/custom_facts/util/resolution_spec.rb'

# Offense count: 185
# Offense count: 179
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Enabled: false
Exclude:
- 'spec/custom_facts/core/aggregate_spec.rb'
- 'spec/custom_facts/util/collection_spec.rb'
- 'spec/custom_facts/util/confine_spec.rb'
- 'spec/custom_facts/util/directory_loader_spec.rb'
- 'spec/custom_facts/util/fact_spec.rb'
- 'spec/custom_facts/util/resolution_spec.rb'
- 'spec/facter/facter_spec.rb'
- 'spec/facter/facts/aix/ssh_spec.rb'
- 'spec/facter/facts/macosx/memory/swap/capacity_spec.rb'
- 'spec/facter/facts/macosx/memory/swap/used_bytes_spec.rb'
- 'spec/facter/facts/macosx/memory/system/capacity_spec.rb'
- 'spec/facter/facts/macosx/memory/system/used_bytes_spec.rb'
- 'spec/facter/facts/windows/dmi/product/serial_number_spec.rb'
- 'spec/facter/facts/windows/hypervisors/hyperv_spec.rb'
- 'spec/facter/facts/windows/hypervisors/kvm_spec.rb'
- 'spec/facter/facts/windows/hypervisors/virtualbox_spec.rb'
- 'spec/facter/facts/windows/hypervisors/vmware_spec.rb'
- 'spec/facter/facts/windows/hypervisors/xen_spec.rb'
- 'spec/facter/facts/windows/identity/privileged_spec.rb'
- 'spec/facter/facts/windows/memory/system/capacity_spec.rb'
- 'spec/facter/facts/windows/memory/system/used_bytes_spec.rb'
- 'spec/facter/facts/windows/networking/dhcp_spec.rb'
- 'spec/facter/facts/windows/networking/interfaces_spec.rb'
- 'spec/facter/facts/windows/networking/mtu_spec.rb'
- 'spec/facter/facts/windows/networking/primary_spec.rb'
- 'spec/facter/facts/windows/processors/models_spec.rb'
- 'spec/facter/facts/windows/virtualization/is_virtual_spec.rb'
- 'spec/facter/facts/windows/virtualization/virtual_spec.rb'
- 'spec/facter/query_parser_spec.rb'
- 'spec/facter/resolvers/aix/architecture_resolver_spec.rb'
- 'spec/facter/resolvers/aix/ffi_helper_spec.rb'
- 'spec/facter/resolvers/aix/hardware_resolver_spec.rb'
- 'spec/facter/resolvers/macosx/mountpoints_resolver_spec.rb'
- 'spec/facter/resolvers/mountpoints_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/current_zone_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/solaris_release_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/zfs_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/zone_resolver_spec.rb'
- 'spec/facter/resolvers/solaris/zpool_resolver_spec.rb'
- 'spec/facter/resolvers/utils/windows/network_utils_spec.rb'
- 'spec/facter/resolvers/utils/windows/win32ole_spec.rb'
- 'spec/facter/resolvers/windows/dmi_bios_resolver_spec.rb'
- 'spec/facter/resolvers/windows/dmi_computersystem_resolver_spec.rb'
- 'spec/facter/resolvers/windows/hardware_architecture_resolver_spec.rb'
- 'spec/facter/resolvers/windows/identity_resolver_spec.rb'
- 'spec/facter/resolvers/windows/kernel_resolver_spec.rb'
- 'spec/facter/resolvers/windows/memory_resolver_spec.rb'
- 'spec/facter/resolvers/windows/networking_resolver_spec.rb'
- 'spec/facter/resolvers/windows/processors_resolver_spec.rb'
- 'spec/facter/resolvers/windows/system32_resolver_spec.rb'
- 'spec/facter/resolvers/windows/uptime_resolver_spec.rb'
- 'spec/facter/resolvers/windows/virtualization_resolver_spec.rb'
- 'spec/facter/resolvers/windows/win_os_description_resolver_spec.rb'
- 'spec/framework/config/block_list_spec.rb'
- 'spec/framework/core/fact_loaders/external_fact_loader_spec.rb'
- 'spec/framework/core/fact_loaders/fact_loader_spec.rb'
- 'spec/framework/core/fact_manager_spec.rb'
- 'spec/framework/core/session_cache_spec.rb'
- 'spec/framework/formatters/hocon_fact_formatter_spec.rb'
- 'spec/framework/formatters/json_fact_formatter_spec.rb'
- 'spec/framework/formatters/legacy_fact_formatter_spec.rb'
- 'spec/framework/formatters/yaml_fact_formatter_spec.rb'
- 'spec/mocks/util.rb'
27 changes: 27 additions & 0 deletions lib/facts/windows/ssh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Facts
module Windows
class Ssh
FACT_NAME = 'ssh'

def call_the_resolver
privileged = Facter::Resolvers::Identity.resolve(:privileged)
ssh_info = Facter::Resolvers::Windows::Ssh.resolve(:ssh) if privileged
ssh_facts = {}
ssh_info&.each { |ssh| ssh_facts.merge!(create_ssh_fact(ssh)) }
Facter::ResolvedFact.new(FACT_NAME, ssh_facts.empty? ? nil : ssh_facts)
end

private

def create_ssh_fact(ssh)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this duplicated in each ssh fact? is there a ticket to solve the duplications?

{ ssh.name.to_sym =>
{ fingerprints: { sha1: ssh.fingerprint.sha1,
sha256: ssh.fingerprint.sha256 },
key: ssh.key,
type: ssh.type } }
end
end
end
end
42 changes: 1 addition & 41 deletions lib/resolvers/ssh_resolver.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require 'base64'
require 'digest/sha1'

module Facter
module Resolvers
class SshResolver < BaseResolver
Expand All @@ -28,49 +25,12 @@ def retrieve_info(fact_name)
next unless file_content

key_type, key = file_content.split(' ')
key_name = determine_ssh_key_name(key_type)
ssh_list << create_ssh(key_name, key_type, key)
ssh_list << ::Resolvers::Utils::SshHelper.create_ssh(key_type, key)
end
end
@fact_list[:ssh] = ssh_list
@fact_list[fact_name]
end

def create_ssh(key_name, key_type, key)
decoded_key = Base64.decode64(key)
ssh_fa = determine_ssh_fingerprint(key_name)
sha1 = "SSHFP #{ssh_fa} 1 #{Digest::SHA1.new.update(decoded_key)}"
sha256 = "SSHFP #{ssh_fa} 2 #{Digest::SHA2.new.update(decoded_key)}"

fingerprint = FingerPrint.new(sha1, sha256)
Ssh.new(fingerprint, key_type, key, key_name)
end

def determine_ssh_key_name(key)
case key
when 'ssh-dss'
'dsa'
when 'ecdsa-sha2-nistp256'
'ecdsa'
when 'ssh-ed25519'
'ed25519'
when 'ssh-rsa'
'rsa'
end
end

def determine_ssh_fingerprint(key_name)
case key_name
when 'rsa'
1
when 'dsa'
2
when 'ecdsa'
3
when 'ed25519'
4
end
end
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/resolvers/utils/ssh_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'base64'
require 'digest/sha1'

module Resolvers
module Utils
class SshHelper
class << self
SSH_NAME = { 'ssh-dss' => 'dsa', 'ecdsa-sha2-nistp256' => 'ecdsa',
'ssh-ed25519' => 'ed25519', 'ssh-rsa' => 'rsa' }.freeze
SSH_FINGERPRINT = { 'rsa' => 1, 'dsa' => 2, 'ecdsa' => 3, 'ed25519' => 4 }.freeze

def create_ssh(key_type, key)
key_name = SSH_NAME[key]
decoded_key = Base64.decode64(key)
ssh_fp = SSH_FINGERPRINT[key_name]
sha1 = "SSHFP #{ssh_fp} 1 #{Digest::SHA1.new.update(decoded_key)}"
sha256 = "SSHFP #{ssh_fp} 2 #{Digest::SHA2.new.update(decoded_key)}"

fingerprint = Facter::FingerPrint.new(sha1, sha256)
Facter::Ssh.new(fingerprint, key_type, key, key_name)
end
end
end
end
end
Loading