rspec_oscal_formatter 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b142acb0a975b36fa4bd8dc44d80c1c6bb8795c142938dff273d6e9dcd76fac2
4
+ data.tar.gz: a54f50d499b2048c20a37e8ce72c7f2b88874d392629bda8cdfe0e260c5796a6
5
+ SHA512:
6
+ metadata.gz: a068241fbe38583fae4483140bf5a044959ec198435461ddd4a29e49ef7f0fd7202753ea21c743479c0d3dba9f02a151a7da0abbe84fbbe544950d4b78f5ed4f
7
+ data.tar.gz: 0c35353fb3b4f817e309b5bb4d7a29aa9d993b344abcc40be53e31f961f7cfc16d95fd3cc823d2ae6417706ac29fe93eb045761d3f36bba757cc823b57a5a058
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-05-29
4
+
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Credentive
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # RSpecOscalFormatter
2
+
3
+ This Library provides an RSpec Formatter that helps you to define tests that align with OSCAL Catalogs and SSPs, and produce Assessment Plan and Assessment Result documents based on the test results.
4
+
5
+ ## Installation
6
+
7
+ Note that this library currently depends on a (temporary) fork of oscal-ruby that includes the artifacts we are interested. We hope to coordinate with that team to merge the codebase and move on to other projects.
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ PLEASE SEE A [DEMO SPEC HERE](https://github.com/Credentive-Sec/rspec_oscal_formatter/blob/main/spec/demo_specs/demo_spec.rb) THAT GIVES AN OVERVIEW OF THE METADATA REQUIRED TO MAKE THIS WORK.
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Credentive-Sec/rspec_oscal_formatter.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'oscal'
5
+
6
+ module RSpec
7
+ module RSpecOscalFormatter
8
+ # Create an assessment plan from the metadata and template
9
+ class CreateAssessmentPlan
10
+ def initialize(metadata)
11
+ @assessment_plan =
12
+ Oscal::AssessmentPlan::AssessmentPlan.new(
13
+ {
14
+ uuid: metadata.assessment_plan_uuid,
15
+ metadata: build_ap_metadata_block(metadata),
16
+ import_ssp: { href: './assessment_plan.json' },
17
+ reviewed_controls: make_reviewed_controls(metadata),
18
+ },
19
+ )
20
+ end
21
+
22
+ def build_ap_metadata_block(metadata)
23
+ {
24
+ title: "Automated Testing Plan for login.gov. It #{metadata.description}",
25
+ last_modified: DateTime.now.iso8601,
26
+ version: DateTime.now.iso8601,
27
+ oscal_version: '1.1.2',
28
+ }
29
+ end
30
+
31
+ def make_reviewed_controls(metadata)
32
+ {
33
+ control_selections: [
34
+ include_controls: [
35
+ {
36
+ control_id: metadata.control_id,
37
+ statement_ids: [metadata.statement_id],
38
+ },
39
+ ],
40
+ ],
41
+ }
42
+ end
43
+
44
+ def get
45
+ @assessment_plan
46
+ end
47
+
48
+ def to_json(*_args)
49
+ @assessment_plan.to_json
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'random/formatter'
5
+
6
+ require 'oscal'
7
+
8
+ module RSpec
9
+ module RSpecOscalFormatter
10
+ # Creates an Assessment Result from an RSpec Unit Test Run
11
+ class CreateAssessmentResult
12
+ def initialize(metadata)
13
+ @assessment_result = Oscal::AssessmentResult::AssessmentResult.new(
14
+ uuid: Random.uuid,
15
+ metadata: build_metadata_block,
16
+ import_ap: { href: './exported_ap.json' }, # This is not correct. Should be dynamic.
17
+ results: create_results_block(metadata),
18
+ )
19
+ end
20
+
21
+ def build_metadata_block
22
+ {
23
+ title: 'Test Result for login.gov.',
24
+ last_modified: DateTime.now.iso8601,
25
+ version: DateTime.now.iso8601,
26
+ oscal_version: '1.1.2',
27
+ }
28
+ end
29
+
30
+ def create_results_block(metadata)
31
+ # TODO: - multiple results per test?
32
+ [{
33
+ uuid: Random.uuid,
34
+ title: metadata.description,
35
+ description: metadata.description,
36
+ start: DateTime.now.iso8601,
37
+ reviewed_controls: create_reviewed_controls(metadata),
38
+ observations: [create_observations(metadata)],
39
+ findings: [create_findings(metadata)],
40
+ }]
41
+ end
42
+
43
+ def create_reviewed_controls(metadata)
44
+ {
45
+ control_selections: [
46
+ {
47
+ include_controls: [
48
+ { control_id: metadata.control_id },
49
+ ],
50
+ },
51
+ ],
52
+ }
53
+ end
54
+
55
+ def create_observations(metadata)
56
+ {
57
+ uuid: Random.uuid,
58
+ title: metadata.description,
59
+ description: metadata.description,
60
+ methods: ['TEST'],
61
+ collected: DateTime.now.iso8601,
62
+ }
63
+ end
64
+
65
+ def create_findings(metadata)
66
+ {
67
+ uuid: Random.uuid,
68
+ title: 'Automated Test Outcome',
69
+ description: metadata.description,
70
+ target: create_target(metadata),
71
+ }
72
+ end
73
+
74
+ def create_target(metadata)
75
+ {
76
+ type: 'statement-id',
77
+ target_id: metadata.statement_id,
78
+ status: {
79
+ state: metadata.state,
80
+ reason: metadata.reason,
81
+ },
82
+ }
83
+ end
84
+
85
+ def get
86
+ @assessment_result
87
+ end
88
+
89
+ def to_json(*_args)
90
+ @assessment_result.to_json
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module RSpecOscalFormatter
5
+ # Tiny helper class to provide easy access to the metadata attributes in the spec.
6
+ class SpecMetaDataParser
7
+ STATUS_MAP = {
8
+ passed: 'pass',
9
+ failed: 'fail',
10
+ pending: 'other',
11
+ }
12
+
13
+ attr_reader(
14
+ *(METADATA = %i[assessment_plan_uuid control_id description statement_id ssp_url reason
15
+ state].freeze),
16
+ )
17
+
18
+ def validate_contents(metadata)
19
+ # Make sure required attributes are present
20
+ METADATA.each do |attribute|
21
+ raise IndexError if attribute.nil?
22
+ end
23
+ end
24
+
25
+ def initialize(example)
26
+ validate_contents(example.metadata) # TODO: - figure out a way to dump out if metadata isn't complete
27
+ @output_directory = example.metadata[:output_directory]
28
+ @assessment_plan_uuid = example.metadata[:assessment_plan_uuid]
29
+ @control_id = example.metadata[:control_id]
30
+ @description = example.metadata[:description]
31
+ @statement_id = example.metadata[:statement_id]
32
+ @reason = STATUS_MAP[example.execution_result.status]
33
+ @state = @reason == 'pass' ? 'satisfied' : 'not-satisfied'
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecOscalFormatter
4
+ VERSION = '0.1.1'
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec'
4
+ require 'date'
5
+ require 'pathname'
6
+
7
+ Dir[File.join(__dir__, "rspec_oscal_formatter", "*.rb")].each { |file| require file }
8
+
9
+ # To format the output of Rspec tests as OSCAL Assessment Plans and Assessment Results
10
+ module RSpec
11
+ module RSpecOscalFormatter
12
+ class Error < StandardError; end
13
+
14
+ # Core class for the formatter
15
+ class Formatter
16
+ RSpec::Core::Formatters.register self, :example_finished
17
+
18
+ OUTPUT_DIRECTORY = Pathname.new('/tmp/oscal_outputs').freeze # TODO: should be a property
19
+
20
+ def initialize(output)
21
+ @output = output
22
+ end
23
+
24
+ # Generate a timestamped directory to save the file
25
+ def create_output_directory
26
+ example_out_dir = OUTPUT_DIRECTORY.join(DateTime.now.iso8601)
27
+ # We should raise an exception here if we can't create the directory
28
+ example_out_dir.mkpath unless example_out_dir.exist? && example_out_dir.directory?
29
+ example_out_dir
30
+ end
31
+
32
+ def example_finished(notification)
33
+ metadata = SpecMetaDataParser.new(notification.example)
34
+
35
+ out_dir = create_output_directory
36
+
37
+ out_dir.join("#{metadata.control_id}-assessment-plan.json").open('w').write(
38
+ CreateAssessmentPlan.new(metadata).to_json,
39
+ )
40
+
41
+ out_dir.join("#{metadata.control_id}-assessment-result.json").open('w').write(
42
+ CreateAssessmentResult.new(metadata).to_json,
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ module RSpecOscalFormatter
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/test.rb ADDED
@@ -0,0 +1,8 @@
1
+ class TestAttr
2
+ attr_reader :test1, :test2
3
+
4
+ def initialize
5
+ @test1 = '1'
6
+ @test2 = '2'
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_oscal_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Sherwood
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oscal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
69
+ description:
70
+ email:
71
+ - robert.sherwood@credentive.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CHANGELOG.md
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - lib/rspec_oscal_formatter.rb
81
+ - lib/rspec_oscal_formatter/create_assessment_plan.rb
82
+ - lib/rspec_oscal_formatter/create_assessment_results.rb
83
+ - lib/rspec_oscal_formatter/parse_spec_metadata.rb
84
+ - lib/rspec_oscal_formatter/version.rb
85
+ - sig/rspec_oscal_formatter.rbs
86
+ - test.rb
87
+ homepage: https://github.com/Credentive-Sec/rspec_oscal_formatter
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ allowed_push_host: https://rubygems.org/
92
+ homepage_uri: https://github.com/Credentive-Sec/rspec_oscal_formatter
93
+ source_code_uri: https://github.com/Credentive-Sec/rspec_oscal_formatter
94
+ changelog_uri: https://github.com/Credentive-Sec/rspec_oscal_formatter/blob/main/CHANGELOG.md
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.5.9
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: An RSpec formatter that allows you to write security focused tests for OSCAL
114
+ catalogs and produce Assessment Plans and Assessment Results.
115
+ test_files: []