Module: AbideDevUtils::XCCDF::Common

Included in:
Benchmark, ObjectContainer, XccdfElement
Defined in:
lib/abide_dev_utils/xccdf.rb

Overview

Common constants and methods included by nearly everything else

Constant Summary collapse

XPATHS =
{
  benchmark: {
    all: 'xccdf:Benchmark',
    title: 'xccdf:Benchmark/xccdf:title',
    version: 'xccdf:Benchmark/xccdf:version'
  },
  cis: {
    profiles: {
      all: 'xccdf:Benchmark/xccdf:Profile',
      relative_title: './xccdf:title',
      relative_select: './xccdf:select'
    }
  }
}.freeze
CONTROL_PREFIX =
/^[\d.]+_/.freeze
UNDERSCORED =
/(\s|\(|\)|-|\.)/.freeze
CIS_NEXT_GEN_WINDOWS =
/[Nn]ext_[Gg]eneration_[Ww]indows_[Ss]ecurity/.freeze
CIS_CONTROL_NUMBER =
/([0-9.]+[0-9]+)/.freeze
CIS_LEVEL_CODE =
/(?:_|^)([Ll]evel_[0-9]|[Ll]1|[Ll]2|[NnBb][GgLl]|#{CIS_NEXT_GEN_WINDOWS})/.freeze
CIS_CONTROL_PARTS =
/#{CIS_CONTROL_NUMBER}#{CIS_LEVEL_CODE}?_+([A-Za-z].*)/.freeze
CIS_PROFILE_PARTS =
/#{CIS_LEVEL_CODE}[_-]+([A-Za-z].*)/.freeze
STIG_PROFILE_PARTS =
/(STIG)/.freeze

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



162
163
164
# File 'lib/abide_dev_utils/xccdf.rb', line 162

def ==(other)
  diff_properties.map { |x| send(x) } == other.diff_properties.map { |x| other.send(x) }
end

#abide_object?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/abide_dev_utils/xccdf.rb', line 166

def abide_object?
  true
end

#control_parts(control, parent_level: nil) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/abide_dev_utils/xccdf.rb', line 132

def control_parts(control, parent_level: nil)
  mdata = control_profile_text(control).match(CIS_CONTROL_PARTS)
  raise AbideDevUtils::Errors::ControlPartsError, control if mdata.nil?

  mdata[2] = parent_level unless parent_level.nil?
  mdata[1..3]
end

#control_profile_text(item) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/abide_dev_utils/xccdf.rb', line 140

def control_profile_text(item)
  return item.raw_title if item.respond_to?(:abide_object?)

  if item.respond_to?(:split)
    return item.split('benchmarks_rule_')[-1] if item.include?('benchmarks_rule_')

    item.split('benchmarks_profile_')[-1]
  else
    return item['idref'].to_s.split('benchmarks_rule_')[-1] if item.name == 'select'

    item['id'].to_s.split('benchmarks_profile_')[-1]
  end
end

#name_normalize_control(control) ⇒ Object



109
110
111
# File 'lib/abide_dev_utils/xccdf.rb', line 109

def name_normalize_control(control)
  normalize_string(control_profile_text(control).gsub(CONTROL_PREFIX, ''))
end

#normalize_control_name(control, number_format: false) ⇒ Object



103
104
105
106
107
# File 'lib/abide_dev_utils/xccdf.rb', line 103

def normalize_control_name(control, number_format: false)
  return number_normalize_control(control) if number_format

  name_normalize_control(control)
end

#normalize_profile_name(prof, **_opts) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/abide_dev_utils/xccdf.rb', line 94

def normalize_profile_name(prof, **_opts)
  prof_name = normalize_string("profile_#{control_profile_text(prof)}").dup
  prof_name.gsub!(CIS_NEXT_GEN_WINDOWS, 'ngws')
  prof_name.delete_suffix!('_environment_general_use')
  prof_name.delete_suffix!('sensitive_data_environment_limited_functionality')
  prof_name.strip!
  prof_name
end

#normalize_string(str) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/abide_dev_utils/xccdf.rb', line 83

def normalize_string(str)
  nstr = str.dup.downcase
  nstr.gsub!(/[^a-z0-9]$/, '')
  nstr.gsub!(/^[^a-z]/, '')
  nstr.gsub!(/(?:_|^)([Ll]1_|[Ll]2_|ng_)/, '')
  nstr.delete!('(/|\\|\+)')
  nstr.gsub!(UNDERSCORED, '_')
  nstr.strip!
  nstr
end

#number_normalize_control(control) ⇒ Object



113
114
115
116
# File 'lib/abide_dev_utils/xccdf.rb', line 113

def number_normalize_control(control)
  numpart = CONTROL_PREFIX.match(control_profile_text(control)).to_s.chop.gsub(UNDERSCORED, '_')
  "c#{numpart}"
end

#profile_parts(profile) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/abide_dev_utils/xccdf.rb', line 122

def profile_parts(profile)
  return ['STIG', ''] if profile == 'STIG'

  parts = control_profile_text(profile).match(CIS_PROFILE_PARTS)
  raise AbideDevUtils::Errors::ProfilePartsError, profile if parts.nil?

  parts[1].gsub!(/[Ll]evel_/, 'L')
  parts[1..2]
end

#sorted_control_classes(raw_select_list, sort_key: :number) ⇒ Object



154
155
156
# File 'lib/abide_dev_utils/xccdf.rb', line 154

def sorted_control_classes(raw_select_list, sort_key: :number)
  raw_select_list.map { |x| Control.new(x) }.sort_by(&sort_key)
end

#sorted_profile_classes(raw_profile_list, sort_key: :title) ⇒ Object



158
159
160
# File 'lib/abide_dev_utils/xccdf.rb', line 158

def sorted_profile_classes(raw_profile_list, sort_key: :title)
  raw_profile_list.map { |x| Profile.new(x) }.sort_by(&sort_key)
end

#text_normalize(control) ⇒ Object



118
119
120
# File 'lib/abide_dev_utils/xccdf.rb', line 118

def text_normalize(control)
  control_profile_text(control).tr('_', ' ')
end

#validate_xccdf(path) ⇒ Object



79
80
81
# File 'lib/abide_dev_utils/xccdf.rb', line 79

def validate_xccdf(path)
  AbideDevUtils::Validate.file(path, extension: '.xml')
end

#xpath(path) ⇒ Object



75
76
77
# File 'lib/abide_dev_utils/xccdf.rb', line 75

def xpath(path)
  @xml.xpath(path)
end