Skip to content

Commit

Permalink
hardcoded-ip-test: Redesign test, add more info on fail
Browse files Browse the repository at this point in the history
Add information about file location, line numbers and lines themselves
if the hardcoded_ip_addresses_in_k8s_runtime_configuration fails

Refs: cnti-testcatalog#2093
Signed-off-by: Konstantin Yarovoy <konstantin.yarovoy@tietoevry.com>
  • Loading branch information
Konstantin Yarovoy committed Jul 2, 2024
1 parent 9497fd6 commit ed666ba
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/tasks/workload/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit ed666ba

Please sign in to comment.