Skip to content

Commit

Permalink
feat: Enable register with environment names and environment types
Browse files Browse the repository at this point in the history
* Allow to register also with environment names. It is possible
  to use environment names with username & password and
  activation-keys & organization authentication
* It is not allowed to use `environmets` and `environment_names`
  registration options together, because it is not possible to use
  env. IDs and names together on candlepin server
* Introduce environment_type as another registration_option
  * When consumer object is returned and it contains environments,
    then all environments are checked if type of environment
    matches given environment_type. If type is missing or is
    different, then system is unregistered and exception is
    raised
* Modified few unit tests related to environments
* Added few unit tests
  • Loading branch information
jirihnidek committed Dec 19, 2024
1 parent 5eee229 commit 5ba01cf
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/rhsm/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,7 @@ def registerConsumer(
facts: Optional[dict] = None,
owner: str = None,
environments: str = None,
environment_names: str = None,
keys: str = None,
installed_products: list = None,
uuid: str = None,
Expand Down Expand Up @@ -1581,6 +1582,11 @@ def registerConsumer(
for environment in environments.split(","):
env_list.append({"id": environment})
params["environments"] = env_list
elif environment_names is not None and self.has_capability(MULTI_ENV):
env_name_list = []
for env_name in environment_names.split(","):
env_name_list.append({"name": env_name})
params["environments"] = env_name_list

headers = {}
if jwt_token:
Expand Down
37 changes: 37 additions & 0 deletions src/rhsmlib/services/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def register(
org: Optional[str],
activation_keys: list = None,
environments: list = None,
environment_names: list = None,
environment_type: str = None,
force: bool = False,
name: str = None,
consumerid: str = None,
Expand All @@ -66,6 +68,11 @@ def register(
if kwargs:
raise exceptions.ValidationError(_("Unknown arguments: %s") % kwargs.keys())

if environments is not None and environment_names is not None:
raise exceptions.ValidationError(
_("Environment IDs and environment names are mutually exclusive")
)

syspurpose = syspurposelib.read_syspurpose()

save_syspurpose = False
Expand Down Expand Up @@ -95,6 +102,7 @@ def register(
options = {
"activation_keys": activation_keys,
"environments": environments,
"environment_names": environment_names,
"force": force,
"name": name,
"consumerid": consumerid,
Expand Down Expand Up @@ -123,6 +131,7 @@ def register(
facts=facts_dict,
owner=org,
environments=environments,
environment_names=environment_names,
keys=options.get("activation_keys"),
installed_products=self.installed_mgr.format_for_server(),
content_tags=self.installed_mgr.tags,
Expand All @@ -138,8 +147,36 @@ def register(
cp_provider: CPProvider = inj.require(inj.CP_PROVIDER)
cp_provider.close_all_connections()

# If environment type was specified, then check that all returned
# environments have required type. Otherwise, raise exception
wrong_type = False
wrong_env_names = []
if environment_type is not None:
for environment in consumer.get("environments", []):
env_type = environment.get("type", None)
if env_type != environment_type:
log.error(
f"environment: '{environment["name"]}' does not have required type: '{environment_type},"
f" it has '{env_type}' type"
)
wrong_type = True
wrong_env_names.append(environment["name"])

managerlib.persist_consumer_cert(consumer)

if wrong_type is True:
# We will not use this consumer object. Thus, delete this object
# on the server
self.identity.reload()
UnregisterService(inj.require(inj.CP_PROVIDER).get_consumer_auth_cp()).unregister()
raise exceptions.ServiceError(
_(
"Environments: '{env_names}' do not have required type '{environment_type}'".format(
env_names=", ".join(wrong_env_names), environment_type=environment_type
)
)
)

access_mode: str = consumer.get("owner", {}).get("contentAccessMode", "unknown")
if access_mode != "org_environment":
log.error(
Expand Down
Loading

0 comments on commit 5ba01cf

Please sign in to comment.