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

linter: add lint rule for required json arguments #4889

Merged

Conversation

jsternberg
Copy link
Collaborator

@jsternberg jsternberg commented Apr 26, 2024

This lint requires json arguments to be used when the SHELL command isn't used previously for ENTRYPOINT and CMD. This is because using non-json arguments can block signals from properly being handled when used in shell mode.

This uses the Shell attribute on the image configuration and therefore
requires the stage to be included in the build for it to properly run.
This is done because we don't want to force image config resolution on
every stage even if the stage isn't part of the build, but the only way
to definitively know if a custom shell is set is to look at the image
configuration.

return hasShellCommand(ds.stage.BaseName, states)
}

func validateJsonArgs(stage instructions.Stage, allDispatchStates *dispatchStates, warn linter.LintWarnFunc) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter wants this validateJSONArgs

@jsternberg
Copy link
Collaborator Author

After an internal conversation, I've updated this to be more accurate by using the image configuration. As a result, this lint will not be run when a stage is not included in the build, but the accuracy will be 100% with no false positives.

@jsternberg jsternberg force-pushed the lint-json-args-required-sans-shell branch from c306ac6 to 92bbd6a Compare May 6, 2024 16:06
@jsternberg jsternberg force-pushed the lint-json-args-required-sans-shell branch from 92bbd6a to ecc09c1 Compare May 6, 2024 19:14
var shell []string
if len(img.Config.Shell) > 0 {
shell = append([]string{}, img.Config.Shell...)
} else {
if warn != nil {
msg := linter.RuleJSONArgsRequired.Format(cmd.Name())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like a rule directly related to withShell helper, but related to how ENTRYPOINT/CMD work (or more how these properties will work in docker run afterward). I think it makes more sense if there is a

if PrependShell && len(img.Config.Shell) == 0 {
  report()
}

In dispatchArg/Entrypoint.

Warnings: []expectedLintWarning{
{
RuleName: "JSONArgsRequired",
Description: "JSON arguments required unless a custom shell is specified",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"required" may be strong word here as build would work fine without it. "preferred/recommended" maybe. Not sure if we need to mention "custom shell" as well as unlikely that the user would know anything about custom shells (and Dockerfile does not contain any SHELL). The warning is already not shown if there is a custom shell.

@jsternberg jsternberg force-pushed the lint-json-args-required-sans-shell branch from ecc09c1 to cd2038a Compare May 6, 2024 21:10
This lint requires json arguments to be used when the `SHELL` command
isn't used previously for `ENTRYPOINT` and `CMD`. This is because using
non-json arguments can block signals from properly being handled when
used in shell mode.

This uses the `Shell` attribute on the image configuration and therefore
requires the stage to be included in the build for it to properly run.
This is done because we don't want to force image config resolution on
every stage even if the stage isn't part of the build, but the only way
to definitively know if a custom shell is set is to look at the image
configuration.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
@jsternberg jsternberg force-pushed the lint-json-args-required-sans-shell branch from cd2038a to 79b0c96 Compare May 7, 2024 14:21
@tonistiigi tonistiigi merged commit 59adccf into moby:master May 7, 2024
73 checks passed
@jsternberg jsternberg deleted the lint-json-args-required-sans-shell branch May 7, 2024 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants