Skip to content

Commit

Permalink
Small improvements to the Credentials class glideinWMS#258
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoCoimbra committed May 22, 2023
1 parent 65f0ecb commit 1866b7d
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions lib/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ class ParameterType(enum.Enum):
class Credential(ABC):
cred_type: CredentialType = None

def __init__(self, string: bytes = None, path: str = None, purpose: CredentialPurpose = None) -> None:
def __init__(self, string: bytes = None, path: str = None) -> None:
self._string = None
self.path = path
self.purpose = purpose
if string or path:
self.load(string, path)

def __repr__(self) -> str:
return f"{self.cred_type}(string={self.string!r}, path={self.path!r}, purpose={self.purpose!r})"
return f"{self.__class__.__name__}(string={self.string!r}, path={self.path!r}, purpose={self.purpose!r})"

def __str__(self) -> str:
return self.string.decode() if self.string else ""
Expand Down Expand Up @@ -172,14 +171,14 @@ def save_to_file(


# Dictionary of Credentials
class Credentials(dict):
class CredentialDict(dict):
def __setitem__(self, __k, __v):
if not isinstance(__v, Credential):
raise TypeError("Value must be a credential")
super().__setitem__(__k, __v)


class Parameter(dict):
class ParameterDict(dict):
def __setitem__(self, __k, __v):
if not isinstance(__k, ParameterType):
raise TypeError("Key must be a ParameterType")
Expand Down Expand Up @@ -278,9 +277,9 @@ def __init__(self, username, security_class):
self.security_class = security_class
self.id = None # id used for tacking the submit credentials
self.cred_dir = "" # location of credentials
self.security_credentials = Credentials()
self.identity_credentials = Credentials()
self.parameters = Parameter()
self.security_credentials = CredentialDict()
self.identity_credentials = CredentialDict()
self.parameters = ParameterDict()

def add_security_credential(self, cred_id, cred_name=None, credential=None, prefix="credential_"):
"""
Expand Down Expand Up @@ -344,13 +343,13 @@ def credential_of_type(cred_type: CredentialType):
raise CredentialError(f"Unknown Credential type: {cred_type}") from err


def create_credential_from_string(string: bytes, cred_type: CredentialType = None, **kargs) -> Credential:
def create_credential_from_string(string: bytes, cred_type: CredentialType = None) -> Credential:
credential = None
credential_types = [cred_type] if cred_type else CredentialType
for cred_type in credential_types:
try:
credential = credential_of_type(cred_type)()
credential.load_from_string(string, **kargs)
credential.load_from_string(string)
return credential
except CredentialError as err:
pass # Credential type incompatible with string
Expand All @@ -359,13 +358,13 @@ def create_credential_from_string(string: bytes, cred_type: CredentialType = Non
raise CredentialError(f"Could not load credential from string: {string}")


def create_credential_from_file(path: str, cred_type: CredentialType = None, **kargs) -> Credential:
def create_credential_from_file(path: str, cred_type: CredentialType = None) -> Credential:
credential = None
credential_types = [cred_type] if cred_type else CredentialType
for cred_type in credential_types:
try:
credential = credential_of_type(cred_type)()
credential.load_from_file(path, **kargs)
credential.load_from_file(path)
return credential
except CredentialError as err:
pass # Credential type incompatible with file
Expand All @@ -374,14 +373,14 @@ def create_credential_from_file(path: str, cred_type: CredentialType = None, **k
raise CredentialError(f"Could not load credential from file: {path}")


def create_credential(string: bytes = None, path: str = None, cred_type: CredentialType = None, **kargs):
def create_credential(string: bytes = None, path: str = None, cred_type: CredentialType = None):
if string:
credential = create_credential_from_string(string, cred_type, **kargs)
credential = create_credential_from_string(string, cred_type)
if path:
credential.path = path
return credential
elif path:
return create_credential_from_file(path, cred_type, **kargs)
return create_credential_from_file(path, cred_type)
else:
raise CredentialError("No string or path provided")

Expand Down

0 comments on commit 1866b7d

Please sign in to comment.