Exemple #1
0
def update_agent_operation_pickup_time(
        operation_id, db_time, conn=None
    ):
    """Update an operation per agent.
        DO NOT CALL DIRECTLY
    Args:
        operation_id (str): the id of the operation.
        db_time (rql date object): r.epoch_time

    Basic Usage:
        >>> from vFense.operations._db import update_agent_operation_pickup_time
        >>> operation_id = '5dc03727-de89-460d-b2a7-7f766c83d2f1'
        >>> db_time = r.epoch_time(time.time())
        >>> update_agent_operation_pickup_time(operation_id, db_time)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    data = {}
    try:
        data = (
            r
            .table(OperationCollections.Agent)
            .get(operation_id)
            .update(
                {
                    AgentOperationKey.AgentsPendingPickUpCount: (
                        r.branch(
                            r.row[AgentOperationKey.AgentsPendingPickUpCount] > 0,
                            r.row[AgentOperationKey.AgentsPendingPickUpCount] - 1,
                            r.row[AgentOperationKey.AgentsPendingPickUpCount],
                        )
                    ),
                    AgentOperationKey.AgentsPendingResultsCount: (
                        r.branch(
                            r.row[AgentOperationKey.AgentsPendingResultsCount] < (
                                r.row[AgentOperationKey.AgentsTotalCount]
                            ),
                            r.row[AgentOperationKey.AgentsPendingResultsCount] + 1,
                            r.row[AgentOperationKey.AgentsPendingResultsCount]
                        )
                    ),
                    AgentOperationKey.UpdatedTime: db_time
                }
            )
            .run(conn)
        )

    except Exception as e:
        logger.exception(e)

    return data
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
def fetch_user_and_all_properties(username, conn=None):
    """Retrieve a user and all of its properties
        This query is beautiful :)
    Args:
        username (str): Name of the user.

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

    Returns:
        Dictionary of user properties.
        {
            "current_customer": "default", 
            "customers": [
                {
                    "admin": true, 
                    "name": "default"
                }
            ], 
            "groups": [
                {
                    "group_id": "1b74a706-34e5-482a-bedc-ffbcd688f066", 
                    "group_name": "Administrator"
                }
            ], 
                "default_customer": "default", 
                "user_name": "admin", 
                "permissions": [
                    "administrator"
                ]
        }
    """
    data = {}
    map_hash = (
        {
            UserKeys.DefaultCustomer: r.row[UserKeys.DefaultCustomer],
            UserKeys.CurrentCustomer: r.row[UserKeys.CurrentCustomer],
            UserKeys.Email: r.row[UserKeys.Email],
            UserKeys.FullName: r.row[UserKeys.FullName],
            UserKeys.UserName: r.row[UserKeys.UserName],
            UserKeys.Groups: (
                r
                .table(GroupCollections.GroupsPerUser)
                .get_all(username, index=GroupsPerUserIndexes.UserName)
                .coerce_to('array')
                .pluck(GroupsPerUserKeys.GroupId, GroupsPerUserKeys.GroupName)
            ),
            UserKeys.Customers: (
                r
                .table(CustomerCollections.CustomersPerUser)
                .get_all(username, index=CustomerPerUserIndexes.UserName)
                .coerce_to('array')
                .map(lambda x:
                    {
                        Permissions.ADMINISTRATOR: r.branch(
                            r
                            .table(GroupCollections.GroupsPerUser)
                            .get_all(username, index=GroupsPerUserIndexes.UserName)
                            .coerce_to('array')
                            .eq_join(lambda y:
                                y[GroupKeys.GroupName],
                                r.table(GroupCollections.Groups),
                                index=GroupsPerUserIndexes.GroupName
                            )
                            .zip()
                            .filter(
                                lambda z:
                                z[GroupKeys.Permissions]
                                .contains(Permissions.ADMINISTRATOR)
                            ),
                            True,
                            False
                        ),
                        CustomerPerUserKeys.CustomerName: x[CustomerPerUserKeys.CustomerName]
                    }
                )
            ),
            UserKeys.Permissions: (
                r
                .table(GroupCollections.GroupsPerUser)
                .get_all(username, index=GroupsPerUserIndexes.UserName)
                .coerce_to('array')
                .eq_join(lambda x:
                    x[GroupKeys.GroupName],
                    r.table(GroupCollections.Groups),
                    index=GroupsPerUserIndexes.GroupName
                )
                .zip()
                .map(lambda x: x[GroupKeys.Permissions])[0]
            )
        }
    )

    try:
        data = (
            r
            .table(UserCollections.Users)
            .get_all(username)
            .map(map_hash)
            .run(conn)
        )
        if data:
            data = data[0]

    except Exception as e:
        logger.exception(e)

    return(data)
Exemple #5
0
def fetch_users_and_all_properties(customer_name=None, conn=None):
    """Retrieve a user and all of its properties
        This query is beautiful :)
    Kwargs:
        customer_name (str): Name of the customer, where the users belong to.

    Basic Usage:
        >>> from vFense.user._db import fetch_users_and_all_properties
        >>> customer_name = 'default'
        >>> fetch_user_and_all_properties(username')

    Returns:
        List of users and their properties.
        [
            {
                "current_customer": "default", 
                "customers": [
                    {
                        "admin": true, 
                        "name": "default"
                    }
                ], 
                "groups": [
                    {
                        "group_id": "1b74a706-34e5-482a-bedc-ffbcd688f066", 
                        "group_name": "Administrator"
                    }
                ], 
                    "default_customer": "default", 
                    "user_name": "admin", 
                    "permissions": [
                        "administrator"
                    ]
            }
        ]
    """
    data = []
    map_hash = (lambda x:
        {
            UserKeys.DefaultCustomer: x[UserKeys.DefaultCustomer],
            UserKeys.CurrentCustomer: x[UserKeys.CurrentCustomer],
            UserKeys.UserName: x[UserKeys.UserName],
            UserKeys.Groups: (
                r
                .table(GroupCollections.GroupsPerUser)
                .get_all(x
                    [GroupsPerUserKeys.UserName],
                    index=GroupsPerUserIndexes.UserName
                )
                .coerce_to('array')
                .pluck(GroupsPerUserKeys.GroupId, GroupsPerUserKeys.GroupName)
            ),
            UserKeys.Customers: (
                r
                .table(CustomerCollections.CustomersPerUser)
                .get_all(
                    x[CustomerPerUserKeys.UserName],
                    index=CustomerPerUserIndexes.UserName
                )
                .coerce_to('array')
                .map(lambda y:
                    {
                        Permissions.ADMINISTRATOR: r.branch(
                            r
                            .table(GroupCollections.GroupsPerUser)
                            .get_all(
                                y[GroupsPerUserKeys.UserName],
                                index=GroupsPerUserIndexes.UserName
                            )
                            .coerce_to('array')
                            .eq_join(lambda z:
                                z[GroupKeys.GroupName],
                                r.table(GroupCollections.Groups),
                                index=GroupsPerUserIndexes.GroupName
                            )
                            .zip()
                            .filter({GroupsPerUserKeys.UserName: y[GroupsPerUserKeys.UserName]})
                            .filter(
                                lambda a:
                                a[GroupKeys.Permissions]
                                .contains(Permissions.ADMINISTRATOR)
                            ).distinct(),
                            True,
                            False
                        ),
                        CustomerPerUserKeys.CustomerName: y[CustomerPerUserKeys.CustomerName]
                    }
                )
            ),
            UserKeys.Permissions: (
                r
                .table(GroupCollections.GroupsPerUser)
                .get_all(x[GroupsPerUserKeys.UserName], index=GroupsPerUserIndexes.UserName)
                .coerce_to('array')
                .eq_join(lambda b:
                    b[GroupKeys.GroupName],
                    r.table(GroupCollections.Groups),
                    index=GroupsPerUserIndexes.GroupName
                )
                .zip()
                .map(lambda b: b[GroupKeys.Permissions])[0]
            )
        }
    )

    try:
        if customer_name:
            data = list(
                r
                .table(CustomerCollections.CustomersPerUser)
                .get_all(
                    customer_name, index=CustomerPerUserIndexes.CustomerName
                )
                .pluck(CustomerPerUserKeys.CustomerName, CustomerPerUserKeys.UserName)
                .distinct()
                .eq_join(lambda x:
                    x[UserKeys.UserName],
                    r.table(UserCollections.Users)
                )
                .zip()
                .map(map_hash)
                .run(conn)
            )

        else:
            data = list(
                r
                .table(UserCollections.Users)
                .map(map_hash)
                .run(conn)
            )

    except Exception as e:
        logger.exception(e)

    return(data)
Exemple #6
0
def fetch_app_data_to_send_to_agent(
        app_id, agent_id,
        collection=AppCollections.UniqueApplications,
        conn=None
    ):
    """Fetch application data by app id
    Args:
        app_id (str): 64 character ID of the application.
        agent_id (str): 36 character UUID of the agent.

    Kwargs:
        collection (str): The name of the apps per agent collection,
            that will be used when updating the application data.

    Basic Usage:
        >>> from vFense.plugins.patching._db import fetch_app_data
        >>> app_id = 'c726edf62d1d17ca8b420f24bbdc9c8fa58d000b51d31614e3826c2fb37a2929'
        >>> agent_id = '33ba8521-b2e5-47dc-9bdc-0f1e3384049d'
        >>> collection = 'unique_applications'
        >>> fetch_app_data_by_agentid(app_id, agent_id, collection)

    Returns:
        Dictionary
        {
            "file_data": [
                {
                    "file_hash": "47dc1daa42e6d53e1a881f4ed9c5472f6732665af2cba082f8fa3931199fb746",
                    "file_name": "gpgv_1.4.11-3ubuntu2.5_amd64.deb",
                    "file_uri": "http://us.archive.ubuntu.com/ubuntu/pool/main/g/gnupg/gpgv_1.4.11-3ubuntu2.5_amd64.deb",
                    "file_size": 185400
                }
            ],
            "cli_options": "",
            "version": "1.4.11-3ubuntu2.5",
            "name": "gpgv"
        }
    """

    data = {}
    try:
        data = list(
            r
            .table(collection)
            .get_all(app_id)
            .map(
                lambda app:
                {
                    DbCommonAppKeys.Name: app[DbCommonAppKeys.Name],
                    DbCommonAppKeys.Version: app[DbCommonAppKeys.Version],
                    DbCommonAppKeys.CliOptions: (
                        r.branch(
                            app.has_fields(DbCommonAppKeys.CliOptions) == False,
                            '',
                            app[DbCommonAppKeys.CliOptions]
                        )
                    ),
                    CommonFileKeys.PKG_FILEDATA: (
                        r
                        .table(FileCollections.Files)
                        .filter(
                            lambda x: (
                                x[FilesKey.AppIds].contains(app_id)
                                &
                                x[FilesKey.AgentIds].contains(agent_id)
                            )
                        )
                        .coerce_to('array')
                        .without(FilesKey.AppIds, FilesKey.AgentIds)
                    )
                }
            )
            .run(conn)
        )
        if data:
            data = data[0]

    except Exception as e:
        logger.exception(e)

    return data
Exemple #7
0
def update_hidden_status(
        app_ids, hidden=CommonKeys.TOGGLE,
        collection=AppCollections.UniqueApplications,
        conn=None
    ):
    """Update the global hidden status of an application
    Args:
        app_ids (list): List of application ids.

    Kwargs:
        hidden (str, optional): yes, no, or toggle
            default = toggle
        collection (str, optional): The collection you are updating for.
            collection = unique_applications

    Basic Usage:
        >>> from vFense.plugins.patching._db import update_hidden_status
        >>> hidden = 'toggle'
        >>> app_ids = ['c71c32209119ad585dd77e67c082f57f1d18395763a5fb5728c02631d511df5c']
        >>> update_hidden_status(app_ids, hidden)

    Returns:
        Tuple (status_code, count, error, generated ids)
        >>> (2001, 1, None, [])
    """
    data = {}
    try:
        if hidden == CommonKeys.YES or hidden == CommonKeys.NO:
            data = (
                r
                .expr(app_ids)
                .for_each(
                    lambda app_id:
                    r
                    .table(collection)
                    .get(app_id)
                    .update(
                        {
                            DbCommonAppKeys.Hidden: hidden
                        }
                    )
                )
                .run(conn)
            )

        elif hidden == CommonKeys.TOGGLE:
            for app_id in app_ids:
                data = (
                    r
                    .table(collection)
                    .get(app_id)
                    .update(
                        {
                            DbCommonAppKeys.Hidden: (
                                r.branch(
                                    r.row[DbCommonAppKeys.Hidden] == CommonKeys.YES,
                                    CommonKeys.NO,
                                    CommonKeys.YES
                                )
                            )
                        }
                    )
                    .run(conn)
                )

    except Exception as e:
        logger.exception(e)

    return data