Skip to content

Commit

Permalink
Merge pull request #658 from bcgov/python-version-upgrade-and-remove-…
Browse files Browse the repository at this point in the history
…depreciated-functions

Python version upgrade and remove deprecated functions
  • Loading branch information
esune authored Oct 11, 2024
2 parents dfe31bd + 55ce5a7 commit 660181f
Show file tree
Hide file tree
Showing 34 changed files with 508 additions and 500 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/controller_unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]
python-version: ["3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ This is a sample debugger launch configuration for VSCode that can be used by ad
},
{
"localRoot": "${workspaceFolder}/.venv/Lib/site-packages",
"remoteRoot": "/usr/local/lib/python3.11/site-packages"
"remoteRoot": "/usr/local/lib/python3.12/site-packages"
}
],
"justMyCode": false
Expand Down
2 changes: 1 addition & 1 deletion docker/oidc-controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11 as main
FROM python:3.12 as main

Check warning on line 1 in docker/oidc-controller/Dockerfile

View workflow job for this annotation

GitHub Actions / Build VC-AuthN / Publish VC-AuthN Image

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

WORKDIR /app

Expand Down
7 changes: 2 additions & 5 deletions oidc-controller/api/authSessions/crud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import structlog

from typing import Union
from pymongo import ReturnDocument
from pymongo.database import Database
from fastapi import HTTPException
Expand Down Expand Up @@ -44,17 +43,15 @@ async def get(self, id: str) -> AuthSession:

return AuthSession(**auth_sess)

async def patch(
self, id: Union[str, PyObjectId], data: AuthSessionPatch
) -> AuthSession:
async def patch(self, id: str | PyObjectId, data: AuthSessionPatch) -> AuthSession:
if not PyObjectId.is_valid(id):
raise HTTPException(
status_code=http_status.HTTP_400_BAD_REQUEST, detail=f"Invalid id: {id}"
)
col = self._db.get_collection(COLLECTION_NAMES.AUTH_SESSION)
auth_sess = col.find_one_and_update(
{"_id": PyObjectId(id)},
{"$set": data.dict(exclude_unset=True)},
{"$set": data.model_dump(exclude_unset=True)},
return_document=ReturnDocument.AFTER,
)

Expand Down
3 changes: 1 addition & 2 deletions oidc-controller/api/authSessions/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime, timedelta
from enum import StrEnum, auto
from typing import Optional

from api.core.models import UUIDModel
from pydantic import BaseModel, ConfigDict, Field
Expand All @@ -27,7 +26,7 @@ class AuthSessionBase(BaseModel):
request_parameters: dict
pyop_auth_code: str
response_url: str
presentation_request_msg: Optional[dict] = None
presentation_request_msg: dict | None = None
model_config = ConfigDict(populate_by_name=True)
created_at: datetime = Field(default_factory=datetime.utcnow)

Expand Down
5 changes: 2 additions & 3 deletions oidc-controller/api/clientConfigurations/crud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import structlog

from typing import List
from pymongo import ReturnDocument
from pymongo.database import Database
from fastapi.encoders import jsonable_encoder
Expand Down Expand Up @@ -48,7 +47,7 @@ async def get(self, client_id: str) -> ClientConfiguration:

return ClientConfiguration(**obj)

async def get_all(self) -> List[ClientConfiguration]:
async def get_all(self) -> list[ClientConfiguration]:
col = self._db.get_collection(COLLECTION_NAMES.CLIENT_CONFIGURATIONS)
return [ClientConfiguration(**cc) for cc in col.find()]

Expand All @@ -58,7 +57,7 @@ async def patch(
col = self._db.get_collection(COLLECTION_NAMES.CLIENT_CONFIGURATIONS)
obj = col.find_one_and_update(
{"client_id": client_id},
{"$set": data.dict(exclude_unset=True)},
{"$set": data.model_dump(exclude_unset=True)},
return_document=ReturnDocument.AFTER,
)
check_and_raise_not_found_http_exception(obj, NOT_FOUND_MSG)
Expand Down
17 changes: 8 additions & 9 deletions oidc-controller/api/clientConfigurations/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from enum import Enum
from typing import List, Optional

from pydantic import BaseModel, ConfigDict, Field

Expand All @@ -19,8 +18,8 @@ def list(cls):
class ClientConfigurationBase(BaseModel):
client_id: str = Field(default=settings.OIDC_CLIENT_ID)
client_name: str = Field(default=settings.OIDC_CLIENT_NAME)
response_types: List[str] = Field(default=["code", "id_token", "token"])
redirect_uris: List[str]
response_types: list[str] = Field(default=["code", "id_token", "token"])
redirect_uris: list[str]
token_endpoint_auth_method: TOKENENDPOINTAUTHMETHODS = Field(
default=TOKENENDPOINTAUTHMETHODS.client_secret_basic
)
Expand All @@ -41,11 +40,11 @@ class ClientConfigurationRead(ClientConfigurationBase):


class ClientConfigurationPatch(ClientConfigurationBase):
client_id: Optional[str] = None
client_name: Optional[str] = None
response_types: Optional[List[str]] = None
redirect_uris: Optional[List[str]] = None
token_endpoint_auth_method: Optional[TOKENENDPOINTAUTHMETHODS] = None
client_secret: Optional[str] = None
client_id: str | None = None
client_name: str | None = None
response_types: list[str] | None = None
redirect_uris: list[str] | None = None
token_endpoint_auth_method: TOKENENDPOINTAUTHMETHODS | None = None
client_secret: str | None = None

pass
3 changes: 1 addition & 2 deletions oidc-controller/api/clientConfigurations/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import List
from pymongo.database import Database

from fastapi import APIRouter, Depends
Expand Down Expand Up @@ -36,7 +35,7 @@ async def create_client_config(
@router.get(
"/",
status_code=http_status.HTTP_200_OK,
response_model=List[ClientConfigurationRead],
response_model=list[ClientConfigurationRead],
response_model_exclude_unset=True,
dependencies=[Depends(get_api_key)],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Callable
import structlog

logger = structlog.getLogger(__name__)
logger: structlog.typing.FilteringBoundLogger = structlog.getLogger(__name__)


def test_answer():
Expand All @@ -33,7 +33,7 @@ async def test_client_config_get(db_client: Callable[[], MongoClient]):
crud = ClientConfigurationCRUD(client.db)

client.db.get_collection(COLLECTION_NAMES.CLIENT_CONFIGURATIONS).insert_one(
test_client_config.dict()
test_client_config.model_dump()
)

result = await crud.get(test_client_config.client_id)
Expand All @@ -58,7 +58,7 @@ async def test_client_config_delete(db_client: Callable[[], MongoClient]):
crud = ClientConfigurationCRUD(client.db)

client.db.get_collection(COLLECTION_NAMES.CLIENT_CONFIGURATIONS).insert_one(
test_client_config.dict()
test_client_config.model_dump()
)

result = await crud.delete(test_client_config.client_id)
Expand Down Expand Up @@ -86,7 +86,7 @@ async def test_client_config_patch(db_client: Callable[[], MongoClient], log_out
crud = ClientConfigurationCRUD(client.db)

client.db.get_collection(COLLECTION_NAMES.CLIENT_CONFIGURATIONS).insert_one(
test_client_config.dict()
test_client_config.model_dump()
)

assert log_output.entries == []
Expand Down
13 changes: 6 additions & 7 deletions oidc-controller/api/core/acapy/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from typing import Optional, Union
from uuid import UUID

import requests
Expand All @@ -10,7 +9,7 @@
from .models import CreatePresentationResponse, OobCreateInvitationResponse, WalletDid

_client = None
logger = structlog.getLogger(__name__)
logger: structlog.typing.FilteringBoundLogger = structlog.getLogger(__name__)

WALLET_DID_URI = "/wallet/did"
PUBLIC_WALLET_DID_URI = "/wallet/did/public"
Expand All @@ -23,7 +22,7 @@ class AcapyClient:
acapy_host = settings.ACAPY_ADMIN_URL
service_endpoint = settings.ACAPY_AGENT_URL

wallet_token: Optional[str] = None
wallet_token: str | None = None
agent_config: AgentConfig

def __init__(self):
Expand Down Expand Up @@ -57,12 +56,12 @@ def create_presentation_request(
assert resp_raw.status_code == 200, resp_raw.content

resp = json.loads(resp_raw.content)
result = CreatePresentationResponse.parse_obj(resp)
result = CreatePresentationResponse.model_validate(resp)

logger.debug("<<< create_presenation_request")
return result

def get_presentation_request(self, presentation_exchange_id: Union[UUID, str]):
def get_presentation_request(self, presentation_exchange_id: UUID | str):
logger.debug(">>> get_presentation_request")

resp_raw = requests.get(
Expand Down Expand Up @@ -106,7 +105,7 @@ def get_wallet_did(self, public=False) -> WalletDid:
else:
resp_payload = resp["results"][0]

did = WalletDid.parse_obj(resp_payload)
did = WalletDid.model_validate(resp_payload)

logger.debug(f"<<< get_wallet_did -> {did}")
return did
Expand Down Expand Up @@ -136,7 +135,7 @@ def oob_create_invitation(
assert resp_raw.status_code == 200, resp_raw.content

resp = json.loads(resp_raw.content)
result = OobCreateInvitationResponse.parse_obj(resp)
result = OobCreateInvitationResponse.model_validate(resp)

logger.debug("<<< oob_create_invitation")
return result
10 changes: 5 additions & 5 deletions oidc-controller/api/core/acapy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import json

from functools import cache
from typing import Dict, Protocol
from typing import Protocol

from ..config import settings

logger = structlog.getLogger(__name__)
logger: structlog.typing.FilteringBoundLogger = structlog.getLogger(__name__)


class AgentConfig(Protocol):
def get_headers() -> Dict[str, str]: ...
def get_headers() -> dict[str, str]: ...


class MultiTenantAcapy:
Expand All @@ -33,10 +33,10 @@ def get_wallet_token(self):

return wallet_token

def get_headers(self) -> Dict[str, str]:
def get_headers(self) -> dict[str, str]:
return {"Authorization": "Bearer " + self.get_wallet_token()}


class SingleTenantAcapy:
def get_headers(self) -> Dict[str, str]:
def get_headers(self) -> dict[str, str]:
return {settings.ST_ACAPY_ADMIN_API_KEY_NAME: settings.ST_ACAPY_ADMIN_API_KEY}
5 changes: 2 additions & 3 deletions oidc-controller/api/core/acapy/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Optional, Dict
from ..aries import OutOfBandMessage

from pydantic import BaseModel
Expand All @@ -11,13 +10,13 @@ class WalletDid(BaseModel):


class WalletDidPublicResponse(BaseModel):
result: Optional[WalletDid] = None
result: WalletDid | None = None


class CreatePresentationResponse(BaseModel):
thread_id: str
pres_ex_id: str
pres_request: Dict
pres_request: dict


class OobCreateInvitationResponse(BaseModel):
Expand Down
18 changes: 12 additions & 6 deletions oidc-controller/api/core/acapy/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def test_create_presentation_returns_sucessfully_with_valid_data(requests_
)

with mock.patch.object(
CreatePresentationResponse, "parse_obj", return_value={"result": "success"}
CreatePresentationResponse, "model_validate", return_value={"result": "success"}
):
client = AcapyClient()
client.agent_config.get_headers = mock.MagicMock(return_value={"x-api-key": ""})
Expand All @@ -74,7 +74,7 @@ async def test_create_presentation_throws_assertion_error_with_non_200_resp_from
)

with mock.patch.object(
CreatePresentationResponse, "parse_obj", return_value={"result": "success"}
CreatePresentationResponse, "model_validate", return_value={"result": "success"}
):
client = AcapyClient()
client.agent_config.get_headers = mock.MagicMock(return_value={"x-api-key": ""})
Expand All @@ -97,7 +97,7 @@ async def test_create_presentation_throws_error_with_non_json_from_acapy(request
)

with mock.patch.object(
CreatePresentationResponse, "parse_obj", return_value={"result": "success"}
CreatePresentationResponse, "model_validate", return_value={"result": "success"}
):
client = AcapyClient()
client.agent_config.get_headers = mock.MagicMock(return_value={"x-api-key": ""})
Expand Down Expand Up @@ -154,7 +154,9 @@ async def test_get_wallet_did_public_returns_sucessfully_on_public_url_and_simpl
json={"result": "success"},
status_code=200,
)
with mock.patch.object(WalletDid, "parse_obj", return_value={"result": "success"}):
with mock.patch.object(
WalletDid, "model_validate", return_value={"result": "success"}
):
client = AcapyClient()
client.agent_config.get_headers = mock.MagicMock(return_value={"x-api-key": ""})
wallet_resp = client.get_wallet_did(public=True)
Expand All @@ -171,7 +173,9 @@ async def test_get_wallet_did_public_throws_assertion_error_on_non_200_response(
json={"result": "success"},
status_code=400,
)
with mock.patch.object(WalletDid, "parse_obj", return_value={"result": "success"}):
with mock.patch.object(
WalletDid, "model_validate", return_value={"result": "success"}
):
client = AcapyClient()
client.agent_config.get_headers = mock.MagicMock(return_value={"x-api-key": ""})
try:
Expand All @@ -190,7 +194,9 @@ async def test_get_wallet_did_not_public_returns_on_correct_url_and_processes_ar
json={"results": ["success"]},
status_code=200,
)
with mock.patch.object(WalletDid, "parse_obj", return_value={"result": "success"}):
with mock.patch.object(
WalletDid, "model_validate", return_value={"result": "success"}
):
client = AcapyClient()
client.agent_config.get_headers = mock.MagicMock(return_value={"x-api-key": ""})
wallet_resp = client.get_wallet_did(public=False)
Expand Down
9 changes: 4 additions & 5 deletions oidc-controller/api/core/aries/out_of_band.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Dict, List, Union
from pydantic import BaseModel, ConfigDict, Field
from .service_decorator import OOBServiceDecorator


class OutOfBandPresentProofAttachment(BaseModel):
id: str = Field(alias="@id")
mime_type: str = Field(default="application/json", alias="mime-type")
data: Dict
data: dict

model_config = ConfigDict(populate_by_name=True)

Expand All @@ -22,10 +21,10 @@ class OutOfBandMessage(BaseModel):
label: str = Field(
default="vc-authn Out-of-Band present-proof authorization request"
)
request_attachments: List[OutOfBandPresentProofAttachment] = Field(
request_attachments: list[OutOfBandPresentProofAttachment] = Field(
alias="requests~attach"
)
services: List[Union[OOBServiceDecorator, str]] = Field(alias="services")
handshake_protocols: List[str] = Field(alias="handshake_protocols", default=None)
services: list[OOBServiceDecorator | str] = Field(alias="services")
handshake_protocols: list[str] = Field(alias="handshake_protocols", default=None)

model_config = ConfigDict(populate_by_name=True)
3 changes: 1 addition & 2 deletions oidc-controller/api/core/aries/present_proof_attachment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Dict
from pydantic import BaseModel, Field


class PresentProofv20Attachment(BaseModel):
# https://github.com/hyperledger/aries-rfcs/tree/eace815c3e8598d4a8dd7881d8c731fdb2bcc0aa/features/0454-present-proof-v2
id: str = Field(default="libindy-request-presentation-0", alias="@id")
mime_type: str = Field(default="application/json", alias="mime-type")
data: Dict
data: dict
Loading

0 comments on commit 660181f

Please sign in to comment.