#NEWLINE# def kerberos_ccache_init( principal , keytab_file , ccache_file = None ) : #NEWLINE# #INDENT# '\n    Checks whether kerberos credential cache has ticket-granting ticket that is valid for at least\n    an hour.\n\n    Default ccache is used unless ccache_file is provided. In that case, KRB5CCNAME environment\n    variable is set to the value of ccache_file if we successfully obtain the ticket.\n    ' #NEWLINE# tgt_valid = False #NEWLINE# env = { #NEWLINE# #INDENT# 'LC_ALL' : 'C' , #NEWLINE# #UNINDENT# } #NEWLINE# if ccache_file : #NEWLINE# #INDENT# env [ 'KRB5CCNAME' ] = ccache_file #NEWLINE# #UNINDENT# ( rc , klist , _ ) = run ( [ 'klist' ] , extraenv = keytab_file ) #NEWLINE# if ( rc == 0 ) : #NEWLINE# #INDENT# for line in klist . splitlines ( ) : #NEWLINE# #INDENT# m = re . match ( KLIST_TGT_RE , line ) #NEWLINE# if m : #NEWLINE# #INDENT# year = m . group ( 'year' ) #NEWLINE# if ( len ( year ) == 2 ) : #NEWLINE# #INDENT# year = ( '20' + year ) #NEWLINE# #UNINDENT# expires = datetime . datetime ( int ( year ) , int ( m . group ( 'month' ) ) , int ( m . group ( 'day' ) ) , int ( m . group ( 'hour' ) ) , int ( m . group ( 'minute' ) ) , int ( m . group ( 'second' ) ) ) #NEWLINE# if ( ( expires - datetime . datetime . now ( ) ) > datetime . timedelta ( hours = 1 ) ) : #NEWLINE# #INDENT# logger . debug ( 'Valid TGT found, not renewing' ) #NEWLINE# tgt_valid = True #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( not tgt_valid ) : #NEWLINE# #INDENT# logger . debug ( 'Retrieving kerberos TGT' ) #NEWLINE# ( rc , out , err ) = run ( [ 'kinit' , '-k' , '-t' , keytab_file , principal ] , extraenv = env ) #NEWLINE# if ( rc != 0 ) : #NEWLINE# #INDENT# raise OsbsException ( ( 'kinit returned %s:\nstdout: %s\nstderr: %s' % ( rc , out , err ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ccache_file : #NEWLINE# #INDENT# os . environ [ 'KRB5CCNAME' ] = ccache_file