diff --git a/src/tasks/workload/configuration.cr b/src/tasks/workload/configuration.cr index 49ef8b04b..4841d96e5 100644 --- a/src/tasks/workload/configuration.cr +++ b/src/tasks/workload/configuration.cr @@ -270,6 +270,7 @@ task "hardcoded_ip_addresses_in_k8s_runtime_configuration" do |t, args| helm_directory = config.cnf_config[:helm_directory] release_name = config.cnf_config[:release_name] destination_cnf_dir = config.cnf_config[:destination_cnf_dir] + helm_chart_yml_path = "#{destination_cnf_dir}/helm_chart.yml" current_dir = FileUtils.pwd helm = Helm::BinarySingleton.helm VERBOSE_LOGGING.info "Helm Path: #{helm}" if check_verbose(args) @@ -281,23 +282,36 @@ task "hardcoded_ip_addresses_in_k8s_runtime_configuration" do |t, args| Log.for(t.name).info { "airgapped mode info: #{info}" } helm_chart = info[:tar_name] end - helm_install = Helm.install("--namespace hardcoded-ip-test hardcoded-ip-test #{helm_chart} --dry-run --debug > #{destination_cnf_dir}/helm_chart.yml") + helm_install = Helm.install("--namespace hardcoded-ip-test hardcoded-ip-test #{helm_chart} --dry-run --debug > #{helm_chart_yml_path}") else - helm_install = Helm.install("--namespace hardcoded-ip-test hardcoded-ip-test #{destination_cnf_dir}/#{helm_directory} --dry-run --debug > #{destination_cnf_dir}/helm_chart.yml") + helm_install = Helm.install("--namespace hardcoded-ip-test hardcoded-ip-test #{destination_cnf_dir}/#{helm_directory} --dry-run --debug > #{helm_chart_yml_path}") VERBOSE_LOGGING.info "helm_directory: #{helm_directory}" if check_verbose(args) end - - ip_search = File.read_lines("#{destination_cnf_dir}/helm_chart.yml").take_while{|x| x.match(/NOTES:/) == nil}.reduce([] of String) do |acc, x| - (x.match(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/) && - x.match(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/).try &.[0] != "0.0.0.0" && - x.match(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/).try &.[0] != "127.0.0.1") ? acc << x : acc + + found_violations = [] of NamedTuple(line_number: Int32, line: String) + line_number = 1 + File.open("#{helm_chart_yml_path}") do |file| + file.each_line do |line| + if line.matches?(/NOTES:/) + break + elsif matches = line.scan(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/) + matches.each do |match| + unless match.to_s == "0.0.0.0" || match.to_s == "127.0.0.1" + found_violations << {line_number: line_number, line: line.strip} + end + end + end + line_number += 1 + end end - VERBOSE_LOGGING.info "IPs: #{ip_search}" if check_verbose(args) - - if ip_search.empty? + if found_violations.empty? CNFManager::TestcaseResult.new(CNFManager::ResultStatus::Passed, "No hard-coded IP addresses found in the runtime K8s configuration") else + stdout_failure("Hard-coded IP addresses found in #{helm_chart_yml_path}") + found_violations.each do |violation| + stdout_failure(" * Line #{violation[:line_number]}: #{violation[:line]}") + end CNFManager::TestcaseResult.new(CNFManager::ResultStatus::Failed, "Hard-coded IP addresses found in the runtime K8s configuration") end rescue