|
| 1 | +require "../../../models/analyzer" |
| 2 | + |
| 3 | +module Analyzer::Specification |
| 4 | + class IstioVirtualservice < Analyzer |
| 5 | + METHOD_ANY = "ANY" |
| 6 | + |
| 7 | + def analyze |
| 8 | + spec_files = CodeLocator.instance.all("istio-virtualservice-spec") |
| 9 | + return @result unless spec_files.is_a?(Array(String)) |
| 10 | + |
| 11 | + spec_files.each do |path| |
| 12 | + next unless File.exists?(path) |
| 13 | + |
| 14 | + details = Details.new(PathInfo.new(path)) |
| 15 | + content = read_file_content(path) |
| 16 | + begin |
| 17 | + YAML.parse_all(content).each { |doc| process_doc(doc, details) } |
| 18 | + rescue e |
| 19 | + @logger.debug "Exception processing #{path}" |
| 20 | + @logger.debug_sub e |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + @result |
| 25 | + end |
| 26 | + |
| 27 | + private def process_doc(doc : YAML::Any, details : Details) |
| 28 | + root = doc.as_h? |
| 29 | + return unless root |
| 30 | + |
| 31 | + kind = root[YAML::Any.new("kind")]?.try(&.as_s?) |
| 32 | + return unless kind == "VirtualService" |
| 33 | + |
| 34 | + api_version = root[YAML::Any.new("apiVersion")]?.try(&.as_s?) |
| 35 | + return unless api_version && api_version.starts_with?("networking.istio.io/") |
| 36 | + |
| 37 | + spec = root[YAML::Any.new("spec")]?.try(&.as_h?) |
| 38 | + return unless spec |
| 39 | + |
| 40 | + hosts = collect_string_array(spec[YAML::Any.new("hosts")]?) |
| 41 | + |
| 42 | + http_rules = spec[YAML::Any.new("http")]?.try(&.as_a?) || [] of YAML::Any |
| 43 | + http_rules.each { |rule| process_http_rule(rule, hosts, details) } |
| 44 | + end |
| 45 | + |
| 46 | + private def collect_string_array(node : YAML::Any?) : Array(String) |
| 47 | + result = [] of String |
| 48 | + return result if node.nil? |
| 49 | + arr = node.as_a? |
| 50 | + return result unless arr |
| 51 | + arr.each do |entry| |
| 52 | + if str = entry.as_s? |
| 53 | + result << str unless str.empty? |
| 54 | + end |
| 55 | + end |
| 56 | + result |
| 57 | + end |
| 58 | + |
| 59 | + private def process_http_rule(rule : YAML::Any, hosts : Array(String), details : Details) |
| 60 | + rule_h = rule.as_h? |
| 61 | + return unless rule_h |
| 62 | + |
| 63 | + matches = rule_h[YAML::Any.new("match")]?.try(&.as_a?) || [] of YAML::Any |
| 64 | + rewrite_target = rule_h[YAML::Any.new("rewrite")]?.try(&.as_h?) |
| 65 | + .try(&.[YAML::Any.new("uri")]?) |
| 66 | + .try(&.as_s?) |
| 67 | + |
| 68 | + matches.each { |match| process_match(match, hosts, rewrite_target, details) } |
| 69 | + end |
| 70 | + |
| 71 | + private def process_match(match : YAML::Any, hosts : Array(String), rewrite_target : String?, details : Details) |
| 72 | + match_h = match.as_h? |
| 73 | + return unless match_h |
| 74 | + |
| 75 | + uri = match_h[YAML::Any.new("uri")]? |
| 76 | + method_node = match_h[YAML::Any.new("method")]? |
| 77 | + |
| 78 | + path, path_type = extract_uri_match(uri) |
| 79 | + return if path.nil? || path.empty? |
| 80 | + |
| 81 | + method = extract_string_match(method_node) || METHOD_ANY |
| 82 | + method = method.upcase |
| 83 | + |
| 84 | + emit_endpoint(path, method, path_type, hosts, "match", details) |
| 85 | + |
| 86 | + if rewrite_target && !rewrite_target.empty? && rewrite_target != path |
| 87 | + emit_endpoint(rewrite_target, method, path_type, hosts, "rewrite", details) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + private def extract_uri_match(node : YAML::Any?) : Tuple(String?, String) |
| 92 | + return {nil, "prefix"} if node.nil? |
| 93 | + h = node.as_h? |
| 94 | + return {nil, "prefix"} unless h |
| 95 | + {"exact", "prefix", "regex"}.each do |kind| |
| 96 | + value = h[YAML::Any.new(kind)]?.try(&.as_s?) |
| 97 | + return {value, kind} if value && !value.empty? |
| 98 | + end |
| 99 | + {nil, "prefix"} |
| 100 | + end |
| 101 | + |
| 102 | + private def extract_string_match(node : YAML::Any?) : String? |
| 103 | + return if node.nil? |
| 104 | + if str = node.as_s? |
| 105 | + return str.empty? ? nil : str |
| 106 | + end |
| 107 | + h = node.as_h? |
| 108 | + return unless h |
| 109 | + {"exact", "prefix", "regex"}.each do |kind| |
| 110 | + value = h[YAML::Any.new(kind)]?.try(&.as_s?) |
| 111 | + return value if value && !value.empty? |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + private def emit_endpoint(path : String, method : String, path_type : String, hosts : Array(String), origin : String, details : Details) |
| 116 | + hosts = [""] if hosts.empty? |
| 117 | + hosts.each do |host| |
| 118 | + endpoint = Endpoint.new(path, method, details) |
| 119 | + endpoint.add_tag(Tag.new("virtualservice-path-type", path_type, "istio_virtualservice_analyzer")) |
| 120 | + endpoint.add_tag(Tag.new("virtualservice-host", host, "istio_virtualservice_analyzer")) unless host.empty? |
| 121 | + endpoint.add_tag(Tag.new("virtualservice-source", origin, "istio_virtualservice_analyzer")) |
| 122 | + @result << endpoint |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments