Module: Harnex::ArtifactReport
- Defined in:
- lib/harnex/artifact_report.rb
Constant Summary collapse
- SCHEMA =
"harnex.artifact_report.v1"- MAX_BYTES =
256 * 1024
- MAX_ARTIFACTS =
50- MAX_COMMANDS =
50- MAX_CANONICAL_ARTIFACTS =
50- MAX_EVIDENCE_ITEMS =
20- MAX_STRING_LENGTH =
2_000
Class Method Summary collapse
- .bounded_string(value) ⇒ Object
- .bounded_string_or_nil(value) ⇒ Object
- .build_payload(path, bytes:, sha256:, report:) ⇒ Object
- .compact_artifacts(value) ⇒ Object
- .compact_commands(value) ⇒ Object
- .compact_validation(value) ⇒ Object
- .file_sha256(path) ⇒ Object
- .finite_float_or_nil(value) ⇒ Object
- .ingest(path) ⇒ Object
- .integer_or_nil(value) ⇒ Object
- .metadata(path, bytes:, sha256:, ingest_status:, schema: nil) ⇒ Object
- .missing(path) ⇒ Object
- .safe_file_sha256(path) ⇒ Object
- .safe_file_size(path) ⇒ Object
- .string_array(value, max_items:) ⇒ Object
- .warning(path, bytes:, sha256:, ingest_status:, warning:, schema: nil) ⇒ Object
Class Method Details
.bounded_string(value) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/harnex/artifact_report.rb', line 176 def bounded_string(value) text = value.to_s return text if text.length <= MAX_STRING_LENGTH text[0, MAX_STRING_LENGTH] end |
.bounded_string_or_nil(value) ⇒ Object
170 171 172 173 174 |
# File 'lib/harnex/artifact_report.rb', line 170 def bounded_string_or_nil(value) return nil if value.nil? bounded_string(value) end |
.build_payload(path, bytes:, sha256:, report:) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/harnex/artifact_report.rb', line 74 def build_payload(path, bytes:, sha256:, report:) artifacts = compact_artifacts(report["artifacts"]) validation = compact_validation(report["validation"]) payload = { "artifact_report" => ( path, bytes: bytes, sha256: sha256, ingest_status: "ok", schema: SCHEMA ).merge( "report_status" => bounded_string_or_nil(report["status"]), "canonical_artifacts" => string_array(report["canonical_artifacts"], max_items: MAX_CANONICAL_ARTIFACTS), "artifact_count" => artifacts.length ) } payload["validation"] = validation if validation payload["artifacts"] = artifacts unless artifacts.empty? payload end |
.compact_artifacts(value) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/harnex/artifact_report.rb', line 147 def compact_artifacts(value) Array(value).first(MAX_ARTIFACTS).filter_map do |entry| next unless entry.is_a?(Hash) compact = { "type" => bounded_string_or_nil(entry["type"]), "summary" => bounded_string_or_nil(entry["summary"]), "evidence" => string_array(entry["evidence"], max_items: MAX_EVIDENCE_ITEMS), "confidence" => finite_float_or_nil(entry["confidence"]), "canonical_ref" => bounded_string_or_nil(entry["canonical_ref"]) } compact.delete_if { |_key, v| v.nil? || v == [] } compact.empty? ? nil : compact end end |
.compact_commands(value) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/harnex/artifact_report.rb', line 132 def compact_commands(value) Array(value).first(MAX_COMMANDS).filter_map do |entry| next unless entry.is_a?(Hash) compact = { "cmd" => bounded_string_or_nil(entry["cmd"]), "exit_code" => integer_or_nil(entry["exit_code"]), "status" => bounded_string_or_nil(entry["status"]), "duration_s" => finite_float_or_nil(entry["duration_s"]) } compact.delete_if { |_key, v| v.nil? } compact.empty? ? nil : compact end end |
.compact_validation(value) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/harnex/artifact_report.rb', line 121 def compact_validation(value) return nil unless value.is_a?(Hash) payload = {} payload["status"] = bounded_string_or_nil(value["status"]) payload["commands"] = compact_commands(value["commands"]) payload["final_reported"] = !!value["final_reported"] if value.key?("final_reported") payload.delete_if { |_key, v| v.nil? || v == [] } payload.empty? ? nil : payload end |
.file_sha256(path) ⇒ Object
200 201 202 203 204 205 206 207 |
# File 'lib/harnex/artifact_report.rb', line 200 def file_sha256(path) digest = Digest::SHA256.new File.open(path, "rb") do |file| buffer = +"" digest.update(buffer) while file.read(16 * 1024, buffer) end digest.hexdigest end |
.finite_float_or_nil(value) ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/harnex/artifact_report.rb', line 191 def finite_float_or_nil(value) return nil if value.nil? number = Float(value) number.finite? ? number : nil rescue ArgumentError, TypeError nil end |
.ingest(path) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/harnex/artifact_report.rb', line 16 def ingest(path) report_path = File.(path.to_s) return missing(report_path) unless File.file?(report_path) bytes = File.size(report_path) sha256 = file_sha256(report_path) if bytes > MAX_BYTES return warning( report_path, bytes: bytes, sha256: sha256, ingest_status: "oversized", warning: "artifact report is #{bytes} bytes; max is #{MAX_BYTES} bytes" ) end parsed = JSON.parse(File.read(report_path, mode: "rb")) unless parsed.is_a?(Hash) return warning( report_path, bytes: bytes, sha256: sha256, ingest_status: "malformed", warning: "artifact report must be a JSON object" ) end schema = parsed["schema"].to_s unless schema == SCHEMA return warning( report_path, bytes: bytes, sha256: sha256, ingest_status: "unsupported_schema", schema: schema.empty? ? nil : bounded_string(schema), warning: "unsupported artifact report schema #{schema.inspect}; expected #{SCHEMA}" ) end build_payload(report_path, bytes: bytes, sha256: sha256, report: parsed) rescue JSON::ParserError => e warning( report_path, bytes: safe_file_size(report_path), sha256: safe_file_sha256(report_path), ingest_status: "malformed", warning: "malformed artifact report JSON: #{bounded_string(e.)}" ) rescue StandardError => e warning( report_path, bytes: safe_file_size(report_path), sha256: safe_file_sha256(report_path), ingest_status: "error", warning: "artifact report ingest failed: #{bounded_string(e.)}" ) end |
.integer_or_nil(value) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/harnex/artifact_report.rb', line 183 def integer_or_nil(value) return nil if value.nil? Integer(value) rescue ArgumentError, TypeError nil end |
.metadata(path, bytes:, sha256:, ingest_status:, schema: nil) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/harnex/artifact_report.rb', line 111 def (path, bytes:, sha256:, ingest_status:, schema: nil) { "path" => path.to_s, "bytes" => bytes, "sha256" => sha256, "ingest_status" => ingest_status, "schema" => schema } end |
.missing(path) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/harnex/artifact_report.rb', line 95 def missing(path) warning( path, bytes: nil, sha256: nil, ingest_status: "missing", warning: "artifact report not found" ) end |
.safe_file_sha256(path) ⇒ Object
215 216 217 218 219 |
# File 'lib/harnex/artifact_report.rb', line 215 def safe_file_sha256(path) file_sha256(path) if File.file?(path) rescue StandardError nil end |
.safe_file_size(path) ⇒ Object
209 210 211 212 213 |
# File 'lib/harnex/artifact_report.rb', line 209 def safe_file_size(path) File.size(path) if File.file?(path) rescue StandardError nil end |
.string_array(value, max_items:) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/harnex/artifact_report.rb', line 163 def string_array(value, max_items:) Array(value).first(max_items).filter_map do |item| text = bounded_string_or_nil(item) text unless text.nil? || text.empty? end end |
.warning(path, bytes:, sha256:, ingest_status:, warning:, schema: nil) ⇒ Object
105 106 107 108 109 |
# File 'lib/harnex/artifact_report.rb', line 105 def warning(path, bytes:, sha256:, ingest_status:, warning:, schema: nil) report = (path, bytes: bytes, sha256: sha256, ingest_status: ingest_status, schema: schema) report["warning"] = bounded_string(warning) { "artifact_report" => report } end |