chef-licensing 1.3.4 → 1.4.0

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: 40ddbe461d4e43fca7603989dbd66f6624558e733eef96d0fdf72b681a870e58
4
- data.tar.gz: 34fb380e627d5e4a5fac88cb50afa968c3deb22d7bd0cf51cd0d846d8f8aab3a
3
+ metadata.gz: a915fc02b070fed2b74e6051992251979b2a0707655126745ad41de2be112b83
4
+ data.tar.gz: f8ee3a481539d4b1d16fc82cd6c21c82a8162241ea80846015a1e253f1b86069
5
5
  SHA512:
6
- metadata.gz: 19e61c49f036e5d074c17469cda52d3695add42a143528ec4420aebaef9d1d053c4efb7049a9d864610369ac3e9e19a649ee97f1c12b833a22370ff081884af6
7
- data.tar.gz: dcd537c41f2928bb92bbaf8a0eb6f5a4731bd79f182b2bad128d789158ebf774c87e9e46f846eedaedb29f18b36830e92dacc2af5a6a6bc3aed96daf87b65c66
6
+ metadata.gz: 84a9d317eb6e0369306179b2eb2de88940264d8c4734d02b40cf4b9cfa1be596ffc807f3e2446fe008c8b5c8e16f59ef1aa716e171390c7ee826498ffaf4f111
7
+ data.tar.gz: 3a23a0987a2889f95cee60d9a1eff1d2385f586d18f9a64b116e1c33d8529b749054186b0ba28558dcf7b57e1abf8e8429bfe79257a3bad2516721fc0265a4da
@@ -17,13 +17,15 @@ module ChefLicensing
17
17
  attr_writer :license_server_url, :logger, :output, :license_server_url_check_in_file, :license_add_command, :license_list_command, :make_licensing_optional
18
18
 
19
19
  # is_local_license_service is used by context class
20
- attr_accessor :is_local_license_service, :chef_entitlement_id, :chef_product_name, :chef_executable_name
20
+ attr_accessor :is_local_license_service, :chef_entitlement_id, :chef_product_name, :chef_executable_name, :persist_license_data
21
21
 
22
22
  def license_server_url(opts = {})
23
23
  return @license_server_url if @license_server_url && @license_server_url_check_in_file
24
24
 
25
25
  license_server_url_from_system = ChefLicensing::ArgFetcher.fetch_value("--chef-license-server", :string) || ChefLicensing::EnvFetcher.fetch_value("CHEF_LICENSE_SERVER", :string)
26
- @license_server_url = ChefLicensing::LicenseKeyFetcher::File.fetch_or_persist_url(@license_server_url, license_server_url_from_system, opts)
26
+ # Default persist to true for backward compatibility, but respect the config setting if set
27
+ persist = @persist_license_data.nil? || @persist_license_data
28
+ @license_server_url = ChefLicensing::LicenseKeyFetcher::File.fetch_or_persist_url(@license_server_url, license_server_url_from_system, opts.merge(persist: persist))
27
29
  @license_server_url_check_in_file = true
28
30
  @license_server_url
29
31
  end
@@ -129,24 +129,30 @@ module ChefLicensing
129
129
  !!seek
130
130
  end
131
131
 
132
- def fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil)
132
+ def fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil, persist: true)
133
133
  dir = @opts[:dir]
134
134
  license_key_file_path = "#{dir}/#{LICENSE_KEY_FILE}"
135
- create_license_directory_if_not_exist(dir, license_key_file_path)
135
+ create_license_directory_if_not_exist(dir, license_key_file_path) if persist
136
136
 
137
137
  @contents = load_license_file(license_key_file_path)
138
138
 
139
139
  # Three possible cases:
140
140
  # 1. If contents is nil or an error occurred while loading, load basic license data with the latest structure.
141
141
  # 2. If contents is not nil and valid, but the license server URL in contents is different from the system's,
142
- # update the license server URL in contents and licenses.yaml file.
142
+ # update the license server URL in contents and licenses.yaml file if persist is true.
143
143
  # 3. If contents is valid and no update is needed, return the existing license server URL.
144
144
  # Handle error cases first - when file loading failed or contents is nil
145
145
  if @contents.is_a?(StandardError) || @contents.nil?
146
146
  url = license_server_url_from_system || license_server_url_from_config
147
147
  load_basic_license_data_to_contents(url, [])
148
148
  elsif @contents && license_server_url_from_system && license_server_url_from_system != @contents[:license_server_url]
149
- @contents[:license_server_url] = license_server_url_from_system
149
+ if persist
150
+ @contents[:license_server_url] = license_server_url_from_system
151
+ else
152
+ # When not persisting, just return the new URL without modifying the file or in-memory contents
153
+ @license_server_url = license_server_url_from_system
154
+ return @license_server_url
155
+ end
150
156
  else
151
157
  # Nothing to change in the file
152
158
  @license_server_url = @contents[:license_server_url]
@@ -156,7 +162,7 @@ module ChefLicensing
156
162
  # Ensure the license server URL is returned to the caller in all cases
157
163
  # (even if it's not persisted to the licenses.yaml file on the disk)
158
164
  begin
159
- write_license_file(license_key_file_path)
165
+ write_license_file(license_key_file_path) if persist
160
166
  rescue StandardError => e
161
167
  handle_error(e)
162
168
  ensure
@@ -178,7 +184,8 @@ module ChefLicensing
178
184
  end
179
185
 
180
186
  def self.fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil, opts = {})
181
- new(opts).fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system)
187
+ persist = opts.delete(:persist) { true }
188
+ new(opts).fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system, persist: persist)
182
189
  end
183
190
 
184
191
  private
@@ -55,6 +55,7 @@ module ChefLicensing
55
55
  # Methods for obtaining consent from the user.
56
56
  #
57
57
  def fetch_and_persist
58
+ ChefLicensing::Config.persist_license_data = true
58
59
  if ChefLicensing::Context.local_licensing_service?
59
60
  perform_on_prem_operations
60
61
  else
@@ -62,6 +63,16 @@ module ChefLicensing
62
63
  end
63
64
  end
64
65
 
66
+ # Method for validating license key without writing to disk
67
+ def fetch_and_validate
68
+ ChefLicensing::Config.persist_license_data = false
69
+ if ChefLicensing::Context.local_licensing_service?
70
+ perform_on_prem_operations
71
+ else
72
+ perform_global_operations(false)
73
+ end
74
+ end
75
+
65
76
  def perform_on_prem_operations
66
77
  # While using on-prem licensing service no option to add/generate license is enabled
67
78
 
@@ -94,10 +105,10 @@ module ChefLicensing
94
105
  raise LicenseKeyNotFetchedError.new("Unable to obtain a License Key.")
95
106
  end
96
107
 
97
- def perform_global_operations
108
+ def perform_global_operations(persist = true)
98
109
  new_keys = fetch_license_key_from_arg
99
110
  license_type = validate_and_fetch_license_type(new_keys)
100
- if license_type && !unrestricted_license_added?(new_keys, license_type)
111
+ if license_type && !unrestricted_license_added?(new_keys, license_type, persist)
101
112
  # break the flow after the prompt if there is a restriction in adding license
102
113
  # and return the license keys persisted in the file or @license_keys if any
103
114
  return license_keys
@@ -105,7 +116,7 @@ module ChefLicensing
105
116
 
106
117
  new_keys = fetch_license_key_from_env
107
118
  license_type = validate_and_fetch_license_type(new_keys)
108
- if license_type && !unrestricted_license_added?(new_keys, license_type)
119
+ if license_type && !unrestricted_license_added?(new_keys, license_type, persist)
109
120
  # break the flow after the prompt if there is a restriction in adding license
110
121
  # and return the license keys persisted in the file or @license_keys if any
111
122
  return license_keys
@@ -125,7 +136,11 @@ module ChefLicensing
125
136
  unless new_keys.empty?
126
137
  # If license type is not selected using TUI, assign it using API call to fetch type.
127
138
  prompt_fetcher.license_type ||= get_license_type(new_keys.first)
128
- persist_and_concat(new_keys, prompt_fetcher.license_type)
139
+ if persist
140
+ persist_and_concat(new_keys, prompt_fetcher.license_type)
141
+ else
142
+ validate_and_concat(new_keys, prompt_fetcher.license_type)
143
+ end
129
144
  license ||= ChefLicensing::Context.license
130
145
  # Expired trial licenses and exhausted free licenses will be blocked
131
146
  # Not blocking commercial licenses
@@ -176,6 +191,10 @@ module ChefLicensing
176
191
  new(opts).fetch_and_persist
177
192
  end
178
193
 
194
+ def self.fetch_and_validate(opts = {})
195
+ new(opts).fetch_and_validate
196
+ end
197
+
179
198
  def self.fetch(opts = {})
180
199
  new(opts).fetch
181
200
  end
@@ -263,6 +282,11 @@ module ChefLicensing
263
282
  @license_keys.concat(new_keys)
264
283
  end
265
284
 
285
+ def validate_and_concat(new_keys, license_type)
286
+ # Only update in-memory license keys without persisting to disk
287
+ @license_keys.concat(new_keys)
288
+ end
289
+
266
290
  def fetch_license_key_from_arg
267
291
  new_key = @arg_fetcher.fetch_value("--chef-license-key")
268
292
  validate_license_key_format(new_key)
@@ -304,7 +328,7 @@ module ChefLicensing
304
328
  prompt_fetcher.fetch
305
329
  end
306
330
 
307
- def unrestricted_license_added?(new_keys, license_type)
331
+ def unrestricted_license_added?(new_keys, license_type, persist = true)
308
332
  if license_restricted?(license_type)
309
333
  # Existing license keys of same license type are fetched to compare if old license key or a new one is added.
310
334
  # However, if user is trying to add Free Tier License, and user has active trial license, we fetch the trial license key
@@ -327,7 +351,11 @@ module ChefLicensing
327
351
  # license addition should be restricted but it is not because the license is expired and warning wont be handled by this restriction
328
352
  true
329
353
  else
330
- persist_and_concat(new_keys, license_type)
354
+ if persist
355
+ persist_and_concat(new_keys, license_type)
356
+ else
357
+ validate_and_concat(new_keys, license_type)
358
+ end
331
359
  true
332
360
  end
333
361
  end
@@ -1,3 +1,3 @@
1
1
  module ChefLicensing
2
- VERSION = "1.3.4".freeze
2
+ VERSION = "1.4.0".freeze
3
3
  end
@@ -77,6 +77,21 @@ module ChefLicensing
77
77
  end
78
78
  end
79
79
 
80
+ # @note fetch_only is invoked to fetch license keys without persisting them to a config file
81
+ def fetch_only
82
+ # Return early if make_licensing_optional is enabled
83
+ return true if ChefLicensing::Config.make_licensing_optional
84
+
85
+ ChefLicensing::LicenseKeyFetcher.fetch_and_validate
86
+ rescue ChefLicensing::ClientError => e
87
+ # Checking specific text phrase for entitlement error
88
+ if e.message.match?(/not entitled/)
89
+ raise(ChefLicensing::SoftwareNotEntitled, "Software is not entitled.")
90
+ else
91
+ raise
92
+ end
93
+ end
94
+
80
95
  def list_license_keys_info(opts = {})
81
96
  ChefLicensing::ListLicenseKeys.display(opts)
82
97
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-licensing
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inspec Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-12-16 00:00:00.000000000 Z
11
+ date: 2026-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef-config