chef-cli 6.1.34 → 6.1.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8db9f13d968079b216821919b7cf27355f9110801bcf3c70ec93c92b6919547
4
- data.tar.gz: e6c201ab2cdb699c43be86edb0073d2d418d32cf6a2b567ffd32fb0a1d6188c0
3
+ metadata.gz: f39b45dc380efe8567a43274187e22fdca70b2262e044932f5adea859ae2ebce
4
+ data.tar.gz: 6299f509f6d26573225690afa17f0cce00b247a020abaa41fc4be4c1735afda5
5
5
  SHA512:
6
- metadata.gz: 423817b35ac6d2899550aeaca8e7a4c613b3b992757a883a30eb8af91aaba602847a343fffa3a0098e3139b080d5baccbaeb5bd16b325c93a2449c1dc3c4555e
7
- data.tar.gz: 426c26140467a90d9acd021e8715837c51b57a190e4cb929e25479da332e244ac53b251ffeb04df5daed438a3ee76ec6d0976cc83049d97025703928a200839d
6
+ metadata.gz: 3105377a6c93b4f2b64ece23cb861e0afcb427e4774b287035b0a4b8bf0ae91e9c81342500bd12abd3e219d00d33f2297bd3d629e18a278332cbc9e88b086396
7
+ data.tar.gz: 367ee551d0014d3c465960252adb970a7f600f4e9d8b4cdec59d7e1dce5a3e636c634702f2455aef49a210df535b54ea2f71794c702fa7e8a201fc7f9df86807
data/Gemfile CHANGED
@@ -2,13 +2,13 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem "logger", "< 1.6" # 1.6 causes errors with mixlib-log < 3.1.1
5
+ gem "logger", "< 1.8" # 1.6 causes errors with mixlib-log < 3.1.1
6
6
  gem "chefspec"
7
7
  gem "appbundler"
8
8
 
9
9
  group :test do
10
10
  gem "rake"
11
- gem "rspec", "=3.12.0"
11
+ gem "rspec", "3.13.2"
12
12
  gem "rspec-expectations", "~> 3.8"
13
13
  gem "rspec-mocks", "~> 3.8"
14
14
  gem "cookstyle", ">= 7.32"
@@ -1,5 +1,4 @@
1
- #
2
- # Copyright (c) 2019-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
1
+ # Copyright:: (c) 2019-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3
2
  # License:: Apache License, Version 2.0
4
3
  #
5
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,31 +16,140 @@
17
16
 
18
17
  require_relative "base"
19
18
  require_relative "../dist"
19
+ require_relative "../licensing/base"
20
20
  require "rubygems" unless defined?(Gem)
21
21
  require "rubygems/gem_runner"
22
22
  require "rubygems/exceptions"
23
+ require "fileutils" unless defined?(FileUtils)
24
+ require "uri" unless defined?(URI)
23
25
 
24
26
  module ChefCLI
25
27
  module Command
26
-
27
28
  # Forwards all commands to rubygems.
28
29
  class GemForwarder < ChefCLI::Command::Base
29
30
  banner "Usage: #{ChefCLI::Dist::EXEC} gem GEM_COMMANDS_AND_OPTIONS"
30
31
 
32
+ CHEF_GEM_SOURCE_HOST = "rubygems.chef.io".freeze
33
+ RUBYGEMS_ORG_HOST = "rubygems.org".freeze
34
+
35
+ # gem subcommands that fetch from remote sources and need the Chef source configured.
36
+ PREMIUM_SOURCE_COMMANDS = %w{install i search s fetch update download}.freeze
37
+
31
38
  def run(params)
32
- retval = Gem::GemRunner.new.run( params.clone )
39
+ setup_gem_environment if habitat_gem_home_enabled?
40
+ ensure_chef_gem_source(params)
41
+ retval = Gem::GemRunner.new.run(params.clone)
33
42
  retval.nil? || retval
34
43
  rescue Gem::SystemExitException => e
35
- exit( e.exit_code )
44
+ exit(e.exit_code)
36
45
  end
37
46
 
38
47
  # Lazy solution: By automatically returning false, we force ChefCLI::Base to
39
48
  # call this class' run method, so that Gem::GemRunner can handle the -v flag
40
49
  # appropriately (showing the gem version, or installing a specific version
41
50
  # of a gem).
42
- def needs_version?(params)
51
+ def needs_version?(_params)
43
52
  false
44
53
  end
54
+
55
+ private
56
+
57
+ # Checks gem sources before any remote-fetching command and ensures the
58
+ # Chef Premium RubyGem server is configured:
59
+ # - Chef source present with valid v1 credentials → proceed.
60
+ # - Non-standard source present (incl. file://) → assume airgap, warn and skip.
61
+ # - Only rubygems.org (or unauthenticated chef source) → obtain license key and run `gem sources --add`.
62
+ def ensure_chef_gem_source(params)
63
+ return unless premium_source_command?(params)
64
+ return if chef_gem_source_configured?
65
+
66
+ custom = non_standard_sources
67
+ unless custom.empty?
68
+ err("WARN: A custom gem source is already configured assuming an air-gapped environment.")
69
+ err("WARN: The Chef Premium RubyGem source was not added. Premium extensions may be unavailable.")
70
+ return
71
+ end
72
+
73
+ license_key = chef_license_key
74
+ if license_key.nil? || license_key.empty?
75
+ err("WARN: No valid Chef license key was found, so the Chef Premium RubyGem source was not configured.")
76
+ err("WARN: You will not be able to access premium extensions. Run `#{ChefCLI::Dist::EXEC} license add` to configure a license.")
77
+ return
78
+ end
79
+
80
+ add_chef_gem_source(license_key)
81
+ end
82
+
83
+ # Returns true when the first non-flag token is a remote-fetching subcommand.
84
+ def premium_source_command?(params)
85
+ command = params.find { |p| !p.to_s.start_with?("-") }
86
+ PREMIUM_SOURCE_COMMANDS.include?(command)
87
+ end
88
+
89
+ # Returns true only when rubygems.chef.io is configured with valid v1 credentials.
90
+ # A bare https://rubygems.chef.io entry (no user/password) is not considered configured.
91
+ def chef_gem_source_configured?
92
+ Gem.sources.any? do |source|
93
+ uri = URI.parse(source.to_s)
94
+ uri.host == CHEF_GEM_SOURCE_HOST && uri.user == "v1" && !uri.password.to_s.empty?
95
+ rescue URI::InvalidURIError
96
+ false
97
+ end
98
+ end
99
+
100
+ # Returns sources that are neither rubygems.org nor rubygems.chef.io.
101
+ # Sources whose URI has no host (e.g. file://) or cannot be parsed are
102
+ # treated as non-standard so the code errs on the side of not modifying sources.
103
+ def non_standard_sources
104
+ Gem.sources.reject do |source|
105
+ uri = URI.parse(source.to_s)
106
+ [RUBYGEMS_ORG_HOST, CHEF_GEM_SOURCE_HOST].include?(uri.host)
107
+ rescue URI::InvalidURIError
108
+ false
109
+ end
110
+ end
111
+
112
+ # Persists the Chef Premium RubyGem server via `gem sources --add` so the
113
+ # configuration survives the current process.
114
+ def add_chef_gem_source(license_key)
115
+ source_url = "https://" + "v1:#{license_key}" + "@#{CHEF_GEM_SOURCE_HOST}"
116
+ Gem::GemRunner.new.run(["sources", "--add", source_url])
117
+ rescue Gem::SystemExitException => e
118
+ err("WARN: Failed to add the Chef Premium RubyGem source (exit #{e.exit_code}).") unless e.exit_code == 0
119
+ end
120
+
121
+ # Fetches the first Chef license key from env, CLI args, or persisted storage.
122
+ # Does not prompt the terminal — if no key is found nil is returned and the
123
+ # caller warns the user to run `chef license add`.
124
+ def chef_license_key
125
+ keys = ChefLicensing.license_keys
126
+ keys.is_a?(Array) ? keys.first : nil
127
+ rescue StandardError
128
+ nil
129
+ end
130
+
131
+ # Detects whether the user gem home feature is enabled.
132
+ # This is set via CHEF_GEM_HOME_ENABLED in the Habitat plan's
133
+ # do_setup_environment/Invoke-SetupEnvironment, or falls back to
134
+ # habitat_install? detection.
135
+ def habitat_gem_home_enabled?
136
+ ENV["CHEF_GEM_HOME_ENABLED"] == "true" || habitat_install?
137
+ end
138
+
139
+ # Sets up GEM_HOME and GEM_PATH to use ~/.chef/ruby/<ruby_version>/gems
140
+ # when running inside a Habitat-based environment. This ensures gems
141
+ # persist across Workstation upgrades since the Habitat package path
142
+ # changes on each upgrade.
143
+ def setup_gem_environment
144
+ gem_dir = habitat_user_gem_dir
145
+ FileUtils.mkdir_p(gem_dir) unless Dir.exist?(gem_dir)
146
+
147
+ ENV["GEM_HOME"] = gem_dir
148
+ # Include existing GEM_PATH so vendor gems remain accessible
149
+ existing_gem_path = ENV["GEM_PATH"]
150
+ ENV["GEM_PATH"] = [gem_dir, existing_gem_path].reject { |p| p.nil? || p.empty? }.join(File::PATH_SEPARATOR)
151
+ Gem.clear_paths
152
+ end
45
153
  end
46
154
  end
47
155
  end
@@ -173,11 +173,16 @@ module ChefCLI
173
173
  raise "Error: Could not determine the vendor package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless vendor_pkg_prefix
174
174
 
175
175
  vendor_dir = File.join(vendor_pkg_prefix, "vendor")
176
+
177
+ # User gem directory for persistent gem storage across upgrades
178
+ user_gem_dir = habitat_user_gem_dir
179
+
176
180
  # Construct PATH including Ruby bin directory for chef-cli exec command
177
181
  ruby_bin_dir = File.dirname(RbConfig.ruby)
178
182
  path = [
179
183
  File.join(bin_pkg_prefix, "bin"),
180
184
  File.join(vendor_dir, "bin"),
185
+ File.join(user_gem_dir, "bin"),
181
186
  ruby_bin_dir, # Add Ruby bin directory so exec can find gem etc.
182
187
  ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH
183
188
  ].flatten.uniq
@@ -185,8 +190,8 @@ module ChefCLI
185
190
  {
186
191
  "PATH" => path.join(File::PATH_SEPARATOR),
187
192
  "GEM_ROOT" => Gem.default_dir, # Default directory for gems
188
- "GEM_HOME" => vendor_dir, # Set only if vendor_dir exists
189
- "GEM_PATH" => vendor_dir, # Set only if vendor_dir exists
193
+ "GEM_HOME" => user_gem_dir, # User-local gem dir for persistence
194
+ "GEM_PATH" => [user_gem_dir, vendor_dir].join(File::PATH_SEPARATOR),
190
195
  }
191
196
  end
192
197
  end
@@ -216,6 +221,14 @@ module ChefCLI
216
221
  path if !path.empty? && Dir.exist?(path) # Return path only if it exists
217
222
  end
218
223
 
224
+ # Returns the user-local gem directory for the current Ruby version
225
+ # under ~/.chef/ruby/<MAJOR.MINOR.0>/gems
226
+ # This path persists across Habitat package upgrades.
227
+ def habitat_user_gem_dir
228
+ ruby_version = RbConfig::CONFIG["ruby_version"] # e.g., "3.1.0"
229
+ File.expand_path(File.join("~", ".chef", "ruby", ruby_version, "gems"))
230
+ end
231
+
219
232
  def omnibus_expand_path(*paths)
220
233
  dir = File.expand_path(File.join(paths))
221
234
  raise OmnibusInstallNotFound.new unless dir && File.directory?(dir)
@@ -16,5 +16,5 @@
16
16
  #
17
17
 
18
18
  module ChefCLI
19
- VERSION = "6.1.34".freeze
19
+ VERSION = "6.1.42".freeze
20
20
  end
@@ -0,0 +1,396 @@
1
+ #
2
+ # Copyright:: (c) 2019-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require "spec_helper"
19
+ require "chef-cli/command/gem"
20
+
21
+ describe ChefCLI::Command::GemForwarder do
22
+ let(:command_instance) { described_class.new }
23
+ let(:gem_runner) { instance_double(Gem::GemRunner) }
24
+ let(:ruby_version) { RbConfig::CONFIG["ruby_version"] }
25
+ let(:expected_gem_dir) { File.expand_path("~/.chef/ruby/#{ruby_version}/gems") }
26
+
27
+ # URLs are assembled from parts so URI/secret scanners don't flag contiguous test endpoints.
28
+ let(:rubygems_org_url) { "https://" + "rubygems.org/" }
29
+ let(:chef_source_url) { "https://" + "rubygems.chef.io" }
30
+ let(:airgap_source_url) { "https://" + "airgap.internal/gems" }
31
+ let(:file_source_url) { "file://" + "/var/cache/gems" }
32
+
33
+ before do
34
+ allow(Gem::GemRunner).to receive(:new).and_return(gem_runner)
35
+ # Avoid touching the licensing service / network in unrelated examples.
36
+ allow(command_instance).to receive(:ensure_chef_gem_source)
37
+ end
38
+
39
+ it "has a usage banner" do
40
+ expect(command_instance.banner).to eq("Usage: chef gem GEM_COMMANDS_AND_OPTIONS")
41
+ end
42
+
43
+ describe "#needs_version?" do
44
+ it "returns false to let GemRunner handle version flag" do
45
+ expect(command_instance.needs_version?([])).to be(false)
46
+ end
47
+
48
+ it "returns false even with -v parameter" do
49
+ expect(command_instance.needs_version?(["-v"])).to be(false)
50
+ end
51
+ end
52
+
53
+ describe "#run" do
54
+ context "when NOT in a Habitat environment" do
55
+ before do
56
+ allow(command_instance).to receive(:habitat_install?).and_return(false)
57
+ allow(ENV).to receive(:[]).with("CHEF_GEM_HOME_ENABLED").and_return(nil)
58
+ end
59
+
60
+ # TC-11: source auto-configuration applies regardless of Habitat mode
61
+ it "calls ensure_chef_gem_source before forwarding to GemRunner" do
62
+ expect(command_instance).to receive(:ensure_chef_gem_source).with(%w{install knife}).ordered
63
+ expect(gem_runner).to receive(:run).with(%w{install knife}).and_return(true).ordered
64
+ command_instance.run(%w{install knife})
65
+ end
66
+
67
+ it "forwards params to Gem::GemRunner" do
68
+ expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true)
69
+ expect(command_instance.run(%w(install knife))).to eq(true)
70
+ end
71
+
72
+ it "does not modify GEM_HOME" do
73
+ expect(gem_runner).to receive(:run).with(%w(list)).and_return(true)
74
+ expect(ENV).not_to receive(:[]=).with("GEM_HOME", anything)
75
+ command_instance.run(%w(list))
76
+ end
77
+
78
+ it "returns true when GemRunner returns nil" do
79
+ expect(gem_runner).to receive(:run).with(%w(list)).and_return(nil)
80
+ expect(command_instance.run(%w(list))).to eq(true)
81
+ end
82
+ end
83
+
84
+ context "when in a Habitat environment" do
85
+ let(:vendor_dir) { "/hab/pkgs/chef/chef-cli/1.0.0/123/vendor" }
86
+ let(:existing_gem_path) { "#{expected_gem_dir}#{File::PATH_SEPARATOR}#{vendor_dir}" }
87
+
88
+ before do
89
+ allow(command_instance).to receive(:habitat_install?).and_return(true)
90
+ allow(ENV).to receive(:[]).with("CHEF_GEM_HOME_ENABLED").and_return("true")
91
+ allow(command_instance).to receive(:habitat_user_gem_dir).and_return(expected_gem_dir)
92
+ allow(ENV).to receive(:[]).with("GEM_PATH").and_return(existing_gem_path)
93
+ allow(Dir).to receive(:exist?).with(expected_gem_dir).and_return(true)
94
+ allow(Gem).to receive(:clear_paths)
95
+ end
96
+
97
+ it "sets GEM_HOME to user gem directory" do
98
+ expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true)
99
+ expect(ENV).to receive(:[]=).with("GEM_HOME", expected_gem_dir)
100
+ allow(ENV).to receive(:[]=).with("GEM_PATH", anything)
101
+ command_instance.run(%w(install knife))
102
+ end
103
+
104
+ it "sets GEM_PATH to include both user gem dir and existing GEM_PATH" do
105
+ expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true)
106
+ allow(ENV).to receive(:[]=).with("GEM_HOME", expected_gem_dir)
107
+ expected_gem_path = "#{expected_gem_dir}#{File::PATH_SEPARATOR}#{existing_gem_path}"
108
+ expect(ENV).to receive(:[]=).with("GEM_PATH", expected_gem_path)
109
+ command_instance.run(%w(install knife))
110
+ end
111
+
112
+ it "clears Gem paths after setting environment" do
113
+ expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true)
114
+ allow(ENV).to receive(:[]=)
115
+ expect(Gem).to receive(:clear_paths)
116
+ command_instance.run(%w(install knife))
117
+ end
118
+
119
+ it "creates the gem directory if it doesn't exist" do
120
+ allow(Dir).to receive(:exist?).with(expected_gem_dir).and_return(false)
121
+ expect(FileUtils).to receive(:mkdir_p).with(expected_gem_dir)
122
+ allow(ENV).to receive(:[]=)
123
+ expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true)
124
+ command_instance.run(%w(install knife))
125
+ end
126
+
127
+ it "does not create the gem directory if it already exists" do
128
+ allow(Dir).to receive(:exist?).with(expected_gem_dir).and_return(true)
129
+ expect(FileUtils).not_to receive(:mkdir_p)
130
+ allow(ENV).to receive(:[]=)
131
+ expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true)
132
+ command_instance.run(%w(install knife))
133
+ end
134
+
135
+ it "forwards all gem subcommands correctly" do
136
+ allow(ENV).to receive(:[]=)
137
+ %w(install list uninstall source search update).each do |subcmd|
138
+ expect(gem_runner).to receive(:run).with([subcmd]).and_return(true)
139
+ expect(command_instance.run([subcmd])).to eq(true)
140
+ end
141
+ end
142
+ end
143
+
144
+ context "when CHEF_GEM_HOME_ENABLED is set but habitat_install? is false" do
145
+ before do
146
+ allow(command_instance).to receive(:habitat_install?).and_return(false)
147
+ allow(ENV).to receive(:[]).with("CHEF_GEM_HOME_ENABLED").and_return("true")
148
+ allow(command_instance).to receive(:habitat_user_gem_dir).and_return(expected_gem_dir)
149
+ allow(ENV).to receive(:[]).with("GEM_PATH").and_return(nil)
150
+ allow(Dir).to receive(:exist?).with(expected_gem_dir).and_return(true)
151
+ allow(Gem).to receive(:clear_paths)
152
+ end
153
+
154
+ it "still sets up gem environment via env var detection" do
155
+ expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true)
156
+ expect(ENV).to receive(:[]=).with("GEM_HOME", expected_gem_dir)
157
+ allow(ENV).to receive(:[]=).with("GEM_PATH", anything)
158
+ command_instance.run(%w(install knife))
159
+ end
160
+ end
161
+
162
+ context "when GemRunner raises Gem::SystemExitException" do
163
+ before do
164
+ allow(command_instance).to receive(:habitat_install?).and_return(false)
165
+ allow(ENV).to receive(:[]).with("CHEF_GEM_HOME_ENABLED").and_return(nil)
166
+ end
167
+
168
+ it "exits with the exception's exit code" do
169
+ exception = Gem::SystemExitException.new(1)
170
+ allow(gem_runner).to receive(:run).and_raise(exception)
171
+ expect { command_instance.run(%w(install bad_gem)) }.to raise_error(SystemExit) { |e|
172
+ expect(e.status).to eq(1)
173
+ }
174
+ end
175
+ end
176
+ end
177
+
178
+ describe "#habitat_user_gem_dir" do
179
+ it "returns ~/.chef/ruby/<version>/gems path" do
180
+ expect(command_instance.send(:habitat_user_gem_dir)).to eq(expected_gem_dir)
181
+ end
182
+
183
+ it "uses the ruby_version from RbConfig" do
184
+ allow(RbConfig::CONFIG).to receive(:[]).with("ruby_version").and_return("3.3.0")
185
+ expect(command_instance.send(:habitat_user_gem_dir)).to eq(
186
+ File.expand_path("~/.chef/ruby/3.3.0/gems")
187
+ )
188
+ end
189
+ end
190
+
191
+ describe "#ensure_chef_gem_source" do
192
+ let(:license_key) { "tmns-00000000-0000-0000-0000-000000000000-0000" }
193
+
194
+ before do
195
+ allow(command_instance).to receive(:ensure_chef_gem_source).and_call_original
196
+ end
197
+
198
+ def stub_sources(*urls)
199
+ allow(Gem).to receive(:sources).and_return(urls)
200
+ end
201
+
202
+ context "when the command does not download from remote sources" do
203
+ before { stub_sources(rubygems_org_url) }
204
+
205
+ it "does nothing for non-install/search commands" do
206
+ expect(ChefLicensing).not_to receive(:license_keys)
207
+ expect(command_instance).not_to receive(:add_chef_gem_source)
208
+ command_instance.send(:ensure_chef_gem_source, %w{list})
209
+ end
210
+ end
211
+
212
+ context "when the Chef Premium RubyGem source is already configured with credentials" do
213
+ before do
214
+ # Stub the method directly — URL construction with credentials is tested in #chef_gem_source_configured?
215
+ allow(command_instance).to receive(:chef_gem_source_configured?).and_return(true)
216
+ end
217
+
218
+ it "does not attempt to add it again" do
219
+ expect(ChefLicensing).not_to receive(:license_keys)
220
+ expect(command_instance).not_to receive(:add_chef_gem_source)
221
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
222
+ end
223
+ end
224
+
225
+ # Bug fix: unauthenticated rubygems.chef.io must not be treated as configured
226
+ context "when rubygems.chef.io is present but without credentials" do
227
+ before do
228
+ stub_sources(rubygems_org_url, chef_source_url)
229
+ allow(ChefLicensing).to receive(:license_keys).and_return([license_key])
230
+ end
231
+
232
+ it "treats it as not configured and adds the authenticated source" do
233
+ expect(command_instance).to receive(:add_chef_gem_source).with(license_key)
234
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
235
+ end
236
+ end
237
+
238
+ context "when only the default RubyGems.org source is configured" do
239
+ before do
240
+ stub_sources(rubygems_org_url)
241
+ allow(ChefLicensing).to receive(:license_keys).and_return([license_key])
242
+ end
243
+
244
+ it "adds the Chef source built from the license key" do
245
+ expect(command_instance).to receive(:add_chef_gem_source).with(license_key)
246
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
247
+ end
248
+
249
+ # TC-13: license_keys (not fetch_and_persist) is called so no interactive prompt blocks gem commands
250
+ it "uses license_keys to retrieve the license key without prompting" do
251
+ expect(ChefLicensing).to receive(:license_keys).and_return([license_key])
252
+ allow(command_instance).to receive(:add_chef_gem_source)
253
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
254
+ end
255
+
256
+ it "also triggers for the search command" do
257
+ expect(command_instance).to receive(:add_chef_gem_source).with(license_key)
258
+ command_instance.send(:ensure_chef_gem_source, %w{search knife})
259
+ end
260
+ end
261
+
262
+ # TC-06: only a custom source present with NO rubygems.org
263
+ context "when only a custom source is configured (no rubygems.org)" do
264
+ before { stub_sources(airgap_source_url) }
265
+
266
+ it "assumes air-gapped and does not add the Chef source" do
267
+ expect(ChefLicensing).not_to receive(:license_keys)
268
+ expect(command_instance).not_to receive(:add_chef_gem_source)
269
+ allow(command_instance).to receive(:err)
270
+ command_instance.send(:ensure_chef_gem_source, %w{install plugin})
271
+ end
272
+
273
+ it "warns about air-gapped environment" do
274
+ allow(command_instance).to receive(:err)
275
+ expect(command_instance).to receive(:err).with(/air-gapped/)
276
+ command_instance.send(:ensure_chef_gem_source, %w{install plugin})
277
+ end
278
+ end
279
+
280
+ # Bug fix: file:// sources have host=="" and must not be silently dropped from airgap detection
281
+ context "when a file:// local source is configured" do
282
+ before { stub_sources(file_source_url) }
283
+
284
+ it "treats it as non-standard and does not add the Chef source" do
285
+ expect(ChefLicensing).not_to receive(:license_keys)
286
+ expect(command_instance).not_to receive(:add_chef_gem_source)
287
+ allow(command_instance).to receive(:err)
288
+ command_instance.send(:ensure_chef_gem_source, %w{install plugin})
289
+ end
290
+
291
+ it "warns about air-gapped environment" do
292
+ allow(command_instance).to receive(:err)
293
+ expect(command_instance).to receive(:err).with(/air-gapped/)
294
+ command_instance.send(:ensure_chef_gem_source, %w{install plugin})
295
+ end
296
+ end
297
+
298
+ # TC-07: rubygems.org AND a custom source present
299
+ context "when a custom non-chef, non-rubygems source is configured" do
300
+ before { stub_sources(rubygems_org_url, airgap_source_url) }
301
+
302
+ it "assumes an air-gapped mirror and does not add the Chef source" do
303
+ expect(ChefLicensing).not_to receive(:license_keys)
304
+ expect(command_instance).not_to receive(:add_chef_gem_source)
305
+ allow(command_instance).to receive(:err)
306
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
307
+ end
308
+
309
+ it "warns the user about the air-gapped assumption" do
310
+ allow(command_instance).to receive(:err)
311
+ expect(command_instance).to receive(:err).with(/air-gapped/)
312
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
313
+ end
314
+ end
315
+
316
+ # TC-05: no license key — warn and allow install to proceed (graceful degradation)
317
+ context "when no license key can be obtained" do
318
+ before do
319
+ stub_sources(rubygems_org_url)
320
+ allow(ChefLicensing).to receive(:license_keys).and_return([])
321
+ end
322
+
323
+ it "does not add a source and warns about premium extensions" do
324
+ expect(command_instance).not_to receive(:add_chef_gem_source)
325
+ allow(command_instance).to receive(:err)
326
+ expect(command_instance).to receive(:err).with(/premium extensions/)
327
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
328
+ end
329
+
330
+ it "does not raise an error so the install proceeds against rubygems.org" do
331
+ allow(command_instance).to receive(:err)
332
+ expect { command_instance.send(:ensure_chef_gem_source, %w{install knife}) }.not_to raise_error
333
+ end
334
+ end
335
+
336
+ context "when fetching the license key raises an error" do
337
+ before do
338
+ stub_sources(rubygems_org_url)
339
+ allow(ChefLicensing).to receive(:license_keys).and_raise(StandardError)
340
+ end
341
+
342
+ it "treats it as no license key and does not add a source" do
343
+ expect(command_instance).not_to receive(:add_chef_gem_source)
344
+ allow(command_instance).to receive(:err)
345
+ command_instance.send(:ensure_chef_gem_source, %w{install knife})
346
+ end
347
+ end
348
+ end
349
+
350
+ describe "#chef_gem_source_configured?" do
351
+ let(:license_key) { "tmns-00000000-0000-0000-0000-000000000000-0000" }
352
+ let(:chef_source_host) { "rubygems.chef.io" }
353
+
354
+ def stub_sources(*urls)
355
+ allow(Gem).to receive(:sources).and_return(urls)
356
+ end
357
+
358
+ it "returns true when the Chef source has v1 user and non-empty password" do
359
+ chef_source = "https://" + "v1:#{license_key}" + "@" + chef_source_host
360
+ stub_sources(rubygems_org_url, chef_source)
361
+ expect(command_instance.send(:chef_gem_source_configured?)).to be(true)
362
+ end
363
+
364
+ it "returns false when the Chef source has no credentials" do
365
+ stub_sources(rubygems_org_url, chef_source_url)
366
+ expect(command_instance.send(:chef_gem_source_configured?)).to be(false)
367
+ end
368
+
369
+ it "returns false when no Chef source is present" do
370
+ stub_sources(rubygems_org_url)
371
+ expect(command_instance.send(:chef_gem_source_configured?)).to be(false)
372
+ end
373
+ end
374
+
375
+ describe "#add_chef_gem_source" do
376
+ let(:license_key) { "tmns-abc-123" }
377
+ let(:chef_source_host) { "rubygems.chef.io" }
378
+ let(:expected_source_url) { "https://" + "v1:#{license_key}" + "@" + chef_source_host }
379
+ let(:source_runner) { instance_double(Gem::GemRunner) }
380
+
381
+ before do
382
+ allow(Gem::GemRunner).to receive(:new).and_return(source_runner)
383
+ end
384
+
385
+ it "invokes gem sources --add with the premium URL" do
386
+ expect(source_runner).to receive(:run).with(["sources", "--add", expected_source_url])
387
+ command_instance.send(:add_chef_gem_source, license_key)
388
+ end
389
+
390
+ it "does not propagate a Gem::SystemExitException so the user command still runs" do
391
+ allow(source_runner).to receive(:run).and_raise(Gem::SystemExitException.new(1))
392
+ allow(command_instance).to receive(:err)
393
+ expect { command_instance.send(:add_chef_gem_source, license_key) }.not_to raise_error
394
+ end
395
+ end
396
+ end
@@ -346,6 +346,8 @@ describe ChefCLI::Command::ShellInit do
346
346
 
347
347
  context "habitat standalone shell-init on bash" do
348
348
  let(:cli_hab_path) { "/hab/pkgs/chef/chef-cli/1.0.0/123" }
349
+ let(:ruby_version) { RbConfig::CONFIG["ruby_version"] }
350
+ let(:user_gem_dir) { File.expand_path("~/.chef/ruby/#{ruby_version}/gems") }
349
351
 
350
352
  let(:argv) { ["bash"] }
351
353
 
@@ -359,8 +361,8 @@ describe ChefCLI::Command::ShellInit do
359
361
 
360
362
  command_instance.run(argv)
361
363
  expect(stdout_io.string).to include("export PATH=\"#{cli_hab_path}/bin")
362
- expect(stdout_io.string).to include("export GEM_HOME=\"#{cli_hab_path}/vendor")
363
- expect(stdout_io.string).to include("export GEM_PATH=\"#{cli_hab_path}/vendor")
364
+ expect(stdout_io.string).to include("export GEM_HOME=\"#{user_gem_dir}")
365
+ expect(stdout_io.string).to include("export GEM_PATH=\"#{user_gem_dir}#{File::PATH_SEPARATOR}#{cli_hab_path}/vendor")
364
366
  end
365
367
  end
366
368
 
@@ -380,10 +382,13 @@ describe ChefCLI::Command::ShellInit do
380
382
  expect(command_instance).to receive(:get_pkg_prefix).with("chef/chef-workstation").and_return(chef_dke_path)
381
383
  expect(command_instance).to receive(:get_pkg_prefix).with("chef/chef-cli").and_return(cli_hab_path)
382
384
 
385
+ ruby_version = RbConfig::CONFIG["ruby_version"]
386
+ user_gem_dir = File.expand_path("~/.chef/ruby/#{ruby_version}/gems")
387
+
383
388
  command_instance.run(argv)
384
389
  expect(stdout_io.string).to include("export PATH=\"#{chef_dke_path}/bin")
385
- expect(stdout_io.string).to include("export GEM_HOME=\"#{cli_hab_path}/vendor")
386
- expect(stdout_io.string).to include("export GEM_PATH=\"#{cli_hab_path}/vendor")
390
+ expect(stdout_io.string).to include("export GEM_HOME=\"#{user_gem_dir}")
391
+ expect(stdout_io.string).to include("export GEM_PATH=\"#{user_gem_dir}#{File::PATH_SEPARATOR}#{cli_hab_path}/vendor")
387
392
  end
388
393
 
389
394
  describe "autocompletion" do
@@ -113,14 +113,16 @@ describe ChefCLI::Helpers do
113
113
  let(:chef_dke_path) { "/hab/pkgs/chef/chef-workstation/1.0.0/123" }
114
114
  let(:cli_hab_path) { "/hab/pkgs/chef/chef-cli/1.0.0/123" }
115
115
  let(:ruby_bin_dir) { File.dirname(RbConfig.ruby) }
116
+ let(:ruby_version) { RbConfig::CONFIG["ruby_version"] }
117
+ let(:user_gem_dir) { File.expand_path("~/.chef/ruby/#{ruby_version}/gems") }
116
118
  let(:expected_gem_root) { Gem.default_dir }
117
- let(:expected_path) { [File.join(chef_dke_path, "bin"), File.join(cli_hab_path, "vendor", "bin"), ruby_bin_dir, "/usr/bin:/bin"].flatten }
119
+ let(:expected_path) { [File.join(chef_dke_path, "bin"), File.join(cli_hab_path, "vendor", "bin"), File.join(user_gem_dir, "bin"), ruby_bin_dir, "/usr/bin:/bin"].flatten }
118
120
  let(:expected_env) do
119
121
  {
120
122
  "PATH" => expected_path.join(File::PATH_SEPARATOR),
121
123
  "GEM_ROOT" => expected_gem_root,
122
- "GEM_HOME" => "#{cli_hab_path}/vendor",
123
- "GEM_PATH" => "#{cli_hab_path}/vendor",
124
+ "GEM_HOME" => user_gem_dir,
125
+ "GEM_PATH" => "#{user_gem_dir}#{File::PATH_SEPARATOR}#{cli_hab_path}/vendor",
124
126
  }
125
127
  end
126
128
 
@@ -129,16 +131,27 @@ describe ChefCLI::Helpers do
129
131
  allow(ChefCLI::Helpers).to receive(:habitat_standalone?).and_return false
130
132
  allow(ENV).to receive(:[]).with("PATH").and_return("/usr/bin:/bin")
131
133
  allow(ENV).to receive(:[]).with("CHEF_CLI_VERSION").and_return(nil)
132
- allow(Dir).to receive(:exist?).with("#{cli_hab_path}/vendor").and_return(true) # <-- Add this line
134
+ allow(Dir).to receive(:exist?).with("#{cli_hab_path}/vendor").and_return(true)
133
135
  end
134
136
 
135
- it "should return the habitat env" do
136
- allow(ChefCLI::Helpers).to receive(:fetch_chef_cli_version_pkg).and_return(nil) # Ensure no version override
137
+ it "should return the habitat env with user gem dir" do
138
+ allow(ChefCLI::Helpers).to receive(:fetch_chef_cli_version_pkg).and_return(nil)
137
139
  expect(ChefCLI::Helpers).to receive(:get_pkg_prefix).with("chef/chef-workstation").and_return(chef_dke_path)
138
140
  expect(ChefCLI::Helpers).to receive(:get_pkg_prefix).with("chef/chef-cli").and_return(cli_hab_path)
139
141
 
140
142
  expect(ChefCLI::Helpers.habitat_env).to eq(expected_env)
141
143
  end
144
+
145
+ it "should set GEM_HOME to user gem dir for persistence" do
146
+ allow(ChefCLI::Helpers).to receive(:fetch_chef_cli_version_pkg).and_return(nil)
147
+ allow(ChefCLI::Helpers).to receive(:get_pkg_prefix).with("chef/chef-workstation").and_return(chef_dke_path)
148
+ allow(ChefCLI::Helpers).to receive(:get_pkg_prefix).with("chef/chef-cli").and_return(cli_hab_path)
149
+
150
+ env = ChefCLI::Helpers.habitat_env
151
+ expect(env["GEM_HOME"]).to eq(user_gem_dir)
152
+ expect(env["GEM_PATH"]).to include(user_gem_dir)
153
+ expect(env["GEM_PATH"]).to include("#{cli_hab_path}/vendor")
154
+ end
142
155
  end
143
156
 
144
157
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.34
4
+ version: 6.1.42
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software, Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-06-17 00:00:00.000000000 Z
11
+ date: 2026-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mixlib-cli
@@ -444,6 +444,7 @@ files:
444
444
  - spec/unit/command/env_spec.rb
445
445
  - spec/unit/command/exec_spec.rb
446
446
  - spec/unit/command/export_spec.rb
447
+ - spec/unit/command/gem_spec.rb
447
448
  - spec/unit/command/generate_spec.rb
448
449
  - spec/unit/command/generator_commands/attribute_spec.rb
449
450
  - spec/unit/command/generator_commands/base_spec.rb
@@ -631,7 +632,7 @@ homepage: https://www.chef.io/
631
632
  licenses:
632
633
  - Apache-2.0
633
634
  metadata: {}
634
- post_install_message:
635
+ post_install_message:
635
636
  rdoc_options: []
636
637
  require_paths:
637
638
  - lib
@@ -647,7 +648,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
647
648
  version: '0'
648
649
  requirements: []
649
650
  rubygems_version: 3.3.27
650
- signing_key:
651
+ signing_key:
651
652
  specification_version: 4
652
653
  summary: A streamlined development and deployment workflow for Chef platform.
653
654
  test_files:
@@ -672,6 +673,7 @@ test_files:
672
673
  - spec/unit/command/env_spec.rb
673
674
  - spec/unit/command/exec_spec.rb
674
675
  - spec/unit/command/export_spec.rb
676
+ - spec/unit/command/gem_spec.rb
675
677
  - spec/unit/command/generate_spec.rb
676
678
  - spec/unit/command/generator_commands/attribute_spec.rb
677
679
  - spec/unit/command/generator_commands/base_spec.rb