Skip to content

Commit 3311679

Browse files
committed
refactor: added validate_credentials
1 parent 1a9ffb1 commit 3311679

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

custom-domain/dstack-ingress/scripts/certman.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# Add script directory to path to import dns_providers
1111
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
1212

13+
from .dns_providers.base import DNSProvider
1314
from dns_providers import DNSProviderFactory
1415

1516

@@ -238,27 +239,6 @@ def _debug_plugin_registration(self) -> None:
238239
except Exception as debug_error:
239240
print(f"Debug failed: {debug_error}")
240241

241-
def _validate_provider_credentials(self) -> bool:
242-
"""Validate provider credentials by testing API access."""
243-
print(f"Validating {self.provider_type} API credentials...")
244-
245-
try:
246-
# For Namecheap, test API access
247-
if hasattr(self.provider, '_make_request'):
248-
test_result = self.provider._make_request("namecheap.users.getBalances")
249-
if test_result.get("success", False):
250-
print(f"✓ {self.provider_type} API credentials are valid")
251-
return True
252-
else:
253-
print(f"✗ {self.provider_type} API validation failed: {test_result.get('errors', ['Unknown error'])}")
254-
return False
255-
else:
256-
print(f"No API validation available for {self.provider_type}, skipping")
257-
return True
258-
except Exception as e:
259-
print(f"Error validating {self.provider_type} credentials: {e}")
260-
return False
261-
262242
def setup_credentials(self) -> bool:
263243
"""Setup credentials file for certbot using provider implementation."""
264244
result = self.provider.setup_certbot_credentials()
@@ -304,7 +284,7 @@ def obtain_certificate(self, domain: str, email: str) -> bool:
304284
return False
305285

306286
# Validate credentials before proceeding
307-
if not self._validate_provider_credentials():
287+
if not self.provider.validate_credentials():
308288
print(f"Failed to validate credentials for {self.provider_type}", file=sys.stderr)
309289
return False
310290

custom-domain/dstack-ingress/scripts/dns_providers/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class DNSProvider(ABC):
5252

5353
# Certbot configuration - override in subclasses
5454
CERTBOT_PLUGIN = ""
55+
CERTBOT_PLUGIN_MODULE = ""
5556
CERTBOT_PACKAGE = ""
5657
CERTBOT_PROPAGATION_SECONDS = 120
5758
CERTBOT_CREDENTIALS_FILE = "" # Path to credentials file
@@ -64,6 +65,10 @@ def setup_certbot_credentials(self) -> bool:
6465
"""Setup credentials file for certbot. Override in subclasses if needed."""
6566
return True # Default: no setup needed
6667

68+
def validate_credentials(self) -> bool:
69+
"""Validate provider credentials. Override in subclasses if needed."""
70+
return True # Default: no validation needed
71+
6772
@classmethod
6873
def suitable(cls) -> bool:
6974
"""Check if the current environment is suitable for this DNS provider."""

custom-domain/dstack-ingress/scripts/dns_providers/namecheap.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ def setup_certbot_credentials(self) -> bool:
5252
print(f"Error setting up credentials file: {e}")
5353
return False
5454

55+
def validate_credentials(self) -> bool:
56+
"""Validate Namecheap API credentials by testing API access."""
57+
print(f"Validating Namecheap API credentials...")
58+
59+
try:
60+
# Test API access with getBalances command
61+
test_result = self._make_request("namecheap.users.getBalances")
62+
if test_result.get("success", False):
63+
print(f"✓ Namecheap API credentials are valid")
64+
return True
65+
else:
66+
print(f"✗ Namecheap API validation failed: {test_result.get('errors', ['Unknown error'])}")
67+
return False
68+
except Exception as e:
69+
print(f"Error validating Namecheap credentials: {e}")
70+
return False
71+
5572
def _make_request(self, command: str, **params) -> Dict:
5673
"""Make a request to the Namecheap API with error handling."""
5774
# Base parameters required for all Namecheap API calls

0 commit comments

Comments
 (0)