Skip to content

scripts: add wrapper to update Postfix configuration safely#3484

Merged
georglauterbach merged 6 commits intomasterfrom
scripts/postfix-helper-append
Aug 22, 2023
Merged

scripts: add wrapper to update Postfix configuration safely#3484
georglauterbach merged 6 commits intomasterfrom
scripts/postfix-helper-append

Conversation

@georglauterbach
Copy link
Copy Markdown
Member

@georglauterbach georglauterbach commented Aug 16, 2023

Description

Add functionality to safely and securely append to, prepend to or init options in /etc/postfix/main.cf.

Fixes #3482

Open Tasks

  • Manually check whether docker restart does not append the string twice.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (README.md or the documentation under docs/)
  • If necessary I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

polarathene
polarathene previously approved these changes Aug 16, 2023
Copy link
Copy Markdown
Member

@polarathene polarathene left a comment

Choose a reason for hiding this comment

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

I think I suggested somewhere using postconf instead of sed to set / append values for main.cf, but can't recall if there were any issues with that (beyond the 2 sec delay to read a setting when not using the mtime workaround),

Regardless this helper is awesome (and well documented!), great work 😁

Comment thread target/scripts/helpers/postfix.sh Outdated
Comment thread target/scripts/startup/setup.d/security/rspamd.sh Outdated
Comment thread target/scripts/helpers/postfix.sh Outdated
-e '/\$dkim_milter/! s|^(smtpd_milters =.*)|\1 \$dkim_milter|g' \
-e '/\$dkim_milter/! s|^(non_smtpd_milters =.*)|\1 \$dkim_milter|g' \
/etc/postfix/main.cf
_append_to_option_in_postfix_main 'smtpd_milters' "\$dkim_milter"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
_append_to_option_in_postfix_main 'smtpd_milters' "\$dkim_milter"
_append_to_option_in_postfix_main 'smtpd_milters' '$dkim_milter'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the shellcheck lint will complain (or did with grep unless I did "\$value" instead of '$value'), and may affect sed? I recall some similar problem when passing strings like this to a function that'd use sed with a variable reference...

@georglauterbach has checked that this is working as intended with repeat runs? I don't think we have any specific test for it. My earlier advice on the original approach to this was to just ensure we reset the config files via a copy at startup, avoiding the issue (so long as the change is not applied again via a setup command or triggered by check-for-changes.sh).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@casperklein The quoting is done in order to not use # shellcheck disable=SC... above.

@polarathene I can double-check, but it may also be a good idea to ask @wligtenberg whether #3380 was properly solved for him. If so, this would indicate success. I would check nevertheless, but don't know when exactly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment thread target/scripts/startup/setup.d/dmarc_dkim_spf.sh Outdated
Comment thread target/scripts/startup/setup.d/security/rspamd.sh Outdated
polarathene
polarathene previously approved these changes Aug 17, 2023
georglauterbach and others added 2 commits August 19, 2023 16:29
The new function can

1. update/append
2. update/prepend
3. initialize if non-existent

options in `/etc/postfix/main.cf` in a safe and secure manner. When the
container is improperly restarted, the option is not applied twice.
@georglauterbach
Copy link
Copy Markdown
Member Author

It turned out there were problem with my method indeed, because the escaping was lost on the way. I have revised the functionality again, and checked whether the fix works. While I was at it, it compacted the code and removed superflous functions again.

I rebased on master, please provide a dedicated review again.

polarathene
polarathene previously approved these changes Aug 19, 2023
Copy link
Copy Markdown
Member

@polarathene polarathene left a comment

Choose a reason for hiding this comment

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

LGTM 👍

  • 1 suggestion to consider (optional, but seems like an improvement)
  • References to db.sh shared as similar functionality exists.
    • Although my stance is to avoid adapting db.sh to support main.cf if it'd complicate db.sh further; the PR version here is simpler to grok and maintain in the context of managing main.cf.
    • I caution changing db.sh usage of it's sed helper, but it may not be equivalent as yours? 🤷‍♂️

Comment thread target/scripts/helpers/postfix.sh
Comment thread target/scripts/helpers/utils.sh
@georglauterbach
Copy link
Copy Markdown
Member Author

@polarathene I applied a patch that I derived after looking at your suggestion. This should include the error handling, but keep the whole function as simple as possible. We also use postfconf now.

Copy link
Copy Markdown
Member

@polarathene polarathene left a comment

Choose a reason for hiding this comment

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

I personally think the suggestion would be more maintainer friendly (wishful thinking of the project getting additional maintainers some day..).

If you disagree, @casperklein could weigh in with their opinion, if not I won't fight it and will approve (requested changes instead of approved was only to provide a bit of time to consider the suggestion).

Comment thread target/scripts/helpers/postfix.sh
Copy link
Copy Markdown
Member

@polarathene polarathene left a comment

Choose a reason for hiding this comment

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

Thank you, much appreciated! ❤️

@georglauterbach georglauterbach enabled auto-merge (squash) August 22, 2023 08:02
@georglauterbach georglauterbach merged commit cf9eb82 into master Aug 22, 2023
@georglauterbach georglauterbach deleted the scripts/postfix-helper-append branch August 22, 2023 08:03
@casperklein
Copy link
Copy Markdown
Member

casperklein commented Aug 26, 2023

Merged too fast, couldn't review. Therefore, see my suggestions in the follow up PR #3503.

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

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

bug report: Postfix main.cf:smtpd_milters appends $rspamd_milter on restarts

3 participants