<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> import six . moves . configparser <EOL> import optparse <EOL> import os <EOL> import sys <EOL> import time <EOL> import duo_client <EOL> from urlparse import urlparse <EOL> class BaseLog ( object ) : <EOL> def __init__ ( self , admin_api , path , logname ) : <EOL> self . admin_api = admin_api <EOL> self . path = path <EOL> self . logname = logname <EOL> self . mintime = <NUM_LIT:0> <EOL> self . events = [ ] <EOL> def get_events ( self ) : <EOL> raise NotImplementedError <EOL> def print_events ( self ) : <EOL> raise NotImplementedError <EOL> def get_last_timestamp_path ( self ) : <EOL> """<STR_LIT>""" <EOL> filename = self . logname + "<STR_LIT>" + self . admin_api . host <EOL> path = os . path . join ( self . path , filename ) <EOL> return path <EOL> def get_mintime ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> path = self . get_last_timestamp_path ( ) <EOL> self . mintime = int ( open ( path ) . read ( ) . strip ( ) ) + <NUM_LIT:1> <EOL> except IOError : <EOL> pass <EOL> def write_last_timestamp ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . events : <EOL> return <EOL> last_timestamp = <NUM_LIT:0> <EOL> for event in self . events : <EOL> last_timestamp = max ( last_timestamp , event [ '<STR_LIT>' ] ) <EOL> path = self . get_last_timestamp_path ( ) <EOL> f = open ( path , "<STR_LIT:w>" ) <EOL> f . write ( str ( last_timestamp ) ) <EOL> f . close ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . events = [ ] <EOL> self . get_mintime ( ) <EOL> self . get_events ( ) <EOL> self . print_events ( ) <EOL> self . write_last_timestamp ( ) <EOL> class AdministratorLog ( BaseLog ) : <EOL> def __init__ ( self , admin_api , path ) : <EOL> BaseLog . __init__ ( self , admin_api , path , "<STR_LIT>" ) <EOL> def get_events ( self ) : <EOL> self . events = self . admin_api . get_administrator_log ( <EOL> mintime = self . mintime , <EOL> ) <EOL> def print_events ( self ) : <EOL> """<STR_LIT>""" <EOL> for event in self . events : <EOL> event [ '<STR_LIT>' ] = time . ctime ( event [ '<STR_LIT>' ] ) <EOL> event [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" } . get ( <EOL> event [ '<STR_LIT:action>' ] , event [ '<STR_LIT:action>' ] ) <EOL> fmtstr = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> if event [ '<STR_LIT:object>' ] : <EOL> fmtstr += '<STR_LIT>' <EOL> if event [ '<STR_LIT:description>' ] : <EOL> fmtstr += '<STR_LIT>' <EOL> print ( fmtstr % event ) <EOL> class AuthenticationLog ( BaseLog ) : <EOL> def __init__ ( self , admin_api , path ) : <EOL> BaseLog . __init__ ( self , admin_api , path , "<STR_LIT>" ) <EOL> def get_events ( self ) : <EOL> self . events = self . admin_api . get_authentication_log ( <EOL> mintime = self . mintime , <EOL> ) <EOL> def print_events ( self ) : <EOL> """<STR_LIT>""" <EOL> for event in self . events : <EOL> event [ '<STR_LIT>' ] = time . ctime ( event [ '<STR_LIT>' ] ) <EOL> fmtstr = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> print ( fmtstr % event ) <EOL> class TelephonyLog ( BaseLog ) : <EOL> def __init__ ( self , admin_api , path ) : <EOL> BaseLog . __init__ ( self , admin_api , path , "<STR_LIT>" ) <EOL> def get_events ( self ) : <EOL> self . events = self . admin_api . get_telephony_log ( <EOL> mintime = self . mintime , <EOL> ) <EOL> def print_events ( self ) : <EOL> """<STR_LIT>""" <EOL> for event in self . events : <EOL> event [ '<STR_LIT>' ] = time . ctime ( event [ '<STR_LIT>' ] ) <EOL> event [ '<STR_LIT:host>' ] = self . admin_api . host <EOL> fmtstr = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> print ( fmtstr % event ) <EOL> def admin_api_from_config ( config_path ) : <EOL> """<STR_LIT>""" <EOL> config = six . moves . configparser . ConfigParser ( ) <EOL> config . read ( config_path ) <EOL> config_d = dict ( config . items ( '<STR_LIT>' ) ) <EOL> ca_certs = config_d . get ( "<STR_LIT>" , None ) <EOL> if ca_certs is None : <EOL> ca_certs = config_d . get ( "<STR_LIT>" , None ) <EOL> ret = duo_client . Admin ( <EOL> ikey = config_d [ '<STR_LIT>' ] , <EOL> skey = config_d [ '<STR_LIT>' ] , <EOL> host = config_d [ '<STR_LIT:host>' ] , <EOL> ca_certs = ca_certs , <EOL> ) <EOL> http_proxy = config_d . get ( "<STR_LIT>" , None ) <EOL> if http_proxy is not None : <EOL> proxy_parsed = urlparse ( http_proxy ) <EOL> proxy_host = proxy_parsed . hostname <EOL> proxy_port = proxy_parsed . port <EOL> ret . set_proxy ( host = proxy_host , port = proxy_port ) <EOL> return ret <EOL> def main ( ) : <EOL> parser = optparse . OptionParser ( usage = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> config_path = os . path . abspath ( __file__ ) <EOL> config_path = os . path . dirname ( config_path ) <EOL> config_path = os . path . join ( config_path , "<STR_LIT>" ) <EOL> else : <EOL> config_path = os . path . abspath ( sys . argv [ <NUM_LIT:1> ] ) <EOL> admin_api = admin_api_from_config ( config_path ) <EOL> path = os . path . dirname ( config_path ) <EOL> for logclass in ( AdministratorLog , AuthenticationLog , TelephonyLog ) : <EOL> log = logclass ( admin_api , path ) <EOL> log . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
