gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:33:11 24 4
gpt4 key购买 nike

我正在开发一个运行 CIS 合规性命令的 InSpec 控件。在使用 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 请求,第二个命令是终端命令。

如何将它们(在获取第一个命令的输出并将其放入第二个命令后)放入 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' }

?

另外,我确实尝试了您之前编写的所有 block ,但没有一个有效。是的,我使用的是 linux 16.04

最佳答案

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

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

mysql -u <user> -p -e部分是从 Linux 命令执行 SQL 查询所必需的。如果您使用的是 Window,您可能需要使用 sqlcmd反而。这允许使用 command 成功执行 SQL 查询方法。

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

我知道这里有很多地方可以实现您正在寻找的功能以及许多修复和改进,因此请务必仔细检查代码和解释以了解我在这里所做的一切。

关于mysql - 如何制作一个可以打开mysql的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50153217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com