[CORD-1502] [CORD-1516] Authenticating the user that is sending the
request and caching it's orm classes
Change-Id: I4754c1395d085ed0cf313c7fffebbd2d0b8c7fd4
diff --git a/src/grpc_client/main.py b/src/grpc_client/main.py
index 5612d76..b3e29b1 100644
--- a/src/grpc_client/main.py
+++ b/src/grpc_client/main.py
@@ -1,8 +1,9 @@
import functools
from xosapi.xos_grpc_client import SecureClient, InsecureClient
-from twisted.internet import reactor, defer
+from twisted.internet import defer
from resources import RESOURCES
from xosconfig import Config
+from twisted.internet import reactor
LOCAL_CERT = '/Users/teone/Sites/opencord/orchestration/xos-tosca/local_certs.crt'
@@ -12,13 +13,14 @@
self.grpc_secure_endpoint = Config.get('grpc.secure_endpoint')
self.grpc_insecure_endpoint = Config.get('grpc.insecure_endpoint')
- self.username = Config.get('grpc.admin_username')
- self.password = Config.get('grpc.admin_password')
- def setup_resources(self, client):
+ def setup_resources(self, client, key, deferred, recipe):
print "[XOS-TOSCA] Loading resources"
+ if key not in RESOURCES:
+ RESOURCES[key] = {}
for k in client.xos_orm.all_model_names:
- RESOURCES[k] = getattr(client.xos_orm, k)
+ RESOURCES[key][k] = getattr(client.xos_orm, k)
+ reactor.callLater(0, deferred.callback, recipe)
def start(self):
print "[XOS-TOSCA] Connecting to xos-core"
@@ -29,15 +31,23 @@
self.client.stop()
self.client.session_change = True
- if self.username and self.password:
- # NOTE if we authenticate users given the parameters in the rest request, do we need this?
- self.client = SecureClient(endpoint=self.grpc_secure_endpoint, username=self.username, password=self.password, cacert=LOCAL_CERT)
- else:
- self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
+ self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
- self.client.set_reconnect_callback(functools.partial(self.setup_resources, self.client))
+ self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client))
self.client.start()
- # TODO can we call this once the client is setted up?
- reactor.callLater(12, deferred.callback, self.client)
- return deferred
\ No newline at end of file
+ return deferred
+
+ def create_secure_client(self, username, password, recipe):
+ """
+ This method will check if this combination of username/password already has stored orm classes in RESOURCES, otherwise create them
+ """
+ deferred = defer.Deferred()
+ key = "%s~%s" % (username, password)
+ if key in RESOURCES:
+ reactor.callLater(0, deferred.callback, recipe)
+ else:
+ client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=LOCAL_CERT)
+ client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, recipe))
+ client.start()
+ return deferred
diff --git a/src/grpc_client/models_accessor.py b/src/grpc_client/models_accessor.py
index 188a9ce..82934e5 100644
--- a/src/grpc_client/models_accessor.py
+++ b/src/grpc_client/models_accessor.py
@@ -6,7 +6,7 @@
"""
@staticmethod
- def get_model_from_classname(class_name, data):
+ def get_model_from_classname(class_name, data, username, password):
"""
Give a Model Class Name and some data, check if that exits or instantiate a new one
"""
@@ -16,10 +16,13 @@
else:
used_key = data.keys()[0]
- if class_name not in RESOURCES:
+ key = "%s~%s" % (username, password)
+ if not key in RESOURCES:
+ raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username)
+ if class_name not in RESOURCES[key]:
raise Exception('[XOS-TOSCA] The model you are trying to create (%s: %s, class: %s) is not know by xos-core' % (used_key, data[used_key], class_name))
- cls = RESOURCES[class_name]
+ cls = RESOURCES[key][class_name]
models = cls.objects.filter(**{used_key: data[used_key]})
if len(models) == 1: