Licensing - Integrates Software Entitlement (#13)

* CFINSPEC-24:Integrate License Software Entitlment.

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* CFINSPEC-506 license execution check

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Software entitlement call changes and handling of error

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>

* Error handling from chef licensing error to standard error

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>

* Updated Gemfile to pick the chef licensing changes from git url

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Trying with https url

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Revert "Trying with https url"

This reverts commit 33f1f4c0ecacf9ba2826e25e55b41219903ea736.

* Adds the license not entitled exit code

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Updates the require statement

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Fix typo

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Set the licensing configure for entitlement id failure

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* Use ChefLicensing::Error instead of standard error

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

* REFACTOR Move out licensing cong=fig to its own file and make unconditional

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* FIX TESTS - Add reference to licensing config for places where we call runner explicitly.

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Update inputs_test to use license configuration from utils

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>

---------

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Co-authored-by: Nikita Mathur <nikita.mathur@chef.io>
Co-authored-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Vasundhara Jagdale 2023-03-27 19:56:35 +05:30 committed by GitHub
parent 031ec21cce
commit 699d73993c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 11 deletions

View file

@ -4,6 +4,7 @@ libdir = File.dirname(__FILE__)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
require "inspec/version"
require "inspec/utils/licensing_config"
require "inspec/exceptions"
require "inspec/utils/deprecation"
require "inspec/profile"
@ -30,4 +31,4 @@ require "inspec/source_reader"
require "inspec/resource"
require "inspec/dependency_loader"
require "inspec/dependency_installer"
require "inspec/dependency_installer"

View file

@ -1,9 +1,11 @@
require "thor" # rubocop:disable Chef/Ruby/UnlessDefinedRequire
require "chef-licensing"
require "inspec/log"
require "inspec/ui"
require "inspec/config"
require "inspec/dist"
require "inspec/utils/deprecation/global_method"
require "inspec/utils/licensing_config"
# Allow end of options during array type parsing
# https://github.com/erikhuda/thor/issues/631
@ -40,10 +42,8 @@ module Inspec
def self.fetch_and_persist_license
allowed_commands = ["-h", "--help", "help", "-v", "--version", "version"]
require "chef-licensing"
begin
if (allowed_commands & ARGV.map(&:downcase)).empty? && !ARGV.empty?
configure_chef_licensing
license_keys = ChefLicensing.license_keys
# Only if EULA acceptance or license key args are present. And licenses are successfully persisted, do clean exit.
@ -55,18 +55,11 @@ module Inspec
Inspec::Log.error "#{Inspec::Dist::PRODUCT_NAME} cannot execute without valid licenses."
Inspec::UI.new.exit(:usage_error)
rescue ChefLicensing::Error => e
Inspec::Log.error "Something went wrong: #{e}"
Inspec::Log.error "Something went wrong: #{e.message}"
Inspec::UI.new.exit(:usage_error)
end
end
def self.configure_chef_licensing
ChefLicensing.configure do |config|
config.chef_product_name = "Inspec"
config.chef_entitlement_id = "3ff52c37-e41f-4f6c-ad4d-365192205968"
end
end
# EULA acceptance
def self.check_license!
allowed_commands = ["-h", "--help", "help", "-v", "--version", "version"]

View file

@ -11,6 +11,7 @@ require "inspec/dependencies/cache"
require "inspec/dist"
require "inspec/reporters"
require "inspec/runner_rspec"
require "chef-licensing"
# spec requirements
module Inspec
@ -171,9 +172,16 @@ module Inspec
end
def run(with = nil)
ChefLicensing.check_software_entitlement! if Inspec::Dist::EXEC_NAME == "inspec"
Inspec::Log.debug "Starting run with targets: #{@target_profiles.map(&:to_s)}"
load
run_tests(with)
rescue ChefLicensing::SoftwareNotEntitled
Inspec::Log.error "License is not entitled to use InSpec."
Inspec::UI.new.exit(:license_not_entitled)
rescue ChefLicensing::Error => e
Inspec::Log.error "Something went wrong: #{e.message}"
Inspec::UI.new.exit(:usage_error)
end
def render_output(run_data)

View file

@ -1,3 +1,6 @@
require "chef-licensing"
require "inspec/dist"
autoload :Pry, "pry"
module Inspec
@ -10,6 +13,7 @@ module Inspec
end
def start
ChefLicensing.check_software_entitlement! if Inspec::Dist::EXEC_NAME == "inspec"
# This will hold a single evaluation binding context as opened within
# the instance_eval context of the anonymous class that the profile
# context creates to evaluate each individual test file. We want to
@ -18,6 +22,12 @@ module Inspec
@ctx_binding = @runner.eval_with_virtual_profile("binding")
configure_pry
@ctx_binding.pry
rescue ChefLicensing::SoftwareNotEntitled
Inspec::Log.error "License is not entitled to use InSpec."
Inspec::UI.new.exit(:license_not_entitled)
rescue ChefLicensing::Error => e
Inspec::Log.error "Something went wrong: #{e.message}"
Inspec::UI.new.exit(:usage_error)
end
def configure_pry # rubocop:disable Metrics/AbcSize

View file

@ -33,6 +33,7 @@ module Inspec
EXIT_GEM_DEPENDENCY_LOAD_ERROR = 4
EXIT_BAD_SIGNATURE = 5
EXIT_LICENSE_NOT_ACCEPTED = 172
EXIT_LICENSE_NOT_ENTITLED = 173
EXIT_FAILED_TESTS = 100
EXIT_SKIPPED_TESTS = 101
EXIT_TERMINATED_BY_CTL_C = 130

View file

@ -0,0 +1,5 @@
require "chef-licensing"
ChefLicensing.configure do |config|
config.chef_product_name = "Inspec"
config.chef_entitlement_id = "3ff52c37-e41f-4f6c-ad4d-365192205968"
end

View file

@ -105,6 +105,7 @@ describe "inputs" do
# require inspec
require "inspec"
require "inspec/runner"
require "inspec/utils/licensing_config"
# inject pretty-printed runner opts
runner_args = #{options.inspect}

View file

@ -1,12 +1,14 @@
require "functional/helper"
require "inspec/runner"
require "inspec/resources/file"
require "inspec/utils/licensing_config"
describe "inspec report tests" do
include FunctionalHelper
describe "report" do
it "loads a json report" do
WebMock.allow_net_connect!
o = { "reporter" => ["json"], "report" => true }
runner = ::Inspec::Runner.new(o)
runner.add_target(example_profile)

View file

@ -4,6 +4,7 @@ require "helper"
require "inspec/secrets"
require "inspec/runner"
require "inspec/fetcher/mock"
require "inspec/utils/licensing_config"
describe Inspec::Runner do
let(:runner) { Inspec::Runner.new({ command_runner: :generic, reporter: [] }) }
@ -73,6 +74,7 @@ describe Inspec::Runner do
describe "testing runner.run exit codes" do
it "returns proper exit code when no profile is added" do
WebMock.allow_net_connect!
_(runner.run).must_equal 0
end
end