Bug 2174887

Summary: Environment module does not accept values with white-space when registering system in interactive mode
Product: Red Hat Enterprise Linux 8 Reporter: Rehana <redakkan>
Component: subscription-managerAssignee: Pino Toscano <ptoscano>
Status: CLOSED ERRATA QA Contact: Red Hat subscription-manager QE Team <rhsm-qe>
Severity: high Docs Contact:
Priority: medium    
Version: 8.8CC: arpandey, jstavel, mhorky, ptoscano, redakkan, rhsm-qe
Target Milestone: rcKeywords: Triaged
Target Release: 8.9   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: subscription-manager-1.28.37-1.el8 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: 2174297 Environment:
Last Closed: 2023-11-14 15:47:57 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 2174297    
Bug Blocks:    

Description Rehana 2023-03-02 14:42:34 UTC
+++ This bug was initially created as a clone of Bug #2174297 +++

Description of problem: when registering the system in interactive mode to a server with multi-environment capability, environment module does not accept values with spaces


Version-Release number of selected component (if applicable):
[root@kvm-03-guest14 ~]# rpm -qa subscription-manager
subscription-manager-1.29.33-1.el9.x86_64

How reproducible:
Below is demonstration of the issue-
[root@kvm-03-guest14 ~]# subscription-manager version
server type: This system is currently not registered.
subscription management server: 4.2.15-1
subscription management rules: 5.43
subscription-manager: 1.29.33-1.el9
[root@kvm-03-guest14 ~]#

[root@kvm-03-guest14 ~]# subscription-manager register --username testuser1 --password ***** --org *******
Registering to: archana-candlepin.usersys.redhat.com:8443/candlepin
Hint: Organization "******" contains following environments: DevEnv, Test Env1
Environments: Test Env1
No such environment: TestEnv1   <<<<  it simply removes the space from value provided
[root@kvm-03-guest14 ~]# 
[root@kvm-03-guest14 ~]# subscription-manager register --username testuser1 --password ****** --org ****
Registering to: archana-candlepin.usersys.redhat.com:8443/candlepin
Hint: Organization "*******" contains following environments: DevEnv, Test Env1
Environments: "Test\ Env1"
No such environment: "Test\Env1"    <<<<  it simply removes the space from value provided
[root@kvm-03-guest14 ~]



Actual results:
spaces in the environment value is getting removed and hence registration fails for wrong value

Expected results:
only preceding and ending whitespaces should be removed

Additional info:

it works fine for cli non interactive mode-
[root@kvm-03-guest14 ~]# subscription-manager register --username testuser1 --password ****** --org **** --environments "Test Env1"
Registering to: archana-candlepin.usersys.redhat.com:8443/candlepin
The system has been registered with ID: 75aa3d13-7af1-4a3f-8c7d-f6ad602a258e
The registered system name is: kvm-03-guest14.hv2.lab.eng.bos.redhat.com

--- Additional comment from Pino Toscano on 2023-03-02 10:50:00 UTC ---

(In reply to Archana Pandey from comment #0)
> [root@kvm-03-guest14 ~]# subscription-manager register --username testuser1
> --password ***** --org *******
> Registering to: archana-candlepin.usersys.redhat.com:8443/candlepin
> Hint: Organization "******" contains following environments: DevEnv, Test
> Env1
> Environments: Test Env1
> No such environment: TestEnv1   <<<<  it simply removes the space from value
> provided

Typing the environments like this (environment names as-they-are) is correct.

> [root@kvm-03-guest14 ~]# subscription-manager register --username testuser1
> --password ****** --org ****
> Registering to: archana-candlepin.usersys.redhat.com:8443/candlepin
> Hint: Organization "*******" contains following environments: DevEnv, Test
> Env1
> Environments: "Test\ Env1"
> No such environment: "Test\Env1"    <<<<  it simply removes the space from
> value provided

This is not correct, as there is no unquoting or unescaping done.

The problem happens only when asking the user for environments:
https://github.com/candlepin/subscription-manager/blob/82a44c368f4f83c32d1933b774d731a5c341e9ef/src/subscription_manager/cli_command/register.py#L441-L444
Tracking back the history of this code, we have (ignoring no-op commits):
- https://github.com/candlepin/subscription-manager/commit/7d0199fb9419daad10530ad87cb2ef7fdae37a62
  this re-adds support for non-multi-environments scenarios
- https://github.com/candlepin/subscription-manager/commit/19ae947ad78a517d8ba999fdc41b62dcd850dbad
  this adds support for multiple environments: note that the `strip()` for non-multi-environments (i.e. only trailing and leading whitespace characters are removed) becomes `replace(" ", "")` (i.e. every single space is removed)
(both the commit above are part of https://github.com/candlepin/subscription-manager/pull/2920 )
- https://github.com/candlepin/subscription-manager/commit/69c4921f4fd9915e19317af44c4bf72cc5bf72e1
  (part of https://github.com/candlepin/subscription-manager/pull/989 )
  this is introduces the interactive query for an environment, doing the `strip()` in the input (unlike what other `input()` prompts do)

My notes:
- I'm not sure why the input is `strip()`ed for non-multi-environment input; the original commit doesn't say that
- I think the logic in removing spaces in the multi-environment case was done to mimic the non-multi-environment one, sadly in a sub-optimal way which creates this issue

The user may type any arbitrary amount of environments to register to when prompted interactively, so I think that the logic to strip spaces is most likely still needed: an input string "Env 1, Env2,MyEnv" must result in "Env 1", "Env2", "MyEnv" (and not as "Env 1", " Env2", "MyEnv"). Looking at the code:
- the result of `RegisterCommand._prompt_for_environment()` is passed directly to `check_set_environment_names()`
- `check_set_environment_names()` does `strip()` on the split elements of an environment string:
  https://github.com/candlepin/subscription-manager/blob/82a44c368f4f83c32d1933b774d731a5c341e9ef/src/subscription_manager/cli_command/environments.py#L196-L203

Hence, I think we can safely do a single `strip()` on the input of `RegisterCommand._prompt_for_environment()`, in both multi-environments and non-multi-environments scenarios: this way, empty or whitespace-only inputs are rejected, prompting the user again.
Attempt of PR for this: https://github.com/candlepin/subscription-manager/pull/3219

Comment 1 Jan Stavel 2023-04-19 12:41:16 UTC
TASK [The First Environment] **************************************************************************************************************************************************************************************
changed: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com]

TASK [The Second Environment] *************************************************************************************************************************************************************************************
changed: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com]

TASK [List of the Available Environments] *************************************************************************************************************************************************************************
ok: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com] => 
  msg: |-
    +-------------------------------------------+
              Environments
    +-------------------------------------------+
    Name:        JStavel Env 01
    Description: new env01
  
    Name:        JStavel Env 02
    Description: Jan Stavel Env 02

TASK [Register a system with environment having space inside a name] **********************************************************************************************************************************************
changed: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com]

TASK [Output from register command] *******************************************************************************************************************************************************************************
ok: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com] => 
  msg: |-
    Registering to: archana-candlepin.usersys.redhat.com:8443/candlepin
    Hint: Organization "admin" contains following environments: JStavel Env 01, JStavel Env 02
    JStavel Env 01
    Environments: JStavel Env 01
    JStavel Env 01
    The system has been registered with ID: edc3e3f5-0a60-49e5-b831-27464c16cf27
    The registered system name is: kvm-04-guest16.hv2.lab.eng.bos.redhat.com

TASK [List of enabled environments] *******************************************************************************************************************************************************************************
changed: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com]

TASK [Enabled Environment should have space inside] ***************************************************************************************************************************************************************
ok: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com] => 
  msg: |-
    +-------------------------------------------+
              Environments
    +-------------------------------------------+
    Name:        JStavel Env 01
    Description: new env01

TASK [Version of the package subscription-manager] ****************************************************************************************************************************************************************
ok: [kvm-04-guest16.hv2.lab.eng.bos.redhat.com] => 
  msg: |-
    server type: Red Hat Subscription Management
    subscription management server: 4.3.1-1
    subscription management rules: 5.43
    subscription-manager: 1.28.36+26.gcc438fc3e-1.git.0.729c474

Comment 7 errata-xmlrpc 2023-11-14 15:47:57 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (subscription-manager bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2023:7092