From a473bcd15f36580340e54daa8e7392811ea92fa1 Mon Sep 17 00:00:00 2001 From: mhorky Date: Tue, 10 Sep 2024 13:13:04 +0200 Subject: [PATCH] feat: Enable automatic cloud registration v2 again After being reverted for release in 8b25a8512002e2f078857b3abb1ab77a26e63efe and 548a2790597af5195bc780a4d8c6b9edb511b4ae, we can enable it again. --- src/rhsm/connection.py | 3 +- .../scripts/rhsmcertd_worker.py | 35 +++++++++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/rhsm/connection.py b/src/rhsm/connection.py index 471b74e4a0..565583acfe 100644 --- a/src/rhsm/connection.py +++ b/src/rhsm/connection.py @@ -1519,11 +1519,10 @@ def getCloudJWT(self, cloud_id: str, metadata: str, signature: str) -> Dict[str, } headers = { "Content-Type": "application/json", - "Accept": "text/plain", } return self.conn.request_post( - method="/cloud/authorize", + method="/cloud/authorize?version=2", params=data, headers=headers, description=_("Fetching cloud token"), diff --git a/src/subscription_manager/scripts/rhsmcertd_worker.py b/src/subscription_manager/scripts/rhsmcertd_worker.py index 2df385b72e..1dfa90ad0a 100644 --- a/src/subscription_manager/scripts/rhsmcertd_worker.py +++ b/src/subscription_manager/scripts/rhsmcertd_worker.py @@ -210,7 +210,7 @@ def _auto_register(cp_provider: "CPProvider") -> ExitStatus: # Obtain automatic registration token try: - token: Dict[str, str] = cache.CloudTokenCache._get_from_server( + token: Dict[str, str] = cache.CloudTokenCache.get( uep=uep, cloud_id=cloud_info["cloud_id"], metadata=cloud_info["metadata"], @@ -220,14 +220,29 @@ def _auto_register(cp_provider: "CPProvider") -> ExitStatus: log.exception("Cloud token could not be obtained. Unable to perform automatic registration.") return ExitStatus.NO_REGISTRATION_TOKEN - try: - _auto_register_standard(uep=uep, token=token) - except Exception: - log.exception("Standard automatic registration failed.") - return ExitStatus.REGISTRATION_FAILED - else: - log.info("Standard automatic registration was successful.") - return ExitStatus.OK + if token["tokenType"] == "CP-Cloud-Registration": + try: + _auto_register_standard(uep=uep, token=token) + except Exception: + log.exception("Standard automatic registration failed.") + return ExitStatus.REGISTRATION_FAILED + else: + log.info("Standard automatic registration was successful.") + return ExitStatus.OK + + if token["tokenType"] == "CP-Anonymous-Cloud-Registration": + try: + _auto_register_anonymous(uep=uep, token=token) + cache.CloudTokenCache.delete_cache() + except Exception: + log.exception("Anonymous automatic registration failed.") + return ExitStatus.REGISTRATION_FAILED + else: + log.info("Anonymous automatic registration was successful.") + return ExitStatus.OK + + log.error(f"Unsupported token type for automatic registration: {token['tokenType']}.") + return ExitStatus.BAD_TOKEN_TYPE def _auto_register_standard(uep: "UEPConnection", token: Dict[str, str]) -> None: @@ -242,7 +257,7 @@ def _auto_register_standard(uep: "UEPConnection", token: Dict[str, str]) -> None _auto_register_wait() service = RegisterService(cp=uep) - service.register(org=None, jwt_token=token) + service.register(org=None, jwt_token=token["token"]) def _auto_register_anonymous(uep: "UEPConnection", token: Dict[str, str]) -> None: