This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ApiGwAuth class for session. Implement APIGW.
- Loading branch information
1 parent
8f81336
commit 4288363
Showing
6 changed files
with
208 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
from typing import TYPE_CHECKING, Literal, Optional | ||
|
||
from pydantic import BaseModel, Field, PositiveInt | ||
from requests import PreparedRequest, post | ||
from requests.auth import AuthBase | ||
from requests.cookies import RequestsCookieJar | ||
|
||
from catalystwan.exceptions import CatalystwanException | ||
from catalystwan.response import ManagerResponse | ||
|
||
if TYPE_CHECKING: | ||
from catalystwan.session import ManagerSession | ||
|
||
LoginMode = Literal["machine", "user", "session"] | ||
|
||
|
||
class ApiGwLogin(BaseModel): | ||
client_id: str | ||
client_secret: str | ||
org_name: str | ||
mode: Optional[LoginMode] = None | ||
username: Optional[str] = None | ||
session: Optional[str] = None | ||
tenant_user: Optional[bool] = None | ||
token_duration: PositiveInt = Field(default=10, description="in minutes") | ||
|
||
|
||
class ApiGwAuth(AuthBase): | ||
def __init__(self, base_url: str, login: ApiGwLogin, verify: bool = False): | ||
self.base_url = base_url | ||
self.verify = verify | ||
self.login = login | ||
self.token = "" | ||
self.set_cookie = RequestsCookieJar() # It is need for compatibility with ManagerSession::login method | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def __call__(self, prepared_request: PreparedRequest) -> PreparedRequest: | ||
if self.token == "": | ||
self.token = self._get_token() | ||
prepared_request.headers.update(self._prepare_header()) | ||
return prepared_request | ||
|
||
def _get_token(self) -> str: | ||
response = post( | ||
url=f"{self.base_url}/apigw/login", | ||
verify=self.verify, | ||
json=self.login.model_dump(exclude_none=True), | ||
timeout=10, | ||
) | ||
token = response.json().get("token", "") | ||
if not token or not isinstance(token, str): | ||
raise CatalystwanException("Failed to get bearer token") | ||
return token | ||
|
||
def _prepare_header(self) -> dict: | ||
return { | ||
"sdwan-org": self.login.org_name, | ||
"Authorization": f"Bearer {self.token}", | ||
} | ||
|
||
def __str__(self): | ||
return f"ApiGatewayAuth(base_url={self.base_url}, mode={self.login.mode})" | ||
|
||
def logout(self, session: "ManagerSession") -> Optional[ManagerResponse]: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from pydantic import BaseModel | ||
|
||
from catalystwan.endpoints import APIEndpoints, delete, post | ||
|
||
|
||
class OnBoardClient(BaseModel): | ||
client_id: str | ||
client_secret: str | ||
client_name: str | ||
|
||
|
||
class ApiGateway(APIEndpoints): | ||
@delete("/certificate/{uuid}") | ||
def delete_configuration(self, uuid: str) -> None: | ||
... | ||
|
||
@post("/apigw/config/reload") | ||
def configuration_reload(self) -> None: | ||
"""After launching the API Gateway, SSP can use the API | ||
and bearer authentication header with provision access token obtained | ||
in the step above. It reloads the configuration from S3 bucket, Secrets Manager | ||
and reset the RDS connection pool.""" | ||
... | ||
|
||
@post("/apigw/client/registration") | ||
def on_board_client(self, payload: OnBoardClient) -> None: | ||
... | ||
|
||
@post("/certificate/vedge/list?action={action}") | ||
def send_to_controllers(self, action: str = "push") -> None: | ||
... | ||
|
||
@post("/certificate/vsmart/list") | ||
def send_to_vbond(self) -> None: | ||
... |
Oops, something went wrong.