Skip to content

Commit

Permalink
Pull request update/241115
Browse files Browse the repository at this point in the history
576441e OS-7978. Add support for Not Set region filter
af39756 OS-7971. Allow none for region_is rule conditions
72747ac OS-7906. Added sending email compatibility with AWS SES
  • Loading branch information
stanfra authored Nov 15, 2024
2 parents 2f6129e + 576441e commit 4e1a049
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 185 deletions.
42 changes: 32 additions & 10 deletions herald/modules/email_sender/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
is_valid_port,
is_email_format
)

CRYPTOGRAPHIC_PROTOCOLS = ['TLS', 'SSL']

LOG = logging.getLogger(__name__)

Expand All @@ -17,8 +17,12 @@ def send_email(message, config_client=None):
smtp_params = config_client.smtp_params()
if smtp_params is not None:
if _is_valid_smtp_params(smtp_params):
server, port, email, password = smtp_params
_send_email_to_user_smtp(server, port, email, password, message)
server, port, email, login, password, protocol = smtp_params
if not login:
LOG.warning('SMTP login is not set. Using email instead')
login = email
_send_email_to_user_smtp(
server, port, email, login, password, message, protocol)
return
else:
LOG.warning("User SMTP parameters are not valid")
Expand All @@ -28,26 +32,44 @@ def send_email(message, config_client=None):
def _is_valid_smtp_params(params):
if params is None:
return False
server, port, email, password = params
server, port, email, login, password, protocol = params
for value in (server, email, password):
if value is None:
return False
if not isinstance(server, str):
return False
if not is_email_format(email):
return False
if protocol.upper() not in CRYPTOGRAPHIC_PROTOCOLS:
return False
return is_valid_port(port)


def _send_email_to_user_smtp(server, port, email, password, message):
def _send_email_smtp_ssl(server, port, email, login, password, message):
context = ssl._create_unverified_context()
with smtplib.SMTP_SSL(server, port, context=context) as smtp_server:
smtp_server.login(login, password)
smtp_server.sendmail(email, message.get("To"), message.as_string())


def _send_email_smtp_tls(server, port, email, login, password, message):
with smtplib.SMTP(server, port) as smtp_server:
smtp_server.starttls()
smtp_server.login(login, password)
smtp_server.sendmail(email, message.get("To"), message.as_string())


def _send_email_to_user_smtp(
server, port, email, login, password, message, protocol):
send_func = {
'SSL': _send_email_smtp_ssl, 'TLS': _send_email_smtp_tls
}.get(protocol)
try:
with smtplib.SMTP_SSL(server, port, context=context) as smtp_server:
smtp_server.login(email, password)
smtp_server.sendmail(email, message.get("To"), message.as_string())
send_func(server, port, email, login, password, message)
except Exception as e:
LOG.error("Could not send mail using server %s and port %s with email %s. "
"Error %s" % (server, port, email, str(e)))
LOG.error("Could not send mail using server %s (protocol %s) and port "
"%s with email %s. Error %s" % (server, protocol, port,
email, str(e)))


def _send_email_from_default_service(message):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NOT_SET_REGION_FILTER_NAME } from "components/forms/AssignmentRuleForm/FormElements/ConditionsFieldArray";
import KeyValueLabel from "components/KeyValueLabel/KeyValueLabel";
import { intl } from "translations/react-intl-config";
import { CONDITION_TYPES, TAG_IS, CLOUD_IS, TAG_VALUE_STARTS_WITH } from "utils/constants";
import { CONDITION_TYPES, TAG_IS, CLOUD_IS, TAG_VALUE_STARTS_WITH, REGION_IS } from "utils/constants";

const prepareData = ({ assignmentRules, entities }) => {
const translateType = (type) =>
Expand Down Expand Up @@ -36,6 +37,9 @@ const prepareData = ({ assignmentRules, entities }) => {
if (type === CLOUD_IS) {
value = entities?.[metaInfo]?.name;
}
if (type === REGION_IS) {
value = metaInfo === null ? NOT_SET_REGION_FILTER_NAME : metaInfo;
}
return {
...resultObject,
conditionsString: `${resultObject.conditionsString ? `${resultObject.conditionsString},` : ""}${translateType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const AssignmentRuleForm = ({
// We need to pass defaultValues to useForm in order to reset the Controller components' value.
// (defaultValues.poolId, defaultValues.ownerId are marked as required in the propTypes definition)
// see https://react-hook-form.com/api#reset
defaultValues,
shouldUnregister: true
defaultValues
});

const { handleSubmit, reset } = methods;
Expand Down Expand Up @@ -71,8 +70,10 @@ const AssignmentRuleForm = ({
};
}
if (REGION_IS_FIELD_NAME in item) {
const { regionName } = item[REGION_IS_FIELD_NAME];

return {
[META_INFO]: item[REGION_IS_FIELD_NAME].trim(),
[META_INFO]: regionName === null ? null : regionName.trim(),
[TYPE]: item[TYPE]
};
}
Expand Down
Loading

0 comments on commit 4e1a049

Please sign in to comment.