Skip to content

Commit

Permalink
Merge branch 'master' into POL-1362-cft-automation
Browse files Browse the repository at this point in the history
  • Loading branch information
XOmniverse authored Oct 15, 2024
2 parents 833bc18 + 8b318a7 commit 77c0d59
Show file tree
Hide file tree
Showing 604 changed files with 61,879 additions and 8,921 deletions.
110 changes: 97 additions & 13 deletions .dangerfile/policy_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,40 @@ def policy_bad_directory?(file)
fail_message += "Policy is not located within a subdirectory specific to the cloud provider or service it is applicable for. For example, AWS cost policies should be in the `/cost/aws` subdirectory, Azure operational policies in the `/operational/azure` subdirectory, etc.\n\n"
end

if (parts[1] == 'flexera' && parts[3].include?('.pt')) && parts[0] != "tools"
if (parts[1] == 'flexera' && parts[3].include?('.pt')) && parts[0] != "tools" && parts[0] != "automation"
fail_message += "Flexera policy is not contained in a subdirectory specific to the Flexera service it is for. For example, Flexera CCO cost policies should be in the `/cost/flexera/cco` subdirectory.\n\n"
end

return fail_message.strip if !fail_message.empty?
return false
end

### README Name Match test
# Verify that the policy template name field matches first line of README
def policy_readme_correct_name?(file, file_parsed)
puts Time.now.strftime("%H:%M:%S.%L") + " *** Testing whether Policy Template name matches first line of README.md..."

fail_message = ""

# Get policy template name from parsed data
template_name = file_parsed.parsed_name

# Get file path for readme file
file_sections = file.split('/')
file_sections.pop
readme_file_path = file_sections.join('/') + "/README.md"

# Get first line of README.md and remove #
readme_name = File.read(readme_file_path).split("\n")[0].split("# ")[1].strip()

if (template_name != readme_name)
fail_message = "Policy Template name `" + template_name + "` does not match the first line of the README.md file. Please ensure that README.md has the correct policy template name on the first line."
end

return fail_message.strip if !fail_message.empty?
return false
end

### Unmodified README test
# Verify that .pt file also has an updated README
def policy_unmodified_readme?(file, changed_readme_files)
Expand Down Expand Up @@ -593,6 +619,29 @@ def policy_sections_out_of_order?(file, file_lines)
policy_fail = false
escalations_fail = false

# Record whether certain policy blocks exist at all
metadata_exists = false
parameters_exists = false
credentials_exists = false
pagination_exists = false
datasources_exists = false
policy_exists = false
escalations_exists = false
cwf_exists = false

file_lines.each_with_index do |line, index|
metadata_exists = true if line.start_with?('name ')
parameters_exists = true if line.strip.start_with?('parameter ') && line.strip.end_with?('do')
credentials_exists = true if line.strip.start_with?('credentials ') && line.strip.end_with?('do')
pagination_exists = true if line.strip.start_with?('pagination ') && line.strip.end_with?('do')
datasources_exists = true if line.strip.start_with?('datasource ') && line.strip.end_with?('do')
policy_exists = true if line.strip.start_with?('policy ') && line.strip.end_with?('do')
escalations_exists = true if line.strip.start_with?('escalation ') && line.strip.end_with?('do')
cwf_exists = true if line.strip.start_with?('define ') && line.strip.end_with?('do')

break if line.strip.start_with?('# Meta Policy [alpha]')
end

# Failsafe for meta policy code which won't be in the correct order by design
found_meta = false

Expand All @@ -611,32 +660,32 @@ def policy_sections_out_of_order?(file, file_lines)
found_escalations = true if line.strip.start_with?('escalation ') && line.strip.end_with?('do')
found_cwf = true if line.strip.start_with?('define ') && line.strip.end_with?('do')

if !metadata_fail && !found_metadata && (found_parameters || found_credentials || found_pagination || found_datasources || found_policy || found_escalations || found_cwf)
if metadata_exists && !metadata_fail && !found_metadata && (found_parameters || found_credentials || found_pagination || found_datasources || found_policy || found_escalations || found_cwf)
fail_message += "Line #{line_number.to_s}: Invalid blocks found before metadata\n\n"
metadata_fail = true
end

if !parameters_fail && !found_parameters && (found_credentials || found_pagination || found_datasources || found_policy || found_escalations || found_cwf)
if parameters_exists && !parameters_fail && !found_parameters && (found_credentials || found_pagination || found_datasources || found_policy || found_escalations || found_cwf)
fail_message += "Line #{line_number.to_s}: Invalid blocks found before parameter\n\n"
parameters_fail = true
end

if !credentials_fail && !found_credentials && (found_pagination || found_datasources || found_policy || found_escalations || found_cwf)
if credentials_exists && !credentials_fail && !found_credentials && (found_pagination || found_datasources || found_policy || found_escalations || found_cwf)
fail_message += "Line #{line_number.to_s}: Invalid blocks found before credentials\n\n"
credentials_fail = true
end

if !datasources_fail && !found_datasources && (found_policy || found_escalations || found_cwf)
if datasources_exists && !datasources_fail && !found_datasources && (found_policy || found_escalations || found_cwf)
fail_message += "Line #{line_number.to_s}: Invalid blocks found before datasources\n\n"
datasources_fail = true
end

if !policy_fail && !found_policy && (found_escalations || found_cwf)
if policy_exists && !policy_fail && !found_policy && (found_escalations || found_cwf)
fail_message += "Line #{line_number.to_s}: Invalid blocks found before policy block\n\n"
policy_fail = true
end

if !escalations_fail && !found_escalations && (found_cwf)
if escalations_exists && !escalations_fail && !found_escalations && (found_cwf)
fail_message += "Line #{line_number.to_s}: Invalid blocks found before escalations\n\n"
escalations_fail = true
end
Expand Down Expand Up @@ -1015,7 +1064,7 @@ def policy_run_script_incorrect_order?(file, file_lines)
value_found = false # Whether we've found a raw value, like a number or string

parameters.each_with_index do |parameter, index|
if parameter.include?("iter_item") || parameter.include?("val(")
if parameter.include?("iter_item") || parameter.include?("val(") || parameter.include?("jq(")
val_found = true
val_index = index
disordered = true if ds_found || param_found || constant_found || value_found
Expand Down Expand Up @@ -1225,18 +1274,32 @@ def policy_bad_comma_spacing?(file, file_lines)
puts Time.now.strftime("%H:%M:%S.%L") + " *** Testing whether Policy Template file has improper comma spacing..."

fail_message = ""

file_lines.each_with_index do |line, index|
line_number = index + 1
line = line.strip
test_line = line
parts = []

# Look for stuff quotations and remove those
# This is to reduce false positives
parts = line.split("\"") if line.include?("\"") && !line.include?("'")
parts = line.split("'") if !line.include?("\"") && line.include?("'")

if parts.length > 2 && parts.length % 2 == 1
test_parts = []
parts.each_with_index { |part, index| test_parts << part if index % 2 == 0 }
test_line = test_parts.join("'")
end

if line.include?(",") && !line.include?("allowed_pattern") && !line.include?('= ","') && !line.include?("(',')") && !line.include?('(",")') && !line.include?("jq(") && !line.include?("/,/")
if line.strip.match(/,\s{2,}/) || line.strip.match(/\s,/) || line.strip.match(/,[^\s]/)
fail_message += "Line #{line_number.to_s}: Possible invalid spacing between comma-separated items found.\nComma separated items should be organized as follows, with a single space following each comma: apple, banana, pear\n\n"
if test_line.include?(",") && !test_line.include?("allowed_pattern") && !test_line.include?('= ","') && !test_line.include?("(',')") && !test_line.include?('(",")') && !test_line.include?("jq(") && !test_line.include?("/,/")
if test_line.match(/,\s{2,}/) || test_line.match(/\s,/) || test_line.match(/,[^\s]/) && !(test_line.match(/\',\'/) || test_line.match(/\",\"/) || test_line.match(/\`,\`/))
fail_message += "\n\n" if fail_message.empty?
fail_message += "Line #{line_number.to_s}: `" + line + "`\n\n"
end
end
end

fail_message = "Issues with comma-separation found:\n\n" + fail_message if !fail_message.empty?
fail_message = "Possible invalid spacing between comma-separated items found:\n\n" + fail_message + "\n\nComma separated items should be organized as follows, with a single space following each comma: apple, banana, pear" if !fail_message.empty?

return fail_message.strip if !fail_message.empty?
return false
Expand Down Expand Up @@ -1409,3 +1472,24 @@ def policy_console_log?(file, file_lines)
return fail_message.strip if !fail_message.empty?
return false
end

### verb "GET" test
# Return false if a datasource never specifies "GET" as a verb value
def policy_verb_get?(file, file_lines)
puts Time.now.strftime("%H:%M:%S.%L") + " *** Testing whether Policy Template file datasources have any verb \"GET\" statements..."

# Message to return of test fails
fail_message = ""

file_lines.each_with_index do |line, index|
break if line.strip.start_with?("# Cloud Workflow")

line_number = index + 1
fail_message += "Line #{line_number.to_s}\n" if line.strip.start_with?("verb \"GET\"") || line.strip.start_with?("verb: \"GET\"") || line.strip.start_with?("verb 'GET'") || line.strip.start_with?("verb: 'GET'")
end

fail_message = "Policy Template has verb \"GET\" statements. The verb field defaults to this value and should only be specified for other values, such as PATCH or POST:\n\n" + fail_message if !fail_message.empty?

return fail_message.strip if !fail_message.empty?
return false
end
Loading

0 comments on commit 77c0d59

Please sign in to comment.