|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require_relative 'hyperproof/config' |
3 | 4 | require_relative 'hyperproof/entities/label' |
4 | 5 | require_relative 'hyperproof/entities/proof' |
5 | 6 | require_relative 'hyperproof/proofs' |
6 | 7 |
|
7 | 8 | module CfaSecurityControls |
8 | 9 | # Top level module for our gem. |
9 | 10 | module Hyperproof |
| 11 | + @mutex = Mutex.new |
| 12 | + |
| 13 | + # Set or load the system configuration. |
| 14 | + # |
| 15 | + # If no configuration is explicitly set, it will be loaded from the |
| 16 | + # environment. |
| 17 | + # |
| 18 | + # @param config [Config] Configuration to set for the system. |
| 19 | + def self.config(config = nil) |
| 20 | + @mutex.synchronize do |
| 21 | + if config |
| 22 | + @config = config |
| 23 | + else |
| 24 | + @config ||= Config.from_environment |
| 25 | + end |
| 26 | + end |
| 27 | + end |
| 28 | + |
10 | 29 | # Collect and upload all proofs to Hyperproof. |
11 | 30 | def self.run |
12 | 31 | Dir.mktmpdir do |dir| |
| 32 | + config.logger.info("Writing proofs to #{dir}") |
13 | 33 | writer = Writer.new(dir) |
14 | 34 | Proofs.proofs.map do |klass| |
15 | 35 | proof = klass.new |
16 | | - filename = proof.write(writer) |
17 | | - |
18 | | - label = Entities::Label.new(proof.label) |
19 | | - label.create unless label.exists? |
20 | | - Entities::Proof.new(File.basename(filename), label:).create(filename) |
| 36 | + filename = collect_proof(proof, writer) |
| 37 | + Entities::Proof.new(File.basename(filename), label: proof_label(proof)) |
| 38 | + .create(filename) |
21 | 39 | end |
22 | 40 | end |
23 | 41 | end |
| 42 | + |
| 43 | + # Collect evidence for a specific proof. |
| 44 | + # |
| 45 | + # @param proof [Proofs::Proof] The proof to collect evidence for. |
| 46 | + # @param writer [Writer] The writer to use for formatting the proof. |
| 47 | + # @return [String] The filename where the proof was written. |
| 48 | + private_class_method def self.collect_proof(proof, writer) |
| 49 | + config.logger.debug("Collecting proof for #{proof.name} (#{proof.label})") |
| 50 | + proof.write(writer) |
| 51 | + end |
| 52 | + |
| 53 | + # Get the label for a proof. |
| 54 | + # |
| 55 | + # @param proof [Proofs::Proof] The proof to get the label for. |
| 56 | + # @return [Entities::Label] The label entity for the proof. |
| 57 | + private_class_method def self.proof_label(proof) |
| 58 | + label = Entities::Label.new(proof.label) |
| 59 | + label.create unless label.exists? |
| 60 | + label |
| 61 | + end |
24 | 62 | end |
25 | 63 | end |
0 commit comments