How to use get method of InspecPlugins.Compliance Package

Best Inspec_ruby code snippet using InspecPlugins.Compliance.get

api_test.rb

Source:api_test.rb Github

copy

Full Screen

...47 'insecure' => true,48 }49 end50 before do51 InspecPlugins::Compliance::API.expects(:get_headers).returns(headers)52 end53 describe 'when a 404 is received' do54 it 'should return an empty hash' do55 response = mock56 response.stubs(:code).returns('404')57 InspecPlugins::Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response)58 InspecPlugins::Compliance::API.version(config).must_equal({})59 end60 end61 describe 'when the returned body is nil' do62 it 'should return an empty hash' do63 response = mock64 response.stubs(:code).returns('200')65 response.stubs(:body).returns(nil)66 InspecPlugins::Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response)67 InspecPlugins::Compliance::API.version(config).must_equal({})68 end69 end70 describe 'when the returned body is an empty string' do71 it 'should return an empty hash' do72 response = mock73 response.stubs(:code).returns('200')74 response.stubs(:body).returns('')75 InspecPlugins::Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response)76 InspecPlugins::Compliance::API.version(config).must_equal({})77 end78 end79 describe 'when the returned body has no version key' do80 it 'should return an empty hash' do81 response = mock82 response.stubs(:code).returns('200')83 response.stubs(:body).returns('{"api":"compliance"}')84 InspecPlugins::Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response)85 InspecPlugins::Compliance::API.version(config).must_equal({})86 end87 end88 describe 'when the returned body has an empty version key' do89 it 'should return an empty hash' do90 response = mock91 response.stubs(:code).returns('200')92 response.stubs(:body).returns('{"api":"compliance","version":""}')93 InspecPlugins::Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response)94 InspecPlugins::Compliance::API.version(config).must_equal({})95 end96 end97 describe 'when the returned body has a proper version' do98 it 'should return an empty hash' do99 response = mock100 response.stubs(:code).returns('200')101 response.stubs(:body).returns('{"api":"compliance","version":"1.2.3"}')102 InspecPlugins::Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response)103 InspecPlugins::Compliance::API.version(config).must_equal({ 'version' => '1.2.3', 'api' => 'compliance' })104 end105 end106 end107 describe 'automate/compliance is? checks' do108 describe 'when the config has a compliance server_type' do109 it 'automate/compliance server is? methods return correctly' do110 config = InspecPlugins::Compliance::Configuration.new111 config.clean112 config['server_type'] = 'compliance'113 InspecPlugins::Compliance::API.is_compliance_server?(config).must_equal true114 InspecPlugins::Compliance::API.is_automate_server?(config).must_equal false115 InspecPlugins::Compliance::API.is_automate_server_pre_080?(config).must_equal false116 InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config).must_equal false117 InspecPlugins::Compliance::API.is_automate2_server?(config).must_equal false118 end119 end120 describe 'when the config has a automate2 server_type' do121 it 'automate/compliance server is? methods return correctly' do122 config = InspecPlugins::Compliance::Configuration.new123 config.clean124 config['server_type'] = 'automate2'125 InspecPlugins::Compliance::API.is_compliance_server?(config).must_equal false126 InspecPlugins::Compliance::API.is_automate_server?(config).must_equal false127 InspecPlugins::Compliance::API.is_automate_server_pre_080?(config).must_equal false128 InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config).must_equal false129 InspecPlugins::Compliance::API.is_automate2_server?(config).must_equal true130 end131 end132 describe 'when the config has an automate server_type and no version key' do133 it 'automate/compliance server is? methods return correctly' do134 config = InspecPlugins::Compliance::Configuration.new135 config.clean136 config['server_type'] = 'automate'137 InspecPlugins::Compliance::API.is_compliance_server?(config).must_equal false138 InspecPlugins::Compliance::API.is_automate_server?(config).must_equal true139 InspecPlugins::Compliance::API.is_automate_server_pre_080?(config).must_equal true140 InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config).must_equal false141 InspecPlugins::Compliance::API.is_automate2_server?(config).must_equal false142 end143 end144 describe 'when the config has an automate server_type and a version key that is not a hash' do145 it 'automate/compliance server is? methods return correctly' do146 config = InspecPlugins::Compliance::Configuration.new147 config.clean148 config['server_type'] = 'automate'149 config['version'] = '1.2.3'150 InspecPlugins::Compliance::API.is_compliance_server?(config).must_equal false151 InspecPlugins::Compliance::API.is_automate_server?(config).must_equal true152 InspecPlugins::Compliance::API.is_automate_server_pre_080?(config).must_equal true153 InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config).must_equal false154 InspecPlugins::Compliance::API.is_automate2_server?(config).must_equal false155 end156 end157 describe 'when the config has an automate server_type and a version hash with no version' do158 it 'automate/compliance server is? methods return correctly' do159 config = InspecPlugins::Compliance::Configuration.new160 config.clean161 config['server_type'] = 'automate'162 config['version'] = {}163 InspecPlugins::Compliance::API.is_compliance_server?(config).must_equal false164 InspecPlugins::Compliance::API.is_automate_server?(config).must_equal true165 InspecPlugins::Compliance::API.is_automate_server_pre_080?(config).must_equal true166 InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config).must_equal false167 end168 end169 describe 'when the config has an automate server_type and a version hash with a version' do170 it 'automate/compliance server is? methods return correctly' do171 config = InspecPlugins::Compliance::Configuration.new172 config.clean173 config['server_type'] = 'automate'174 config['version'] = { 'version' => '0.8.1' }175 InspecPlugins::Compliance::API.is_compliance_server?(config).must_equal false176 InspecPlugins::Compliance::API.is_automate_server?(config).must_equal true177 InspecPlugins::Compliance::API.is_automate_server_pre_080?(config).must_equal false178 InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config).must_equal true179 end180 end181 end182 describe '.server_version_from_config' do183 it 'returns nil when the config has no version key' do184 config = {}185 InspecPlugins::Compliance::API.server_version_from_config(config).must_be_nil186 end187 it 'returns nil when the version value is not a hash' do188 config = { 'version' => '123' }189 InspecPlugins::Compliance::API.server_version_from_config(config).must_be_nil190 end191 it 'returns nil when the version value is a hash but has no version key inside' do192 config = { 'version' => {} }193 InspecPlugins::Compliance::API.server_version_from_config(config).must_be_nil194 end195 it 'returns the version if the version value is a hash containing a version' do196 config = { 'version' => { 'version' => '1.2.3' } }197 InspecPlugins::Compliance::API.server_version_from_config(config).must_equal '1.2.3'198 end199 end200 describe 'profile_split' do201 it 'handles a profile without version' do202 InspecPlugins::Compliance::API.profile_split('admin/apache-baseline').must_equal ['admin', 'apache-baseline', nil]203 end204 it 'handles a profile with a version' do205 InspecPlugins::Compliance::API.profile_split('admin/apache-baseline#2.0.1').must_equal ['admin', 'apache-baseline', '2.0.1']206 end207 end208 describe 'target_url' do209 it 'handles a automate profile with and without version' do210 config = InspecPlugins::Compliance::Configuration.new211 config.clean212 config['server_type'] = 'automate'213 config['server'] = 'https://myautomate'214 config['version'] = '1.6.99'215 InspecPlugins::Compliance::API.target_url(config, 'admin/apache-baseline').must_equal 'https://myautomate/profiles/admin/apache-baseline/tar'216 InspecPlugins::Compliance::API.target_url(config, 'admin/apache-baseline#2.0.2').must_equal 'https://myautomate/profiles/admin/apache-baseline/version/2.0.2/tar'217 end218 it 'handles a chef-compliance profile with and without version' do219 config = InspecPlugins::Compliance::Configuration.new220 config.clean221 config['server_type'] = 'compliance'222 config['server'] = 'https://mychefcompliance'223 config['version'] = '1.1.2'224 InspecPlugins::Compliance::API.target_url(config, 'admin/apache-baseline').must_equal 'https://mychefcompliance/owners/admin/compliance/apache-baseline/tar'225 InspecPlugins::Compliance::API.target_url(config, 'admin/apache-baseline#2.0.2').must_equal 'https://mychefcompliance/owners/admin/compliance/apache-baseline/tar'226 end227 end228 describe 'exist?' do229 it 'works with profiles returned by Automate' do230 # ruby 2.3.3 has issues running stub_requests properly231 # skipping for that specific version232 return if RUBY_VERSION = '2.3.3'233 config = InspecPlugins::Compliance::Configuration.new234 config.clean235 config['owner'] = 'admin'236 config['server_type'] = 'automate'237 config['server'] = 'https://myautomate'238 config['version'] = '1.6.99'239 config['automate'] = { 'ent'=>'automate', 'token_type'=>'dctoken' }240 config['version'] = { 'api'=> 'compliance', 'version'=>'0.8.24' }241 242 stub_request(:get, 'https://myautomate/profiles/admin')243 .with(headers: { 'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Chef-Delivery-Enterprise'=>'automate', 'User-Agent'=>'Ruby', 'X-Data-Collector-Token'=>'' })244 .to_return(status: 200, body: profiles_response.to_json, headers: {})245 InspecPlugins::Compliance::API.exist?(config, 'admin/apache-baseline').must_equal true246 InspecPlugins::Compliance::API.exist?(config, 'admin/apache-baseline#2.0.1').must_equal true247 InspecPlugins::Compliance::API.exist?(config, 'admin/apache-baseline#2.0.999').must_equal false248 InspecPlugins::Compliance::API.exist?(config, 'admin/missing-in-action').must_equal false249 end250 end251 describe '.determine_server_type' do252 let(:url) { 'https://someserver.onthe.net/' }253 let(:compliance_endpoint) { '/api/version' }254 let(:automate_endpoint) { '/compliance/version' }255 let(:automate2_endpoint) { '/dex/auth' }256 let(:headers) { nil }257 let(:insecure) { true }258 let(:good_response) { mock }259 let(:bad_response) { mock }260 it 'returns `:automate2` when a 400 is received from `https://URL/dex/auth`' do261 good_response.stubs(:code).returns('400')262 InspecPlugins::Compliance::HTTP.expects(:get)263 .with(url + automate2_endpoint, headers, insecure)264 .returns(good_response)265 InspecPlugins::Compliance::API.determine_server_type(url, insecure).must_equal(:automate2)266 end267 it 'returns `:automate` when a 401 is received from `https://URL/compliance/version`' do268 good_response.stubs(:code).returns('401')269 bad_response.stubs(:code).returns('404')270 InspecPlugins::Compliance::HTTP.expects(:get)271 .with(url + automate2_endpoint, headers, insecure)272 .returns(bad_response)273 InspecPlugins::Compliance::HTTP.expects(:get)274 .with(url + automate_endpoint, headers, insecure)275 .returns(good_response)276 InspecPlugins::Compliance::API.determine_server_type(url, insecure).must_equal(:automate)277 end278 # Chef Automate currently returns 401 for `/compliance/version` but some279 # versions of OpsWorks Chef Automate return 200 and a Chef Manage page when280 # unauthenticated requests are received.281 it 'returns `:automate` when a 200 is received from `https://URL/compliance/version`' do282 bad_response.stubs(:code).returns('404')283 good_response.stubs(:code).returns('200')284 good_response.stubs(:body).returns('Are You Looking For the Chef Server?')285 InspecPlugins::Compliance::HTTP.expects(:get)286 .with(url + automate2_endpoint, headers, insecure)287 .returns(bad_response)288 InspecPlugins::Compliance::HTTP.expects(:get)289 .with(url + automate_endpoint, headers, insecure)290 .returns(good_response)291 InspecPlugins::Compliance::API.determine_server_type(url, insecure).must_equal(:automate)292 end293 it 'returns `nil` if a 200 is received from `https://URL/compliance/version` but not redirected to Chef Manage' do294 bad_response.stubs(:code).returns('200')295 bad_response.stubs(:body).returns('No Chef Manage here')296 InspecPlugins::Compliance::HTTP.expects(:get)297 .with(url + automate_endpoint, headers, insecure)298 .returns(bad_response)299 InspecPlugins::Compliance::HTTP.expects(:get)300 .with(url + automate2_endpoint, headers, insecure)301 .returns(bad_response)302 mock_compliance_response = mock303 mock_compliance_response.stubs(:code).returns('404')304 InspecPlugins::Compliance::HTTP.expects(:get)305 .with(url + compliance_endpoint, headers, insecure)306 .returns(mock_compliance_response)307 InspecPlugins::Compliance::API.determine_server_type(url, insecure).must_be_nil308 end309 it 'returns `:compliance` when a 200 is received from `https://URL/api/version`' do310 good_response.stubs(:code).returns('200')311 bad_response.stubs(:code).returns('404')312 InspecPlugins::Compliance::HTTP.expects(:get)313 .with(url + automate_endpoint, headers, insecure)314 .returns(bad_response)315 InspecPlugins::Compliance::HTTP.expects(:get)316 .with(url + automate2_endpoint, headers, insecure)317 .returns(bad_response)318 InspecPlugins::Compliance::HTTP.expects(:get)319 .with(url + compliance_endpoint, headers, insecure)320 .returns(good_response)321 InspecPlugins::Compliance::API.determine_server_type(url, insecure).must_equal(:compliance)322 end323 it 'returns `nil` if it cannot determine the server type' do324 bad_response.stubs(:code).returns('404')325 InspecPlugins::Compliance::HTTP.expects(:get)326 .with(url + automate2_endpoint, headers, insecure)327 .returns(bad_response)328 InspecPlugins::Compliance::HTTP.expects(:get)329 .with(url + automate_endpoint, headers, insecure)330 .returns(bad_response)331 InspecPlugins::Compliance::HTTP.expects(:get)332 .with(url + compliance_endpoint, headers, insecure)333 .returns(bad_response)334 InspecPlugins::Compliance::API.determine_server_type(url, insecure).must_be_nil335 end336 end337end...

Full Screen

Full Screen

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'100 end101 private102 # determine the owner_id and the profile name from the url103 def compliance_profile_name104 m = if InspecPlugins::Compliance::API.is_automate_server_pre_080?(@config)105 %r{^#{@config['server']}/(?<owner>[^/]+)/(?<id>[^/]+)/tar$}106 elsif InspecPlugins::Compliance::API.is_automate_server_080_and_later?(@config)107 %r{^#{@config['server']}/profiles/(?<owner>[^/]+)/(?<id>[^/]+)/tar$}108 else109 %r{^#{@config['server']}/owners/(?<owner>[^/]+)/compliance/(?<id>[^/]+)/tar$}110 end.match(@target)111 if InspecPlugins::Compliance::API.is_automate2_server?(@config)112 m = {}113 m[:owner] = @config['profile'][0]114 m[:id] = @config['profile'][1]115 end116 raise 'Unable to determine compliance profile name. This can be caused by ' \117 'an incorrect server in your configuration. Try to login to compliance ' \118 'via the `inspec compliance login` command.' if m.nil?119 "#{m[:owner]}/#{m[:id]}"120 end121 end122 end123end...

Full Screen

Full Screen

get

Using AI Code Generation

copy

Full Screen

1InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')2InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')3InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')4InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')5InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')6InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')7InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')8InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')9InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')10InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')11InspecPlugins::Compliance::API.new('https://compliance.test', 'token').get('profiles')

Full Screen

Full Screen

get

Using AI Code Generation

copy

Full Screen

1report = InspecPlugins::Compliance::ComplianceTarget.new("https://compliance-server.compliance", "user", "password").get_report("profile-name")2InspecPlugins::Compliance::ComplianceTarget.new("https://compliance-server.compliance", "user", "password").get_report("profile-name")3InspecPlugins::Compliance::ComplianceTarget.new("https://compliance-server.compliance", "user", "password")4InspecPlugins::Compliance::ComplianceTarget.new("https://compliance-server.compliance", "user", "password").get_report("profile-name")

Full Screen

Full Screen

get

Using AI Code Generation

copy

Full Screen

1compliance_cli.get('admin/linux-baseline', '1.0.0')2inspec.compliance('get', 'admin/linux-baseline', '1.0.0')3inspec.compliance('get', 'admin/linux-baseline', '1.0.0', '--target-dir', '/tmp')

Full Screen

Full Screen

get

Using AI Code Generation

copy

Full Screen

1api = InspecPlugins::Compliance::API.new(2api = InspecPlugins::Compliance::API.new(3profile = profiles.find { |p| p['name'] == 'my_profile' }4controls = InspecPlugins::Compliance::Profile.new(profile).controls

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful