Beispiel #1
0
def check_session(session_token, http_client=HttpRequestor(), logger=Logger()):
    try:
        response = http_client.get(Settings.SESSION_URL.format(session_token=session_token)).json()
        return response.get(Constants.STATUS, Constants.EMPTY) == Constants.OKTA_ACTIVE_STATUS
    except Exception as ex:  # pylint: disable=broad-except
        logger.error(f'Error checking session. ex: {ex}')
        return False
Beispiel #2
0
class BaseModel:
    logger = Logger()
    id: str  # pylint: disable=invalid-name

    def __init__(self, dictionary=None):
        if dictionary:
            vars(self).update(dictionary)

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        return self.to_json(self)

    # pylint: disable=inconsistent-return-statements
    @staticmethod
    def safe_set_attribute(attribute):
        if attribute:
            return attribute

    def get_attribute(self, attribute_name):
        return self.__dict__.get(attribute_name, Constants.EMPTY)

    @staticmethod
    def from_json(json_string):
        return json.loads(json_string, object_hook=BaseModel)

    def get_json(self):
        return BaseModel.to_json(self)

    @staticmethod
    def to_json(model):
        return json.dumps(model.__dict__, default=str)

    def validate(self, model=None, schema=None):
        if schema is None:
            schema = self.to_schema()
            if not schema:
                return True
        validator = Validator(schema)
        model_data = self.__dict__
        if model:
            model_data = model
        result = validator.validate(model_data)
        self.logger.info(f'Validation errors: {validator.errors}')
        return result

    def to_schema(self):
        schema = {}
        # pylint: disable=no-member
        functions = self.__annotations__
        for key, value in functions.items():
            # pylint: disable=unidiomatic-typecheck
            if type(value) is not Field:
                return False
            schema[key] = {
                'type': SchemaTypeMappings[value.type.__name__],
            }
            schema[key].update(value.schema)
        return schema
Beispiel #3
0
 def __init__(self, queue_name, **kwargs):
     self.queue_name = queue_name
     self.runner = True
     self.exit = False
     self.client = kwargs.pop(Constants.CLIENT,
                              MessageQueue(queue_name=self.queue_name))
     self.logger = kwargs.pop(Constants.LOGGER, Logger())
     super().__init__(**kwargs)
Beispiel #4
0
 def __init__(self, **kwargs):
     super().__init__(**kwargs)
     self.logger = Logger()
     self.table_name = kwargs.get(Constants.TABLE, Settings.SERVICE_NAME)
     self.client = kwargs.get(Constants.CLIENT, get_default_dynamo_client())
     self.resource = kwargs.get(Constants.RESOURCE,
                                get_default_dynamo_resource())
     self._initialize_tables()
Beispiel #5
0
 def __init__(self,
              account_name,
              http=HttpRequestor(),
              secrets_manager=SecretManager()):
     self.http = http
     self.secrets_manager = secrets_manager
     self.account_name = account_name
     self.account_id = get_newrelic_account_id(self.account_name)
     self.api_key = get_newrelic_api_key(self.account_name,
                                         self.secrets_manager)
     self.logger = Logger(Settings.SERVICE_NAME)
Beispiel #6
0
 def __init__(self, **kwargs):
     """ Initialization for mongodb with support for TDD. """
     super().__init__(**kwargs)
     self.db_name = Settings.MONGO_DB_NAME
     collection_name = kwargs.get(Constants.TABLE, Settings.SERVICE_NAME)
     self.host = kwargs.get(Constants.HOST, Settings.MONGO_URL)
     self.port = kwargs.get(Constants.PORT, Settings.MONGO_PORT)
     self.client = kwargs.get(Constants.CLIENT, MongoClient(self.host, self.port, serverSelectionTimeoutMS=20))
     self.collection = None
     self.logger = Logger()
     self.database = self.client[self.db_name]
     self._select_collection(collection_name)
Beispiel #7
0
 def __init__(self, **kwargs):
     self.logger = kwargs.get(Constants.LOGGER, Logger())
     self.queue_name = kwargs.get(Constants.QUEUE_NAME, None)
     self.endpoint_url = None
     if not Settings.ENV:
         self.endpoint_url = kwargs.get(
             Constants.ENDPOINT_URL,
             f'{Settings.SQS_URL}:{Settings.SQS_PORT}')
     self.sqs_resource = kwargs.get(Constants.CLIENT,
                                    get_sqs_resource(self.endpoint_url))
     self.sqs_client = kwargs.get(Constants.CLIENT,
                                  get_sqs_client(self.endpoint_url))
Beispiel #8
0
 def create(component_type, **kwargs):
     if isinstance(component_type, Components.DataClients):
         return Factory._create_data_client(component_type, **kwargs)
     if isinstance(component_type, Components.ServiceClients):
         return Factory._create_service_client(component_type, **kwargs)
     if isinstance(component_type, Components.MonitorClients):
         return MonitoringProvider()
     if isinstance(component_type, Components.LogClients):
         return Logger()
     if isinstance(component_type, Components.SecretsClients):
         return SecretManager()
     if isinstance(component_type, Components.Controllers):
         return Factory._create_controller(component_type, **kwargs)
     return None
Beispiel #9
0
 def __init__(self, **kwargs):
     self.logger = Logger()
     self.collector = kwargs.get(Constants.COLLECTOR, None)
     self.controller = kwargs.get(Constants.CONTROLLER,
                                  DataController(**kwargs))
     self.client = kwargs.get(Constants.CLIENT, None)
Beispiel #10
0
 def __init__(self, data_client):
     self.data_client = data_client
     self.logger = Logger()
Beispiel #11
0
 def log_metric_for_checks(self, worker_ctx, metric_key, metric_value=Constants.EMPTY, logger=Logger(__name__)):
     request_url = self.get_request_url(worker_ctx)
     if 'infrastructure_get' in metric_key:
         logger.debug(f'Metric log for {request_url} - {metric_key}: {metric_value}')
Beispiel #12
0
 def __init__(self, http=HttpRequestor()):
     self.http = http
     self.logger = Logger(Settings.SERVICE_NAME)
     self.headers = {Constants.CONTENT_TYPE_HEADER: Constants.CONTENT_TYPE_JSON,
                     Constants.ACCEPT_HEADER: Constants.CONTENT_TYPE_JSON}