Skip to content

Commit

Permalink
Merge pull request #2564 from joshcooper/gce_sshKeys
Browse files Browse the repository at this point in the history
(FACT-3136) Split GCE ssh keys on newlines
  • Loading branch information
mhashizume authored Apr 27, 2023
2 parents db39fd5 + f670cd0 commit 8e38ac9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 28 deletions.
22 changes: 3 additions & 19 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ require:

Layout/LineLength:
Max: 120
Exclude:
- spec/facter/resolvers/gce_spec.rb

Lint/RaiseException:
Enabled: true
Expand Down Expand Up @@ -51,25 +53,7 @@ Naming/ClassAndModuleCamelCase:
- 'spec/mocks/**/*'

Metrics/AbcSize:
Max: 16
Exclude:
- 'spec/custom_facts/util/parser_spec.rb'
- 'spec/custom_facts/core/logging_spec.rb'
- 'lib/facter/custom_facts/util/values.rb'
- 'lib/facter/custom_facts/util/loader.rb'
- 'lib/facter/custom_facts/util/confine.rb'
- 'lib/facter/custom_facts/util/confine.rb'
- 'lib/facter/custom_facts/core/execution/windows.rb'
- 'lib/facter/custom_facts/core/execution/base.rb'
- 'lib/facter/custom_facts/core/resolvable.rb'
- 'lib/facter/resolvers/bsd/ffi/ffi_helper.rb'
- 'install.rb'
- 'scripts/generate_changelog.rb'
- 'lib/facter/resolvers/aix/ffi/ffi_helper.rb'
- 'lib/facter/custom_facts/core/execution/popen3.rb'
- 'lib/facter.rb'
- 'lib/facter/framework/parsers/query_parser.rb'
- 'lib/facter/framework/core/fact_manager.rb'
Enabled: false

Metrics/PerceivedComplexity:
Exclude:
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/resolvers/freebsd/swap_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def post_resolve(fact_name, _options)
@fact_list.fetch(fact_name) { read_swap_memory(fact_name) }
end

def read_swap_memory(fact_name) # rubocop:disable Metrics/AbcSize
def read_swap_memory(fact_name)
output = Facter::Core::Execution.execute('swapinfo -k', logger: log)
data = output.split("\n")[1..-1].map { |line| line.split(/\s+/) }

Expand Down
9 changes: 9 additions & 0 deletions lib/facter/resolvers/gce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def parse_instance(gce_data)
instance_data = gce_data['instance']
return if instance_data.nil? || instance_data.empty?

# See https://cloud.google.com/compute/docs/metadata for information about these values
%w[sshKeys ssh-keys].each do |name|
keys = gce_data.dig('project', 'attributes', name)
gce_data['project']['attributes'][name] = keys.strip.split("\n") if keys

keys = instance_data.dig('attributes', name)
instance_data['attributes'][name] = keys.strip.split("\n") if keys
end

%w[image machineType zone].each do |key|
instance_data[key] = instance_data[key].split('/').last if instance_data[key]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/resolvers/macosx/swap_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def post_resolve(fact_name, _options)
@fact_list.fetch(fact_name) { read_swap_memory(fact_name) }
end

def read_swap_memory(fact_name) # rubocop:disable Metrics/AbcSize
def read_swap_memory(fact_name)
output = Facter::Core::Execution.execute('sysctl -n vm.swapusage', logger: log)
data = output.match(/^total = ([\d.]+)M used = ([\d.]+)M free = ([\d.]+)M (\(encrypted\))$/)

Expand Down
2 changes: 1 addition & 1 deletion lib/facter/resolvers/solaris/mountpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def exclude_auto_home_mounts!
end
end

def read_mounts(fact_name) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def read_mounts(fact_name) # rubocop:disable Metrics/MethodLength
@mounts = []
@auto_home_paths = []

Expand Down
2 changes: 1 addition & 1 deletion lib/facter/util/api_debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Facter
module Util
module ApiDebugger
def self.prepended(receiver) # rubocop:disable Metrics/AbcSize
def self.prepended(receiver)
exclude, print_caller = parse_options(ENV['API_DEBUG'])

receiver_methods = receiver.instance_methods - Object.methods
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/util/resolvers/ffi/hostname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.getffihostname
raw_hostname.read_string
end

def self.getffiaddrinfo(hostname) # rubocop:disable Metrics/AbcSize
def self.getffiaddrinfo(hostname)
ret = FFI::MemoryPointer.new(:pointer)

hints = Facter::Util::Resolvers::Ffi::AddrInfo.new
Expand Down
18 changes: 16 additions & 2 deletions spec/facter/resolvers/gce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
{
'instance' => {
'attributes' => {
# resolver transforms key1\nkey2 into array of keys
'ssh-keys' => [
'john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
'jane_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'
],
'sshKeys' => [
'jill_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
'jacob_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'
]
},
'cpuPlatform' => 'Intel Broadwell',
'description' => '',
Expand Down Expand Up @@ -122,8 +131,13 @@
},
'project' => {
'attributes' => {
'ssh-keys' => 'john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B' \
" google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\n"
# resolver transforms key1\nkey2 into array of keys
'ssh-keys' => ['john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
'jane_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'],
'sshKeys' => [
'jill_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
'jacob_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'
]
},
'numericProjectId' => 728_618_928_092,
'projectId' => 'facter-performance-history'
Expand Down
8 changes: 6 additions & 2 deletions spec/fixtures/gce
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"instance":
{
"attributes":{},
"attributes":{
"ssh-keys":"john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\njane_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\n",
"sshKeys":"jill_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\njacob_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}"
},
"cpuPlatform":"Intel Broadwell",
"description":"",
"disks":[{"deviceName":"instance-3","index":0,"interface":"SCSI","mode":"READ_WRITE","type":"PERSISTENT"}],
Expand Down Expand Up @@ -63,7 +66,8 @@
{
"attributes":
{
"ssh-keys":"john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\n"
"ssh-keys":"john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\njane_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\n",
"sshKeys":"jill_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\njacob_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}"
},
"numericProjectId":728618928092,
"projectId":"facter-performance-history"
Expand Down

0 comments on commit 8e38ac9

Please sign in to comment.