summaryrefslogtreecommitdiffstats
path: root/ipsilon/util/policy.py
blob: 7fdac0c02ecd5832734570fc2d9bebbbe4724266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Copyright (C) 2015  Ipsilon project Contributors, for licensee see COPYING

from ipsilon.util.log import Log
import copy
import sys


class Policy(Log):

    def __init__(self, mappings=None, allowed=None):
        """ A Policy engine to filter attributes.
        Mappings is a list of lists where the first value ia a list itself
        and the second value is an attribute name or a list if the values
        should go in a sub dictionary.
        Note that mappings is a list and not a dictionary as this allows
        to map the same original attribute to different resulting attributes
        if wanted, by simply repeating the 'key list' with different values
        or 'value lists'.

            Example: [[['extras', 'shoes'], 'shoeNumber']]

        A '*' can be used to allow any attribute.

        The default mapping is [[['*'], '*']]
        This copies all attributes without transformation.

        Allowed is a list of allowed attributes.
        Normally mapping should be called before filtering, this means
        allowed attributes should name the mapped attributes.
        Allowed attributes can be multi-element lists

            Example: ['fullname', ['groups', 'domain users']]

        Allowed is '*' by default.
        """

        self.mappings = None
        if mappings:
            if not isinstance(mappings, list):
                raise ValueError("Mappings should be a list not '%s'" %
                                 type(mappings))
            for el in mappings:
                if not isinstance(el, list):
                    raise ValueError("Mappings must be lists, not '%s'" %
                                     type(el))
                if len(el) != 2:
                    raise ValueError("Mappings must contain 2 elements, "
                                     "found %d" % len(el))
                if isinstance(el[0], list) and len(el[0]) > 2:
                    raise ValueError("1st Mapping element can contain at "
                                     "most 2 values, found %d" % len(el[0]))
                if isinstance(el[1], list) and len(el[1]) > 2:
                    raise ValueError("2nd Mapping element can contain at "
                                     "most 2 values, found %d" % len(el[1]))
            self.mappings = mappings
        else:
            # default mapping, return all userdata and groups
            # but ignore extras
            self.mappings = [['*', '*']]

        self.allowed = ['*']
        if allowed:
            if not isinstance(allowed, list):
                raise ValueError("Allowed should be a list not '%s'" %
                                 type(allowed))
            self.allowed = allowed

    def map_attributes(self, attributes, ignore_case=False):

        if not isinstance(attributes, dict):
            raise ValueError("Attributes must be dictionary, not %s" %
                             type(attributes))

        not_mapped = copy.deepcopy(attributes)
        mapped = dict()

        # If ignore_case is True,
        # then PD translates case insensitively prefixes
        PD = dict()
        for k in attributes.keys():
            if ignore_case:
                # note duplicates that differ only by case
                # will be lost here, beware!
                PD[k.lower()] = k
            else:
                PD[k] = k

        for (key, value) in self.mappings:
            if not isinstance(key, list):
                key = [key]
            if len(key) == 2:
                prefix = key[0]
                name = key[1]
            else:
                prefix = None
                name = key[0]

            if not isinstance(value, list):
                value = [value]
            if len(value) == 2:
                mapprefix = value[0]
                mapname = value[1]
            else:
                mapprefix = None
                mapname = value[0]

            if ignore_case:
                if prefix:
                    prefix = prefix.lower()
                name = name.lower()

            if prefix:
                if prefix in PD:
                    attr = attributes[PD[prefix]]
                else:
                    # '*' in a prefix matches nothing
                    continue

                # If ignore_case is True,
                # then ND translates case insensitively names
                ND = dict()
                if isinstance(attr, list):
                    klist = attr
                else:
                    klist = attr.keys()
                for k in klist:
                    if ignore_case:
                        # note duplicates that differ only by case
                        # will be lost here, beware!
                        ND[k.lower()] = k
                    else:
                        ND[k] = k
            else:
                attr = attributes
                ND = PD

            if name in ND and ND[name] in attr:
                if isinstance(attr, list):
                    if mapprefix:
                        if mapprefix not in mapped:
                            mapped[mapprefix] = list()
                        mapped[mapprefix].append(mapname)
                        if not_mapped:
                            if PD[prefix] in not_mapped:
                                while ND[name] in not_mapped[PD[prefix]]:
                                    not_mapped[PD[prefix]].remove(ND[name])
                    else:
                        if mapname not in mapped:
                            mapped[mapname] = list()
                        mapped[mapname].append(attr[ND[name]])
                        if not_mapped:
                            if PD[prefix] in not_mapped:
                                del not_mapped[PD[prefix]]
                else:
                    mapin = copy.deepcopy(attr[ND[name]])
                    if mapname == '*':
                        mapname = ND[name]
                    if mapprefix:
                        if mapprefix not in mapped:
                            mapped[mapprefix] = dict()
                        mapped[mapprefix].update({mapname: mapin})
                    else:
                        mapped.update({mapname: mapin})
                    if not_mapped:
                        if prefix:
                            if PD[prefix] in not_mapped:
                                if ND[name] in not_mapped[PD[prefix]]:
                                    del not_mapped[PD[prefix]][ND[name]]
                        elif ND[name] in not_mapped:
                            del not_mapped[ND[name]]
            elif name == '*':
                mapin = copy.deepcopy(attr)
                # mapname is ignored if name == '*'
                if mapprefix:
                    if mapprefix not in mapped:
                        mapped[mapprefix] = mapin
                    else:
                        mapped[mapprefix].update(mapin)
                else:
                    mapped.update(mapin)
                if not_mapped:
                    if prefix and PD[prefix] in not_mapped:
                        del not_mapped[PD[prefix]]
                    else:
                        not_mapped = None
            else:
                continue

        return mapped, not_mapped

    def filter_attributes(self, attributes, whitelist=True):

        filtered = dict()

        for name in self.allowed:
            if isinstance(name, list):
                key = name[0]
                value = name[1]
                if key in attributes:
                    attr = attributes[key]
                    if value == '*':
                        filtered[key] = attr
                    elif isinstance(attr, dict):
                        if key not in filtered:
                            filtered[key] = dict()
                        if value in attr:
                            filtered[key][value] = attr[value]
                    elif isinstance(attr, list):
                        if key not in filtered:
                            filtered[key] = list()
                        if value in attr:
                            filtered[key].append(value)
                    else:
                        continue
            else:
                if name in attributes:
                    filtered[name] = attributes[name]
                elif name == '*':
                    filtered = attributes

        if whitelist:
            allowed = filtered
        else:
            # filtered contains the blacklisted
            allowed = copy.deepcopy(attributes)
            for lvl1 in filtered:
                attr = filtered[lvl1]
                if isinstance(attr, dict):
                    for lvl2 in attr:
                        del allowed[lvl1][lvl2]
                elif isinstance(attr, list):
                    for lvl2 in attr:
                        allowed[lvl1].remove(lvl2)
                else:
                    allowed[lvl1] = {}
                if len(allowed[lvl1]) == 0:
                    del allowed[lvl1]

        return allowed

# Unit tests
if __name__ == '__main__':

    ret = 0

    # Policy
    t_attributes = {'onenameone': 'onevalueone',
                    'onenametwo': 'onevaluetwo',
                    'two': {'twonameone': 'twovalueone',
                            'twonametwo': 'twovaluetwo'},
                    'three': {'threenameone': 'threevalueone',
                              'threenametwo': 'threevaluetwo'},
                    'four': {'fournameone': 'fourvalueone',
                             'fournametwo': 'fourvaluetwo'},
                    'five': ['one', 'two', 'three'],
                    'six': ['one', 'two', 'three']}

    # test defaults first
    p = Policy()

    print 'Default attribute mapping'
    m, n = p.map_attributes(t_attributes)
    if m == t_attributes and n is None:
        print 'SUCCESS'
    else:
        ret += 1
        print 'FAIL: Expected %s\nObtained %s' % (t_attributes, m)

    print 'Default attribute filtering'
    f = p.filter_attributes(t_attributes)
    if f == t_attributes:
        print 'SUCCESS'
    else:
        ret += 1
        print 'Expected %s\nObtained %s' % (t_attributes, f)

    # test custom mappings and filters
    t_mappings = [[['onenameone'], 'onemappedone'],
                  [['onenametwo'], 'onemappedtwo'],
                  [['two', '*'], '*'],
                  [['three', 'threenameone'], 'threemappedone'],
                  [['three', 'threenameone'], 'threemappedbis'],
                  [['four', '*'], ['four', '*']],
                  [['five'], 'listfive'],
                  [['six', 'one'], ['six', 'mapone']]]

    m_result = {'onemappedone': 'onevalueone',
                'onemappedtwo': 'onevaluetwo',
                'twonameone': 'twovalueone',
                'twonametwo': 'twovaluetwo',
                'threemappedone': 'threevalueone',
                'threemappedbis': 'threevalueone',
                'four': {'fournameone': 'fourvalueone',
                         'fournametwo': 'fourvaluetwo'},
                'listfive': ['one', 'two', 'three'],
                'six': ['mapone']}

    n_result = {'three': {'threenametwo': 'threevaluetwo'},
                'six': ['two', 'three']}

    t_allowed = ['twonameone',
                 ['four', 'fournametwo'],
                 ['listfive', 'three'],
                 ['six', '*']]

    f_result = {'twonameone': 'twovalueone',
                'four': {'fournametwo': 'fourvaluetwo'},
                'listfive': ['three'],
                'six': ['mapone']}

    p = Policy(t_mappings, t_allowed)

    print 'Custom attribute mapping'
    m, n = p.map_attributes(t_attributes)
    if m == m_result and n == n_result:
        print 'SUCCESS'
    else:
        ret += 1
        print 'Expected %s\nObtained %s' % (m_result, m)

    print 'Custom attribute filtering'
    f = p.filter_attributes(m)
    if f == f_result:
        print 'SUCCESS'
    else:
        ret += 1
        print 'Expected %s\nObtained %s' % (f_result, f)

    t2_allowed = ['onemappedone', 'twonametwo', 'threemappedone',
                  ['listfive', 'two']]

    f2_result = {'onemappedtwo': 'onevaluetwo',
                 'twonameone': 'twovalueone',
                 'threemappedbis': 'threevalueone',
                 'four': {'fournameone': 'fourvalueone',
                          'fournametwo': 'fourvaluetwo'},
                 'listfive': ['one', 'three'],
                 'six': ['mapone']}

    p = Policy(t_mappings, t2_allowed)

    print 'Custom attribute filtering 2'
    m, _ = p.map_attributes(t_attributes)
    f = p.filter_attributes(m, whitelist=False)
    if f == f2_result:
        print 'SUCCESS'
    else:
        ret += 1
        print 'Expected %s\nObtained %s' % (f2_result, f)

    # Case Insensitive matching
    tci_attributes = {'oneNameone': 'onevalueone',
                      'onenamEtwo': 'onevaluetwo',
                      'Two': {'twonameone': 'twovalueone',
                              'twonameTwo': 'twovaluetwo'},
                      'thrEE': {'threeNAMEone': 'threevalueone',
                                'thrEEnametwo': 'threevaluetwo'},
                      'foUr': {'fournameone': 'fourvalueone',
                               'fournametwo': 'fourvaluetwo'},
                      'FIVE': ['one', 'two', 'three'],
                      'six': ['ONE', 'two', 'three']}

    tci_mappings = [[['onenameone'], 'onemappedone'],
                    [['onenametwo'], 'onemappedtwo'],
                    [['two', '*'], '*'],
                    [['three', 'threenameone'], 'threemappedone'],
                    [['three', 'threenameone'], 'threemappedbis'],
                    [['four', '*'], ['Four', '*']],
                    [['five'], 'listfive'],
                    [['six', 'one'], ['six', 'mapone']]]

    mci_result = {'onemappedone': 'onevalueone',
                  'onemappedtwo': 'onevaluetwo',
                  'twonameone': 'twovalueone',
                  'twonameTwo': 'twovaluetwo',
                  'threemappedone': 'threevalueone',
                  'threemappedbis': 'threevalueone',
                  'Four': {'fournameone': 'fourvalueone',
                           'fournametwo': 'fourvaluetwo'},
                  'listfive': ['one', 'two', 'three'],
                  'six': ['mapone']}

    nci_result = {'thrEE': {'thrEEnametwo': 'threevaluetwo'},
                  'six': ['two', 'three']}

    p = Policy(tci_mappings)
    print 'Case insensitive attribute mapping'
    m, n = p.map_attributes(tci_attributes, ignore_case=True)
    if m == mci_result and n == nci_result:
        print 'SUCCESS'
    else:
        ret += 1
        print 'FAIL: Expected %s // %s\nObtained %s // %s' % \
            (mci_result, nci_result, m, n)

    sys.exit(ret)