Skip to content

Commit d7c9302

Browse files
authored
fix(ds0014): check curl and wget usage per stage (#546)
* refactor: restructure instruction filtering helpers Signed-off-by: Nikita Pivkin <[email protected]> * fix(ds0014): check curl and wget usage per stage Signed-off-by: Nikita Pivkin <[email protected]> --------- Signed-off-by: Nikita Pivkin <[email protected]>
1 parent 87242b4 commit d7c9302

3 files changed

Lines changed: 66 additions & 58 deletions

File tree

checks/docker/run_using_wget_and_curl.rego

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ import rego.v1
2727
import data.lib.docker
2828

2929
deny contains res if {
30-
wget := get_tool_usage(docker.run[_], "wget")
31-
curl := get_tool_usage(docker.run[_], "curl")
30+
some _, runs in docker.stage_instructions("run")
31+
wget := get_tool_usage(runs[_], "wget")
32+
curl := get_tool_usage(runs[_], "curl")
3233

3334
count(wget) > 0
3435
count(curl) > 0
@@ -46,7 +47,7 @@ get_tool_usage(cmd, cmd_name) := r if {
4647

4748
commands_list = regex.split(`\s*&&\s*`, cmd.Value[0])
4849

49-
reg_exp = sprintf("^( )*%s", [cmd_name])
50+
reg_exp = sprintf(`^\s*%s`, [cmd_name])
5051

5152
r := [x |
5253
instruction := commands_list[_]

checks/docker/run_using_wget_and_curl_test.rego

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,36 @@ test_install_allowed if {
119119

120120
count(r) == 0
121121
}
122+
123+
test_multi_stage_allowed if {
124+
r := deny with input as {"Stages": [
125+
{
126+
"Name": "builder:1",
127+
"Commands": [
128+
{
129+
"Cmd": "from",
130+
"Value": ["alpine"],
131+
},
132+
{
133+
"Cmd": "run",
134+
"Value": ["wget http://google.com"],
135+
},
136+
],
137+
},
138+
{
139+
"Name": "runtime:1",
140+
"Commands": [
141+
{
142+
"Cmd": "from",
143+
"Value": ["alpine"],
144+
},
145+
{
146+
"Cmd": "run",
147+
"Value": ["curl http://bing.com"],
148+
},
149+
],
150+
},
151+
]}
152+
153+
count(r) == 0
154+
}

lib/docker/docker.rego

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,74 +8,48 @@ package lib.docker
88

99
import rego.v1
1010

11-
from contains instruction if {
12-
instruction := input.Stages[_].Commands[_]
13-
instruction.Cmd == "from"
14-
}
11+
from := instructions("from")
1512

16-
add contains instruction if {
17-
instruction := input.Stages[_].Commands[_]
18-
instruction.Cmd == "add"
19-
}
13+
add := instructions("add")
2014

21-
run contains instruction if {
22-
instruction := input.Stages[_].Commands[_]
23-
instruction.Cmd == "run"
24-
}
15+
run := instructions("run")
2516

26-
copy contains instruction if {
27-
instruction := input.Stages[_].Commands[_]
28-
instruction.Cmd == "copy"
29-
}
17+
copy := instructions("copy")
3018

31-
stage_copies[stage] := copies if {
32-
stage := input.Stages[_]
33-
copies := [copy | copy := stage.Commands[_]; copy.Cmd == "copy"]
34-
}
19+
entrypoint := instructions("entrypoint")
3520

36-
entrypoint contains instruction if {
37-
instruction := input.Stages[_].Commands[_]
38-
instruction.Cmd == "entrypoint"
39-
}
21+
expose := instructions("expose")
4022

41-
stage_entrypoints[stage] := entrypoints if {
42-
stage := input.Stages[_]
43-
entrypoints := [entrypoint | entrypoint := stage.Commands[_]; entrypoint.Cmd == "entrypoint"]
44-
}
23+
user := instructions("user")
4524

46-
stage_cmd[stage] := cmds if {
47-
stage := input.Stages[_]
48-
cmds := [cmd | cmd := stage.Commands[_]; cmd.Cmd == "cmd"]
49-
}
25+
workdir := instructions("workdir")
5026

51-
stage_healthcheck[stage] := hlthchecks if {
52-
stage := input.Stages[_]
53-
hlthchecks := [hlthcheck | hlthcheck := stage.Commands[_]; hlthcheck.Cmd == "healthcheck"]
54-
}
27+
healthcheck := instructions("healthcheck")
5528

56-
stage_user[stage] := users if {
57-
stage := input.Stages[_]
58-
users := [cmd | cmd := stage.Commands[_]; cmd.Cmd == "user"]
59-
}
29+
stage_copies := stage_instructions("copy")
6030

61-
expose contains instruction if {
62-
instruction := input.Stages[_].Commands[_]
63-
instruction.Cmd == "expose"
64-
}
31+
stage_entrypoints := stage_instructions("entrypoint")
6532

66-
user contains instruction if {
67-
instruction := input.Stages[_].Commands[_]
68-
instruction.Cmd == "user"
69-
}
33+
stage_run := stage_instructions("run")
7034

71-
workdir contains instruction if {
72-
instruction := input.Stages[_].Commands[_]
73-
instruction.Cmd == "workdir"
74-
}
35+
stage_cmd := stage_instructions("cmd")
7536

76-
healthcheck contains instruction if {
77-
instruction := input.Stages[_].Commands[_]
78-
instruction.Cmd == "healthcheck"
37+
stage_healthcheck := stage_instructions("healthcheck")
38+
39+
stage_user := stage_instructions("user")
40+
41+
instructions(typ) := [inst |
42+
some stage in input.Stages
43+
some inst in stage.Commands
44+
inst.Cmd == typ
45+
]
46+
47+
stage_instructions(typ) := {stage: instructions |
48+
some stage in input.Stages
49+
instructions := [inst |
50+
some inst in stage.Commands
51+
inst.Cmd == typ
52+
]
7953
}
8054

8155
split_cmd(s) := sh.parse_commands(s)

0 commit comments

Comments
 (0)