pyceo/ceo/krb_check.py

36 lines
819 B
Python
Raw Normal View History

2021-08-23 09:59:01 -04:00
import subprocess
import gssapi
2021-08-28 23:09:02 -04:00
_username = None
def get_username():
"""Get the user currently logged into CEO."""
return _username
2021-08-23 09:59:01 -04:00
def krb_check():
"""
Spawns a `kinit` process if no credentials are available or the
credentials have expired.
2021-08-28 23:09:02 -04:00
Stores the username for later use by get_username().
2021-08-23 09:59:01 -04:00
"""
2021-08-28 23:09:02 -04:00
global _username
2021-08-23 19:01:24 -04:00
for _ in range(2):
try:
creds = gssapi.Credentials(usage='initiate')
result = creds.inquire()
2021-08-28 23:09:02 -04:00
princ = str(result.name)
_username = princ[:princ.index('@')]
return
2021-08-23 19:01:24 -04:00
except (gssapi.raw.misc.GSSError, gssapi.raw.exceptions.ExpiredCredentialsError):
kinit()
2021-08-23 09:59:01 -04:00
2021-08-23 19:01:24 -04:00
raise Exception('could not acquire GSSAPI credentials')
2021-08-23 09:59:01 -04:00
def kinit():
subprocess.run(['kinit'], check=True)