有没有办法包含基于正则表达式的“依赖”配置文件的控件?如果没有,有没有一种方法可以包含所有控件,并在所有控件上覆盖固定值的影响?
代码应该看起来像这样,其中'controlname‘是需要以某种方式确定的变量:
include_controls 'depends-profile' do
if controlname.match(/some regex/)
control *controlname*
impact 1.0
end
end目标是避免必须单独添加所有控件。
稍微挖掘一下,我得到了这样的结论:
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有没有办法以一种整洁和未来的方式来实现这一点?
发布于 2021-03-26 20:03:25
解决方案可能如下所示:
include_controls '<dependent-profile>' do
# Scan through all the controls we pulled in
profile_context.all_controls.each do |c|
# Grab the control name from control c
handled_control_name = c.instance_variable_get(:@__rule_id)
# If it matches the regex
if handled_control_name =~ /<myRegex>/
# Overwrite the impact and tags
control handled_control_name do
impact 'critical'
tag 'myTag'
end
end
end
endhttps://stackoverflow.com/questions/65932231
复制相似问题