Exemplo n.º 1
0
    def _insert_into_db(self,
                        hwinfo=None,
                        added_by='agent',
                        agent_id=None,
                        conn=None):
        collection = 'hardware_per_agent'
        added = True
        msg = ''
        logger.info(hwinfo)
        if hwinfo:
            try:
                if added_by == 'agent':
                    (r.table(collection).get_all(agent_id,
                                                 index='agent_id').filter({
                                                     'added_by':
                                                     added_by
                                                 }).delete().run(conn))

                for hw in hwinfo:

                    (r.table(collection).insert(hw, upsert=True).run(conn))
                msg = 'hardware added: %s' % (hwinfo)
                logger.info(msg)

            except Exception as e:
                added = False
                msg = ('Failed to add hardware: %s and %s' % (hwinfo, e))
                logger.exception(msg)

        return (added, msg)
Exemplo n.º 2
0
def fetch_agent_info(agent_id, keys_to_pluck=None, conn=None):
    """Retrieve information of an agent
    Args:
        agent_id(str): 36 character uuid of the agent you are updating

    Kwargs:
        keys_to_pluck(list): (Optional) Specific keys that you are retrieving
        from the database

    Basic Usage:
        >>> from vFense.core.agent._db import fetch_agent_info
        >>> agent_id = '52faa1db-290a-47a7-a4cf-e4ad70e25c38'
        >>> keys_to_pluck = ['production_level', 'needs_reboot']
        >>> fetch_agent_info(agent_id, keys_to_pluck)

    Return:
        Dictionary of the agent data
        {
            u'agent_id': u'52faa1db-290a-47a7-a4cf-e4ad70e25c38',
            u'production_level': u'Development'
        }
    """
    data = {}
    try:
        if agent_id and keys_to_pluck:
            data = r.table(AgentCollections.Agents).get(agent_id).merge(Merge.TAGS).pluck(keys_to_pluck).run(conn)

        elif agent_id and not keys_to_pluck:
            data = r.table(AgentCollections.Agents).get(agent_id).merge(Merge.TAGS).run(conn)

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 3
0
def filter_by_and_query(username,
                        customer_name,
                        keys_to_pluck,
                        key=AgentKey.ComputerName,
                        count=30,
                        offset=0,
                        query=None,
                        uri=None,
                        method=None,
                        conn=None):

    if query:
        count = (r.table(AgentsCollection).get_all(
            customer_name, index=AgentKey.CustomerName).filter(
                (r.row[AgentKey.ComputerName].match("(?i)" +
                                                    query))).count().run(conn))

        data = list(
            r.table(AgentsCollection).get_all(
                customer_name, index=AgentKey.CustomerName).filter(
                    (r.row[key].match("(?i)" + query))).pluck(
                        keys_to_pluck).skip(offset).limit(count).run(conn))
    else:

        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(
                    keys_to_pluck).skip(offset).limit(count).run(conn))
    return (data)
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def _set_operation_per_agent_merge(self):
        agent_pluck = self._set_agent_collection_pluck()
        merge = ({
            AgentOperationKey.CreatedTime:
            r.row[AgentOperationKey.CreatedTime].to_epoch_time(),
            AgentOperationKey.UpdatedTime:
            r.row[AgentOperationKey.UpdatedTime].to_epoch_time(),
            AgentOperationKey.CompletedTime:
            r.row[AgentOperationKey.CompletedTime].to_epoch_time(),
            OperationSearchValues.AGENTS:
            (r.table(OperationCollections.OperationPerAgent).get_all(
                r.row[AgentOperationKey.OperationId],
                index=OperationPerAgentIndexes.OperationId).coerce_to(
                    'array').eq_join(OperationPerAgentKey.AgentId,
                                     r.table(AgentCollections.Agents)).zip().
             pluck(agent_pluck).merge(
                 lambda x: {
                     OperationPerAgentKey.PickedUpTime:
                     x[OperationPerAgentKey.PickedUpTime].to_epoch_time(),
                     OperationPerAgentKey.CompletedTime:
                     x[OperationPerAgentKey.CompletedTime].to_epoch_time(),
                     OperationPerAgentKey.ExpiredTime:
                     x[OperationPerAgentKey.ExpiredTime].to_epoch_time(),
                 }))
        })

        return (merge)
Exemplo n.º 6
0
def get_users_of_customer(customer_name=None, conn=None):

    if not customer_name:
        return None

    users = (
        r.table(Collection.UsersPerCustomer)
        .get_all(
            customer_name,
            index=UsersPerCustomerKey.CustomerId
        )
        .pluck(UsersPerCustomerKey.UserId)
        .eq_join(
            UsersPerCustomerKey.UserId,
            r.table(Collection.Users),
            index=UserKey.UserName
        )
        .zip()
        .run(conn)
    )

    u = []
    for user in users:
        u.append(user)

    return u
Exemplo n.º 7
0
    def _set_agent_operation_base_query(self):
        base_filter = (r.table(OperationCollections.Agent))
        if self.customer_name:
            base_filter = (r.table(OperationCollections.Agent).get_all(
                self.customer_name, index=AgentOperationIndexes.CustomerName))

        return (base_filter)
Exemplo n.º 8
0
def get_customers_of_user(user_name=None, conn=None):

    if not user_name:
        return None

    customers = (
        r.table(Collection.UsersPerCustomer)
        .get_all(
            user_name,
            index=UsersPerCustomerKey.UserId
        )
        .pluck(UsersPerCustomerKey.CustomerId)
        .eq_join(
            UsersPerCustomerKey.CustomerId,
            r.table(Collection.Customers),
            index=CustomerKey.CustomerName
        )
        .zip()
        .run(conn)
    )

    c = []
    for customer in customers:
        c.append(customer)

    return c
Exemplo n.º 9
0
def delete_multiple_jobs(job_ids, conn=None):
    """Delete all multiple jobs in the queue by job ids.
        DO NOT CALL DIRECTLY
    Args:
        job_ids (list): List of job ids to delete ['id1', 'id2']

    Basic Usage:
        >>> from vFense.queue._db import delete_multiple_jobs
        >>> job_ids = ['id1', 'id2']
        >>> delete_multiple_jobs(job_ids)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    jobs_deleted = 0
    try:
        if isinstance(job_ids, list):
            jobs = r.table(QueueCollections.Agent).get_all(*job_ids).delete().run(conn)
            jobs_deleted = jobs.get("deleted")

        else:
            jobs = r.table(QueueCollections.Agent).get(job_ids).delete().run(conn)
            jobs_deleted = jobs.get("deleted")

    except Exception as e:
        logger.exception(e)

    return jobs_deleted
Exemplo n.º 10
0
    def get_sending_emails(self, notif_rules, conn=None):
        try:
            email_sender_list = []
            for notif in notif_rules:
                if notif[NotificationKeys.Group]:
                    users_list = (r.table(Collection.Groups).filter({
                        'name':
                        notif['group']
                    }).filter(lambda x: x['customer']['name'] == notif[
                        'customer_name']).map(lambda x: x['users']).run(conn))
                    if users_list:
                        users = map(lambda x: x['name'], user_list[0])
                        email_sender_list += (r.expr(users).map(
                            lambda user: r.table('users').get(user)).map(
                                lambda x: x['email']).run(conn))

                elif notif[NotificationKeys.User]:
                    email = (r.table(Collection.Users).get(
                        notif[NotificationKeys.User]).pluck(
                            UserKey.Email).run(conn))
                    if email:
                        email_sender_list.append(email[UserKey.Email])

        except Exception as e:
            logger.exception(e)

        return (email_sender_list)
Exemplo n.º 11
0
    def lower_version_exists_of_app(self,
                                    app,
                                    app_info,
                                    status=CommonAppKeys.AVAILABLE,
                                    conn=None):
        try:
            lower_version_exists = list(
                r.table(AppCollections.UniqueApplications).get_all(
                    app_info[AppsKey.Name],
                    index=AppsIndexes.Name).filter(lambda x: x[
                        AppsKey.Version] < app_info[AppsKey.Version]).eq_join(
                            lambda y: [
                                app[AgentKey.AgentId],
                                app[AppsPerAgentKey.AppId],
                            ],
                            r.table(AppCollections.AppsPerAgent),
                            index=AppsPerAgentIndexes.AgentId).zip().filter({
                                AppsPerAgentKey.Status:
                                status
                            }).pluck(AppsKey.AppId, AppsPerAgentKey.Id,
                                     AppsKey.Name,
                                     AppsPerAgentKey.AgentId).run(conn))

        except Exception as e:
            logger.exception(e)

        return (lower_version_exists)
Exemplo n.º 12
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)
Exemplo n.º 13
0
 def insert_app_and_delete_old(self, app, lower_apps, conn=None):
     try:
         (r.table(AppCollections.SupportedAppsPerAgent).insert(app).run(conn))
         for lower_app in lower_apps:
             (r.table(AppCollections.AppsPerAgent).get(lower_app[AppsPerAgentKey.Id]).delete().run(conn))
     except Exception as e:
         logger.exception(e)
Exemplo n.º 14
0
 def _set_install_operation_email_alert_merge(self):
     agent_pluck = self._set_agent_collection_pluck()
     app_without = self._set_app_collection_without()
     merge = ({
         AgentOperationKey.CreatedTime:
         (r.row[AgentOperationKey.CreatedTime].to_iso8601()),
         AgentOperationKey.UpdatedTime:
         (r.row[AgentOperationKey.UpdatedTime].to_iso8601()),
         AgentOperationKey.CompletedTime:
         (r.row[AgentOperationKey.CompletedTime].to_iso8601()),
         OperationSearchValues.AGENTS:
         (r.table(OperationCollections.OperationPerAgent).get_all(
             r.row[AgentOperationKey.OperationId],
             index=OperationPerAgentIndexes.OperationId).coerce_to(
                 'array').eq_join(OperationPerAgentKey.AgentId,
                                  r.table(AgentCollections.Agents)).zip().
          pluck(agent_pluck).merge(
              lambda x: {
                  OperationPerAgentKey.PickedUpTime:
                  (x[OperationPerAgentKey.PickedUpTime].to_iso8601()),
                  OperationPerAgentKey.CompletedTime:
                  (x[OperationPerAgentKey.CompletedTime].to_iso8601()),
                  OperationSearchValues.APPLICATIONS_FAILED:
                  (r.table(OperationCollections.OperationPerApp
                           ).get_all([
                               x[AgentOperationKey.OperationId], x[
                                   OperationPerAgentKey.AgentId]
                           ],
                                     index=OperationPerAppIndexes.
                                     OperationIdAndAgentId).
                   filter(lambda y: y[OperationPerAppKey.Results] ==
                          AgentOperationCodes.ResultsReceivedWithErrors).
                   coerce_to('array').merge(
                       lambda y: {
                           OperationPerAppKey.ResultsReceivedTime:
                           (y[OperationPerAppKey.ResultsReceivedTime].
                            to_iso8601())
                       }).without(app_without)),
                  OperationSearchValues.APPLICATIONS_PASSED:
                  (r.table(OperationCollections.OperationPerApp
                           ).get_all([
                               x[AgentOperationKey.OperationId], x[
                                   OperationPerAgentKey.AgentId]
                           ],
                                     index=OperationPerAppIndexes.
                                     OperationIdAndAgentId).
                   filter(lambda y: y[OperationPerAppKey.Results
                                      ] == AgentOperationCodes.
                          ResultsReceived).coerce_to('array').merge(
                              lambda y: {
                                  OperationPerAppKey.ResultsReceivedTime:
                                  (y[OperationPerAppKey.ResultsReceivedTime]
                                   .to_iso8601())
                              }).without(app_without))
              }))
     })
     return (merge)
Exemplo n.º 15
0
 def insert_app_and_delete_old(self, app, lower_apps, conn=None):
     try:
         (r.table(
             AppCollections.SupportedAppsPerAgent).insert(app).run(conn))
         for lower_app in lower_apps:
             (r.table(AppCollections.AppsPerAgent).get(
                 lower_app[AppsPerAgentKey.Id]).delete().run(conn))
     except Exception as e:
         logger.exception(e)
Exemplo n.º 16
0
    def fetch_all_by_tagid(self, tag_id, conn=None):
        """Fetch all operations by tag id
        Basic Usage:
            >>> from vFense.operations.search._db_agent_search import FetchAgentOperations
            >>> customer_name = 'default'
            >>> tag_id = '78076908-e93f-4116-8d49-ad42b4ad0297'
            >>> operation = FetchAgentOperations(customer_name)
            >>> operation.fetch_all_by_tagid(tag_id)
        Returns:
            List [count, data]
            [
                1,
                [
                    {
                        "agents_expired_count": 0,
                        "cpu_throttle": "normal",
                        "agents_total_count": 2,
                        "plugin": "rv",
                        "tag_id": "78076908-e93f-4116-8d49-ad42b4ad0297",
                        "agents_completed_with_errors_count": 0,
                        "action_performed_on": "tag",
                        "created_by": "admin",
                        "agents_pending_pickup_count": 0,
                        "completed_time": 1398110835,
                        "operation_status": 6006,
                        "agents_completed_count": 2,
                        "operation_id": "d6956a46-165f-49b6-a3df-872a1453ab88",
                        "created_time": 1398110770,
                        "agents_pending_results_count": 0,
                        "operation": "install_os_apps",
                        "updated_time": 1398110835,
                        "net_throttle": 0,
                        "agents_failed_count": 0,
                        "restart": "none",
                        "customer_name": "default"
                    }
                ]
            ]
        """

        count = 0
        data = []
        base_time_merge = self._set_base_time_merge()
        try:
            count = (r.table(OperationCollections.Agent).get_all(
                tag_id, index=AgentOperationKey.TagId).count().run(conn))

            data = list(
                r.table(OperationCollections.Agent).get_all(
                    tag_id, index=AgentOperationKey.TagId).order_by(
                        self.sort(self.sort_key)).skip(self.offset).limit(
                            self.count).merge(base_time_merge).run(conn))

        except Exception as e:
            logger.exception(e)

        return (count, data)
Exemplo n.º 17
0
def update_supported_apps(json_data):

    try:
        rv_q = Queue("downloader", connection=RQ_PKG_POOL)
        conn = db_connect()

        inserted_count = 0
        all_customers = list(r.table(Collection.Customers).map(lambda x: x[CustomerKey.CustomerName]).run(conn))

        for i in range(len(json_data)):
            json_data[i][SupportedAppsKey.Customers] = all_customers
            json_data[i][SupportedAppsKey.ReleaseDate] = r.epoch_time(json_data[i][SupportedAppsKey.ReleaseDate])
            json_data[i][SupportedAppsKey.FilesDownloadStatus] = PackageCodes.FilePendingDownload
            json_data[i][SupportedAppsKey.Hidden] = "no"
            json_data[i][SupportedAppsKey.VulnerabilityId] = ""

            insert_app_data(json_data[i], DownloadCollections.LatestDownloadedSupported)
            file_data = json_data[i].get(SupportedAppsKey.FileData)
            add_file_data(json_data[i][SupportedAppsKey.AppId], file_data)
            data_to_update = {SupportedAppsKey.Customers: all_customers}
            exists = r.table(AppCollections.SupportedApps).get(json_data[i][SupportedAppsKey.AppId]).run(conn)

            if exists:
                updated = (
                    r.table(AppCollections.SupportedApps)
                    .get(json_data[i][SupportedAppsKey.AppId])
                    .update(data_to_update)
                    .run(conn)
                )

            else:
                updated = r.table(AppCollections.SupportedApps).insert(json_data[i]).run(conn)

            rv_q.enqueue_call(
                func=download_all_files_in_app,
                args=(
                    json_data[i][SupportedAppsKey.AppId],
                    json_data[i][SupportedAppsKey.OsCode],
                    None,
                    file_data,
                    0,
                    AppCollections.SupportedApps,
                ),
                timeout=86400,
            )

            inserted_count += updated["inserted"]

        conn.close()

        update_apps = IncomingSupportedApps()
        update_apps.sync_supported_updates_to_all_agents(json_data)

    except Exception as e:
        logger.exception(e)
Exemplo n.º 18
0
Arquivo: _db.py Projeto: vFense/vFense
def fetch_group_by_name(group_name,
                        customer_name,
                        fields_to_pluck=None,
                        conn=None):
    """Retrieve a group by its name from the database
    Args:
        group_name (str): Name of group.
        customer_name (str): name of the customer, that the group belongs to.
    
    Kwargs:
        fields_to_pluck (list): List of fields you want to retrieve.

    Basic Usage:
        >>> from vFense.group._db import fetch_group_by_name
        >>> group_name = 'Administrator'
        >>> customer_name = 'default'
        >>> fetch_group_by_name(group_name, customer_name)

    Returns:
        Returns a Dict of the properties of a customer
        {
            u'group_name': u'Administrator',
            u'customer_name': u'default',
            u'id': u'8757b79c-7321-4446-8882-65457f28c78b',
            u'Permissions': [
                u'administrator'
            ]
        }
    """
    try:
        if fields_to_pluck:
            data = list(
                r.table(GroupCollections.Groups).filter({
                    GroupKeys.GroupName:
                    group_name,
                    GroupKeys.CustomerName:
                    customer_name
                }).pluck(fields_to_pluck).run(conn))
        else:
            data = list(
                r.table(GroupCollections.Groups).filter({
                    GroupKeys.GroupName:
                    group_name,
                    GroupKeys.CustomerName:
                    customer_name
                }).run(conn))

        if len(data) == 1:
            data = data[0]

    except Exception as e:
        logger.exception(e)
        data = {}

    return (data)
Exemplo n.º 19
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)
Exemplo n.º 20
0
def fetch_agent_ids(customer_name=None, agent_os=None, conn=None):
    """Retrieve a list of agent ids
    Kwargs:
        customer_name (str): Name of the customer, where the agent is located
        agent_os (str):  The os code you are filtering on.
            (linux or windows or darwin)

    Basic Usage:
        >>> from vFense.core.agent._db import fetch_agent_ids
        >>> customer_name = 'default'
        >>> os_code = 'os_code'
        >>> fetch_agent_ids(customer_name, os_code)

    Returns:
        List of agent ids
        [
            u'52faa1db-290a-47a7-a4cf-e4ad70e25c38',
            u'3ea8fd7a-8aad-40da-aff0-8da6fa5f8766'
        ]
    """
    data = []
    try:
        if customer_name and agent_os:
            data = list(
                r.table(AgentCollections.Agents)
                .get_all(customer_name, index=AgentIndexes.CustomerName)
                .filter({AgentKey.OsCode: agent_os})
                .map(lambda x: x[AgentKey.AgentId])
                .run(conn)
            )

        elif customer_name and not agent_os:
            data = list(
                r.table(AgentCollections.Agents)
                .get_all(customer_name, index=AgentIndexes.CustomerName)
                .map(lambda x: x[AgentKey.AgentId])
                .run(conn)
            )

        elif agent_os and not customer_name:
            data = list(
                r.table(AgentCollections.Agents)
                .filter({AgentKey.OsCode: agent_os})
                .map(lambda x: x[AgentKey.AgentId])
                .run(conn)
            )

        elif not agent_os and not customer_name:
            data = list(r.table(AgentCollections.Agents).map(lambda x: x[AgentKey.AgentId]).run(conn))

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 21
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)
Exemplo n.º 22
0
Arquivo: _db.py Projeto: vFense/vFense
def fetch_group_properties(group_id, conn=None):
    """Retrieve a group and all of its properties.
    Args:
        group_id: 36 Character UUID.

    Basic Usage:
        >>> from vFense.group._db import fetch_group_properties
        >>> group_id = 'a7d4690e-5851-4d92-9626-07e16acaea1f'
        >>> fetch_group_properties(group_id)

    Returns:
        Returns a Dict of the properties of a group
        {
            "users": [
                {
                    "user_name": "admin"
                }, 
                {
                    "user_name": "agent_api"
                }
            ], 
            "permissions": [
                "administrator"
            ], 
            "group_name": "Administrator", 
            "id": "1b74a706-34e5-482a-bedc-ffbcd688f066", 
            "customer_name": "default"
        }
    """
    map_hash = (lambda x: {
        GroupKeys.GroupId:
        x[GroupKeys.GroupId],
        GroupKeys.GroupName:
        x[GroupKeys.GroupName],
        GroupKeys.CustomerName:
        x[GroupKeys.CustomerName],
        GroupKeys.Permissions:
        x[GroupKeys.Permissions],
        GroupKeys.Users: (r.table(GroupCollections.GroupsPerUser).get_all(
            group_id, index=GroupsPerUserIndexes.GroupId).coerce_to('array').
                          pluck(GroupsPerUserKeys.UserName))
    })
    data = {}
    try:
        data = (r.table(
            GroupCollections.Groups).get_all(group_id).map(map_hash).run(conn))
        if data:
            data = data[0]

    except Exception as e:
        logger.exception(e)

    return (data)
Exemplo n.º 23
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)
Exemplo n.º 24
0
def update_supported_apps(json_data):

    try:
        rv_q = Queue('downloader', connection=RQ_PKG_POOL)
        conn = db_connect()

        inserted_count = 0
        all_customers = list(
            r.table(Collection.Customers).map(
                lambda x: x[CustomerKey.CustomerName]).run(conn))

        for i in range(len(json_data)):
            json_data[i][SupportedAppsKey.Customers] = all_customers
            json_data[i][SupportedAppsKey.ReleaseDate] = \
                r.epoch_time(json_data[i][SupportedAppsKey.ReleaseDate])
            json_data[i][SupportedAppsKey.FilesDownloadStatus] = \
                PackageCodes.FilePendingDownload
            json_data[i][SupportedAppsKey.Hidden] = 'no'
            json_data[i][SupportedAppsKey.VulnerabilityId] = ''

            insert_app_data(json_data[i],
                            DownloadCollections.LatestDownloadedSupported)
            file_data = json_data[i].get(SupportedAppsKey.FileData)
            add_file_data(json_data[i][SupportedAppsKey.AppId], file_data)
            data_to_update = {SupportedAppsKey.Customers: all_customers}
            exists = (r.table(AppCollections.SupportedApps).get(
                json_data[i][SupportedAppsKey.AppId]).run(conn))

            if exists:
                updated = (r.table(AppCollections.SupportedApps).get(
                    json_data[i][SupportedAppsKey.AppId]).update(
                        data_to_update).run(conn))

            else:
                updated = (r.table(AppCollections.SupportedApps).insert(
                    json_data[i]).run(conn))

            rv_q.enqueue_call(func=download_all_files_in_app,
                              args=(json_data[i][SupportedAppsKey.AppId],
                                    json_data[i][SupportedAppsKey.OsCode],
                                    None, file_data, 0,
                                    AppCollections.SupportedApps),
                              timeout=86400)

            inserted_count += updated['inserted']

        conn.close()

        update_apps = IncomingSupportedApps()
        update_apps.sync_supported_updates_to_all_agents(json_data)

    except Exception as e:
        logger.exception(e)
Exemplo n.º 25
0
Arquivo: _db.py Projeto: vFense/vFense
def fetch_agent_ids(customer_name=None, agent_os=None, conn=None):
    """Retrieve a list of agent ids
    Kwargs:
        customer_name (str): Name of the customer, where the agent is located
        agent_os (str):  The os code you are filtering on.
            (linux or windows or darwin)

    Basic Usage:
        >>> from vFense.core.agent._db import fetch_agent_ids
        >>> customer_name = 'default'
        >>> os_code = 'os_code'
        >>> fetch_agent_ids(customer_name, os_code)

    Returns:
        List of agent ids
        [
            u'52faa1db-290a-47a7-a4cf-e4ad70e25c38',
            u'3ea8fd7a-8aad-40da-aff0-8da6fa5f8766'
        ]
    """
    data = []
    try:
        if customer_name and agent_os:
            data = list(
                r.table(AgentCollections.Agents).get_all(
                    customer_name, index=AgentIndexes.CustomerName).filter({
                        AgentKey.OsCode:
                        agent_os
                    }).map(lambda x: x[AgentKey.AgentId]).run(conn))

        elif customer_name and not agent_os:
            data = list(
                r.table(AgentCollections.Agents).get_all(
                    customer_name, index=AgentIndexes.CustomerName).map(
                        lambda x: x[AgentKey.AgentId]).run(conn))

        elif agent_os and not customer_name:
            data = list(
                r.table(AgentCollections.Agents).filter({
                    AgentKey.OsCode:
                    agent_os
                }).map(lambda x: x[AgentKey.AgentId]).run(conn))

        elif not agent_os and not customer_name:
            data = list(
                r.table(AgentCollections.Agents).map(
                    lambda x: x[AgentKey.AgentId]).run(conn))

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 26
0
Arquivo: _db.py Projeto: vFense/vFense
def fetch_groups_for_user(username, fields_to_pluck=None, conn=None):
    """Retrieve all groups for a user by username
    Args:
        username (str): Get all groups for which this user is part of.

    Kwargs:
        fields_to_pluck (list): List of fields you want to pluck
        from the database

    Basic Usage:
        >>> from vFense.group._db import fetch_groups_for_user
        >>> username = '******'
        >>> fetch_groups_for_user(username)

    Returns:
        Returns a list of groups that the user belongs to.
        [
            {
                u'group_name': u'FooLah',
                u'group_id': u'0834e656-27a5-4b13-ba56-635797d0d1fc',
                u'user_name': u'alien',
                u'id': u'ee54820c-cb4e-46a1-9d11-73afe8c4c4e3',
                u'customer_name': u'default'
            },
            {
                u'group_name': u'Administrator',
                u'group_id': u'8757b79c-7321-4446-8882-65457f28c78b',
                u'user_name': u'alien',
                u'id': u'6bd51a04-fcec-46a7-bbe1-48c6221115ec',
                u'customer_name': u'default'
            }
        ]
    """
    data = []
    try:
        if username and not fields_to_pluck:
            data = list(
                r.table(GroupCollections.GroupsPerUser).get_all(
                    username, index=GroupsPerUserIndexes.UserName).run(conn))

        elif username and fields_to_pluck:
            data = list(
                r.table(GroupCollections.GroupsPerUser).get_all(
                    username, index=GroupsPerUserIndexes.UserName).pluck(
                        fields_to_pluck).run(conn))

    except Exception as e:
        logger.exception(e)

    return (data)
Exemplo n.º 27
0
    def fetch_all_by_agentid(self, agent_id, conn=None):
        """Fetch all operations by agent id
        Basic Usage:
            >>> from vFense.operations.search._db_agent_search import FetchAgentOperations
            >>> customer_name = 'default'
            >>> agent_id = '6c0209d5-b350-48b7-808a-158ddacb6940'
            >>> operation = FetchAgentOperations(customer_name)
            >>> operation.fetch_all_by_agentid(agent_id)
        Returns:
            List
            [
                {
                    "agents_expired_count": 0,
                    "agents_total_count": 1,
                    "tag_id": null,
                    "agents_completed_with_errors_count": 0,
                    "created_by": "admin",
                    "agents_pending_pickup_count": 0,
                    "completed_time": 1398092303,
                    "operation_status": 6006,
                    "agents_completed_count": 1,
                    "operation_id": "6c0209d5-b350-48b7-808a-158ddacb6940",
                    "created_time": 1398092302,
                    "agents_pending_results_count": 0,
                    "operation": "install_os_apps",
                    "updated_time": 1398092303,
                    "agents_failed_count": 0,
                    "customer_name": "default"
                }
            ]
        """
        count = 0
        data = []
        base_time_merge = self._set_base_time_merge()
        try:
            count = (r.table(OperationCollections.Agent).get_all(
                agent_id,
                index=AgentOperationIndexes.AgentIds).count().run(conn))

            data = list(
                r.table(OperationCollections.Agent).get_all(
                    agent_id, index=AgentOperationIndexes.AgentIds).order_by(
                        self.sort(self.sort_key)).skip(self.offset).limit(
                            self.count).merge(base_time_merge).run(conn))

        except Exception as e:
            logger.exception(e)

        return (count, data)
Exemplo n.º 28
0
def validate_permission_for_user(username, customer_name, permission, conn=None):
    permission_exist = False
    try:
        is_empty = (
            r
            .table(GroupCollections.GroupsPerUser)
            .get_all(username, index=GroupsPerUserIndexes.UserName)
            .filter({GroupsPerUserKeys.CustomerName: customer_name})
            .eq_join(
                lambda group: group[GroupsPerUserKeys.GroupName],
                r.table(GroupCollections.Groups),
                index=GroupIndexes.GroupName
            )
            .zip()
            .filter(r.row[GroupKeys.Permissions].contains(permission))
            .is_empty()
            .run(conn)
        )
        if not is_empty:
            permission_exist = True

        else:
            is_admin_empty = (
                r
                .table(GroupCollections.GroupsPerUser)
                .get_all(username, index=GroupsPerUserIndexes.UserName)
                .filter({GroupsPerUserKeys.CustomerName: customer_name})
                .eq_join(
                    lambda group: group[GroupsPerUserKeys.GroupName],
                    r.table(GroupCollections.Groups),
                    index=GroupIndexes.GroupName
                )
                .zip()
                .filter(
                    r.row[GroupKeys.Permissions].contains(
                        Permissions.ADMINISTRATOR
                    )
                )
                .is_empty()
                .run(conn)
            )

            if not is_admin_empty:
                permission_exist = True

    except Exception as e:
        logger.exception(e)

    return(permission_exist)
Exemplo n.º 29
0
def get_all_expired_jobs(now=None, conn=None):
    """Retrieve all expired jobs
    Kwargs:
        now (float): The epoch time to compare against,
            during the retrieval process.
            Default: (Is to use the epoch time of right now)

    Basic Usage:
        >>> from vFense.queue._db import get_all_expired_jobs
        >>> now = 1396780822.0
        >>> get_all_expired_jobs(now)

    Returns:
        List of dictionairies
        [
            {
                "agent_queue_ttl": 1396778416, 
                "plugin": "rv", 
                "order_id": 1, 
                "server_queue_ttl": 1396777816, 
                "agent_id": "d4119b36-fe3c-4973-84c7-e8e3d72a3e02", 
                "created_time": 1396777216, 
                "operation_id": "b95837d9-5df7-4ab0-9449-a7be196a2b12", 
                "operation": "updatesapplications", 
                "id": "f9817e07-6877-4857-aef3-e80f57022ac8", 
                "expire_minutes": 10, 
                "customer_name": "default"
            }
        ]
    """
    expired_jobs = []
    try:
        if not now:
            expired_jobs = list(
                r.table(QueueCollections.Agent).filter(lambda x: x[AgentQueueKey.ServerQueueTTL] <= r.now()).run(conn)
            )

        else:
            expired_jobs = list(
                r.table(QueueCollections.Agent)
                .filter(lambda x: x[AgentQueueKey.ServerQueueTTL].to_epoch_time() <= now)
                .run(conn)
            )

    except Exception as e:
        logger.exception(e)

    return expired_jobs
Exemplo n.º 30
0
def recently_released_packages(username,
                               customer_name,
                               uri,
                               method,
                               count=5,
                               conn=None):

    data = []

    try:
        data = list(
            r.table(AppCollections.AppsPerAgent, use_outdated=True).get_all(
                [CommonAppKeys.AVAILABLE, customer_name],
                index=AppsPerAgentIndexes.StatusAndCustomer).eq_join(
                    AppsKey.AppId, r.table(AppCollections.UniqueApplications)).
            map(
                lambda x: {
                    AppsKey.Name:
                    x['right'][AppsKey.Name],
                    AppsKey.AppId:
                    x['right'][AppsKey.AppId],
                    AppsKey.RvSeverity:
                    x['right'][AppsKey.RvSeverity],
                    AppsKey.Hidden:
                    x['right'][AppsKey.Hidden],
                    AppsKey.ReleaseDate:
                    x['right'][AppsKey.ReleaseDate].to_epoch_time(),
                    'count': (r.table(AppCollections.AppsPerAgent).get_all(
                        [x['right'][AppsKey.AppId], CommonAppKeys.AVAILABLE],
                        index=AppsPerAgentIndexes.AppIdAndStatus).eq_join(
                            AppsKey.AppId,
                            r.table(AppCollections.UniqueApplications)).
                              filter(lambda y: y['right'][AppsKey.Hidden] ==
                                     CommonKeys.NO).count())
                }).pluck(AppsKey.Name, AppsKey.AppId, AppsKey.Hidden,
                         AppsKey.RvSeverity, AppsKey.ReleaseDate,
                         'count').order_by(r.desc(
                             AppsKey.ReleaseDate)).limit(count).run(conn))

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

    except Exception as e:
        results = (GenericResults(username, uri, method).something_broke(
            'recently released os apps', 'widget', e))
        logger.exception(results)

    return (results)
Exemplo n.º 31
0
 def _delete_old_supported_apps_from_agent(self, agent_id, conn):
     (
         r.table(AppCollections.SupportedAppsPerAgent)
         .get_all(agent_id, index=SupportedAppsPerAgentIndexes.AgentId)
         .delete()
         .run(conn)
     )
Exemplo n.º 32
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)
Exemplo n.º 33
0
def update_data_in_table(primary_key, data, collection, conn=None):
    """Update data by the primary key of the collection
    Args:
        primary_key (str): The primary key, of the object you are searching.
        data (dict): Dictionary of the data you are updating.
            you are inserting.
        collection (str): The name of the collection you are updating.

    Basic Usage:
        >>> from vFense.core._db import update_data_in_table
        >>> primary_key = 'default'
        >>> data = {'enabled': 'no'}
        >>> collection = 'users'
        >>> update_data_in_table(primary_key, data, collection)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """

    results = {}
    try:
        results = (r.table(collection).get(primary_key).update(data).run(conn))

    except Exception as e:
        logger.exception(e)

    return results
Exemplo n.º 34
0
def fetch_vuln_ids(name, version, os_string, conn=None):
    """Retrieve Ubuntu USN IDS and CVE_IDS for an Application
    Args:
        name (str): The name of the application
        version (str): The version of the application
        os_string (str): The Version of Ubuntu (Ubuntu 12.04 LTS)

    Basic Usage:
        >>> from vFense.plugins.vuln.ubuntu._db import fetch_ubuntu_vuln_ids
        >>> name = 'nvidia-173'
        >>> version = '173.14.22-0ubuntu11.2'
        >>> os_string = 'Ubuntu 10.04 LTS'
        >>> fetch_ubuntu_vuln_ids(name, version, os_string)

    Returns:
        Dictionary
        {
            "cve_ids": [], 
            "bulletin_id": "USN-1523-1"
        }
    """
    data = []
    try:
        data = list(
            r.table(UbuntuSecurityCollection.Bulletin).get_all(
                [name, version],
                index=UbuntuSecurityBulletinIndexes.NameAndVersion).pluck(
                    UbuntuSecurityBulletinKey.BulletinId,
                    UbuntuSecurityBulletinKey.CveIds).run(conn))

    except Exception as e:
        logger.exception(e)

    return (data)
Exemplo n.º 35
0
def get_all_stats_by_appid(
    username, customer_name, uri, method, app_id, collection=AppCollections.AppsPerAgent, conn=None
):

    if collection == AppCollections.AppsPerAgent:
        CurrentAppsPerAgentCollection = AppCollections.AppsPerAgent
        CurrentAppsPerAgentKey = AppsPerAgentKey
        CurrentAppsPerAgentIndexes = AppsPerAgentIndexes

    elif collection == AppCollections.SupportedAppsPerAgent:
        CurrentAppsPerAgentCollection = AppCollections.SupportedAppsPerAgent
        CurrentAppsPerAgentKey = SupportedAppsPerAgentKey
        CurrentAppsPerAgentIndexes = SupportedAppsPerAgentIndexes

    elif collection == AppCollections.CustomAppsPerAgent:
        CurrentAppsPerAgentCollection = AppCollections.CustomAppsPerAgent
        CurrentAppsPerAgentKey = CustomAppsPerAgentKey
        CurrentAppsPerAgentIndexes = CustomAppsPerAgentIndexes

    elif collection == AppCollections.vFenseAppsPerAgent:
        CurrentAppsPerAgentCollection = AppCollections.vFenseAppsPerAgent
        CurrentAppsPerAgentKey = AgentAppsPerAgentKey
        CurrentAppsPerAgentIndexes = AgentAppsPerAgentIndexes

    try:
        data = []
        apps = (
            r.table(CurrentAppsPerAgentCollection)
            .get_all([app_id, customer_name], index=CurrentAppsPerAgentIndexes.AppIdAndCustomer)
            .group(CurrentAppsPerAgentKey.Status)
            .count()
            .ungroup()
            .run(conn)
        )
        if apps:
            for i in apps:
                new_data = i["reduction"]
                new_data = {
                    CurrentAppsPerAgentKey.Status: i["group"],
                    CommonAppKeys.COUNT: i["reduction"],
                    CommonAppKeys.NAME: i["group"].capitalize(),
                }
                data.append(new_data)

        statuses = map(lambda x: x["status"], data)
        difference = set(CommonAppKeys.ValidPackageStatuses).difference(statuses)
        if len(difference) > 0:
            for status in difference:
                status = {CommonAppKeys.COUNT: 0, CommonAppKeys.STATUS: status, CommonAppKeys.NAME: status.capitalize()}
                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
Exemplo n.º 36
0
def object_exist(primary_key, collection, conn=None):
    """Verify if an object exist in the database, by primary key.
    Args:
        primary_key (str):  The primary key that you are doing a get against.
        collection (str):  The table aka collection you are doing the query against.

    Basic Usage:
        >>> from vFense._db import object_exist
        >>> collection, = 'users'
        >>> primary_key = 'admin'
        >>> object_exist(primary_key, collection)

    Returns:
        Boolean
    """
    exist = False
    try:
        is_empty = (
            r.table(collection).get_all(primary_key).is_empty().run(conn))
        if not is_empty:
            exist = True

    except Exception as e:
        logger.exception(e)

    return exist
Exemplo n.º 37
0
def db_delete(collection=None, _id=None, conn=None):
    """Attempts to delete data from the DB.

    Tries to delete a document based on the id or filter provided. If filter is
    used, the first document returned is deleted.

    Args:

        collection_name: Name of the collection to be used.

        _id: Id (primary key) representing a document

    Returns:

        True if document was deleted, False otherwise.

    """

    success = None

    if _id:

        result = r.table(collection).get(_id).delete().run(conn)

        if 'deleted' in result and result['deleted'] > 0:

            success = True

    return success
Exemplo n.º 38
0
def fetch_supported_os_strings(customer_name, conn=None):
    """Retrieve all the operating systems that is in the database
    Args:
        customer_name (str): Name of the customer, where the agent is located

    Basic Usage:
        >>> from vFense.core.agent._db import fetch_supported_os_strings
        >>> customer_name = 'default'
        >>> fetch_supported_os_strings(customer_name)

    Returns:
        List of available operating system strings in the database
        [
            u'CentOS 6.5',
            u'Ubuntu 12.04',
            u'Windows 7 Professional Service Pack 1',
            u'Windows 8.1 '
        ]
    """
    data = []
    try:
        data = (
            r.table(AgentCollections.Agents)
            .get_all(customer_name, index=AgentIndexes.CustomerName)
            .pluck(AgentKey.OsString)
            .distinct()
            .map(lambda x: x[AgentKey.OsString])
            .run(conn)
        )

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 39
0
def move_agents_to_customer(agent_ids, new_customer, conn=None):
    """Move a list of agents into another customer
    Args:
        agent_ids (list): List of agent ids
        new_customer (str): Name of the new customer.

    Basic Usage:
        >>> from vFense.agent._db import move_agents_to_customer
        >>> new_customer = 'test'
        >>> agent_ids = ['7f242ab8-a9d7-418f-9ce2-7bcba6c2d9dc']
        >>> move_agents_to_customer(agent_ids, new_customer)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    data = {}
    try:
        data = (
            r.expr(agent_ids)
            .for_each(
                lambda agent_id: r.table(AgentCollections.Agents)
                .get(agent_id)
                .update({AgentKey.CustomerName: new_customer})
            )
            .run(conn)
        )

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 40
0
def insert_agent_data(agent_data, conn=None):
    """ Insert a new agent and its properties into the database
        This function should not be called directly.
    Args:
        agent_data (list|dict): Can either be a list of
            dictionaries or a dictionary of the data
            you are inserting.

    Basic Usage:
        >>> from vFense.core.agent._db import insert_agent_data
        >>> agent_data = {'customer_name': 'vFense', 'needs_reboot': 'no'}
        >>> insert_agent_data(agent_data)

    Return:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    data = {}
    try:
        data = r.table(AgentCollections.Agents).insert(agent_data).run(conn)

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 41
0
def fetch_production_levels_from_agent(customer_name, conn=None):
    """Retrieve all the production levels that is in the database
    Args:
        customer_name (str): Name of the customer, where the agent is located

    Basic Usage:
        >>> from vFense.core.agent._db import fetch_production_levels_from_agent
        >>> customer_name = 'default'
        >>> fetch_production_levels_from_agent(customer_name)

    Returns:
        List of Production levels in the system
        [
            u'Development',
            u'Production'
        ]
    """
    data = []
    try:
        data = (
            r.table(AgentCollections.Agents)
            .get_all(customer_name, index=AgentIndexes.CustomerName)
            .pluck(AgentKey.ProductionLevel)
            .distinct()
            .map(lambda x: x[AgentKey.ProductionLevel])
            .run(conn)
        )

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 42
0
def db_get(
    collection=None,
    primary_id=None,
    pluck=None,
    conn=None
):
    if (
        not collection
        or not primary_id
    ):
        return None

    query = r.table(collection).get(primary_id)

    if pluck:

        if isinstance(pluck, list):
            doc = query.pluck(*pluck).run(conn)
        else:
            doc = query.pluck(pluck).run(conn)

    else:
        doc = query.run(conn)

    return doc
Exemplo n.º 43
0
def get_email_config(customer_name=None, conn=None):
    mail_config = None
    config_exists = False
    msg = ""
    try:
        mail_config = list(
            r.table(NotificationCollections.NotificationPlugins)
            .get_all(customer_name, index=NotificationPluginIndexes.CustomerName)
            .filter({NotificationPluginKeys.PluginName: "email"})
            .run(conn)
        )
        if not mail_config:
            mail_config = {
                "modified_time": "",
                "username": "",
                "password": "",
                "server": "",
                "port": "",
                "is_tls": "",
                "is_ssl": "",
                "from_email": "",
                "to_email": "",
                "last_modify_user": "",
            }
            msg = "mail_config does not exist"
        else:
            config_exists = True

    except Exception as e:
        msg = "Failed to get mail config: %s" % (str(e))
        logger.exception(e)

    return {"pass": config_exists, "message": msg, "data": mail_config}
Exemplo n.º 44
0
def delete_data_in_table(primary_key, collection, conn=None):
    """Delete data in a collection
    Args:
        primary_key (str): The primary key of the collection
            you are searching for,
        collection (str): The name of the collection you are removing all data from.

    Basic Usage:
        >>> from vFense.core._db import delete_data_in_table
        >>> primary_key = 'default'
        >>> collection = 'agents'
        >>> delete_data_in_table(primary_key, collection)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """

    results = {}
    try:
        results = (r.table(collection).get(primary_key).delete().run(conn))

    except Exception as e:
        logger.exception(e)

    return results
Exemplo n.º 45
0
def move_all_agents_to_customer(current_customer, new_customer, conn=None):
    """Move all agents in current customer to the new customer.
    Args:
        current_customer (str): Name of the current customer.
        new_customer (str): Name of the new customer.

    Basic Usage:
        >>> from vFense.agent._db import move_all_agents_to_customer
        >>> current_customer = 'default'
        >>> new_customer = 'test'
        >>> move_all_agents_to_customer(current_customer, new_customer)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    data = {}
    try:
        data = (
            r.table(AgentCollections.Agents)
            .get_all(current_customer, index=AgentIndexes.CustomerName)
            .update({AgentKey.CustomerName: new_customer})
            .run(conn)
        )

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 46
0
    def insert_app(self, app, conn=None):
        try:
            (r.table(
                AppCollections.SupportedAppsPerAgent).insert(app).run(conn))

        except Exception as e:
            logger.exception(e)
Exemplo n.º 47
0
def insert_data_in_table(data, collection, conn=None):
    """Insert data in a collection
    Args:
        data (list|dict): List of dictionaries or a dictionary of the data
            you are inserting.
        collection (str): The name of the collection you are removing all data from.

    Basic Usage:
        >>> from vFense.core._db import insert_data_in_table
        >>> data = [{'name': 'foo'}]
        >>> collection = 'agents'
        >>> insert_data_in_table(data, collection)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """

    results = {}
    try:
        results = (r.table(collection).insert(data, upsert=True).run(conn))

    except Exception as e:
        logger.exception(e)

    return results
Exemplo n.º 48
0
def insert_into_agent_queue(operation, conn=None):
    """Insert data into the agent_queue
        DO NOT CALL DIRECTLY
    Args:
        operation (list|dict): operation data

    Basic Usage:
        >>> from vFense.queue._db import insert_into_agent_queue
        >>> operation = [{'operation': 'data'}]
        >>> insert_into_agent_queue(operation)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    data = {}
    try:
        operation[AgentQueueKey.CreatedTime] = r.epoch_time(operation[AgentQueueKey.CreatedTime])
        operation[AgentQueueKey.ServerQueueTTL] = r.epoch_time(operation[AgentQueueKey.ServerQueueTTL])
        operation[AgentQueueKey.AgentQueueTTL] = r.epoch_time(operation[AgentQueueKey.AgentQueueTTL])

        data = r.table(QueueCollections.Agent).insert(operation).run(conn)

    except Exception as e:
        logger.exception(e)

    return data
Exemplo n.º 49
0
Arquivo: _db.py Projeto: vFense/vFense
def update_user(username, user_data, conn=None):
    """Update  user's properties
    Args:
        username (str): username of the user you are updating
        user_data (list|dict): Dictionary of the data you are updating

    Basic Usage::
        >>> from vFense.user._db import update_user
        >>> username = '******'
        >>> data = {'production_level': 'Development', 'needs_reboot': 'no'}
        >>> update_user(username, data)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    data = {}
    try:
        data = (r.table(
            UserCollections.Users).get(username).update(user_data).run(conn))

    except Exception as e:
        logger.exception(e)

    return (data)
Exemplo n.º 50
0
Arquivo: _db.py Projeto: vFense/vFense
def user_status_toggle(username, conn=None):
    """Enable or disable a user
    Args:
        username (str): The username you are enabling or disabling

    Basic Usage:
        >>> from vFense.user._db import status_toggle
        >>> username = '******'
        >>> status_toggle(username)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    try:
        toggled = (r.table(UserCollections.Users).get(username).update({
            UserKeys.Enabled:
            (r.branch(r.row[UserKeys.Enabled] == CommonKeys.YES, CommonKeys.NO,
                      CommonKeys.YES))
        }).run(conn))

    except Exception as e:
        logger.exception(e)

    return (toggled)
Exemplo n.º 51
0
def top_packages_needed(username, customer_name,
                        uri, method, count=5, conn=None):
    
    apps_needed=[]
    
    try:
        data = (
            r
            .table(AppCollections.AppsPerAgent)
            .get_all(
                [CommonAppKeys.AVAILABLE, customer_name],
                index=AppsPerAgentIndexes.StatusAndCustomer
            )
            .eq_join(AppsKey.AppId, r.table(AppCollections.UniqueApplications))
            .filter(
                lambda x: x['right'][AppsKey.Hidden] == CommonKeys.NO
            )
            .map(
                lambda x:
                {
                    AppsKey.Name: x['right'][AppsKey.Name],
                    AppsKey.AppId: x['right'][AppsKey.AppId],
                    AppsKey.RvSeverity: x['right'][AppsKey.RvSeverity],
                    AppsKey.ReleaseDate: x['right'][AppsKey.ReleaseDate].to_epoch_time(),
                }
            )
            .group(AppsKey.Name, AppsKey.AppId, AppsKey.RvSeverity, AppsKey.ReleaseDate)
            .count()
            .ungroup()
            .map(
                lambda x:
                {
                    AppsKey.Name: x['group'][0],
                    AppsKey.AppId: x['group'][1],
                    AppsKey.RvSeverity: x['group'][2],
                    AppsKey.ReleaseDate: x['group'][3],
                    'count': x['reduction'],
                }
            )
            .order_by(r.desc('count'), r.desc(AppsKey.ReleaseDate))
            .limit(count)
            .run(conn)
        )

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

    except Exception as e:
        results = (
            GenericResults(
                username, uri, method
            ).something_broke('top os apps needed', 'widget', e)
        )
        logger.exception(results)

    return(results)
Exemplo n.º 52
0
    def get_all(self, conn=None):
        try:
            count = (
                r
                .table(TagsCollection)
                .get_all(self.customer_name, index=TagsIndexes.CustomerName)
                .count()
                .run(conn)
            )
            data = list(
                r
                .table(TagsCollection)
                .get_all(self.customer_name, index=TagsIndexes.CustomerName)
                .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(
                            data[tag][TagsKey.TagId]
                        )
                    )

                    agents_in_tag = list(
                        r
                        .table(TagsPerAgentCollection)
                        .get_all(data[tag][TagsPerAgentKey.TagId],
                                 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'])

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

            logger.exception(status['message'])

        return(status)
Exemplo n.º 53
0
def fetch_appids_by_agentid_and_status(agent_id, status, sev=None,
        collection=AppCollections.AppsPerAgent, conn=None):

    if collection == AppCollections.AppsPerAgent:
        join_table = AppCollections.UniqueApplications

    elif collection == AppCollections.CustomAppsPerAgent:
        join_table = AppCollections.CustomApps

    elif collection == AppCollections.SupportedAppsPerAgent:
        join_table = AppCollections.SupportedApps

    elif collection == AppCollections.vFenseAppsPerAgent:
        join_table = AppCollections.vFenseApps


    if sev:
        appids = list(
            r
            .table(collection)
            .get_all(
                [
                    status, agent_id
                ],
                index=DbCommonAppPerAgentIndexes.StatusAndAgentId
            )
            .eq_join(
                lambda x:
                [
                    x[DbCommonAppKeys.AppId],
                    sev
                ],
                r.table(join_table),
                index=DbCommonAppIndexes.AppIdAndRvSeverity
            )
            .map(
                lambda y: y['right'][DbCommonAppKeys.AppId]
            )
            .run(conn)
        )

    else:
        appids = list(
            r
            .table(collection)
            .get_all(
                [
                    status, agent_id
                ],
                index=DbCommonAppPerAgentIndexes.StatusAndAgentId
            )
            .map(
                lambda y: y[DbCommonAppPerAgentKeys.AppId]
            )
            .run(conn)
        )

    return appids
Exemplo n.º 54
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)
Exemplo n.º 55
0
    def sync_supported_updates_to_all_agents(self, apps):
        try:
            conn = db_connect()
            deleted_count = 0
            (r.table(AppCollections.SupportedAppsPerAgent).delete().run(conn))
            conn.close()
            self.update_agents_with_supported(apps)

        except Exception as e:
            logger.exception(e)
Exemplo n.º 56
0
    def get_tag(self, tag_id, conn=None):
        try:
            tag_info = (
                r
                .table(TagsCollection)
                .get(tag_id)
                .run(conn)
            )

            if tag_info:
                agents_in_tag = list(
                    r
                    .table(TagsPerAgentCollection)
                    .get_all(tag_id, index=TagsPerAgentIndexes.TagId)
                    .eq_join(TagsPerAgentKey.AgentId, r.table(AgentsCollection))
                    .zip()
                    .pluck(
                        TagsPerAgentKey.AgentId, AgentKey.ComputerName,
                        AgentKey.DisplayName)
                    .run(conn)
                )

                tag_info[CommonAppKeys.BASIC_RV_STATS] = (
                    get_all_app_stats_by_tagid(tag_id)
                )

                tag_info['agents'] = agents_in_tag

                status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).information_retrieved(tag_info, 1)
                )

                logger.info(status['message'])

            else:
                status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).invalid_id(tag_id, 'tag_id')
                )

                logger.info(status['message'])

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

            logger.exception(status['message'])

        return(status)
Exemplo n.º 57
0
def db_get_all(
    collection=None,
    conn=None
):

    result = list(
        r.table(collection)
        .run(conn)
    )

    return result
Exemplo n.º 58
0
def get_customer(customer_name=None, conn=None):

    if not customer_name:
        return None

    customer = (
        r.table(Collection.Customers)
        .get(customer_name)
        .run(conn)
    )

    return customer