-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
890 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
23 changes: 23 additions & 0 deletions
23
cumulus_lambda_functions/lib/authorization/uds_authorizer_abstract.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class UDSAuthorizorAbstract(ABC): | ||
@abstractmethod | ||
def add_authorized_group(self, action: [str], resource: [str], tenant: str, venue: str, ldap_group_name: str): | ||
return | ||
|
||
@abstractmethod | ||
def delete_authorized_group(self, tenant: str, venue: str, ldap_group_name: str): | ||
return | ||
|
||
@abstractmethod | ||
def list_authorized_groups_for(self, tenant: str, venue: str): | ||
return | ||
|
||
@abstractmethod | ||
def update_authorized_group(self, action: [str], resource: [str], tenant: str, venue: str, ldap_group_name: str): | ||
return | ||
|
||
@abstractmethod | ||
def get_authorized_tenant(self, username: str, action: str, resource: str) -> list: | ||
return [] |
121 changes: 121 additions & 0 deletions
121
cumulus_lambda_functions/lib/authorization/uds_authorizer_es_identity_pool.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import logging | ||
import os | ||
|
||
from cumulus_lambda_functions.lib.authorization.uds_authorizer_abstract import UDSAuthorizorAbstract | ||
from cumulus_lambda_functions.lib.aws.aws_cognito import AwsCognito | ||
from cumulus_lambda_functions.lib.aws.es_abstract import ESAbstract | ||
from cumulus_lambda_functions.lib.aws.es_factory import ESFactory | ||
from cumulus_lambda_functions.lib.uds_db.db_constants import DBConstants | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class UDSAuthorizorEsIdentityPool(UDSAuthorizorAbstract): | ||
|
||
def __init__(self, user_pool_id: str) -> None: | ||
super().__init__() | ||
es_url = os.getenv('ES_URL') # TODO validation | ||
self.__authorization_index = os.getenv('AUTHORIZATION_INDEX') # LDAP_Group_Permission | ||
es_port = int(os.getenv('ES_PORT', '443')) | ||
self.__cognito = AwsCognito(user_pool_id) | ||
self.__es: ESAbstract = ESFactory().get_instance('AWS', | ||
index=self.__authorization_index, | ||
base_url=es_url, | ||
port=es_port) | ||
|
||
def add_authorized_group(self, action: [str], resource: [str], tenant: str, venue: str, ldap_group_name: str): | ||
self.__es.index_one({ | ||
DBConstants.action_key: action, | ||
DBConstants.resource_key: resource, | ||
DBConstants.tenant: tenant, | ||
DBConstants.tenant_venue: venue, | ||
DBConstants.authorized_group_name_key: ldap_group_name, | ||
}, f'{tenant}__{venue}__{ldap_group_name}', self.__authorization_index) | ||
return | ||
|
||
def delete_authorized_group(self, tenant: str, venue: str, ldap_group_name: str): | ||
self.__es.delete_by_query({ | ||
'query': { | ||
'bool': { | ||
'must': [ | ||
{'term': {DBConstants.tenant: tenant}}, | ||
{'term': {DBConstants.tenant_venue: venue}}, | ||
{'term': {DBConstants.authorized_group_name_key: ldap_group_name}}, | ||
] | ||
} | ||
} | ||
}) | ||
return | ||
|
||
def list_authorized_groups_for(self, tenant: str, venue: str): | ||
result = self.__es.query_pages({ | ||
'query': { | ||
'bool': { | ||
'must': [ | ||
{'term': {DBConstants.tenant: tenant}}, | ||
{'term': {DBConstants.tenant_venue: venue}}, | ||
] | ||
} | ||
}, | ||
'sort': [ | ||
{DBConstants.tenant: {'order': 'asc'}}, | ||
{DBConstants.tenant_venue: {'order': 'asc'}}, | ||
{DBConstants.authorized_group_name_key: {'order': 'asc'}}, | ||
] | ||
}) | ||
result = [k['_source'] for k in result['hits']['hits']] | ||
return result | ||
|
||
def update_authorized_group(self, action: [str], resource: [str], tenant: str, venue: str, ldap_group_name: str): | ||
self.__es.update_one({ | ||
DBConstants.action_key: action, | ||
DBConstants.resource_key: resource, | ||
DBConstants.tenant: tenant, | ||
DBConstants.tenant_venue: venue, | ||
DBConstants.authorized_group_name_key: ldap_group_name, | ||
}, f'{tenant}__{venue}__{ldap_group_name}', self.__authorization_index) | ||
return | ||
|
||
def get_authorized_tenant(self, username: str, action: str, resource: str) -> list: | ||
belonged_groups = set(self.__cognito.get_groups(username)) | ||
|
||
authorized_groups = self.__es.query({ | ||
'query': { | ||
'bool': { | ||
'must': [ | ||
{ | ||
'terms': { | ||
DBConstants.authorized_group_name_key: list(belonged_groups), | ||
} | ||
}, | ||
{ | ||
'term': { | ||
DBConstants.action_key: action, | ||
} | ||
}, | ||
{ | ||
'term': { | ||
DBConstants.resource_key: resource, | ||
} | ||
} | ||
] | ||
} | ||
} | ||
}) | ||
return [k['_source'] for k in authorized_groups['hits']['hits']] | ||
|
||
def authorize(self, username, resource, action) -> bool: | ||
belonged_groups = set(self.__cognito.get_groups(username)) | ||
authorized_groups = self.__es.query({ | ||
'query': { | ||
'match_all': {} # TODO | ||
} | ||
}) | ||
LOGGER.debug(f'belonged_groups for {username}: {belonged_groups}') | ||
authorized_groups = set([k['_source']['group_name'] for k in authorized_groups['hits']['hits']]) | ||
LOGGER.debug(f'authorized_groups for {resource}-{action}: {authorized_groups}') | ||
if any([k in authorized_groups for k in belonged_groups]): | ||
LOGGER.debug(f'{username} is authorized for {resource}-{action}') | ||
return True | ||
LOGGER.debug(f'{username} is NOT authorized for {resource}-{action}') | ||
return False |
12 changes: 12 additions & 0 deletions
12
cumulus_lambda_functions/lib/authorization/uds_authorizer_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from cumulus_lambda_functions.lib.aws.factory_abstract import FactoryAbstract | ||
|
||
|
||
class UDSAuthorizerFactory(FactoryAbstract): | ||
cognito = 'COGNITO' | ||
|
||
def get_instance(self, class_type, **kwargs): | ||
if class_type == self.cognito: | ||
from cumulus_lambda_functions.lib.authorization.uds_authorizer_es_identity_pool import \ | ||
UDSAuthorizorEsIdentityPool | ||
return UDSAuthorizorEsIdentityPool(kwargs['user_pool_id']) | ||
raise ValueError(f'class_type: {class_type} not implemented') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from cumulus_lambda_functions.lib.aws.aws_cred import AwsCred | ||
|
||
|
||
class AwsCognito(AwsCred): | ||
def __init__(self, user_pool_id: str): | ||
super().__init__() | ||
self.__cognito = self.get_client('cognito-idp') | ||
self.__user_pool_id = user_pool_id | ||
|
||
def get_groups(self, username: str): | ||
response = self.__cognito.admin_list_groups_for_user( | ||
Username=username, | ||
UserPoolId=self.__user_pool_id, | ||
Limit=60, | ||
# NextToken='string' | ||
) | ||
if response is None or 'Groups' not in response: | ||
return [] | ||
belonged_groups = [k['GroupName'] for k in response['Groups']] | ||
return belonged_groups |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.