Skip to content

Commit 3ee1624

Browse files
authored
Add Hammer support for invalidating users JWTs (#17468)
1 parent ab543d6 commit 3ee1624

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

robottelo/cli/user.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,15 @@ def mail_notification_add(cls, options=None):
132132
"""
133133
cls.command_sub = 'mail-notification add'
134134
return cls.execute(cls._construct_command(options), output_format='csv')
135+
136+
@classmethod
137+
def invalidate(cls, options=None):
138+
"""Invalidate JWTs for a single user"""
139+
cls.command_sub = 'registration-tokens invalidate'
140+
return cls.execute(cls._construct_command(options))
141+
142+
@classmethod
143+
def invalidate_multiple(cls, options=None):
144+
"""Invalidate JWTs for multiple users"""
145+
cls.command_sub = 'registration-tokens invalidate-multiple'
146+
return cls.execute(cls._construct_command(options))

tests/foreman/cli/test_registration.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from fauxfactory import gen_mac, gen_string
2020
import pytest
21+
from wait_for import wait_for
2122

2223
from robottelo.config import settings
2324
from robottelo.constants import CLIENT_PORT
@@ -400,3 +401,84 @@ def test_positive_verify_default_location_for_registered_host(
400401
query={"search": f'name={rhel_contenthost.hostname}'}
401402
)[0]
402403
assert host.location.read().name == module_location.name
404+
405+
406+
@pytest.mark.no_containers
407+
@pytest.mark.rhel_ver_list([settings.content_host.default_rhel_version])
408+
def test_positive_invalidating_users_tokens(
409+
module_target_sat, rhel_contenthost, module_activation_key, module_org, request
410+
):
411+
"""Verify invalidating single and multiple users tokens.
412+
413+
:id: 5db602d4-9c57-4b70-8d46-5323044824e0
414+
415+
:steps:
416+
1. Create an admin user and a non-admin user with "edit_users" and "register_hosts" permission.
417+
2. Generate a token with admin user and register a host with it, it should be successful.
418+
3. Invalidate the token and try to use the generated token again to register the host, it should fail.
419+
4. Invalidate tokens for multiple users with "invalidate-multiple" command, it should invalidate all the tokens for provided users.
420+
5. Repeat Steps 2,3 and 4 with non-admin user and it should work the same way.
421+
422+
:expectedresults: Host registration will not be possible after/with invalidated tokens.
423+
424+
:CaseImportance: Critical
425+
426+
:Verifies: SAT-30385
427+
"""
428+
password = settings.server.admin_password
429+
admin_user = module_target_sat.api.User().search(
430+
query={'search': f'login={settings.server.admin_username}'}
431+
)[0]
432+
433+
# Non-Admin user with "edit_users" permission and "Register hosts" role
434+
non_admin_user = module_target_sat.api.User(
435+
login=gen_string('alpha'), password=password, organization=[module_org]
436+
).create()
437+
role = module_target_sat.cli_factory.make_role({'organization-id': module_org.id})
438+
module_target_sat.cli_factory.add_role_permissions(
439+
role.id,
440+
resource_permissions={'User': {'permissions': ['edit_users']}},
441+
)
442+
module_target_sat.cli.User.add_role({'id': non_admin_user.id, 'role-id': role.id})
443+
module_target_sat.cli.User.add_role({'id': non_admin_user.id, 'role': 'Register hosts'})
444+
445+
# delete the host and the user
446+
@request.addfinalizer
447+
def _finalize():
448+
wait_for(lambda: module_target_sat.cli.Host.delete({'name': rhel_contenthost.hostname}))
449+
module_target_sat.cli.User.delete({'login': non_admin_user.login})
450+
451+
# Generate token and verify token invalidation
452+
for usertype in (admin_user, non_admin_user):
453+
user = admin_user if usertype.admin else non_admin_user
454+
cmd = module_target_sat.cli.HostRegistration.with_user(
455+
user.login, password
456+
).generate_command(
457+
options={
458+
'activation-keys': module_activation_key.name,
459+
'insecure': 'true',
460+
'organization-id': module_org.id,
461+
}
462+
)
463+
result = rhel_contenthost.execute(cmd.strip('\n'))
464+
assert result.status == 0, f'Failed to register host: {result.stderr}'
465+
466+
# Invalidate JWTs for a single user
467+
result = module_target_sat.cli.User.with_user(user.login, password).invalidate(
468+
options={
469+
'user-id': user.id,
470+
}
471+
)
472+
assert f'Successfully invalidated registration tokens for {user.login}' in result
473+
474+
rhel_contenthost.unregister()
475+
# Re-register the host with invalidated token
476+
result = rhel_contenthost.execute(cmd.strip('\n'))
477+
assert result.status == 1
478+
assert 'ERROR: unauthorized' in result.stdout
479+
480+
# Invalidate JWTs for multiple users
481+
result = module_target_sat.cli.User.with_user(user.login, password).invalidate_multiple(
482+
options={'search': f"id ^ ({admin_user.id}, {non_admin_user.id})"}
483+
)
484+
assert 'Successfully invalidated registration tokens' in result

0 commit comments

Comments
 (0)