From b269505aad757d8e68c26d7a6ad376cbd68c2425 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Wed, 12 Jun 2019 15:44:28 +0300 Subject: [PATCH] Handle relative ssh key paths in TF JSON and validate file exists --- lib/pharos/command_options/tf_json.rb | 20 +++++++++++++++++--- lib/pharos/config_schema.rb | 9 ++++++++- lib/pharos/terraform/json_parser.rb | 6 ++++-- lib/pharos/terraform/legacy_json_parser.rb | 6 ++++-- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/pharos/command_options/tf_json.rb b/lib/pharos/command_options/tf_json.rb index 4df519443..c6694b4bf 100644 --- a/lib/pharos/command_options/tf_json.rb +++ b/lib/pharos/command_options/tf_json.rb @@ -33,7 +33,7 @@ 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, @@ -41,7 +41,7 @@ def load_terraform(file, config) 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'] ||= {} @@ -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 diff --git a/lib/pharos/config_schema.rb b/lib/pharos/config_schema.rb index 8a134499a..45e5c24f6 100644 --- a/lib/pharos/config_schema.rb +++ b/lib/pharos/config_schema.rb @@ -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" } } ) @@ -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 diff --git a/lib/pharos/terraform/json_parser.rb b/lib/pharos/terraform/json_parser.rb index 1ab5dec7e..4dfb4e157 100644 --- a/lib/pharos/terraform/json_parser.rb +++ b/lib/pharos/terraform/json_parser.rb @@ -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? diff --git a/lib/pharos/terraform/legacy_json_parser.rb b/lib/pharos/terraform/legacy_json_parser.rb index 02cffff1d..0c7ba39e8 100644 --- a/lib/pharos/terraform/legacy_json_parser.rb +++ b/lib/pharos/terraform/legacy_json_parser.rb @@ -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]