如何制作一个可以打开mysql的程序

35g0bw71  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(259)

我正在开发一个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

vngu2lb8

vngu2lb81#

可以使用以下方法提取sql请求的输出:

command('mysql -u <user> -p -e "show variables where variable_name = \'datadir\'"').stdout.split(' ')

这个 mysql -u <user> -p -e 部分是从linux命令执行sql查询所必需的。如果您使用的是window,则可能需要使用 sqlcmd 相反。这使得sql查询可以使用 command 方法。
原因是 command 方法在这里工作是因为它是一个定制的rspec类型(因此隐式地也是一个类构造函数,在ruby有构造函数的意义上),它将在本地或远程的测试系统上执行。这个 .stdout 方法是捕获命令标准输出的类的成员。 .split 将确保输出变量存储在以空格分隔的数组中。
现在我们可以在下一个命令中使用它,如下所示:


# store array of variables

variables = command('mysql -u <user> -p -e "show variables where variable_name = \'datadir\'"').stdout.split(' ')

# use array in command

variables.each do |variable|
  describe command("ls -l #{variable}/.. | egrep \"^d[r|w|x]{3}------\s*.\s*mysql\s*mysql\s*\d*.*mysql\"") do
    its('stdout') { should match ''}
  end
end

在上面,我们遍历sql查询中捕获的变量数组,并在 describe command() rspec试验。执行此测试的更好方法是测试 command 在匹配器中而不是 egrep . 这样做和清理 match 方法:


# store array of variables

variables = command('mysql -u <user> -p -e "show variables where variable_name = \'datadir\'"').stdout.split(' ')

# use array in command

variables.each do |variable|
  describe command("ls -l #{variable}/..") do
    its('stdout') { should_not match(/^d[r|w|x]{3}------\s*.\s*mysql\s*mysql\s*\d*.*mysql/)}
  end
end

更新到未弃用的rspec匹配器并修复 stdout 方法作为字符串而不是符号:


# store array of variables

variables = command('mysql -u <user> -p -e "show variables where variable_name = \'datadir\'"').stdout.split(' ')

# use array in command

variables.each do |variable|
  describe command("ls -l #{variable}/..") do
    its(:stdout) { is_expected.to_not match(/^d[r|w|x]{3}------\s*.\s*mysql\s*mysql\s*\d*.*mysql/)}
  end
end

我们可以做的另一个改进是使用更合适的 file 键入和权限匹配器,而不是原始命令。这有助于平台独立测试:


# store array of variables

variables = command('mysql -u <user> -p -e "show variables where variable_name = \'datadir\'"').stdout.split(' ')

# use array in file type

variables.each do |variable|
  describe file("#{variable}/..") do
    # check permissions
    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' }
  end
end

我知道这里有一个很好的位来实现您正在寻找的功能,以及许多修复和改进,所以一定要仔细检查代码和解释,以了解我在这里所做的一切。

相关问题