Skip to content

tests(fix): Avoid introducing CRLF into postfix-accounts.cf during setup#2820

Merged
georglauterbach merged 1 commit intodocker-mailserver:masterfrom
polarathene:fix/avoid-introducing-crlf-into-config
Oct 9, 2022
Merged

tests(fix): Avoid introducing CRLF into postfix-accounts.cf during setup#2820
georglauterbach merged 1 commit intodocker-mailserver:masterfrom
polarathene:fix/avoid-introducing-crlf-into-config

Conversation

@polarathene
Copy link
Copy Markdown
Member

Description

test/tests.bats has been extremely unreliable during the setup stage. This should finally resolve that. Big thanks to @casperklein and @georglauterbach who have helped rule out other causes to narrow it down to this.

Recent changes I introduced to tests.bats better surfaced change detection problems more consistently, to the point failure frequency was extremely probable. I investigated and identified the cause 🎉

Currently when a change detection would be triggered and during processing, a CRLF is found and converted to a LF, which modifies the postfix-accounts.cf file, triggering another change event 😬

Marked for v11.2.0 as it's only a change to our tests and should make existing PRs much happier :)


Solution: Avoid introducing CRLF into config by adding accounts only via setup email add

There is no need for the first approach used in tests.bats to add an account, and it is the culprit for causing the CRLF to appear.

It is unclear why a CRLF is added to the end of the file, only when using docker run with the DMS docker image to output a postfix-accounts.cf config line via echo and using that stdout result to append (>>) to the postfix-accounts.cf.

It did not seem to occur via docker exec and performing a similar command within the container to update the file internally, nor without docker by updating postfix-accounts.cf in the same manner directly to the file at the volume mount location.

Additional Details

Potential run-time modification of configs intended for fixing to keep in mind

In future, the following could be considered for removal, which may require better handling of the scripts (or ideally, porting to a more capable language where this type of problem would not cause bugs when processing configs):

sed -i 's|\r||g' "${DATABASE_ACCOUNTS}"

sed -i -e '$a\' "${DATABASE_ACCOUNTS}"

sed -i 's|\r||g' "${DATABASE_DOVECOT_MASTERS}"

sed -i -e '$a\' "${DATABASE_DOVECOT_MASTERS}"

sed -i -e "s|, |,|g" -e "s|,$||g" "${DATABASE_VIRTUAL}"

I do not have the time to ensure removing those modifications would not risk causing problems with our scripts that read them. We also have another issue open regarding making a copy of config files to read to avoid issues with something else modifying the file while we process it.

History of these particular lines

  • sed -i -e '$a\' /tmp/postfix/accounts.cf introduced in Aug 2015 by original DMS project author. No PR, but does reference an issue the commit is meant to resolve - however I don't see anything in the discussion that looks related to it being added. I assume it's mostly specific to compatibility with shell scripts and unix utilities that expect it?
  • sed -i 's/\r//g' /tmp/docker-mailserver/postfix-accounts.cf introduced April 2016, which references an issue where a user experienced a bug from their postfix-accounts.cf having a CRLF present, scripts did not generate accounts in Dovecot /etc/dovecot/userdb properly.
  • sed -i -e "s|, |,|g" -e "s|,$||g" "${DATABASE_VIRTUAL}" introduced in March 2018 to better support alias management (notably setup alias del) so that , would be normalized to always having a space suffix , .

Technically, if config files are only managed by our setup command, then there should be no need for any of these I think? Users may manually edit the files directly or have setup some other way to modify / create the configs, like the legacy make generate-accounts approach and what we see here in tests.bats.

Thus these are more a precaution / safe-guard. If we don't care about external config updates being made while the container is running, and not having change events "repair" / fix those potential issues, then we could move these into startup or setup fixes, or into the docs.

Otherwise they're not likely to cause much trouble, they were just silently messing with this particular test file for some time. While make generate-accounts probably introduces CRLF as well, those were handled during startup before a checksum was made for the change detector, thus already fixed ahead of time.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • New and existing unit tests pass locally with my changes

…setup

Currently a change detection would be triggered and during processing, a CRLF is converted to LF, which updates the `postfix-accounts.cf` file and triggers another change event.

No need for the first approach to add an account, and it is the culprit for causing the CRLF to appear.
Copy link
Copy Markdown
Member

@casperklein casperklein left a comment

Choose a reason for hiding this comment

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

Currently when a change detection would be triggered and during processing, a CRLF is found and converted to a LF, which modifies the postfix-accounts.cf file, triggering another change event 😬

Very nice investigation 👍

@casperklein
Copy link
Copy Markdown
Member

casperklein commented Oct 9, 2022

It is unclear why a CRLF is added to the end of the file,

I found out, that the -t switch here is the cause for that:

docker run --rm -e [email protected] -e MAIL_PASS=mypassword -t "${NAME}" /bin/sh -c 'echo "${MAIL_USER}|$(doveadm pw -s SHA512-CRYPT -u ${MAIL_USER} -p ${MAIL_PASS})"' >> "${PRIVATE_CONFIG}/postfix-accounts.cf"

Proof:

docker run --rm debian bash -c "echo foo" > bar; hd bar
00000000  66 6f 6f 0a                                       |foo.|

Same, but with -t

docker run --rm -t debian bash -c "echo foo" > bar; hd bar
00000000  66 6f 6f 0d 0a                                    |foo..|

We see, that there is an additional carriage return \r --> 0d added.

@polarathene
Copy link
Copy Markdown
Member Author

I found out, that the -t switch here is the cause for that

Very interesting, thanks for sharing that finding with --tty introducing it 🤯

@casperklein
Copy link
Copy Markdown
Member

casperklein commented Oct 9, 2022

In my test branch, tests did immediately run successfully, after removing -t 🎉

The solution in this PR is much clearer, than just removing -t 👍

I don't understand, why this was used in the first place:

docker run ... > file-on-host
docker exec ... # modify file directly in the container

Why were there two methods used for adding accounts to a running container 🤷

@georglauterbach georglauterbach merged commit 70ad765 into docker-mailserver:master Oct 9, 2022
@georglauterbach
Copy link
Copy Markdown
Member

Hooray :D

PS: Notices that CI does not use cache here as well. GH had some recent downtime, maybe there now is something wrong with our cache? I will have a look. The changes themselves should definitely not have anything to do with the current caching issue. The issue itself is minor as well, as we only loose a minute, but annoying nevertheless.

@polarathene
Copy link
Copy Markdown
Member Author

maybe there now is something wrong with our cache? I will have a look.

It is using the same cache-key as I only made a change to a test which doesn't warrant caching a new image.

I assume something didn't cache well with that key, but once we have a new one we'll have a better idea about cache working properly or not.

@polarathene
Copy link
Copy Markdown
Member Author

Why were there two methods used for adding accounts to a running container

Since it was for tests.bats my guess is it's legacy from before when it was all in a Makefile, especially since we have similar with make generate-accounts. Then when setup commands arrived to handle account management better, they added the addmailuser command to test that was working too, when maybe we didn't have much tests for that support until later?

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.

3 participants