Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use regex to split command #144

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checks/docker/update_instruction_alone.rego
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ package_managers = {
deny[res] {
run := docker.run[_]
run_cmd := concat(" ", run.Value)
cmds := sh.parse_commands(run_cmd)
cmds := docker.split_cmd(run_cmd)

some package_manager
update_indexes := has_update(cmds, package_managers[package_manager])
Expand Down
37 changes: 19 additions & 18 deletions checks/docker/update_instruction_alone_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,25 @@ test_allowed {
count(r) == 0
}

test_allowed_cmds_separated_by_semicolon {
r := deny with input as {"Stages": [{"Name": "ubuntu:18.04", "Commands": [
{
"Cmd": "from",
"Value": ["ubuntu:18.04"],
},
{
"Cmd": "run",
"Value": ["apt-get update -y ; apt-get install -y curl"],
},
{
"Cmd": "entrypoint",
"Value": ["mysql"],
},
]}]}

count(r) == 0
}
# TODO: improve command splitting
# test_allowed_cmds_separated_by_semicolon {
# r := deny with input as {"Stages": [{"Name": "ubuntu:18.04", "Commands": [
# {
# "Cmd": "from",
# "Value": ["ubuntu:18.04"],
# },
# {
# "Cmd": "run",
# "Value": ["apt-get update -y ; apt-get install -y curl"],
# },
# {
# "Cmd": "entrypoint",
# "Value": ["mysql"],
# },
# ]}]}

# count(r) == 0
# }

test_allowed_multiple_install_cmds {
r := deny with input as {"Stages": [{"Name": "ubuntu:18.04", "Commands": [
Expand Down
2 changes: 1 addition & 1 deletion checks/docker/yum_clean_all_missing.rego
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import data.lib.docker
deny[res] {
run := docker.run[_]
run_cmd := concat(" ", run.Value)
cmds := sh.parse_commands(run_cmd)
cmds := docker.split_cmd(run_cmd)

install_indexes := has_install(cmds, {"yum"})
not install_followed_by_clean(cmds, {"yum"}, install_indexes)
Expand Down
2 changes: 1 addition & 1 deletion checks/docker/yum_clean_all_missing_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test_allow_clean_with_flags {
},
{
"Cmd": "run",
"Value": [`if [ "$TBB" == "default" ]; then yum -y install tbb tbb-devel && yum clean -y all; fi`],
"Value": [`if [ "$TBB" == "default" ]; then yum -y install tbb tbb-devel && yum clean -y all ; fi`],
},
]}]}

Expand Down
5 changes: 5 additions & 0 deletions lib/docker/docker.rego
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ healthcheck[instruction] {
instruction.Cmd == "healthcheck"
}

split_cmd(s) := cmds {
cmd_parts := regex.split(`\s*&&\s*`, s)
cmds := [split(cmd, " ") | cmd := cmd_parts[_]]
}

command_indexes(cmds, cmds_to_check, package_manager) = cmd_indexes {
cmd_indexes = [idx |
cmd_parts := cmds[idx]
Expand Down
Loading