Module: ChefLicensing

Defined in:
lib/chef-licensing/license.rb,
lib/chef-licensing.rb,
lib/chef-licensing/log.rb,
lib/chef-licensing/config.rb,
lib/chef-licensing/context.rb,
lib/chef-licensing/version.rb,
lib/chef-licensing/api/client.rb,
lib/chef-licensing/api/describe.rb,
lib/chef-licensing/cli_flags/thor.rb,
lib/chef-licensing/pstore_adapter.rb,
lib/chef-licensing/exceptions/error.rb,
lib/chef-licensing/api/list_licenses.rb,
lib/chef-licensing/api/parser/client.rb,
lib/chef-licensing/list_license_keys.rb,
lib/chef-licensing/restful_client/v1.rb,
lib/chef-licensing/string_refinements.rb,
lib/chef-licensing/api/parser/describe.rb,
lib/chef-licensing/license_key_fetcher.rb,
lib/chef-licensing/restful_client/base.rb,
lib/chef-licensing/cli_flags/mixlib_cli.rb,
lib/chef-licensing/license_key_validator.rb,
lib/chef-licensing/tui_engine/tui_engine.rb,
lib/chef-licensing/tui_engine/tui_prompt.rb,
lib/chef-licensing/tui_engine/tui_actions.rb,
lib/chef-licensing/exceptions/client_error.rb,
lib/chef-licensing/licensing_service/local.rb,
lib/chef-licensing/license_key_fetcher/base.rb,
lib/chef-licensing/license_key_fetcher/file.rb,
lib/chef-licensing/exceptions/describe_error.rb,
lib/chef-licensing/tui_engine/tui_exceptions.rb,
lib/chef-licensing/config_fetcher/arg_fetcher.rb,
lib/chef-licensing/config_fetcher/env_fetcher.rb,
lib/chef-licensing/exceptions/invalid_license.rb,
lib/chef-licensing/license_key_fetcher/prompt.rb,
lib/chef-licensing/tui_engine/tui_interaction.rb,
lib/chef-licensing/tui_engine/tui_engine_state.rb,
lib/chef-licensing/exceptions/list_licenses_error.rb,
lib/chef-licensing/api/license_feature_entitlement.rb,
lib/chef-licensing/exceptions/feature_not_entitled.rb,
lib/chef-licensing/exceptions/restful_client_error.rb,
lib/chef-licensing/api/license_software_entitlement.rb,
lib/chef-licensing/exceptions/software_not_entitled.rb,
lib/chef-licensing/exceptions/license_file_corrupted.rb,
lib/chef-licensing/exceptions/unsupported_content_type.rb,
lib/chef-licensing/license_key_fetcher/license_file/v3.rb,
lib/chef-licensing/license_key_fetcher/license_file/v4.rb,
lib/chef-licensing/license_key_fetcher/license_file/base.rb,
lib/chef-licensing/exceptions/invalid_file_format_version.rb,
lib/chef-licensing/exceptions/missing_api_credentials_error.rb,
lib/chef-licensing/exceptions/restful_client_connection_error.rb

Overview

LicenseKeyFetcher allows us to inspect obtain the license Key from the user in a variety of ways.

Defined Under Namespace

Modules: Api, CLIFlags, LicenseFile, RestfulClient, StringRefinements Classes: ArgFetcher, ClientError, Config, Context, DescribeError, EnvFetcher, Error, FeatureNotEntitled, InvalidFileFormatVersion, InvalidLicense, License, LicenseFileCorrupted, LicenseKeyFetcher, LicenseKeyValidator, LicensingService, ListLicenseKeys, ListLicensesError, Log, MissingAPICredentialsError, PStoreAdapter, RestfulClientConnectionError, RestfulClientError, SoftwareNotEntitled, TUIEngine, UnsupportedContentType

Constant Summary collapse

VERSION =
"1.3.0".freeze

Class Method Summary collapse

Class Method Details

.add_licenseObject



87
88
89
# File 'lib/chef-licensing.rb', line 87

def add_license
  ChefLicensing::LicenseKeyFetcher.add_license
end

.check_feature_entitlement!(feature_name: nil, feature_id: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chef-licensing.rb', line 25

def check_feature_entitlement!(feature_name: nil, feature_id: nil)
  # If licensing is optional, bypass entitlement checks
  return true if ChefLicensing::Config.make_licensing_optional

  # Checking for feature presence in license feature entitlements
  license = client(license_keys: license_keys)
  feature_entitlements = license.feature_entitlements.select { |feature| feature.id == feature_id || feature.name == feature_name }
  if feature_entitlements.empty?
    raise(ChefLicensing::FeatureNotEntitled)
  else
    true
  end
end

.check_software_entitlement!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef-licensing.rb', line 39

def check_software_entitlement!
  # If API call is not breaking that means license is entitled.
  unless ChefLicensing::Config.make_licensing_optional
    if license_keys.empty?
      raise ChefLicensing::LicenseKeyFetcher::LicenseKeyNotFetchedError,
        "Unable to obtain a License Key."
    end

    client(license_keys: license_keys)
  end
  true
rescue ChefLicensing::ClientError => e
  # Checking specific text phrase for entitlement error
  if e.message.match?(/not entitled/)
    raise(ChefLicensing::SoftwareNotEntitled)
  else
    raise
  end
end

.client(opts = {}) ⇒ Object



83
84
85
# File 'lib/chef-licensing.rb', line 83

def client(opts = {})
  ChefLicensing::Api::Client.info(opts)
end

.configure {|ChefLicensing::Config| ... } ⇒ Object

Examples:

ChefLicensing.configure do |config|
  config.licensing_server_url  = 'LICENSE_SERVER'
  config.logger = Logger.new($stdout)
end

Yields:



21
22
23
# File 'lib/chef-licensing.rb', line 21

def configure(&block)
  yield(ChefLicensing::Config)
end

.fetch_and_persistObject

Note:

fetch_and_persist is invoked by chef-products to fetch and persist the license keys



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chef-licensing.rb', line 65

def fetch_and_persist
  # Return early if make_licensing_optional is enabled
  return true if ChefLicensing::Config.make_licensing_optional

  ChefLicensing::LicenseKeyFetcher.fetch_and_persist
rescue ChefLicensing::ClientError => e
  # Checking specific text phrase for entitlement error
  if e.message.match?(/not entitled/)
    raise(ChefLicensing::SoftwareNotEntitled, "Software is not entitled.")
  else
    raise
  end
end

.license_contextObject



91
92
93
# File 'lib/chef-licensing.rb', line 91

def license_context
  ChefLicensing::Context.license
end

.license_keysObject

Note:

no in-memory caching of the licenses so that it fetches updated licenses always



60
61
62
# File 'lib/chef-licensing.rb', line 60

def license_keys
  ChefLicensing::LicenseKeyFetcher.fetch
end

.list_license_keys_info(opts = {}) ⇒ Object



79
80
81
# File 'lib/chef-licensing.rb', line 79

def list_license_keys_info(opts = {})
  ChefLicensing::ListLicenseKeys.display(opts)
end