How to use is_automate2_server method of InspecPlugins.Compliance Package

Best Inspec_ruby code snippet using InspecPlugins.Compliance.is_automate2_server

api.rb

Source:api.rb Github

copy

Full Screen

...22 # Chef Compliance23 if is_compliance_server?(config)24 url = "#{config['server']}/user/compliance"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'...

Full Screen

Full Screen

is_automate2_server

Using AI Code Generation

copy

Full Screen

1 class CLI < Inspec.plugin(2, :cli_command)2 def version(*args)3InspecPlugins::Compliance::CLI.new(ARGV)

Full Screen

Full Screen

is_automate2_server

Using AI Code Generation

copy

Full Screen

1InspecPlugins::Compliance::ComplianceCLI.new.is_automate2_server('https://automate2-server.test')2InspecPlugins::Compliance::ComplianceCLI.new.is_automate2_server('https://automate2-server.test')3InspecPlugins::Compliance::ComplianceCLI.is_automate2_server('https://automate2-server.test')4InspecPlugins::Compliance::ComplianceCLI.new.is_automate2_server('https://automate2-server.test')5InspecPlugins::Compliance::ComplianceCLI.is_automate2_server('https://automate2-server.test')6InspecPlugins::Compliance::ComplianceCLI.is_automate2_server('https://automate2-server.test')7InspecPlugins::Compliance::ComplianceCLI.new.is_automate2_server('https://automate2-server.test')

Full Screen

Full Screen

is_automate2_server

Using AI Code Generation

copy

Full Screen

1InspecPlugins::Compliance::Api.new(nil).is_automate2_server2 describe InspecPlugins::Compliance::Api.new(nil).is_automate2_server do3 it { should eq true }

Full Screen

Full Screen

is_automate2_server

Using AI Code Generation

copy

Full Screen

1 class ComplianceCLI < Inspec.plugin(2, :cli_command)2 def self.is_automate2_server(server)3 if Inspec::Plugins::Compliance.respond_to?(:is_automate2_server)4 Inspec::Plugins::Compliance.is_automate2_server(server)5 Inspec::Plugins::Compliance::API.is_automate2_server(server)6 class ComplianceCLI < Inspec.plugin(2, :cli_command)7 def self.is_automate2_server(server)8 Inspec::Plugins::Compliance.is_automate2_server(server)9 class ComplianceCLI < Inspec.plugin(2, :cli_command)10 def self.is_automate2_server(server)11 Inspec::Plugins::Compliance::API.is_automate2_server(server)12 class ComplianceCLI < Inspec.plugin(2, :cli_command)13 def self.is_automate2_server(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