control_check 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 363814479b2fc693997b3cdf0992a3c8e1af6c4cf4aefb5e97f9a29623e71cff
4
- data.tar.gz: 6f739fd188bb3256e9e2d0ced5dfe84c7da5e3f9ac93e87367e87c85fea96b94
3
+ metadata.gz: 0a2607cb0bbaab3681b426073f695951aedb5585ef9223b222ad63c9fc951fe0
4
+ data.tar.gz: 3f164d5dc6cd3af36d63fc72dd77c90724bbb25353c88eb66b52618aa1fbc709
5
5
  SHA512:
6
- metadata.gz: e9675c929eb5911fe75c54c3187be154522ebc69d2baa2a8265d723996216476c360c2f3572399cb8e65e13a1f1d292e1cc3295fcc65306685a7ec3dfe6dd878
7
- data.tar.gz: 440e3919c28b232105d594dee4d47cbc2428e131d2d45d7fe8b7266072b3c06af19b4c144ba6238afe0cd0e6605518da6dabed90d9c754850c5183b67d20fd2c
6
+ metadata.gz: 2c5b1f04d0998b08ef6de077001b9a1c75d51ce47662e4dfeb9e1d0a851d7788231ff0c0c2ec186e543880c53fe548667bfba2b94273408a2327c8d25ab6d68a
7
+ data.tar.gz: dc29cb6545bd64239472fd02edf3411eb9b29d0b6fde8457f51523ef995c322cd03f7b8e731a2753098954cd11ca579dfa3bff463151e91e47671f819dc457df
data/README.md CHANGED
@@ -86,6 +86,24 @@ secrets:
86
86
  line: 2
87
87
  ```
88
88
 
89
+ To name the criteria from published standards that a control supports, add a `criteria`
90
+ section listing, under each control's name, a `standard` and a `reference` for each
91
+ criterion. The controls it may name are `encryption`, `secrets`, `support_session`,
92
+ `access_trail` and `retention`. The list only records which requirement each control
93
+ relates to, and the checker does not judge whether the repo meets it:
94
+
95
+ ```yaml
96
+ criteria:
97
+ encryption:
98
+ - standard: SOC 2
99
+ reference: CC6.1
100
+ - standard: PCI DSS
101
+ reference: "3.5"
102
+ retention:
103
+ - standard: GDPR
104
+ reference: Article 5(1)(e)
105
+ ```
106
+
89
107
  Run the check from the root of the codebase:
90
108
 
91
109
  ```sh
@@ -190,18 +208,38 @@ The check stops, writes no report, prints a message and exits with status 2 when
190
208
  not committed. The message names it.
191
209
  - The control file holds a key the checker does not know, at any level. The message names
192
210
  the key.
211
+ - A criterion under `criteria` has no `standard` or no `reference`, or is not written as
212
+ the two. The message names the control.
193
213
  - A Ruby file a control reads, or `db/schema.rb`, does not parse. The message names its path.
194
214
 
195
215
  ### The report
196
216
 
197
- The report names the commit, the checker version and the SHA-256 digest of the control
198
- file, and holds each control's result. Its keys are sorted and it holds no time, so two
199
- runs on one commit with one checker version write the same bytes.
217
+ The report names the commit, the checker version, the SHA-256 digest of the control
218
+ file and the number of committed files the run read, and holds each control's result. Its
219
+ keys are sorted and it holds no time, so two runs on one commit with one checker version
220
+ write the same bytes.
221
+
222
+ Each control's result names under `items_checked` how many items the control checked, and
223
+ says `0` when it checked none:
224
+
225
+ - Encryption: each attribute the control file names.
226
+ - Support session: each committed Ruby file it searched under `app/`, `hubs/`, `lib/` and
227
+ `config/`.
228
+ - Access trail: each action the control file names.
229
+ - Retention: each record the control file names.
230
+ - Secrets: each committed file it scanned, leaving out encrypted credentials files.
231
+
232
+ The report's `criteria` names, under each control the run checked, the criteria the
233
+ control file lists for it, and an empty list for a control it lists none for.
200
234
 
201
235
  ### The summary
202
236
 
203
- The summary names the commit, the checker version, each control as `passed` or
204
- `failed`, and the SHA-256 digest of the report file.
237
+ The summary names the commit, the checker version, the SHA-256 digest of the control
238
+ file as committed at that commit, each control as `passed` or `failed`, the standard
239
+ criteria each control supports under `criteria`, the number of committed files the run read
240
+ under `files_read`, how many items each control checked under `items_checked`, and the
241
+ SHA-256 digest of the report file. A repo that publishes its control file lets a reader confirm
242
+ it is the file the results came from.
205
243
 
206
244
  ### Exit status
207
245
 
@@ -29,7 +29,9 @@ module ControlCheck
29
29
  def report
30
30
  control_file = read_control_file
31
31
  declared = ControlFile.new(control_file).declared
32
- Report.new(commit: @files.commit, control_file: control_file, controls: controls(declared))
32
+ results = controls(declared)
33
+ Report.new(commit: @files.commit, control_file: control_file, controls: results, files_read: @files.files_read,
34
+ criteria: criteria(declared, results.keys))
33
35
  end
34
36
 
35
37
  private
@@ -42,6 +44,11 @@ module ControlCheck
42
44
  results.merge(declared_controls(declared))
43
45
  end
44
46
 
47
+ def criteria(declared, names)
48
+ listed = declared.fetch("criteria", {})
49
+ names.to_h { |name| [name, listed.fetch(name, [])] }
50
+ end
51
+
45
52
  def declared_controls(declared)
46
53
  DECLARED_CONTROLS.select { |name| declared.key?(name) }.to_h { |name| [name, send(name, declared[name])] }
47
54
  end
@@ -8,6 +8,7 @@ module ControlCheck
8
8
  class CommittedFiles
9
9
  def initialize(root)
10
10
  @root = root
11
+ @read = Set.new
11
12
  end
12
13
 
13
14
  def commit
@@ -18,11 +19,17 @@ module ControlCheck
18
19
  git("ls-tree", "-r", "-z", "--name-only", "HEAD").split("\0")
19
20
  end
20
21
 
22
+ def files_read
23
+ @read.size
24
+ end
25
+
21
26
  def read(path)
27
+ @read << path
22
28
  git("cat-file", "blob", "HEAD:#{path}")
23
29
  end
24
30
 
25
31
  def read_all(paths)
32
+ @read.merge(paths)
26
33
  requests = paths.map { |path| "HEAD:#{path}\n" }.join
27
34
  output = StringIO.new(git("cat-file", "--batch", stdin_data: requests, binmode: true))
28
35
  paths.to_h { |path| [path, next_blob(output)] }
@@ -7,13 +7,16 @@ module ControlCheck
7
7
  class ControlFile
8
8
  PATH = ".control_check.yml"
9
9
  ANY_NAME = "*"
10
+ CRITERIA = [{ "standard" => nil, "reference" => nil }].freeze
11
+ CONTROLS = %w[encryption secrets support_session access_trail retention].freeze
10
12
  KEYS = {
11
13
  "encryption" => nil,
12
14
  "not_sensitive" => nil,
13
15
  "secrets" => { "sample_values" => [{ "file" => nil, "line" => nil }] },
14
16
  "support_session" => { "sign_in_method" => nil, "support_session_method" => nil, "sign_in_files" => nil },
15
17
  "access_trail" => { "recording_call" => nil, "actions" => nil },
16
- "retention" => { ANY_NAME => { "period" => nil, "job" => nil, "gem" => nil } }
18
+ "retention" => { ANY_NAME => { "period" => nil, "job" => nil, "gem" => nil } },
19
+ "criteria" => CONTROLS.to_h { |name| [name, CRITERIA] }
17
20
  }.freeze
18
21
 
19
22
  def initialize(source)
@@ -23,11 +26,24 @@ module ControlCheck
23
26
  def declared
24
27
  declared = YAML.safe_load(@source) || {}
25
28
  refuse_unknown(declared, KEYS, [])
29
+ refuse_incomplete_criteria(declared.fetch("criteria", {}) || {})
26
30
  declared
27
31
  end
28
32
 
29
33
  private
30
34
 
35
+ def refuse_incomplete_criteria(criteria)
36
+ criteria.each do |control, entries|
37
+ Array(entries).product(CRITERIA.first.keys).each { |entry, key| refuse_missing(entry, key, control) }
38
+ end
39
+ end
40
+
41
+ def refuse_missing(entry, key, control)
42
+ return if entry.is_a?(Hash) && !entry[key].to_s.strip.empty?
43
+
44
+ raise Error, "#{PATH} lists a criterion under criteria.#{control} with no #{key}."
45
+ end
46
+
31
47
  def refuse_unknown(value, keys, path)
32
48
  case [keys, value]
33
49
  in [Array, Array] then value.each { |entry| refuse_unknown(entry, keys.first, path) }
@@ -12,7 +12,7 @@ module ControlCheck
12
12
 
13
13
  def result
14
14
  found = findings
15
- { "passed" => found.empty?, "actions" => actions, "findings" => found }
15
+ { "passed" => found.empty?, "actions" => actions, "findings" => found, "items_checked" => actions.size }
16
16
  end
17
17
 
18
18
  private
@@ -17,7 +17,8 @@ module ControlCheck
17
17
 
18
18
  def result
19
19
  found = findings
20
- { "passed" => found.empty?, "attributes" => attributes, "findings" => found }
20
+ { "passed" => found.empty?, "attributes" => attributes, "findings" => found,
21
+ "items_checked" => attributes.size }
21
22
  end
22
23
 
23
24
  private
@@ -16,7 +16,8 @@ module ControlCheck
16
16
 
17
17
  def result
18
18
  found = findings
19
- { "passed" => found.empty?, "checks" => CHECKS, "records" => records, "findings" => found }
19
+ { "passed" => found.empty?, "checks" => CHECKS, "records" => records, "findings" => found,
20
+ "items_checked" => records.size }
20
21
  end
21
22
 
22
23
  private
@@ -27,18 +27,22 @@ module ControlCheck
27
27
 
28
28
  def result
29
29
  found = findings
30
- { "passed" => found.empty?, "findings" => found }
30
+ { "passed" => found.empty?, "findings" => found, "items_checked" => scanned.size }
31
31
  end
32
32
 
33
33
  private
34
34
 
35
35
  def findings
36
36
  refuse_missing_sample_files
37
- sorted = @sources.reject { |path, _| path.match?(ENCRYPTED_CREDENTIALS) }.sort
37
+ sorted = scanned.sort
38
38
  sorted.filter_map { |path, _| secrets_file(path) } +
39
39
  sorted.flat_map { |path, source| shaped_values(path, source) }
40
40
  end
41
41
 
42
+ def scanned
43
+ @sources.reject { |path, _| path.match?(ENCRYPTED_CREDENTIALS) }
44
+ end
45
+
42
46
  def refuse_missing_sample_files
43
47
  missing = @config.fetch("sample_values", []).map { |sample| sample["file"] }.uniq - @sources.keys
44
48
  raise NotInCode, "file #{missing.first}" if missing.any?
@@ -19,7 +19,7 @@ module ControlCheck
19
19
 
20
20
  def result
21
21
  found = findings
22
- { "passed" => found.empty?, "findings" => found }
22
+ { "passed" => found.empty?, "findings" => found, "items_checked" => @index.file_count }
23
23
  end
24
24
 
25
25
  private
@@ -8,10 +8,12 @@ module ControlCheck
8
8
  class Report
9
9
  CONTROL_FILE_PATH = ".control_check.yml"
10
10
 
11
- def initialize(commit:, control_file:, controls:)
11
+ def initialize(commit:, control_file:, controls:, files_read:, criteria: {})
12
12
  @commit = commit
13
13
  @control_file = control_file
14
14
  @controls = controls
15
+ @files_read = files_read
16
+ @criteria = criteria
15
17
  end
16
18
 
17
19
  def to_h
@@ -19,7 +21,9 @@ module ControlCheck
19
21
  "checker_version" => VERSION,
20
22
  "commit" => @commit,
21
23
  "control_file" => { "path" => CONTROL_FILE_PATH, "sha256" => Digest::SHA256.hexdigest(@control_file) },
22
- "controls" => @controls
24
+ "controls" => @controls,
25
+ "criteria" => @criteria,
26
+ "files_read" => @files_read
23
27
  }
24
28
  end
25
29
 
@@ -7,7 +7,10 @@ module ControlCheck
7
7
  Definition = Struct.new(:name, :file, :line, :calls)
8
8
  Call = Struct.new(:name, :file, :line)
9
9
 
10
+ attr_reader :file_count
11
+
10
12
  def initialize(sources)
13
+ @file_count = sources.size
11
14
  @definitions = []
12
15
  @calls = []
13
16
  ClassReader.read(sources) { |path| Collector.new(self, path) }
@@ -14,13 +14,28 @@ module ControlCheck
14
14
  {
15
15
  "checker_version" => report["checker_version"],
16
16
  "commit" => report["commit"],
17
- "controls" => report["controls"].sort.to_h { |name, result| [name, result["passed"] ? "passed" : "failed"] },
17
+ "control_file_sha256" => report["control_file"]["sha256"],
18
+ "files_read" => report["files_read"],
18
19
  "report_sha256" => Digest::SHA256.hexdigest(@report.to_json)
19
- }
20
+ }.merge(per_control(report)).sort.to_h
20
21
  end
21
22
 
22
23
  def to_json(*)
23
24
  "#{JSON.pretty_generate(to_h)}\n"
24
25
  end
26
+
27
+ private
28
+
29
+ def per_control(report)
30
+ {
31
+ "controls" => each_control(report) { |result| result["passed"] ? "passed" : "failed" },
32
+ "criteria" => report["criteria"].sort.to_h,
33
+ "items_checked" => each_control(report) { |result| result["items_checked"] }
34
+ }
35
+ end
36
+
37
+ def each_control(report)
38
+ report["controls"].sort.to_h { |name, result| [name, yield(result)] }
39
+ end
25
40
  end
26
41
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ControlCheck
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: control_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tylercschneider