From c17dbab6e8168e7521d6fe37e5e463ab3fefaa7d Mon Sep 17 00:00:00 2001 From: Carrie Roberts Date: Mon, 11 May 2020 13:15:27 -0600 Subject: [PATCH] Update links on Indexes (#983) * index update * index update --- atomic_red_team/atomic_red_team.rb | 24 ++++++++++++++++++++---- bin/generate-atomic-docs.rb | 4 ++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/atomic_red_team/atomic_red_team.rb b/atomic_red_team/atomic_red_team.rb index 70cb156a..5489f830 100755 --- a/atomic_red_team/atomic_red_team.rb +++ b/atomic_red_team/atomic_red_team.rb @@ -46,21 +46,37 @@ class AtomicRedTeam # # Returns a Markdown formatted Github link to a technique. This will be to the edit page for # techniques that already have one or more Atomic Red Team tests, or the create page for - # techniques that have no existing tests. + # techniques that have no existing tests for the given OS. # - def github_link_to_technique(technique, include_identifier: false, link_new_to_contrib: true) + def github_link_to_technique(technique, include_identifier: false, only_platform: only_platform) technique_identifier = ATTACK_API.technique_identifier_for_technique(technique).upcase link_display = "#{"#{technique_identifier.upcase} " if include_identifier}#{technique['name']}" + yaml_file = "#{ATOMICS_DIRECTORY}/#{technique_identifier}/#{technique_identifier}.yaml" + markdown_file = "#{ATOMICS_DIRECTORY}/#{technique_identifier}/#{technique_identifier}.md" - if File.exists? "#{ATOMICS_DIRECTORY}/#{technique_identifier}/#{technique_identifier}.md" + if atomic_yaml_has_test_for_platform(yaml_file, only_platform) && (File.exists? markdown_file) # we have a file for this technique, so link to it's Markdown file "[#{link_display}](../../#{technique_identifier}/#{technique_identifier}.md)" else - # we don't have a file for this technique, so link to an edit page + # we don't have a file for this technique, or there are not tests for the given platform, so link to an edit page "#{link_display} [CONTRIBUTE A TEST](https://atomicredteam.io/contributing)" end end + def atomic_yaml_has_test_for_platform(yaml_file, only_platform) + has_test_for_platform = false + if File.exists? yaml_file + yaml = YAML.load_file(yaml_file) + yaml['atomic_tests'].each_with_index do |atomic, i| + if atomic["supported_platforms"].any? {|platform| platform.downcase =~ only_platform} + has_test_for_platform = true + break + end + end + end + return has_test_for_platform + end + def validate_atomic_yaml!(yaml) raise("YAML file has no elements") if yaml.nil? diff --git a/bin/generate-atomic-docs.rb b/bin/generate-atomic-docs.rb index 2d2e6137..6431d3e1 100755 --- a/bin/generate-atomic-docs.rb +++ b/bin/generate-atomic-docs.rb @@ -100,7 +100,7 @@ class AtomicRedTeamDocs ATTACK_API.ordered_tactic_to_technique_matrix(only_platform: only_platform).each do |row_of_techniques| row_values = row_of_techniques.collect do |technique| if technique - ATOMIC_RED_TEAM.github_link_to_technique(technique, include_identifier: false, link_new_to_contrib: false) + ATOMIC_RED_TEAM.github_link_to_technique(technique, include_identifier: false, only_platform: only_platform) end end result += "| #{row_values.join(' | ')} |\n" @@ -120,7 +120,7 @@ class AtomicRedTeamDocs ATTACK_API.techniques_by_tactic(only_platform: only_platform).each do |tactic, techniques| result += "# #{tactic}\n" techniques.each do |technique| - result += "- #{ATOMIC_RED_TEAM.github_link_to_technique(technique, include_identifier: true, link_new_to_contrib: true)}\n" + result += "- #{ATOMIC_RED_TEAM.github_link_to_technique(technique, include_identifier: true, only_platform: only_platform)}\n" ATOMIC_RED_TEAM.atomic_tests_for_technique(technique).each_with_index do |atomic_test, i| next unless atomic_test['supported_platforms'].any? {|platform| platform.downcase =~ only_platform}