Пример #1
0
    def filter_by_status(self, pkg_status, conn=None):
        try:
            if pkg_status in CommonAppKeys.ValidPackageStatuses:
                base = (
                    r
                    .table(self.CurrentAppsPerAgentCollection, use_outdated=True)
                    .get_all(
                        [pkg_status, self.customer_name],
                        index=self.CurrentAppsPerAgentIndexes.StatusAndCustomer)
                    .eq_join(self.CurrentAppsKey.AppId, r.table(self.CurrentAppsCollection))
                    .map(self.joined_map_hash)
                )

                if self.show_hidden == CommonKeys.NO:
                    base = base.filter(
                        {self.CurrentAppsKey.Hidden: CommonKeys.NO}
                    )

                packages = list(
                    base
                    .distinct()
                    .order_by(self.sort(self.sort_key))
                    .skip(self.offset)
                    .limit(self.count)
                    .run(conn)
                )

                pkg_count = (
                    base
                    .pluck(self.CurrentAppsKey.AppId)
                    .distinct()
                    .count()
                    .run(conn)
                )

                return_status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).information_retrieved(packages, pkg_count)
                )

            else:
                return_status = (
                    PackageResults(
                        self.username, self.uri, self.method
                    ).invalid_global_status(pkg_status)
                )

        except Exception as e:
            return_status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke(
                    "Package Searching went haywire",
                    'os_updates', e
                    )
            )
            logger.exception(e)

        return(return_status)
Пример #2
0
def systems_os_details(username,
                       customer_name,
                       key,
                       query,
                       uri=None,
                       method=None):

    keys_to_pluck = [
        AgentKey.ComputerName, AgentKey.OsCode, AgentKey.OsString,
        AgentKey.MachineType, AgentKey.SysArch
    ]

    data = filter_by_and_query(username=username,
                               customer_name=customer_name,
                               key=key,
                               query=query,
                               keys_to_pluck=keys_to_pluck)

    try:
        data = data
        results = (GenericResults(
            username,
            uri,
            method,
        ).information_retrieved(data, len(data)))

    except Exception as e:
        logger.exception(e)
        results = (GenericResults(username, uri, method).something_broke(
            'Systems_os_details', 'failed to retrieve data', e))
    return (results)
Пример #3
0
    def get_common_fields(self, fields, uri=None, method=None, conn=None):
        try:
            if isinstance(fields, list):
                agent_data = (
                    r
                    .table(AgentsCollection)
                    .get(self.agent_id)
                    .pluck(fields)
                    .run(conn)
                )
                if agent_data:
                    status = (
                        GenericResults(
                            self.username, uri, method
                            ).information_retrieved(agent_data, 1)
                    )
                    logger.info(status['message'])
                else:
                    status = (
                        GenericResults(
                            self.username, uri, method
                            ).invalid_id(self.agent_id, 'agent_id')
                    )
                    logger.info(status['message'])

        except Exception as e:
            status = (
                GenericResults(
                    self.username, uri, method
                ).something_broke(self.agent_id, 'agents', e)
            )
            logger.error(status['message'])

        return(status)
Пример #4
0
 def get(self):
     username = self.get_current_user().encode('utf-8')
     customer_name = (
         get_user_property(username, UserKeys.CurrentCustomer)
     )
     uri = self.request.uri
     method = self.request.method
     try:
         app_stats = (
             get_all_app_stats_by_customer(customer_name)
         )
         results = (
             GenericResults(
                 username, uri, method
             ).information_retrieved(app_stats)
         )
         self.set_status(results['http_status'])
         self.set_header('Content-Type', 'application/json')
         self.write(json.dumps(results, indent=4))
     except Exception as e:
         results = (
             GenericResults(
                 username, uri, method
             ).something_broke('widget handler', 'widgets', e)
         )
         logger.exception(results)
         self.set_status(results['http_status'])
         self.set_header('Content-Type', 'application/json')
         self.write(json.dumps(results, indent=4))
Пример #5
0
    def get_notification(self, notification_id, conn=None):
        try:
            data = (
                r
                .table(NotificationCollections.Notifications)
                .get_all(notification_id)
                .map(self.map_list)
                .run(conn)
            )
            results = (
                GenericResults(
                    self.username, self.uri, self.method
                ).information_retrieved(data[0], len(data[0]))
            )
        except Exception as e:
            logger.exception(e)
            results = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke(
                    notification_id,
                    'failed to get notifcation', e
                )
            )

        return(results)
Пример #6
0
    def delete(self):
        username = self.get_current_user()
        customer_name = (get_user_property(username, UserKeys.CurrentCustomer))
        uri = self.request.uri
        method = self.request.method
        try:
            tag_id = self.arguments.get('id', None)
            if tag_id:
                tag = TagsManager(username, customer_name, uri, method)
                results = tag.remove_tag(tag_id)

            else:
                results = (GenericResults(username, uri,
                                          method).incorrect_arguments())

            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(username, uri, method).something_broke(
                'agentids and tag_id', 'delete agents_in_tagid', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #7
0
    def post(self, tag_id):
        username = self.get_current_user()
        customer_name = (get_user_property(username, UserKeys.CurrentCustomer))
        uri = self.request.uri
        method = self.request.method
        try:
            reboot = self.arguments.get('reboot', None)
            shutdown = self.arguments.get('shutdown', None)
            apps_refresh = self.arguments.get('apps_refresh', None)
            operation = (StoreOperation(username, customer_name, uri, method))
            if reboot:
                results = (operation.reboot(tag_id))
            elif shutdown:
                results = (operation.shutdown(tag_id))
            elif apps_refresh:
                results = (operation.apps_refresh(tag_id))
            else:
                results = (GenericResults(username, uri,
                                          method).incorrect_arguments())

            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(username, uri,
                                      method).something_broke(tag_id, '', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #8
0
    def get_data(self):
        try:
            agent_data = get_agent_info(agent_id)
            agent_data[AgentKey.LastAgentUpdate] = (
                int(agent_data[AgentKey.LastAgentUpdate].strftime('%s'))
            )
            if agent_data:
                agent_data['tags'] = get_tags_by_agent_id(agent_id=self.agent_id)
                agent_data[CommonAppKeys.BASIC_RV_STATS] = (
                    get_all_app_stats_by_agentid(self.agent_id)
                )
                status = (
                    GenericResults(
                        self.username, uri, method
                        ).information_retrieved(agent_data, 1)
                )
                logger.info(status['message'])
            else:
                status = (
                    GenericResults(
                        self.username, uri, method
                        ).invalid_id(self.agent_id, 'agent_id')
                )
                logger.info(status['message'])

        except Exception as e:
            agent_data = None
            status = (
                GenericResults(
                    self.username, uri, method
                ).something_broke(self.agent_id, 'agents', e)
            )
            logger.error(status['message'])

        return(status)
Пример #9
0
def get_severity_bar_chart_stats_for_tag(username,
                                         customer_name,
                                         uri,
                                         method,
                                         tag_id,
                                         conn=None):
    try:
        sevs = (r.table(TagsPerAgentCollection, use_outdated=True).get_all(
            tag_id, index=TagsPerAgentIndexes.TagId
        ).pluck(TagsPerAgentKey.AgentId).eq_join(
            lambda x: [CommonAppKeys.AVAILABLE, x[AppsPerAgentKey.AgentId]],
            r.table(AppCollections.AppsPerAgent),
            index=AppsPerAgentIndexes.StatusAndAgentId).map({
                AppsKey.AppId:
                r.row['right'][AppsKey.AppId],
            }).eq_join(AppsKey.AppId, r.table(
                AppCollections.UniqueApplications)).filter(lambda x: x[
                    'right'][AppsKey.Hidden] == CommonKeys.NO).map({
                        AppsKey.AppId:
                        r.row['right'][AppsKey.AppId],
                        AppsKey.RvSeverity:
                        r.row['right'][AppsKey.RvSeverity]
                    }).group(AppsKey.RvSeverity).count().ungroup().order_by(
                        r.desc('reduction')).run(conn))
        data = app_stats_by_severity(sevs)

        results = (GenericResults(username, uri, method).information_retrieved(
            data, len(CommonSeverityKeys.ValidRvSeverities)))

    except Exception as e:
        results = (GenericResults(username, uri, method).something_broke(
            'widget severity stats', 'widget', e))
        logger.exception(results)

    return (results)
Пример #10
0
        def wrapped(*args, **kwargs):
            granted = False
            tornado_handler = args[0]
            username = tornado_handler.get_current_user()
            uri = tornado_handler.request.uri
            method = tornado_handler.request.method
            granted, status_code = (verify_permission_for_user(
                username, permission))
            if granted and status_code == GenericCodes.PermissionGranted:
                fn(*args, **kwargs)

            elif not granted and status_code == GenericCodes.PermissionDenied:
                results = (GenericResults(username, uri,
                                          method).permission_denied(username))

                tornado_handler.set_header('Content-Type', 'application/json')

                tornado_handler.write(dumps(results, indent=4))

            elif not granted and status_code == GenericCodes.InvalidPermission:
                results = (GenericResults(username, uri,
                                          method).invalid_permission(
                                              username, permission))

                tornado_handler.set_header('Content-Type', 'application/json')
                tornado_handler.write(dumps(results, indent=4))

            elif not granted and status_code == GenericCodes.InvalidId:
                results = (GenericResults(username, uri, method).invalid_id(
                    username, 'permissions'))

                tornado_handler.set_header('Content-Type', 'application/json')
                tornado_handler.write(dumps(results, indent=4))
Пример #11
0
    def create_tag(self, tag_name, prod_level='Production', conn=None):
        tag_id = None
        ninsert = {
            TagsKey.TagName: tag_name,
            TagsKey.CustomerName: self.customer_name,
            TagsKey.ProductionLevel: prod_level,
        }
        try:
            tag_exists = list(
                r
                .table(TagsCollection)
                .get_all([self.customer_name, tag_name],
                         index=TagsIndexes.TagNameAndCustomer)
                .pluck(TagsKey.TagId)
                .run(conn)
            )
            if len(tag_exists) == 0:
                inserted = (
                    r
                    .table(TagsCollection)
                    .insert(ninsert)
                    .run(conn)
                )
                if 'inserted' in inserted:
                    tag_id = inserted['generated_keys'][0]
                    data = {
                        TagsKey.TagId: tag_id,
                        TagsKey.TagName: tag_name,
                    }

                    status = (
                        GenericResults(
                            self.username, self.uri,
                            self.method
                        ).object_created(tag_id, tag_name, data)
                    )

                    logger.info(status['message'])

            else:
                status = (
                    GenericResults(
                        self.username, self.uri,
                        self.method
                    ).object_exists(tag_id, tag_name)
                )

                logger.warn(status['message'])

        except Exception as e:
            status = (
                GenericResults(
                    self.username, self.uri,
                    self.method
                ).something_broke(tag_name, 'while creating a tag', e)
            )

            logger.exception(e)

        return(status)
Пример #12
0
    def modify_alerting_rule(self, conn=None, **kwargs):
        try:
            data = self.__populate_data(kwargs)
            data_validated = self.__validate_data(data)
            rule_exists = (notification_rule_exists(
                data_validated['data'][NotificationKeys.NotificationId]))
            if rule_exists and data_validated['http_status'] == 200:
                (r.table(NotificationCollections.Notifications).replace(
                    data_validated['data']).run(conn))

                data_validated['data'][NotificationKeys.CreatedTime] = self.now
                data_validated['data'][
                    NotificationKeys.ModifiedTime] = self.now
                results = (GenericResults(
                    self.username, self.uri, self.method).object_updated(
                        data[NotificationKeys.NotificationId],
                        NotificationKeys.NotificationId,
                        data_validated['data']))
            else:
                results = (GenericResults(
                    self.username, self.uri, self.method).invalid_id(
                        data[NotificationKeys.NotificationId],
                        NotificationKeys.NotificationId))

        except Exception as e:
            logger.exception(e)
            return (GenericResults(self.username, self.uri,
                                   self.method).something_broke(
                                       'Failed to update notification',
                                       'notification', e))

        return (results)
Пример #13
0
    def get(self, group_id):
        active_user = self.get_current_user()
        uri = self.request.uri
        method = self.request.method
        count = 0
        group_data = {}
        try:
            group_data = get_group_properties(group_id)
            if group_data:
                count = 1
                results = (GenericResults(active_user, uri,
                                          method).information_retrieved(
                                              group_data, count))
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(active_user, uri,
                                      method).something_broke(
                                          active_user, 'Group', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #14
0
    def delete(self):
        active_user = self.get_current_user()
        uri = self.request.uri
        method = self.request.method
        usernames = self.arguments.get(ApiArguments.USERNAMES)
        try:
            if not isinstance(usernames, list):
                usernames = usernames.split()

            if not active_user in usernames:
                results = remove_users(usernames, active_user, uri, method)
            else:
                results = (GenericResults(active_user, uri,
                                          method).something_broke(
                                              active_user, 'User',
                                              'can not delete yourself'))
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(active_user, uri,
                                      method).something_broke(
                                          active_user, 'User', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #15
0
    def post(self, app_id):
        username = self.get_current_user().encode('utf-8')
        customer_name = (get_user_property(username, UserKeys.CurrentCustomer))
        uri = self.request.uri
        method = self.request.method
        try:
            severity = self.arguments.get('severity').capitalize()

            if severity in CommonSeverityKeys.ValidRvSeverities:
                sev_data = {AppsKey.RvSeverity: severity}
                update_app_data_by_app_id(app_id, sev_data,
                                          AppCollections.vFenseApps)

                results = GenericResults(username, uri, method).object_updated(
                    app_id, 'app severity', [sev_data])

                self.set_status(results['http_status'])
                self.set_header('Content-Type', 'application/json')
                self.write(json.dumps(results, indent=4))

            else:
                results = (PackageResults(username, uri,
                                          method).invalid_severity(severity))
                self.set_status(results['http_status'])
                self.set_header('Content-Type', 'application/json')
                self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(username, uri, method).something_broke(
                app_id, 'update_severity', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #16
0
    def get(self, username):
        active_user = self.get_current_user()
        uri = self.request.uri
        method = self.request.method
        count = 0
        user_data = {}
        try:
            granted, status_code = (verify_permission_for_user(
                active_user, Permissions.ADMINISTRATOR))
            if not username or username == active_user:
                user_data = get_user_properties(active_user)
            elif username and granted:
                user_data = get_user_properties(username)
            elif username and not granted:
                results = (return_results_for_permissions(
                    active_user, granted, status_code,
                    Permissions.ADMINISTRATOR, uri, method))

            if user_data:
                count = 1
                results = (GenericResults(active_user, uri,
                                          method).information_retrieved(
                                              user_data, count))
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(active_user, uri,
                                      method).something_broke(
                                          active_user, 'User', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #17
0
    def get_tags(self, uri=None, method=None, conn=None):
        try:
            tags = get_tags_by_agent_id(agent_id=self.agent_id)
            if tags:
                for i in range(len(tags)):
                    name = tags[i].pop('tag_name')
                    tags[i]['name'] = name

                    tag_id = tags[i].pop('tag_id')
                    tags[i]['id'] = tag_id

            status = (
                GenericResults(
                    self.username, uri, method
                ).information_retrieved(tags, 1)
            )
            logger.info(status['message'])

        except Exception as e:
            status = (
                GenericResults(
                    self.username, uri, method
                ).something_broke(self.agent_id, 'agent', e)
            )
            logger.error(status['message'])

        return(status)
Пример #18
0
    def post(self, agent_id):
        username = self.get_current_user()
        uri = self.request.uri
        method = self.request.method
        try:
            data = self.arguments.get('data')
            mem = data['memory']
            cpu = data['cpu']
            file_system = data['file_system']

            update_agent_monit_stats(agent=agent_id,
                                     memory=mem,
                                     cpu=cpu,
                                     file_system=file_system)
            results = (GenericResults(username, uri, method).object_updated(
                agent_id, 'monitoring data'))
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(username, uri, method).something_broke(
                agent_id, 'monitoring data', e))
            logger.exception(results)

            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(dumps(results, indent=4))
Пример #19
0
def agents_last_updated(username,
                        customer_name,
                        os_code=None,
                        tag_id=None,
                        uri=None,
                        method=None):

    agents_uptime_info = []
    agentids = get_agentids(os_code=os_code,
                            customer_name=customer_name,
                            tag_id=tag_id)
    for agentid in agentids:
        agent_info = get_agent_info(agentid=agentid)
        last_updated = agent_last_updated(agent_info)
        if last_updated:
            agents_uptime_info.append(last_updated)

    try:
        data = agents_uptime_info
        results = (GenericResults(
            username,
            uri,
            method,
        ).information_retrieved(data, len(data)))

    except Exception as e:
        logger.exception(e)
        results = (GenericResults(username, uri, method).something_broke(
            'Systems_os_details', 'failed to retrieve data', e))
    return (results)
Пример #20
0
def get_all_agentids(username,
                     customer_name,
                     count=30,
                     offset=0,
                     uri=None,
                     method=None,
                     conn=None):

    try:
        count = (r.table(AgentsCollection).get_all(
            customer_name, index=AgentKey.CustomerName).count().run(conn))

        data = list(
            r.table(AgentsCollection).get_all(
                customer_name,
                index=AgentKey.CustomerName).pluck(AgentKey.AgentId).order_by(
                    AgentKey.ComputerName).skip(offset).limit(count).run(conn))

        if data:
            for agent in data:
                agent[BASIC_RV_STATS] = (get_all_app_stats_by_agentid(
                    agent[AgentKey.AgentId]))
        status = (GenericResults(username, uri,
                                 method).information_retrieved(data, count))
        logger.info(status['message'])

    except Exception as e:
        status = (GenericResults(username, uri, method).something_broke(
            'get_all_agents', 'agent', e))
        return (status)
Пример #21
0
    def get(self, customer_name):
        active_user = self.get_current_user()
        uri = self.request.uri
        method = self.request.method
        count = 0
        customer_data = {}
        try:
            customer_data = get_properties_for_customer(customer_name)
            if customer_data:
                count = 1
                results = (GenericResults(active_user, uri,
                                          method).information_retrieved(
                                              customer_data, count))
            else:
                results = (GenericResults(active_user, uri, method).invalid_id(
                    customer_name, 'customer'))
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(active_user, uri,
                                      method).something_broke(
                                          active_user, 'User', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #22
0
    def get(self):
        username = self.get_current_user().encode('utf-8')
        customer_name = (get_user_property(username, UserKeys.CurrentCustomer))
        uri = self.request.uri
        method = self.request.method
        try:
            os_code = self.get_argument('os_code', None)
            os_string = self.get_argument('os_string', None)
            if not os_code and not os_string or os_code and not os_string:
                data = get_supported_os_codes()

            elif os_string:
                data = get_supported_os_strings(customer_name)

            results = (GenericResults(username, uri,
                                      method).information_retrieved(
                                          data, len(data)))
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(username, uri, method).something_broke(
                'Get OS Codes', 'Agent', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #23
0
    def delete_agent(self, uri, method, conn=None):
        try:
            agent_info = get_agent_info(self.agent_id)
            if agent_info:
                (
                    r
                    .table(AgentsCollection)
                    .get(self.agent_id)
                    .delete()
                    .run(conn)
                )

                (
                    r
                    .table(HardwarePerAgentCollection)
                    .get_all(self.agent_id, index=HardwarePerAgentIndexes.AgentId)
                    .delete()
                    .run(conn)
                )
                (
                    r
                    .table(TagsPerAgentCollection)
                    .get_all(self.agent_id, index=TagsPerAgentIndexes.AgentId)
                    .delete()
                    .run(conn)
                )

                rv_q = Queue('delete_agent', connection=rq_pool)
                rv_q.enqueue_call(
                    func=remove_all_app_data_for_agent,
                    args=(self.agent_id,),
                    timeout=3600,
                )
                status = (
                    GenericResults(
                        self.username, uri, method
                    ).object_deleted(self.agent_id, 'agents')
                )
                logger.info(status['message'])

            else:
                status = (
                    GenericResults(
                        self.username, uri, method
                    ).invalid_id(self.agent_id, 'agents')
                )
                logger.info(status['message'])

        except Exception as e:
            status = (
                GenericResults(
                    self.username, uri, method
                ).something_broke(self.agent_id, 'agents', e)
            )
            logger.exception(status['message'])

        return(status)
Пример #24
0
    def query_by_name(self, name, conn=None):
        try:
            agent = get_agent_info(self.agent_id)
            if agent:
                base = (
                    r
                    .table(self.CurrentAppsPerAgentCollection)
                    .get_all(self.agent_id, index=self.CurrentAppsPerAgentIndexes.AgentId)
                    .eq_join(self.CurrentAppsPerAgentKey.AppId, r.table(self.CurrentAppsCollection))
                    .zip()
                )
                if self.show_hidden == CommonKeys.NO:
                    base = base.filter({self.CurrentAppsKey.Hidden: CommonKeys.NO})

                packages = list(
                    base
                    .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name))
                    .map(self.map_hash)
                    .order_by(self.sort(self.sort_key))
                    .skip(self.offset)
                    .limit(self.count)
                    .run(conn)
                )

                pkg_count = (
                    base
                    .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name))
                    .count()
                    .run(conn)
                )

                return_status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).information_retrieved(packages, pkg_count)
                )

            else:
                return_status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).invalid_id(self.agent_id, 'agents')
                )

        except Exception as e:
            return_status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke(
                    "Package Searching went haywire",
                    'os_updates', e
                    )
            )
            logger.exception(e)

        return(return_status)
Пример #25
0
    def post(self, username):
        active_user = self.get_current_user()
        active_customer = (get_user_property(username,
                                             UserKeys.CurrentCustomer))
        uri = self.request.uri
        method = self.request.method
        results = None
        try:
            customer_context = (self.arguments.get(
                ApiArguments.CUSTOMER_CONTEXT, active_customer))
            action = self.arguments.get(ApiArguments.ACTION, ApiValues.ADD)

            ###Update Groups###
            group_ids = self.arguments.get(ApiArguments.GROUP_IDS, None)
            if group_ids and isinstance(group_ids, list):
                if action == ApiValues.ADD:
                    results = (add_user_to_groups(username, customer_context,
                                                  group_ids, username, uri,
                                                  method))
                if action == ApiValues.DELETE:
                    results = (remove_groups_from_user(username, group_ids,
                                                       username, uri, method))
            ###Update Customers###
            customer_names = self.arguments.get('customer_names')
            if customer_names and isinstance(customer_names, list):
                if action == 'add':
                    results = (add_user_to_customers(username, customer_names,
                                                     username, uri, method))

                elif action == 'delete':
                    results = (remove_customers_from_user(
                        username, customer_names, username, uri, method))

            if results:
                self.set_status(results['http_status'])
                self.set_header('Content-Type', 'application/json')
                self.write(json.dumps(results, indent=4))

            else:
                results = (GenericResults(active_user, uri,
                                          method).incorrect_arguments())

                self.set_status(results['http_status'])
                self.set_header('Content-Type', 'application/json')
                self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(active_user, uri,
                                      method).something_broke(
                                          active_user, 'User', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #26
0
    def filter_by(self, fkey, fval, conn=None):
        try:
            if fkey in self.list_of_valid_keys:
                count = (r.table(TagsCollection).filter({
                    fkey:
                    fval,
                    TagsKey.CustomerName:
                    self.customer_name
                }).count().run(conn))
                data = list(
                    r.table(TagsCollection).filter({
                        fkey:
                        fval,
                        TagsKey.CustomerName:
                        self.customer_name
                    }).order_by(self.sort(self.sort_key)).skip(
                        self.qoffset).limit(self.qcount).run(conn))
                if data:
                    for tag in xrange(len(data)):
                        data[tag][CommonAppKeys.BASIC_RV_STATS] = (
                            get_all_avail_stats_by_tagid(tag[TagsKey.TagId]))

                        agents_in_tag = list(
                            r.table(TagsPerAgentCollection).get_all(
                                data[tag][TagsPerAgentKey.Id],
                                index=TagsPerAgentIndexes.TagId).eq_join(
                                    TagsPerAgentKey.AgentId,
                                    r.table(AgentsCollection)).zip().pluck(
                                        TagsPerAgentKey.AgentId,
                                        AgentKey.ComputerName,
                                        AgentKey.DisplayName).run(conn))
                        data[tag]['agents'] = agents_in_tag

                status = (GenericResults(self.username, self.uri,
                                         self.method).information_retrieved(
                                             data, count))

                logger.info(status['message'])

            else:
                status = (GenericResults(self.username, self.uri,
                                         self.method).incorrect_arguments(
                                             data, count))

                logger.info(status['message'])

        except Exception as e:
            status = (GenericResults(self.username, self.uri,
                                     self.method).something_broke(
                                         'search_tags_by_filter', 'tags', e))

            logger.exception(status['message'])

        return (status)
Пример #27
0
    def filter_by_severity(self, sev, conn=None):
        try:
            if sev in CommonSeverityKeys.ValidRvSeverities:
                base = (
                    r
                    .table(self.CurrentAppsCollection)
                    .get_all(self.customer_name, sev, index=self.CurrentAppsIndexes.CustomerAndRvSeverity)
                )

                if self.show_hidden == CommonKeys.NO:
                    base = base.filter({self.CurrentAppsKey.Hidden: CommonKeys.NO})

                packages = list(
                    base
                    .map(self.map_hash)
                    .order_by(self.sort(self.sort_key))
                    .skip(self.offset)
                    .limit(self.count)
                    .run(conn)
                )

                pkg_count = (
                    base
                    .pluck(self.pluck_list)
                    .count()
                    .run(conn)
                )

                return_status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).information_retrieved(packages, pkg_count)
                )

            else:
                return_status = (
                    PackageResults(
                        self.username, self.uri, self.method
                    ).invalid_severity(sev)
                )

        except Exception as e:
            return_status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke(
                    "Package Searching went haywire",
                    'os_updates', e
                    )
            )
            logger.exception(e)

        return(return_status)
Пример #28
0
    def post(self, agent_id):
        username = self.get_current_user()
        customer_name = (get_user_property(username, UserKeys.CurrentCustomer))
        uri = self.request.uri
        method = self.request.method
        try:
            reboot = self.arguments.get('reboot', None)
            shutdown = self.arguments.get('shutdown', None)
            apps_refresh = self.arguments.get('apps_refresh', None)
            operation = (StoreAgentOperations(username, customer_name, uri,
                                              method))
            if reboot:
                granted, status_code = (verify_permission_for_user(
                    username, Permissions.REBOOT))
                if granted:
                    results = (operation.reboot([agent_id]))

                else:
                    results = (return_results_for_permissions(
                        username, granted, status_code, Permissions.REBOOT,
                        uri, method))

            elif shutdown:
                granted, status_code = (verify_permission_for_user(
                    username, Permissions.SHUTDOWN))
                if granted:
                    results = (operation.shutdown([agent_id]))

                else:
                    results = (return_results_for_permissions(
                        username, granted, status_code, Permissions.SHUTDOWN,
                        uri, method))

            elif apps_refresh:
                operation = (StorePatchingOperation(username, customer_name,
                                                    uri, method))
                results = (operation.apps_refresh([agent_id]))

            else:
                results = (GenericResults(username, uri,
                                          method).incorrect_arguments())

            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(username, uri,
                                      method).something_broke(agent_id, '', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))
Пример #29
0
def get_all_agents_per_appid(username,
                             customer_name,
                             uri,
                             method,
                             app_id,
                             conn=None):
    data = []
    try:
        agents = (r.table(AppCollections.vFenseAppsPerAgent).get_all(
            app_id, index=AgentAppsPerAgentKey.AppId).eq_join(
                AgentAppsPerAgentKey.AgentId,
                r.table(AgentsCollection)).zip().group(
                    lambda x: x[AgentAppsPerAgentKey.Status]).map(
                        lambda x: {
                            AGENTS: [{
                                AgentKey.ComputerName:
                                x[AgentKey.ComputerName],
                                AgentKey.DisplayName:
                                x[AgentKey.DisplayName],
                                AgentAppsPerAgentKey.AgentId:
                                x[AgentAppsPerAgentKey.AgentId]
                            }],
                            COUNT:
                            1
                        }).reduce(lambda x, y: {
                            AGENTS: x[AGENTS] + y[AGENTS],
                            COUNT: x[COUNT] + y[COUNT]
                        }).ungroup().run(conn))
        if agents:
            for i in agents:
                new_data = i['reduction']
                new_data[AgentAppsPerAgentKey.Status] = i['group']
                data.append(new_data)

        statuses = map(lambda x: x['status'], data)
        difference = set(ValidPackageStatuses).difference(statuses)
        if len(difference) > 0:
            for status in difference:
                status = {COUNT: 0, AGENTS: [], STATUS: status}
                data.append(status)

        results = (GenericResults(username, uri, method).information_retrieved(
            data, len(data)))

        logger.info(results)

    except Exception as e:
        results = (GenericResults(username, uri, method).something_broke(
            'getting_pkg_stats', 'updates', e))

        logger.info(results)

    return (results)
Пример #30
0
    def get(self):
        active_user = self.get_current_user()
        uri = self.request.uri
        method = self.request.method
        active_customer = (get_user_property(active_user,
                                             UserKeys.CurrentCustomer))
        customer_context = self.get_argument(ApiArguments.CUSTOMER_CONTEXT,
                                             None)
        all_customers = self.get_argument(ApiArguments.ALL_CUSTOMERS, None)
        user_name = self.get_argument(ApiArguments.USER_NAME, None)
        count = 0
        user_data = []
        try:
            granted, status_code = (verify_permission_for_user(
                active_user, Permissions.ADMINISTRATOR))
            if granted and not customer_context and not all_customers and not user_name:
                user_data = get_properties_for_all_users(active_customer)

            elif granted and customer_context and not all_customers and not user_name:

                user_data = get_properties_for_all_users(customer_context)

            elif granted and all_customers and not customer_context and not user_name:
                user_data = get_properties_for_all_users()

            elif granted and user_name and not customer_context and not all_customers:
                user_data = get_properties_for_user(user_name)
                if user_data:
                    user_data = [user_data]
                else:
                    user_data = []

            elif customer_context and not granted or all_customers and not granted:
                results = (return_results_for_permissions(
                    active_user, granted, status_code,
                    Permissions.ADMINISTRATOR, uri, method))

            count = len(user_data)
            results = (GenericResults(active_user, uri,
                                      method).information_retrieved(
                                          user_data, count))
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))

        except Exception as e:
            results = (GenericResults(active_user, uri,
                                      method).something_broke(
                                          active_user, 'User', e))
            logger.exception(e)
            self.set_status(results['http_status'])
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(results, indent=4))