Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add params and content method to parse_config #876

Merged
merged 1 commit into from
Aug 5, 2016

Conversation

chris-rock
Copy link
Contributor

This PR allows users to access the content easier:

  • parse_config_file('/etc/sysctl.conf', options).params['kernel.domainname']
  • parse_config_file('/etc/sysctl.conf', options).params
  • parse_config_file('/etc/sysctl.conf', options).content
Welcome to the interactive InSpec Shell
To find out how to use it, type: help

inspec> parse_config_file('/etc/sysctl.conf', options).params['kernel.domainname']
NameError: undefined local variable or method `options' for #<#<Class:0x007fec28acf910>:0x007fec28acf4d8>
from (pry):1:in `load'
inspec> options = {
inspec>   assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/  
inspec> }  
=> {:assignment_re=>/^\s*([^=]*?)\s*=\s*(.*?)\s*$/}
inspec> parse_config_file('/etc/sysctl.conf', options).params['kernel.domainname']
=> "example.com"
inspec> parse_config_file('/etc/sysctl.conf', options).params
=> {"kernel.domainname"=>"example.com"}
inspec> parse_config_file('/etc/sysctl.conf', options).content
=> "#\n# /etc/sysctl.conf - Configuration file for setting system variables\n# See /etc/sysctl.d/ for additional system variables.\n# See sysctl.conf (5) for information.\n#\n\nkernel.domainname = example.com\n\n# Uncomment the following to stop low-level messages on console\n#kernel.printk = 3 4 1 3\n\n##############################################################3\n# Functions previously found in netbase\n#\n\n# Uncomment the next two lines to enable Spoof protection (reverse-path filter)\n# Turn on Source Address Verification in all interfaces to\n# prevent some spoofing attacks\n#net.ipv4.conf.default.rp_filter=1\n#net.ipv4.conf.all.rp_filter=1\n\n# Uncomment the next line to enable TCP/IP SYN cookies\n# See http://lwn.net/Articles/277146/\n# Note: This may impact IPv6 TCP sessions too\n#net.ipv4.tcp_syncookies=1\n\n# Uncomment the next line to enable packet forwarding for IPv4\n#net.ipv4.ip_forward=1\n\n# Uncomment the next line to enable packet forwarding for IPv6\n#  Enabling this option disables Stateless Address Autoconfiguration\n#  based on Router Advertisements for this host\n#net.ipv6.conf.all.forwarding=1\n\n\n###################################################################\n# Additional settings - these settings can improve the network\n# security of the host and prevent against some network attacks\n# including spoofing attacks and man in the middle attacks through\n# redirection. Some network environments, however, require that these\n# settings are disabled so review and enable them as needed.\n#\n# Do not accept ICMP redirects (prevent MITM attacks)\n#net.ipv4.conf.all.accept_redirects = 0\n#net.ipv6.conf.all.accept_redirects = 0\n# _or_\n# Accept ICMP redirects only for gateways listed in our default\n# gateway list (enabled by default)\n# net.ipv4.conf.all.secure_redirects = 1\n#\n# Do not send ICMP redirects (we are not a router)\n#net.ipv4.conf.all.send_redirects = 0\n#\n# Do not accept IP source route packets (we are not a router)\n#net.ipv4.conf.all.accept_source_route = 0\n#net.ipv6.conf.all.accept_source_route = 0\n#\n# Log Martian Packets\n#net.ipv4.conf.all.log_martians = 1\n#\n"
inspec> 

@chris-rock
Copy link
Contributor Author

fixed #866

@arlimus
Copy link
Contributor

arlimus commented Aug 5, 2016

Looks great, thank you @chris-rock 👍

@chris-rock chris-rock merged commit 61eb9cf into master Aug 5, 2016
@chris-rock chris-rock deleted the chris-rock/parseconfig-improvement branch August 5, 2016 10:20
def to_s
"Parse Config #{@conf_path}"
def read_params
return @params if defined?(@params)
Copy link
Contributor

@stevendanna stevendanna Aug 5, 2016

Choose a reason for hiding this comment

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

@params.nil? would be the more idiomatic guard here I think. Instance variables will return nil by default if they haven't previously been defined. In our other ruby code bases I think we would generally follow this style:

def read_params
  @params ||= if content.nil?
                {}
              else
                SimpleConfig.new(content, @opts).params
              end
end

preferring the early return in cases where the assignment logic is more complex. Although, I'm not sure what latest rubocop says about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great feedback, I'll add fix this in another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Improves an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants