Skip to content

Commit

Permalink
Refactored build system, added dataclass (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladvildanov authored Dec 18, 2024
1 parent 7b9e700 commit 1358d89
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea/
entraid/__pycache__
redis_entraid/__pycache__
tests/__pycache__
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "redispy-entraid-credentials"
version = "0.0.1"
authors = [
{ name="Redis Inc.", email="[email protected]" },
]
description = "Entra ID credentials provider implementation for Redis-py client"
readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"redis @ git+https://github.com/redis/redis-py.git/@vv-tba-support",
"PyJWT~=2.9.0",
"msal~=1.31.0",
]

[tool.setuptools.packages.find]
include = ["redis_entraid"]
excludea = ["tests", ".github"]
File renamed without changes.
84 changes: 15 additions & 69 deletions entraid/cred_provider.py → redis_entraid/cred_provider.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from dataclasses import dataclass
from typing import Union, Tuple, Callable, Any, Awaitable

from redis.credentials import StreamingCredentialProvider
from redis.auth.token_manager import TokenManagerConfig, RetryPolicy, TokenManager, CredentialsListener

from entraid.identity_provider import EntraIDIdentityProvider
from redis_entraid.identity_provider import EntraIDIdentityProvider


@dataclass
class TokenAuthConfig:
"""
Configuration for token authentication.
Expand All @@ -19,82 +21,26 @@ class TokenAuthConfig:
DEFAULT_MAX_ATTEMPTS = 3
DEFAULT_DELAY_IN_MS = 3

def __init__(self, idp: EntraIDIdentityProvider):
self._expiration_refresh_ratio = self.DEFAULT_EXPIRATION_REFRESH_RATIO
self._lower_refresh_bound_millis = self.DEFAULT_LOWER_REFRESH_BOUND_MILLIS
self._token_request_execution_timeout_in_ms = self.DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS
self._max_attempts = self.DEFAULT_MAX_ATTEMPTS
self._delay_in_ms = self.DEFAULT_DELAY_IN_MS
self._idp = idp
idp: EntraIDIdentityProvider
expiration_refresh_ratio: float = DEFAULT_EXPIRATION_REFRESH_RATIO
lower_refresh_bound_millis: int = DEFAULT_LOWER_REFRESH_BOUND_MILLIS
token_request_execution_timeout_in_ms: int = DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS
max_attempts: int = DEFAULT_MAX_ATTEMPTS
delay_in_ms: int = DEFAULT_DELAY_IN_MS

def get_token_manager_config(self) -> TokenManagerConfig:
return TokenManagerConfig(
self._expiration_refresh_ratio,
self._lower_refresh_bound_millis,
self._token_request_execution_timeout_in_ms,
self.expiration_refresh_ratio,
self.lower_refresh_bound_millis,
self.token_request_execution_timeout_in_ms,
RetryPolicy(
self._max_attempts,
self._delay_in_ms
self.max_attempts,
self.delay_in_ms
)
)

def get_identity_provider(self) -> EntraIDIdentityProvider:
return self._idp

def expiration_refresh_ratio(self, value: float):
"""
Percentage value of total token TTL when refresh should be triggered.
Default: 0.8
:param value: float
:return: Self
"""
self._expiration_refresh_ratio = value
return self

def lower_refresh_bound_millis(self, value: int):
"""
Represents the minimum time in milliseconds before token expiration to trigger a refresh, in milliseconds.
Default: 0
:param value: int
:return: Self
"""
self._lower_refresh_bound_millis = value
return self

def token_request_execution_timeout_in_ms(self, value: int):
"""
Represents the maximum time in milliseconds to wait for a token request to complete.
Default: 100
:param value: int
:return: Self
"""
self._token_request_execution_timeout_in_ms = value
return self

def max_attempts(self, value: int):
"""
Represents the maximum number of attempts to trigger a refresh in case of error.
Default: 3
:param value: int
:return: Self
"""
self._max_attempts = value
return self

def delay_in_ms(self, value: int):
"""
Represents the delay between retries.
Default: 10
:param value: int
:return: Self
"""
self._delay_in_ms = value
return self
return self.idp


class EntraIdCredentialsProvider(StreamingCredentialProvider):
Expand Down
File renamed without changes.
20 changes: 0 additions & 20 deletions setup.py

This file was deleted.

12 changes: 6 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from redis import CredentialProvider
from redis.auth.idp import IdentityProviderInterface

from entraid.cred_provider import EntraIdCredentialsProvider, TokenAuthConfig
from entraid.identity_provider import ManagedIdentityType, create_provider_from_managed_identity, \
from redis_entraid.cred_provider import EntraIdCredentialsProvider, TokenAuthConfig
from redis_entraid.identity_provider import ManagedIdentityType, create_provider_from_managed_identity, \
create_provider_from_service_principal, EntraIDIdentityProvider


Expand Down Expand Up @@ -93,10 +93,10 @@ def get_credential_provider(request) -> CredentialProvider:
)

auth_config = TokenAuthConfig(idp)
auth_config.expiration_refresh_ratio(expiration_refresh_ratio)
auth_config.lower_refresh_bound_millis(lower_refresh_bound_millis)
auth_config.max_attempts(max_attempts)
auth_config.delay_in_ms(delay_in_ms)
auth_config.expiration_refresh_ratio = expiration_refresh_ratio
auth_config.lower_refresh_bound_millis = lower_refresh_bound_millis
auth_config.max_attempts = max_attempts
auth_config.delay_in_ms = delay_in_ms

return EntraIdCredentialsProvider(
config=auth_config,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cred_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from redis.auth.token import TokenInterface

from entraid.cred_provider import EntraIdCredentialsProvider
from redis_entraid.cred_provider import EntraIdCredentialsProvider


class TestEntraIdCredentialsProvider:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_identity_provider.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from msal import TokenCache

from entraid.identity_provider import EntraIDIdentityProvider
from redis_entraid.identity_provider import EntraIDIdentityProvider


class TestEntraIDIdentityProvider:
Expand Down

0 comments on commit 1358d89

Please sign in to comment.