@@ -535,51 +535,91 @@ def test_tlsobject_store_key_ls(self, _set_store, cephadm_module: CephadmOrchest
535535 @mock .patch ("cephadm.module.CephadmOrchestrator.get_store_prefix" )
536536 def test_tlsobject_store_load (self , _get_store_prefix , cephadm_module : CephadmOrchestrator ):
537537
538- rgw_frontend_rgw_foo_host2_cert = 'fake-rgw-cert'
539- grafana_host1_key = 'fake-grafana-host1-cert'
540- nvmeof_server_cert = 'nvmeof-server-cert'
541- nvmeof_client_cert = 'nvmeof-client-cert'
542- nvmeof_root_ca_cert = 'nvmeof-root-ca-cert'
543- nvmeof_server_key = 'nvmeof-server-key'
544- nvmeof_client_key = 'nvmeof-client-key'
545- nvmeof_encryption_key = 'nvmeof-encryption-key'
546- unknown_cert_entity = 'unknown_per_service_cert'
547- unknown_cert_key = 'unknown_per_service_key'
538+ # Define certs and keys with their corresponding scopes
539+ certs = {
540+ 'rgw_frontend_ssl_cert' : ('rgw.foo' , 'fake-rgw-cert' , TLSObjectScope .SERVICE ),
541+ 'nvmeof_server_cert' : ('nvmeof.foo' , 'nvmeof-server-cert' , TLSObjectScope .SERVICE ),
542+ 'nvmeof_client_cert' : ('nvmeof.foo' , 'nvmeof-client-cert' , TLSObjectScope .SERVICE ),
543+ 'nvmeof_root_ca_cert' : ('nvmeof.foo' , 'nvmeof-root-ca-cert' , TLSObjectScope .SERVICE ),
544+ 'ingress_ssl_cert' : ('ingress' , 'ingress-ssl-cert' , TLSObjectScope .SERVICE ),
545+ 'iscsi_ssl_cert' : ('iscsi' , 'iscsi-ssl-cert' , TLSObjectScope .SERVICE ),
546+ 'grafana_cert' : ('host1' , 'grafana-cert' , TLSObjectScope .HOST ),
547+ 'mgmt_gw_cert' : ('mgmt-gateway' , 'mgmt-gw-cert' , TLSObjectScope .GLOBAL ),
548+ 'oauth2_proxy_cert' : ('oauth2-proxy' , 'oauth2-proxy-cert' , TLSObjectScope .GLOBAL ),
549+ }
550+ unknown_certs = {
551+ 'unknown_per_service_cert' : ('unknown-svc.foo' , 'unknown-cert' , TLSObjectScope .SERVICE ),
552+ 'unknown_per_host_cert' : ('unknown-host.foo' , 'unknown-cert' , TLSObjectScope .HOST ),
553+ 'unknown_global_cert' : ('unknown-global.foo' , 'unknown-cert' , TLSObjectScope .GLOBAL ),
554+ 'cert_with_unknown_scope' : ('unknown-global.foo' , 'unknown-cert' , TLSObjectScope .UNKNOWN ),
555+ }
548556
557+ keys = {
558+ 'grafana_key' : ('host1' , 'fake-grafana-host1-key' , TLSObjectScope .HOST ),
559+ 'nvmeof_server_key' : ('nvmeof.foo' , 'nvmeof-server-key' , TLSObjectScope .SERVICE ),
560+ 'nvmeof_client_key' : ('nvmeof.foo' , 'nvmeof-client-key' , TLSObjectScope .SERVICE ),
561+ 'nvmeof_encryption_key' : ('nvmeof.foo' , 'nvmeof-encryption-key' , TLSObjectScope .SERVICE ),
562+ 'mgmt_gw_key' : ('mgmt-gateway' , 'mgmt-gw-key' , TLSObjectScope .GLOBAL ),
563+ 'oauth2_proxy_key' : ('oauth2-proxy' , 'oauth2-proxy-key' , TLSObjectScope .GLOBAL ),
564+ 'ingress_ssl_key' : ('ingress' , 'ingress-ssl-key' , TLSObjectScope .SERVICE ),
565+ 'iscsi_ssl_key' : ('iscsi' , 'iscsi-ssl-key' , TLSObjectScope .SERVICE ),
566+ }
567+ unknown_keys = {
568+ 'unknown_per_service_key' : ('unknown-svc.foo' , 'unknown-key' , TLSObjectScope .SERVICE ),
569+ 'unknown_per_host_key' : ('unknown-host.foo' , 'unknown-key' , TLSObjectScope .HOST ),
570+ 'unknown_global_key' : ('unknown-global.foo' , 'unknown-key' , TLSObjectScope .GLOBAL ),
571+ 'key_with_unknown_scope' : ('unknown-global.foo' , 'unknown-key' , TLSObjectScope .UNKNOWN ),
572+ }
573+
574+ # Mock function to simulate store behavior
549575 def _fake_prefix_store (key ):
576+ from itertools import chain
550577 if key == 'cert_store.cert.' :
551578 return {
552- f'{ TLSOBJECT_STORE_CERT_PREFIX } rgw_frontend_ssl_cert ' : json .dumps ({ 'rgw.foo' : Cert ( rgw_frontend_rgw_foo_host2_cert , True ). to_json ()}),
553- f' { TLSOBJECT_STORE_CERT_PREFIX } nvmeof_server_cert' : json . dumps ({ 'nvmeof.foo' : Cert (nvmeof_server_cert , True ).to_json ()}),
554- f' { TLSOBJECT_STORE_CERT_PREFIX } nvmeof_client_cert' : json . dumps ({ 'nvmeof.foo' : Cert (nvmeof_client_cert , True ).to_json ()}),
555- f' { TLSOBJECT_STORE_CERT_PREFIX } nvmeof_root_ca_cert' : json . dumps ({ 'nvmeof.foo' : Cert ( nvmeof_root_ca_cert , True ). to_json ()}),
556- f' { TLSOBJECT_STORE_CERT_PREFIX } { unknown_cert_entity } ' : json . dumps ({ 'unkonwn.foo' : Cert ( rgw_frontend_rgw_foo_host2_cert , True ). to_json ()}),
579+ f'{ TLSOBJECT_STORE_CERT_PREFIX } { cert_name } ' : json .dumps (
580+ { target : Cert (cert_value , True ).to_json ()} if scope != TLSObjectScope . GLOBAL
581+ else Cert (cert_value , True ).to_json ()
582+ )
583+ for cert_name , ( target , cert_value , scope ) in chain ( certs . items (), unknown_certs . items ())
557584 }
558585 elif key == 'cert_store.key.' :
559586 return {
560- f'{ TLSOBJECT_STORE_KEY_PREFIX } grafana_key ' : json .dumps ({ 'host1' : PrivKey ( grafana_host1_key ). to_json ()}),
561- f' { TLSOBJECT_STORE_KEY_PREFIX } nvmeof_server_key' : json . dumps ({ 'nvmeof.foo' : PrivKey (nvmeof_server_key ).to_json ()}),
562- f' { TLSOBJECT_STORE_KEY_PREFIX } nvmeof_client_key' : json . dumps ({ 'nvmeof.foo' : PrivKey (nvmeof_client_key ).to_json ()}),
563- f' { TLSOBJECT_STORE_KEY_PREFIX } nvmeof_encryption_key' : json . dumps ({ 'nvmeof.foo' : PrivKey ( nvmeof_encryption_key ). to_json ()}),
564- f' { TLSOBJECT_STORE_KEY_PREFIX } { unknown_cert_key } ' : json . dumps ({ 'unkonwn.foo' : PrivKey ( nvmeof_encryption_key ). to_json ()}),
587+ f'{ TLSOBJECT_STORE_KEY_PREFIX } { key_name } ' : json .dumps (
588+ { target : PrivKey (key_value ).to_json ()} if scope != TLSObjectScope . GLOBAL
589+ else PrivKey (key_value ).to_json ()
590+ )
591+ for key_name , ( target , key_value , scope ) in chain ( keys . items (), unknown_keys . items ())
565592 }
566593 else :
567- raise Exception (f'Get store with unexpected value { key } ' )
594+ raise Exception (f'Unexpected key access in store: { key } ' )
568595
596+ # Inject the mock store behavior and the cert manager
569597 _get_store_prefix .side_effect = _fake_prefix_store
570598 cephadm_module ._init_cert_mgr ()
571599
572- assert cephadm_module .cert_mgr .cert_store .known_entities ['rgw_frontend_ssl_cert' ]['rgw.foo' ] == Cert (rgw_frontend_rgw_foo_host2_cert , True )
573- assert cephadm_module .cert_mgr .cert_store .known_entities ['nvmeof_server_cert' ]['nvmeof.foo' ] == Cert (nvmeof_server_cert , True )
574- assert cephadm_module .cert_mgr .cert_store .known_entities ['nvmeof_client_cert' ]['nvmeof.foo' ] == Cert (nvmeof_client_cert , True )
575- assert cephadm_module .cert_mgr .cert_store .known_entities ['nvmeof_root_ca_cert' ]['nvmeof.foo' ] == Cert (nvmeof_root_ca_cert , True )
576- assert cephadm_module .cert_mgr .key_store .known_entities ['grafana_key' ]['host1' ] == PrivKey (grafana_host1_key )
577- assert unknown_cert_entity not in cephadm_module .cert_mgr .cert_store .known_entities
578-
579- assert cephadm_module .cert_mgr .key_store .known_entities ['nvmeof_server_key' ]['nvmeof.foo' ] == PrivKey (nvmeof_server_key )
580- assert cephadm_module .cert_mgr .key_store .known_entities ['nvmeof_client_key' ]['nvmeof.foo' ] == PrivKey (nvmeof_client_key )
581- assert cephadm_module .cert_mgr .key_store .known_entities ['nvmeof_encryption_key' ]['nvmeof.foo' ] == PrivKey (nvmeof_encryption_key )
582- assert unknown_cert_key not in cephadm_module .cert_mgr .key_store .known_entities
600+ # Validate certificates in cert_store
601+ for cert_name , (target , cert_value , scope ) in certs .items ():
602+ assert cert_name in cephadm_module .cert_mgr .cert_store .known_entities
603+ if scope == TLSObjectScope .GLOBAL :
604+ assert cephadm_module .cert_mgr .cert_store .known_entities [cert_name ] == Cert (cert_value , True )
605+ else :
606+ assert cephadm_module .cert_mgr .cert_store .known_entities [cert_name ][target ] == Cert (cert_value , True )
607+
608+ # Validate keys in key_store
609+ for key_name , (target , key_value , scope ) in keys .items ():
610+ assert key_name in cephadm_module .cert_mgr .key_store .known_entities
611+ if scope == TLSObjectScope .GLOBAL :
612+ assert cephadm_module .cert_mgr .key_store .known_entities [key_name ] == PrivKey (key_value )
613+ else :
614+ assert cephadm_module .cert_mgr .key_store .known_entities [key_name ][target ] == PrivKey (key_value )
615+
616+ # Check unknown certificates are not loaded
617+ for unknown_cert in unknown_certs :
618+ assert unknown_cert not in cephadm_module .cert_mgr .cert_store .known_entities
619+
620+ # Check unknown keys are not loaded
621+ for unknown_key in unknown_keys :
622+ assert unknown_key not in cephadm_module .cert_mgr .key_store .known_entities
583623
584624 def test_tlsobject_store_get_cert_key (self , cephadm_module : CephadmOrchestrator ):
585625
0 commit comments