Skip to content

Commit

Permalink
Add support for Red Hat API token
Browse files Browse the repository at this point in the history
fix mixed up

fix version
  • Loading branch information
ecchong committed Jan 4, 2023
1 parent 84ebda6 commit 7435bfd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- redhat_subscription - adds ``token`` parameter for subscription-manager authentication using Red Hat API token (https://github.com/ansible-collections/community.general/pull/5725).
24 changes: 18 additions & 6 deletions plugins/modules/redhat_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
description:
- access.redhat.com or Red Hat Satellite or Katello password
type: str
token:
description:
- sso.redhat.com API access token.
type: str
version_added: 6.3.0
server_hostname:
description:
- Specify an alternative Red Hat Subscription Management or Red Hat Satellite or Katello server
Expand Down Expand Up @@ -294,10 +299,11 @@ class RegistrationBase(object):

REDHAT_REPO = "/etc/yum.repos.d/redhat.repo"

def __init__(self, module, username=None, password=None):
def __init__(self, module, username=None, password=None, token=None):
self.module = module
self.username = username
self.password = password
self.token = token

def configure(self):
raise NotImplementedError("Must be implemented by a sub-class")
Expand Down Expand Up @@ -340,8 +346,8 @@ def subscribe(self, **kwargs):


class Rhsm(RegistrationBase):
def __init__(self, module, username=None, password=None):
RegistrationBase.__init__(self, module, username, password)
def __init__(self, module, username=None, password=None, token=None):
RegistrationBase.__init__(self, module, username, password, token)
self.module = module

def enable(self):
Expand Down Expand Up @@ -397,7 +403,7 @@ def is_registered(self):
else:
return False

def register(self, username, password, auto_attach, activationkey, org_id,
def register(self, username, password, token, auto_attach, activationkey, org_id,
consumer_type, consumer_name, consumer_id, force_register, environment,
release):
'''
Expand Down Expand Up @@ -433,6 +439,8 @@ def register(self, username, password, auto_attach, activationkey, org_id,

if activationkey:
args.extend(['--activationkey', activationkey])
elif token:
args.extend(['--token', token])
else:
if username:
args.extend(['--username', username])
Expand Down Expand Up @@ -794,6 +802,7 @@ def main():
'state': {'default': 'present', 'choices': ['present', 'absent']},
'username': {},
'password': {'no_log': True},
'token': {'no_log': True},
'server_hostname': {},
'server_insecure': {},
'server_prefix': {},
Expand Down Expand Up @@ -831,17 +840,20 @@ def main():
['server_proxy_hostname', 'server_proxy_port'],
['server_proxy_user', 'server_proxy_password']],
mutually_exclusive=[['activationkey', 'username'],
['activationkey', 'token'],
['token', 'username'],
['activationkey', 'consumer_id'],
['activationkey', 'environment'],
['activationkey', 'auto_attach'],
['pool', 'pool_ids']],
required_if=[['state', 'present', ['username', 'activationkey'], True]],
required_if=[['state', 'present', ['username', 'activationkey', 'token'], True]],
)

rhsm.module = module
state = module.params['state']
username = module.params['username']
password = module.params['password']
token = module.params['token']
server_hostname = module.params['server_hostname']
server_insecure = module.params['server_insecure']
server_prefix = module.params['server_prefix']
Expand Down Expand Up @@ -914,7 +926,7 @@ def main():
try:
rhsm.enable()
rhsm.configure(**module.params)
rhsm.register(username, password, auto_attach, activationkey, org_id,
rhsm.register(username, password, token, auto_attach, activationkey, org_id,
consumer_type, consumer_name, consumer_id, force_register,
environment, release)
if syspurpose and 'sync' in syspurpose and syspurpose['sync'] is True:
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/plugins/modules/test_redhat_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,37 @@ def test_without_required_parameters(capfd, patch_redhat_subscription):
'msg': "System successfully registered to 'satellite.company.com'."
}
],
# Test simple registration using token
[
{
'state': 'present',
'server_hostname': 'satellite.company.com',
'token': 'fake_token',
},
{
'id': 'test_registeration_token',
'run_command.calls': [
(
['/testbin/subscription-manager', 'identity'],
{'check_rc': False},
(1, '', '')
),
(
['/testbin/subscription-manager', 'config', '--server.hostname=satellite.company.com'],
{'check_rc': True},
(0, '', '')
),
(
['/testbin/subscription-manager', 'register',
'--token', 'fake_token'],
{'check_rc': True, 'expand_user_and_vars': False},
(0, '', '')
)
],
'changed': True,
'msg': "System successfully registered to 'satellite.company.com'."
}
],
# Test unregistration, when system is unregistered
[
{
Expand Down

0 comments on commit 7435bfd

Please sign in to comment.