How to use fetch method of Inspec Package

Best Inspec_ruby code snippet using Inspec.fetch

windows_updates.rb

Source:windows_updates.rb Github

copy

Full Screen

...47 @update_mgmt = select_update_mgmt48 end49 # returns all available updates50 def all51 updates = fetch_updates52 updates.map { |update| WindowsUpdate.new(update) }53 end54 # returns all important updates55 def important56 updates = fetch_updates57 updates58 .select do |update|59 @update_mgmt.important?(update)60 end.map do |update| # rubocop:disable Style/MultilineBlockChain61 WindowsUpdate.new(update)62 end63 end64 # returns all optional updates65 def optional66 updates = fetch_updates67 updates.select do |update|68 @update_mgmt.optional?(update)69 end.map do |update| # rubocop:disable Style/MultilineBlockChain70 WindowsUpdate.new(update)71 end72 end73 def reboot_required?74 return @reboot_required if defined?(@reboot_required)75 @reboot_required = inspec.registry_key('HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update').has_property?('RebootRequired')76 end77 def to_s78 'Windows Update Services'79 end80 # private81 # detection for nano server82 # @see https://msdn.microsoft.com/en-us/library/hh846315(v=vs.85).aspx83 def windows_nano?84 return false unless inspec.os[:release].to_i >= 1085 inspec.powershell('Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels" | Select -ExpandProperty "NanoServer" ').stdout.chomp == '1'86 end87 private88 def select_update_mgmt89 if windows_nano?90 WindowsNanoUpdateFetcher.new(inspec)91 else92 Windows2012UpdateFetcher.new(inspec)93 end94 end95 def fetch_updates96 return [] if @update_mgmt.nil?97 @update_mgmt.fetch_updates98 end99 def hotfixes100 return [] if @update_mgmt.nil?101 @update_mgmt.hotfixes102 end103end104class UpdateFetcher105 def initialize(inspec)106 @inspec = inspec107 end108 def hotfixes109 []110 end111 def fetch_updates112 []113 end114end115class Windows2012UpdateFetcher < UpdateFetcher116 def hotfixes117 return @cache_hotfix_installed if defined?(@cache_hotfix_installed)118 hotfix_cmd = 'Get-HotFix | Select-Object -Property Status, Description, HotFixId, Caption, InstallDate, InstalledBy | ConvertTo-Json'119 cmd = @inspec.command(hotfix_cmd)120 begin121 @cache_hotfix_installed = JSON.parse(cmd.stdout)122 rescue JSON::ParserError => _e123 []124 end125 end126 def fetch_updates127 return @cache_available if defined?(@cache_available)128 script = <<-SCR129 $updateSession = new-object -com "Microsoft.Update.Session"130 $searcher=$updateSession.CreateupdateSearcher().Search(("IsInstalled=0 and Type='Software'"))131 $updates = $searcher.Updates | ForEach-Object {132 $update = $_133 $value = New-Object psobject -Property @{134 "UpdateID" = $update.Identity.UpdateID;135 "RevisionNumber" = $update.Identity.RevisionNumber;136 "CategoryIDs" = $update.Categories | % { $_.CategoryID }137 "Title" = $update.Title138 "SecurityBulletinIDs" = $update.SecurityBulletinIDs139 "RebootRequired" = $update.RebootRequired140 "KBArticleIDs" = $update.KBArticleIDs141 "CveIDs" = $update.CveIDs142 "MsrcSeverity" = $update.MsrcSeverity143 }144 $value145 }146 $updates | ConvertTo-Json147 SCR148 cmd = @inspec.powershell(script)149 begin150 result = JSON.parse(cmd.stdout)151 # PowerShell's `ConvertTo-Json` returns an Array of Hashes only if there152 # is more than one object passed into it, otherwise it returns a single153 # Hash. The below ensures that an Array is always returned regardless.154 @cache_available = result.is_a?(Array) ? result : [result]155 rescue JSON::ParserError => _e156 # Return `{}` if parsing fails to indicate that we couldn't retrieve data157 @cache_available = {}158 end159 end160 def important?(update)161 security_category?(update['CategoryIDs'])162 end163 def optional?(update)164 !important?(update)165 end166 # @see: https://msdn.microsoft.com/en-us/library/ff357803(v=vs.85).aspx167 # e6cf1350-c01b-414d-a61f-263d14d133b4 -> Critical Updates168 # 0fa1201d-4330-4fa8-8ae9-b877473b6441 -> Security Updates169 # 28bc880e-0592-4cbf-8f95-c79b17911d5f -> Update Rollups170 # does not include recommended updates yet171 def security_category?(uuids)172 return if uuids.nil?173 uuids.include?('0fa1201d-4330-4fa8-8ae9-b877473b6441') ||174 uuids.include?('28bc880e-0592-4cbf-8f95-c79b17911d5f') ||175 uuids.include?('e6cf1350-c01b-414d-a61f-263d14d133b4')176 end177end178class WindowsNanoUpdateFetcher < UpdateFetcher179 def fetch_updates180 return @cache_available if defined?(@cache_available)181 script = <<-SCR182 $sess = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession183 $scanResults = Invoke-CimMethod -InputObject $sess -MethodName ScanForUpdates -Arguments @{SearchCriteria="IsInstalled=0";OnlineScan=$true}184 $updates = $scanResults.Updates | ForEach-Object {185 $update = $_186 $value = New-Object psobject -Property @{187 "UpdateID" = $update.UpdateID;188 "RevisionNumber" = $update.RevisionNumber;189 "Title" = $update.Title190 "MsrcSeverity" = $update.MsrcSeverity191 }192 $value193 }...

Full Screen

Full Screen

archer.rb

Source:archer.rb Github

copy

Full Screen

...40 def initialize(opts = {})41 unless inspec.command('curl').exist?42 raise Inspec::Exceptions::ResourceSkipped, "Package `curl` and `powershell` not avaiable on the host"43 end44 @url = opts.fetch(:url, 'https://localhost/')45 @creds = {}46 @creds['InstanceName'] = opts.fetch(:instancename, nil)47 @creds['Username'] = opts.fetch(:username, nil)48 @creds['UserDomain'] = opts.fetch(:userdomain, nil)49 @creds['Password'] = opts.fetch(:password, nil)50 @ssl_verify = opts.fetch(:ssl_verify, true)51 login if session_token.nil?52 end53 def to_s54 "Archer Instance #{@creds['InstanceName']}"55 end56 def default_administrative_user57 content = securityparameter[0]58 verify_json_payload!(content)59 Hashie::Mash.new(content['RequestedObject'])60 end61 def general_user_parameter62 content = securityparameter[1]63 verify_json_payload!(content)64 Hashie::Mash.new(content['RequestedObject'])65 end66 def archer_services_parameter67 content = securityparameter[2]68 verify_json_payload!(content)69 Hashie::Mash.new(content['RequestedObject'])70 end71 def curl_command(request: nil, headers: nil, body: nil, path: nil)72 cmd_string = ['curl']73 cmd_string << '-k' unless @ssl_verify74 cmd_string << "-X #{request}" unless request.nil?75 cmd_string << headers.map { |x, y| "-H \"#{x}\":\"#{y}\"" }.join(' ') unless headers.nil?76 cmd_string << "-H 'Content-Type: application/json'"77 cmd_string << "-d '#{body.to_json}'" unless body.nil?78 cmd_string << URI.join(@url, path)79 cmd_string.join(' ')80 end81 def invoke_restmethod_command(request: nil, headers: nil, body: nil, path: nil)82 cmd_string = ['Invoke-RestMethod']83 cmd_string << "-Method #{request}" unless request.nil?84 cmd_string << "-Headers @{#{headers.map { |x, y| " '#{x}' = '#{y}'" }.join(';')} }" unless headers.nil?85 cmd_string << "-ContentType 'application/json'"86 cmd_string << "-Body '#{body.to_json}'" unless body.nil?87 cmd_string << URI.join(@url, path)88 cmd_string << '| ConvertTo-Json'89 return "#{SKIP_CERT_CHECK} \n #{cmd_string.join(' ')}" unless @ssl_verify90 cmd_string.join(' ')91 end92 def parse_response(response)93 JSON.parse(response)94 rescue JSON::ParserError => e95 raise Inspec::Exceptions::ResourceSkipped, "Error Parsing JSON response from Archer: #{ e.message}."96 end97 def session_token=(token)98 ENV['INSPEC_ARCHER_SESSION_TOKEN'] = token99 end100 def session_token101 ENV['INSPEC_ARCHER_SESSION_TOKEN']102 end103 def login104 if inspec.os.windows?105 cmd = invoke_restmethod_command(request: 'POST',106 headers: nil,107 body: @creds,108 path: LOGIN_PATH)109 response = inspec.powershell(cmd)110 verify_pwsh_success!(response)111 else112 cmd = curl_command(request: 'POST',113 headers: nil,114 body: @creds,115 path: LOGIN_PATH)116 response = inspec.command(cmd)117 verify_curl_success!(response)118 end119 content = parse_response(response.stdout)120 verify_json_payload!(content)121 ENV['INSPEC_ARCHER_SESSION_TOKEN'] = content['RequestedObject']['SessionToken']122 end123 def securityparameter124 headers = { 'Authorization' => "Archer session-id=#{session_token}" }125 if inspec.os.windows?126 cmd = invoke_restmethod_command(request: 'GET',127 headers: headers,128 body: nil,129 path: SECURITY_PARAMETERS_PATH)130 response = inspec.powershell(cmd)131 verify_pwsh_success!(response)132 parse_response(response.stdout)['value']133 else134 cmd = curl_command(request: 'GET',135 headers: headers,136 body: nil,137 path: SECURITY_PARAMETERS_PATH)138 response = inspec.command(cmd)139 verify_curl_success!(response)140 parse_response(response.stdout)141 end142 end143 def verify_curl_success!(cmd)144 raise Inspec::Exceptions::ResourceSkipped, "Error fetching Archer data from curl #{@url}\nError: #{cmd.stderr.match(/curl: \(\d.\).*$/)}" unless cmd.exit_status.zero?145 end146 def verify_pwsh_success!(cmd)147 if cmd.stderr =~ /No HTTP resource was found that matches the request URI|DNS_FAIL/148 raise Inspec::Exceptions::ResourceSkipped, "Connection refused - please check the URL #{@url} for accuracy"149 end150 if cmd.stderr =~ /Could not establish trust relationship for the SSL\/TLS secure channel/151 raise Inspec::Exceptions::ResourceSkipped, 'Connection refused - peer certificate issuer is not recognized; try setting ssl_verify to false'152 end153 raise Inspec::Exceptions::ResourceSkipped, "Error fetching Archer data from Invoke-RestMethod #{@url}: #{cmd.stderr}" unless cmd.exit_status.zero?154 end155 def verify_json_payload!(content)156 raise Inspec::Exceptions::ResourceSkipped, "Message: #{content['ValidationMessages'][0]['MessageKey']}" unless content['IsSuccessful']#{url}\nError: #{cmd.stderr.match(/curl: \(\d.\).*$/)}""157 end158end...

Full Screen

Full Screen

terraform.rb

Source:terraform.rb Github

copy

Full Screen

...78 # @raise [::Kitchen::ActionFailed] if the result of the action is a failure.79 # @return [void]80 def call(state)81 state82 .fetch :kitchen_terraform_output do83 raise(84 ::Kitchen::Terraform::Error,85 "The Test Kitchen state does not include :kitchen_terraform_output; this implies that the " \86 "kitchen-terraform provisioner has not successfully converged"87 )88 end89 .tap do |output|90 ::Kitchen::Verifier::Terraform::EnumerateGroupsAndHostnames91 .call(92 groups: config_groups,93 output: ::Kitchen::Util.stringified_hash(output)94 ) do |group:, hostname:|95 state96 .store(97 :kitchen_terraform_group,98 group99 )100 state101 .store(102 :kitchen_terraform_hostname,103 hostname104 )105 info "Verifying host '#{hostname}' of group '#{group.fetch :name}'"106 super state107 end108 end109 rescue ::Kitchen::Terraform::Error => error110 raise(111 ::Kitchen::ActionFailed,112 error.message113 )114 end115 private116 # Modifies the Inspec Runner options generated by the kitchen-inspec verifier to support the verification of each117 # group's hosts.118 #119 # @api private120 # @return [::Hash] Inspec Runner options.121 # @see https://github.com/chef/inspec/blob/master/lib/inspec/runner.rb ::Inspec::Runner122 def runner_options(transport, state = {}, platform = nil, suite = nil)123 super(transport, state, platform, suite)124 .tap do |options|125 ::Kitchen::Verifier::Terraform::ConfigureInspecRunnerBackend126 .call(127 hostname: state.fetch(:kitchen_terraform_hostname),128 options: options129 )130 ::Kitchen::Verifier::Terraform::ConfigureInspecRunnerHost131 .call(132 hostname: state.fetch(:kitchen_terraform_hostname),133 options: options134 )135 ::Kitchen::Verifier::Terraform::ConfigureInspecRunnerPort136 .call(137 group: state.fetch(:kitchen_terraform_group),138 options: options139 )140 ::Kitchen::Verifier::Terraform::ConfigureInspecRunnerSSHKey141 .call(142 group: state.fetch(:kitchen_terraform_group),143 options: options144 )145 ::Kitchen::Verifier::Terraform::ConfigureInspecRunnerUser146 .call(147 group: state.fetch(:kitchen_terraform_group),148 options: options149 )150 options151 .store(152 :attributes,153 ::Kitchen::Verifier::Terraform::ConfigureInspecRunnerAttributes154 .call(155 group: state.fetch(:kitchen_terraform_group),156 output: ::Kitchen::Util.stringified_hash(state.fetch(:kitchen_terraform_output))157 )158 )159 ::Kitchen::Verifier::Terraform::ConfigureInspecRunnerControls160 .call(161 group: state.fetch(:kitchen_terraform_group),162 options: options163 )164 end165 end166end167require "kitchen/verifier/terraform/configure_inspec_runner_attributes"168require "kitchen/verifier/terraform/configure_inspec_runner_backend"169require "kitchen/verifier/terraform/configure_inspec_runner_controls"170require "kitchen/verifier/terraform/configure_inspec_runner_host"171require "kitchen/verifier/terraform/configure_inspec_runner_port"172require "kitchen/verifier/terraform/configure_inspec_runner_ssh_key"173require "kitchen/verifier/terraform/configure_inspec_runner_user"174require "kitchen/verifier/terraform/enumerate_groups_and_hostnames"...

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1describe attribute('my_attribute') do2 its('value') { should eq 'my_value' }3describe attribute('my_attribute') do4 its('value') { should eq 'my_value' }5describe attribute('my_attribute') do6 its('value') { should eq 'my_value' }7describe attribute('my_attribute') do8 its('value') { should eq 'my_value' }9describe attribute('my_attribute') do10 its('value') { should eq 'my_value' }11describe attribute('my_attribute') do12 its('value') { should eq 'my_value' }13describe attribute('my_attribute') do14 its('value') { should eq 'my_value' }15describe attribute('my_attribute') do16 its('value') { should eq 'my_value' }17describe attribute('my_attribute') do18 its('value') { should eq 'my_value' }19describe attribute('my_attribute') do20 its('value') { should eq 'my_value' }

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1 describe package("nginx") do2 it { should be_installed }3 describe service("nginx") do4 it { should be_running }5 describe package("nginx") do6 it { should be_installed }7 its("version") { should cmp "1.10.3" }8 describe port(80) do9 it { should be_listening }10 describe http("http://localhost") do11 its("status") { should cmp 200 }12 its("body") { should match /Welcome to nginx!/ }13 describe http("http://localhost") do14 its("status") { should cmp 200 }15 its("body") { should match /Welcome to nginx!/ }

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1control_name = inspec.fetch(:control_name)2control_group = inspec.fetch(:control_group)3control_file = inspec.fetch(:control_file)4control = inspec.fetch(:control)5control_name = inspec.fetch(:control_name)6control_group = inspec.fetch(:control_group)7control_file = inspec.fetch(:control_file)8control = inspec.fetch(:control)9control_name = inspec.fetch(:control_name)10control_group = inspec.fetch(:control_group)11control_file = inspec.fetch(:control_file)12control = inspec.fetch(:control)13control_name = inspec.fetch(:control_name)14control_group = inspec.fetch(:control_group)15control_file = inspec.fetch(:control_file)16control = inspec.fetch(:control)17control_name = inspec.fetch(:control_name)18control_group = inspec.fetch(:control_group)19control_file = inspec.fetch(:control_file)20control = inspec.fetch(:control)21control_name = inspec.fetch(:control_name)22control_group = inspec.fetch(:control_group)23control_file = inspec.fetch(:control_file)24control = inspec.fetch(:control)25control_name = inspec.fetch(:control_name)26control_group = inspec.fetch(:control_group)27control_file = inspec.fetch(:control_file)28control = inspec.fetch(:control)29control_name = inspec.fetch(:control_name)30control_group = inspec.fetch(:control_group)31control_file = inspec.fetch(:control_file)32control = inspec.fetch(:control)33control_name = inspec.fetch(:control_name)34control_group = inspec.fetch(:control_group)35control_file = inspec.fetch(:control_file)36control = inspec.fetch(:control)

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1my_attribute = attribute('my_attribute', default: 'default value')2my_attribute = attribute('my_attribute', default: 'default value')3my_attribute = attribute('my_attribute', default: 'default value')4my_attribute = attribute('my_attribute', default: 'default value')5my_attribute = attribute('my_attribute', default: 'default value')6my_attribute = attribute('my_attribute', default: 'default value')7my_attribute = attribute('my_attribute', default: 'default value')8my_attribute = attribute('my_attribute', default: 'default value')9my_attribute = attribute('my_attribute', default: 'default value')10my_attribute = attribute('my_attribute', default: 'default value')11my_attribute = attribute('my_attribute', default: 'default value')12my_attribute = attribute('my_attribute', default: '

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1a = attribute('my_attribute')2 it { should eq 'value' }3 value: { 'key1': 'val1', 'key2': 'val2' }

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1 describe input('input_var') do2 it { should eq 'input_var_value' }3 describe input('input_var') do4 it { should eq 'input_var_value' }5 describe input('input_var') do6 it { should eq 'input_var_value' }7 describe input('input_var') do8 it { should eq 'input_var_value' }9 describe input('input_var') do10 it { should eq 'input_var_value' }11 describe input('input_var') do12 it { should eq 'input_var_value' }13 describe input('input_var') do14 it { should eq 'input_var_value' }15 describe input('input_var') do16 it { should eq 'input_var_value' }

Full Screen

Full Screen

fetch

Using AI Code Generation

copy

Full Screen

1control_attribute_value = inspec.fetch('test_attribute', 'default_value')2inspec.attribute('test_attribute', value: 'test_value')3inspec.attribute('test_attribute', value: 'test_value', description: 'description of the control attribute')4inspec.attribute('test_attribute', value: 'test_value', description: 'description of the control attribute', required: true)5inspec.attribute('test_attribute', value: 'test_value', description: 'description of the control attribute', required: true, type: 'string')6inspec.attribute('test_attribute', value: 'test_value', description: 'description of the control attribute', required: true, type: '

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.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful