1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-18 06:03:54 +03:00

Merge remote-tracking branch 'origin/v3.6'

This commit is contained in:
Adolfo Gómez García 2023-04-07 01:51:53 +02:00
commit db47235354
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
5 changed files with 31 additions and 49 deletions

View File

@ -19,8 +19,12 @@ import requests.adapters
KEY_SIZE = 4096
SECRET_SIZE = 32
# Ensure that we do not get warnings about self signed certificates and so
requests.packages.urllib3.disable_warnings() # type: ignore
try:
# Ensure that we do not get warnings about self signed certificates and so
import requests.packages.urllib3 # type: ignore
requests.packages.urllib3.disable_warnings() # @UndefinedVariable
except:
pass
def selfSignedCert(ip: str) -> typing.Tuple[str, str, str]:

View File

@ -35,7 +35,7 @@ import json
import logging
import typing
import requests
from uds.core.util import security
from . import urls
from . import fake
@ -43,6 +43,7 @@ from . import fake
logger = logging.getLogger(__name__)
if typing.TYPE_CHECKING:
import requests
from uds.core.util.cache import Cache
# Fake part
@ -64,7 +65,7 @@ def ensureConnected(fnc: typing.Callable[..., RT]) -> typing.Callable[..., RT]:
# Result checker
def ensureResponseIsValid(
response: requests.Response, errMsg: typing.Optional[str] = None
response: 'requests.Response', errMsg: typing.Optional[str] = None
) -> typing.Any:
if not response.ok:
if not errMsg:
@ -133,11 +134,10 @@ class OpenGnsysClient:
) -> typing.Any:
if not FAKE:
return ensureResponseIsValid(
requests.post(
security.secureRequestsSession(verify=self.verifyCert).post(
self._ogUrl(path),
data=json.dumps(data),
headers=self.headers,
verify=self.verifyCert,
timeout=TIMEOUT,
),
errMsg=errMsg,
@ -148,10 +148,8 @@ class OpenGnsysClient:
def _get(self, path: str, errMsg: typing.Optional[str] = None) -> typing.Any:
if not FAKE:
return ensureResponseIsValid(
requests.get(
self._ogUrl(path),
headers=self.headers,
verify=self.verifyCert,
security.secureRequestsSession(verify=self.verifyCert).get(
self._ogUrl(path), headers=self.headers, verify=self.verifyCert,
timeout=TIMEOUT,
),
errMsg=errMsg,
@ -162,10 +160,9 @@ class OpenGnsysClient:
def _delete(self, path: str, errMsg: typing.Optional[str] = None) -> typing.Any:
if not FAKE:
return ensureResponseIsValid(
requests.delete(
security.secureRequestsSession(verify=self.verifyCert).delete(
self._ogUrl(path),
headers=self.headers,
verify=self.verifyCert,
timeout=TIMEOUT,
),
errMsg=errMsg,

View File

@ -34,15 +34,15 @@ import logging
import json
import typing
import requests
# import dateutil.parser
from django.utils.translation import gettext as _
from uds.core.util import security
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
pass
import requests
logger = logging.getLogger(__name__)
@ -59,7 +59,7 @@ VERIFY_SSL = False
# Helpers
def ensureResponseIsValid(
response: requests.Response, errMsg: typing.Optional[str] = None
response: 'requests.Response', errMsg: typing.Optional[str] = None
) -> None:
if response.ok is False:
try:
@ -81,7 +81,7 @@ def ensureResponseIsValid(
def getRecurringUrlJson(
url: str,
session: requests.Session,
session: 'requests.Session',
headers: typing.Dict[str, str],
key: str,
params: typing.Optional[typing.Mapping[str, str]] = None,
@ -93,7 +93,7 @@ def getRecurringUrlJson(
counter += 1
logger.debug('Requesting url #%s: %s / %s', counter, url, params)
r = session.get(
url, params=params, headers=headers, verify=VERIFY_SSL, timeout=timeout
url, params=params, headers=headers, timeout=timeout
)
ensureResponseIsValid(r, errMsg)
@ -154,7 +154,7 @@ class Client: # pylint: disable=too-many-public-methods
_project: typing.Optional[str]
_region: typing.Optional[str]
_timeout: int
_session: requests.Session
_session: 'requests.Session'
# Legacyversion is True for versions <= Ocata
def __init__(
@ -171,7 +171,7 @@ class Client: # pylint: disable=too-many-public-methods
access: typing.Optional[str] = None,
proxies: typing.Optional[typing.MutableMapping[str, str]] = None,
):
self._session = requests.Session()
self._session = security.secureRequestsSession(verify=VERIFY_SSL)
if proxies:
self._session.proxies = proxies
@ -260,7 +260,6 @@ class Client: # pylint: disable=too-many-public-methods
self._authUrl + 'v3/auth/tokens',
data=json.dumps(data),
headers={'content-type': 'application/json'},
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -481,7 +480,6 @@ class Client: # pylint: disable=too-many-public-methods
self._getEndpointFor('compute', 'compute_legacy')
+ '/servers/{server_id}'.format(server_id=serverId),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
ensureResponseIsValid(r, 'Get Server information')
@ -493,7 +491,6 @@ class Client: # pylint: disable=too-many-public-methods
self._getEndpointFor(self._volume)
+ '/volumes/{volume_id}'.format(volume_id=volumeId),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -511,7 +508,6 @@ class Client: # pylint: disable=too-many-public-methods
self._getEndpointFor(self._volume)
+ '/snapshots/{snapshot_id}'.format(snapshot_id=snapshotId),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -538,7 +534,6 @@ class Client: # pylint: disable=too-many-public-methods
+ '/snapshots/{snapshot_id}'.format(snapshot_id=snapshotId),
data=json.dumps(data),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -566,7 +561,6 @@ class Client: # pylint: disable=too-many-public-methods
self._getEndpointFor(self._volume) + '/snapshots',
data=json.dumps(data),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -594,7 +588,6 @@ class Client: # pylint: disable=too-many-public-methods
self._getEndpointFor(self._volume) + '/volumes',
data=json.dumps(data),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -644,7 +637,6 @@ class Client: # pylint: disable=too-many-public-methods
self._getEndpointFor('compute', 'compute_legacy') + '/servers',
data=json.dumps(data),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -658,14 +650,12 @@ class Client: # pylint: disable=too-many-public-methods
# self._getEndpointFor('compute', , 'compute_legacy') + '/servers/{server_id}/action'.format(server_id=serverId),
# data='{"forceDelete": null}',
# headers=self._requestHeaders(),
# verify=VERIFY_SSL,
# timeout=self._timeout
# )
r = self._session.delete(
self._getEndpointFor('compute', 'compute_legacy')
+ '/servers/{server_id}'.format(server_id=serverId),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -681,7 +671,6 @@ class Client: # pylint: disable=too-many-public-methods
self._getEndpointFor(self._volume)
+ '/snapshots/{snapshot_id}'.format(snapshot_id=snapshotId),
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -696,7 +685,6 @@ class Client: # pylint: disable=too-many-public-methods
+ '/servers/{server_id}/action'.format(server_id=serverId),
data='{"os-start": null}',
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -711,7 +699,6 @@ class Client: # pylint: disable=too-many-public-methods
+ '/servers/{server_id}/action'.format(server_id=serverId),
data='{"os-stop": null}',
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -724,7 +711,6 @@ class Client: # pylint: disable=too-many-public-methods
+ '/servers/{server_id}/action'.format(server_id=serverId),
data='{"suspend": null}',
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -737,7 +723,6 @@ class Client: # pylint: disable=too-many-public-methods
+ '/servers/{server_id}/action'.format(server_id=serverId),
data='{"resume": null}',
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -750,7 +735,6 @@ class Client: # pylint: disable=too-many-public-methods
+ '/servers/{server_id}/action'.format(server_id=serverId),
data='{"reboot":{"type":"HARD"}}',
headers=self._requestHeaders(),
verify=VERIFY_SSL,
timeout=self._timeout,
)
@ -762,7 +746,7 @@ class Client: # pylint: disable=too-many-public-methods
# We need api version 3.2 or greater
try:
r = self._session.get(
self._authUrl, verify=VERIFY_SSL, headers=self._requestHeaders()
self._authUrl, headers=self._requestHeaders()
)
except Exception:
logger.exception('Testing')

View File

@ -30,11 +30,10 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import requests
import logging
import typing
from uds.core.util import security
from uds.core import services
logger = logging.getLogger(__name__)
@ -79,7 +78,7 @@ class IPServiceBase(services.Service):
if wolurl:
logger.info('Launching WOL: %s', wolurl)
try:
requests.get(wolurl, verify=verify_ssl, timeout=5)
security.secureRequestsSession(verify=verify_ssl).get(wolurl)
# logger.debug('Result: %s', result)
except Exception as e:
logger.error('Error on WOL: %s', e)

View File

@ -41,6 +41,8 @@ import requests
from . import types
from uds.core.util import security
from uds.core.util.decorators import allowCache, ensureConected
# DEFAULT_PORT = 8006
@ -128,7 +130,7 @@ class ProxmoxClient:
}
@staticmethod
def checkError(response: requests.Response) -> typing.Any:
def checkError(response: 'requests.Response') -> typing.Any:
if not response.ok:
errMsg = 'Status code {}'.format(response.status_code)
if response.status_code == 595:
@ -152,11 +154,10 @@ class ProxmoxClient:
def _get(self, path: str) -> typing.Any:
try:
result = requests.get(
result = security.secureRequestsSession(verify=self._validateCert).get(
self._getPath(path),
headers=self.headers,
cookies={'PVEAuthCookie': self._ticket},
verify=self._validateCert,
timeout=self._timeout,
)
@ -174,12 +175,11 @@ class ProxmoxClient:
data: typing.Optional[typing.Iterable[typing.Tuple[str, str]]] = None,
) -> typing.Any:
try:
result = requests.post(
result = security.secureRequestsSession(verify=self._validateCert).post(
self._getPath(path),
data=list(data or []) or None,
headers=self.headers,
cookies={'PVEAuthCookie': self._ticket},
verify=self._validateCert,
timeout=self._timeout,
)
@ -197,12 +197,11 @@ class ProxmoxClient:
data: typing.Optional[typing.Iterable[typing.Tuple[str, str]]] = None,
) -> typing.Any:
try:
result = requests.delete(
result = security.secureRequestsSession(verify=self._validateCert).delete(
self._getPath(path),
data=list(data or []) or None,
headers=self.headers,
cookies={'PVEAuthCookie': self._ticket},
verify=self._validateCert,
timeout=self._timeout,
)
@ -230,11 +229,10 @@ class ProxmoxClient:
return
try:
result = requests.post(
result = security.secureRequestsSession(verify=self._validateCert).post(
url=self._getPath('access/ticket'),
data=self._credentials,
headers=self.headers,
verify=self._validateCert,
timeout=self._timeout,
)
if not result.ok: