Class: Autosign::Config
- Inherits:
-
Object
- Object
- Autosign::Config
- Defined in:
- lib/autosign/config.rb
Overview
Class to manage configuration settings. The purpose of this class is to interact with the configuration file, merge defaults and user-provided settings, and present a configuration hash so that validators and other components do not have to re-implement config file handling.
Class Method Summary collapse
-
.generate_default(settings_param = {}) ⇒ Object
Generate a default configuration file As a convenience for the user, we can generate a default config file This class is currently too tightly coupled with the JWT token validator.
Instance Method Summary collapse
-
#initialize(settings_param = {}) ⇒ Autosign::Config
constructor
Create a config instance to interact with configuration settings To specify a configuration file, settings_param should include something like: => '/usr/local/etc/autosign.conf'.
-
#settings ⇒ Hash
Return a merged settings hash of defaults, config file settings and passed in settings (such as from the CLI).
Constructor Details
#initialize(settings_param = {}) ⇒ Autosign::Config
Create a config instance to interact with configuration settings To specify a configuration file, settings_param should include something like: => '/usr/local/etc/autosign.conf'
If no defaults are provided, the class checks several common locations for config file path.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/autosign/config.rb', line 39 def initialize(settings_param = {}) # set up logging @log = Logging.logger[self.class] @log.debug "initializing #{self.class.name}" # validate parameter raise 'settings is not a hash' unless settings_param.is_a?(Hash) # look in the following places for a config file @config_file_paths = ['/etc/puppetlabs/puppetserver/autosign.conf', '/etc/autosign.conf', '/usr/local/etc/autosign.conf'] # HOME is unset when puppet runs, so we need to only use it if it's set unless ENV['HOME'].nil? @config_file_paths << File.join(Dir.home, '.autosign.conf') end unless settings_param['config_file'].nil? @config_file_paths = [settings_param['config_file']] end @settings = settings_param @log.debug 'Using merged settings hash: ' + @settings.to_s end |
Class Method Details
.generate_default(settings_param = {}) ⇒ Object
Generate a default configuration file As a convenience for the user, we can generate a default config file This class is currently too tightly coupled with the JWT token validator
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/autosign/config.rb', line 144 def self.generate_default(settings_param = {}) os_defaults = ( case RbConfig::CONFIG['host_os'] when /darwin|mac os/ { 'logpath' => File.join(Dir.home, 'autosign.log'), 'confpath' => File.join(Dir.home, '.autosign.conf'), 'journalfile' => File.join(Dir.home, '.autosign.journal') } when /linux/ { 'logpath' => '/var/log/autosign.log', 'confpath' => '/etc/autosign.conf', 'journalfile' => File.join(Dir.home, '/var/autosign/autosign.journal') } when /bsd/ { 'logpath' => '/var/log/autosign.log', 'confpath' => '/usr/local/etc/autosign.conf', 'journalfile' => File.join(Dir.home, '/var/autosign/autosign.journal') } else raise Autosign::Exceptions::Error, "unsupported os: #{host_os.inspect}" end ) config = { 'general' => { 'loglevel' => 'warn', 'logfile' => os_defaults['logpath'], 'validation_order' => %w[ jwt_token password_list multiplexer ] }, 'jwt_token' => { 'secret' => SecureRandom.base64(20), 'validity' => '7200', 'journalfile' => os_defaults['journalfile'] } } # config = IniParse.gen do |doc| # doc.section("general") do |general| # general.option("loglevel", "warn") # general.option("logfile", os_defaults['logpath']) # end # doc.section("jwt_token") do |jwt_token| # jwt_token.option("secret", SecureRandom.base64(15)) # jwt_token.option("validity", 7200) # jwt_token.option("journalfile", os_defaults['journalfile']) # end # doc.section("multiplexer") do |jwt_token| # jwt_token.option(";external_policy_executable", '/usr/local/bin/some_autosign_executable') # jwt_token.option(";external_policy_executable", '/usr/local/bin/another_autosign_executable') # end # doc.section("password_list") do |jwt_token| # jwt_token.option(";password", 'static_autosign_password_here') # jwt_token.option(";password", 'another_static_autosign_password') # end # end.to_ini config_file = settings_param['config_file'] || os_defaults['confpath'] if File.file?(config_file) raise Autosign::Exceptions::Error, "file #{config_file} already exists, aborting" end return config_file if File.write(config_file, config.to_yaml) end |
Instance Method Details
#settings ⇒ Hash
Return a merged settings hash of defaults, config file settings and passed in settings (such as from the CLI)
66 67 68 69 70 71 72 |
# File 'lib/autosign/config.rb', line 66 def settings @log.debug 'merging settings' setting_sources = [default_settings, configfile, @settings] merged_settings = setting_sources.inject({}) { |merged, hash| merged.deep_merge!(hash, {:overwrite_arrays => true}) } @log.debug 'using merged settings: ' + merged_settings.to_s merged_settings end |