Skip to content

Commit f8ba936

Browse files
committed
Make oidc ssl cert verification configurable
Signed-off-by: Andrea Lamparelli <[email protected]>
1 parent d2d6b56 commit f8ba936

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/horreum/configs.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
2-
from typing import Optional
32
from enum import Enum
3+
from typing import Optional
44

55
import httpx
66
from kiota_abstractions.request_option import RequestOption
@@ -27,3 +27,5 @@ class ClientConfiguration:
2727
options: Optional[dict[str, RequestOption]] = None
2828
# which authentication method to use
2929
auth_method: AuthMethod = AuthMethod.BEARER
30+
# SSL cert verification against the oidc provider
31+
auth_verify: bool = True

src/horreum/horreum_client.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import base64
2+
import logging
13
from importlib.metadata import version
24
from typing import Optional
35

4-
import base64
56
import httpx
6-
import logging
77
from kiota_abstractions.authentication import AuthenticationProvider, ApiKeyAuthenticationProvider, KeyLocation
88
from kiota_abstractions.authentication.access_token_provider import AccessTokenProvider
99
from kiota_abstractions.authentication.anonymous_authentication_provider import AnonymousAuthenticationProvider
@@ -21,16 +21,17 @@
2121

2222
logger = logging.getLogger(__name__)
2323

24-
async def setup_auth_provider(base_url: str, username: str, password: str) -> AccessTokenProvider:
24+
25+
async def setup_auth_provider(base_url: str, username: str, password: str, http_client: httpx.AsyncClient = None,
26+
verify: bool = True) -> AccessTokenProvider:
2527
# Use not authenticated client to fetch the auth mechanism
2628
auth_provider = AnonymousAuthenticationProvider()
27-
req_adapter = HttpxRequestAdapter(auth_provider)
29+
req_adapter = HttpxRequestAdapter(authentication_provider=auth_provider, http_client=http_client)
2830
req_adapter.base_url = base_url
2931
auth_client = HorreumRawClient(req_adapter)
3032

3133
auth_config = await auth_client.api.config.keycloak.get()
32-
# TODO: we could generalize using a generic OIDC client
33-
return KeycloakAccessProvider(auth_config, username, password)
34+
return KeycloakAccessProvider(auth_config, username, password, verify)
3435

3536

3637
class HorreumClient:
@@ -49,6 +50,7 @@ def __init__(self, base_url: str, credentials: Optional[HorreumCredentials],
4950
self.__base_url = base_url
5051
self.__credentials = credentials
5152
self.__client_config = client_config
53+
self.__auth_verify = client_config.auth_verify if client_config is not None else True
5254

5355
if client_config and client_config.http_client and client_config.use_default_middlewares:
5456
self.__http_client = KiotaClientFactory.create_with_default_middleware(client=client_config.http_client,
@@ -62,20 +64,25 @@ async def setup(self):
6264
"""
6365

6466
if self.__credentials:
65-
if self.__credentials.apikey is not None and (self.__client_config is None or self.__client_config.auth_method == AuthMethod.API_KEY):
67+
if self.__credentials.apikey is not None and (
68+
self.__client_config is None or self.__client_config.auth_method == AuthMethod.API_KEY):
6669
# API key authentication
67-
self.auth_provider = ApiKeyAuthenticationProvider(KeyLocation.Header, self.__credentials.apikey, "X-Horreum-API-Key")
70+
self.auth_provider = ApiKeyAuthenticationProvider(KeyLocation.Header, self.__credentials.apikey,
71+
"X-Horreum-API-Key")
6872
logger.info('Using API Key authentication')
6973

7074
elif self.__credentials.username is not None:
7175
if self.__client_config is None or self.__client_config.auth_method == AuthMethod.BEARER:
7276
# Bearer token authentication
73-
access_provider = await setup_auth_provider(self.__base_url, self.__credentials.username, self.__credentials.password)
77+
access_provider = await setup_auth_provider(self.__base_url, self.__credentials.username,
78+
self.__credentials.password, self.__http_client,
79+
self.__auth_verify)
7480
self.auth_provider = BaseBearerTokenAuthenticationProvider(access_provider)
7581
logger.info('Using OIDC bearer token authentication')
7682
elif self.__client_config.auth_method == AuthMethod.BASIC:
7783
# Basic authentication
78-
basic = "Basic " + base64.b64encode((self.__credentials.username + ":" + self.__credentials.password).encode()).decode()
84+
basic = "Basic " + base64.b64encode(
85+
(self.__credentials.username + ":" + self.__credentials.password).encode()).decode()
7986
self.auth_provider = ApiKeyAuthenticationProvider(KeyLocation.Header, basic, "Authentication")
8087
logger.info('Using Basic HTTP authentication')
8188
elif self.__credentials.password is not None:

src/horreum/keycloak_access_provider.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ class KeycloakAccessProvider(AccessTokenProvider):
1212
username: str
1313
password: str
1414

15-
def __init__(self, config: KeycloakConfig, username: str, password: str):
15+
def __init__(self, config: KeycloakConfig, username: str, password: str, verify: bool = True):
1616
super()
1717
self.config = config
1818
self.username = username
1919
self.password = password
2020
self.keycloak_openid = KeycloakOpenID(
2121
server_url=config.url,
2222
client_id=config.client_id,
23-
realm_name=config.realm
23+
realm_name=config.realm,
24+
verify=verify
2425
)
2526

2627
async def get_authorization_token(self, uri: str, additional_authentication_context: Dict[str, Any] = {}) -> str:

0 commit comments

Comments
 (0)