From a140d07ca0a1825cbf022878618d70e5b1d66076 Mon Sep 17 00:00:00 2001 From: Kidy Lee Date: Tue, 27 Dec 2022 12:00:02 +0800 Subject: [PATCH] add ssl verify --- tests/test_web/test_webapi.py | 14 ++++++++------ tidy3d/web/config.py | 10 ++++++++++ tidy3d/web/httputils.py | 13 ++++++++----- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/tests/test_web/test_webapi.py b/tests/test_web/test_webapi.py index cb92fe014..a102a7fe5 100644 --- a/tests/test_web/test_webapi.py +++ b/tests/test_web/test_webapi.py @@ -3,6 +3,8 @@ import requests import datetime +from requests import Session + import tidy3d as td import tidy3d.web as web from tidy3d.web import httputils, s3utils, webapi @@ -113,18 +115,18 @@ def get_response(url: str) -> str: method = url.split(preamble)[-1] return RESPONSE_MAP[method] - def mock_get(url, **kwargs): - return get_response(url) + class MockRequests: + def get(self, url, **kwargs): + return get_response(url) - def mock_post(url, **kwargs): - return get_response(url) + def post(self, url, **kwargs): + return get_response(url) monkeypatch.setattr( httputils, "get_headers", lambda: {"Authorization": None, "Application": "TIDY3D"} ) monkeypatch.setattr(webapi, "upload_string", lambda a, b, c: None) - monkeypatch.setattr(requests, "get", mock_get) - monkeypatch.setattr(requests, "post", mock_post) + monkeypatch.setattr(httputils, "session", MockRequests()) def make_sim(): diff --git a/tidy3d/web/config.py b/tidy3d/web/config.py index e1a1278b7..b9d51cf34 100644 --- a/tidy3d/web/config.py +++ b/tidy3d/web/config.py @@ -3,6 +3,15 @@ from typing import Any, Dict import pydantic as pd +from pydantic import Field + + +class EnvSettings(pd.BaseSettings): + """ + Settings for reading environment variables + """ + + ssl_verify: bool = Field(True, env="TIDY3D_SSL_VERIFY") class WebConfig(pd.BaseModel): # pylint:disable=too-many-instance-attributes @@ -18,6 +27,7 @@ class WebConfig(pd.BaseModel): # pylint:disable=too-many-instance-attributes auth: str = None user: Dict[str, str] = None auth_retry: int = 1 + env_settings: EnvSettings = EnvSettings() # development config diff --git a/tidy3d/web/httputils.py b/tidy3d/web/httputils.py index 82d2ccb81..ad2dd2b1a 100644 --- a/tidy3d/web/httputils.py +++ b/tidy3d/web/httputils.py @@ -5,12 +5,15 @@ from enum import Enum import jwt -import requests +from requests import Session from .auth import get_credentials, MAX_ATTEMPTS from .config import DEFAULT_CONFIG as Config from ..log import WebError +session = Session() +session.verify = Config.env_settings.ssl_verify + class ResponseCodes(Enum): """HTTP response codes to handle individually.""" @@ -87,7 +90,7 @@ def post(method, data=None): """Uploads the file.""" query_url = get_query_url(method) headers = get_headers() - return requests.post(query_url, headers=headers, json=data) + return session.post(query_url, headers=headers, json=data) @handle_response @@ -95,7 +98,7 @@ def put(method, data): """Runs the file.""" query_url = get_query_url(method) headers = get_headers() - return requests.put(query_url, headers=headers, json=data) + return session.put(query_url, headers=headers, json=data) @handle_response @@ -103,7 +106,7 @@ def get(method): """Downloads the file.""" query_url = get_query_url(method) headers = get_headers() - return requests.get(query_url, headers=headers) + return session.get(query_url, headers=headers) @handle_response @@ -111,4 +114,4 @@ def delete(method): """Deletes the file.""" query_url = get_query_url(method) headers = get_headers() - return requests.delete(query_url, headers=headers) + return session.delete(query_url, headers=headers)