Skip to content

Commit 0d28f1c

Browse files
authored
inspec2ckl SIDATA parse stigid and title from profile names (#234)
* Update metadata.json examples for inspc2ckl * inspec2ckl parses Profile names and sets the CKL metadata correctly. inspec2ckl will now parse overlayed Profile and single Profile results for the name of the profile. This information will be set in the SIDATA CKL XML tag. inspec2ckl now creates both a title SIDATA XML element and a stigid SIDATA XML element using the new parsed Profile name as the data. inspec2ckl now has the following hierarchy of inheritance for SIDATA data: * Provided metadata * Parsed Profile name * '' string
1 parent 45820f5 commit 0d28f1c

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

examples/inspec2ckl/metadata.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
{
22
"stigid" : "RHEL_7_STIG",
3+
"role": "my role",
4+
"type": "my type",
5+
"tech_area": "my area",
6+
"target_area": "my area",
7+
"web_or_database": "my database",
8+
"web_db_site": "my web_db site",
9+
"web_db_instance": "my web_db instance",
310
"hostname" : "myawesome",
411
"ip" : "10.10.10.10",
512
"fqdn" : "myawesome.host.com",
613
"mac" : "aa:aa:99:99:99:99"
7-
}
14+
}

lib/happy_mapper_tools/stig_checklist.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Asset
2727
# Class Asset maps from the 'SI_DATA' from Checklist XML file using HappyMapper
2828
class SiData
2929
include HappyMapper
30+
31+
def initialize(name, data)
32+
self.name = name
33+
self.data = data
34+
end
3035
tag 'SI_DATA'
3136
element :name, String, tag: 'SID_NAME'
3237
element :data, String, tag: 'SID_DATA'
@@ -35,6 +40,11 @@ class SiData
3540
# Class Asset maps from the 'STIG_INFO' from Checklist XML file using HappyMapper
3641
class StigInfo
3742
include HappyMapper
43+
44+
def initialize(si_data)
45+
self.si_data = si_data
46+
end
47+
3848
tag 'STIG_INFO'
3949
has_many :si_data, SiData, tag: 'SI_DATA'
4050
end
@@ -68,6 +78,11 @@ class Vuln
6878
# Class Asset maps from the 'iSTIG' from Checklist XML file using HappyMapper
6979
class IStig
7080
include HappyMapper
81+
82+
def initialize(stig_info, vulns)
83+
self.stig_info = stig_info
84+
self.vuln = vulns
85+
end
7186
tag 'iSTIG'
7287
has_one :stig_info, StigInfo, tag: 'STIG_INFO'
7388
has_many :vuln, Vuln, tag: 'VULN'
@@ -76,6 +91,10 @@ class IStig
7691
# Class Asset maps from the 'STIGS' from Checklist XML file using HappyMapper
7792
class Stigs
7893
include HappyMapper
94+
95+
def initialize(istig)
96+
self.istig = istig
97+
end
7998
tag 'STIGS'
8099
has_one :istig, IStig, tag: 'iSTIG'
81100
end

lib/inspec_tools/inspec.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ def to_csv
6161

6262
private
6363

64+
def topmost_profile_name
65+
find_topmost_profile_name(0)
66+
end
67+
68+
def find_topmost_profile_name(index, parent_name = nil)
69+
# Return nil when the index is out of bounds.
70+
# nil returned here will set the profile name to '' in the calling functions.
71+
return nil if index > @json['profiles'].length - 1
72+
73+
# No parent profile means this is the parent
74+
if !@json['profiles'][index].key?('parent_profile') && (@json['profiles'][index]['name'] == parent_name || index.zero?)
75+
# For the initial case, parent_name will be nil, and if we are already at the parent index is also zero
76+
return @json['profiles'][index]['name']
77+
end
78+
79+
parent_name = @json['profiles'][index]['parent_profile']
80+
find_topmost_profile_name(index + 1, parent_name)
81+
end
82+
6483
###
6584
# This method converts an inspec json to an array of arrays
6685
#
@@ -111,28 +130,19 @@ def update_ckl
111130
end
112131

113132
def generate_ckl
114-
stigs = HappyMapperTools::StigChecklist::Stigs.new
115-
istig = HappyMapperTools::StigChecklist::IStig.new
116-
117133
vuln_list = []
118134
@data.keys.each do |control_id|
119135
vuln_list.push(generate_vuln_data(@data[control_id]))
120136
end
121137

122-
si_data = HappyMapperTools::StigChecklist::SiData.new
123-
si_data.name = 'stigid'
124-
si_data.data = ''
125-
if !@metadata['stigid'].nil?
126-
si_data.data = @metadata['stigid']
127-
end
138+
si_data_data = @metadata['stigid'] || topmost_profile_name || ''
139+
si_data_stigid = HappyMapperTools::StigChecklist::SiData.new('stigid', si_data_data)
140+
si_data_title = HappyMapperTools::StigChecklist::SiData.new('title', si_data_data)
128141

129-
stig_info = HappyMapperTools::StigChecklist::StigInfo.new
130-
stig_info.si_data = si_data
131-
istig.stig_info = stig_info
142+
stig_info = HappyMapperTools::StigChecklist::StigInfo.new([si_data_stigid, si_data_title])
132143

133-
istig.vuln = vuln_list
134-
stigs.istig = istig
135-
@checklist.stig = stigs
144+
istig = HappyMapperTools::StigChecklist::IStig.new(stig_info, vuln_list)
145+
@checklist.stig = HappyMapperTools::StigChecklist::Stigs.new(istig)
136146

137147
@checklist.asset = generate_asset
138148
end

0 commit comments

Comments
 (0)