Skip to content

Instantly share code, notes, and snippets.

@colegatron
Last active May 12, 2016 09:48
Show Gist options
  • Save colegatron/00cea2a778cd9448801e9e1e33e4e71e to your computer and use it in GitHub Desktop.
Save colegatron/00cea2a778cd9448801e9e1e33e4e71e to your computer and use it in GitHub Desktop.

Revisions

  1. colegatron renamed this gist May 12, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. colegatron revised this gist May 12, 2016. 2 changed files with 9 additions and 3 deletions.
    8 changes: 6 additions & 2 deletions README
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    This is an update of the Russel's state to use it basically as mail forwarder.
    This is an update of the Russel's state to use it basically as mail forwarder in auto scaled environments.

    The tipycal problem is that if you get notification emails from "[email protected]" in an environment where you really have 5 or 6 webservers for the same application, it is difficult to track back the issue to the right server.

    I have solved it renaming the server and the headers:

    All the emails sent from the server for <myhostname>.domain.com will renamed to <senderuser>@<myhostname-my-ipv4>.domain.com.
    Also adds a header replace to avoid problems with Office365 mail servers, which rejects to send emails with different names on the hostname and the email headers
    Also adds a header replacement to avoid problems with Office365 mail servers, which rejects to send emails with different names on the hostname and the email headers

    Original job: http://russell.ballestrini.net/postfix-salt-state-formula/

    4 changes: 3 additions & 1 deletion salt.postfix.main.cf
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    #
    # Managed by config management
    # See /usr/share/postfix/main.cf.dist for a commented, more complete version
    #

    {% set myhostname = salt["pillar.get"]("postfix:myhostname") %}
    {% set myfakehostname = salt["pillar.get"]("postfix:myfakehostname") %}

    {# This file could be dinamycally modified importing with jinja a different "main.cf-grain-hostname", but there are endless posibilities depending of your own use case #}


    # See /usr/share/postfix/main.cf.dist for a commented, more complete version


    # Debian specific: Specifying a file name will cause the first
  3. colegatron created this gist May 12, 2016.
    8 changes: 8 additions & 0 deletions README
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    This is an update of the Russel's state to use it basically as mail forwarder.

    All the emails sent from the server for <myhostname>.domain.com will renamed to <senderuser>@<myhostname-my-ipv4>.domain.com.
    Also adds a header replace to avoid problems with Office365 mail servers, which rejects to send emails with different names on the hostname and the email headers

    Original job: http://russell.ballestrini.net/postfix-salt-state-formula/

    note: replace "." in file names with "/" to get the right folder structure.
    22 changes: 22 additions & 0 deletions pillar.postfix.postfix-forwarder.sls
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@

    {% set h = salt['grains.get']('host', 'noname') %}
    {% set d = salt['grains.get']('domain', 'nodomain') %}
    {% set i = salt['grains.get']('ipv4', 'nodomain')[1] %}

    {% set myfakehostname = h + "-" + i + "-" + d %}
    {% set myhostname = h + "." + d %}


    postfix:
    # I need this to access to this vars in the states and also in the pillar without having to duplicate var definitions
    myhostname: {{ myhostname }}
    myfakehostname: {{ myfakehostname }}

    # real postfix conf
    aliases: |
    postmaster: root
    root: [email protected]
    sender_canonical_maps: |
    /^(.*)@(.*).domain.com$/ ${1}@{{ myfakehostname }}.domain.com
    header_check: |
    /From:(.*)@{{ myhostname }}.domain.com/ REPLACE From: ${1}@{{ myfakehostname }}.domain.com
    2 changes: 2 additions & 0 deletions salt.postfix.header_check
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Managed by config management
    {{pillar['postfix']['header_check']}}
    127 changes: 127 additions & 0 deletions salt.postfix.init.sls
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    # Install mutt and postfix mutt packages.
    #
    # This formula supports setting an optional:
    #
    # * 'aliases' file
    # * 'virtual' map file
    #
    # Both aliases and virtual use a pillar data schema
    # which takes the following form:
    #
    # postfix:
    # aliases: |
    # postmaster: root
    # root: testuser
    # testuser: [email protected]
    # virtual: |
    # example.com this is a comment
    # [email protected] [email protected]
    # [email protected] [email protected]
    # sender_canonical_maps: |
    # /.+/ [email protected]
    #
    # header_check: |
    # /From:.*/ REPLACE From: [email protected]
    #

    # install mutt
    mutt:
    pkg:
    - installed

    # install postfix have service watch main.cf
    postfix:
    pkg:
    - installed
    service:
    - running
    - enable: True
    - watch:
    - pkg: postfix
    - file: /etc/postfix/main.cf

    # postfix main configuration file
    /etc/postfix/main.cf:
    file.managed:
    - source: salt://postfix/main.cf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - require:
    - pkg: postfix

    # manage /etc/aliases if data found in pillar
    {% if 'aliases' in pillar.get('postfix', '') %}
    /etc/aliases:
    file.managed:
    - source: salt://postfix/aliases
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - require:
    - pkg: postfix

    run-newaliases:
    cmd.wait:
    - name: newaliases
    - cwd: /
    - watch:
    - file: /etc/aliases
    {% endif %}

    # manage /etc/postfix/virtual if data found in pillar
    {% if 'virtual' in pillar.get('postfix', '') %}
    /etc/postfix/virtual:
    file.managed:
    - source: salt://postfix/virtual
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - require:
    - pkg: postfix

    run-postmap:
    cmd.wait:
    - name: /usr/sbin/postmap /etc/postfix/virtual
    - cwd: /
    - watch:
    - file: /etc/postfix/virtual
    {% endif %}


    # manage /etc/postfix/sender_canonical_maps if data found in pillar
    {% if 'sender_canonical_maps' in pillar.get('postfix', '') %}
    /etc/postfix/sender_canonical_maps:
    file.managed:
    - source: salt://postfix/sender_canonical_maps
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - require:
    - pkg: postfix
    - watch_in:
    - service: postfix
    {% endif %}


    # manage /etc/postfix/header_check if data found in pillar
    {% if 'header_check' in pillar.get('postfix', '') %}
    /etc/postfix/header_check:
    file.managed:
    - source: salt://postfix/header_check
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - require:
    - pkg: postfix
    - watch_in:
    - service: postfix
    {% endif %}



    59 changes: 59 additions & 0 deletions salt.postfix.main.cf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #
    # Managed by config management
    #

    {% set myhostname = salt["pillar.get"]("postfix:myhostname") %}
    {% set myfakehostname = salt["pillar.get"]("postfix:myfakehostname") %}


    # See /usr/share/postfix/main.cf.dist for a commented, more complete version


    # Debian specific: Specifying a file name will cause the first
    # line of that file to be used as the name. The Debian default
    # is /etc/mailname.
    #myorigin = /etc/mailname

    smtpd_banner = $myhostname ESMTP $mail_name
    biff = no

    # appending .domain is the MUA's job.
    append_dot_mydomain = no

    # Uncomment the next line to generate "delayed mail" warnings
    #delay_warning_time = 4h

    readme_directory = no

    # TLS parameters
    smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
    smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
    smtpd_use_tls=yes
    smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
    smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

    # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
    # information on enabling SSL in the smtp client.

    sender_canonical_classes = envelope_sender, header_sender
    sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps
    smtp_header_checks = regexp:/etc/postfix/header_check

    myhostname = {{ myhostname }}
    myorigin = {{ myfakehostname }}
    mydestination = {{ myhostname }} localhost

    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases

    relayhost =

    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = all

    {% if 'virtual' in pillar.get('postfix','') %}
    virtual_alias_maps = hash:/etc/postfix/virtual
    {% endif %}
    2 changes: 2 additions & 0 deletions salt.postfix.sender_canonical_maps
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Managed by config management
    {{pillar['postfix']['sender_canonical_maps']}}