mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-11 05:17:55 +03:00
* Added xml_marshaller as a requirement
* advanced in REST actor service * Added uuid capability to cryptomanager * Used uuid capability from cryptomanager in downloads manager * Fixed "old class" style forgot on Config :S * State now uses ugettext_lazy instead o ugettext because can be used on some classes and will fail with ugettext prior to translation environment initialization
This commit is contained in:
parent
d085f350e0
commit
bbe2b7d1c2
@ -1 +1,3 @@
|
||||
ujson
|
||||
xml_marshaller
|
||||
six
|
||||
|
@ -32,10 +32,12 @@
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from uds.core.util import Config
|
||||
from uds.core.managers import cryptoManager
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from uds.REST import Handler, AccessDenied
|
||||
from uds.core.util import Config
|
||||
from uds.core.util import log
|
||||
from uds.core.managers import cryptoManager
|
||||
from uds.REST import Handler, AccessDenied, RequestError
|
||||
|
||||
import datetime
|
||||
|
||||
@ -44,9 +46,11 @@ import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
Config.Config.section(Config.SECURITY_SECTION).value('actorKey',
|
||||
cryptoManager().uuid(datetime.datetime.now()).replace('-', ''),
|
||||
type=Config.Config.NUMERIC_FIELD).get()
|
||||
# Actor key, configurable in Security Section of administration interface
|
||||
actorKey = Config.Config.section(Config.SECURITY_SECTION).value('actorKey',
|
||||
cryptoManager().uuid(datetime.datetime.now()).replace('-', ''),
|
||||
type=Config.Config.NUMERIC_FIELD)
|
||||
actorKey.get()
|
||||
|
||||
|
||||
# Enclosed methods under /actor path
|
||||
@ -56,15 +60,54 @@ class Actor(Handler):
|
||||
'''
|
||||
authenticated = False # Actor requests are not authenticated
|
||||
|
||||
def test(self):
|
||||
return {'result': _('Correct'), 'date': datetime.datetime.now()}
|
||||
|
||||
def getClientIdAndMessage(self):
|
||||
|
||||
# Now we will process .../clientIds/message
|
||||
if len(self._args) < 3:
|
||||
raise RequestError('Invalid arguments provided')
|
||||
|
||||
clientIds, message = self._args[1].split(',')[:5], self._args[2]
|
||||
|
||||
return clientIds, message
|
||||
|
||||
def validateRequest(self):
|
||||
# Ensures that key is first parameter
|
||||
# Here, path will be .../actor/KEY/... (probably /rest/actor/KEY/...)
|
||||
if self._args[0] != actorKey.get(True):
|
||||
raise AccessDenied('Invalid actor key')
|
||||
|
||||
def get(self):
|
||||
'''
|
||||
Processes get requests
|
||||
'''
|
||||
logger.debug("Actor args for GET: {0}".format(self._args))
|
||||
return self._args
|
||||
|
||||
self.validateRequest() # Wil raise an access denied exception if not valid
|
||||
|
||||
# if path is .../test (/rest/actor/KEY/test)
|
||||
if self._args[1] == 'test':
|
||||
return self.test()
|
||||
|
||||
clientIds, message = self.getClientIdAndMessage()
|
||||
|
||||
try:
|
||||
data = self._args[3]
|
||||
except Exception:
|
||||
data = ''
|
||||
|
||||
return clientIds, message, data
|
||||
|
||||
def post(self):
|
||||
'''
|
||||
Processes post requests
|
||||
'''
|
||||
return self._params
|
||||
self.validateRequest() # Wil raise an access denied exception if not valid
|
||||
|
||||
clientIds, message = self.getClientIdAndMessage()
|
||||
|
||||
data = self._params[0]
|
||||
|
||||
return clientIds, message, data
|
||||
|
@ -32,12 +32,13 @@
|
||||
'''
|
||||
|
||||
# Make sure that all services are "available" at service startup
|
||||
import services # to make sure that the packages are initialized at this point
|
||||
import auths # To make sure that the packages are initialized at this point
|
||||
import osmanagers # To make sure that packages are initialized at this point
|
||||
import transports # To make sure that packages are initialized at this point
|
||||
import dispatchers
|
||||
import models
|
||||
import plugins
|
||||
import uds.services # to make sure that the packages are initialized at this point
|
||||
import uds.auths # To make sure that the packages are initialized at this point
|
||||
import uds.osmanagers # To make sure that packages are initialized at this point
|
||||
import uds.transports # To make sure that packages are initialized at this point
|
||||
import uds.dispatchers
|
||||
import uds.models
|
||||
import uds.plugins # To make sure plugins are loaded on memory
|
||||
import uds.REST # To make sure REST initializes all what it needs
|
||||
|
||||
import xmlrpc
|
||||
import uds.xmlrpc # To make actor live
|
||||
|
@ -38,6 +38,8 @@ from OpenSSL import crypto
|
||||
from Crypto.Random import atfork
|
||||
import hashlib
|
||||
import array
|
||||
import uuid
|
||||
|
||||
|
||||
import logging
|
||||
import six
|
||||
@ -58,6 +60,7 @@ class CryptoManager(object):
|
||||
|
||||
def __init__(self):
|
||||
self._rsa = RSA.importKey(settings.RSA_KEY)
|
||||
self._namespace = uuid.UUID('627a37a5-e8db-431a-b783-73f7d20b4934')
|
||||
|
||||
@staticmethod
|
||||
def manager():
|
||||
@ -119,3 +122,15 @@ class CryptoManager(object):
|
||||
return ''
|
||||
|
||||
return six.text_type(hashlib.sha1(value).hexdigest())
|
||||
|
||||
def uuid(self, obj):
|
||||
'''
|
||||
Generates an uuid from obj.
|
||||
Right now, obj must be an string
|
||||
'''
|
||||
|
||||
if isinstance(obj, six.text_type):
|
||||
obj = obj.decode('utf-8')
|
||||
else:
|
||||
obj = six. binary_type(obj)
|
||||
return six.text_type(uuid.uuid5(self._namespace, six.binary_type(obj)))
|
||||
|
@ -37,6 +37,8 @@ import uuid
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.core.servers.basehttp import FileWrapper
|
||||
|
||||
from uds.core.managers import cryptoManager
|
||||
|
||||
import six
|
||||
|
||||
import logging
|
||||
@ -59,11 +61,10 @@ class DownloadsManager(object):
|
||||
|
||||
def __init__(self):
|
||||
self._downloadables = {}
|
||||
self._namespace = uuid.UUID('627a37a5-e8db-431a-b783-73f7d20b4934')
|
||||
|
||||
@staticmethod
|
||||
def manager():
|
||||
if DownloadsManager._manager == None:
|
||||
if DownloadsManager._manager is None:
|
||||
DownloadsManager._manager = DownloadsManager()
|
||||
return DownloadsManager._manager
|
||||
|
||||
@ -74,7 +75,7 @@ class DownloadsManager(object):
|
||||
@param path: path to file
|
||||
@params zip: If download as zip
|
||||
'''
|
||||
_id = six.text_type(uuid.uuid5(self._namespace, six.binary_type(name)))
|
||||
_id = cryptoManager().uuid(name)
|
||||
self._downloadables[_id] = {'name': name, 'comment': comment, 'path': path, 'mime': mime}
|
||||
|
||||
def getDownloadables(self):
|
||||
|
@ -43,6 +43,7 @@ GLOBAL_SECTION = 'UDS'
|
||||
SECURITY_SECTION = 'Security'
|
||||
CLUSTER_SECTION = 'Cluster'
|
||||
|
||||
|
||||
class Config(object):
|
||||
'''
|
||||
Keeps persistence configuration data
|
||||
@ -71,7 +72,7 @@ class Config(object):
|
||||
try:
|
||||
if force or self._data is None:
|
||||
# logger.debug('Accessing db config {0}.{1}'.format(self._section.name(), self._key))
|
||||
readed = dbConfig.objects.filter(section=self._section.name(), key=self._key)[0]
|
||||
readed = dbConfig.objects.filter(section=self._section.name(), key=self._key)[0] # @UndefinedVariable
|
||||
self._data = readed.value
|
||||
self._crypt = [self._crypt, True][readed.crypt] # True has "higher" precedende than False
|
||||
self._longText = readed.long
|
||||
@ -130,16 +131,16 @@ class Config(object):
|
||||
'''
|
||||
logger.debug('Saving config {0}.{1} as {2}'.format(self._section.name(), self._key, value))
|
||||
try:
|
||||
if dbConfig.objects.filter(section=self._section.name(), key=self._key).update(value=value, crypt=self._crypt, long=self._longText, field_type=self._type) == 0:
|
||||
if dbConfig.objects.filter(section=self._section.name(), key=self._key).update(value=value, crypt=self._crypt, long=self._longText, field_type=self._type) == 0: # @UndefinedVariable
|
||||
raise Exception() # Do not exists, create a new one
|
||||
except Exception:
|
||||
try:
|
||||
dbConfig.objects.create(section=self._section.name(), key=self._key, value=value, crypt=self._crypt, long=self._longText, field_type=self._type)
|
||||
dbConfig.objects.create(section=self._section.name(), key=self._key, value=value, crypt=self._crypt, long=self._longText, field_type=self._type) # @UndefinedVariable
|
||||
except Exception:
|
||||
# Probably a migration issue, just ignore it
|
||||
logger.info("Could not save configuration key {0}.{1}".format(self._section.name(), self._key))
|
||||
|
||||
class _Section:
|
||||
class _Section(object):
|
||||
def __init__(self, sectionName):
|
||||
self._sectionName = sectionName
|
||||
|
||||
@ -161,7 +162,7 @@ class Config(object):
|
||||
|
||||
@staticmethod
|
||||
def enumerate():
|
||||
for cfg in dbConfig.objects.all().order_by('key'):
|
||||
for cfg in dbConfig.objects.all().order_by('key'): # @UndefinedVariable
|
||||
logger.debug('{0}.{1}:{2},{3}'.format(cfg.section, cfg.key, cfg.value, cfg.field_type))
|
||||
if cfg.crypt is True:
|
||||
val = Config.section(cfg.section).valueCrypt(cfg.key)
|
||||
@ -173,7 +174,7 @@ class Config(object):
|
||||
def update(section, key, value):
|
||||
# If cfg value does not exists, simply ignore request
|
||||
try:
|
||||
cfg = dbConfig.objects.filter(section=section, key=key)[0]
|
||||
cfg = dbConfig.objects.filter(section=section, key=key)[0] # @UndefinedVariable
|
||||
if cfg.crypt is True:
|
||||
value = CryptoManager.manager().encrypt(value)
|
||||
cfg.value = value
|
||||
|
@ -32,7 +32,7 @@
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.translation import ugettext_noop as _, ugettext
|
||||
from django.utils.translation import ugettext_noop as _, ugettext_lazy
|
||||
|
||||
|
||||
# States for different objects. Not all objects supports all States
|
||||
@ -156,5 +156,5 @@ class State(object):
|
||||
'''
|
||||
res = {}
|
||||
for k, v in State.string.iteritems():
|
||||
res[k] = ugettext(v)
|
||||
res[k] = ugettext_lazy(v)
|
||||
return res
|
||||
|
Loading…
Reference in New Issue
Block a user