Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ssl verify #629

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions tests/test_web/test_webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
10 changes: 10 additions & 0 deletions tidy3d/web/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions tidy3d/web/httputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -87,28 +90,28 @@ 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
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
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
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)