How to use is_automate_server method of InspecPlugins.Compliance Package

Best Inspec_ruby code snippet using InspecPlugins.Compliance.is_automate_server

api_test.rb

Source:api_test.rb Github

copy

Full Screen

...111 config = InspecPlugins::Compliance::Configuration.new112 config.clean113 config["server_type"] = "compliance"114 _(InspecPlugins::Compliance::API.is_compliance_server?(config)).must_equal true115 _(InspecPlugins::Compliance::API.is_automate_server?(config)).must_equal false116 _(InspecPlugins::Compliance::API.is_automate_server_pre_080?(config)).must_equal false117 _(InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config)).must_equal false118 _(InspecPlugins::Compliance::API.is_automate2_server?(config)).must_equal false119 end120 end121 describe "when the config has a automate2 server_type" do122 it "automate/compliance server is? methods return correctly" do123 config = InspecPlugins::Compliance::Configuration.new124 config.clean125 config["server_type"] = "automate2"126 _(InspecPlugins::Compliance::API.is_compliance_server?(config)).must_equal false127 _(InspecPlugins::Compliance::API.is_automate_server?(config)).must_equal false128 _(InspecPlugins::Compliance::API.is_automate_server_pre_080?(config)).must_equal false129 _(InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config)).must_equal false130 _(InspecPlugins::Compliance::API.is_automate2_server?(config)).must_equal true131 end132 end133 describe "when the config has an automate server_type and no version key" do134 it "automate/compliance server is? methods return correctly" do135 config = InspecPlugins::Compliance::Configuration.new136 config.clean137 config["server_type"] = "automate"138 _(InspecPlugins::Compliance::API.is_compliance_server?(config)).must_equal false139 _(InspecPlugins::Compliance::API.is_automate_server?(config)).must_equal true140 _(InspecPlugins::Compliance::API.is_automate_server_pre_080?(config)).must_equal true141 _(InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config)).must_equal false142 _(InspecPlugins::Compliance::API.is_automate2_server?(config)).must_equal false143 end144 end145 describe "when the config has an automate server_type and a version key that is not a hash" do146 it "automate/compliance server is? methods return correctly" do147 config = InspecPlugins::Compliance::Configuration.new148 config.clean149 config["server_type"] = "automate"150 config["version"] = "1.2.3"151 _(InspecPlugins::Compliance::API.is_compliance_server?(config)).must_equal false152 _(InspecPlugins::Compliance::API.is_automate_server?(config)).must_equal true153 _(InspecPlugins::Compliance::API.is_automate_server_pre_080?(config)).must_equal true154 _(InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config)).must_equal false155 _(InspecPlugins::Compliance::API.is_automate2_server?(config)).must_equal false156 end157 end158 describe "when the config has an automate server_type and a version hash with no version" do159 it "automate/compliance server is? methods return correctly" do160 config = InspecPlugins::Compliance::Configuration.new161 config.clean162 config["server_type"] = "automate"163 config["version"] = {}164 _(InspecPlugins::Compliance::API.is_compliance_server?(config)).must_equal false165 _(InspecPlugins::Compliance::API.is_automate_server?(config)).must_equal true166 _(InspecPlugins::Compliance::API.is_automate_server_pre_080?(config)).must_equal true167 _(InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config)).must_equal false168 end169 end170 describe "when the config has an automate server_type and a version hash with a version" do171 it "automate/compliance server is? methods return correctly" do172 config = InspecPlugins::Compliance::Configuration.new173 config.clean174 config["server_type"] = "automate"175 config["version"] = { "version" => "0.8.1" }176 _(InspecPlugins::Compliance::API.is_compliance_server?(config)).must_equal false177 _(InspecPlugins::Compliance::API.is_automate_server?(config)).must_equal true178 _(InspecPlugins::Compliance::API.is_automate_server_pre_080?(config)).must_equal false179 _(InspecPlugins::Compliance::API.is_automate_server_080_and_later?(config)).must_equal true180 end181 end182 end183 describe ".server_version_from_config" do184 it "returns nil when the config has no version key" do185 config = {}186 _(InspecPlugins::Compliance::API.server_version_from_config(config)).must_be_nil187 end188 it "returns nil when the version value is not a hash" do189 config = { "version" => "123" }190 _(InspecPlugins::Compliance::API.server_version_from_config(config)).must_be_nil191 end192 it "returns nil when the version value is a hash but has no version key inside" do193 config = { "version" => {} }...

Full Screen

Full Screen

api.rb

Source:api.rb Github

copy

Full Screen

...25 # Chef Automate226 elsif is_automate2_server?(config)27 url = "#{config['server']}/compliance/profiles/search"28 # Chef Automate29 elsif is_automate_server?(config)30 url = "#{config['server']}/profiles/#{owner}"31 else32 raise ServerConfigurationMissing33 end34 headers = get_headers(config)35 if profile_filter36 _owner, id, ver = profile_split(profile_filter)37 else38 id, ver = nil39 end40 if is_automate2_server?(config)41 body = { owner: owner, name: id }.to_json42 response = InspecPlugins::Compliance::HTTP.post_with_headers(url, headers, body, config['insecure'])43 else44 response = InspecPlugins::Compliance::HTTP.get(url, headers, config['insecure'])45 end46 data = response.body47 response_code = response.code48 case response_code49 when '200'50 msg = 'success'51 profiles = JSON.parse(data)52 # iterate over profiles53 if is_compliance_server?(config)54 mapped_profiles = []55 profiles.values.each { |org|56 mapped_profiles += org.values57 }58 # Chef Automate pre 0.8.059 elsif is_automate_server_pre_080?(config)60 mapped_profiles = profiles.values.flatten61 elsif is_automate2_server?(config)62 mapped_profiles = []63 profiles['profiles'].each { |p|64 mapped_profiles << p65 }66 else67 mapped_profiles = profiles.map { |e|68 e['owner_id'] = owner69 e70 }71 end72 # filter by name and version if they were specified in profile_filter73 mapped_profiles.select! do |p|74 (!ver || p['version'] == ver) && (!id || p['name'] == id)75 end76 return msg, mapped_profiles77 when '401'78 msg = '401 Unauthorized. Please check your token.'79 return msg, []80 else81 msg = "An unexpected error occurred (HTTP #{response_code}): #{response.message}"82 return msg, []83 end84 end85 # return the server api version86 # NB this method does not use Compliance::Configuration to allow for using87 # it before we know the version (e.g. oidc or not)88 def self.version(config)89 url = config['server']90 insecure = config['insecure']91 raise ServerConfigurationMissing if url.nil?92 headers = get_headers(config)93 response = InspecPlugins::Compliance::HTTP.get(url+'/version', headers, insecure)94 return {} if response.code == '404'95 data = response.body96 return {} if data.nil? || data.empty?97 parsed = JSON.parse(data)98 return {} unless parsed.key?('version') && !parsed['version'].empty?99 parsed100 end101 # verifies that a profile exists102 def self.exist?(config, profile)103 _msg, profiles = InspecPlugins::Compliance::API.profiles(config, profile)104 !profiles.empty?105 end106 def self.upload(config, owner, profile_name, archive_path)107 # Chef Compliance108 if is_compliance_server?(config)109 url = "#{config['server']}/owners/#{owner}/compliance/#{profile_name}/tar"110 # Chef Automate pre 0.8.0111 elsif is_automate_server_pre_080?(config)112 url = "#{config['server']}/#{owner}"113 elsif is_automate2_server?(config)114 url = "#{config['server']}/compliance/profiles?owner=#{owner}"115 # Chef Automate116 else117 url = "#{config['server']}/profiles/#{owner}"118 end119 headers = get_headers(config)120 if is_automate2_server?(config)121 res = InspecPlugins::Compliance::HTTP.post_multipart_file(url, headers, archive_path, config['insecure'])122 else123 res = InspecPlugins::Compliance::HTTP.post_file(url, headers, archive_path, config['insecure'])124 end125 [res.is_a?(Net::HTTPSuccess), res.body]126 end127 # Use username and refresh_token to get an API access token128 def self.get_token_via_refresh_token(url, refresh_token, insecure)129 uri = URI.parse("#{url}/login")130 req = Net::HTTP::Post.new(uri.path)131 req.body = { token: refresh_token }.to_json132 access_token = nil133 response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)134 data = response.body135 if response.code == '200'136 begin137 tokendata = JSON.parse(data)138 access_token = tokendata['access_token']139 msg = 'Successfully fetched API access token'140 success = true141 rescue JSON::ParserError => e142 success = false143 msg = e.message144 end145 else146 success = false147 msg = "Failed to authenticate to #{url} \n\148 Response code: #{response.code}\n Body: #{response.body}"149 end150 [success, msg, access_token]151 end152 # Use username and password to get an API access token153 def self.get_token_via_password(url, username, password, insecure)154 uri = URI.parse("#{url}/login")155 req = Net::HTTP::Post.new(uri.path)156 req.body = { userid: username, password: password }.to_json157 access_token = nil158 response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)159 data = response.body160 if response.code == '200'161 access_token = data162 msg = 'Successfully fetched an API access token valid for 12 hours'163 success = true164 else165 success = false166 msg = "Failed to authenticate to #{url} \n\167 Response code: #{response.code}\n Body: #{response.body}"168 end169 [success, msg, access_token]170 end171 def self.get_headers(config)172 token = get_token(config)173 if is_automate_server?(config) || is_automate2_server?(config)174 headers = { 'chef-delivery-enterprise' => config['automate']['ent'] }175 if config['automate']['token_type'] == 'dctoken'176 headers['x-data-collector-token'] = token177 else178 headers['chef-delivery-user'] = config['user']179 headers['chef-delivery-token'] = token180 end181 else182 headers = { 'Authorization' => "Bearer #{token}" }183 end184 headers185 end186 def self.get_token(config)187 return config['token'] unless config['refresh_token']188 _success, _msg, token = get_token_via_refresh_token(config['server'], config['refresh_token'], config['insecure'])189 token190 end191 def self.target_url(config, profile)192 owner, id, ver = profile_split(profile)193 return "#{config['server']}/compliance/profiles/tar" if is_automate2_server?(config)194 return "#{config['server']}/owners/#{owner}/compliance/#{id}/tar" unless is_automate_server?(config)195 if ver.nil?196 "#{config['server']}/profiles/#{owner}/#{id}/tar"197 else198 "#{config['server']}/profiles/#{owner}/#{id}/version/#{ver}/tar"199 end200 end201 def self.profile_split(profile)202 owner, id = profile.split('/')203 id, version = id.split('#')204 [owner, id, version]205 end206 # returns a parsed url for `admin/profile` or `compliance://admin/profile`207 def self.sanitize_profile_name(profile)208 if URI(profile).scheme == 'compliance'209 uri = URI(profile)210 else211 uri = URI("compliance://#{profile}")212 end213 uri.to_s.sub(%r{^compliance:\/\/}, '')214 end215 def self.is_compliance_server?(config)216 config['server_type'] == 'compliance'217 end218 def self.is_automate_server_pre_080?(config)219 # Automate versions before 0.8.x do not have a valid version in the config220 return false unless config['server_type'] == 'automate'221 server_version_from_config(config).nil?222 end223 def self.is_automate_server_080_and_later?(config)224 # Automate versions 0.8.x and later will have a "version" key in the config225 # that is properly parsed out via server_version_from_config below226 return false unless config['server_type'] == 'automate'227 !server_version_from_config(config).nil?228 end229 def self.is_automate2_server?(config)230 config['server_type'] == 'automate2'231 end232 def self.is_automate_server?(config)233 config['server_type'] == 'automate'234 end235 def self.server_version_from_config(config)236 # Automate versions 0.8.x and later will have a "version" key in the config237 # that looks like: "version":{"api":"compliance","version":"0.8.24"}238 return nil unless config.key?('version')239 return nil unless config['version'].is_a?(Hash)240 config['version']['version']241 end242 def self.determine_server_type(url, insecure)243 if target_is_automate2_server?(url, insecure)244 :automate2245 elsif target_is_automate_server?(url, insecure)246 :automate247 elsif target_is_compliance_server?(url, insecure)248 :compliance249 else250 Inspec::Log.debug('Could not determine server type using known endpoints')251 nil252 end253 end254 def self.target_is_automate2_server?(url, insecure)255 automate_endpoint = '/dex/auth'256 response = InspecPlugins::Compliance::HTTP.get(url + automate_endpoint, nil, insecure)257 if response.code == '400'258 Inspec::Log.debug(259 "Received 400 from #{url}#{automate_endpoint} - " \260 'assuming target is a Chef Automate2 instance',261 )262 true263 else264 false265 end266 end267 def self.target_is_automate_server?(url, insecure)268 automate_endpoint = '/compliance/version'269 response = InspecPlugins::Compliance::HTTP.get(url + automate_endpoint, nil, insecure)270 case response.code271 when '401'272 Inspec::Log.debug(273 "Received 401 from #{url}#{automate_endpoint} - " \274 'assuming target is a Chef Automate instance',275 )276 true277 when '200'278 # Chef Automate currently returns 401 for `/compliance/version` but some279 # versions of OpsWorks Chef Automate return 200 and a Chef Manage page280 # when unauthenticated requests are received.281 if response.body.include?('Are You Looking For the Chef Server?')...

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