77require 'bolt/logger'
88require 'bolt/util'
99require 'bolt/config/options'
10+ require 'bolt/config/validator'
1011
1112module Bolt
1213 class UnknownTransportError < Bolt ::Error
@@ -73,6 +74,16 @@ def self.from_file(configfile, overrides = {})
7374 new ( project , data , overrides )
7475 end
7576
77+ def self . defaults_schema
78+ base = OPTIONS . slice ( *BOLT_DEFAULTS_OPTIONS )
79+ base [ 'inventory-config' ] [ :properties ] = TRANSPORT_CONFIG . transform_values ( &:schema )
80+ base
81+ end
82+
83+ def self . bolt_schema
84+ OPTIONS . slice ( *BOLT_OPTIONS ) . merge ( TRANSPORT_CONFIG . transform_values ( &:schema ) )
85+ end
86+
7687 def self . system_path
7788 # Lazy-load expensive gem code
7889 require 'win32/dir' if Bolt ::Util . windows?
@@ -106,6 +117,10 @@ def self.load_bolt_defaults_yaml(dir)
106117 )
107118 end
108119
120+ # Validate the config against the schema. This will raise a single error
121+ # with all validation errors.
122+ Validator . new . validate ( data , defaults_schema , filepath )
123+
109124 # Remove project-specific config such as hiera-config, etc.
110125 project_config = data . slice ( *( BOLT_PROJECT_OPTIONS - BOLT_DEFAULTS_OPTIONS ) )
111126
@@ -161,6 +176,10 @@ def self.load_bolt_yaml(dir)
161176 msg : "Configuration file #{ filepath } is deprecated and will be removed in a future version " \
162177 "of Bolt. Use '#{ dir + BOLT_DEFAULTS_NAME } ' instead." } ]
163178
179+ # Validate the config against the schema. This will raise a single error
180+ # with all validation errors.
181+ Validator . new . validate ( data , bolt_schema , filepath )
182+
164183 { filepath : filepath , data : data , logs : logs , deprecations : deprecations }
165184 end
166185
@@ -271,7 +290,7 @@ def normalize_overrides(options)
271290
272291 # Set console log to debug if in debug mode
273292 if options [ :debug ]
274- overrides [ 'log' ] = { 'console' => { 'level' => : debug } }
293+ overrides [ 'log' ] = { 'console' => { 'level' => ' debug' } }
275294 end
276295
277296 if options [ :puppetfile_path ]
@@ -280,6 +299,9 @@ def normalize_overrides(options)
280299
281300 overrides [ 'trace' ] = opts [ 'trace' ] if opts . key? ( 'trace' )
282301
302+ # Validate the overrides
303+ Validator . new . validate ( overrides , OPTIONS , 'command line' )
304+
283305 overrides
284306 end
285307
@@ -397,35 +419,11 @@ def validate
397419 "is automatically appended to the modulepath and cannot be configured."
398420 end
399421
400- keys = OPTIONS . keys - %w[ plugins plugin_hooks puppetdb ]
401- keys . each do |key |
402- next unless Bolt ::Util . references? ( @data [ key ] )
403- valid_keys = TRANSPORT_CONFIG . keys + %w[ plugins plugin_hooks puppetdb ]
404- raise Bolt ::ValidationError ,
405- "Found unsupported key _plugin in config setting #{ key } . Plugins are only available in " \
406- "#{ valid_keys . join ( ', ' ) } ."
407- end
408-
409- unless concurrency . is_a? ( Integer ) && concurrency > 0
410- raise Bolt ::ValidationError ,
411- "Concurrency must be a positive Integer, received #{ concurrency . class } #{ concurrency } "
412- end
413-
414- unless compile_concurrency . is_a? ( Integer ) && compile_concurrency > 0
415- raise Bolt ::ValidationError ,
416- "Compile concurrency must be a positive Integer, received #{ compile_concurrency . class } " \
417- "#{ compile_concurrency } "
418- end
419-
420422 compile_limit = 2 * Etc . nprocessors
421423 unless compile_concurrency < compile_limit
422424 raise Bolt ::ValidationError , "Compilation is CPU-intensive, set concurrency less than #{ compile_limit } "
423425 end
424426
425- unless %w[ human json rainbow ] . include? format
426- raise Bolt ::ValidationError , "Unsupported format: '#{ format } '"
427- end
428-
429427 Bolt ::Util . validate_file ( 'hiera-config' , @data [ 'hiera-config' ] ) if @data [ 'hiera-config' ]
430428 Bolt ::Util . validate_file ( 'trusted-external-command' , trusted_external ) if trusted_external
431429
0 commit comments