Skip to content

Commit 203af26

Browse files
committed
feat: sub-argument matching for arguments in build as code and build service
Signed-off-by: Tim Yarkov <[email protected]>
1 parent 4e5815d commit 203af26

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/macaron/slsa_analyzer/checks/build_as_code_check.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,27 @@ def _has_deploy_command(self, commands: list[list[str]], build_tool: BaseBuildTo
120120
logger.info("No deploy arguments required. Accept %s as deploy command.", str(com))
121121
return str(com)
122122

123-
for word in com[(prog_name_index + 1) :]:
123+
for i, word in enumerate(com[(prog_name_index + 1) :]):
124124
# TODO: allow plugin versions in arguments, e.g., maven-plugin:1.6.8:deploy.
125125
if word in build_tool.deploy_arg:
126126
logger.info("Found deploy command %s.", str(com))
127127
return str(com)
128+
129+
# Check all required deploy arguments match
130+
for arg in build_tool.deploy_arg:
131+
match = True
132+
133+
for deploy_sub, com_sub in zip(com[i + 1 :], arg.split(" ")):
134+
if deploy_sub != com_sub:
135+
match = False
136+
break
137+
138+
if match:
139+
logger.info("Found deploy command %s.", str(com))
140+
return str(com)
141+
142+
return ""
143+
128144
return ""
129145

130146
def _check_build_tool(

src/macaron/slsa_analyzer/checks/build_service_check.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,27 @@ def _has_build_command(self, commands: list[list[str]], build_tool: BaseBuildToo
110110
if not build_tool.build_arg:
111111
logger.info("No build arguments required. Accept %s as build command.", str(com))
112112
return str(com)
113-
for word in com[(prog_name_index + 1) :]:
114-
# TODO: allow plugin versions in arguments, e.g., maven-plugin:1.6.8:package.
113+
114+
for i, word in enumerate(com[(prog_name_index + 1) :]):
115+
# TODO: allow plugin versions in arguments, e.g., maven-plugin:1.6.8:deploy.
115116
if word in build_tool.build_arg:
116-
logger.info("Found build command %s.", str(com))
117+
logger.info("Found deploy command %s.", str(com))
117118
return str(com)
119+
120+
# Check all required deploy arguments match
121+
for arg in build_tool.build_arg:
122+
match = True
123+
124+
for build_sub, com_sub in zip(com[i + 1 :], arg.split(" ")):
125+
if build_sub != com_sub:
126+
match = False
127+
break
128+
129+
if match:
130+
logger.info("Found deploy command %s.", str(com))
131+
return str(com)
132+
133+
return ""
118134
return ""
119135

120136
def _check_build_tool(

tests/slsa_analyzer/checks/test_build_as_code_check.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from macaron.slsa_analyzer.build_tool.maven import Maven
1717
from macaron.slsa_analyzer.build_tool.pip import Pip
1818
from macaron.slsa_analyzer.build_tool.poetry import Poetry
19+
from macaron.slsa_analyzer.build_tool.yarn import Yarn
1920
from macaron.slsa_analyzer.checks.build_as_code_check import BuildAsCodeCheck
2021
from macaron.slsa_analyzer.checks.check_result import CheckResult, CheckResultType
2122
from macaron.slsa_analyzer.ci_service.circleci import CircleCI
@@ -33,6 +34,7 @@ def test_build_as_code_check(
3334
gradle_tool: Gradle,
3435
poetry_tool: Poetry,
3536
pip_tool: Pip,
37+
yarn_tool: Yarn,
3638
github_actions_service: GitHubActions,
3739
jenkins_service: Jenkins,
3840
travis_service: Travis,
@@ -187,6 +189,13 @@ def test_build_as_code_check(
187189
multi_deploy.dynamic_data["ci_services"] = [ci_info]
188190
assert check.run_check(multi_deploy, check_result) == CheckResultType.PASSED
189191

192+
# Using Yarn with multi-word deploy command
193+
multi_deploy = MockAnalyzeContext(macaron_path=macaron_path, output_dir="")
194+
multi_deploy.dynamic_data["build_spec"]["tools"] = [yarn_tool]
195+
bash_commands["commands"] = [["yarn", "npm", "publish"]]
196+
multi_deploy.dynamic_data["ci_services"] = [ci_info]
197+
assert check.run_check(multi_deploy, check_result) == CheckResultType.PASSED
198+
190199
# Using both gradle and maven, but maven incorrect (singular failure in a list)
191200
multi_deploy = MockAnalyzeContext(macaron_path=macaron_path, output_dir="")
192201
multi_deploy.dynamic_data["build_spec"]["tools"] = [gradle_tool, maven_tool]

0 commit comments

Comments
 (0)