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_outcome(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
193 194 195 196 197 198 |
# File 'lib/harnex/artifact_report.rb', line 193 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
187 188 189 190 191 |
# File 'lib/harnex/artifact_report.rb', line 187 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 94 95 |
# 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"]) outcome = compact_outcome(report["outcome"]) 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["outcome"] = outcome if outcome payload end |
.compact_artifacts(value) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/harnex/artifact_report.rb', line 164 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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/harnex/artifact_report.rb', line 134 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_outcome(value) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/harnex/artifact_report.rb', line 149 def compact_outcome(value) return nil unless value.is_a?(Hash) status = value["status"].to_s return nil unless %w[accepted rejected no_change unknown].include?(status) payload = { "status" => status, "summary" => bounded_string_or_nil(value["summary"]), "commit_sha" => bounded_string_or_nil(value["commit_sha"]) } payload.delete_if { |_key, item| item.nil? } payload end |
.compact_validation(value) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/harnex/artifact_report.rb', line 123 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
217 218 219 220 221 222 223 224 |
# File 'lib/harnex/artifact_report.rb', line 217 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
208 209 210 211 212 213 214 215 |
# File 'lib/harnex/artifact_report.rb', line 208 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
200 201 202 203 204 205 206 |
# File 'lib/harnex/artifact_report.rb', line 200 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
113 114 115 116 117 118 119 120 121 |
# File 'lib/harnex/artifact_report.rb', line 113 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
97 98 99 100 101 102 103 104 105 |
# File 'lib/harnex/artifact_report.rb', line 97 def missing(path) warning( path, bytes: nil, sha256: nil, ingest_status: "missing", warning: "artifact report not found" ) end |
.safe_file_sha256(path) ⇒ Object
232 233 234 235 236 |
# File 'lib/harnex/artifact_report.rb', line 232 def safe_file_sha256(path) file_sha256(path) if File.file?(path) rescue StandardError nil end |
.safe_file_size(path) ⇒ Object
226 227 228 229 230 |
# File 'lib/harnex/artifact_report.rb', line 226 def safe_file_size(path) File.size(path) if File.file?(path) rescue StandardError nil end |
.string_array(value, max_items:) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/harnex/artifact_report.rb', line 180 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
107 108 109 110 111 |
# File 'lib/harnex/artifact_report.rb', line 107 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 |