summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md9
-rw-r--r--Gemfile.lock22
-rw-r--r--VERSION2
-rw-r--r--azure-pipelines.yml6
-rw-r--r--chef-bin/lib/chef-bin/version.rb2
-rw-r--r--chef-config/lib/chef-config/config.rb34
-rw-r--r--chef-config/lib/chef-config/mixin/credentials.rb2
-rw-r--r--chef-config/lib/chef-config/version.rb2
-rw-r--r--chef-utils/lib/chef-utils/version.rb2
-rw-r--r--docs/dev/design_documents/data_collector.md5
-rw-r--r--lib/chef/application.rb4
-rw-r--r--lib/chef/application/base.rb5
-rw-r--r--lib/chef/application/client.rb7
-rw-r--r--lib/chef/application/knife.rb4
-rw-r--r--lib/chef/client.rb4
-rw-r--r--lib/chef/cookbook_site_streaming_uploader.rb14
-rw-r--r--lib/chef/cookbook_uploader.rb2
-rw-r--r--lib/chef/data_collector.rb7
-rw-r--r--lib/chef/data_collector/config_validation.rb30
-rw-r--r--lib/chef/data_collector/run_end_message.rb4
-rw-r--r--lib/chef/data_collector/run_start_message.rb2
-rw-r--r--lib/chef/digester.rb4
-rw-r--r--lib/chef/dsl/chef_vault.rb2
-rw-r--r--lib/chef/dsl/data_query.rb4
-rw-r--r--lib/chef/dsl/platform_introspection.rb2
-rw-r--r--lib/chef/encrypted_data_bag_item.rb4
-rw-r--r--lib/chef/encrypted_data_bag_item/decryptor.rb6
-rw-r--r--lib/chef/encrypted_data_bag_item/encryptor.rb6
-rw-r--r--lib/chef/formatters/error_inspectors/api_error_formatting.rb1
-rw-r--r--lib/chef/http.rb10
-rw-r--r--lib/chef/http/auth_credentials.rb6
-rw-r--r--lib/chef/http/authenticator.rb2
-rw-r--r--lib/chef/http/basic_client.rb6
-rw-r--r--lib/chef/http/http_request.rb8
-rw-r--r--lib/chef/http/socketless_chef_zero_client.rb3
-rw-r--r--lib/chef/http/ssl_policies.rb2
-rw-r--r--lib/chef/json_compat.rb2
-rw-r--r--lib/chef/knife/core/object_loader.rb2
-rw-r--r--lib/chef/knife/yaml_convert.rb2
-rw-r--r--lib/chef/mixin/openssl_helper.rb5
-rw-r--r--lib/chef/mixin/template.rb4
-rw-r--r--lib/chef/mixin/uris.rb4
-rw-r--r--lib/chef/monkey_patches/net_http.rb8
-rw-r--r--lib/chef/provider/ifconfig.rb2
-rw-r--r--lib/chef/provider/launchd.rb2
-rw-r--r--lib/chef/provider/package/rubygems.rb33
-rw-r--r--lib/chef/provider/package/windows.rb4
-rw-r--r--lib/chef/provider/package/windows/registry_uninstall_entry.rb4
-rw-r--r--lib/chef/provider/remote_file/content.rb3
-rw-r--r--lib/chef/provider/remote_file/ftp.rb10
-rw-r--r--lib/chef/provider/remote_file/sftp.rb10
-rw-r--r--lib/chef/provider/route.rb2
-rw-r--r--lib/chef/provider/service/macosx.rb2
-rw-r--r--lib/chef/provider/user/dscl.rb4
-rw-r--r--lib/chef/provider/user/mac.rb2
-rw-r--r--lib/chef/provider/windows_task.rb3
-rw-r--r--lib/chef/recipe.rb2
-rw-r--r--lib/chef/resource.rb2
-rw-r--r--lib/chef/resource/apt_repository.rb2
-rw-r--r--lib/chef/resource/build_essential.rb2
-rw-r--r--lib/chef/resource/chef_vault_secret.rb2
-rw-r--r--lib/chef/resource/macos_userdefaults.rb2
-rw-r--r--lib/chef/resource/openssl_ec_private_key.rb8
-rw-r--r--lib/chef/resource/openssl_rsa_private_key.rb8
-rw-r--r--lib/chef/resource/osx_profile.rb4
-rw-r--r--lib/chef/resource/plist.rb2
-rw-r--r--lib/chef/resource/windows_certificate.rb6
-rw-r--r--lib/chef/resource/windows_printer.rb8
-rw-r--r--lib/chef/resource/windows_printer_port.rb10
-rw-r--r--lib/chef/run_context.rb2
-rw-r--r--lib/chef/run_context/cookbook_compiler.rb2
-rw-r--r--lib/chef/search/query.rb9
-rw-r--r--lib/chef/shell.rb5
-rw-r--r--lib/chef/train_transport.rb2
-rw-r--r--lib/chef/util/diff.rb6
-rw-r--r--lib/chef/util/powershell/cmdlet.rb4
-rw-r--r--lib/chef/util/powershell/ps_credential.rb32
-rw-r--r--lib/chef/version.rb2
-rw-r--r--lib/chef/win32/registry.rb3
-rw-r--r--spec/unit/data_collector/config_validation_spec.rb207
-rw-r--r--spec/unit/data_collector_spec.rb113
81 files changed, 481 insertions, 307 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3c9b2b3d1d..5cb838e1d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,15 +1,18 @@
<!-- usage documentation: http://expeditor-docs.es.chef.io/configuration/changelog/ -->
-<!-- latest_release 16.5.33 -->
-## [v16.5.33](https://github.com/chef/chef/tree/v16.5.33) (2020-09-08)
+<!-- latest_release 16.5.36 -->
+## [v16.5.36](https://github.com/chef/chef/tree/v16.5.36) (2020-09-08)
#### Merged Pull Requests
-- chef_client_launchd: create a launchd daemon to handle the client restart [#10390](https://github.com/chef/chef/pull/10390) ([tas50](https://github.com/tas50))
+- Improve cli boot performance by prefering autoload over requires [#10383](https://github.com/chef/chef/pull/10383) ([mwrock](https://github.com/mwrock))
<!-- latest_release -->
<!-- release_rollup since=16.4.41 -->
### Changes not yet released to stable
#### Merged Pull Requests
+- Improve cli boot performance by prefering autoload over requires [#10383](https://github.com/chef/chef/pull/10383) ([mwrock](https://github.com/mwrock)) <!-- 16.5.36 -->
+- [data-collector] improved output_locations validation &amp; bug fixes [#10393](https://github.com/chef/chef/pull/10393) ([vsingh-msys](https://github.com/vsingh-msys)) <!-- 16.5.35 -->
+- Resolve RuboCop Style/RedundantInterpolation warnings [#10394](https://github.com/chef/chef/pull/10394) ([tas50](https://github.com/tas50)) <!-- 16.5.34 -->
- chef_client_launchd: create a launchd daemon to handle the client restart [#10390](https://github.com/chef/chef/pull/10390) ([tas50](https://github.com/tas50)) <!-- 16.5.33 -->
- Add --logfile to chef-apply command [#10389](https://github.com/chef/chef/pull/10389) ([tas50](https://github.com/tas50)) <!-- 16.5.32 -->
- Add a :reboot_delay property to the windows_ad_join resource [#10388](https://github.com/chef/chef/pull/10388) ([chef-davin](https://github.com/chef-davin)) <!-- 16.5.31 -->
diff --git a/Gemfile.lock b/Gemfile.lock
index bbdadc34d8..adfcdcfd3b 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -27,12 +27,12 @@ GIT
PATH
remote: .
specs:
- chef (16.5.33)
+ chef (16.5.36)
addressable
bcrypt_pbkdf (= 1.1.0.rc1)
bundler (>= 1.10)
- chef-config (= 16.5.33)
- chef-utils (= 16.5.33)
+ chef-config (= 16.5.36)
+ chef-utils (= 16.5.36)
chef-vault
chef-zero (>= 14.0.11)
diff-lcs (>= 1.2.4, < 1.4.0)
@@ -63,12 +63,12 @@ PATH
tty-screen (~> 0.6)
tty-table (~> 0.11.0)
uuidtools (~> 2.1.5)
- chef (16.5.33-universal-mingw32)
+ chef (16.5.36-universal-mingw32)
addressable
bcrypt_pbkdf (= 1.1.0.rc1)
bundler (>= 1.10)
- chef-config (= 16.5.33)
- chef-utils (= 16.5.33)
+ chef-config (= 16.5.36)
+ chef-utils (= 16.5.36)
chef-vault
chef-zero (>= 14.0.11)
diff-lcs (>= 1.2.4, < 1.4.0)
@@ -114,15 +114,15 @@ PATH
PATH
remote: chef-bin
specs:
- chef-bin (16.5.33)
- chef (= 16.5.33)
+ chef-bin (16.5.36)
+ chef (= 16.5.36)
PATH
remote: chef-config
specs:
- chef-config (16.5.33)
+ chef-config (16.5.36)
addressable
- chef-utils (= 16.5.33)
+ chef-utils (= 16.5.36)
fuzzyurl
mixlib-config (>= 2.2.12, < 4.0)
mixlib-shellout (>= 2.0, < 4.0)
@@ -131,7 +131,7 @@ PATH
PATH
remote: chef-utils
specs:
- chef-utils (16.5.33)
+ chef-utils (16.5.36)
GEM
remote: https://rubygems.org/
diff --git a/VERSION b/VERSION
index ee712ac5cf..6d14836fd8 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-16.5.33 \ No newline at end of file
+16.5.36 \ No newline at end of file
diff --git a/azure-pipelines.yml b/azure-pipelines.yml
index f808fa6ff9..d5e77904f7 100644
--- a/azure-pipelines.yml
+++ b/azure-pipelines.yml
@@ -82,6 +82,12 @@ jobs:
bundle config set without 'omnibus_package docgen ruby_prof'
bundle install --jobs=3 --retry=3 --path=vendor/bundle
gem install berkshelf --no-doc
+ # berks emits a ruby warning when it loads net/http due to a previously
+ # defined constant. Even though it is just a warning, powershell immediately
+ # exits 1. I'm not sure why but this just suppresses the warnings.
+ $env:RUBYOPT="-W0"
berks vendor cookbooks
+ # restore the default warning level
+ $env:RUBYOPT="-W1"
chef-client -z -o end_to_end --chef-license accept-no-persist
displayName: 'Run end_to_end::default recipe'
diff --git a/chef-bin/lib/chef-bin/version.rb b/chef-bin/lib/chef-bin/version.rb
index ab97c75cef..00b5cff708 100644
--- a/chef-bin/lib/chef-bin/version.rb
+++ b/chef-bin/lib/chef-bin/version.rb
@@ -21,7 +21,7 @@
module ChefBin
CHEFBIN_ROOT = File.expand_path("..", __dir__)
- VERSION = "16.5.33".freeze
+ VERSION = "16.5.36".freeze
end
#
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb
index a767d072ef..2c4323db82 100644
--- a/chef-config/lib/chef-config/config.rb
+++ b/chef-config/lib/chef-config/config.rb
@@ -20,8 +20,8 @@
# limitations under the License.
require "mixlib/config" unless defined?(Mixlib::Config)
-require "pathname" unless defined?(Pathname)
-require "chef-utils" unless defined?(ChefUtils::CANARY)
+autoload :Pathname, "pathname"
+autoload :ChefUtils, "chef-utils"
require_relative "fips"
require_relative "logger"
@@ -29,11 +29,13 @@ require_relative "windows"
require_relative "path_helper"
require_relative "mixin/fuzzy_hostname_matcher"
-require "mixlib/shellout" unless defined?(Mixlib::ShellOut::DEFAULT_READ_TIMEOUT)
-require "uri" unless defined?(URI)
-require "addressable/uri" unless defined?(Addressable::URI)
-require "openssl" unless defined?(OpenSSL)
-require "yaml"
+module Mixlib
+ autoload :ShellOut, "mixlib/shellout"
+end
+autoload :URI, "uri"
+autoload :Addressable, "addressable/uri"
+autoload :OpenSSL, "openssl"
+autoload :YAML, "yaml"
require_relative "dist"
module ChefConfig
@@ -1102,13 +1104,6 @@ module ChefConfig
export_no_proxy(no_proxy) if key?(:no_proxy) && no_proxy
end
- # Character classes for Addressable
- # See https://www.ietf.org/rfc/rfc3986.txt 3.2.1
- # The user part may not have a : in it
- USER = Addressable::URI::CharacterClasses::UNRESERVED + Addressable::URI::CharacterClasses::SUB_DELIMS
- # The password part may have any valid USERINFO characters
- PASSWORD = USER + "\\:"
-
# Builds a proxy uri and exports it to the appropriate environment variables. Examples:
# http://username:password@hostname:port
# https://username@hostname:port
@@ -1120,15 +1115,22 @@ module ChefConfig
# pass = password
# @api private
def self.export_proxy(scheme, path, user, pass)
+ # Character classes for Addressable
+ # See https://www.ietf.org/rfc/rfc3986.txt 3.2.1
+ # The user part may not have a : in it
+ user_class = Addressable::URI::CharacterClasses::UNRESERVED + Addressable::URI::CharacterClasses::SUB_DELIMS
+ # The password part may have any valid USERINFO characters
+ password_class = user_class + "\\:"
+
path = "#{scheme}://#{path}" unless path.include?("://")
# URI.split returns the following parts:
# [scheme, userinfo, host, port, registry, path, opaque, query, fragment]
uri = Addressable::URI.encode(path, Addressable::URI)
if user && !user.empty?
- userinfo = Addressable::URI.encode_component(user, USER)
+ userinfo = Addressable::URI.encode_component(user, user_class)
if pass
- userinfo << ":#{Addressable::URI.encode_component(pass, PASSWORD)}"
+ userinfo << ":#{Addressable::URI.encode_component(pass, password_class)}"
end
uri.userinfo = userinfo
end
diff --git a/chef-config/lib/chef-config/mixin/credentials.rb b/chef-config/lib/chef-config/mixin/credentials.rb
index 12fceef75d..de004a062f 100644
--- a/chef-config/lib/chef-config/mixin/credentials.rb
+++ b/chef-config/lib/chef-config/mixin/credentials.rb
@@ -15,7 +15,7 @@
# limitations under the License.
#
-require "tomlrb"
+autoload :Tomlrb, "tomlrb"
require_relative "../path_helper"
module ChefConfig
diff --git a/chef-config/lib/chef-config/version.rb b/chef-config/lib/chef-config/version.rb
index d0c365d360..c417fad5c6 100644
--- a/chef-config/lib/chef-config/version.rb
+++ b/chef-config/lib/chef-config/version.rb
@@ -15,5 +15,5 @@
module ChefConfig
CHEFCONFIG_ROOT = File.expand_path("..", __dir__)
- VERSION = "16.5.33".freeze
+ VERSION = "16.5.36".freeze
end
diff --git a/chef-utils/lib/chef-utils/version.rb b/chef-utils/lib/chef-utils/version.rb
index 4d0bfaeedc..9d8a7b0bc3 100644
--- a/chef-utils/lib/chef-utils/version.rb
+++ b/chef-utils/lib/chef-utils/version.rb
@@ -15,5 +15,5 @@
module ChefUtils
CHEFUTILS_ROOT = File.expand_path("..", __dir__)
- VERSION = "16.5.33".freeze
+ VERSION = "16.5.36".freeze
end
diff --git a/docs/dev/design_documents/data_collector.md b/docs/dev/design_documents/data_collector.md
index 889306e026..67782ed9ba 100644
--- a/docs/dev/design_documents/data_collector.md
+++ b/docs/dev/design_documents/data_collector.md
@@ -507,11 +507,12 @@ Chef::Config[:data_collector][:token] = "mytoken"
This works for chef-clients, which are configured to hit a Chef server, but use a custom non-Chef-Automate endpoint for reporting, or for chef-solo/zero users.
-XXX: There is also the `Chef::Config[:data_collector][:output_locations] = { uri: [ "https://chef.acme.local/myendpoint.html" ] }` method, which will behave differently, particularly on non-chef-solo use cases.
+XXX: There is also the `Chef::Config[:data_collector][:output_locations] = { urls: [ "https://chef.acme.local/myendpoint.html" ] }` method, which will behave differently, particularly on non-chef-solo use cases.
In that case, the Data Collector `server_url` will still be automatically derived from the `chef_server_url` and the Data Collector will attempt to contact that endpoint.
But with the token being supplied, the Data Collector will use that token and will not use Chef Server authentication.
Thus, the server should 403 back.
-Also, if `raise_on_failure` is left to the default of `false`, then the Data Collector will simply drop that failure and continue without raising, which will appear to work, and output will be send to the configured `output_locations`.
+Also, if `raise_on_failure` is left to the default of `false`, then the Data Collector will simply drop that failure and continue without raising, which will appear to work, and output will be send to the configured `output_locations`.
+
Note that the presence of a token flips all external URIs to using the token. So it is **not** possible to use this feature to talk to both a Chef Automate endpoint and a custom URI reporting endpoint.
This would seem to be the most useful of an incredibly marginally useful feature and it does not work.
But given how hopelessly complicated this is, the recommendation is to use the `server_url` and to avoid using any `url` options in the `output_locations` since that feature is fairly poorly designed at this point in time.
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index 42bcf9445e..be69e01f7f 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -28,7 +28,9 @@ require "tmpdir" unless defined?(Dir.mktmpdir)
require "rbconfig" unless defined?(RbConfig)
require_relative "application/exit_code"
require_relative "dist"
-require "license_acceptance/acceptor"
+module LicenseAcceptance
+ autoload :Acceptor, "license_acceptance/acceptor"
+end
class Chef
class Application
diff --git a/lib/chef/application/base.rb b/lib/chef/application/base.rb
index 1749284ea2..738788fa96 100644
--- a/lib/chef/application/base.rb
+++ b/lib/chef/application/base.rb
@@ -24,7 +24,9 @@ require_relative "../dist"
require_relative "../daemon"
require "chef-config/mixin/dot_d"
require "license_acceptance/cli_flags/mixlib_cli"
-require "mixlib/archive" unless defined?(Mixlib::Archive)
+module Mixlib
+ autoload :Archive, "mixlib/archive"
+end
# This is a temporary class being used as a part of an effort to reduce duplication
# between Chef::Application::Client and Chef::Application::Solo.
@@ -360,6 +362,7 @@ class Chef::Application::Base < Chef::Application
end
def fetch_recipe_tarball(url, path)
+ require "open-uri" unless defined?(OpenURI)
Chef::Log.trace("Download recipes tarball from #{url} to #{path}")
if File.exist?(url)
FileUtils.cp(url, path)
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb
index af942c0254..03ceff1727 100644
--- a/lib/chef/application/client.rb
+++ b/lib/chef/application/client.rb
@@ -20,7 +20,12 @@
require_relative "base"
require_relative "../handler/error_report"
require_relative "../workstation_config_loader"
-require "uri" unless defined?(URI)
+autoload :URI, "uri"
+module Mixlib
+ module Authentication
+ autoload :Log, "mixlib/authentication"
+ end
+end
# DO NOT MAKE EDITS, see Chef::Application::Base
#
diff --git a/lib/chef/application/knife.rb b/lib/chef/application/knife.rb
index c719768f11..3d6563f43a 100644
--- a/lib/chef/application/knife.rb
+++ b/lib/chef/application/knife.rb
@@ -19,7 +19,9 @@ require_relative "../knife"
require_relative "../application"
require "mixlib/log"
require "ohai/config"
-require "chef/monkey_patches/net_http.rb"
+module Net
+ autoload :HTTP, File.expand_path("../monkey_patches/net_http", __dir__)
+end
require_relative "../dist"
class Chef::Application::Knife < Chef::Application
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index 91d1758f70..1a6da64c8f 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -29,7 +29,7 @@ require_relative "api_client/registration"
require_relative "node"
require_relative "role"
require_relative "file_cache"
-require_relative "run_context"
+Chef.autoload :RunContext, File.expand_path("run_context", __dir__)
require_relative "runner"
require_relative "run_status"
require_relative "cookbook/cookbook_collection"
@@ -48,7 +48,7 @@ require_relative "action_collection"
require_relative "resource_reporter"
require_relative "data_collector"
require_relative "run_lock"
-require_relative "policy_builder"
+Chef.autoload :PolicyBuilder, File.expand_path("policy_builder", __dir__)
require_relative "request_id"
require_relative "platform/rebooter"
require_relative "mixin/deprecation"
diff --git a/lib/chef/cookbook_site_streaming_uploader.rb b/lib/chef/cookbook_site_streaming_uploader.rb
index 1598808234..7de89f9ad8 100644
--- a/lib/chef/cookbook_site_streaming_uploader.rb
+++ b/lib/chef/cookbook_site_streaming_uploader.rb
@@ -18,10 +18,16 @@
# limitations under the License.
#
-require "uri" unless defined?(URI)
-require "net/http" unless defined?(Net::HTTP)
-require "mixlib/authentication/signedheaderauth"
-require "openssl" unless defined?(OpenSSL)
+autoload :URI, "uri"
+module Net
+ autoload :HTTP, File.expand_path("monkey_patches/net_http", __dir__)
+end
+autoload :OpenSSL, "openssl"
+module Mixlib
+ module Authentication
+ autoload :SignedHeaderAuth, "mixlib/authentication/signedheaderauth"
+ end
+end
require_relative "dist"
class Chef
diff --git a/lib/chef/cookbook_uploader.rb b/lib/chef/cookbook_uploader.rb
index 816cbf95fe..235a539b94 100644
--- a/lib/chef/cookbook_uploader.rb
+++ b/lib/chef/cookbook_uploader.rb
@@ -1,5 +1,5 @@
-require "set" unless defined?(Set)
+autoload :Set, "set"
require_relative "exceptions"
require_relative "knife/cookbook_metadata"
require_relative "digester"
diff --git a/lib/chef/data_collector.rb b/lib/chef/data_collector.rb
index 6c7b2edb56..e1d7d9b97b 100644
--- a/lib/chef/data_collector.rb
+++ b/lib/chef/data_collector.rb
@@ -21,7 +21,7 @@
require_relative "server_api"
require_relative "http/simple_json"
require_relative "event_dispatch/base"
-require "set" unless defined?(Set)
+autoload :Set, "set"
require_relative "data_collector/run_end_message"
require_relative "data_collector/run_start_message"
require_relative "data_collector/config_validation"
@@ -212,8 +212,9 @@ class Chef
def send_to_output_locations(message)
return unless Chef::Config[:data_collector][:output_locations]
+ Chef::DataCollector::ConfigValidation.validate_output_locations!
Chef::Config[:data_collector][:output_locations].each do |type, locations|
- locations.each do |location|
+ Array(locations).each do |location|
send_to_file_location(location, message) if type == :files
send_to_http_location(location, message) if type == :urls
end
@@ -226,7 +227,7 @@ class Chef
# @param message [Hash] the message to render as JSON
#
def send_to_file_location(file_name, message)
- File.open(file_name, "a") do |fh|
+ File.open(File.expand_path(file_name), "a") do |fh|
fh.puts Chef::JSONCompat.to_json(message, validate_utf8: false)
end
end
diff --git a/lib/chef/data_collector/config_validation.rb b/lib/chef/data_collector/config_validation.rb
index 1a7940b0ae..a58472a82b 100644
--- a/lib/chef/data_collector/config_validation.rb
+++ b/lib/chef/data_collector/config_validation.rb
@@ -46,14 +46,14 @@ class Chef
return unless output_locations
# but deliberately setting an empty output_location we consider to be an error (XXX: but should we?)
- if output_locations.empty?
+ unless valid_hash_with_keys?(output_locations, :urls, :files)
raise Chef::Exceptions::ConfigurationError,
"Chef::Config[:data_collector][:output_locations] is empty. Please supply an hash of valid URLs and / or local file paths."
end
# loop through all the types and locations and validate each one-by-one
output_locations.each do |type, locations|
- locations.each do |location|
+ Array(locations).each do |location|
validate_url!(location) if type == :urls
validate_file!(location) if type == :files
end
@@ -89,8 +89,13 @@ class Chef
"Please upgrade #{Chef::Dist::SERVER_PRODUCT} to 12.11 or later and remove the token from your config file " \
"to use key based authentication instead")
true
- when Chef::Config[:data_collector][:output_locations] && Chef::Config[:data_collector][:output_locations][:files] && !Chef::Config[:data_collector][:output_locations][:files].empty?
+ when Chef::Config[:data_collector][:output_locations] && !valid_hash_with_keys?(Chef::Config[:data_collector][:output_locations], :urls)
# we can run fine to a file without a token, even in solo mode.
+ unless valid_hash_with_keys?(Chef::Config[:data_collector][:output_locations], :files)
+ raise Chef::Exceptions::ConfigurationError,
+ "Chef::Config[:data_collector][:output_locations] is empty. Please supply an hash of valid URLs and / or local file paths."
+ end
+
true
when running_mode == :solo && !Chef::Config[:data_collector][:token]
# we are in solo mode and are not logging to a file, so must have a token
@@ -105,16 +110,10 @@ class Chef
# validate an output_location file
def validate_file!(file)
- open(file, "a") {}
- rescue Errno::ENOENT
+ return true if Chef::Config.path_accessible?(File.expand_path(file))
+
raise Chef::Exceptions::ConfigurationError,
"Chef::Config[:data_collector][:output_locations][:files] contains the location #{file}, which is a non existent file path."
- rescue Errno::EACCES
- raise Chef::Exceptions::ConfigurationError,
- "Chef::Config[:data_collector][:output_locations][:files] contains the location #{file}, which cannot be written to by Chef."
- rescue Exception => e
- raise Chef::Exceptions::ConfigurationError,
- "Chef::Config[:data_collector][:output_locations][:files] contains the location #{file}, which is invalid: #{e.message}."
end
# validate an output_location url
@@ -125,6 +124,15 @@ class Chef
"Chef::Config[:data_collector][:output_locations][:urls] contains the url #{url} which is not valid."
end
+ # Validate the hash contains at least one of the given keys.
+ #
+ # @param hash [Hash] the hash to be validated.
+ # @param keys [Array] an array of keys to check existence of in the hash.
+ # @return [Boolean] true if the hash contains any of the given keys.
+ #
+ def valid_hash_with_keys?(hash, *keys)
+ hash.is_a?(Hash) && keys.any? { |k| hash.key?(k) }
+ end
end
end
end
diff --git a/lib/chef/data_collector/run_end_message.rb b/lib/chef/data_collector/run_end_message.rb
index 6f9f90b323..1900effa26 100644
--- a/lib/chef/data_collector/run_end_message.rb
+++ b/lib/chef/data_collector/run_end_message.rb
@@ -60,8 +60,8 @@ class Chef
"cookbooks" => ( node && node["cookbooks"] ) || {},
"policy_name" => node&.policy_name,
"policy_group" => node&.policy_group,
- "start_time" => run_status.start_time.utc.iso8601,
- "end_time" => run_status.end_time.utc.iso8601,
+ "start_time" => run_status&.start_time&.utc&.iso8601,
+ "end_time" => run_status&.end_time&.utc&.iso8601,
"source" => solo_run? ? "chef_solo" : "chef_client",
"status" => status,
"total_resource_count" => all_action_records(action_collection).count,
diff --git a/lib/chef/data_collector/run_start_message.rb b/lib/chef/data_collector/run_start_message.rb
index e5023b83ed..20ac867ef1 100644
--- a/lib/chef/data_collector/run_start_message.rb
+++ b/lib/chef/data_collector/run_start_message.rb
@@ -51,7 +51,7 @@ class Chef
"organization_name" => organization,
"run_id" => run_status&.run_id,
"source" => solo_run? ? "chef_solo" : "chef_client",
- "start_time" => run_status.start_time.utc.iso8601,
+ "start_time" => run_status&.start_time&.utc&.iso8601,
}
end
end
diff --git a/lib/chef/digester.rb b/lib/chef/digester.rb
index b9180ddd24..36df63f3a1 100644
--- a/lib/chef/digester.rb
+++ b/lib/chef/digester.rb
@@ -18,8 +18,8 @@
# limitations under the License.
#
-require "openssl" unless defined?(OpenSSL)
-require "digest" unless defined?(Digest)
+autoload :OpenSSL, "openssl"
+autoload :Digest, "digest"
require "singleton" unless defined?(Singleton)
class Chef
diff --git a/lib/chef/dsl/chef_vault.rb b/lib/chef/dsl/chef_vault.rb
index bfb256ef3f..031627c358 100644
--- a/lib/chef/dsl/chef_vault.rb
+++ b/lib/chef/dsl/chef_vault.rb
@@ -17,7 +17,7 @@
# limitations under the License.
#
-require "chef-vault"
+autoload :ChefVault, "chef-vault"
require_relative "data_query"
class Chef
diff --git a/lib/chef/dsl/data_query.rb b/lib/chef/dsl/data_query.rb
index 8bfeff96b1..3b15affb5b 100644
--- a/lib/chef/dsl/data_query.rb
+++ b/lib/chef/dsl/data_query.rb
@@ -17,8 +17,8 @@
#
require_relative "../search/query"
-require_relative "../data_bag"
-require_relative "../data_bag_item"
+Chef.autoload :DataBag, File.expand_path("../data_bag", __dir__)
+Chef.autoload :DataBagItem, File.expand_path("../data_bag_item", __dir__)
require_relative "../encrypted_data_bag_item"
require_relative "../encrypted_data_bag_item/check_encrypted"
diff --git a/lib/chef/dsl/platform_introspection.rb b/lib/chef/dsl/platform_introspection.rb
index aa6c8946d9..49ec396cf2 100644
--- a/lib/chef/dsl/platform_introspection.rb
+++ b/lib/chef/dsl/platform_introspection.rb
@@ -16,7 +16,7 @@
# limitations under the License.
#
-require "chef-utils" unless defined?(ChefUtils::CANARY)
+autoload :ChefUtils, "chef-utils"
require_relative "../mixin/chef_utils_wiring" unless defined?(Chef::Mixin::ChefUtilsWiring)
class Chef
diff --git a/lib/chef/encrypted_data_bag_item.rb b/lib/chef/encrypted_data_bag_item.rb
index 4a28d7ed35..c36c6923c5 100644
--- a/lib/chef/encrypted_data_bag_item.rb
+++ b/lib/chef/encrypted_data_bag_item.rb
@@ -17,11 +17,10 @@
#
require_relative "config"
-require_relative "data_bag_item"
+Chef.autoload :DataBagItem, File.expand_path("data_bag_item", __dir__)
require_relative "encrypted_data_bag_item/decryptor"
require_relative "encrypted_data_bag_item/encryptor"
require_relative "dist"
-require "open-uri" unless defined?(OpenURI)
# An EncryptedDataBagItem represents a read-only data bag item where
# all values, except for the value associated with the id key, have
@@ -129,6 +128,7 @@ class Chef::EncryptedDataBagItem
end
def self.load_secret(path = nil)
+ require "open-uri" unless defined?(OpenURI)
path ||= Chef::Config[:encrypted_data_bag_secret]
unless path
raise ArgumentError, "No secret specified and no secret found at #{Chef::Config.platform_specific_path(Chef::Dist::CONF_DIR + "/encrypted_data_bag_secret")}"
diff --git a/lib/chef/encrypted_data_bag_item/decryptor.rb b/lib/chef/encrypted_data_bag_item/decryptor.rb
index 9bc4f32ed3..57119796a9 100644
--- a/lib/chef/encrypted_data_bag_item/decryptor.rb
+++ b/lib/chef/encrypted_data_bag_item/decryptor.rb
@@ -16,10 +16,10 @@
# limitations under the License.
#
-require "yaml" unless defined?(YAML)
+autoload :YAML, "yaml"
require_relative "../json_compat"
-require "openssl" unless defined?(OpenSSL)
-require "base64" unless defined?(Base64)
+autoload :OpenSSL, "openssl"
+autoload :Base64, "base64"
require "digest/sha2" unless defined?(Digest::SHA2)
require_relative "../encrypted_data_bag_item"
require_relative "unsupported_encrypted_data_bag_item_format"
diff --git a/lib/chef/encrypted_data_bag_item/encryptor.rb b/lib/chef/encrypted_data_bag_item/encryptor.rb
index 14094928ca..f068d134d8 100644
--- a/lib/chef/encrypted_data_bag_item/encryptor.rb
+++ b/lib/chef/encrypted_data_bag_item/encryptor.rb
@@ -16,10 +16,10 @@
# limitations under the License.
#
-require "base64" unless defined?(Base64)
+autoload :Base64, "base64"
require "digest/sha2" unless defined?(Digest::SHA2)
-require "openssl" unless defined?(OpenSSL)
-require "ffi_yajl" unless defined?(FFI_Yajl)
+autoload :OpenSSL, "openssl"
+autoload :FFI_Yajl, "ffi_yajl"
require_relative "../encrypted_data_bag_item"
require_relative "unsupported_encrypted_data_bag_item_format"
require_relative "encryption_failure"
diff --git a/lib/chef/formatters/error_inspectors/api_error_formatting.rb b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
index 6a1c75e0d3..47e8523323 100644
--- a/lib/chef/formatters/error_inspectors/api_error_formatting.rb
+++ b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
@@ -18,6 +18,7 @@
require_relative "../../http/authenticator"
require_relative "../../dist"
+require "timeout" unless defined?(Timeout)
class Chef
module Formatters
diff --git a/lib/chef/http.rb b/lib/chef/http.rb
index 650ee73bb7..619cdbecec 100644
--- a/lib/chef/http.rb
+++ b/lib/chef/http.rb
@@ -22,11 +22,13 @@
#
require "tempfile" unless defined?(Tempfile)
-require "openssl" unless defined?(OpenSSL)
-require "net/http" unless defined?(Net::HTTP)
-require "uri" unless defined?(URI)
+autoload :OpenSSL, "openssl"
+autoload :URI, "uri"
+module Net
+ autoload :HTTP, File.expand_path("monkey_patches/net_http", __dir__)
+ autoload :HTTPClientException, File.expand_path("monkey_patches/net_http", __dir__)
+end
require_relative "http/basic_client"
-require_relative "monkey_patches/net_http"
require_relative "config"
require_relative "platform/query_helpers"
require_relative "exceptions"
diff --git a/lib/chef/http/auth_credentials.rb b/lib/chef/http/auth_credentials.rb
index 765c9ee778..1702ae7e24 100644
--- a/lib/chef/http/auth_credentials.rb
+++ b/lib/chef/http/auth_credentials.rb
@@ -21,7 +21,11 @@
# limitations under the License.
#
require_relative "../log"
-require "mixlib/authentication/signedheaderauth"
+module Mixlib
+ module Authentication
+ autoload :SignedHeaderAuth, "mixlib/authentication/signedheaderauth"
+ end
+end
class Chef
class HTTP
diff --git a/lib/chef/http/authenticator.rb b/lib/chef/http/authenticator.rb
index 4a29fcea33..80b32be750 100644
--- a/lib/chef/http/authenticator.rb
+++ b/lib/chef/http/authenticator.rb
@@ -18,7 +18,7 @@
require_relative "auth_credentials"
require_relative "../exceptions"
-require "openssl" unless defined?(OpenSSL)
+autoload :OpenSSL, "openssl"
class Chef
class HTTP
diff --git a/lib/chef/http/basic_client.rb b/lib/chef/http/basic_client.rb
index fa77b67427..f4be3b98ec 100644
--- a/lib/chef/http/basic_client.rb
+++ b/lib/chef/http/basic_client.rb
@@ -20,8 +20,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-require "uri" unless defined?(URI)
-require "net/http" unless defined?(Net::HTTP)
+autoload :URI, "uri"
+module Net
+ autoload :HTTP, File.expand_path("../monkey_patches/net_http", __dir__)
+end
require_relative "ssl_policies"
require_relative "http_request"
diff --git a/lib/chef/http/http_request.rb b/lib/chef/http/http_request.rb
index 9569c4133a..dbb1e0ce70 100644
--- a/lib/chef/http/http_request.rb
+++ b/lib/chef/http/http_request.rb
@@ -20,9 +20,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-require "uri" unless defined?(URI)
-require "cgi" unless defined?(CGI)
-require "net/http" unless defined?(Net::HTTP)
+autoload :URI, "uri"
+autoload :CGI, "cgi"
+module Net
+ autoload :HTTP, File.expand_path("../monkey_patches/net_http", __dir__)
+end
require_relative "../dist"
# To load faster, we only want ohai's version string.
diff --git a/lib/chef/http/socketless_chef_zero_client.rb b/lib/chef/http/socketless_chef_zero_client.rb
index 0cd7b564d9..feebba5a2c 100644
--- a/lib/chef/http/socketless_chef_zero_client.rb
+++ b/lib/chef/http/socketless_chef_zero_client.rb
@@ -45,6 +45,9 @@
require "chef_zero/server"
require_relative "../dist"
+module Net
+ autoload :HTTPResponse, File.expand_path("../monkey_patches/net_http", __dir__)
+end
class Chef
class HTTP
diff --git a/lib/chef/http/ssl_policies.rb b/lib/chef/http/ssl_policies.rb
index 05e4baf581..d03494defe 100644
--- a/lib/chef/http/ssl_policies.rb
+++ b/lib/chef/http/ssl_policies.rb
@@ -21,7 +21,7 @@
# limitations under the License.
#
-require "openssl" unless defined?(OpenSSL)
+autoload :OpenSSL, "openssl"
require_relative "../util/path_helper"
class Chef
diff --git a/lib/chef/json_compat.rb b/lib/chef/json_compat.rb
index 188a5d38f3..47d66dba96 100644
--- a/lib/chef/json_compat.rb
+++ b/lib/chef/json_compat.rb
@@ -17,7 +17,7 @@
# Wrapper class for interacting with JSON.
-require "ffi_yajl" unless defined?(FFI_Yajl)
+autoload :FFI_Yajl, "ffi_yajl"
require_relative "exceptions"
# We're requiring this to prevent breaking consumers using Hash.to_json
require "json" unless defined?(JSON)
diff --git a/lib/chef/knife/core/object_loader.rb b/lib/chef/knife/core/object_loader.rb
index 9cabbf97db..5421fc46ce 100644
--- a/lib/chef/knife/core/object_loader.rb
+++ b/lib/chef/knife/core/object_loader.rb
@@ -16,7 +16,7 @@
# limitations under the License.
#
-require "ffi_yajl" unless defined?(FFI_Yajl)
+autoload :FFI_Yajl, "ffi_yajl"
require_relative "../../util/path_helper"
require_relative "../../data_bag_item"
diff --git a/lib/chef/knife/yaml_convert.rb b/lib/chef/knife/yaml_convert.rb
index ff64f5c53a..6bd2d1c0ea 100644
--- a/lib/chef/knife/yaml_convert.rb
+++ b/lib/chef/knife/yaml_convert.rb
@@ -16,7 +16,7 @@
# limitations under the License.
#
-require "yaml" unless defined?(YAML)
+autoload :YAML, "yaml"
require_relative "../knife"
class Chef::Knife::YamlConvert < Chef::Knife
diff --git a/lib/chef/mixin/openssl_helper.rb b/lib/chef/mixin/openssl_helper.rb
index db422175bb..1d50523df8 100644
--- a/lib/chef/mixin/openssl_helper.rb
+++ b/lib/chef/mixin/openssl_helper.rb
@@ -14,15 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
+autoload :OpenSSL, "openssl"
class Chef
module Mixin
# various helpers for use with openssl. Currently used by the openssl_* resources
module OpenSSLHelper
- def self.included(_base)
- require "openssl" unless defined?(::OpenSSL)
- end
-
# determine the key filename from the cert filename
# @param [String] cert_filename the path to the certfile
# @return [String] the path to the keyfile
diff --git a/lib/chef/mixin/template.rb b/lib/chef/mixin/template.rb
index d72bfe9e3a..a369cae9e5 100644
--- a/lib/chef/mixin/template.rb
+++ b/lib/chef/mixin/template.rb
@@ -16,8 +16,8 @@
# limitations under the License.
#
-require "tempfile" unless defined?(Tempfile)
-require "erubis" unless defined?(Erubis)
+autoload :Tempfile, "tempfile"
+autoload :Erubis, "erubis"
class Chef
module Mixin
diff --git a/lib/chef/mixin/uris.rb b/lib/chef/mixin/uris.rb
index ab33c6791e..74c2af73d8 100644
--- a/lib/chef/mixin/uris.rb
+++ b/lib/chef/mixin/uris.rb
@@ -16,8 +16,8 @@
# limitations under the License.
#
-require "uri" unless defined?(URI)
-require "addressable/uri" unless defined?(Addressable::URI)
+autoload :URI, "uri"
+autoload :Addressable, "addressable/uri"
class Chef
module Mixin
diff --git a/lib/chef/monkey_patches/net_http.rb b/lib/chef/monkey_patches/net_http.rb
index 42007d23a7..5955f1c6d2 100644
--- a/lib/chef/monkey_patches/net_http.rb
+++ b/lib/chef/monkey_patches/net_http.rb
@@ -7,16 +7,16 @@ end
require "net/http" unless defined?(Net::HTTP)
module Net
- class HTTPError
+ class HTTPError < Net::ProtocolError
include ChefNetHTTPExceptionExtensions
end
- class HTTPRetriableError
+ class HTTPRetriableError < Net::ProtoRetriableError
include ChefNetHTTPExceptionExtensions
end
- class HTTPClientException
+ class HTTPClientException < Net::ProtoServerError
include ChefNetHTTPExceptionExtensions
end
- class HTTPFatalError
+ class HTTPFatalError < Net::ProtoFatalError
include ChefNetHTTPExceptionExtensions
end
end
diff --git a/lib/chef/provider/ifconfig.rb b/lib/chef/provider/ifconfig.rb
index fbff49468a..d5e18ae730 100644
--- a/lib/chef/provider/ifconfig.rb
+++ b/lib/chef/provider/ifconfig.rb
@@ -20,7 +20,7 @@ require_relative "../log"
require_relative "../provider"
require_relative "../resource/file"
require_relative "../exceptions"
-require "erb" unless defined?(Erb)
+autoload :ERB, "erb"
class Chef
class Provider
diff --git a/lib/chef/provider/launchd.rb b/lib/chef/provider/launchd.rb
index 78ae823596..37c73924f8 100644
--- a/lib/chef/provider/launchd.rb
+++ b/lib/chef/provider/launchd.rb
@@ -20,7 +20,7 @@ require_relative "../provider"
require_relative "../resource/file"
require_relative "../resource/cookbook_file"
require_relative "../resource/macosx_service"
-require "plist"
+autoload :Plist, "plist"
require "forwardable" unless defined?(Forwardable)
class Chef
diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb
index 0dd62338dc..f9e89ead02 100644
--- a/lib/chef/provider/package/rubygems.rb
+++ b/lib/chef/provider/package/rubygems.rb
@@ -17,7 +17,7 @@
# limitations under the License.
#
-require "uri" unless defined?(URI)
+autoload :URI, "uri"
require_relative "../package"
require_relative "../../resource/package"
require_relative "../../mixin/get_source_from_package"
@@ -25,31 +25,34 @@ require_relative "../../mixin/which"
require_relative "../../dist"
# Class methods on Gem are defined in rubygems
-require "rubygems" unless defined?(Gem)
+autoload :Gem, "rubygems"
# Ruby 1.9's gem_prelude can interact poorly with loading the full rubygems
# explicitly like this. Make sure rubygems/specification is always last in this
# list
-require "rubygems/version"
-require "rubygems/dependency"
-require "rubygems/spec_fetcher"
-require "rubygems/platform"
-require "rubygems/package" unless defined?(Gem::Package)
-require "rubygems/dependency_installer"
-require "rubygems/uninstaller"
-require "rubygems/specification"
+Gem.autoload :Version, "rubygems/version"
+Gem.autoload :Dependency, "rubygems/dependency"
+Gem.autoload :SpecFetcher, "rubygems/spec_fetcher"
+Gem.autoload :Platform, "rubygems/platform"
+Gem.autoload :Package, "rubygems/package"
+Gem.autoload :DependencyInstaller, "rubygems/dependency_installer"
+Gem.autoload :Uninstaller, "rubygems/uninstaller"
+Gem.autoload :Specification, "rubygems/specification"
class Chef
class Provider
class Package
class Rubygems < Chef::Provider::Package
class GemEnvironment
- # HACK: trigger gem config load early. Otherwise it can get lazy
- # loaded during operations where we've set Gem.sources to an
- # alternate value and overwrite it with the defaults.
- Gem.configuration
-
DEFAULT_UNINSTALLER_OPTS = { ignore: true, executables: true }.freeze
+ def initialize(*args)
+ super
+ # HACK: trigger gem config load early. Otherwise it can get lazy
+ # loaded during operations where we've set Gem.sources to an
+ # alternate value and overwrite it with the defaults.
+ Gem.configuration
+ end
+
# The paths where rubygems should search for installed gems.
# Implemented by subclasses.
def gem_paths
diff --git a/lib/chef/provider/package/windows.rb b/lib/chef/provider/package/windows.rb
index e654c80c83..c722d8222c 100644
--- a/lib/chef/provider/package/windows.rb
+++ b/lib/chef/provider/package/windows.rb
@@ -21,7 +21,7 @@ require_relative "../../resource/windows_package"
require_relative "../package"
require_relative "../../util/path_helper"
require_relative "../../mixin/checksum"
-require "cgi" unless defined?(CGI)
+autoload :CGI, "cgi"
class Chef
class Provider
@@ -33,7 +33,7 @@ class Chef
provides :package, os: "windows"
provides :windows_package
- require "chef/provider/package/windows/registry_uninstall_entry.rb"
+ autoload :RegistryUninstallEntry, ::File.expand_path("windows/registry_uninstall_entry.rb", __dir__)
def define_resource_requirements
if new_resource.checksum
diff --git a/lib/chef/provider/package/windows/registry_uninstall_entry.rb b/lib/chef/provider/package/windows/registry_uninstall_entry.rb
index f1814d5dcf..6e7b825256 100644
--- a/lib/chef/provider/package/windows/registry_uninstall_entry.rb
+++ b/lib/chef/provider/package/windows/registry_uninstall_entry.rb
@@ -17,7 +17,9 @@
# limitations under the License.
#
-require "win32/registry" if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
+module Win32
+ autoload :Registry, File.expand_path("../../../monkey_patches/win32/registry", __dir__) if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
+end
class Chef
class Provider
diff --git a/lib/chef/provider/remote_file/content.rb b/lib/chef/provider/remote_file/content.rb
index c76029c091..665da47e8d 100644
--- a/lib/chef/provider/remote_file/content.rb
+++ b/lib/chef/provider/remote_file/content.rb
@@ -21,6 +21,9 @@ require "uri" unless defined?(URI)
require "tempfile" unless defined?(Tempfile)
require_relative "../../file_content_management/content_base"
require_relative "../../mixin/uris"
+module Net
+ autoload :FTPError, "net/ftp"
+end
class Chef
class Provider
diff --git a/lib/chef/provider/remote_file/ftp.rb b/lib/chef/provider/remote_file/ftp.rb
index e2b32ddaf6..44a6d1c6e8 100644
--- a/lib/chef/provider/remote_file/ftp.rb
+++ b/lib/chef/provider/remote_file/ftp.rb
@@ -16,10 +16,12 @@
# limitations under the License.
#
-require "uri" unless defined?(URI)
-require "cgi" unless defined?(CGI)
-require "tempfile" unless defined?(Tempfile)
-require "net/ftp"
+autoload :URI, "uri"
+autoload :CGI, "cgi"
+autoload :Tempfile, "tempfile"
+module Net
+ autoload :FTP, "net/ftp"
+end
require_relative "../remote_file"
require_relative "../../file_content_management/tempfile"
diff --git a/lib/chef/provider/remote_file/sftp.rb b/lib/chef/provider/remote_file/sftp.rb
index 43654bd67c..be2a34fc54 100644
--- a/lib/chef/provider/remote_file/sftp.rb
+++ b/lib/chef/provider/remote_file/sftp.rb
@@ -16,10 +16,12 @@
# limitations under the License.
#
-require "uri" unless defined?(URI)
-require "cgi" unless defined?(CGI)
-require "tempfile" unless defined?(Tempfile)
-require "net/sftp"
+autoload :URI, "uri"
+autoload :CGI, "cgi"
+autoload :Tempfile, "tempfile"
+module Net
+ autoload :SFTP, "net/sftp"
+end
require_relative "../remote_file"
require_relative "../../file_content_management/tempfile"
diff --git a/lib/chef/provider/route.rb b/lib/chef/provider/route.rb
index afb1fd5a88..3eac41aef0 100644
--- a/lib/chef/provider/route.rb
+++ b/lib/chef/provider/route.rb
@@ -19,7 +19,7 @@
require_relative "../log"
require_relative "../provider"
-require "ipaddr" unless defined?(IPAddr)
+autoload :IPAddr, "ipaddr"
class Chef
class Provider
diff --git a/lib/chef/provider/service/macosx.rb b/lib/chef/provider/service/macosx.rb
index 7e695aaf2b..ae04c7bfdd 100644
--- a/lib/chef/provider/service/macosx.rb
+++ b/lib/chef/provider/service/macosx.rb
@@ -18,7 +18,7 @@
#
require "etc" unless defined?(Etc)
-require "rexml/document" unless defined?(REXML::Document)
+autoload :REXML, "rexml/document"
require_relative "../../resource/service"
require_relative "../../resource/macosx_service"
require_relative "simple"
diff --git a/lib/chef/provider/user/dscl.rb b/lib/chef/provider/user/dscl.rb
index 7b86fa5269..80853d43da 100644
--- a/lib/chef/provider/user/dscl.rb
+++ b/lib/chef/provider/user/dscl.rb
@@ -19,8 +19,8 @@
require_relative "../../mixin/shell_out"
require_relative "../user"
require_relative "../../resource/user/dscl_user"
-require "openssl" unless defined?(OpenSSL)
-require "plist"
+autoload :OpenSSL, "openssl"
+autoload :Plist, "plist"
require_relative "../../util/path_helper"
class Chef
diff --git a/lib/chef/provider/user/mac.rb b/lib/chef/provider/user/mac.rb
index 9ae1628d5c..a738fa4aa0 100644
--- a/lib/chef/provider/user/mac.rb
+++ b/lib/chef/provider/user/mac.rb
@@ -22,7 +22,7 @@ require_relative "../../mixin/shell_out"
require_relative "../../mixin/which"
require_relative "../user"
require_relative "../../resource/user/mac_user"
-require "plist"
+autoload :Plist, "plist"
class Chef
class Provider
diff --git a/lib/chef/provider/windows_task.rb b/lib/chef/provider/windows_task.rb
index c402818321..963c52ef02 100644
--- a/lib/chef/provider/windows_task.rb
+++ b/lib/chef/provider/windows_task.rb
@@ -16,8 +16,7 @@
# limitations under the License.
#
-require "rexml/document" unless defined?(REXML::Document)
-require "iso8601" if ChefUtils.windows?
+autoload :ISO8601, "iso8601" if ChefUtils.windows?
require_relative "../provider"
require_relative "../util/path_helper"
require "win32/taskscheduler" if ChefUtils.windows?
diff --git a/lib/chef/recipe.rb b/lib/chef/recipe.rb
index be188817d6..4009677936 100644
--- a/lib/chef/recipe.rb
+++ b/lib/chef/recipe.rb
@@ -17,7 +17,7 @@
# limitations under the License.
#
-require "yaml" unless defined?(YAML)
+autoload :YAML, "yaml"
require_relative "dsl/recipe"
require_relative "mixin/from_file"
require_relative "mixin/deprecation"
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 735ef47b63..2e5b17f277 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -36,7 +36,7 @@ require_relative "resource/resource_notification"
require_relative "provider_resolver"
require_relative "resource_resolver"
require_relative "provider"
-require "set" unless defined?(Set)
+autoload :Set, "set"
require_relative "mixin/deprecation"
require_relative "mixin/properties"
diff --git a/lib/chef/resource/apt_repository.rb b/lib/chef/resource/apt_repository.rb
index bc0c904745..e4569561fc 100644
--- a/lib/chef/resource/apt_repository.rb
+++ b/lib/chef/resource/apt_repository.rb
@@ -19,7 +19,7 @@
require_relative "../resource"
require_relative "../http/simple"
require "tmpdir" unless defined?(Dir.mktmpdir)
-require "addressable" unless defined?(Addressable)
+autoload :Addressable, "addressable"
class Chef
class Resource
diff --git a/lib/chef/resource/build_essential.rb b/lib/chef/resource/build_essential.rb
index ad37d3924f..17b97ebd97 100644
--- a/lib/chef/resource/build_essential.rb
+++ b/lib/chef/resource/build_essential.rb
@@ -15,7 +15,7 @@
#
require_relative "../resource"
-require "plist"
+autoload :Plist, "plist"
class Chef
class Resource
diff --git a/lib/chef/resource/chef_vault_secret.rb b/lib/chef/resource/chef_vault_secret.rb
index d8a2e89ffe..1c8fa985f9 100644
--- a/lib/chef/resource/chef_vault_secret.rb
+++ b/lib/chef/resource/chef_vault_secret.rb
@@ -16,7 +16,7 @@
#
require_relative "../resource"
-require "chef-vault"
+autoload :ChefVault, "chef-vault"
class Chef
class Resource
diff --git a/lib/chef/resource/macos_userdefaults.rb b/lib/chef/resource/macos_userdefaults.rb
index d163fd8402..ab789635e7 100644
--- a/lib/chef/resource/macos_userdefaults.rb
+++ b/lib/chef/resource/macos_userdefaults.rb
@@ -17,7 +17,7 @@
require_relative "../resource"
require_relative "../dist"
-require "plist"
+autoload :Plist, "plist"
class Chef
class Resource
diff --git a/lib/chef/resource/openssl_ec_private_key.rb b/lib/chef/resource/openssl_ec_private_key.rb
index 36ad8f43ae..26f28b1ad8 100644
--- a/lib/chef/resource/openssl_ec_private_key.rb
+++ b/lib/chef/resource/openssl_ec_private_key.rb
@@ -66,10 +66,12 @@ class Chef
description: "The desired passphrase for the key."
property :key_cipher, String,
- equal_to: OpenSSL::Cipher.ciphers,
- validation_message: "key_cipher must be a cipher known to openssl. Run `openssl list-cipher-algorithms` to see available options.",
description: "The designed cipher to use when generating your key. Run `openssl list-cipher-algorithms` to see available options.",
- default: "des3"
+ default: lazy { "des3" },
+ callbacks: {
+ "key_cipher must be a cipher known to openssl. Run `openssl list-cipher-algorithms` to see available options." =>
+ proc { |v| OpenSSL::Cipher.ciphers.include?(v) },
+ }
property :owner, [String, Integer],
description: "The owner applied to all files created by the resource."
diff --git a/lib/chef/resource/openssl_rsa_private_key.rb b/lib/chef/resource/openssl_rsa_private_key.rb
index c8129a728e..381e2af696 100644
--- a/lib/chef/resource/openssl_rsa_private_key.rb
+++ b/lib/chef/resource/openssl_rsa_private_key.rb
@@ -65,10 +65,12 @@ class Chef
description: "The desired passphrase for the key."
property :key_cipher, String,
- equal_to: OpenSSL::Cipher.ciphers,
- validation_message: "key_cipher must be a cipher known to openssl. Run `openssl list-cipher-algorithms` to see available options.",
description: "The designed cipher to use when generating your key. Run `openssl list-cipher-algorithms` to see available options.",
- default: "des3"
+ default: lazy { "des3" },
+ callbacks: {
+ "key_cipher must be a cipher known to openssl. Run `openssl list-cipher-algorithms` to see available options." =>
+ proc { |v| OpenSSL::Cipher.ciphers.include?(v) },
+ }
property :owner, [String, Integer],
description: "The owner applied to all files created by the resource."
diff --git a/lib/chef/resource/osx_profile.rb b/lib/chef/resource/osx_profile.rb
index c3ff099436..d1a2791d3c 100644
--- a/lib/chef/resource/osx_profile.rb
+++ b/lib/chef/resource/osx_profile.rb
@@ -19,8 +19,8 @@
require_relative "../resource"
require_relative "../log"
require_relative "../resource/file"
-require "uuidtools"
-require "plist"
+autoload :UUIDTools, "uuidtools"
+autoload :Plist, "plist"
class Chef
class Resource
diff --git a/lib/chef/resource/plist.rb b/lib/chef/resource/plist.rb
index 06811ea8b6..f4058520a9 100644
--- a/lib/chef/resource/plist.rb
+++ b/lib/chef/resource/plist.rb
@@ -16,7 +16,7 @@
# limitations under the License.
#
require_relative "../resource"
-require "plist"
+autoload :Plist, "plist"
class Chef
class Resource
diff --git a/lib/chef/resource/windows_certificate.rb b/lib/chef/resource/windows_certificate.rb
index ce1bcede40..215f87d9c3 100644
--- a/lib/chef/resource/windows_certificate.rb
+++ b/lib/chef/resource/windows_certificate.rb
@@ -19,8 +19,10 @@
require_relative "../util/path_helper"
require_relative "../resource"
-require "win32-certstore" if Chef::Platform.windows?
-require "openssl" unless defined?(OpenSSL)
+module Win32
+ autoload :Certstore, "win32-certstore" if Chef::Platform.windows?
+end
+autoload :OpenSSL, "openssl"
require_relative "../dist"
class Chef
diff --git a/lib/chef/resource/windows_printer.rb b/lib/chef/resource/windows_printer.rb
index 947477baeb..dea15ba112 100644
--- a/lib/chef/resource/windows_printer.rb
+++ b/lib/chef/resource/windows_printer.rb
@@ -24,7 +24,7 @@ class Chef
class WindowsPrinter < Chef::Resource
unified_mode true
- require "resolv"
+ autoload :Resolv, "resolv"
provides(:windows_printer) { true }
@@ -78,8 +78,10 @@ class Chef
property :ipv4_address, String,
description: "The IPv4 address of the printer, such as `10.4.64.23`",
- validation_message: "The ipv4_address property must be in the IPv4 format of `WWW.XXX.YYY.ZZZ`",
- regex: Resolv::IPv4::Regex
+ callbacks: {
+ "The ipv4_address property must be in the IPv4 format of `WWW.XXX.YYY.ZZZ`" =>
+ proc { |v| v.match(Resolv::IPv4::Regex) },
+ }
PRINTERS_REG_KEY = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\\'.freeze unless defined?(PRINTERS_REG_KEY)
diff --git a/lib/chef/resource/windows_printer_port.rb b/lib/chef/resource/windows_printer_port.rb
index 1b3f8753ba..2a4eaa09b3 100644
--- a/lib/chef/resource/windows_printer_port.rb
+++ b/lib/chef/resource/windows_printer_port.rb
@@ -24,7 +24,7 @@ class Chef
class WindowsPrinterPort < Chef::Resource
unified_mode true
- require "resolv"
+ autoload :Resolv, "resolv"
provides(:windows_printer_port) { true }
@@ -61,9 +61,11 @@ class Chef
property :ipv4_address, String,
name_property: true,
- regex: Resolv::IPv4::Regex,
- validation_message: "The ipv4_address property must be in the format of WWW.XXX.YYY.ZZZ!",
- description: "An optional property for the IPv4 address of the printer if it differs from the resource block's name."
+ description: "An optional property for the IPv4 address of the printer if it differs from the resource block's name.",
+ callbacks: {
+ "The ipv4_address property must be in the format of WWW.XXX.YYY.ZZZ!" =>
+ proc { |v| v.match(Resolv::IPv4::Regex) },
+ }
property :port_name, String,
description: "The port name."
diff --git a/lib/chef/run_context.rb b/lib/chef/run_context.rb
index 7ba7d9f33e..bfefc6e101 100644
--- a/lib/chef/run_context.rb
+++ b/lib/chef/run_context.rb
@@ -28,7 +28,7 @@ require_relative "event_dispatch/events_output_stream"
require_relative "train_transport"
require_relative "exceptions"
require "forwardable" unless defined?(Forwardable)
-require "set" unless defined?(Set)
+autoload :Set, "set"
class Chef
diff --git a/lib/chef/run_context/cookbook_compiler.rb b/lib/chef/run_context/cookbook_compiler.rb
index 51b841d798..27461fea9a 100644
--- a/lib/chef/run_context/cookbook_compiler.rb
+++ b/lib/chef/run_context/cookbook_compiler.rb
@@ -16,7 +16,7 @@
# limitations under the License.
#
-require "set" unless defined?(Set)
+autoload :Set, "set"
require_relative "../log"
require_relative "../recipe"
require_relative "../resource/lwrp_base"
diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb
index cd4275e8db..70b31cd5e4 100644
--- a/lib/chef/search/query.rb
+++ b/lib/chef/search/query.rb
@@ -20,8 +20,8 @@ require_relative "../config"
require_relative "../exceptions"
require_relative "../server_api"
-require "uri" unless defined?(URI)
-require "addressable/uri" unless defined?(Addressable::URI)
+autoload :URI, "uri"
+autoload :Addressable, "addressable/uri"
class Chef
class Search
@@ -132,10 +132,9 @@ class Chef
args_h
end
- QUERY_PARAM_VALUE = Addressable::URI::CharacterClasses::QUERY + "\\&\\;"
-
def escape_value(s)
- s && Addressable::URI.encode_component(s.to_s, QUERY_PARAM_VALUE)
+ query_param_value = Addressable::URI::CharacterClasses::QUERY + "\\&\\;"
+ s && Addressable::URI.encode_component(s.to_s, query_param_value)
end
def create_query_string(type, query, rows, start)
diff --git a/lib/chef/shell.rb b/lib/chef/shell.rb
index 8429110eec..1a012144ed 100644
--- a/lib/chef/shell.rb
+++ b/lib/chef/shell.rb
@@ -15,6 +15,11 @@
# limitations under the License.
#
+module Mixlib
+ module Authentication
+ autoload :Log, "mixlib/authentication"
+ end
+end
require "singleton" unless defined?(Singleton)
require "pp" unless defined?(PP)
require "etc" unless defined?(Etc)
diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb
index 81d1ae09f2..a4f311fc51 100644
--- a/lib/chef/train_transport.rb
+++ b/lib/chef/train_transport.rb
@@ -16,7 +16,7 @@
#
require "chef-config/mixin/credentials"
-require "train"
+autoload :Train, "train"
require_relative "dist"
class Chef
diff --git a/lib/chef/util/diff.rb b/lib/chef/util/diff.rb
index 3f8a925c4d..0774dea813 100644
--- a/lib/chef/util/diff.rb
+++ b/lib/chef/util/diff.rb
@@ -40,9 +40,6 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OF OTHER DEALINGS IN THE
# SOFTWARE.
-require "diff/lcs"
-require "diff/lcs/hunk"
-
class Chef
class Util
class Diff
@@ -86,6 +83,9 @@ class Chef
# produces a unified-output-format diff with 3 lines of context
# ChefFS uses udiff() directly
def udiff(old_file, new_file)
+ require "diff/lcs"
+ require "diff/lcs/hunk"
+
diff_str = ""
file_length_difference = 0
diff --git a/lib/chef/util/powershell/cmdlet.rb b/lib/chef/util/powershell/cmdlet.rb
index 344e20f466..ea84798814 100644
--- a/lib/chef/util/powershell/cmdlet.rb
+++ b/lib/chef/util/powershell/cmdlet.rb
@@ -16,7 +16,9 @@
# limitations under the License.
#
-require "mixlib/shellout" unless defined?(Mixlib::ShellOut::DEFAULT_READ_TIMEOUT)
+module Mixlib
+ autoload :ShellOut, "mixlib/shellout"
+end
require_relative "../../mixin/windows_architecture_helper"
require_relative "cmdlet_result"
diff --git a/lib/chef/util/powershell/ps_credential.rb b/lib/chef/util/powershell/ps_credential.rb
index cc11cc6c36..0404f5a1ac 100644
--- a/lib/chef/util/powershell/ps_credential.rb
+++ b/lib/chef/util/powershell/ps_credential.rb
@@ -18,24 +18,28 @@
require_relative "../../win32/crypto" if ChefUtils.windows?
-class Chef::Util::Powershell
- class PSCredential
- def initialize(username, password)
- @username = username
- @password = password
- end
+class Chef
+ class Util
+ class Powershell
+ class PSCredential
+ def initialize(username, password)
+ @username = username
+ @password = password
+ end
- def to_psobject
- "New-Object System.Management.Automation.PSCredential('#{@username}',('#{encrypt(@password)}' | ConvertTo-SecureString))"
- end
+ def to_psobject
+ "New-Object System.Management.Automation.PSCredential('#{@username}',('#{encrypt(@password)}' | ConvertTo-SecureString))"
+ end
- alias to_s to_psobject
- alias to_text to_psobject
+ alias to_s to_psobject
+ alias to_text to_psobject
- private
+ private
- def encrypt(str)
- Chef::ReservedNames::Win32::Crypto.encrypt(str)
+ def encrypt(str)
+ Chef::ReservedNames::Win32::Crypto.encrypt(str)
+ end
+ end
end
end
end
diff --git a/lib/chef/version.rb b/lib/chef/version.rb
index aa8105c16f..0978e07843 100644
--- a/lib/chef/version.rb
+++ b/lib/chef/version.rb
@@ -23,7 +23,7 @@ require_relative "version_string"
class Chef
CHEF_ROOT = File.expand_path("..", __dir__)
- VERSION = Chef::VersionString.new("16.5.33")
+ VERSION = Chef::VersionString.new("16.5.36")
end
#
diff --git a/lib/chef/win32/registry.rb b/lib/chef/win32/registry.rb
index 6a9f229b27..4b5f8ede41 100644
--- a/lib/chef/win32/registry.rb
+++ b/lib/chef/win32/registry.rb
@@ -21,9 +21,8 @@ require_relative "api"
require_relative "../mixin/wide_string"
if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
- require_relative "../monkey_patches/win32/registry"
+ Win32.autoload :Registry, File.expand_path("../monkey_patches/win32/registry", __dir__)
require_relative "api/registry"
- require "win32/registry" unless defined?(Win32::Registry)
require "win32/api"
end
diff --git a/spec/unit/data_collector/config_validation_spec.rb b/spec/unit/data_collector/config_validation_spec.rb
new file mode 100644
index 0000000000..d00811f3ef
--- /dev/null
+++ b/spec/unit/data_collector/config_validation_spec.rb
@@ -0,0 +1,207 @@
+#
+# Copyright:: Copyright (c) Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+require "chef/data_collector/config_validation"
+
+describe Chef::DataCollector::ConfigValidation do
+ describe "#should_be_enabled?" do
+ shared_examples_for "a solo-like run" do
+ it "is disabled in solo-legacy without a data_collector url and token" do
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ it "is disabled in solo-legacy with only a url" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ it "is disabled in solo-legacy with only a token" do
+ Chef::Config[:data_collector][:token] = "admit_one"
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ it "is enabled in solo-legacy with both a token and url" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ Chef::Config[:data_collector][:token] = "no_cash_value"
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+
+ it "is enabled in solo-legacy with only an output location to a file" do
+ Chef::Config[:data_collector][:output_locations] = { files: [ "/always/be/counting/down" ] }
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+
+ it "is disabled in solo-legacy with only an output location to a uri" do
+ Chef::Config[:data_collector][:output_locations] = { urls: [ "https://esa.local/ariane5" ] }
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ it "is enabled in solo-legacy with only an output location to a uri with a token" do
+ Chef::Config[:data_collector][:output_locations] = { urls: [ "https://esa.local/ariane5" ] }
+ Chef::Config[:data_collector][:token] = "good_for_one_fare"
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+
+ it "is enabled in solo-legacy when the mode is :solo" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ Chef::Config[:data_collector][:token] = "non_redeemable"
+ Chef::Config[:data_collector][:mode] = :solo
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+
+ it "is enabled in solo-legacy when the mode is :both" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ Chef::Config[:data_collector][:token] = "non_negotiable"
+ Chef::Config[:data_collector][:mode] = :both
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+
+ it "is disabled in solo-legacy when the mode is :client" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ Chef::Config[:data_collector][:token] = "NYCTA"
+ Chef::Config[:data_collector][:mode] = :client
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ it "is disabled in solo-legacy mode when the mode is :nonsense" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ Chef::Config[:data_collector][:token] = "MTA"
+ Chef::Config[:data_collector][:mode] = :nonsense
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+ end
+
+ it "by default it is enabled" do
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+
+ it "is disabled in why-run" do
+ Chef::Config[:why_run] = true
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ describe "a solo legacy run" do
+ before(:each) do
+ Chef::Config[:solo_legacy_mode] = true
+ end
+
+ it_behaves_like "a solo-like run"
+ end
+
+ describe "a local mode run" do
+ before(:each) do
+ Chef::Config[:local_mode] = true
+ end
+
+ it_behaves_like "a solo-like run"
+ end
+
+ it "is enabled in client mode when the mode is :both" do
+ Chef::Config[:data_collector][:mode] = :both
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+
+ it "is disabled in client mode when the mode is :solo" do
+ Chef::Config[:data_collector][:mode] = :solo
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ it "is disabled in client mode when the mode is :nonsense" do
+ Chef::Config[:data_collector][:mode] = :nonsense
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
+ end
+
+ it "is still enabled if you set a token in client mode" do
+ Chef::Config[:data_collector][:token] = "good_for_one_ride"
+ expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
+ end
+ end
+
+ describe "validate_server_url!" do
+ it "with valid server url" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ expect(Chef::DataCollector::ConfigValidation.validate_server_url!).to be_nil
+ end
+
+ it "with invalid server URL" do
+ Chef::Config[:data_collector][:server_url] = "not valid URL"
+ expect { Chef::DataCollector::ConfigValidation.validate_server_url! }.to raise_error(Chef::Exceptions::ConfigurationError,
+ "Chef::Config[:data_collector][:server_url] (not valid URL) is not a valid URI.")
+ end
+
+ it "with invalid server URL without host" do
+ Chef::Config[:data_collector][:server_url] = "no-host"
+ expect { Chef::DataCollector::ConfigValidation.validate_server_url! }.to raise_error(Chef::Exceptions::ConfigurationError,
+ "Chef::Config[:data_collector][:server_url] (no-host) is a URI with no host. Please supply a valid URL.")
+ end
+
+ it "skip validation if output_locations is set" do
+ Chef::Config[:data_collector][:output_locations] = { files: ["https://www.esa.local/ariane5"] }
+ expect(Chef::DataCollector::ConfigValidation.validate_server_url!).to be_nil
+ end
+
+ it "skip validation if output_locations & server_url both are set" do
+ Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
+ Chef::Config[:data_collector][:output_locations] = { files: ["https://www.esa.local/ariane5"] }
+ expect(Chef::DataCollector::ConfigValidation.validate_server_url!).to be_nil
+ end
+ end
+
+ describe "validate_output_locations!" do
+ it "with nil or not set skip validation" do
+ Chef::Config[:data_collector][:output_locations] = nil
+ expect(Chef::DataCollector::ConfigValidation.validate_output_locations!).to be_nil
+ end
+
+ it "with empty value raise validation error" do
+ Chef::Config[:data_collector][:output_locations] = {}
+ expect { Chef::DataCollector::ConfigValidation.validate_output_locations! }.to raise_error(Chef::Exceptions::ConfigurationError,
+ "Chef::Config[:data_collector][:output_locations] is empty. Please supply an hash of valid URLs and / or local file paths.")
+ end
+
+ it "with valid URLs options" do
+ Chef::Config[:data_collector][:output_locations] = { urls: ["https://www.esa.local/ariane5/data-collector"] }
+ expect { Chef::DataCollector::ConfigValidation.validate_output_locations! }.not_to raise_error
+ end
+
+ context "output_locations contains files" do
+ let(:file_path) { "/tmp/client-runs.txt" }
+
+ before(:each) do
+ allow(File).to receive(:exist?).with(file_path).and_return(true)
+ allow(File).to receive(:readable?).with(file_path).and_return(true)
+ allow(File).to receive(:writable?).with(file_path).and_return(true)
+ end
+
+ it "with valid files options" do
+ Chef::Config[:data_collector][:output_locations] = { files: [file_path] }
+ expect { Chef::DataCollector::ConfigValidation.validate_output_locations! }.not_to raise_error
+ end
+
+ it "with valid files & URLs options" do
+ Chef::Config[:data_collector][:output_locations] = { urls: ["https://www.esa.local/ariane5/data-collector"], files: [file_path] }
+ expect { Chef::DataCollector::ConfigValidation.validate_output_locations! }.not_to raise_error
+ end
+
+ it "with valid files options & String location value" do
+ Chef::Config[:data_collector][:output_locations] = { files: file_path }
+ expect { Chef::DataCollector::ConfigValidation.validate_output_locations! }.not_to raise_error
+ end
+ end
+ end
+end
diff --git a/spec/unit/data_collector_spec.rb b/spec/unit/data_collector_spec.rb
index bcce058d81..6d9d5e2379 100644
--- a/spec/unit/data_collector_spec.rb
+++ b/spec/unit/data_collector_spec.rb
@@ -296,119 +296,6 @@ describe Chef::DataCollector do
end
end
- describe "#should_be_enabled?" do
- shared_examples_for "a solo-like run" do
- it "is disabled in solo-legacy without a data_collector url and token" do
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- it "is disabled in solo-legacy with only a url" do
- Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- it "is disabled in solo-legacy with only a token" do
- Chef::Config[:data_collector][:token] = "admit_one"
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- it "is enabled in solo-legacy with both a token and url" do
- Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
- Chef::Config[:data_collector][:token] = "no_cash_value"
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
-
- it "is enabled in solo-legacy with only an output location to a file" do
- Chef::Config[:data_collector][:output_locations] = { files: [ "/always/be/counting/down" ] }
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
-
- it "is disabled in solo-legacy with only an output location to a uri" do
- Chef::Config[:data_collector][:output_locations] = { urls: [ "https://esa.local/ariane5" ] }
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- it "is enabled in solo-legacy with only an output location to a uri with a token" do
- Chef::Config[:data_collector][:output_locations] = { urls: [ "https://esa.local/ariane5" ] }
- Chef::Config[:data_collector][:token] = "good_for_one_fare"
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
-
- it "is enabled in solo-legacy when the mode is :solo" do
- Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
- Chef::Config[:data_collector][:token] = "non_redeemable"
- Chef::Config[:data_collector][:mode] = :solo
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
-
- it "is enabled in solo-legacy when the mode is :both" do
- Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
- Chef::Config[:data_collector][:token] = "non_negotiable"
- Chef::Config[:data_collector][:mode] = :both
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
-
- it "is disabled in solo-legacy when the mode is :client" do
- Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
- Chef::Config[:data_collector][:token] = "NYCTA"
- Chef::Config[:data_collector][:mode] = :client
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- it "is disabled in solo-legacy mode when the mode is :nonsense" do
- Chef::Config[:data_collector][:server_url] = "https://www.esa.local/ariane5"
- Chef::Config[:data_collector][:token] = "MTA"
- Chef::Config[:data_collector][:mode] = :nonsense
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
- end
-
- it "by default it is enabled" do
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
-
- it "is disabled in why-run" do
- Chef::Config[:why_run] = true
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- describe "a solo legacy run" do
- before(:each) do
- Chef::Config[:solo_legacy_mode] = true
- end
-
- it_behaves_like "a solo-like run"
- end
-
- describe "a local mode run" do
- before(:each) do
- Chef::Config[:local_mode] = true
- end
-
- it_behaves_like "a solo-like run"
- end
-
- it "is enabled in client mode when the mode is :both" do
- Chef::Config[:data_collector][:mode] = :both
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
-
- it "is disabled in client mode when the mode is :solo" do
- Chef::Config[:data_collector][:mode] = :solo
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- it "is disabled in client mode when the mode is :nonsense" do
- Chef::Config[:data_collector][:mode] = :nonsense
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be false
- end
-
- it "is still enabled if you set a token in client mode" do
- Chef::Config[:data_collector][:token] = "good_for_one_ride"
- expect(Chef::DataCollector::ConfigValidation.should_be_enabled?).to be true
- end
- end
-
describe "when the run fails during node load" do
let(:exception) { Exception.new("imperial to metric conversion error") }
let(:error_description) { Chef::Formatters::ErrorMapper.registration_failed(node_name, exception, Chef::Config).for_json }