我正在开发一个inspec控件来运行cis遵从性命令。在mysql上工作时,我被困在这里:
执行以下sql语句以确定 datadir
:
show variables where variable_name = 'datadir';
我需要从上面的命令中提取输出并在下一个命令中重用它:
ls -l <THE OUTPUT OF THE PREVIOUS COMMAND>/.. | egrep "^d[r|w|x]{3}------\s*.\s*mysql\s*mysql\s*\d*.*mysql"
问题是第一个命令是sql请求,第二个命令是terminal命令。
如何将这两个命令(在获得第一个命令的输出并将其放入第二个命令后)放入inspec控件中,如下所示:
control "mysql1" do
impact 1.0
title "Use dedicated Least Privileged Account for MySQL Daemon/Service"
desc "May reduce the impact of a MySQL-born vulnerability"
describe command ('ps -ef |e grep "^mysql.*$"') do
its('stdout') { should match ''}
end
end
谢谢你的帮助@matt
我读了你的答案,发现它真的很有用,除了最后一段代码:does
egrep "^d[r|w|x]{3}------\s*.\s*mysql\s*mysql\s*\d*.*mysql"
意思是
it { expect(subject).to_not be_owned_by 'mysql' }
it { expect(subject).to_not be_grouped_into 'mysql' }
it { expect(subject).to_not be_executable_by 'mysql' }
?
另外,我尝试了你以前写的所有模块,但没有一个成功。。是的,我使用的是linux 16.04
1条答案
按热度按时间vngu2lb81#
可以使用以下方法提取sql请求的输出:
这个
mysql -u <user> -p -e
部分是从linux命令执行sql查询所必需的。如果您使用的是window,则可能需要使用sqlcmd
相反。这使得sql查询可以使用command
方法。原因是
command
方法在这里工作是因为它是一个定制的rspec类型(因此隐式地也是一个类构造函数,在ruby有构造函数的意义上),它将在本地或远程的测试系统上执行。这个.stdout
方法是捕获命令标准输出的类的成员。.split
将确保输出变量存储在以空格分隔的数组中。现在我们可以在下一个命令中使用它,如下所示:
在上面,我们遍历sql查询中捕获的变量数组,并在
describe command()
rspec试验。执行此测试的更好方法是测试command
在匹配器中而不是egrep
. 这样做和清理match
方法:更新到未弃用的rspec匹配器并修复
stdout
方法作为字符串而不是符号:我们可以做的另一个改进是使用更合适的
file
键入和权限匹配器,而不是原始命令。这有助于平台独立测试:我知道这里有一个很好的位来实现您正在寻找的功能,以及许多修复和改进,所以一定要仔细检查代码和解释,以了解我在这里所做的一切。