-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathchain.py
More file actions
62 lines (51 loc) · 2.15 KB
/
chain.py
File metadata and controls
62 lines (51 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Credential provider chain for the Cloudsmith CLI.
Implements an AWS SDK-style credential resolution chain that evaluates
credential sources sequentially and returns the first valid result.
"""
from __future__ import annotations
import logging
from .models import CredentialContext, CredentialResult
from .provider import CredentialProvider
logger = logging.getLogger(__name__)
class CredentialProviderChain:
"""Evaluates credential providers in order, returning the first valid result.
If no providers are given, uses the default chain:
Keyring → CLIFlag → OIDC.
"""
def __init__(self, providers: list[CredentialProvider] | None = None):
if providers is not None:
self.providers = providers
else:
from .providers import CLIFlagProvider, KeyringProvider, OidcProvider
self.providers = [
KeyringProvider(),
CLIFlagProvider(),
OidcProvider(),
]
def resolve(self, context: CredentialContext) -> CredentialResult | None:
"""Evaluate each provider in order. Return the first successful result."""
for provider in self.providers:
try:
result = provider.resolve(context)
if result is not None:
if context.debug:
logger.debug(
"Credentials resolved by %s: %s",
provider.name,
result.source_detail or result.source_name,
)
return result
if context.debug:
logger.debug(
"Provider %s did not resolve credentials, trying next",
provider.name,
)
except Exception: # pylint: disable=broad-exception-caught
# Intentionally broad - one provider failing shouldn't stop others
logger.debug(
"Provider %s raised an exception, skipping",
provider.name,
exc_info=True,
)
continue
return None