Skip to content

Commit

Permalink
Merge pull request #2132 from RyanEweSeng/SOFTWARE-3914.handle-incorr…
Browse files Browse the repository at this point in the history
…ect-cert-or-password

Handle incorrect cert path or password in osg-notify (SOFTWARE-3914)
  • Loading branch information
brianhlin authored Dec 1, 2021
2 parents f6564ea + 8b264ad commit 7b3417e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
34 changes: 29 additions & 5 deletions bin/osg-notify
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,20 @@ def main():

recipients = set(args.recipients.split())
if args.oim_recipients and 'vos' in args.oim_recipients:
results = topology_utils.get_vo_contacts(args)
attempts = 3
while attempts > 0:
try:
results = topology_utils.get_vo_contacts(args)
except topology_utils.InvalidPathError as exc:
print(exc)
exit(1)
except topology_utils.IncorrectPasswordError as exc:
attempts -= 1
if attempts == 0:
print("Too many incorrect password attempts, exiting")
exit(1)
else:
print(exc)
results = topology_utils.filter_contacts(args, results)
emails = set()
for name in results.keys():
Expand All @@ -185,10 +198,21 @@ def main():
emails.add(contact['Email'])
recipients.update(emails)
if args.oim_recipients and 'resources' in args.oim_recipients:
if args.fqdn_filter:
results = topology_utils.get_resource_contacts_by_fqdn(args)
else:
results = topology_utils.get_resource_contacts(args)
attempts = 3
while attempts > 0:
try:
if args.fqdn_filter:
results = topology_utils.get_resource_contacts_by_fqdn(args)
else:
results = topology_utils.get_resource_contacts(args)
except topology_utils.InvalidPathError as exc:
exit(str(exc))
except topology_utils.IncorrectPasswordError as exc:
attempts -= 1
if attempts == 0:
exit("Too many incorrect password attempts, exiting")
else:
print(exc)
results = topology_utils.filter_contacts(args, results)
emails = set()
for name in results.keys():
Expand Down
29 changes: 27 additions & 2 deletions src/topology_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@

import requests

class Error(Exception):
pass

class AuthError(Error):
pass

class InvalidPathError(AuthError):
pass

class IncorrectPasswordError(AuthError):
pass

def get_auth_session(args):
"""
Return a requests session ready for an XML query.
Expand All @@ -37,8 +49,13 @@ def get_auth_session(args):

if os.path.exists(cert):
session.cert = cert
else:
raise InvalidPathError("Error: could not find cert at %s" % cert)

if os.path.exists(key):
session.cert = (cert, key)
else:
raise InvalidPathError("Error: could not find key at %s" % key)

return session

Expand Down Expand Up @@ -216,8 +233,16 @@ def get_contacts(args, urltype, roottype):
"&active=on&active_value=1&disable=on&disable_value=0"
with get_auth_session(args) as session:
url = mangle_url(base_url, args, session)
#print(url)
response = session.get(url)
try:
response = session.get(url)
except requests.exceptions.ConnectionError as exc:
try:
if exc.args[0].args[1].errno == 22:
raise IncorrectPasswordError("Incorrect password, please try again")
else:
raise exc
except (TypeError, AttributeError, IndexError):
raise exc

if old_no_proxy is not None:
os.environ['no_proxy'] = old_no_proxy
Expand Down

0 comments on commit 7b3417e

Please sign in to comment.