Exemple #1
0
def remove_profile(name=None):
    """ Remove profile file from profile name passed by user.

        :param: name: profile's name (same as plateform yaml file) passed by user
        :type: name: list of one str element
        :return: True if remove succeed
        :rtype: bool

        .. seealso:: heimdall.conf.profiles.getProfileObject(), heimdall.core.yml.remove_yml()
    """
    from core.yml import remove_yml
    from core.profile import Profile
    from conf.profiles import getProfileObject
    from core.exceptions import ProfileTypeError
    p = getProfileObject(name[0])
    try:
        if isinstance(p, Profile):
            remove_yml(p.path)
            print "[*] - Profile %s successfully removed" % name
            return True
        else:
            raise ProfileTypeError(
                "Argument passed to %s must be a Profile Object." %
                update_profile.__name__)
    except ProfileTypeError as pte:
        print pte
        exit(pte.code)
Exemple #2
0
def link_rule(profile=None, type=None, id=None):
    """ Link rule to a profile.
        Retrieve Profile Object and update it with new Type Object who contains Rules Objects.

        :param: profile: profile name (same as profile yaml file).
        :param: type: rule's type (same as type yaml file).
        :param: id: rule's id passed by user.
        :type: profile: list of str
        :type: type: list of str
        :type: id: list of int
        :return: list of profile name updated
        :rtype: list of str

        .. seealso:: heimdall.conf.rules.getTypeObject(), heimdall.core.type.Type, heimdall.conf.profiles.getProfileObject()
    """
    from conf.profiles import getProfileObject
    from utils.profile import update_profile
    from conf.rules import getTypeObject, getRuleObject

    for prof in profile:
        p = getProfileObject(prof)
        for ty in p.types:
            if ty.name == type[0]:  # If type exist in profile, we just add all rules or one from this type.
                if id == -1:  # Deleting all rules, and add Type Object with all rules.
                    p.remove_type(ty)
                    p.add_type(getTypeObject(type[0]))
                else:  # add new Rules Objects to Type Object for all id passed by user
                    [ty.add_rule(getRuleObject(ty.name, i)) for i in id]
        else:  # Profile object doesnt have this type
            if id == -1:
                p.add_type(getTypeObject(type[0]))
            else:
                p.add_type(getTypeObject(type[0], id))
        update_profile(p)
    return profile
Exemple #3
0
def get_profile(profiles=None, type=None, id=None):
    """ Take profile information passed by user, and return list of Profile Objects filtered.

        :param: profiles: list of profiles's name (same as type yaml file) passed by user
        :param: type: list of type's name (same as type yaml file) passed by user
        :param: id: list of rule's id (same as type yaml file) passed by user
        :type: profiles: list of str
        :type: type: list of str
        :type: id: list of int
        :return: list of Profile Objects filtered
        :rtype: list of Profile Objects

        ..seealso:: heimdall.conf.profiles.getProfileObject()
    """
    from conf.profiles import getAllProfilesObjects, getProfileObject

    profiles_availables = []
    all_profile = []
    if profiles:
        [all_profile.append(getProfileObject(p)) for p in profiles]
    else:
        all_profile = getAllProfilesObjects()

    for p in all_profile:
        rtype = []
        rulesid = []
        if type:
            [rtype.append(t) for t in p.type if t.name in type]
            if rtype:
                if id:
                    [rulesid.append(r.id) for rt in rtype for r in rt.rules]
                    if set(rulesid).intersection(id):
                        profiles_availables.append(p)
                else:
                    profiles_availables.append(p)
        elif id:
            [rtype.append(t) for t in p.type]
            [rulesid.append(r.id) for rt in rtype for r in rt.rules]
            if set(rulesid).intersection(id):
                profiles_availables.append(p)
        else:
            profiles_availables = all_profile

    return profiles_availables
Exemple #4
0
def update_self_profile(name=None, desc=None, newname=None):
    """ Update a Profile Object from information passed by user, and return the new Profile Object created.
        New object is write in corresponding yaml file.

        :param: name: profile's name (same as profile yaml file) passed by user
        :param: desc: profile's description passed by user
        :param: newname: profile's new name (profile yaml file will be rename) passed by user
        :type: name: list of one str element
        :type: desc: list of one str element
        :type: newname: list of one str element
        :return: updated new Profile Object
        :rtype: Profile Object

        .. seealso:: heimdall.conf.profiles.getProfileObject(), heimdall.core.yml.rename_yml(), update_profile()
    """
    from core.profile import Profile
    from core.yml import rename_yml
    from conf.profiles import getProfileObject
    from core.exceptions import ProfileTypeError

    p = getProfileObject(name[0])
    oldpath = p.path

    if desc:
        p.desc = desc[0]
    if newname:
        p.name = newname[0]
        p.path = p.path.replace(name[0], newname[0])

    try:
        if isinstance(p, Profile):
            rename_yml(oldpath=oldpath, newpath=p.path)
            update_profile(p)
            return True
        else:
            raise ProfileTypeError(
                "Argument passed to %s must be a Profile Object." %
                update_profile.__name__)
    except ProfileTypeError as pte:
        print pte
        exit(pte.code)

    update_profile(p)
    return p
Exemple #5
0
def unlink_rule(profile=None, type=None, id=None):
    """ Unlink rule to a profile.
        Retrieve Profile Object and update it with new Type Object with Rules Objects deleted.

        :param: profile: profile name (same as profile yaml file).
        :param: type: rule's type (same as type yaml file).
        :param: id: rule's id passed by user.
        :type: profile: list of str
        :type: type: list of str
        :type: id: list of int
        :return: list of profile name updated
        :rtype: list of str

        .. seealso:: heimdall.conf.rules.getTypeObject(), heimdall.core.type.Type, heimdall.conf.profiles.getProfileObject()
    """
    from conf.profiles import getProfileObject
    from utils.profile import update_profile
    from core.profile import Profile

    for prof in profile:
        if not isinstance(prof, Profile):
            p = getProfileObject(prof)
        else:
            p = prof
        if id == -1:  # remove all
            [p.remove_type(t) for t in p.types if t.name == type[0]]
        else:
            update_needed = False
            for t in p.types:
                rules_num = len(t.rules)
                if t.name == type[0]:
                    [t.remove_rule(r) for r in t.rules for i in id if str(i) == str(r.id)]
                if rules_num != len(t.rules):
                    update_needed = True

        if update_needed:  # one or more rules has been removed
            update_profile(p)
    return profile