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 two import problems: snippets and import literals. #2205

Merged
merged 5 commits into from
Jun 28, 2018

Conversation

augustoroman
Copy link

@augustoroman augustoroman commented Jun 16, 2018

Two problems are fixed by this code simplification:

  1. Snippets defined in one import file are strangely not available in
    another.
  2. If an imported file had a directive with an argument "import", then
    the rest of the tokens on the line would be converted to absolute
    filepaths.

An example of 2 would be the following directive in an imported file:

server {
    basicauth / import secret
}

In this case, the password would actually be an absolute path to the
file 'secret' (whether or not it exists) in the directory of the imported
Caddyfile.

The problem was the blind token processing to fix import paths in the
imported tokens without considering the context of the 'import' token.

My first inclination was to just add more context (detect 'import' tokens
at the beginning of lines and check the value tokens against defined
snippets), however I eventually realized that we already do all of this
in the parser, so the code was redundant. Instead we just use the current
token's File property when importing. This works fine with imported tokens
since they already have the absolute path to the imported file!

Fixes #2204

Two problems are fixed by this code simplification:
1. Snippets defined in one import file are strangely not available in
   another.
2. If an imported file had a directive with an argument "import", then
   the rest of the tokens on the line would be converted to absolute
   filepaths.

An example of caddyserver#2 would be the following directive in an imported file:
    basicauth / import secret

In this case, the password would actually be an absolute path to the
file 'secret' (whether or not it exists) in the directory of the imported
Caddyfile.

The problem was the blind token processing to fix import paths in the
imported tokens without considering the context of the 'import' token.

My first inclination was to just add more context (detect 'import' tokens
at the beginning of lines and check the value tokens against defined
snippets), however I eventually realized that we already do all of this
in the parser, so the code was redundant. Instead we just use the current
token's File property when importing. This works fine with imported tokens
since they already have the absolute path to the imported file!

Fixes caddyserver#2204
@mholt
Copy link
Member

mholt commented Jun 16, 2018

Ooo, nice! I'm looking forward to going over these changes. Of course, I invite any other collaborators to give it a review too, since they'll probably be able to sooner than I am.

}

func TestImportedFilesIgnoreNonDirectiveImportTokens(t *testing.T) {
file2 := writeStringToTempFileOrDie(t, `
Copy link

@theodesp theodesp Jun 16, 2018

Choose a reason for hiding this comment

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

Can you name it just fileName for readability?

Copy link
Author

Choose a reason for hiding this comment

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

sure, renamed

@augustoroman
Copy link
Author

ping? Is this ready to merge?

@mholt mholt merged commit 053373a into caddyserver:master Jun 28, 2018
@mholt
Copy link
Member

mholt commented Jun 28, 2018

Thanks @augustoroman -- I love how the solution was basically to delete code and add tests. If you'd like to be a collaborator on the project to help review PRs and issues and get push access, let me know. :)

@augustoroman augustoroman deleted the snippet-fixes branch June 28, 2018 16:53
admpub added a commit to admpub/caddy that referenced this pull request Jul 1, 2018
caddyfile: Fix multi-file snippets and import literals. (caddyserver#2205)
@dpantel dpantel mentioned this pull request Nov 11, 2018
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.

snippets don't work across files
3 participants