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

[Feature] Add SSL Certificate Context And Setting For Async/Sync HTTP Requests #6976

Open
wants to merge 47 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
6634dd0
add SSL certificate context and setting
deeleeramone Nov 29, 2024
ea65641
codespell
deeleeramone Nov 29, 2024
1a0a679
linting
deeleeramone Nov 29, 2024
1d36365
cleanup section
deeleeramone Nov 30, 2024
f6159ab
allow defining uvicorn.run kwargs in system_settings
deeleeramone Dec 1, 2024
44da668
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 1, 2024
a38770a
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 3, 2024
dec94bc
refactor to return session objects
deeleeramone Dec 5, 2024
46362fa
hub service rename it as _request_session
deeleeramone Dec 5, 2024
43b753a
typing
deeleeramone Dec 5, 2024
1754813
more linting
deeleeramone Dec 5, 2024
2297169
pylint
deeleeramone Dec 5, 2024
cc46123
missing __init__ files
deeleeramone Dec 5, 2024
1a9e2fe
fix test_hub_service
deeleeramone Dec 5, 2024
8dad0f9
missing init files..?
deeleeramone Dec 5, 2024
b759d90
shouldn't need an init file there..
deeleeramone Dec 5, 2024
09404c2
now for some random linting
deeleeramone Dec 5, 2024
4645282
maybe need that __init__ after all...?
deeleeramone Dec 5, 2024
1e061d5
fix test_make_request
deeleeramone Dec 5, 2024
dbcd973
relative import in conftest..?
deeleeramone Dec 5, 2024
ef4704a
issue can't be from missing init files..
deeleeramone Dec 5, 2024
dcd81ad
revert the inits
deeleeramone Dec 5, 2024
61ff2f3
small cleanup
deeleeramone Dec 5, 2024
6d87de5
pass the kwargs to the async session object on init for use outside o…
deeleeramone Dec 5, 2024
dfa5c2c
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 5, 2024
e64ec0d
try something...
deeleeramone Dec 5, 2024
de000a5
try something else..
deeleeramone Dec 5, 2024
08c5dac
turn on verbosity for errors..
deeleeramone Dec 5, 2024
ea49615
try something else...
deeleeramone Dec 5, 2024
e62d2f5
undo test change
deeleeramone Dec 5, 2024
714df64
Update lock files
piiq Dec 5, 2024
be3446a
undo another test check thing
deeleeramone Dec 5, 2024
2a0b354
Merge remote-tracking branch 'upstream/feature/ssl-context' into feat…
piiq Dec 5, 2024
cfa1b59
Revert "Update lock files"
deeleeramone Dec 5, 2024
60f6768
try with just the platform lock
deeleeramone Dec 5, 2024
c77af38
unexpected argument
deeleeramone Dec 5, 2024
cb43eed
do a dummy check to return the session object if it is passed in the …
deeleeramone Dec 6, 2024
91e5c64
also dummy check for TCPConnector instance
deeleeramone Dec 6, 2024
c690661
bit more cleanup
deeleeramone Dec 6, 2024
228518a
no implementation of server_hostname
deeleeramone Dec 6, 2024
50d737b
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 7, 2024
808d93b
Move setting session to PosthogHandler init
deeleeramone Dec 7, 2024
4f9c026
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 7, 2024
5a65612
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 11, 2024
1d50c89
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 13, 2024
a8da6dd
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 17, 2024
54c1deb
Merge branch 'develop' into feature/ssl-context
deeleeramone Jan 3, 2025
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
12 changes: 11 additions & 1 deletion openbb_platform/core/openbb_core/api/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ async def lifespan(_: FastAPI):


if __name__ == "__main__":
# pylint: disable=import-outside-toplevel
import uvicorn

uvicorn.run("openbb_core.api.rest_api:app", reload=True)
Env()
uvicorn_kwargs = system.python_settings.model_dump().get("uvicorn", {})
uvicorn_reload = uvicorn_kwargs.pop("reload", None)

if uvicorn_reload is None or uvicorn_reload:
uvicorn_kwargs["reload"] = True

uvicorn_app = uvicorn_kwargs.pop("app", None) or "openbb_core.api.rest_api:app"

uvicorn.run(uvicorn_app, **uvicorn_kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def log_to_dict(self, log_info: str) -> dict:
def send(self, record: logging.LogRecord):
"""Send log record to Posthog."""
# pylint: disable=import-outside-toplevel
import re
import re # noqa
from openbb_core.provider.utils.helpers import get_certificates, restore_certs

level_name = logging.getLevelName(record.levelno)
log_line = FormatterWithExceptions.filter_log_line(text=record.getMessage())
Expand All @@ -116,8 +117,10 @@ def send(self, record: logging.LogRecord):
if event_name not in [e.value for e in self.AllowedEvents]:
return

old_verify = get_certificates()
self.identify()
posthog.capture(self.distinct_id(), event_name, properties=log_extra)
restore_certs(old_verify)

def extract_log_extra(self, record: logging.LogRecord) -> Dict[str, Any]:
"""Extract log extra from record."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from typing import List, Optional

from pydantic import BaseModel, Field, PositiveInt
from pydantic import BaseModel, ConfigDict, Field, PositiveInt


class PythonSettings(BaseModel):
"""Settings model for Python interface configuration."""

model_config = ConfigDict(extra="allow")

docstring_sections: List[str] = Field(
default_factory=lambda: ["description", "parameters", "returns", "examples"],
description="Sections to include in autogenerated docstrings.",
Expand Down
24 changes: 24 additions & 0 deletions openbb_platform/core/openbb_core/app/service/hub_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ def __init__(
base_url: Optional[str] = None,
):
"""Initialize Hub service."""
# pylint: disable=import-outside-toplevel
from openbb_core.provider.utils.helpers import get_certificates, restore_certs

self._base_url = base_url or Env().HUB_BACKEND
self._session = session
self._hub_user_settings: Optional[HubUserSettings] = None
self._get_certificates = get_certificates
self._restore_certs = restore_certs

@property
def base_url(self) -> str:
Expand Down Expand Up @@ -70,10 +75,13 @@ def connect(

def disconnect(self) -> bool:
"""Disconnect from Hub."""
old_verify = self._get_certificates()
if self._session:
result = self._post_logout(self._session)
self._session = None
self._restore_certs(old_verify)
return result
self._restore_certs(old_verify)
raise OpenBBError(
"No session found. Login or provide a 'HubSession' on initialization."
)
Expand Down Expand Up @@ -112,6 +120,8 @@ def _get_session_from_email_password(self, email: str, password: str) -> HubSess
if not password:
raise OpenBBError("Password not found.")

old_verify = self._get_certificates()

response = post(
url=self._base_url + "/login",
json={
Expand All @@ -122,6 +132,8 @@ def _get_session_from_email_password(self, email: str, password: str) -> HubSess
timeout=self.TIMEOUT,
)

self._restore_certs(old_verify)

if response.status_code == 200:
session = response.json()
hub_session = HubSession(
Expand All @@ -143,6 +155,7 @@ def _get_session_from_platform_token(self, token: str) -> HubSession:
raise OpenBBError("Platform personal access token not found.")

self._check_token_expiration(token)
old_verify = self._get_certificates()

response = post(
url=self._base_url + "/sdk/login",
Expand All @@ -152,6 +165,8 @@ def _get_session_from_platform_token(self, token: str) -> HubSession:
timeout=self.TIMEOUT,
)

self._restore_certs(old_verify)

if response.status_code == 200:
session = response.json()
hub_session = HubSession(
Expand All @@ -172,6 +187,7 @@ def _post_logout(self, session: HubSession) -> bool:
access_token = session.access_token.get_secret_value()
token_type = session.token_type
authorization = f"{token_type.title()} {access_token}"
old_verify = self._get_certificates()

response = get(
url=self._base_url + "/logout",
Expand All @@ -180,6 +196,8 @@ def _post_logout(self, session: HubSession) -> bool:
timeout=self.TIMEOUT,
)

self._restore_certs(old_verify)

if response.status_code == 200:
result = response.json()
return result.get("success", False)
Expand All @@ -193,12 +211,15 @@ def _get_user_settings(self, session: HubSession) -> HubUserSettings:
access_token = session.access_token.get_secret_value()
token_type = session.token_type
authorization = f"{token_type.title()} {access_token}"
old_verify = self._get_certificates()

response = get(
url=self._base_url + "/terminal/user",
headers={"Authorization": authorization},
timeout=self.TIMEOUT,
)
self._restore_certs(old_verify)

if response.status_code == 200:
user_settings = response.json()
filtered = {k: v for k, v in user_settings.items() if v is not None}
Expand All @@ -214,6 +235,7 @@ def _put_user_settings(
access_token = session.access_token.get_secret_value()
token_type = session.token_type
authorization = f"{token_type.title()} {access_token}"
old_verify = self._get_certificates()

response = put(
url=self._base_url + "/user",
Expand All @@ -222,6 +244,8 @@ def _put_user_settings(
timeout=self.TIMEOUT,
)

self._restore_certs(old_verify)

if response.status_code == 200:
return True
status_code = response.status_code
Expand Down
Loading
Loading