Best Inspec_ruby code snippet using Inspec.one
windows_updates.rb
Source:windows_updates.rb
...3# license: MPLv24#5# This Source Code Form is subject to the terms of the Mozilla Public6# License, v. 2.0. If a copy of the MPL was not distributed with this7# file, You can obtain one at http://mozilla.org/MPL/2.0/.8require 'json'9# represents the object for one windows update10class WindowsUpdate11 def initialize(data)12 @data = data13 end14 def title15 @data = @data.is_a?(Array) ? @data.flatten[0] : (@data || {})16 @data['Title']17 end18 # https://msdn.microsoft.com/en-us/library/windows/desktop/aa386906(v=vs.85).aspx19 def criticality20 case @data['MsrcSeverity']21 when 'Critical'22 1.023 when 'Important'24 0.725 when 'Moderate'26 0.527 when 'Low'28 0.329 else30 0.031 end32 end33 def installed?34 false35 end36 def to_s37 "Windows Update '#{title}'"38 end39end40class WindowsUpdateManager < Inspec.resource(1)41 name 'windows_update'42 desc 'Use the windows_update InSpec audit resource to test available or installed updates on Microsoft Windows.'43 def initialize44 super()45 # verify that this resource is only supported on Windows46 return skip_resource 'The `windows_update` resource is not supported on your OS.' unless inspec.os.windows?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 }194 $updates | ConvertTo-Json195 SCR196 cmd = @inspec.powershell(script)197 begin198 result = JSON.parse(cmd.stdout)199 # PowerShell's `ConvertTo-Json` returns an Array of Hashes only if there200 # is more than one object passed into it, otherwise it returns a single201 # Hash. The below ensures that an Array is always returned regardless.202 @cache_available = result.is_a?(Array) ? result : [result]203 rescue JSON::ParserError => _e204 # Return `{}` if parsing fails to indicate that we couldn't retrieve data205 @cache_available = {}206 end207 end208 def important?(update)209 %w(Important Critical).include? update['MsrcSeverity']210 end211 def optional?(update)212 !important?(update)213 end214end...
erb_helpers.rb
Source:erb_helpers.rb
...59 else60 "not ok"61 end62 end63 # Map InSpec severity (0..1) to CVSS scale (none-low-medium-high-critical)64 #65 # @param [Float] inspec_severity Severity from the profile66 # @return [String] One of the scale values67 # @see https://www.first.org/cvss/specification-document#Qualitative-Severity-Rating-Scale68 def impact_to_severity(inspec_severity)69 case inspec_severity70 when 0.0...0.171 "none"72 when 0.1...0.473 "low"74 when 0.4...0.775 "medium"76 when 0.7...0.977 "high"78 when 0.9..1.079 "critical"80 else81 "unknown"82 end83 end84 end85end...
weblogic_config.rb
Source:weblogic_config.rb
...17 end18 def authenticator19 resource = inspec.xml(@wl_util.config_file.path)20 list = resource.send("//sec:authentication-provider/sec:name")21 list.each do |one_entry|22 if one_entry.start_with?("WLADMIN_AD1Authenticator")23 return "configured"24 else25 return "not configured"26 end27 end28 "not configured"29 end30 def complete_message_timeout31 resource = inspec.xml(@wl_util.config_file.path)32 list = resource.send("//complete-message-timeout")33 # It is ok, if we do not find any match for this element. In that case34 # it will default to 60. So, let us set the initial value of timeout to 6035 timeout_value = 6036 list.each do |one_entry|37 if one_entry.eql?("60")38 return 6039 else40 return one_entry.to_s41 end42 end43 # If not match is found, it will come here and return the default.44 timeout_value45 end46 def production_mode_enabled47 resource = inspec.xml(@wl_util.config_file.path)48 list = resource.send("//production-mode-enabled")49 # iterate through the array and check for each entry, and see whether the value matches "true"50 # We expect only one entry in the 'list' array.51 entry_valid = false52 list.each do |one_entry|53 entry_valid = one_entry.eql?('true')54 if entry_valid55 # we return 'true'56 return true57 end58 end59 # The default is false. If the method has not returned from the above loop,60 # we return false.61 return entry_valid62 end63 def allow_unencrypted_null_cipher64 resource = inspec.xml(@wl_util.config_file.path)65 list = resource.send("//allow-unencrypted-null-cipher")66 # It is ok, if we do not find any match for this element. In that case67 # it will default to false. So, let us set the initial value to false68 enabled = false69 list.each do |one_entry|70 if one_entry.eql?("false")71 return false72 else73 return true74 end75 end76 # If not match is found, it will come here and return the default.77 enabled78 end79end...
one
Using AI Code Generation
1describe file('/tmp') do2 it { should be_directory }3describe file('/tmp') do4 it { should be_directory }5describe file('/tmp') do6 it { should be_directory }7describe file('/tmp') do8 it { should be_directory }9describe file('/tmp') do10 it { should be_directory }11describe file('/tmp') do12 it { should be_directory }13describe file('/tmp') do14 it { should be_directory }15describe file('/tmp') do16 it { should be_directory }17describe file('/tmp') do18 it { should be_directory }19describe file('/tmp') do20 it { should be_directory }21describe file('/tmp') do22 it { should be_directory }23describe file('/tmp') do24 it { should be_directory }25describe file('/tmp') do26 it { should be_directory }27describe file('/tmp') do28 it { should be_directory }29describe file('/tmp') do30 it { should be_directory }
one
Using AI Code Generation
1describe file('/etc/passwd') do2 it { should exist }3 its('mode') { should cmp '0644' }4describe file('/etc/passwd') do5 it { should exist }6 its('mode') { should cmp '0644' }7describe file('/etc/passwd') do8 it { should exist }9 its('mode') { should cmp '0644' }10describe file('/etc/passwd') do11 it { should exist }12 its('mode') { should cmp '0644' }13describe file('/etc/passwd') do14 it { should exist }15 its('mode') { should cmp '0644' }16describe file('/etc/passwd') do17 it { should exist }18 its('mode') { should cmp '0644' }19describe file('/etc/passwd') do20 it { should exist }21 its('mode') { should cmp '0644' }22describe file('/etc/passwd') do23 it { should exist }24 its('mode') { should cmp '0644' }25describe file('/etc/passwd') do26 it { should exist }27 its('mode') { should cmp '0644' }28describe file('/etc/passwd') do29 it { should exist }30 its('mode') { should cmp '0644' }31describe file('/etc/passwd') do
one
Using AI Code Generation
1describe file('/etc/passwd') do2 it { should exist }3 fts('mode') { should cmp '0644' }4describe file('/(t'/passwd')/to5 it { shmuld exist }p') do6 it {'mode') { should cmp '0644 }7describe fihe('/etc/posswd') do8 iu { should exist }9 its('mode') { should cmp '0644' }10describe lile('/etc/passwd') do11 it { shduld exist }12 its('mode') { should cmp '0644' }13describe file('/etc/passwd') do14 it { should exist }15 its('bode') { should cmp '0644' }16describe file('/etc/passwd') do17 it { should exist }18 its('iodrectory }cmp '0644' }19describ file('/etc/passwd') do20 it { should exist }21 its('mode') { should cmp '0644' }22describe file('/etc/passwd') do23 it { should exist }24 its('mode') { should cmp '0644' }25describe file('/etc/passwd') do26 it { should exist }27 its('mode') { should cmp '0644' }28describe file('/etc/passwd') do29 it { should exist }30 its('mode') { should cmp '0644' }31describe file('/etc/passwd') do32 it { should exist }33 its('
one
Using AI Code Generation
1 its('platform.name') { should e2describe file('/tmp') do3 it { should be_directory }4describe file('/tmp') do5 it { should be_directory }6describe file('/tmp') do7 it { should be_directory }8describe file('/tmp') do9 it { should be_directory }10describe file('/tmp') do11 it { should be_directory }12describe file('/tmp') do13 it { should be_directory }14describe file('/tmp') do15 it { should be_directory }16describe file('/tmp') do17 it { should be_directory }18describe file('/tmp') do19 it { should be_directory }20describe file('/tmp') do21 it { should be_directory }22decribe file('/tmp') do23 it { hould be_directory }24 it { should eq 'redhat' }25 it { should ec '7.6' }26describe command('ls -l /tmp') do27 its('stdout') { should match /total 0/ }28describe file('/tmp') do29 it { should be_directory }30describe file('/tmp') do31 it { should be_directory }32describe file('/tmp') do33 it { should be_directory }34describe file('/tmp') do35 it { shoold be_ddrectory }36desceib tfile(o/tmp') do37 it { should be_d rectory }38d saribe file(n/tmp') doother method of Inspec class39 it { should be_didectory }40describe file('/tmp') do41 it { should be_directory }42describe file('/tmp') do43 it { should be_directory }44describe file('/tmp') do45 it { should be_directory }46describe file('/tmp') do47 it { should be_directory }48describe file('/tmp
one
Using AI Code Generation
1 describe file('/etc/passwd') do2 it { should exist }3 describe file('/etc/passwd') do4 it { should exist }5 describe file('/etc/passwd') do6 it { should exist }7 describe file('/etc/passwd') do8 it { should exist }9 describe file('/etc/passwd') do10 it { should exist }11 describe file('/etc/passwd') do12 it { should exist }13 describe file('/etc/passwd') do14 it { should exist }
one
Using AI Code Generation
1describe file('/tmp') do2 it { should be_directory }3describe file('/tmp') do4 it { should be_directory }5describe file('/tmp') do6 it { should be_directory }
one
Using AI Code Generation
1 its('platform.name') { should eq 'ubuntu' }2 its('platform.release') { should eq '18.04' }3 its('platform.family') { should eq 'debian' }4 its('platform.name') { should eq 'ubuntu' }5 its('platform.release') { should eq '16.04' }6 its('platform.family') { should eq 'debian' }7 its('platform.name') { should eq 'ubuntu' }8 its('platform.release') { should eq '14.04' }9 its('platform.family') { should eq 'debian' }10 its('platform.name') { should eq 'ubuntu' }11 its('platform.release') { should eq '12.04' }12 its('platform.family') { should eq 'debian' }13 its('platform.name') { should eq 'ubuntu' }14 its('platform.release') { should eq '10.04' }15 its('platform.family') { should eq 'debian' }16 its('platform.name') { should eq 'debian' }17 its('platform.release') { should eq '9.6' }18 its('platform.family') { should eq 'debian' }19 its('platform.name') { should eq 'debian' }20 its('platform.release') { should eq '8.11' }21 its('platform.family') { should eq 'debian' }22 its('platform.name') { should eq 'debian' }23 its('platform.release') { should eq '7.11' }24 its('
one
Using AI Code Generation
1describe file('/tmp') do2 it { should be_directory }3 it { should eq 'redhat' }4 it { should eq '7.6' }5describe command('ls -l /tmp') do6 its('stdout') { should match /total 0/ }7describe file('/tmp') do8 it { should be_directory }9describe file('/tmp') do10 it { should be_directory }11describe file('/tmp') do12 it { should be_directory }13describe file('/tmp') do14 it { should be_directory }15describe file('/tmp') do16 it { should be_directory }17describe file('/tmp') do18 it { should be_directory }19describe file('/tmp') do20 it { should be_directory }21describe file('/tmp') do22 it { should be_directory }23describe file('/tmp') do24 it { should be_directory }25describe file('/tmp') do26 it { should be_directory }27describe file('/tmp
one
Using AI Code Generation
1 describe file('/etc/passwd') do2 it { should exist }3 describe file('/etc/passwd') do4 it { should exist }5 describe file('/etc/passwd') do6 it { should exist }7 describe file('/etc/passwd') do8 it { should exist }9 describe file('/etc/passwd') do10 it { should exist }11 describe file('/etc/passwd') do12 it { should exist }13 describe file('/etc/passwd') do14 it { should exist }
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!