Skip to content

Commit b11969e

Browse files
committed
(FACT-3136) Split ssh-keys
GCE deprecated "sshKeys" and recommends "ssh-keys", so transform both. [1] https://cloud.google.com/compute/docs/metadata/default-metadata-values
1 parent 3d9436d commit b11969e

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

lib/facter/resolvers/gce.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ def parse_instance(gce_data)
4040
return if instance_data.nil? || instance_data.empty?
4141

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

46-
keys = instance_data.dig('attributes', 'sshKeys')
47-
instance_data['attributes']['sshKeys'] = keys.strip.split("\n") if keys
47+
keys = instance_data.dig('attributes', name)
48+
instance_data['attributes'][name] = keys.strip.split("\n") if keys
49+
end
4850

4951
%w[image machineType zone].each do |key|
5052
instance_data[key] = instance_data[key].split('/').last if instance_data[key]

spec/facter/resolvers/gce_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
'instance' => {
2222
'attributes' => {
2323
# resolver transforms key1\nkey2 into array of keys
24+
'ssh-keys' => [
25+
'john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
26+
'jane_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'
27+
],
2428
'sshKeys' => [
2529
'jill_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
2630
'jacob_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'
@@ -128,8 +132,8 @@
128132
'project' => {
129133
'attributes' => {
130134
# resolver transforms key1\nkey2 into array of keys
131-
'ssh-keys' => 'john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B' \
132-
" google-ssh {\"userName\":\"john[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\n",
135+
'ssh-keys' => ['john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
136+
'jane_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"jane[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'],
133137
'sshKeys' => [
134138
'jill_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}',
135139
'jacob_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {"userName":"[email protected]","expireOn":"2020-08-13T12:17:19+0000"}'

spec/fixtures/gce

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"instance":
33
{
44
"attributes":{
5+
"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",
56
"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\"}"
67
},
78
"cpuPlatform":"Intel Broadwell",
@@ -65,7 +66,7 @@
6566
{
6667
"attributes":
6768
{
68-
"ssh-keys":"john_doe:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9D8Op48TtEiDmb+Gtna3Bs9B google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2020-08-13T12:17:19+0000\"}\n",
69+
"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",
6970
"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\"}"
7071
},
7172
"numericProjectId":728618928092,

0 commit comments

Comments
 (0)