From d0d5d876f6476548543465449baf734a014e7028 Mon Sep 17 00:00:00 2001 From: mhorky Date: Tue, 23 Apr 2024 16:42:55 +0200 Subject: [PATCH] this patch makes sub-man explode when non-SCA mode is detected --- src/subscription_manager/cache.py | 5 +- .../cli_command/autoheal.py | 70 ------ .../cli_command/redeem.py | 90 -------- .../cli_command/register.py | 22 +- .../cli_command/remove.py | 212 ------------------ src/subscription_manager/managercli.py | 16 -- src/subscription_manager/utils.py | 6 +- 7 files changed, 12 insertions(+), 409 deletions(-) delete mode 100644 src/subscription_manager/cli_command/autoheal.py delete mode 100644 src/subscription_manager/cli_command/redeem.py delete mode 100644 src/subscription_manager/cli_command/remove.py diff --git a/src/subscription_manager/cache.py b/src/subscription_manager/cache.py index 64e3bd9c03..db7a2ae554 100644 --- a/src/subscription_manager/cache.py +++ b/src/subscription_manager/cache.py @@ -1046,7 +1046,10 @@ def _sync_with_server( ) else: if "contentAccessMode" in current_owner: - return current_owner["contentAccessMode"] + mode = current_owner["contentAccessMode"] + if mode != "org_environment": + raise RuntimeError("IS NOT SCA") + return mode else: log.debug( "The owner returned from the server did not contain a " diff --git a/src/subscription_manager/cli_command/autoheal.py b/src/subscription_manager/cli_command/autoheal.py deleted file mode 100644 index d4ead1f2fd..0000000000 --- a/src/subscription_manager/cli_command/autoheal.py +++ /dev/null @@ -1,70 +0,0 @@ -# -# Subscription manager command line utility. -# -# Copyright (c) 2021 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public License, -# version 2 (GPLv2). There is NO WARRANTY for this software, express or -# implied, including the implied warranties of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 -# along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. -# -# Red Hat trademarks are not licensed under GPLv2. No permission is -# granted to use or replicate Red Hat trademarks that are incorporated -# in this software or its documentation. -# -import subscription_manager.injection as inj - -from subscription_manager.cli_command.cli import CliCommand -from subscription_manager.i18n import ugettext as _ - - -class AutohealCommand(CliCommand): - def __init__(self): - self.uuid = inj.require(inj.IDENTITY).uuid - - shortdesc = _("Set if subscriptions are attached on a schedule (default of daily)") - self._org_help_text = _("specify whether to enable or disable auto-attaching of subscriptions") - super(AutohealCommand, self).__init__("auto-attach", shortdesc, False) - - self.parser.add_argument( - "--enable", - dest="enable", - action="store_true", - help=_("try to attach subscriptions for uncovered products each check-in"), - ) - self.parser.add_argument( - "--disable", - dest="disable", - action="store_true", - help=_("do not try to automatically attach subscriptions each check-in"), - ) - self.parser.add_argument( - "--show", - dest="show", - action="store_true", - help=_("show the current auto-attach preference"), - ) - - def _toggle(self, autoheal): - self.cp.updateConsumer(self.uuid, autoheal=autoheal) - self._show(autoheal) - - def _validate_options(self): - if not self.uuid: - self.assert_should_be_registered() - - def _show(self, autoheal): - if autoheal: - print(_("Auto-attach preference: enabled")) - else: - print(_("Auto-attach preference: disabled")) - - def _do_command(self): - self._validate_options() - - if not self.options.enable and not self.options.disable: - self._show(self.cp.getConsumer(self.uuid)["autoheal"]) - else: - self._toggle(self.options.enable or False) diff --git a/src/subscription_manager/cli_command/redeem.py b/src/subscription_manager/cli_command/redeem.py deleted file mode 100644 index fe74273d68..0000000000 --- a/src/subscription_manager/cli_command/redeem.py +++ /dev/null @@ -1,90 +0,0 @@ -# -# Subscription manager command line utility. -# -# Copyright (c) 2021 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public License, -# version 2 (GPLv2). There is NO WARRANTY for this software, express or -# implied, including the implied warranties of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 -# along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. -# -# Red Hat trademarks are not licensed under GPLv2. No permission is -# granted to use or replicate Red Hat trademarks that are incorporated -# in this software or its documentation. -# -import os - -import rhsm.connection as connection -import subscription_manager.injection as inj - -from subscription_manager.cli import system_exit -from subscription_manager.cli_command.cli import CliCommand, handle_exception -from subscription_manager.i18n import ugettext as _ - - -class RedeemCommand(CliCommand): - def __init__(self): - shortdesc = _("Attempt to redeem a subscription for a preconfigured system") - super(RedeemCommand, self).__init__("redeem", shortdesc, False) - - self.parser.add_argument( - "--email", - dest="email", - action="store", - help=_("email address to notify when " "subscription redemption is complete"), - ) - self.parser.add_argument( - "--locale", - dest="locale", - action="store", - help=_( - "optional language to use for email " - "notification when subscription redemption is " - "complete (Examples: en-us, de-de)" - ), - ) - - def _validate_options(self): - if not self.options.email: - system_exit( - os.EX_USAGE, _("Error: This command requires that you specify an email address with --email.") - ) - - def _do_command(self): - """ - Executes the command. - """ - self.assert_should_be_registered() - self._validate_options() - - try: - # FIXME: why just facts and package profile update here? - # update facts first, if we need to - facts = inj.require(inj.FACTS) - facts.update_check(self.cp, self.identity.uuid) - - profile_mgr = inj.require(inj.PROFILE_MANAGER) - profile_mgr.update_check(self.cp, self.identity.uuid) - - # BZ 1248833 Ensure we print out the display message if we get any back - response = self.cp.activateMachine(self.identity.uuid, self.options.email, self.options.locale) - if response is None: - system_exit(os.EX_SOFTWARE, _("Error: Unable to redeem subscription for this system.")) - if response.get("displayMessage"): - system_exit(0, response.get("displayMessage")) - except connection.GoneException as ge: - raise ge - except connection.RestlibException as e: - # candlepin throws an exception during activateMachine, even for - # 200's. We need to look at the code in the RestlibException and proceed - # accordingly - if 200 <= e.code <= 210: - system_exit(0, e) - else: - handle_exception("Unable to redeem: {e}".format(e=e), e) - except Exception as e: - handle_exception("Unable to redeem: {e}".format(e=e), e) - - self._request_validity_check() diff --git a/src/subscription_manager/cli_command/register.py b/src/subscription_manager/cli_command/register.py index 0c7ae9bd46..d46fae701a 100644 --- a/src/subscription_manager/cli_command/register.py +++ b/src/subscription_manager/cli_command/register.py @@ -106,17 +106,6 @@ def __init__(self): dest="release", help=_("set a release version"), ) - self.parser.add_argument( - "--autosubscribe", - action="store_true", - help=_("Deprecated, see --auto-attach"), - ) - self.parser.add_argument( - "--auto-attach", - action="store_true", - dest="autoattach", - help=_("automatically attach compatible subscriptions to this system"), - ) self.parser.add_argument( "--force", action="store_true", @@ -135,7 +124,6 @@ def __init__(self): ) def _validate_options(self): - self.autoattach = self.options.autosubscribe or self.options.autoattach if self.is_registered() and not self.options.force: system_exit(os.EX_USAGE, _("This system is already registered. Use --force to override")) elif self.options.consumername == "": @@ -148,12 +136,12 @@ def _validate_options(self): ) elif self.options.environments and self.options.activation_keys: system_exit(os.EX_USAGE, _("Error: Activation keys do not allow environments to be specified.")) - elif self.autoattach and self.options.activation_keys: + elif self.options.activation_keys: system_exit(os.EX_USAGE, _("Error: Activation keys cannot be used with --auto-attach.")) # 746259: Don't allow the user to pass in an empty string as an activation key elif self.options.activation_keys and "" in self.options.activation_keys: system_exit(os.EX_USAGE, _("Error: Must specify an activation key")) - elif self.options.service_level and not self.autoattach: + elif self.options.service_level: system_exit(os.EX_USAGE, _("Error: Must use --auto-attach with --servicelevel.")) elif self.options.activation_keys and not self.options.org: system_exit(os.EX_USAGE, _("Error: Must provide --org with activation keys.")) @@ -402,13 +390,9 @@ def _do_command(self): # TODO: grab the list of valid options, and check self.cp.updateConsumer(consumer["uuid"], release=self.options.release) - if self.autoattach: - self._do_auto_attach(consumer) - if ( self.options.consumerid or self.options.activation_keys - or self.autoattach or self.cp.has_capability(CONTENT_ACCESS_CERT_CAPABILITY) ): log.debug("System registered, updating entitlements if needed") @@ -419,7 +403,7 @@ def _do_command(self): self._upload_profile(consumer) subscribed = 0 - if self.options.activation_keys or self.autoattach: + if self.options.activation_keys: # update with the latest cert info self.sorter = inj.require(inj.CERT_SORTER) self.sorter.force_cert_check() diff --git a/src/subscription_manager/cli_command/remove.py b/src/subscription_manager/cli_command/remove.py deleted file mode 100644 index 3e58b1dd48..0000000000 --- a/src/subscription_manager/cli_command/remove.py +++ /dev/null @@ -1,212 +0,0 @@ -# -# Subscription manager command line utility. -# -# Copyright (c) 2021 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public License, -# version 2 (GPLv2). There is NO WARRANTY for this software, express or -# implied, including the implied warranties of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 -# along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. -# -# Red Hat trademarks are not licensed under GPLv2. No permission is -# granted to use or replicate Red Hat trademarks that are incorporated -# in this software or its documentation. -# -import logging -import os - -import rhsm.connection as connection - -from rhsmlib.services import entitlement - -from subscription_manager.cli import system_exit -from subscription_manager.cli_command.cli import CliCommand, handle_exception -from subscription_manager.i18n import ungettext, ugettext as _ -from subscription_manager.utils import unique_list_items - -log = logging.getLogger(__name__) - - -class RemoveCommand(CliCommand): - def __init__(self): - super(RemoveCommand, self).__init__(self._command_name(), self._short_description(), self._primary()) - - self.parser.add_argument( - "--serial", - action="append", - dest="serials", - metavar="SERIAL", - help=_("certificate serial number to remove (can be specified more than once)"), - ) - self.parser.add_argument( - "--pool", - action="append", - dest="pool_ids", - metavar="POOL_ID", - help=_("the ID of the pool to remove (can be specified more than once)"), - ) - self.parser.add_argument( - "--all", - dest="all", - action="store_true", - help=_("remove all subscriptions from this system"), - ) - - def _short_description(self): - return _("Remove all or specific subscriptions from this system") - - def _command_name(self): - return "remove" - - def _primary(self): - return True - - def _validate_options(self): - if self.options.serials: - bad = False - for serial in self.options.serials: - if not serial.isdigit(): - print(_("Error: '{serial}' is not a valid serial number").format(serial=serial)) - bad = True - if bad: - system_exit(os.EX_USAGE) - elif self.options.pool_ids: - if not self.cp.has_capability("remove_by_pool_id"): - system_exit( - os.EX_UNAVAILABLE, - _( - "Error: The registered entitlement server does not support remove --pool." - "\nInstead, use the remove --serial option." - ), - ) - elif not self.options.all and not self.options.pool_ids: - system_exit( - os.EX_USAGE, - _("Error: This command requires that you specify one of --serial, --pool or --all."), - ) - - def _print_unbind_ids_result(self, success, failure, id_name): - if success: - if id_name == "pools": - print(_("The entitlement server successfully removed these pools:")) - elif id_name == "serial numbers": - print(_("The entitlement server successfully removed these serial numbers:")) - else: - print(_("The entitlement server successfully removed these IDs:")) - for id_ in success: - print(" {id_}".format(id_=id_)) - if failure: - if id_name == "pools": - print(_("The entitlement server failed to remove these pools:")) - elif id_name == "serial numbers": - print(_("The entitlement server failed to remove these serial numbers:")) - else: - print(_("The entitlement server failed to remove these IDs:")) - for id_ in failure: - print(" {id_}".format(id_=id_)) - - def _do_command(self): - """ - Executes the command. - """ - self._validate_options() - return_code = 0 - if self.is_registered(): - ent_service = entitlement.EntitlementService(self.cp) - try: - if self.options.all: - total = ent_service.remove_all_entitlements() - # total will be None on older Candlepins that don't - # support returning the number of subscriptions unsubscribed from - if total is None: - print(_("All subscriptions have been removed at the server.")) - else: - count = total["deletedRecords"] - print( - ungettext( - "%s subscription removed at the server.", - "%s subscriptions removed at the server.", - count, - ) - % count - ) - else: - # Try to remove subscriptions defined by pool IDs first (remove --pool=...) - if self.options.pool_ids: - ( - removed_pools, - unremoved_pools, - removed_serials, - ) = ent_service.remove_entitlements_by_pool_ids(self.options.pool_ids) - if not removed_pools: - return_code = 1 - self._print_unbind_ids_result(removed_pools, unremoved_pools, "pools") - else: - removed_serials = [] - # Then try to remove subscriptions defined by serials (remove --serial=...) - unremoved_serials = [] - if self.options.serials: - serials = unique_list_items(self.options.serials) - # Don't remove serials already removed by a pool - serials_to_remove = [serial for serial in serials if serial not in removed_serials] - _removed_serials, unremoved_serials = ent_service.remove_entitlements_by_serials( - serials_to_remove - ) - removed_serials.extend(_removed_serials) - if not _removed_serials: - return_code = 1 - # Print final result of removing pools - self._print_unbind_ids_result(removed_serials, unremoved_serials, "serial numbers") - except connection.GoneException as ge: - raise ge - except connection.RestlibException as err: - log.error(err) - - system_exit(os.EX_SOFTWARE, err) - except Exception as e: - handle_exception( - _("Unable to perform remove due to the following exception: {e}").format(e=e), e - ) - else: - # We never got registered, just remove the cert - try: - if self.options.all: - total = 0 - for ent in self.entitlement_dir.list(): - ent.delete() - total = total + 1 - print( - ungettext( - "{total} subscription removed from this system.", - "{total} subscriptions removed from this system.", - total, - ).format(total=total) - ) - else: - if self.options.serials or self.options.pool_ids: - serials = self.options.serials or [] - pool_ids = self.options.pool_ids or [] - count = 0 - for ent in self.entitlement_dir.list(): - ent_pool_id = str(getattr(ent.pool, "id", None) or "") - if str(ent.serial) in serials or ent_pool_id in pool_ids: - ent.delete() - print( - _( - "Subscription with serial number {serial} removed from this system" - ).format(serial=str(ent.serial)) - ) - count = count + 1 - if count == 0: - return_code = 1 - except Exception as e: - handle_exception( - _("Unable to perform remove due to the following exception: {e}").format(e=e), e - ) - - # it is okay to call this no matter what happens above, - # it's just a notification to perform a check - self._request_validity_check() - return return_code diff --git a/src/subscription_manager/managercli.py b/src/subscription_manager/managercli.py index 64957ebc31..9c9772c9e7 100644 --- a/src/subscription_manager/managercli.py +++ b/src/subscription_manager/managercli.py @@ -23,9 +23,6 @@ from subscription_manager import managerlib from subscription_manager.cli import CLI -from subscription_manager.cli_command.addons import AddonsCommand -from subscription_manager.cli_command.attach import AttachCommand -from subscription_manager.cli_command.autoheal import AutohealCommand from subscription_manager.cli_command.clean import CleanCommand from subscription_manager.cli_command.config import ConfigCommand from subscription_manager.cli_command.environments import EnvironmentsCommand @@ -36,18 +33,13 @@ from subscription_manager.cli_command.override import OverrideCommand from subscription_manager.cli_command.owners import OwnersCommand from subscription_manager.cli_command.plugins import PluginsCommand -from subscription_manager.cli_command.redeem import RedeemCommand from subscription_manager.cli_command.refresh import RefreshCommand from subscription_manager.cli_command.register import RegisterCommand from subscription_manager.cli_command.release import ReleaseCommand -from subscription_manager.cli_command.remove import RemoveCommand from subscription_manager.cli_command.repos import ReposCommand -from subscription_manager.cli_command.role import RoleCommand -from subscription_manager.cli_command.service_level import ServiceLevelCommand from subscription_manager.cli_command.status import StatusCommand from subscription_manager.cli_command.syspurpose import SyspurposeCommand from subscription_manager.cli_command.unregister import UnRegisterCommand -from subscription_manager.cli_command.usage import UsageCommand from subscription_manager.cli_command.version import VersionCommand from subscription_manager.i18n import ugettext as _ from subscription_manager.repolib import YumPluginManager @@ -60,28 +52,20 @@ def __init__(self): commands: List[Type[CliCommand]] = [ RegisterCommand, UnRegisterCommand, - AddonsCommand, ConfigCommand, ListCommand, IdentityCommand, OwnersCommand, RefreshCommand, CleanCommand, - RedeemCommand, ReposCommand, ReleaseCommand, StatusCommand, EnvironmentsCommand, ImportCertCommand, - ServiceLevelCommand, VersionCommand, - RemoveCommand, - AttachCommand, PluginsCommand, - AutohealCommand, OverrideCommand, - RoleCommand, - UsageCommand, FactsCommand, SyspurposeCommand, ] diff --git a/src/subscription_manager/utils.py b/src/subscription_manager/utils.py index 31c6cbfab1..870da305af 100644 --- a/src/subscription_manager/utils.py +++ b/src/subscription_manager/utils.py @@ -203,7 +203,11 @@ def is_simple_content_access( if identity.uuid is None: return False content_access_mode = inj.require(inj.CONTENT_ACCESS_MODE_CACHE).read_data(uep=uep) - return content_access_mode == "org_environment" + + is_sca = content_access_mode == "org_environment" + if not is_sca: + raise RuntimeError("IS NOT SCA") + return is_sca def get_current_owner(uep: Optional["UEPConnection"] = None, identity: "Identity" = None) -> dict: