<s> import json <EOL> import logging <EOL> import time <EOL> from django . utils . translation import ugettext as _ <EOL> from desktop . lib . django_util import JsonResponse <EOL> from libsentry . api import get_api <EOL> from libsentry . sentry_site import get_sentry_server_admin_groups <EOL> from hadoop . cluster import get_defaultfs <EOL> from beeswax . api import autocomplete <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def fetch_hive_path ( request ) : <EOL> path = request . GET [ '<STR_LIT:path>' ] <EOL> database = None <EOL> table = None <EOL> column = None <EOL> if path : <EOL> database = path <EOL> if '<STR_LIT:/>' in path : <EOL> database , table = path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT:.>' in table : <EOL> table , column = table . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> resp = autocomplete ( request , database , table , column ) <EOL> if database and request . GET [ '<STR_LIT>' ] != request . user . username : <EOL> request . GET = request . GET . copy ( ) <EOL> request . GET [ '<STR_LIT>' ] = request . GET [ '<STR_LIT>' ] <EOL> resp = autocomplete ( request , database , table , column ) <EOL> return resp <EOL> def list_sentry_roles_by_group ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> if request . POST [ '<STR_LIT>' ] : <EOL> groupName = request . POST [ '<STR_LIT>' ] <EOL> else : <EOL> groupName = None if request . user . groups . filter ( name__in = get_sentry_server_admin_groups ( ) ) . exists ( ) else '<STR_LIT:*>' <EOL> roles = get_api ( request . user ) . list_sentry_roles_by_group ( groupName ) <EOL> result [ '<STR_LIT>' ] = sorted ( roles , key = lambda role : role [ '<STR_LIT:name>' ] ) <EOL> result [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in str ( e ) : <EOL> result [ '<STR_LIT>' ] = [ ] <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> else : <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def list_sentry_privileges_by_role ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> roleName = request . POST [ '<STR_LIT>' ] <EOL> sentry_privileges = get_api ( request . user ) . list_sentry_privileges_by_role ( roleName ) <EOL> result [ '<STR_LIT>' ] = sorted ( sentry_privileges , key = lambda privilege : '<STR_LIT>' % ( privilege [ '<STR_LIT>' ] , privilege [ '<STR_LIT>' ] , privilege [ '<STR_LIT>' ] , privilege [ '<STR_LIT>' ] ) ) <EOL> result [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def _to_sentry_privilege ( privilege ) : <EOL> return { <EOL> '<STR_LIT>' : privilege [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : privilege [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : privilege [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : privilege [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : privilege [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _massage_uri ( privilege [ '<STR_LIT>' ] . encode ( '<STR_LIT:utf-8>' ) ) , <EOL> '<STR_LIT:action>' : privilege [ '<STR_LIT:action>' ] , <EOL> '<STR_LIT>' : privilege [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : <NUM_LIT:1> if privilege [ '<STR_LIT>' ] else <NUM_LIT:0> , <EOL> } <EOL> def _hive_add_privileges ( user , role , privileges ) : <EOL> api = get_api ( user ) <EOL> _privileges = [ ] <EOL> for privilege in privileges : <EOL> if privilege [ '<STR_LIT:status>' ] not in ( '<STR_LIT>' , ) : <EOL> api . alter_sentry_role_grant_privilege ( role [ '<STR_LIT:name>' ] , _to_sentry_privilege ( privilege ) ) <EOL> _privileges . append ( { <EOL> '<STR_LIT>' : int ( time . time ( ) ) , <EOL> '<STR_LIT>' : privilege . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:action>' : privilege . get ( '<STR_LIT:action>' ) , <EOL> '<STR_LIT>' : privilege . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : privilege . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : privilege . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : privilege . get ( '<STR_LIT>' ) . encode ( '<STR_LIT:utf-8>' ) , <EOL> '<STR_LIT>' : privilege . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : privilege . get ( '<STR_LIT>' ) == <NUM_LIT:1> <EOL> } ) <EOL> return _privileges <EOL> def _massage_uri ( uri ) : <EOL> if uri : <EOL> if uri . startswith ( '<STR_LIT>' ) : <EOL> uri = uri . replace ( '<STR_LIT>' , get_defaultfs ( ) ) <EOL> elif uri . startswith ( '<STR_LIT:/>' ) : <EOL> uri = get_defaultfs ( ) + uri <EOL> return uri <EOL> def _get_splitted_path ( path ) : <EOL> parts = path . split ( '<STR_LIT:.>' ) <EOL> db , table , column = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> if len ( parts ) >= <NUM_LIT:1> : <EOL> db = parts [ <NUM_LIT:0> ] <EOL> if len ( parts ) >= <NUM_LIT:2> : <EOL> table = parts [ <NUM_LIT:1> ] <EOL> if len ( parts ) >= <NUM_LIT:3> : <EOL> column = parts [ <NUM_LIT:2> ] <EOL> return db , table , column <EOL> def _drop_sentry_privilege ( user , role , authorizable ) : <EOL> return get_api ( user ) . alter_sentry_role_revoke_privilege ( role [ '<STR_LIT:name>' ] , _to_sentry_privilege ( authorizable ) ) <EOL> def create_role ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> role = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> api = get_api ( request . user ) <EOL> api . create_sentry_role ( role [ '<STR_LIT:name>' ] ) <EOL> privileges = [ privilege for privilege in role [ '<STR_LIT>' ] if privilege [ '<STR_LIT:status>' ] not in ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> result [ '<STR_LIT>' ] = _hive_add_privileges ( request . user , role , privileges ) <EOL> api . alter_sentry_role_add_groups ( role [ '<STR_LIT:name>' ] , role [ '<STR_LIT>' ] ) <EOL> result [ '<STR_LIT>' ] = { "<STR_LIT:name>" : role [ '<STR_LIT:name>' ] , "<STR_LIT>" : role [ '<STR_LIT>' ] } <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def update_role_groups ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> role = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> new_groups = set ( role [ '<STR_LIT>' ] ) - set ( role [ '<STR_LIT>' ] ) <EOL> deleted_groups = set ( role [ '<STR_LIT>' ] ) - set ( role [ '<STR_LIT>' ] ) <EOL> api = get_api ( request . user ) <EOL> if new_groups : <EOL> api . alter_sentry_role_add_groups ( role [ '<STR_LIT:name>' ] , new_groups ) <EOL> if deleted_groups : <EOL> api . alter_sentry_role_delete_groups ( role [ '<STR_LIT:name>' ] , deleted_groups ) <EOL> result [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def save_privileges ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> role = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> new_privileges = [ privilege for privilege in role [ '<STR_LIT>' ] if privilege [ '<STR_LIT:status>' ] == '<STR_LIT>' ] <EOL> result [ '<STR_LIT>' ] = _hive_add_privileges ( request . user , role , new_privileges ) <EOL> deleted_privileges = [ privilege for privilege in role [ '<STR_LIT>' ] if privilege [ '<STR_LIT:status>' ] == '<STR_LIT>' ] <EOL> for privilege in deleted_privileges : <EOL> _drop_sentry_privilege ( request . user , role , privilege ) <EOL> modified_privileges = [ privilege for privilege in role [ '<STR_LIT>' ] if privilege [ '<STR_LIT:status>' ] == '<STR_LIT>' ] <EOL> old_privileges_ids = [ privilege [ '<STR_LIT:id>' ] for privilege in modified_privileges ] <EOL> _hive_add_privileges ( request . user , role , modified_privileges ) <EOL> for privilege in role [ '<STR_LIT>' ] : <EOL> if privilege [ '<STR_LIT:id>' ] in old_privileges_ids : <EOL> _drop_sentry_privilege ( request . user , role , privilege ) <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def grant_privilege ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> roleName = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> privilege = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> result [ '<STR_LIT>' ] = _hive_add_privileges ( request . user , { '<STR_LIT:name>' : roleName } , [ privilege ] ) <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) % roleName <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def create_sentry_role ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> roleName = request . POST [ '<STR_LIT>' ] <EOL> get_api ( request . user ) . create_sentry_role ( roleName ) <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def drop_sentry_role ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> roleName = request . POST [ '<STR_LIT>' ] <EOL> get_api ( request . user ) . drop_sentry_role ( roleName ) <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def list_sentry_privileges_by_authorizable ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> groups = [ request . POST [ '<STR_LIT>' ] ] if request . POST [ '<STR_LIT>' ] else None <EOL> authorizableSet = [ json . loads ( request . POST [ '<STR_LIT>' ] ) ] <EOL> _privileges = [ ] <EOL> for authorizable , roles in get_api ( request . user ) . list_sentry_privileges_by_authorizable ( authorizableSet = authorizableSet , groups = groups ) : <EOL> for role , privileges in roles . iteritems ( ) : <EOL> for privilege in privileges : <EOL> privilege [ '<STR_LIT>' ] = role <EOL> _privileges . append ( privilege ) <EOL> result [ '<STR_LIT>' ] = sorted ( _privileges , key = lambda privilege : privilege [ '<STR_LIT>' ] ) <EOL> result [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def bulk_delete_privileges ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> checkedPaths = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> authorizableHierarchy = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> for path in [ path [ '<STR_LIT:path>' ] for path in checkedPaths ] : <EOL> db , table , column = _get_splitted_path ( path ) <EOL> authorizableHierarchy . update ( { <EOL> '<STR_LIT>' : db , <EOL> '<STR_LIT>' : table , <EOL> '<STR_LIT>' : column , <EOL> } ) <EOL> get_api ( request . user ) . drop_sentry_privileges ( authorizableHierarchy ) <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def bulk_add_privileges ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> privileges = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> checkedPaths = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> authorizableHierarchy = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> privileges = [ privilege for privilege in privileges if privilege [ '<STR_LIT:status>' ] == '<STR_LIT>' ] <EOL> for path in [ path [ '<STR_LIT:path>' ] for path in checkedPaths ] : <EOL> db , table , column = _get_splitted_path ( path ) <EOL> privilegeScope = '<STR_LIT>' if column else '<STR_LIT>' if table else '<STR_LIT>' if db else '<STR_LIT>' <EOL> authorizableHierarchy . update ( { <EOL> '<STR_LIT>' : db , <EOL> '<STR_LIT>' : table , <EOL> '<STR_LIT>' : column , <EOL> } ) <EOL> for privilege in privileges : <EOL> privilege [ '<STR_LIT>' ] = db <EOL> privilege [ '<STR_LIT>' ] = table <EOL> privilege [ '<STR_LIT>' ] = column <EOL> privilege [ '<STR_LIT>' ] = privilegeScope <EOL> _hive_add_privileges ( request . user , { '<STR_LIT:name>' : privilege [ '<STR_LIT>' ] } , [ privilege ] ) <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def rename_sentry_privilege ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> oldAuthorizable = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> newAuthorizable = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> get_api ( request . user ) . rename_sentry_privilege ( oldAuthorizable , newAuthorizable ) <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) <EOL> def list_sentry_privileges_for_provider ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> try : <EOL> groups = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> roleSet = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> authorizableHierarchy = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> sentry_privileges = get_api ( request . user ) . list_sentry_privileges_for_provider ( groups = groups , roleSet = roleSet , authorizableHierarchy = authorizableHierarchy ) <EOL> result [ '<STR_LIT>' ] = sentry_privileges <EOL> result [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT:message>' ] = unicode ( str ( e ) , "<STR_LIT:utf8>" ) <EOL> return JsonResponse ( result ) </s>
