Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hardcoded-ip-test: Redesign test, add more info on fail #2096

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/tasks/workload/configuration.cr
Original file line number Diff line number Diff line change
@@ -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[0] == "0.0.0.0" || match[0] == "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