398 lines
14 KiB
Python
398 lines
14 KiB
Python
'''
|
|
VERIDIC Project - Towards a centralized access control system
|
|
|
|
Copyright (C) 2011 Mikael Ates
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
'''
|
|
|
|
|
|
import datetime
|
|
import logging
|
|
|
|
from cPickle import loads, dumps
|
|
|
|
from django.utils.translation import ugettext as _
|
|
from django.db import models
|
|
from django.contrib.auth.models import User
|
|
|
|
from attribute_aggregator.signals import any_attributes_call, \
|
|
listed_attributes_call
|
|
from attribute_aggregator.mapping import ATTRIBUTE_MAPPING
|
|
from attribute_aggregator.core import convert_from_string, \
|
|
get_def_name_from_name_and_ns_of_attribute, iso8601_to_datetime
|
|
|
|
|
|
logger = logging.getLogger('attribute_aggregator')
|
|
|
|
|
|
class AttributeSource(models.Model):
|
|
name = models.CharField(max_length = 200, unique=True)
|
|
|
|
def __unicode__(self):
|
|
return self.name
|
|
|
|
def get_source_instance(self):
|
|
try:
|
|
return self.ldapsource
|
|
except:
|
|
pass
|
|
return None
|
|
|
|
|
|
def get_source_from_name(name):
|
|
try:
|
|
return AttributeSource.objects.get(name=name)
|
|
except:
|
|
return None
|
|
|
|
|
|
class LdapSource(AttributeSource):
|
|
server = models.CharField(max_length=200, unique=True)
|
|
user = models.CharField(max_length=200, blank=True, null=True)
|
|
password = models.CharField(max_length=200, blank=True, null=True)
|
|
base = models.CharField(max_length=200)
|
|
port = models.IntegerField(default=389)
|
|
ldaps = models.BooleanField(default=False)
|
|
certificate = models.TextField(blank=True)
|
|
is_auth_backend = models.BooleanField(default=False)
|
|
|
|
|
|
class UserAliasInSource(models.Model):
|
|
name = models.CharField(max_length = 200, unique=True)
|
|
source = models.ForeignKey(AttributeSource,
|
|
verbose_name = _('Attribute Source'))
|
|
user = models.ForeignKey(User, related_name='user_alias_in_source')
|
|
|
|
|
|
class AttributeData:
|
|
|
|
def __init__(self, definition, values=None, source=None,
|
|
expiration_date=None):
|
|
self.definition = definition
|
|
self.values = list()
|
|
for value in values:
|
|
self.values.append(value)
|
|
if isinstance(source, AttributeSource):
|
|
self.source_id = source.id
|
|
else:
|
|
self.source_id = -1
|
|
'''ISO8601'''
|
|
try:
|
|
iso8601_to_datetime(expiration_date)
|
|
self.expiration_date = expiration_date
|
|
except:
|
|
self.expiration_date = None
|
|
|
|
def get_definition(self):
|
|
return self.definition
|
|
|
|
def get_full_definition(self):
|
|
if not self.definition in ATTRIBUTE_MAPPING:
|
|
return None
|
|
return ATTRIBUTE_MAPPING[self.definition]
|
|
|
|
def get_exptime_in_iso8601(self):
|
|
return self.expiration_date
|
|
|
|
def get_exptime_in_datetime(self):
|
|
return iso8601_to_datetime(self.expiration_date)
|
|
|
|
def set_exptime_in_iso8601(self, expiration_date):
|
|
try:
|
|
iso8601_to_datetime(expiration_date)
|
|
self.expiration_date = expiration_date
|
|
except:
|
|
self.expiration_date = None
|
|
self.save()
|
|
return self.expiration_date
|
|
|
|
def set_exptime_in_datetime(self, expiration_date):
|
|
try:
|
|
self.expiration_date = expiration_date.isoformat()
|
|
except:
|
|
self.expiration_date = None
|
|
self.save()
|
|
return self.expiration_date
|
|
|
|
def get_values(self):
|
|
return self.values
|
|
|
|
def get_source(self):
|
|
try:
|
|
return AttributeSource.objects.get(pk=self.source_id)
|
|
except:
|
|
return None
|
|
|
|
def get_source_id(self):
|
|
return self.source_id
|
|
|
|
def add_value(self, value):
|
|
try:
|
|
self.values.append(value)
|
|
self.save()
|
|
return 0
|
|
except:
|
|
return -1
|
|
|
|
def remove_value(self, value):
|
|
try:
|
|
self.values.remove(value)
|
|
self.save()
|
|
return 0
|
|
except:
|
|
return -1
|
|
|
|
def does_expire(self):
|
|
if self.expiration_date:
|
|
return self.expiration_date
|
|
else:
|
|
return 0
|
|
|
|
def __unicode__(self):
|
|
s = "%s with values %s" % (self.get_definition(),
|
|
[v for v in self.get_values()])
|
|
source = self.get_source()
|
|
if source:
|
|
s += " from %s" % str(source)
|
|
if self.does_expire():
|
|
s += " (Expires on %s)" % self.does_expire()
|
|
return s
|
|
|
|
|
|
class UserAttributeProfile(models.Model):
|
|
user = models.OneToOneField(User, null=True, blank=True,
|
|
related_name='user_attribute_profile')
|
|
data = models.TextField(null=True, blank=True)
|
|
|
|
def add_data(self, data):
|
|
if not isinstance(data, AttributeData):
|
|
return -1
|
|
try:
|
|
l = None
|
|
if not self.data:
|
|
l = list()
|
|
else:
|
|
l = loads(str(self.data))
|
|
l.append(data)
|
|
self.data = dumps(l)
|
|
except:
|
|
return -1
|
|
return 0
|
|
|
|
def remove_data(self, position):
|
|
try:
|
|
l = loads(str(self.data))
|
|
res = l.pop(position)
|
|
self.data = dumps(l)
|
|
return res
|
|
except:
|
|
return None
|
|
|
|
def get_all_data(self):
|
|
try:
|
|
l = loads(str(self.data))
|
|
return l
|
|
except:
|
|
return None
|
|
|
|
def get_data_of_definition(self, definition, in_list=None):
|
|
l = None
|
|
if in_list:
|
|
l = in_list
|
|
else:
|
|
l = self.get_all_data()
|
|
if not l:
|
|
return None
|
|
return [d for d in l if d.get_definition() == definition]
|
|
|
|
def get_freshest_data_of_definition(self, definition):
|
|
l = self.get_data_of_definition(definition)
|
|
if not l:
|
|
return None
|
|
l.sort(key=lambda x: x.expiration_date, reverse=True)
|
|
return l[0]
|
|
|
|
def get_data_of_source(self, source, in_list=None):
|
|
l = None
|
|
if in_list:
|
|
l = in_list
|
|
else:
|
|
l = self.get_all_data()
|
|
if not l or not isinstance(source, AttributeSource):
|
|
return None
|
|
return [d for d in l if d.get_source_id() == source.id]
|
|
|
|
def get_data_of_source_by_name(self, source):
|
|
l = self.get_all_data()
|
|
if not l:
|
|
return None
|
|
s = None
|
|
try:
|
|
s = AttributeSource.objects.get(name=source)
|
|
except:
|
|
return None
|
|
return [d for d in l if d.get_source_id() == s.id]
|
|
|
|
def get_data_of_definition_and_source(self, definition, source):
|
|
return self.get_data_of_definition(definition,
|
|
in_list=self.get_data_of_source(source))
|
|
|
|
def get_data_of_definition_and_source_by_name(self, definition, source):
|
|
return self.get_data_of_definition(definition,
|
|
in_list=self.get_data_of_source_by_name(source))
|
|
|
|
def load_by_dic(self, dictionnary):
|
|
'''
|
|
Dictionnary format:
|
|
attributes = dict()
|
|
data_from_source = list()
|
|
a1 = dict()
|
|
a1['definition'] = definition_name
|
|
a1['name'] = attribute_name_in_ns
|
|
a1['namespace'] = ns_name
|
|
a1['values'] = list_of_values
|
|
data_from_source.append(a1)
|
|
...
|
|
data_from_source.append(a2)
|
|
attributes[source_name] = data_from_source
|
|
|
|
First attempt on 'definition' key.
|
|
Else, definition is searched by 'name' and 'namespece' keys.
|
|
'''
|
|
if not dictionnary:
|
|
logger.error('load_by_dic: \
|
|
Missing profile or dictionnary')
|
|
return -1
|
|
for source_name in dictionnary:
|
|
logger.debug('load_by_dic: loading from source with name: %s' \
|
|
% source_name)
|
|
source = get_source_from_name(source_name)
|
|
if source:
|
|
logger.debug('load_by_dic: attributes: %s' \
|
|
% str(dictionnary[source_name]))
|
|
for attribute in dictionnary[source_name]:
|
|
if (not ('definition' in attribute \
|
|
and attribute['definition'] \
|
|
in ATTRIBUTE_MAPPING) \
|
|
and not('name' in attribute \
|
|
and 'namespace' in attribute)) \
|
|
or not 'values' in attribute:
|
|
logger.warn('load_by_dic: \
|
|
missing data to treat %s' % str(attribute))
|
|
else:
|
|
definition = None
|
|
if 'definition' in attribute \
|
|
and attribute['definition'] \
|
|
in ATTRIBUTE_MAPPING:
|
|
definition = attribute['definition']
|
|
else:
|
|
definition = \
|
|
get_def_name_from_name_and_ns_of_attribute(\
|
|
attribute['name'],
|
|
attribute['namespace'])
|
|
if not definition:
|
|
logger.warn('load_by_dic: \
|
|
unable to find definition for %s' \
|
|
% str(attribute))
|
|
else:
|
|
logger.debug('load_by_dic: \
|
|
definition %s found' % definition)
|
|
|
|
expiration_date = None
|
|
if 'expiration_date' in attribute:
|
|
logger.debug('load_by_dic: expire at %s' \
|
|
% attribute['expiration_date'])
|
|
try:
|
|
iso8601_to_datetime(\
|
|
attribute['expiration_date'])
|
|
expiration_date = \
|
|
attribute['expiration_date']
|
|
logger.debug('load_by_dic: expiration \
|
|
date has the ISO8601 format')
|
|
except:
|
|
logger.warn('load_by_dic: expiration \
|
|
date has not the ISO8601 format')
|
|
if not expiration_date:
|
|
expiration_date = \
|
|
datetime.datetime.now().isoformat()
|
|
|
|
values = [value for value in attribute['values'] \
|
|
if convert_from_string(definition, value)]
|
|
|
|
if self.add_data(AttributeData(\
|
|
definition,
|
|
values=values,
|
|
source=source,
|
|
expiration_date=expiration_date)) == 0:
|
|
logger.debug('load_by_dic: \
|
|
attribute successfully added')
|
|
else:
|
|
logger.warn('load_by_dic: \
|
|
error addind attribute')
|
|
else:
|
|
logger.critical('load_by_dic: \
|
|
The source with name %s providing attributes %s \
|
|
is unknown of the system' \
|
|
% (str(source_name), str(dictionnary[source_name])))
|
|
self.save()
|
|
return 0
|
|
|
|
def load_greedy(self):
|
|
if self.user:
|
|
attributes_provided = any_attributes_call.send(sender=None,
|
|
user=self.user)
|
|
for attrs in attributes_provided:
|
|
logger.info('load_greedy: \
|
|
attributes_call connected to function %s' % \
|
|
attrs[0].__name__)
|
|
logger.info('load_greedy: \
|
|
attributes provided are %s' %str(attrs[1]))
|
|
self.load_by_dic(attrs[1])
|
|
|
|
def load_listed_attributes(self, definitions):
|
|
if self.user:
|
|
attributes_provided = listed_attributes_call.send(sender=None,
|
|
user=self.user, definitions=definitions)
|
|
for attrs in attributes_provided:
|
|
logger.info('load_listed_attributes: \
|
|
attributes_call connected to function %s' % \
|
|
attrs[0].__name__)
|
|
logger.info('load_listed_attributes: \
|
|
attributes provided are %s' %str(attrs[1]))
|
|
self.load_by_dic(attrs[1])
|
|
|
|
def cleanup(self):
|
|
l = self.get_all_data()
|
|
if not l:
|
|
return 0
|
|
now = datetime.datetime.now()
|
|
self.data = dumps([d for d in l if d.expiration_date \
|
|
and d.get_exptime_in_datetime() > now])
|
|
self.save()
|
|
|
|
def __unicode__(self):
|
|
s = None
|
|
if self.user:
|
|
s = "Profile of %s" % self.user
|
|
else:
|
|
s = "Anonymous profile"
|
|
if not self.get_all_data():
|
|
s += " is empty."
|
|
return s
|
|
s += " that contains:"
|
|
for d in self.get_all_data():
|
|
s = s + "\n\t" + d.__unicode__()
|
|
return s
|