How to use get_headers method of InspecPlugins.Compliance Package

Best Inspec_ruby code snippet using InspecPlugins.Compliance.get_headers

api.rb

Source:api.rb Github

copy

Full Screen

...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 end...

Full Screen

Full Screen

get_headers

Using AI Code Generation

copy

Full Screen

1http = InspecPlugins::Compliance::HttpClient.new('https://automate.example.com', headers)2response = http.get('/compliance/profiles/search')3profiles = JSON.parse(response.body)4puts JSON.pretty_generate(profiles)5File.open('profiles.json', 'w') do |f|6 f.puts JSON.pretty_generate(profiles)7File.open('profiles.json', 'w') do |f|8 f.puts JSON.pretty_generate(profiles)9File.open('profiles.json', 'w') do |f|10 f.puts JSON.pretty_generate(profiles)11File.open('profiles.json', 'w') do |f|12 f.puts JSON.pretty_generate(profiles)13File.open('profiles.json', 'w') do |f|14 f.puts JSON.pretty_generate(profiles)15File.open('profiles.json', 'w') do |f|16 f.puts JSON.pretty_generate(profiles)17File.open('profiles.json', 'w') do |f|18 f.puts JSON.pretty_generate(profiles)19File.open('profiles.json', 'w') do |f|20 f.puts JSON.pretty_generate(profiles)

Full Screen

Full Screen

get_headers

Using AI Code Generation

copy

Full Screen

1http = InspecPlugins::Compliance::HttpClient.new('https://automate.example.com', headers)2response = http.get('/compliance/profiles/search')3profiles = JSON.parse(response.body)4puts JSON.pretty_generate(profiles)5File.open('profiles.json', 'w') do |f|6 f.puts JSON.pretty_generate(profiles)7File.open('profiles.json', 'w') do |f|8 f.puts JSON.pretty_generate(profiles)9File.open('profiles.json', 'w') do |f|10 f.puts JSON.pretty_generate(profiles)11File.open('profiles.json', 'w') do |f|12 f.puts JSON.pretty_generate(profiles)13File.open('profiles.json', 'w') do |f|14 f.puts JSON.pretty_generate(profiles)15File.open('profiles.json', 'w') do |f|16 f.puts JSON.pretty_generate(profiles)17File.open('profiles.json', 'w') do |f|18 f.puts JSON.pretty_generate(profiles)19File.open('profiles.json', 'w') do |f|20 f.puts JSON.pretty_generate(profiles)

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