Coverage for frappe_manager / ssl_manager / service_factory.py: 64%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-07-02 18:35 +0530

1""" 

2Factory for creating SSL certificate service instances. 

3 

4This module provides a factory function that creates the appropriate SSL certificate 

5service based on certificate configuration, following the dependency injection pattern. 

6""" 

7 

8from frappe_manager.logger.contextual import ContextualLogger 

9from frappe_manager.output_manager import OutputHandler 

10from frappe_manager.ssl_manager import SUPPORTED_SSL_TYPES 

11from frappe_manager.ssl_manager.acmesh_certificate_service import AcmeShCertificateService 

12from frappe_manager.ssl_manager.certificate import SSLCertificate 

13from frappe_manager.ssl_manager.no_op_certificate_service import NoOpCertificateService 

14from frappe_manager.ssl_manager.ssl_certificate_service import SSLCertificateService 

15from frappe_manager.ssl_manager.storage_config import SSLStorageConfig 

16 

17 

18def create_certificate_service( 

19 logger: ContextualLogger, 

20 certificate: SSLCertificate, 

21 storage_config: SSLStorageConfig, 

22 output_handler: OutputHandler, 

23) -> SSLCertificateService: 

24 """ 

25 Create an appropriate SSL certificate service based on certificate configuration. 

26 

27 This factory function uses acme.sh for all Let's Encrypt certificates. 

28 

29 Args: 

30 certificate: The SSL certificate to create a service for 

31 storage_config: Storage configuration with paths for SSL operations 

32 output_handler: Output handler for user-facing messages 

33 

34 Returns: 

35 An SSL certificate service instance appropriate for the certificate 

36 

37 Raises: 

38 ValueError: If certificate configuration is invalid 

39 """ 

40 if hasattr(certificate, "ssl_type") and certificate.ssl_type == SUPPORTED_SSL_TYPES.none: 

41 return NoOpCertificateService( 

42 logger=logger, 

43 root_dir=storage_config.ssl_dir, 

44 output_handler=output_handler, 

45 ) 

46 

47 if hasattr(certificate, "enabled") and not certificate.enabled: 

48 return NoOpCertificateService( 

49 logger=logger, 

50 root_dir=storage_config.ssl_dir, 

51 output_handler=output_handler, 

52 ) 

53 

54 return AcmeShCertificateService( 

55 logger=logger, 

56 ssl_service_dir=storage_config.ssl_dir, 

57 webroot_dir=storage_config.webroot_dir, 

58 output_handler=output_handler, 

59 )