|
| 1 | +import json |
| 2 | +import time |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | + |
| 8 | +# ARI retry configuration per RFC 9773 Section 4.3.3 |
| 9 | +DEFAULT_RETRY_AFTER = 21600 # 6 hours default |
| 10 | +MIN_RETRY_AFTER = 60 # 1 minute minimum |
| 11 | +MAX_RETRY_AFTER = 86400 # 1 day maximum |
| 12 | +MAX_RETRIES = 3 # Max temporary error retries |
| 13 | + |
| 14 | + |
| 15 | +def fetch_renewal_info(ari_endpoint: str, cert_id: str, retries: int = MAX_RETRIES, timeout: int = 30) -> dict: |
| 16 | + """ |
| 17 | + Fetch renewal information from ACME server per RFC 9773 Section 4 |
| 18 | +
|
| 19 | + Implements exponential backoff for temporary errors per RFC 9773 Section 4.3.3 |
| 20 | +
|
| 21 | + :param ari_endpoint: RenewalInfo endpoint URL |
| 22 | + :param cert_id: Unique certificate identifier from get_cert_id() |
| 23 | + :param retries: Number of retries remaining for temporary errors |
| 24 | + :param timeout: Request timeout in seconds |
| 25 | + :return: Dict with error field (None if success), suggestedWindow (start/end datetimes), |
| 26 | + optional explanationURL, retry_after |
| 27 | + """ |
| 28 | + url = f'{ari_endpoint.rstrip("/")}/{cert_id}' |
| 29 | + backoff_delay = 1 |
| 30 | + response = None |
| 31 | + |
| 32 | + for attempt in range(retries + 1): |
| 33 | + try: |
| 34 | + response = requests.get(url, timeout=timeout) |
| 35 | + |
| 36 | + # Check for HTTP 409 alreadyReplaced error (RFC 9773 Section 7.4) |
| 37 | + if response.status_code == 409: |
| 38 | + return {'error': 'Certificate has already been marked as replaced'} |
| 39 | + |
| 40 | + # Handle 5xx server errors as temporary (RFC 9773 Section 4.3.3) |
| 41 | + if 500 <= response.status_code < 600: |
| 42 | + if attempt < retries: |
| 43 | + time.sleep(backoff_delay) |
| 44 | + backoff_delay *= 2 |
| 45 | + continue |
| 46 | + return {'error': f'ARI server error after {retries + 1} attempts: HTTP {response.status_code}'} |
| 47 | + |
| 48 | + if response.status_code not in (200, 201, 204): |
| 49 | + return {'error': f'ARI request failed: HTTP {response.status_code}'} |
| 50 | + |
| 51 | + data = response.json() |
| 52 | + break |
| 53 | + |
| 54 | + except (ConnectionError, TimeoutError, requests.exceptions.RequestException) as e: |
| 55 | + if attempt < retries: |
| 56 | + time.sleep(backoff_delay) |
| 57 | + backoff_delay *= 2 |
| 58 | + continue |
| 59 | + |
| 60 | + return {'error': f'ARI request failed after {retries + 1} attempts: {e}'} |
| 61 | + except json.JSONDecodeError as e: |
| 62 | + return {'error': f'Invalid JSON response: {e}'} |
| 63 | + except Exception as e: |
| 64 | + return {'error': f'ARI request failed: {e}'} |
| 65 | + |
| 66 | + if 'suggestedWindow' not in data: |
| 67 | + return {'error': 'Invalid ARI response: missing suggestedWindow'} |
| 68 | + |
| 69 | + window = data['suggestedWindow'] |
| 70 | + if 'start' not in window or 'end' not in window: |
| 71 | + return {'error': 'Invalid suggestedWindow: missing start or end'} |
| 72 | + |
| 73 | + try: |
| 74 | + start = datetime.fromisoformat(window['start'].replace('Z', '+00:00')) |
| 75 | + end = datetime.fromisoformat(window['end'].replace('Z', '+00:00')) |
| 76 | + except (ValueError, TypeError) as e: |
| 77 | + return {'error': f'Invalid date format in suggestedWindow: {e}'} |
| 78 | + |
| 79 | + result = { |
| 80 | + 'error': None, |
| 81 | + 'suggested_window': {'start': start, 'end': end}, |
| 82 | + 'retry_after': None, |
| 83 | + 'explanation_url': data.get('explanationURL'), |
| 84 | + } |
| 85 | + |
| 86 | + # Parse Retry-After header per RFC 9773 Section 4.3 |
| 87 | + if response and 'Retry-After' in response.headers: |
| 88 | + try: |
| 89 | + retry_after = int(response.headers['Retry-After']) |
| 90 | + # Clamp to reasonable limits per RFC 9773 Section 4.3.2 |
| 91 | + retry_after = max(MIN_RETRY_AFTER, min(retry_after, MAX_RETRY_AFTER)) |
| 92 | + result['retry_after'] = retry_after |
| 93 | + except ValueError: |
| 94 | + result['retry_after'] = DEFAULT_RETRY_AFTER |
| 95 | + else: |
| 96 | + # Use default if not provided per RFC 9773 Section 4.3.3 |
| 97 | + result['retry_after'] = DEFAULT_RETRY_AFTER |
| 98 | + |
| 99 | + return result |
0 commit comments