Skip to content

Commit

Permalink
Handle relative ssh key paths in TF JSON and validate file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimmo Lehto committed Jun 12, 2019
1 parent 1454b65 commit b269505
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
20 changes: 17 additions & 3 deletions lib/pharos/command_options/tf_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def load_terraform(file, config)
puts("==> Importing configuration from Terraform ...".green) if $stdout.tty?

json = File.read(file)
tf_parser = Pharos::Terraform::JsonParser.new(json)
tf_parser = Pharos::Terraform::JsonParser.new(json, file)
if tf_parser.valid?
config.deep_merge!(
tf_parser.cluster,
overwrite_arrays: false,
union_arrays: true
)
else
tf_parser = Pharos::Terraform::LegacyJsonParser.new(json)
tf_parser = Pharos::Terraform::LegacyJsonParser.new(json, file)
config['hosts'] ||= []
config['api'] ||= {}
config['addons'] ||= {}
Expand All @@ -55,7 +55,21 @@ def load_terraform(file, config)
end
end

config
config['hosts'].each do |host|
if host[:ssh_key_path]
unless File.exist?(host[:ssh_key_path])
expanded = File.expand_path(host[:ssh_key_path])
host[:ssh_key_path] = File.exist?(expanded) ? expanded : File.join(File.dirname(file), host[:ssh_key_path])
end
end

if host.dig(:bastion, :ssh_key_path)
unless File.exist?(host[:bastion][:ssh_key_path])
expanded = File.expand_path(host[:bastion][:ssh_key_path])
host[:ssh_key_path] = File.exist?(expanded) ? expanded : File.join(File.dirname(file), host[:bastion][:ssh_key_path])
end
end
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/pharos/config_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def self.messages
errors: {
network_dns_replicas: "network.dns_replicas cannot be larger than the number of hosts",
hostname_or_ip?: "is invalid",
unique_address?: "is not unique"
unique_address?: "is not unique",
host_ssh_key_path: "file does not exist"
}
}
)
Expand Down Expand Up @@ -105,6 +106,12 @@ def self.messages
optional(:ssh_key_path).filled(:str?)
optional(:ssh_port).filled(:int?, gt?: 0, lt?: 65_536)
optional(:ssh_proxy_command).filled(:str?)
validate(host_ssh_key_path: [:ssh_key_path]) do |ssh_key_path|
ssh_key_path.nil? ? true : File.exist?(File.expand_path(ssh_key_path))
end
end
validate(host_ssh_key_path: [:ssh_key_path]) do |ssh_key_path|
ssh_key_path.nil? ? true : File.exist?(File.expand_path(ssh_key_path))
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/pharos/terraform/json_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ class ParserError < Pharos::Error; end

class JsonParser
# @param json [String]
def initialize(json)
# @param path [String]
def initialize(json, path)
@json = json
@path = path
end

def data
@data ||= JSON.parse(@json)
rescue JSON::ParserError => ex
raise ParserError, ex.message
raise ParserError, ex.message + "in '#{@path}'"
end

def valid?
Expand Down
6 changes: 4 additions & 2 deletions lib/pharos/terraform/legacy_json_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ module Pharos
module Terraform
class LegacyJsonParser
# @param json [String]
def initialize(json)
# @param path [String]
def initialize(json, path)
@json = json
@path = path
end

def data
@data ||= JSON.parse(@json)
rescue JSON::ParserError => ex
raise ParserError, ex.message
raise ParserError, ex.message + "in '#{path}'"
end

# @return [Array<Hash>]
Expand Down

0 comments on commit b269505

Please sign in to comment.