Skip to content

Commit

Permalink
Make first login into OSIDB fault-tolerant
Browse files Browse the repository at this point in the history
On a user's first login we trigger a signal that creates a new Profile
object which will link the kerberos login to Bugzilla and JIRA
usernames, however this can fail for a number of reasons (expired token,
service unavailable, network failure, etc.) and thus we should take care
to handle such exceptions and simply let the user login without linking
said credentials.

In the future we should try to sync at other points in time if possible.
  • Loading branch information
Adrian Torres committed Jan 23, 2023
1 parent 8642fa0 commit 853a92d
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions osidb/signals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os

from bugzilla import Bugzilla
Expand All @@ -8,37 +9,53 @@

from osidb.models import Profile

logger = logging.getLogger(__name__)


def get_bz_user_id(email: str) -> str:
api_key = os.getenv("BZIMPORT_BZ_API_KEY")
bz_url = os.getenv("BZIMPORT_BZ_URL", "https://bugzilla.redhat.com")
bz_api = Bugzilla(
bz_url,
api_key=api_key,
force_rest=True,
)
users = bz_api.searchusers([email])
if users:
return users[0].name
return ""
try:
bz_api = Bugzilla(
bz_url,
api_key=api_key,
force_rest=True,
)
users = bz_api.searchusers([email])
except Exception:
logger.error(
f"Failed to fetch Bugzilla username for {email}, is the Bugzilla token valid?"
)
return ""
else:
if users:
return users[0].name
return ""


def get_jira_user_id(email: str) -> str:
auth_token = os.getenv("JIRA_AUTH_TOKEN")
jira_url = os.getenv("JIRA_URL", "https://issues.redhat.com")
jira_api = JIRA(
{
"server": jira_url,
# avoid auto-updating the lib
"check_update": False,
},
token_auth=auth_token,
get_server_info=False,
)
users = jira_api.search_users([email])
if users:
return users[0].name
return ""
try:
jira_api = JIRA(
{
"server": jira_url,
# avoid auto-updating the lib
"check_update": False,
},
token_auth=auth_token,
get_server_info=False,
)
users = jira_api.search_users([email])
except Exception:
logger.error(
f"Failed to fetch JIRA username for {email}, is the JIRA token valid?"
)
return ""
else:
if users:
return users[0].name
return ""


@receiver(post_save, sender=User)
Expand Down

0 comments on commit 853a92d

Please sign in to comment.