From dca3f116a41af5476a8abf860d0dda3b4c80f8b5 Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Jun 10 2020 18:35:38 +0000 Subject: IPA-EPN: Add mail-test option for testing sending live email To make testing easier for administrators the --mail-test option can be used to send live e-mail from ipa-epn. It sends mail to the smtp_admin user processing the template with dummy data. https://pagure.io/freeipa/issue/3687 Signed-off-by: Rob Crittenden Reviewed-By: Francois Cami --- diff --git a/client/man/epn.conf.5 b/client/man/epn.conf.5 index f63fd17..9682d43 100644 --- a/client/man/epn.conf.5 +++ b/client/man/epn.conf.5 @@ -66,6 +66,10 @@ Specifies the number of seconds to wait for SMTP to respond. Default 60. .B smtp_security Specifies the type of secure connection to make. Options are: none, starttls and ssl. The default is none. .TP +.B smtp_admin
+Specifies the From e-mail address value in the e-mails sent. The default is +root@localhost. Bounces will be sent here. +.TP .B mail_from
Specifies the From: e-mal address value in the e-mails sent. The default is noreply@ipadefaultemaildomain. This value can be found by running diff --git a/client/man/ipa-epn.1 b/client/man/ipa-epn.1 index 33b9ee9..9999ea8 100644 --- a/client/man/ipa-epn.1 +++ b/client/man/ipa-epn.1 @@ -51,6 +51,12 @@ See \fB\-\-to\-nbdays\fR for an explanation. This option must be used in conjonc The \fB\-\-dry\-run\fR CLI option is intented to test ipa\-epn's configuration. For instance, if notify_ttls is set to 21, 14, 3, \fB\-\-dry-run\fR would display the list of users whose passwords would expire in 21, 14, and 3 days in the future. +.TP +\fB\-\-mail\-test\fR +The \fB\-\-mail\-test\fR CLI option will send an e-mail to the configured +smtp_admin value in /etc/ipa/epn.conf. Generic values for the substitution +variables are set so this is also useful for testing and configuring the +mail template. .SH "TEMPLATE" The template for the e\-mail message is contained in /etc/ipa/epn/expire_msg.template. The following template variables are available. diff --git a/ipaclient/install/ipa_epn.py b/ipaclient/install/ipa_epn.py index bf1644e..2bf9198 100644 --- a/ipaclient/install/ipa_epn.py +++ b/ipaclient/install/ipa_epn.py @@ -226,6 +226,13 @@ class EPN(admintool.AdminTool): default=False, help="Dry run mode. JSON ouput only.", ) + parser.add_option( + "--mail-test", + dest="mailtest", + action="store_true", + default=False, + help="Send a test e-mail", + ) def validate_options(self): super(EPN, self).validate_options(needs_root=True) @@ -235,6 +242,10 @@ class EPN(admintool.AdminTool): self.option_parser.error( "You cannot specify --from-nbdays without --to-nbdays" ) + if self.options.mailtest and self.options.dry_run: + self.option_parser.error( + "You cannot specify --mail-test and --dry-run together" + ) def setup_logging(self, log_file_mode="a"): super(EPN, self).setup_logging(log_file_mode="a") @@ -254,11 +265,14 @@ class EPN(admintool.AdminTool): self._get_connection() self._read_ipa_configuration() drop_privileges() - if self.options.to_nbdays: - self._build_cli_date_ranges() - for date_range in self._date_ranges: - self._fetch_data_from_ldap(date_range) - self._parse_ldap_data() + if self.options.mailtest: + self._gentestdata() + else: + if self.options.to_nbdays: + self._build_cli_date_ranges() + for date_range in self._date_ranges: + self._fetch_data_from_ldap(date_range) + self._parse_ldap_data() if self.options.dry_run: self._pretty_print_data() else: @@ -499,6 +513,20 @@ class EPN(admintool.AdminTool): expdate) self._mailer.cleanup() + def _gentestdata(self): + """Generate a sample user to process through the template. + """ + expdate = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') + entry = dict( + uid=["SAUSER"], + cn=["SAMPLE USER"], + givenname=["SAMPLE"], + sn=["USER"], + krbpasswordexpiration=[expdate], + mail=[api.env.smtp_admin], + ) + self._expiring_password_user_list.add(entry) + def _build_cli_date_ranges(self): """When self.options.to_nbdays is set, override the date ranges read from the configuration file and build the date ranges from the CLI