Skip to content

Commit a8bddf2

Browse files
authored
Merge pull request #116 from waveclaw/rhsm-config-help
Resolve #117 by filtering options
2 parents a1ced94 + 8f03efa commit a8bddf2

File tree

4 files changed

+148
-3
lines changed

4 files changed

+148
-3
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ available in current version of satellite. To use this module beyond version 5.6
3636
with a older Satellite installation, you may want to include the service in your
3737
own Puppet code. An example is provided for the `rhsmcertd` case bellow.
3838

39+
In Satellite 6.5.x and later, the remote command execution replaces this feature.
40+
This uses the ssh service as the remove agent and does require configuration of
41+
both the user and ssh configuration. This is a topic well discussed [elsewhere](https://www.ssh.org).
42+
3943
### Terminology
4044

4145
Due to various terminology differences between RHN Satellite, the upstream
@@ -281,6 +285,10 @@ rhsm_register { 'subscription.rhn.example.com':
281285
Please see man(5) RHSM.CONF for your locally supported options. There are quite
282286
a few and they require specific inputs.
283287

288+
rhsm_config cannot set values that are not available from the `subscription-manager config` sub-command.
289+
290+
For the rhsm.conf options that are not supported directly, it is recommended to use either the puppetlabs-stdlib `file_line` or one of the many forge.puppet.com modules for managing ini-format files.
291+
284292
##### rhsm_config options
285293

286294
See the documentation at [RedHat Support](https://access.redhat.com/documentation/en-US/Red_Hat_Subscription_Management/1/html/RHSM/rhsm-config.html#tab.rhsm.conf-parameters) for RedHat provided details on the `/etc/rhsm/rhsm.conf` file.
@@ -297,9 +305,13 @@ The most important settings are as follows. Specific support is made for them.
297305
Other options can be rolled into a configuration hash and fed to the module as a
298306
whole. See init.pp and the following YAML example for details.
299307

308+
Do know the supported options to the specific version of `subscription-manager config --help` on your platform.
309+
310+
***Options that are not supported will be ignored!***
311+
300312
##### rhsm_config Examples
301313

302-
As a resource.
314+
As a resource, the format of `[section]` and `section.key` is transformed into the puppet language compatible `section_key`.
303315

304316
```puppet
305317
rhsm_config { 'katello.example.com':

lib/puppet/provider/rhsm_config/subscription_manager.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,33 @@ def self.ini_parse(input)
223223
output
224224
end
225225

226+
# Obtain the list of parameters from the config help subcommand
227+
# @return Array(String) the options you can set though the cmd
228+
def config_help_options()
229+
data = subscription_manager(['config', '--help'])
230+
opts = conf_help_parse(data) unless data.nil?
231+
unless opts.nil?
232+
Puppet.debug("Valid config parameters were #{opts}")
233+
opts
234+
end
235+
end
236+
237+
# Parse the output of a subscription_mnager config command
238+
# @params data String the data from a command run
239+
# @return Array(String) a list of section.option that can be set
240+
def conf_help_parse(data)
241+
opts = []
242+
unless data.nil?
243+
data.split("\n").each do |line|
244+
m = line.match(%r{^\s+--([a-z_0-9]+\.[a-z_0-9]+) [A-Z]+.*})
245+
unless m.nil?
246+
opts.push m[1]
247+
end
248+
end
249+
end
250+
opts
251+
end
252+
226253
# Build a config option string
227254
# @param removal Symbol :remove if to remove things
228255
# @return [Hash(Array(String))] the options for a config command
@@ -231,16 +258,19 @@ def self.ini_parse(input)
231258
def build_config_parameters(removal)
232259
setparams = []
233260
removeparams = []
261+
cmdparams = config_help_options()
262+
# a map of parameter:section hence the 'reversed' nature of the bellow
234263
options = Puppet::Type.type(:rhsm_config).binary_options.merge(
235264
Puppet::Type.type(:rhsm_config).text_options,
236265
)
237266
# filter out praramters from properties
238-
arguments = @property_hash.select do |opt, _value|
267+
arguments = @property_hash.select do |opt, value|
239268
options.keys.include?(opt)
240269
end
241270
Puppet.debug("Updates to subscription configuration are '#{arguments}'")
242271
arguments.each do |opt, value|
243272
section = options[opt]
273+
next unless cmdparams.include? section
244274
param = resolve_value(removal, opt, value)
245275
if param.nil?
246276
removeparams.push("--remove=#{section}")

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "waveclaw-subscription_manager",
3-
"version": "5.6.5",
3+
"version": "5.7.0",
44
"author": "JD Powell <waveclaw@hotmail.com>",
55
"summary": "Manage Katello or Satellite registrations.",
66
"license": "Apache-2.0",

spec/unit/provider/rhsm_config/subscription_manager_spec.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@
142142
rhsm_baseurl: 'https://katello01.example.com/pulp/repos',
143143
}
144144

145+
raw_help_data = <<-EOH
146+
usage: subscription-manager config [OPTIONS]
147+
148+
List, set, or remove the configuration parameters in use by this system
149+
150+
optional arguments:
151+
-h, --help show this help message and exit
152+
--list list the configuration for this system
153+
--remove REMOVE remove configuration entry by section.name
154+
--server.proxy_scheme SERVER.PROXY_SCHEME
155+
Section: server, Name: proxy_scheme
156+
--server.server_timeout SERVER.SERVER_TIMEOUT
157+
Section: server, Name: server_timeout
158+
--server.proxy_hostname SERVER.PROXY_HOSTNAME
159+
Section: server, Name: proxy_hostname
160+
--server.no_proxy SERVER.NO_PROXY
161+
Section: server, Name: no_proxy
162+
--server.insecure SERVER.INSECURE
163+
Section: server, Name: insecure
164+
EOH
165+
166+
help_values = [
167+
"server.proxy_scheme",
168+
"server.server_timeout",
169+
"server.proxy_hostname",
170+
"server.no_proxy",
171+
"server.insecure"
172+
]
173+
145174
let(:resource) do
146175
Puppet::Type.type(:rhsm_config).new(properties)
147176
end
@@ -241,6 +270,9 @@
241270
res.provider.set(server_port: 443)
242271
res[:server_port] = 8080
243272
expect(res.provider).to receive(:exists?).and_call_original
273+
expect(res.provider).to receive(:config_help_options).and_return([
274+
'server.port','server.insecure'
275+
])
244276
expect(res.provider).to receive(:build_config_parameters).with(:remove).and_call_original
245277
expect(res.provider).to receive(:subscription_manager).with(
246278
'config', '--remove=server.insecure'
@@ -452,11 +484,53 @@
452484
end
453485
end
454486

487+
describe 'config_help_options' do
488+
it 'returns nothing for an empty configuration' do
489+
resource = Puppet::Type.type(:rhsm_config).new(
490+
provider: provider, name: title,
491+
)
492+
expect(provider.class).to receive(:subscription_manager).with(['config', '--help']).and_return('')
493+
expect(resource.provider.config_help_options()).to eq([])
494+
end
495+
it 'returns expected values for a given configuration' do
496+
resource = Puppet::Type.type(:rhsm_config).new(
497+
provider: provider, name: title,
498+
)
499+
expect(provider.class).to receive(:subscription_manager).with(['config', '--help']).and_return(raw_help_data)
500+
expect(resource.provider).to receive(:conf_help_parse).and_return(help_values)
501+
expect(resource.provider.config_help_options()).to eq(help_values)
502+
end
503+
end
504+
505+
describe 'conf_help_parse' do
506+
it 'returns nothing for an empty configuration' do
507+
resource = Puppet::Type.type(:rhsm_config).new(
508+
provider: provider, name: title,
509+
)
510+
expect(resource.provider.conf_help_parse('')).to eq([])
511+
end
512+
it 'returns nothing for garbage' do
513+
resource = Puppet::Type.type(:rhsm_config).new(
514+
provider: provider, name: title,
515+
)
516+
expect(resource.provider.conf_help_parse('asdlk;j12349567[[]]')).to eq([])
517+
end
518+
help_values.each do |key|
519+
it "parse the #{key} option" do
520+
resource = Puppet::Type.type(:rhsm_config).new(
521+
provider: provider, name: title,
522+
)
523+
expect(resource.provider.conf_help_parse(raw_help_data)).to include(key)
524+
end
525+
end
526+
end
527+
455528
describe 'build_config_parameters' do
456529
it 'returns nothing when provider or title are the only parameters' do
457530
resource = Puppet::Type.type(:rhsm_config).new(
458531
provider: provider, name: title,
459532
)
533+
expect(resource.provider).to receive(:config_help_options).and_return(nil)
460534
expect(resource.provider.build_config_parameters(:apply)).to eq(
461535
apply: nil, remove: nil,
462536
)
@@ -474,6 +548,9 @@
474548
expect(resource.provider).to receive(:resolve_value).with(:remove, :server_insecure, '').and_return(nil)
475549
expect(resource.provider).to receive(:resolve_value).with(:remove, :server_port, '').and_return(nil)
476550
expect(resource.provider).to receive(:resolve_value).with(:remove, :rhsm_ca_cert_dir, '').and_return(nil)
551+
expect(resource.provider).to receive(:config_help_options).and_return([
552+
'server.insecure', 'server.port', 'rhsm.ca_cert_dir'
553+
])
477554
result = resource.provider.build_config_parameters(:remove)
478555
expect(result).not_to eq(nil)
479556
expect(result.keys.sort).to eq([:apply, :remove])
@@ -497,17 +574,21 @@
497574
binary_opt = Puppet::Type.type(:rhsm_config).binary_options[key]
498575
value = (properties[key] == true) ? 1 : 0
499576
expect(resource.provider).to receive(:resolve_value).and_return(value)
577+
expect(resource.provider).to receive(:config_help_options).and_return([binary_opt])
500578
expect(resource.provider.build_config_parameters(:apply)[:apply]).to eq([
501579
"--#{binary_opt}=#{value}",
502580
])
503581
expect(resource.provider).to receive(:resolve_value).and_return(nil)
582+
expect(resource.provider).to receive(:config_help_options).and_return([binary_opt])
504583
expect(resource.provider.build_config_parameters(:remove)[:remove]).to eq(["--remove=#{binary_opt}"])
505584
else
506585
text_opt = resource.class.text_options[key]
507586
value = properties[key].to_s
508587
expect(resource.provider).to receive(:resolve_value).and_call_original
588+
expect(resource.provider).to receive(:config_help_options).and_return([text_opt])
509589
expect(resource.provider.build_config_parameters(:apply)[:apply]).to eq(["--#{text_opt}=#{value}"])
510590
expect(resource.provider).to receive(:resolve_value).and_return(nil)
591+
expect(resource.provider).to receive(:config_help_options).and_return([text_opt])
511592
expect(resource.provider.build_config_parameters(:remove)[:remove]).to eq(["--remove=#{text_opt}"])
512593
end
513594
end
@@ -521,6 +602,9 @@
521602
resource[:server_insecure] = 'true'
522603
resource.provider.set(server_port: nil)
523604
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
605+
expect(resource.provider).to receive(:config_help_options).and_return([
606+
'rhsm.ca_cert_dir', 'server.insecure', 'server.port'
607+
])
524608
combo = resource.provider.build_config_parameters(:apply)
525609
apply_expected = ['--rhsm.ca_cert_dir=/etc/rhsm/ca/',
526610
'--server.insecure=0'].sort
@@ -535,11 +619,17 @@
535619
resource.provider.set(server_insecure: false)
536620
resource.provider.set(server_port: 443)
537621
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
622+
expect(resource.provider).to receive(:config_help_options).and_return([
623+
'server.port','rhsm.ca_cert_dir','server.insecure'
624+
])
538625
apply = resource.provider.build_config_parameters(:apply)
539626
expect(apply[:apply].sort!).to eq([
540627
'--server.port=443', '--rhsm.ca_cert_dir=/etc/rhsm/ca/', '--server.insecure=0'
541628
].sort!)
542629
expect(apply[:remove]).to eq(nil)
630+
expect(resource.provider).to receive(:config_help_options).and_return([
631+
'server.port', 'rhsm.ca_cert_dir', 'server.insecure'
632+
])
543633
remove = resource.provider.build_config_parameters(:remove)
544634
expect(remove[:apply]).to eq(nil)
545635
expect(remove[:remove].sort!).to eq([
@@ -554,8 +644,21 @@
554644
resource.provider.set(server_port: 443)
555645
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
556646
resource.provider.class.defaults_to = [:server_port]
647+
expect(resource.provider).to receive(:config_help_options).and_return(['server.insecure'])
557648
apply = resource.provider.build_config_parameters(:apply)[:apply]
558649
expect(apply).to include('--server.insecure=0')
559650
end
651+
it 'does skip unsupported options' do
652+
resource = Puppet::Type.type(:rhsm_config).new(
653+
provider: provider, name: title,
654+
)
655+
resource.provider.set(server_insecure: false)
656+
resource.provider.set(server_port: 443)
657+
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
658+
resource.provider.class.defaults_to = [:server_port]
659+
expect(resource.provider).to receive(:config_help_options).and_return([])
660+
apply = resource.provider.build_config_parameters(:apply)[:apply]
661+
expect(apply).to eq(nil)
662+
end
560663
end
561664
end

0 commit comments

Comments
 (0)