How to use fetch method of Inspec.Plugins Package

Best Inspec_ruby code snippet using Inspec.Plugins.fetch

target.rb

Source:target.rb Github

copy

Full Screen

1# encoding: utf-82require 'uri'3require 'inspec/fetcher'4require 'inspec/errors'5# InSpec Target Helper for Chef Compliance6# reuses UrlHelper, but it knows the target server and the access token already7# similar to `inspec exec http://localhost:2134/owners/%base%/compliance/%ssh%/tar --user %token%`8module InspecPlugins9 module Compliance10 class Fetcher < Fetchers::Url11 name 'compliance'12 priority 50013 attr_reader :upstream_sha25614 def initialize(target, opts)15 super(target, opts)16 @upstream_sha256 = ''17 if target.is_a?(Hash) && target.key?(:url)18 @target = target[:url]19 @upstream_sha256 = target[:sha256]20 elsif target.is_a?(String)21 @target = target22 end23 end24 def sha25625 upstream_sha256.empty? ? super : upstream_sha25626 end27 def self.check_compliance_token(uri, config)28 if config['token'].nil? && config['refresh_token'].nil?29 if config['server_type'] == 'automate'30 server = 'automate'31 msg = 'inspec compliance login https://your_automate_server --user USER --ent ENT --dctoken DCTOKEN or --token USERTOKEN'32 elsif config['server_type'] == 'automate2'33 server = 'automate2'34 msg = 'inspec compliance login https://your_automate2_server --user USER --token APITOKEN'35 else36 server = 'compliance'37 msg = "inspec compliance login https://your_compliance_server --user admin --insecure --token 'PASTE TOKEN HERE' "38 end39 raise Inspec::FetcherFailure, <<~EOF40 Cannot fetch #{uri} because your #{server} token has not been41 configured.42 Please login using43 #{msg}44 EOF45 end46 end47 def self.get_target_uri(target)48 if target.is_a?(String) && URI(target).scheme == 'compliance'49 URI(target)50 elsif target.respond_to?(:key?) && target.key?(:compliance)51 URI("compliance://#{target[:compliance]}")52 end53 end54 def self.resolve(target)55 uri = get_target_uri(target)56 return nil if uri.nil?57 config = InspecPlugins::Compliance::Configuration.new58 profile = InspecPlugins::Compliance::API.sanitize_profile_name(uri)59 profile_fetch_url = InspecPlugins::Compliance::API.target_url(config, profile)60 # we have detailed information available in our lockfile, no need to ask the server61 if target.respond_to?(:key?) && target.key?(:sha256)62 profile_checksum = target[:sha256]63 else64 check_compliance_token(uri, config)65 # verifies that the target e.g base/ssh exists66 # Call profiles directly instead of exist? to capture the results67 # so we can access the upstream sha256 from the results.68 _msg, profile_result = InspecPlugins::Compliance::API.profiles(config, profile)69 if profile_result.empty?70 raise Inspec::FetcherFailure, "The compliance profile #{profile} was not found on the configured compliance server"71 else72 # Guarantee sorting by verison and grab the latest.73 # If version was specified, it will be the first and only result.74 # Note we are calling the sha256 as a string, not a symbol since75 # it was returned as json from the Compliance API.76 profile_info = profile_result.sort_by { |x| Gem::Version.new(x['version']) }[0]77 profile_checksum = profile_info.key?('sha256') ? profile_info['sha256'] : ''78 end79 end80 # We need to pass the token to the fetcher81 config['token'] = InspecPlugins::Compliance::API.get_token(config)82 # Needed for automate2 post request83 profile_stub = profile || target[:compliance]84 config['profile'] = InspecPlugins::Compliance::API.profile_split(profile_stub)85 new({ url: profile_fetch_url, sha256: profile_checksum }, config)86 rescue URI::Error => _e87 nil88 end89 # We want to save compliance: in the lockfile rather than url: to90 # make sure we go back through the Compliance API handling.91 def resolved_source92 @resolved_source ||= {93 compliance: compliance_profile_name,94 url: @target,95 sha256: sha256,96 }97 end98 def to_s99 'Chef Compliance Profile Loader'...

Full Screen

Full Screen

input.rb

Source:input.rb Github

copy

Full Screen

...9 attr_reader :priority10 attr_reader :input_name11 attr_reader :logger12 def initialize13 @plugin_conf = Inspec::Config.cached.fetch_plugin_config("inspec-vault")14 @logger = Inspec::Log15 logger.debug format("Inspec-Vault plugin version %s", VERSION)16 @mount_point = fetch_plugin_setting("mount_point", "secret")17 @path_prefix = fetch_plugin_setting("path_prefix", "inspec")18 # We need priority to be numeric; even though env vars or JSON may present it as string - hence the to_i19 @priority = fetch_plugin_setting("priority", 60).to_i20 @vault = Vault::Client.new(21 address: fetch_vault_setting("vault_addr"),22 token: fetch_vault_setting("vault_token")23 )24 end25 # What priority should an input value recieve from us?26 # This plgin does not currently allow setting this on a per-input basis,27 # so they all recieve the same "default" value.28 # Implements https://github.com/inspec/inspec/blob/master/dev-docs/plugins.md#default_priority29 def default_priority30 priority31 end32 # returns Array of input names as strings33 def list_inputs(profile_name)34 vault.with_retries(Vault::HTTPConnectionError) do35 path = logical_path_for_profile(profile_name)36 doc = vault.logical.read(path)37 return [] unless doc38 return doc.data[:data].keys.map(&:to_s)39 end40 end41 # Fetch a value of a single input from Vault42 # TODO we should probably cache these - https://github.com/inspec/inspec-vault/issues/1543 def fetch(profile_name, input_name)44 @input_name = input_name45 path = logical_path_for_profile(profile_name)46 item = input_name47 if absolute_path?48 _empty, *path, item = input_name.split("/")49 path = logical_path path.join("/")50 end51 logger.info format("Reading Vault secret from %s", path)52 vault.with_retries(Vault::HTTPConnectionError) do53 doc = vault.logical.read(path)54 # Keys from vault are always symbolized55 return doc.data[:data][item.to_sym] if doc56 end57 end58 private59 # Assumption for profile based lookups: inputs have been stored on documents named60 # for their profiles, and each input has a key-value pair in the document.61 def logical_path_for_profile(profile_name)62 logical_path(profile_name)63 end64 def logical_path(relative_path)65 # When you actually read a value, on the KV2 backend you must66 # read secret/data/path, not secret/path (as on the CLI)67 # https://www.vaultproject.io/api/secret/kv/kv-v2.html#read-secret-version68 # Is this true for all backends?69 "#{mount_point}/data/#{prefix}#{relative_path}"70 end71 def prefix72 return "#{path_prefix}/" unless absolute_path?73 ""74 end75 def absolute_path?76 input_name.start_with?("/")77 end78 def fetch_plugin_setting(setting_name, default = nil)79 env_var_name = "INSPEC_VAULT_#{setting_name.upcase}"80 ENV[env_var_name] || plugin_conf[setting_name] || default81 end82 def fetch_vault_setting(setting_name)83 ENV[setting_name.upcase] || plugin_conf[setting_name]84 end85 end86end...

Full Screen

Full Screen

reporter.rb

Source:reporter.rb Github

copy

Full Screen

...26 # Detailed OS27 platform_arch = runner.backend.backend.os.arch28 platform_name = runner.backend.backend.os.title29 # Allow template-based settings30 template_config = config.fetch("template_config", {})31 # ... also can use all InSpec resources via "inspec_resource.NAME.PROPERTY"32 output(template.result(binding))33 end34 # Return Constraints.35 #36 # @return [String] Always "~> 0.0"37 def self.run_data_schema_constraints38 "~> 0.0"39 end40 private41 # Read contents of requested template.42 #43 # @return [String] ERB Template44 # @raise [IOError]45 def template_contents46 @template_contents ||= File.read full_path(config["template_file"])47 end48 # Initialize configuration with defaults and Plugin config.49 #50 # @return [Hash] Configuration data after merge51 def config52 @config unless @config.nil?53 # Defaults54 @config = {55 "template_file" => "templates/flex.erb",56 }57 @config.merge! inspec_config.fetch_plugin_config("inspec-reporter-flex")58 @config.merge! config_environment59 logger.debug format("Configuration: %<config>s", config: @config)60 @config61 end62 # Allow (top-level) setting overrides from environment.63 #64 # @return [Hash] Configuration data from environment65 def config_environment66 env_reporter = env.select { |var, _| var.start_with?(ENV_PREFIX) }67 env_reporter.transform_keys { |key| key.delete_prefix(ENV_PREFIX).downcase }68 end69 # Return environment variables.70 #71 # @return [Hash] Mapping of environment variables...

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1Inspec::Plugins.fetch('train')2Inspec::Plugins.fetch('train')3Inspec::Plugins.fetch('train')4Inspec::Plugins.fetch('train')5Inspec::Plugins.fetch('train')6Inspec::Plugins.fetch('train')7Inspec::Plugins.fetch('train')8Inspec::Plugins.fetch('train')9Inspec::Plugins.fetch('train')10Inspec::Plugins.fetch('train')11Inspec::Plugins.fetch('train')12Inspec::Plugins.fetch('train')13Inspec::Plugins.fetch('train')14Inspec::Plugins.fetch('train')15Inspec::Plugins.fetch('train')

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1 class Plugin < Inspec.plugin(2)2Inspec::Plugins.fetch(:'inspec-my_plugin').hello3 class Plugin < Inspec.plugin(2)

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1plugin_path = Inspec::Plugins::plugin_path('inspec-test-fixture')2plugin_version = Inspec::Plugins::plugin_version('inspec-test-fixture')3plugin_name = Inspec::Plugins::plugin_name('inspec-test-fixture')4plugin_type = Inspec::Plugins::plugin_type('inspec-test-fixture')5Inspec::Plugins::plugin_path('inspec-test-fixture')6Inspec::Plugins::plugin_version('inspec-test-fixture')7Inspec::Plugins::plugin_name('inspec-test-fixture')8Inspec::Plugins::plugin_type('inspec-test-fixture')9plugin_path = Inspec::Plugins.plugin_path('inspec-test-fixture')10Inspec::Plugins.plugin_path('inspec-test-fixture')11plugin_version = Inspec::Plugins.plugin_version('inspec-test-fixture')12Inspec::Plugins.plugin_version('inspec-test-fixture')13plugin_name = Inspec::Plugins.plugin_name('inspec-test-fixture')14Inspec::Plugins.plugin_name('inspec-test-fixture')

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1plugins.fetch('inspec-plugin-name').each do |plugin|2plugins.fetch('inspec-plugin-name', '1.0.0').each do |plugin|3plugins.fetch('inspec-plugin-name', '1.0.0', 'inspec-plugin-type').each do |plugin|4plugins.fetch('inspec-plugin-name', '1.0.0', 'inspec-plugin-type', 'inspec-plugin-subtype').each do |plugin|5plugins.fetch('inspec-plugin-name', '1.0.0', 'inspec-plugin-type', 'inspec-plugin-subtype', 'inspec-plugin-subsubtype').each do |plugin|

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Inspec_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful