From dbc88163dd738d02b194bca3a564a20e1b0372a4 Mon Sep 17 00:00:00 2001 From: kakwa Date: Sun, 17 May 2015 12:24:29 +0200 Subject: [PATCH] begin attributes handling manipulation --- conf/attributes.yml | 64 +++++++++++++++++++++++++--------------- ldapcherry/attributes.py | 22 ++++++++++---- ldapcherry/exceptions.py | 5 ++++ misc/debug_attributes.py | 18 +++++++++++ tests/cfg/attributes.yml | 1 + tests/test_Attributes.py | 59 ++++++++++++++++++++++++++++++++++++ 6 files changed, 141 insertions(+), 28 deletions(-) create mode 100644 misc/debug_attributes.py create mode 120000 tests/cfg/attributes.yml create mode 100644 tests/test_Attributes.py diff --git a/conf/attributes.yml b/conf/attributes.yml index 071f680..f28408b 100644 --- a/conf/attributes.yml +++ b/conf/attributes.yml @@ -1,53 +1,70 @@ -last-name: - description: "Last Name of the user" - display_name: "Last Name" +cn: + description: "Firt Name and Display Name" + display_name: "Display Name" + type: string + autofill: + function: cn + args: + - $first-name + - $name backend-attributes: ldap: cn ad: CN - first-name: description: "First name of the user" + display_name: "First Name" + type: string + backend-attributes: + ldap: givenName + ad: givenName +name: + description: "Family name of the user" display_name: "Name" + type: string backend-attributes: ldap: sn ad: sn email: description: "Email of the user" display_name: "Name" + type: email autofill: function: email - args: - - $first-name - - $last-name - - @example.com + args: + - $first-name + - $last-name + - '@example.com' backend-attributes: - ldap: cn - ad: CN + ldap: email + ad: EMAIL uid: description: "UID of the user" display_name: "UID" + type: string autofill: function: uid - args: - - $first-name - - $last-name + args: + - $first-name + - $last-name backend-attributes: ldap: uid ad: UID uidNumber: description: "User ID Number of the user" display_name: "UID Number" + type: int autofill: function: uidNumber - args: - - $first-name - - $last-name + args: + - $first-name + - $last-name backend-attributes: ldap: uidNumber ad: UIDNumber gidNumber: description: "Group ID Number of the user" display_name: "GID Number" + type: int default: 10000 backend-attributes: ldap: gidNumber @@ -55,8 +72,8 @@ gidNumber: shell: description: "Shell of the user" display_name: "Shell" - self: true - type: list + self: True + type: stringlist values: - /bin/bash - /bin/zsh @@ -67,12 +84,13 @@ shell: home: description: "Home user path" display_name: "Home" + type: string autofill: function: home - args: - - $first-name - - $last-name - - /home/ + args: + - $first-name + - $last-name + - /home/ backend-attributes: ldap: home ad: Home @@ -80,7 +98,7 @@ home: password: decription: "Password of the user" display_name: "Password" - self: true + self: True type: password backend-attributes: ldap: userPassword diff --git a/ldapcherry/attributes.py b/ldapcherry/attributes.py index 8c9866c..1047625 100644 --- a/ldapcherry/attributes.py +++ b/ldapcherry/attributes.py @@ -8,15 +8,27 @@ import os import sys -try: - from yaml import CLoader as Loader, CDumper as Dumper -except ImportError: - from yaml import Loader, Dumper +from ldapcherry.pyyamlwrapper import loadNoDump +from ldapcherry.pyyamlwrapper import DumplicatedKey +from ldapcherry.exceptions import MissingAttributesFile +from sets import Set +import yaml + +types = ['string', 'email', 'int', 'stringlist', 'fix', 'password'] class Attributes: def __init__(self, attributes_file): - pass + self.attributes_file = attributes_file + self.backends = Set([]) + try: + stream = open(attributes_file, 'r') + except: + raise MissingAttributesFile(attributes_file) + try: + self.attributes = loadNoDump(stream) + except DumplicatedKey as e: + raise DumplicateAttributesKey(e.key) def get_selfattributes(self): """get the list of groups from roles""" diff --git a/ldapcherry/exceptions.py b/ldapcherry/exceptions.py index 2f47509..124e54d 100644 --- a/ldapcherry/exceptions.py +++ b/ldapcherry/exceptions.py @@ -38,3 +38,8 @@ class MissingRolesFile(Exception): def __init__(self, rolefile): self.rolefile = rolefile self.log = "fail to open role file <%(rolefile)s>" % { 'rolefile' : rolefile} + +class MissingAttributesFile(Exception): + def __init__(self, attributesfile): + self.attributesfile = attributesfile + self.log = "fail to open attributes file <%(attributesfile)s>" % { 'attributesfile' : attributesfile} diff --git a/misc/debug_attributes.py b/misc/debug_attributes.py new file mode 100644 index 0000000..f338111 --- /dev/null +++ b/misc/debug_attributes.py @@ -0,0 +1,18 @@ +from ldapcherry.attributes import Attributes +from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent, MissingRolesFile +from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError +from yaml import load, dump +import yaml + +try: + from yaml import CLoader as Loader, CDumper as Dumper +except ImportError: + from yaml import Loader, Dumper + +class CustomDumper(yaml.SafeDumper): + "A custom YAML dumper that never emits aliases" + + def ignore_aliases(self, _data): + return True + +inv = Attributes('./conf/attributes.yml') diff --git a/tests/cfg/attributes.yml b/tests/cfg/attributes.yml new file mode 120000 index 0000000..2ef0902 --- /dev/null +++ b/tests/cfg/attributes.yml @@ -0,0 +1 @@ +../../conf/attributes.yml \ No newline at end of file diff --git a/tests/test_Attributes.py b/tests/test_Attributes.py new file mode 100644 index 0000000..53739ef --- /dev/null +++ b/tests/test_Attributes.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from __future__ import with_statement +from __future__ import unicode_literals + +import pytest +import sys +from sets import Set +from ldapcherry.attributes import Attributes +from ldapcherry.exceptions import MissingAttributesFile +from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError + +class TestError(object): + + def testNominal(self): + inv = Attributes('./tests/cfg/attributes.yml') + return True + + def testNoFile(self): + try: + inv = Attributes('./tests/cfg/dontexist') + except MissingAttributesFile: + return + else: + raise AssertionError("expected an exception") + +# def testMissingDisplayName(self): +# try: +# inv = Attributes('./tests/cfg/attributes_missing_diplay_name.yml') +# except MissingKey: +# return +# else: +# raise AssertionError("expected an exception") +# +# def testAttrKeyDuplication(self): +# try: +# inv = Attributes('./tests/cfg/attributes_key_dup.yml') +# except DumplicateAttrKey: +# return +# else: +# raise AssertionError("expected an exception") +# + +# def testGetDisplayNameMissingAttr(self): +# inv = Attributes('./tests/cfg/attributes.yml') +# try: +# res = inv.get_display_name('notarole') +# except MissingAttr: +# return +# else: +# raise AssertionError("expected an exception") +# +# def testGetDisplayName(self): +# inv = Attributes('./tests/cfg/attributes.yml') +# res = inv.get_display_name('users') +# expected = 'Simple Users' +# assert res == expected +#