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

fix: bugs creating Certificates from URI #61

Merged
merged 9 commits into from
Nov 6, 2023
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
6 changes: 3 additions & 3 deletions pki_tools/types/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from functools import lru_cache
from typing import List, Type, T

import httpx
from cryptography import x509
from loguru import logger

Expand All @@ -12,6 +11,7 @@
from pki_tools.exceptions import OcspIssuerFetchFailure
from pki_tools.types.certificate import Certificate
from pki_tools.types.crypto_parser import CryptoParser
from pki_tools.utils import HTTPX_CLIENT

CACHE_TIME_SECONDS = 60 * 60 * 24 * 30 # 1 month

Expand Down Expand Up @@ -100,7 +100,7 @@ def from_uri(
@classmethod
@lru_cache(maxsize=None)
def _from_uri(cls: T, uri: str, ttl=None) -> T:
ret = httpx.get(uri)
ret = HTTPX_CLIENT.get(uri)

if ret.status_code != 200:
logger.bind(status=ret.status_code).error(
Expand All @@ -110,7 +110,7 @@ def _from_uri(cls: T, uri: str, ttl=None) -> T:
f"Issuer URI fetch failed. Status: {ret.status_code}"
)

return cls(certificates=x509.load_pem_x509_certificate(ret.content))
return cls.from_pem_string(ret.text)

@property
def pem_string(self):
Expand Down
8 changes: 7 additions & 1 deletion test/test_types_cetificates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os


from conftest import CURRENT_DIR
from pki_tools import Certificates

Expand All @@ -21,3 +20,10 @@ def test_certificates_save_and_read_file(cert_pem_string):
os.remove(file_path)

assert certs.pem_string == new_certs.pem_string


def test_certificates_from_uri(mocked_requests_get, cert_pem_string):
mocked_requests_get.return_value.status_code = 200
mocked_requests_get.return_value.text = cert_pem_string

Certificates.from_uri("http://TEST_URI")