Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

Inspec include_controls or require_controls based on regex

Is there a way to include controls of a 'depends' profile based on a regex? And if not, is there a way to include all controls, and overwrite impact with fixed value on all controls?

Code should look something like this, where 'controlname' is the variable that is required to be determined somehow:

include_controls 'depends-profile' do
  if controlname.match(/some regex/)    
     control *controlname*
     impact 1.0
  end
end

Goal is to prevent having to add all controls individually.

A bit digging delivered me this:

   include_controls 'dependent-profile' do
      list_of_controls = @conf['profile'].runner_context.rules.keys
      list_of_controls.map { |path| path.gsub(@conf['profile'].profile_name+'/','') }
      list_of_controls.each do |controlname|
          if controlname.match(/some regex/)
             control controlname do        # include and overwrite impact
                impact 0.1
             end
          end
          if controlname.match(/some other regex/)
             control controlname           # just include
          end
      end
   end

Any ideas how to accomplish this in a neat and future proof way?

question from:https://stackoverflow.com/questions/65932231/inspec-include-controls-or-require-controls-based-on-regex

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...