Skip to content

Commit

Permalink
Merge pull request #116 from waveclaw/rhsm-config-help
Browse files Browse the repository at this point in the history
Resolve #117 by filtering options
  • Loading branch information
waveclaw authored May 6, 2022
2 parents a1ced94 + 8f03efa commit a8bddf2
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ available in current version of satellite. To use this module beyond version 5.6
with a older Satellite installation, you may want to include the service in your
own Puppet code. An example is provided for the `rhsmcertd` case bellow.

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

### Terminology

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

rhsm_config cannot set values that are not available from the `subscription-manager config` sub-command.

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.

##### rhsm_config options

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.
Expand All @@ -297,9 +305,13 @@ The most important settings are as follows. Specific support is made for them.
Other options can be rolled into a configuration hash and fed to the module as a
whole. See init.pp and the following YAML example for details.

Do know the supported options to the specific version of `subscription-manager config --help` on your platform.

***Options that are not supported will be ignored!***

##### rhsm_config Examples

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

```puppet
rhsm_config { 'katello.example.com':
Expand Down
32 changes: 31 additions & 1 deletion lib/puppet/provider/rhsm_config/subscription_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,33 @@ def self.ini_parse(input)
output
end

# Obtain the list of parameters from the config help subcommand
# @return Array(String) the options you can set though the cmd
def config_help_options()
data = subscription_manager(['config', '--help'])
opts = conf_help_parse(data) unless data.nil?
unless opts.nil?
Puppet.debug("Valid config parameters were #{opts}")
opts
end
end

# Parse the output of a subscription_mnager config command
# @params data String the data from a command run
# @return Array(String) a list of section.option that can be set
def conf_help_parse(data)
opts = []
unless data.nil?
data.split("\n").each do |line|
m = line.match(%r{^\s+--([a-z_0-9]+\.[a-z_0-9]+) [A-Z]+.*})
unless m.nil?
opts.push m[1]
end
end
end
opts
end

# Build a config option string
# @param removal Symbol :remove if to remove things
# @return [Hash(Array(String))] the options for a config command
Expand All @@ -231,16 +258,19 @@ def self.ini_parse(input)
def build_config_parameters(removal)
setparams = []
removeparams = []
cmdparams = config_help_options()
# a map of parameter:section hence the 'reversed' nature of the bellow
options = Puppet::Type.type(:rhsm_config).binary_options.merge(
Puppet::Type.type(:rhsm_config).text_options,
)
# filter out praramters from properties
arguments = @property_hash.select do |opt, _value|
arguments = @property_hash.select do |opt, value|
options.keys.include?(opt)
end
Puppet.debug("Updates to subscription configuration are '#{arguments}'")
arguments.each do |opt, value|
section = options[opt]
next unless cmdparams.include? section
param = resolve_value(removal, opt, value)
if param.nil?
removeparams.push("--remove=#{section}")
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waveclaw-subscription_manager",
"version": "5.6.5",
"version": "5.7.0",
"author": "JD Powell <[email protected]>",
"summary": "Manage Katello or Satellite registrations.",
"license": "Apache-2.0",
Expand Down
103 changes: 103 additions & 0 deletions spec/unit/provider/rhsm_config/subscription_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,35 @@
rhsm_baseurl: 'https://katello01.example.com/pulp/repos',
}

raw_help_data = <<-EOH
usage: subscription-manager config [OPTIONS]
List, set, or remove the configuration parameters in use by this system
optional arguments:
-h, --help show this help message and exit
--list list the configuration for this system
--remove REMOVE remove configuration entry by section.name
--server.proxy_scheme SERVER.PROXY_SCHEME
Section: server, Name: proxy_scheme
--server.server_timeout SERVER.SERVER_TIMEOUT
Section: server, Name: server_timeout
--server.proxy_hostname SERVER.PROXY_HOSTNAME
Section: server, Name: proxy_hostname
--server.no_proxy SERVER.NO_PROXY
Section: server, Name: no_proxy
--server.insecure SERVER.INSECURE
Section: server, Name: insecure
EOH

help_values = [
"server.proxy_scheme",
"server.server_timeout",
"server.proxy_hostname",
"server.no_proxy",
"server.insecure"
]

let(:resource) do
Puppet::Type.type(:rhsm_config).new(properties)
end
Expand Down Expand Up @@ -241,6 +270,9 @@
res.provider.set(server_port: 443)
res[:server_port] = 8080
expect(res.provider).to receive(:exists?).and_call_original
expect(res.provider).to receive(:config_help_options).and_return([
'server.port','server.insecure'
])
expect(res.provider).to receive(:build_config_parameters).with(:remove).and_call_original
expect(res.provider).to receive(:subscription_manager).with(
'config', '--remove=server.insecure'
Expand Down Expand Up @@ -452,11 +484,53 @@
end
end

describe 'config_help_options' do
it 'returns nothing for an empty configuration' do
resource = Puppet::Type.type(:rhsm_config).new(
provider: provider, name: title,
)
expect(provider.class).to receive(:subscription_manager).with(['config', '--help']).and_return('')
expect(resource.provider.config_help_options()).to eq([])
end
it 'returns expected values for a given configuration' do
resource = Puppet::Type.type(:rhsm_config).new(
provider: provider, name: title,
)
expect(provider.class).to receive(:subscription_manager).with(['config', '--help']).and_return(raw_help_data)
expect(resource.provider).to receive(:conf_help_parse).and_return(help_values)
expect(resource.provider.config_help_options()).to eq(help_values)
end
end

describe 'conf_help_parse' do
it 'returns nothing for an empty configuration' do
resource = Puppet::Type.type(:rhsm_config).new(
provider: provider, name: title,
)
expect(resource.provider.conf_help_parse('')).to eq([])
end
it 'returns nothing for garbage' do
resource = Puppet::Type.type(:rhsm_config).new(
provider: provider, name: title,
)
expect(resource.provider.conf_help_parse('asdlk;j12349567[[]]')).to eq([])
end
help_values.each do |key|
it "parse the #{key} option" do
resource = Puppet::Type.type(:rhsm_config).new(
provider: provider, name: title,
)
expect(resource.provider.conf_help_parse(raw_help_data)).to include(key)
end
end
end

describe 'build_config_parameters' do
it 'returns nothing when provider or title are the only parameters' do
resource = Puppet::Type.type(:rhsm_config).new(
provider: provider, name: title,
)
expect(resource.provider).to receive(:config_help_options).and_return(nil)
expect(resource.provider.build_config_parameters(:apply)).to eq(
apply: nil, remove: nil,
)
Expand All @@ -474,6 +548,9 @@
expect(resource.provider).to receive(:resolve_value).with(:remove, :server_insecure, '').and_return(nil)
expect(resource.provider).to receive(:resolve_value).with(:remove, :server_port, '').and_return(nil)
expect(resource.provider).to receive(:resolve_value).with(:remove, :rhsm_ca_cert_dir, '').and_return(nil)
expect(resource.provider).to receive(:config_help_options).and_return([
'server.insecure', 'server.port', 'rhsm.ca_cert_dir'
])
result = resource.provider.build_config_parameters(:remove)
expect(result).not_to eq(nil)
expect(result.keys.sort).to eq([:apply, :remove])
Expand All @@ -497,17 +574,21 @@
binary_opt = Puppet::Type.type(:rhsm_config).binary_options[key]
value = (properties[key] == true) ? 1 : 0
expect(resource.provider).to receive(:resolve_value).and_return(value)
expect(resource.provider).to receive(:config_help_options).and_return([binary_opt])
expect(resource.provider.build_config_parameters(:apply)[:apply]).to eq([
"--#{binary_opt}=#{value}",
])
expect(resource.provider).to receive(:resolve_value).and_return(nil)
expect(resource.provider).to receive(:config_help_options).and_return([binary_opt])
expect(resource.provider.build_config_parameters(:remove)[:remove]).to eq(["--remove=#{binary_opt}"])
else
text_opt = resource.class.text_options[key]
value = properties[key].to_s
expect(resource.provider).to receive(:resolve_value).and_call_original
expect(resource.provider).to receive(:config_help_options).and_return([text_opt])
expect(resource.provider.build_config_parameters(:apply)[:apply]).to eq(["--#{text_opt}=#{value}"])
expect(resource.provider).to receive(:resolve_value).and_return(nil)
expect(resource.provider).to receive(:config_help_options).and_return([text_opt])
expect(resource.provider.build_config_parameters(:remove)[:remove]).to eq(["--remove=#{text_opt}"])
end
end
Expand All @@ -521,6 +602,9 @@
resource[:server_insecure] = 'true'
resource.provider.set(server_port: nil)
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
expect(resource.provider).to receive(:config_help_options).and_return([
'rhsm.ca_cert_dir', 'server.insecure', 'server.port'
])
combo = resource.provider.build_config_parameters(:apply)
apply_expected = ['--rhsm.ca_cert_dir=/etc/rhsm/ca/',
'--server.insecure=0'].sort
Expand All @@ -535,11 +619,17 @@
resource.provider.set(server_insecure: false)
resource.provider.set(server_port: 443)
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
expect(resource.provider).to receive(:config_help_options).and_return([
'server.port','rhsm.ca_cert_dir','server.insecure'
])
apply = resource.provider.build_config_parameters(:apply)
expect(apply[:apply].sort!).to eq([
'--server.port=443', '--rhsm.ca_cert_dir=/etc/rhsm/ca/', '--server.insecure=0'
].sort!)
expect(apply[:remove]).to eq(nil)
expect(resource.provider).to receive(:config_help_options).and_return([
'server.port', 'rhsm.ca_cert_dir', 'server.insecure'
])
remove = resource.provider.build_config_parameters(:remove)
expect(remove[:apply]).to eq(nil)
expect(remove[:remove].sort!).to eq([
Expand All @@ -554,8 +644,21 @@
resource.provider.set(server_port: 443)
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
resource.provider.class.defaults_to = [:server_port]
expect(resource.provider).to receive(:config_help_options).and_return(['server.insecure'])
apply = resource.provider.build_config_parameters(:apply)[:apply]
expect(apply).to include('--server.insecure=0')
end
it 'does skip unsupported options' do
resource = Puppet::Type.type(:rhsm_config).new(
provider: provider, name: title,
)
resource.provider.set(server_insecure: false)
resource.provider.set(server_port: 443)
resource.provider.set(rhsm_ca_cert_dir: '/etc/rhsm/ca/')
resource.provider.class.defaults_to = [:server_port]
expect(resource.provider).to receive(:config_help_options).and_return([])
apply = resource.provider.build_config_parameters(:apply)[:apply]
expect(apply).to eq(nil)
end
end
end

1 comment on commit a8bddf2

@vchepkov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@waveclaw , could you, please, tag v5.6.5 ?

Please sign in to comment.