Skip to content

Improve dockerfile directive parsing#27270

Closed
tonistiigi wants to merge 2 commits intomoby:masterfrom
tonistiigi:parse-directives
Closed

Improve dockerfile directive parsing#27270
tonistiigi wants to merge 2 commits intomoby:masterfrom
tonistiigi:parse-directives

Conversation

@tonistiigi
Copy link
Member

Refactor so that new directives can be easily added in the future by just adding a new lines in https://github.com/tonistiigi/docker/blob/parse-directives/builder/dockerfile/parser/parser.go#L150-L152 . Simplify so that there is no need to store an instance of directives with the builder or pass it to the Parse() function.

Fix bug where directive with whitespace prefix would still be applied.

In current version, error handling was inconsistent. Specifying invalid escape character would not produce an error but using escape directive twice would. Changed it so that if the directive has correct syntax and valid directive key, it will fail with a proper error.

cc @jhowardmsft

Copy link
Contributor

Choose a reason for hiding this comment

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

github shows some funky chars at the end - is that due to a missing crlf? Was that on purpose and part of the test? if so, let's add a comment so that people don't edit the file and add it back in by mistake.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, this doesn't change the test. Only thing we are testing is if the first line sets escape char or not.

Copy link
Member

Choose a reason for hiding this comment

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

Wasn't deliberately missed.

Copy link
Contributor

Choose a reason for hiding this comment

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

why was the char removed? Or, why did it work before?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bug in the previous implementation that allowed garbage data after the directive https://github.com/docker/docker/blob/61753568f5e482c551df6f3693cb7c942261b7de/builder/dockerfile/parser/parser.go#L51 . Only ` and \ are valid escape characters.

Copy link
Member

Choose a reason for hiding this comment

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

Actually it was kinda by design ;) The escape semantic was # escape = char. So it literally deliberately only interpreted the char bit, and ignored anything after it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jhowardmsft It would be very easy to fix but I don't agree this is correct. Directive syntax should be # key=value, not # key=value+any garbage. Directives in general are not limited to single char.

Copy link
Member

Choose a reason for hiding this comment

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

I don't disagree with you in the slightest. Just commenting on why it was as it was - I know that bit came up in one of the many conversations when introducing the feature 😇

Copy link
Member

Choose a reason for hiding this comment

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

I guess the comment could be updated here for this line.

Copy link
Member Author

Choose a reason for hiding this comment

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

added

Copy link
Member

Choose a reason for hiding this comment

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

This concerns me causing in panic in the daemon...

Copy link
Member Author

Choose a reason for hiding this comment

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

There is no way for any external data to cause this. This can only happen if code change introduces a bug. This is functionally equal to MustCompile of a regexp.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, but I'd still rather just log something.

Copy link
Member

Choose a reason for hiding this comment

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

Is this returned externally? I don't believe we ever returned an error back to the caller on a bad directive, just silently swallowed it (by design).

Copy link
Member Author

Choose a reason for hiding this comment

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

This error is for catching the "silent error", if parsing reaches this error it will not be reported but parsing will just continue.

Copy link
Member

Choose a reason for hiding this comment

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

Got it 👍

Copy link
Member

Choose a reason for hiding this comment

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

I thought this was swallowed (by design) before and just ignored, the whole line being treated as a comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

This causes a real error in current version and in this version.

Copy link
Member

Choose a reason for hiding this comment

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

OK. I'd forgotten that then, but it was a while back 😇

@thaJeztah
Copy link
Member

Should we wait for #24725 to be merged? That one has been open for a long time

@lowenna
Copy link
Member

lowenna commented Oct 11, 2016

@thaJeztah No need - they're not at all related.

Refactor so that new directives can be easily
added in the future.

Fix bug where directive with whitespace prefix
would still be applied.

Signed-off-by: Tonis Tiigi <[email protected]>
@lowenna
Copy link
Member

lowenna commented Oct 11, 2016

Seems reasonable to me. Moving to code review unless any objection from @duglin or @thaJeztah. Probably worth squashing the commits. My only real thing is the just-in-case panic but otherwise looks good.

@thaJeztah
Copy link
Member

ping @duglin @jhowardmsft PTAL!


# Switch back to root and double check that worked exactly as we might expect it to
USER root
# Add a "supplementary" group for our dockerio user
Copy link
Member

Choose a reason for hiding this comment

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

Should this be in a separate PR? Doesn't seem related...

Copy link
Member Author

Choose a reason for hiding this comment

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

It is needed. I thought I left a comment for that in this pr... . It is the same fix as in https://github.com/docker/docker/pull/24725/files#diff-64d1c3a97fd8b1abf170680883e81aaaR3580 . Doesn't matter how we handle empty lines, this Dockerfile should not be parseable.

@lowenna
Copy link
Member

lowenna commented Oct 21, 2016

Minor comments, LGTM when those are addressed.

@LK4D4
Copy link
Contributor

LK4D4 commented Nov 10, 2016

@tonistiigi needs rebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants