Exemple #1
0
def passwd(client, required_by, username, new_password):
    """Allow to change the password of a user.
    
    Parameters
    ----------
    client --> pymoMongoClient class, MongoDB client
    required_by --> str, user who required the operation
    username --> str, the name of the user for which the operation is required
    new_password --> str, the new password for the user
    
    Returns
    -------
    updatedone_documents --> list, the list of the conditions for updating MongoDB documents, the values which have to be updated and the collection in which perform the update 
    """
    #for master namenode
    updatedone_documents = []
    #only the root and the user itself can perform this operation
    #e.g. only me can change my password (or the sysadmin)
    if required_by != 'root' and required_by != username:
        logging.warning(
            'Access denied: the operation required is not allowed on {}'.
            format(username))
        raise AccessDeniedException(username)
    users = get_users(client)
    #check if the user exists
    if not users.find_one({'name': username}):
        logging.warning('The user does not exist: {}'.format(username))
        raise UserNotFoundException(username)  #the user does not exist
    #update the new password
    users.update_one({'name': username}, {'$set': {'password': new_password}})
    #insert into the list for aligning the other namenodes
    updatedone_documents.append(({
        'name': username
    }, {
        '$set': {
            'password': new_password
        }
    }, 'users'))
    logging.info('Password updated for user {}'.format(username))
    return updatedone_documents
def mkfs(client, required_by):
    """Allow to initialize a file system but the user has required the operation must
    to be root, because everything in the file system will be deleted and will be created 
    a new empty one; there will be created a root directory (/) and a directory for a default user.
       
    Parameters
    ----------
    client --> pymoMongoClient class, MongoDB client
    required_by --> str, user who required the operation (must be root)
       
    Returns
    -------
    (root_usr_id, user_usr_id, root_grp_id, user_grp_id, root_id, user_id, inserted_documents) --> tuple(bson.objectid.ObjectId class, bson.objectid.ObjectId class, bson.objectid.ObjectId class, bson.objectid.ObjectId class, bson.objectid.ObjectId class, bson.objectid.ObjectId class, list), MongoDB object id for the user root, MongoDB object id for the user user, MongoDB object id for the group root, MongoDB object id for the group user, MongoDB object id for the directory / (root), MongoDB object id for the directory /user, the list of the documents to insert and the collections in which they must be inserted
    """
    #for master namenode
    inserted_documents = []
    #this operation can be performed only by the root
    if required_by != 'root':
        logging.warning(
            'Operation not allowed: who requires this operation MUST be root')
        raise RootNecessaryException(
        )  #if the user it's not root raise the exception
    #get the MongoDB metadata collections
    fs = get_fs(client)
    users = get_users(client)
    groups = get_groups(client)
    trash = get_trash(client)
    #clean all the MongoDB metadata collections
    res1 = fs.delete_many({})
    res2 = users.delete_many({})
    res3 = groups.delete_many({})
    res4 = trash.delete_many({})
    logging.info('Metadata DB cleaned')
    #create the root user
    root_usr = create_user_node('root', 'root1.', ['root'])
    #create the default user "user", a user which has not the root privileges
    user_usr = create_user_node('user', 'user1.', ['user'])
    #create the respective "root" and "user" groups
    root_grp = create_group_node('root', ['root'])
    user_grp = create_group_node('user', ['user'])
    #insert into MongoDB the new users/groups just created
    #and take note of this for the other slave namenodes for aligning them
    root_usr_id = users.insert_one(root_usr).inserted_id
    root_usr = users.find_one({'_id': root_usr_id})
    inserted_documents.append((root_usr, 'users'))
    user_usr_id = users.insert_one(user_usr).inserted_id
    user_usr = users.find_one({'_id': user_usr_id})
    inserted_documents.append((user_usr, 'users'))
    root_grp_id = groups.insert_one(root_grp).inserted_id
    root_grp = groups.find_one({'_id': root_grp_id})
    inserted_documents.append((root_grp, 'groups'))
    user_grp_id = groups.insert_one(user_grp).inserted_id
    user_grp = groups.find_one({'_id': user_grp_id})
    inserted_documents.append((user_grp, 'groups'))
    #create the root directory, which has not any parent (of course, let's say!)
    root = create_directory_node('/', None, 'root', 'root')
    root['directories'].append('user')
    root_id = fs.insert_one(root).inserted_id
    root = fs.find_one({'_id': root_id})
    inserted_documents.append((root, 'fs'))
    #create the home directory for the default user
    user_dir = create_directory_node('user', root_id, 'user', 'user')
    user_id = fs.insert_one(user_dir).inserted_id
    user = fs.find_one({'_id': user_id})
    inserted_documents.append((user, 'fs'))
    #clean the storage directory of each datanode
    datanodes = get_datanodes_list()
    for dn in datanodes:
        delete('http://{}/mkfs'.format(dn))
    logging.info('DFS initializated')
    return (root_usr_id, user_usr_id, root_grp_id, user_grp_id, root_id,
            user_id, inserted_documents)
Exemple #3
0
def useradd(client, required_by, username, password):
    """Allow to create a group, only if you have root privileges; 
    when a user is created, it will be created also a group with the
    same user's name and it will be added the user to this group.
    
    Parameters
    ----------
    client --> pymoMongoClient class, MongoDB client
    required_by --> str, user who required the operation
    username --> str, the name of the user for which the operation is required
    password --> str, the password for the user
    
    Returns
    -------
    (user_id, grp_id, dir_id, inserted_documents, updatedone_documents) --> tuple(bson.objectid.ObjectId class, bson.objectid.ObjectId class, bson.objectid.ObjectId class, list, list), the MongoDB result after having added the user, the list of the documents to insert and the collections in which they must be inserted, the list of the conditions for updating MongoDB documents, the values which have to be updated and the collection in which perform the update
    """
    #for master namenode
    inserted_documents = []
    updatedone_documents = []
    #only the root can perform this operation
    if required_by != 'root':
        logging.warning('Operation not allowed: you MUST be root')
        raise RootNecessaryException()
    fs = get_fs(client)
    users = get_users(client)
    groups = get_groups(client)
    #verify both if the new user is already present and if a group with the same name of the new group is already present
    if not users.find_one({'name': username}):
        if not groups.find_one({'name': username}):
            #create the user and the main group for the user nodes and insert them into MongoDB
            user = create_user_node(username, password, [username])
            grp = create_group_node(username, [username])
            user_id = users.insert_one(user).inserted_id
            curr_usr = users.find_one({'_id': user_id})
            #insert into the list needed for aligning the other namenodes
            inserted_documents.append((curr_usr, 'users'))
            grp_id = groups.insert_one(grp).inserted_id
            curr_grp = groups.find_one({'_id': grp_id})
            #insert into the list needed for aligning the other namenodes
            inserted_documents.append((curr_grp, 'groups'))
            #create the home directory for the new user
            (directory_id, inserted_documents_mkdir,
             updatedone_documents_mkdir) = mkdir(client,
                                                 Path('/{}'.format(username)),
                                                 required_by, ['root'],
                                                 parent=True)
            #insert into the list needed for aligning the other namenodes
            inserted_documents.extend(inserted_documents_mkdir)
            updatedone_documents.extend(updatedone_documents_mkdir)
            updatedone_documents_chown = chown(client,
                                               Path('/{}'.format(username)),
                                               username, required_by, ['root'])
            updatedone_documents.extend(updatedone_documents_chown)
            updatedone_documents_chgrp = chgrp(client,
                                               Path('/{}'.format(username)),
                                               username, required_by, ['root'])
            updatedone_documents.extend(updatedone_documents_chgrp)
            logging.info('User {} added'.format(username))
            return (user_id, grp_id, directory_id, inserted_documents,
                    updatedone_documents)
        logging.warning('The group already exists')
        raise GroupAlreadyExistsException(
        )  #a group with the same name of the new user is already present
    logging.warning('The user already exists')
    raise UserAlreadyExistsException()  #the user is already present
Exemple #4
0
def usermod(client, required_by, username, grps, operation):
    """Allow to add/delete a user to multiple groups. Operations allowed = + (add) or - (delete).
    
    Parameters
    ----------
    client --> pymoMongoClient class, MongoDB client
    required_by --> str, user who required the operation
    username --> str, the name of the user for which the operation is required
    grps --> list, the groups list for which the operation is required
    operation --> str, add or delete (+ or -)
    
    Returns
    -------
    updatedone_documents --> list, the list of the conditions for updating MongoDB documents, the values which have to be updated and the collection in which perform the update
    """
    #for master namenode
    updatedone_documents = []
    #only the root can perform this operation
    if required_by != 'root':
        logging.warning('Operation not allowed: you MUST be root')
        raise RootNecessaryException
    users = get_users(client)
    groups = get_groups(client)
    usr = users.find_one({'name': username})
    if not usr:
        logging.warning('The user does not exist: {}'.format(username))
        raise UserNotFoundException(username)  #the user does not exist
    for g in grps:

        #check if the groups already exist
        if not groups.find_one({'name': g}):
            logging.warning('The group does not exist: {}'.format(g))
            raise GroupNotFoundException(
                g)  #at least one of the group does not exist
    #it's required to add the user to new groups
    if operation == '+':
        for g in grps:
            #the user already belongs to the current group, so do nothing
            if g in usr['groups']:
                continue
            #add the group to the user's groups
            users.update_one({'_id': usr['_id']}, {'$push': {'groups': g}})
            #insert into the list for aligning the other namenodes
            updatedone_documents.append(({
                '_id': usr['_id']
            }, {
                '$push': {
                    'groups': g
                }
            }, 'users'))
            #add the user to the group users
            groups.update_one({'name': g}, {'$push': {'users': username}})
            #insert into the list for aligning the other namenodes
            updatedone_documents.append(({
                'name': g
            }, {
                '$push': {
                    'users': username
                }
            }, 'groups'))
        #print('User {} added to the following groups: {}'.format(username, grps))
        logging.info('User {} added to the following groups: {}'.format(
            username, grps))
    #it's required to delete the user from groups to which it belongs
    else:
        for g in grps:
            #the user doesn't belong to the current group, so do nothing
            if g not in usr['groups']:
                continue
            #remove the group from the user's groups
            users.update_one({'_id': usr['_id']}, {'$pull': {'groups': g}})
            #insert into the list for aligning the other namenodes
            updatedone_documents.append(({
                '_id': usr['_id']
            }, {
                '$pull': {
                    'groups': g
                }
            }, 'users'))
            #remove the user from the group users
            groups.update_one({'name': g}, {'$pull': {'users': username}})
            #insert into the list for aligning the other namenodes
            updatedone_documents.append(({
                'name': g
            }, {
                '$pull': {
                    'users': username
                }
            }, 'groups'))
        #print('User {} deleted from the following groups: {}'.format(username, grps))
        logging.info('User {} deleted from the following groups: {}'.format(
            username, grps))
    return updatedone_documents
Exemple #5
0
def userdel(client, required_by, username):
    """Allow to delete a user, only if you have root privileges.
    
    Parameters
    ----------
    client --> pymoMongoClient class, MongoDB client
    required_by --> str, user who required the operation
    username --> str, the name of the user for which the operation is required
    
    Returns
    -------
    (deleted, f_deleted, d_updt, updatedone_documents, updatedmany_documents, deletedone_documents, deletemany_documents) --> tuple(pymongo.results.DeleteResult class, pymongo.results.DeleteResult class, pymongo.results.UpdateResult, list, list, list, list), the MongoDB result after having deleted the user, the list of the conditions for updating MongoDB documents, the values which have to be updated and the collection in which perform the update, the list of the conditions for updating MongoDB documents, the values which have to be updated and the collection in which perform the update, the list of the conditions for deleting MongoDB documents and the collection in which perform the delete, the list of the conditions for deleting MongoDB documents and the collection in which perform the delete
    """
    #for master namenode
    updatedone_documents = []
    updatedmany_documents = []
    deletedone_documents = []
    deletemany_documents = []
    #only the root can perform this operation
    if required_by != 'root':
        logging.warning('Operation not allowed: you MUST be root')
        raise RootNecessaryException()
    groups = get_groups(client)
    users = get_users(client)
    fs = get_fs(client)
    #check if the user already exists
    if not users.find_one({'name': username}):
        logging.warning('The user does not exist: {}'.format(username))
        raise UserNotFoundException(username)  #the user does not exist
    usr = users.find_one({'name': username})
    #remove the user from the groups to which it belongs
    for g in usr['groups']:
        groups.update_one({'name': g}, {'$pull': {'users': username}})
        updatedone_documents.append(({
            'name': g
        }, {
            '$pull': {
                'users': username
            }
        }, 'groups'))
    #delete the user
    deleted = users.delete_one({'_id': usr['_id']})
    #insert into the list for aligning the other namenodes
    deletedone_documents.append(({'_id': usr['_id']}, 'users'))
    #delete the file whose owner was the user to delete
    f_deleted = fs.delete_many({'type': 'f', 'own': username})
    #insert into the list for aligning the other namenodes
    deletemany_documents.append(({'type': 'f', 'own': username}, 'fs'))
    #set "root" as the new owner of the directories whose owner was the user to delete
    d_updt = fs.update_many({
        'type': 'd',
        'own': username
    }, {'$set': {
        'own': required_by
    }})
    #insert into the list for aligning the other namenodes
    updatedmany_documents.append(({
        'type': 'd',
        'own': username
    }, {
        '$set': {
            'own': required_by
        }
    }, 'fs'))
    logging.info('User {} deleted'.format(username))
    return (deleted, f_deleted, d_updt, updatedone_documents,
            updatedmany_documents, deletedone_documents, deletemany_documents)
Exemple #6
0
def groupdel(client, required_by, group_name):
    """Allow to delete a group, only if you have root privileges.
    
    Parameters
    ----------
    client --> pymoMongoClient class, MongoDB client
    required_by --> str, user who required the operation
    group_name --> str, the name of the group for which the operation is required
    
    Returns
    -------
    (deleted, res_updt, updatedone_documents, updatedmany_documents, deletedone_documents) --> tuple(pymongo.results.DeleteResult class, pymongo.results.UpdateResult class, list, list, list), the MongoDB result after having deleted the group, the list of the conditions for updating MongoDB documents, the values which have to be updated and the collection in which perform the update, the list of the conditions for updating MongoDB documents, the values which have to be updated and the collection in which perform the update, the list of the conditions for deleting MongoDB documents and the collection in which perform the delete
    """
    #for master namenode
    updatedone_documents = []
    updatedmany_documents = []
    deletedone_documents = []
    #only the root can perform this operation
    if required_by != 'root':
        logging.warning('Operation not allowed: you MUST be root')
        raise RootNecessaryException()
    groups = get_groups(client)
    users = get_users(client)
    fs = get_fs(client)
    #check if the group already exists
    if not groups.find_one({'name': group_name}):
        logging.warning('The group does not exist: {}'.format(group_name))
        raise GroupNotFoundException(group_name)  #the group does not exist
    #check if the group is the main one of a user
    if users.find_one({'name': group_name}):
        logging.warning(
            'The group {} is the main group of a user'.format(group_name))
        raise MainUserGroupException(
            group_name
        )  #the group cannot be deleted because it's the main one of a user
    grp = groups.find_one({'name': group_name})
    for u in grp['users']:
        #delete the group from the list of the groups for each user
        users.update_one({'name': u}, {'$pull': {'groups': group_name}})
        #insert into the list for aligning the other namenodes
        updatedone_documents.append(({
            'name': u
        }, {
            '$pull': {
                'groups': group_name
            }
        }, 'users'))
    #delete the group
    deleted = groups.delete_one({'_id': grp['_id']})
    #insert into the list for aligning the other namenodes
    deletedone_documents.append(({'_id': grp['_id']}, 'groups'))
    #update the documents whose group was the one which has been deleted
    res_updt = fs.update_many({'grp': group_name},
                              {'$set': {
                                  'grp': required_by
                              }})
    #insert into the list for aligning the other namenodes
    updatedmany_documents.append(({
        'grp': group_name
    }, {
        '$set': {
            'grp': required_by
        }
    }, 'fs'))
    logging.info('Group {} deleted'.format(group_name))
    return (deleted, res_updt, updatedone_documents, updatedmany_documents,
            deletedone_documents)