Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding function to parse output of klist -k KeytabFile to get first principal #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions krbticket/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
import multiprocessing
import os

logger = logging.getLogger(__name__)


Expand All @@ -20,7 +19,6 @@ def __init__(self, principal=None, keytab=None, kinit_bin="kinit",
'wait_exponential_multiplier': 1000,
'wait_exponential_max': 30000,
'stop_max_attempt_number': 10}):
self.principal = principal
self.keytab = keytab
self.kinit_bin = kinit_bin
self.klist_bin = klist_bin
Expand All @@ -33,6 +31,10 @@ def __init__(self, principal=None, keytab=None, kinit_bin="kinit",
self.ccache_name = ccache_name if ccache_name else self._ccache_name()
self.ccache_lockfile = '{}.krbticket.lock'.format(self.ccache_name)
self.ccache_cmd_lockfile = '{}.krbticket.cmd.lock'.format(self.ccache_name)
if principal is None and keytab is not None:
self.principal = _get_first_principal_from_keytab()
else:
self.principal = principal

def __str__(self):
super_str = super(KrbConfig, self).__str__()
Expand Down Expand Up @@ -71,3 +73,21 @@ def _per_process_ccache_name(self):
logger.info("env KRB5CCNAME is updated to '{}' for multiprocessing".format(new_ccname))

return os.environ.get('KRB5CCNAME')

def _get_first_principal_from_keytab()
from krbticket.commands import KrbCommand
commands = []
commands.append(self.klist_bin)
commands.append("-k")
commands.append(self.keytab)

klistFromKeytabRows = KrbCommand._call(config, commands).splitlines()

if len(klistFromKeytabRows) >= 5:
principals = list()
for rowIndex in range(3,len(klistFromKeytabRows)-1):
principals.append(klistFromKeytabRows[rowIndex].strip().split(" ")[1])
if len(principals) >= 1:
return principals[0]
else:
return None