How to use inspec method of Plugins Package

Best Inspec_ruby code snippet using Plugins.inspec

generate.rb

Source:generate.rb Github

copy

Full Screen

1# parses Terraform d.tfstate files2require "inspec/objects/control"3require "inspec/objects/ruby_helper"4require "inspec/objects/describe"5require "inspec-iggy/file_helper"6require "inspec-iggy/inspec_helper"7module InspecPlugins::Iggy::Terraform8 class Generate9 # parse through the JSON and generate InSpec controls10 def self.parse_generate(tf_file, resource_path, platform)11 # parse the tfstate file to get the Terraform resources12 tfstate = InspecPlugins::Iggy::FileHelper.parse_json(tf_file)13 absolutename = File.absolute_path(tf_file)14 # take those Terraform resources and map to InSpec resources by name and keep all attributes15 # resources -> [{name1 -> {unfiltered_attributes}, name2 -> {unfiltered_attributes}]16 parsed_resources = parse_resources(tfstate, resource_path, platform)17 # InSpec controls generated from matched_resources and attributes18 generated_controls = parse_controls(parsed_resources, absolutename, platform)19 Inspec::Log.debug "Iggy::Terraform::Generate.parse_generate generated_controls = #{generated_controls}"20 generated_controls21 end22 # returns the list of all InSpec resources found in the tfstate file23 def self.parse_resources(tfstate, resource_path, _platform)24 # iterate over the resources25 resources = {}26 tf_resources = tfstate["resources"]27 tf_resources.each do |tf_res|28 resource_type = tf_res["type"]29 next if resource_type.eql?("random_id") # this is a Terraform resource, not a provider resource30 # load resource pack resources31 InspecPlugins::Iggy::InspecHelper.load_resource_pack(resource_path) if resource_path32 # add translation layer33 if InspecPlugins::Iggy::InspecHelper::TRANSLATED_RESOURCES.key?(resource_type)34 Inspec::Log.debug "Iggy::Terraform::Generate.parse_resources resource_type = #{resource_type} #{InspecPlugins::Iggy::InspecHelper::TRANSLATED_RESOURCES[resource_type]} TRANSLATED"35 resource_type = InspecPlugins::Iggy::InspecHelper::TRANSLATED_RESOURCES[resource_type]36 end37 resources[resource_type] = {} if resources[resource_type].nil?38 # does this match an InSpec resource?39 if InspecPlugins::Iggy::InspecHelper.available_resources.include?(resource_type)40 Inspec::Log.debug "Iggy::Terraform::Generate.parse_resources resource_type = #{resource_type} MATCHED"41 tf_res["instances"].each do |instance|42 resource_id = instance["attributes"]["id"]43 resource_attributes = instance["attributes"]44 resources[resource_type][resource_id] = resource_attributes45 end46 else47 Inspec::Log.debug "Iggy::Terraform.Generate.parse_generate resource_type = #{resource_type} SKIPPED"48 end49 end50 resources51 end52 # take the resources and map to describes53 def self.parse_controls(resources, absolutename, platform) # rubocop:disable Metrics/AbcSize54 controls = []55 # iterate over the resources types and their ids56 resources.keys.each do |resource_type|57 resources[resource_type].keys.each do |resource_id|58 # insert new control based off the resource's ID59 ctrl = Inspec::Control.new60 ctrl.id = "#{resource_type}::#{resource_id}"61 ctrl.title = "InSpec-Iggy #{resource_type}::#{resource_id}"62 ctrl.descriptions[:default] = "#{resource_type}::#{resource_id} from the source file #{absolutename}\nGenerated by InSpec-Iggy v#{InspecPlugins::Iggy::VERSION}"63 ctrl.impact = "1.0"64 describe = Inspec::Describe.new65 case platform # this may need to get refactored away once Azure is tested66 when "aws"67 qualifier = [resource_type, {}]68 if InspecPlugins::Iggy::InspecHelper.available_resource_qualifiers(platform).key?(resource_type) # there are additional qualifiers69 first = true70 InspecPlugins::Iggy::InspecHelper.available_resource_qualifiers(platform)[resource_type].each do |parameter|71 Inspec::Log.debug "Iggy::Terraform::Generate.parse_controls #{resource_type} qualifier found = #{parameter} MATCHED"72 if first # this is the id for the resource73 value = resources[resource_type][resource_id]["id"] # pull value out of the tf attributes74 first = false75 else76 value = resources[resource_type][resource_id][parameter.to_s] # pull value out of the tf attributes77 end78 qualifier[1][parameter] = value79 end80 end81 describe.qualifier.push(qualifier)82 when "azure" # rubocop:disable Lint/EmptyWhen83 # this is a hack for azure, we need a better longterm solution84 # if resource.start_with?('azure_')85 # name = resource_id.split('/').last86 # else87 # name = resource_id88 # end89 # if resource_type.start_with?('azure_')90 # if resource_type.eql?('azure_resource_group')91 # describe.qualifier.push([resource_type, name: name])92 # else93 # resource_group = resource_id.split('resourceGroups/').last.split('/').first94 # describe.qualifier.push([resource_type, name: name, group_name: resource_group])95 # end96 when "gcp"97 qualifier = [resource_type, {}]98 if InspecPlugins::Iggy::InspecHelper.available_resource_qualifiers(platform).key?(resource_type)99 InspecPlugins::Iggy::InspecHelper.available_resource_qualifiers(platform)[resource_type].each do |parameter|100 Inspec::Log.debug "Iggy::Terraform::Generate.parse_controls #{resource_type} qualifier found = #{parameter} MATCHED"101 value = resources[resource_type][resource_id][parameter.to_s] # pull value out of the tf attributes102 qualifier[1][parameter] = value103 end104 end105 describe.qualifier.push(qualifier)106 end107 # ensure the resource exists unless Azure, which currently doesn't support it as of InSpec 2.2108 describe.add_test(nil, "exist", nil) unless resource_type.start_with?("azure_")109 # if there's a match, see if there are matching InSpec properties110 inspec_properties = InspecPlugins::Iggy::InspecHelper.resource_properties(resource_type, platform)111 # push stuff back into inspec_properties?112 resources[resource_type][resource_id].keys.each do |attr|113 if inspec_properties.member?(attr)114 Inspec::Log.debug "Iggy::Terraform::Generate.parse_controls #{resource_type} inspec_property = #{attr} MATCHED"115 value = resources[resource_type][resource_id][attr]116 if value117 # check to see if there is a translate for this attr118 property = InspecPlugins::Iggy::InspecHelper.translated_resource_property(platform, resource_type, attr)119 describe.add_test(property, "cmp", value)120 else121 Inspec::Log.debug "Iggy::Terraform::Generate.parse_controls #{resource_type} inspec_property = #{attr} SKIPPED FOR NIL"122 end123 else124 Inspec::Log.debug "Iggy::Terraform::Generate.parse_controls #{resource_type} inspec_property = #{attr} SKIPPED"125 end126 end127 ctrl.add_test(describe)128 controls.push(ctrl)129 end130 end131 Inspec::Log.debug "Iggy::Terraform::Generate.parse_generate controls = #{controls}"132 controls133 end134 end135end...

Full Screen

Full Screen

reporter.rb

Source:reporter.rb Github

copy

Full Screen

...5 class Reporter < Inspec::Reporters::Json6 include InspecPlugins::FlexReporter::ErbHelpers7 include InspecPlugins::FlexReporter::FileResolver8 ENV_PREFIX = "INSPEC_REPORTER_FLEX_".freeze9 attr_writer :config, :env, :inspec_config, :logger, :template_contents10 # Render report11 def render12 template = ERB.new(template_contents)13 # Use JSON Reporter base's `report` to have pre-processed data14 mushy_report = Hashie::Mash.new(report)15 # Eeease of use here16 platform = mushy_report.platform17 profiles = mushy_report.profiles18 statistics = mushy_report.statistics19 version = mushy_report.version20 # Some pass/fail information21 test_results = profiles.map(&:controls).flatten.map(&:results).flatten.map(&:status)22 passed_tests = test_results.select { |text| text == "passed" }.count23 failed_tests = test_results.count - passed_tests24 percent_pass = 100.0 * passed_tests / test_results.count25 percent_fail = 100.0 - percent_pass26 # 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 variables72 def env73 @env ||= ENV74 end75 # Return InSpec Config object.76 #77 # @return [Inspec::Config] The InSpec config object78 def inspec_config79 @inspec_config ||= Inspec::Config.cached80 end81 # Return InSpec logger.82 #83 # @return [Inspec:Log] The Logger object84 def logger85 @logger ||= Inspec::Log86 end87 # Return InSpec Runner for further inspection.88 #89 # @return [Inspec::Runner] Runner instance90 def runner91 require "binding_of_caller"92 binding.callers.find { |b| b.frame_description == "run_tests" }.receiver93 end94 end95end...

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1Inspec::Plugins::CLI.subcommand('example', 'Example plugin', Inspec::Plugins::Example::CLI)2 class CLI < Inspec.plugin(2, :cli_command)3Inspec::Plugins::CLI.subcommand('example', 'Example plugin', Inspec::Plugins

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1plugin_registry.add(plugin)2plugin_config.add_plugin_registry(plugin_registry)3plugin_loader = Inspec::Plugins::Loader.new(plugin_config)4Inspec::Plugins::Loader.new(Inspec::Plugins::Config.new).load

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1 class Plugin < Inspec.plugin(2)2 class Plugin < Inspec.plugin(2)3 class Plugin < Inspec.plugin(2)4 class Plugin < Inspec.plugin(2)5 class Plugin < Inspec.plugin(2)

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1default_plugin_path = File.join(File.dirname(__FILE__), "plugins")2user_plugin_path = File.join(File.dirname(__FILE__), "my_plugins")3user_plugin_path_2 = File.join(File.dirname(__FILE__), "my_plugins_2")4user_plugin_path_3 = File.join(File.dirname(__FILE__), "my_plugins_3")5user_plugin_path_4 = File.join(File.dirname(__FILE__), "my_plugins_4")6user_plugin_path_5 = File.join(File.dirname(__FILE__), "my_plugins_5")7user_plugin_path_6 = File.join(File.dirname(__FILE__), "my_plugins_6")8user_plugin_path_7 = File.join(File.dirname(__FILE__), "my_plugins_7")9user_plugin_path_8 = File.join(File.dirname(__FILE__), "my_plugins_8")10user_plugin_path_9 = File.join(File.dirname(__FILE__), "my_plugins_9")11user_plugin_path_10 = File.join(File.dirname(__FILE__), "my_plugins_10")12user_plugin_path_11 = File.join(File.dirname(__FILE__), "my_plugins_11")13user_plugin_path_12 = File.join(File.dirname(__FILE__), "my_plugins_12")14user_plugin_path_13 = File.join(File.dirname(__FILE__), "my_plugins_13")15user_plugin_path_14 = File.join(File.dirname(__FILE__), "my_plugins_14")

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1 class Plugin < Inspec.plugin(2)2 def self.setup_plugin(opts)3 def self.register_resource(resource_class)4 def self.register_reporter(reporter_class)5class InspecPlugins::ExamplePlugin::ExampleResource < Inspec.resource(1)

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1plugin_registry.add(plugin)2plugin_config.add_plugin_registry(plugin_registry)3plugin_loader = Inspec::Plugins::Loader.new(plugin_config)4Inspec::Plugins::Loader.new(Inspec::Plugins::Config.new).load

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1 class Plugin < Inspec.plugin(2)2 class Plugin < Inspec.plugin(2)3 class Plugin < Inspec.plugin(2)4 class Plugin < Inspec.plugin(2)5 class Plugin < Inspec.plugin(2)

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1default_plugin_path = File.join(File.dirname(__FILE__), "plugins")2user_plugin_path = File.join(File.dirname(__FILE__), "my_plugins")3user_plugin_path_2 = File.join(File.dirname(__FILE__), "my_plugins_2")4user_plugin_path_3 = File.join(File.dirname(__FILE__), "my_plugins_3")5user_plugin_path_4 = File.join(File.dirname(__FILE__), "my_plugins_4")6user_plugin_path_5 = File.join(File.dirname(__FILE__), "my_plugins_5")7user_plugin_path_6 = File.join(File.dirname(__FILE__), "my_plugins_6")8user_plugin_path_7 = File.join(File.dirname(__FILE__), "my_plugins_7")9user_plugin_path_8 = File.join(File.dirname(__FILE__), "my_plugins_8")10user_plugin_path_9 = File.join(File.dirname(__FILE__), "my_plugins_9")11user_plugin_path_10 = File.join(File.dirname(__FILE__), "my_plugins_10")12user_plugin_path_11 = File.join(File.dirname(__FILE__), "my_plugins_11")13user_plugin_path_12 = File.join(File.dirname(__FILE__), "my_plugins_12")14user_plugin_path_13 = File.join(File.dirname(__FILE__), "my_plugins_13")15user_plugin_path_14 = File.join(File.dirname(__FILE__), "my_plugins_14")

Full Screen

Full Screen

inspec

Using AI Code Generation

copy

Full Screen

1 class Plugin < Inspec.plugin(2)2 def self.setup_plugin(opts)3 def self.register_resource(resource_class)4 def self.register_reporter(reporter_class)5class InspecPlugins::ExamplePlugin::ExampleResource < Inspec.resource(1)

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