From 9e32996fa808a8a232c4dc7b74c06e9f71060662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Thu, 16 Apr 2020 06:55:35 +0200 Subject: [PATCH] Added FIXED & Optional openstack Project ID & region for platforms that do not expose user apis --- server/src/uds/core/managers/crypto.py | 2 +- server/src/uds/core/ui/user_interface.py | 3 +-- .../OpenStack/openStack/UDSOpenStackClient.py | 14 ++++++++++---- server/src/uds/services/OpenStack/provider.py | 8 +++++--- server/src/uds/services/OpenStack/service.py | 12 ++++++++++-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/server/src/uds/core/managers/crypto.py b/server/src/uds/core/managers/crypto.py index 68c75ff9..61125ae7 100644 --- a/server/src/uds/core/managers/crypto.py +++ b/server/src/uds/core/managers/crypto.py @@ -136,7 +136,7 @@ class CryptoManager: logger.exception('Decripting: %s', value) # logger.error(inspect.stack()) return 'decript error' - logger.debug('Decripted: %s %s', data, decrypted) + # logger.debug('Decripted: %s %s', data, decrypted) return decrypted.decode() def AESCrypt(self, text: bytes, key: bytes, base64: bool = False) -> bytes: diff --git a/server/src/uds/core/ui/user_interface.py b/server/src/uds/core/ui/user_interface.py index f88ca3ca..19b3c63c 100644 --- a/server/src/uds/core/ui/user_interface.py +++ b/server/src/uds/core/ui/user_interface.py @@ -942,7 +942,7 @@ class UserInterface(metaclass=UserInterfaceType): if v.isType(gui.InputField.EDITABLE_LIST) or v.isType(gui.InputField.MULTI_CHOICE_TYPE): # logger.debug('Serializing value {0}'.format(v.value)) val = b'\001' + pickle.dumps(v.value, protocol=0) - if v.isType(gui.InfoField.PASSWORD_TYPE): + elif v.isType(gui.InfoField.PASSWORD_TYPE): val = b'\004' + cryptoManager().AESCrypt(v.value.encode('utf8'), UDSB, True) elif v.isType(gui.InputField.NUMERIC_TYPE): val = str(int(v.num())).encode('utf8') @@ -989,7 +989,6 @@ class UserInterface(metaclass=UserInterfaceType): if v and v[0] == 1: val = pickle.loads(v[1:]) elif v and v[0] == 4: - logger.debug('Unpickling crypted') val = cryptoManager().AESDecrypt(v[1:], UDSB, True).decode() else: val = v diff --git a/server/src/uds/services/OpenStack/openStack/UDSOpenStackClient.py b/server/src/uds/services/OpenStack/openStack/UDSOpenStackClient.py index 3a7283d1..63604ab9 100644 --- a/server/src/uds/services/OpenStack/openStack/UDSOpenStackClient.py +++ b/server/src/uds/services/OpenStack/openStack/UDSOpenStackClient.py @@ -558,15 +558,21 @@ class Client: # pylint: disable=too-many-public-methods @authProjectRequired def deleteServer(self, serverId: str) -> None: - r = requests.post( - self._getEndpointFor('compute') + '/servers/{server_id}/action'.format(server_id=serverId), - data='{"forceDelete": null}', + # r = requests.post( + # self._getEndpointFor('compute') + '/servers/{server_id}/action'.format(server_id=serverId), + # data='{"forceDelete": null}', + # headers=self._requestHeaders(), + # verify=VERIFY_SSL, + # timeout=self._timeout + # ) + r = requests.delete( + self._getEndpointFor('compute') + '/servers/{server_id}'.format(server_id=serverId), headers=self._requestHeaders(), verify=VERIFY_SSL, timeout=self._timeout ) - ensureResponseIsValid(r, 'Cannot start server (probably server does not exists).') + ensureResponseIsValid(r, 'Cannot delete server (probably server does not exists).') # This does not returns anything diff --git a/server/src/uds/services/OpenStack/provider.py b/server/src/uds/services/OpenStack/provider.py index fc15b980..e50b8077 100644 --- a/server/src/uds/services/OpenStack/provider.py +++ b/server/src/uds/services/OpenStack/provider.py @@ -107,8 +107,8 @@ class OpenStackProvider(ServiceProvider): timeout = gui.NumericField(length=3, label=_('Timeout'), defvalue='10', minValue=1, maxValue=128, order=99, tooltip=_('Timeout in seconds of connection to OpenStack'), required=True, tab=gui.ADVANCED_TAB) - # tenant = gui.TextField(length=64, label=_('Project'), order=6, tooltip=_('Project (tenant) for this provider'), required=True, defvalue='') - # region = gui.TextField(length=64, label=_('Region'), order=7, tooltip=_('Region for this provider'), required=True, defvalue='RegionOne') + tenant = gui.TextField(length=64, label=_('Project Id'), order=6, tooltip=_('Project (tenant) for this provider. Set only if required by server.'), required=False, defvalue='', tab=gui.ADVANCED_TAB) + region = gui.TextField(length=64, label=_('Region'), order=7, tooltip=_('Region for this provider. Set only if required by server.'), required=False, defvalue='', tab=gui.ADVANCED_TAB) legacy = False @@ -125,6 +125,8 @@ class OpenStackProvider(ServiceProvider): self.timeout.value = validators.validateTimeout(self.timeout.value) def api(self, projectId=None, region=None) -> openStack.Client: + projectId = projectId or self.tenant.value or None + region = region or self.region.value or None if self._api is None: self._api = openStack.Client( self.endpoint.value, @@ -151,7 +153,7 @@ class OpenStackProvider(ServiceProvider): True if all went fine, false if id didn't """ - + logger.debug('Testing connection to OpenStack') try: if self.api().testConnection() is False: raise Exception('Check connection credentials, server, etc.') diff --git a/server/src/uds/services/OpenStack/service.py b/server/src/uds/services/OpenStack/service.py index e15a44cd..e155d27a 100644 --- a/server/src/uds/services/OpenStack/service.py +++ b/server/src/uds/services/OpenStack/service.py @@ -182,10 +182,18 @@ class LiveService(Service): Loads required values inside """ api = self.parent().api() - regions = [gui.choiceItem(r['id'], r['id']) for r in api.listRegions()] + + if not self.parent().legacy and self.parent().region.value: + regions = [gui.choiceItem(self.parent().region.value, self.parent().region.value)] + else: + regions = [gui.choiceItem(r['id'], r['id']) for r in api.listRegions()] + self.region.setValues(regions) - tenants = [gui.choiceItem(t['id'], t['name']) for t in api.listProjects()] + if not self.parent().legacy and self.parent().tenant.value: + tenants = [gui.choiceItem(self.parent().tenant.value, self.parent().tenant.value)] + else: + tenants = [gui.choiceItem(t['id'], t['name']) for t in api.listProjects()] self.project.setValues(tenants) # So we can instantiate parent to get API