No auth header sent if username/password fields are blank

This commit is contained in:
beeankha
2019-06-25 13:57:32 -04:00
parent 0a0b09b394
commit 99737937cd

View File

@@ -23,12 +23,12 @@ class WebhookBackend(AWXBaseEmailBackend):
recipient_parameter = "url" recipient_parameter = "url"
sender_parameter = None sender_parameter = None
def __init__(self, headers, http_method, disable_ssl_verification=False, fail_silently=False, username=None, password=None, **kwargs): def __init__(self, http_method, headers, disable_ssl_verification=False, fail_silently=False, username=None, password=None, **kwargs):
self.http_method = http_method self.http_method = http_method
self.disable_ssl_verification = disable_ssl_verification self.disable_ssl_verification = disable_ssl_verification
self.headers = headers self.headers = headers
self.username = username self.username = username
self.password = password if password != "" else None self.password = password
super(WebhookBackend, self).__init__(fail_silently=fail_silently) super(WebhookBackend, self).__init__(fail_silently=fail_silently)
def format_body(self, body): def format_body(self, body):
@@ -42,15 +42,17 @@ class WebhookBackend(AWXBaseEmailBackend):
raise ValueError("HTTP method must be either 'POST' or 'PUT'.") raise ValueError("HTTP method must be either 'POST' or 'PUT'.")
chosen_method = getattr(requests, self.http_method.lower(), None) chosen_method = getattr(requests, self.http_method.lower(), None)
for m in messages: for m in messages:
if chosen_method is not None: auth = None
r = chosen_method("{}".format(m.recipients()[0]), if self.username and self.password:
auth=(self.username, self.password), auth = (self.username, self.password)
json=m.body, r = chosen_method("{}".format(m.recipients()[0]),
headers=self.headers, auth=auth,
verify=(not self.disable_ssl_verification)) json=m.body,
if r.status_code >= 400: headers=self.headers,
logger.error(smart_text(_("Error sending notification webhook: {}").format(r.text))) verify=(not self.disable_ssl_verification))
if not self.fail_silently: if r.status_code >= 400:
raise Exception(smart_text(_("Error sending notification webhook: {}").format(r.text))) logger.error(smart_text(_("Error sending notification webhook: {}").format(r.text)))
sent_messages += 1 if not self.fail_silently:
raise Exception(smart_text(_("Error sending notification webhook: {}").format(r.text)))
sent_messages += 1
return sent_messages return sent_messages