How to use loggedin method of InspecPlugins.Compliance Package

Best Inspec_ruby code snippet using InspecPlugins.Compliance.loggedin

cli.rb

Source:cli.rb Github

copy

Full Screen

...37 option :owner, type: :string, required: false,38 desc: 'owner whose profiles to list'39 def profiles40 config = InspecPlugins::Compliance::Configuration.new41 return if !loggedin(config)42 # set owner to config43 config['owner'] = options['owner'] || config['user']44 msg, profiles = InspecPlugins::Compliance::API.profiles(config)45 profiles.sort_by! { |hsh| hsh['title'] }46 if !profiles.empty?47 # iterate over profiles48 headline('Available profiles:')49 profiles.each { |profile|50 owner = profile['owner_id'] || profile['owner']51 li("#{profile['title']} v#{profile['version']} (#{mark_text(owner + '/' + profile['name'])})")52 }53 else54 puts msg if msg != 'success'55 puts 'Could not find any profiles'56 exit 157 end58 rescue InspecPlugins::Compliance::ServerConfigurationMissing59 STDERR.puts "\nServer configuration information is missing. Please login using `inspec compliance login`"60 exit 161 end62 desc 'exec PROFILE', 'executes a Chef Compliance profile'63 exec_options64 def exec(*tests)65 config = InspecPlugins::Compliance::Configuration.new66 return if !loggedin(config)67 o = opts(:exec).dup68 diagnose(o)69 configure_logger(o)70 # iterate over tests and add compliance scheme71 tests = tests.map { |t| 'compliance://' + InspecPlugins::Compliance::API.sanitize_profile_name(t) }72 runner = Inspec::Runner.new(o)73 tests.each { |target| runner.add_target(target) }74 exit runner.run75 rescue ArgumentError, RuntimeError, Train::UserError => e76 $stderr.puts e.message77 exit 178 end79 desc 'download PROFILE', 'downloads a profile from Chef Compliance'80 option :name, type: :string,81 desc: 'Name of the archive filename (file type will be added)'82 def download(profile_name)83 o = options.dup84 configure_logger(o)85 config = InspecPlugins::Compliance::Configuration.new86 return if !loggedin(config)87 profile_name = InspecPlugins::Compliance::API.sanitize_profile_name(profile_name)88 if InspecPlugins::Compliance::API.exist?(config, profile_name)89 puts "Downloading `#{profile_name}`"90 fetcher = InspecPlugins::Compliance::Fetcher.resolve(91 {92 compliance: profile_name,93 },94 )95 # we provide a name, the fetcher adds the extension96 _owner, id = profile_name.split('/')97 file_name = fetcher.fetch(o.name || id)98 puts "Profile stored to #{file_name}"99 else100 puts "Profile #{profile_name} is not available in Chef Compliance."101 exit 1102 end103 end104 desc 'upload PATH', 'uploads a local profile to Chef Compliance'105 option :overwrite, type: :boolean, default: false,106 desc: 'Overwrite existing profile on Server.'107 option :owner, type: :string, required: false,108 desc: 'Owner that should own the profile'109 def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity110 config = InspecPlugins::Compliance::Configuration.new111 return if !loggedin(config)112 # set owner to config113 config['owner'] = options['owner'] || config['user']114 unless File.exist?(path)115 puts "Directory #{path} does not exist."116 exit 1117 end118 vendor_deps(path, options) if File.directory?(path)119 o = options.dup120 configure_logger(o)121 # only run against the mock backend, otherwise we run against the local system122 o[:backend] = Inspec::Backend.create(Inspec::Config.mock)123 o[:check_mode] = true124 o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache])125 # check the profile, we only allow to upload valid profiles126 profile = Inspec::Profile.for_target(path, o)127 # start verification process128 error_count = 0129 error = lambda { |msg|130 error_count += 1131 puts msg132 }133 result = profile.check134 unless result[:summary][:valid]135 error.call('Profile check failed. Please fix the profile before upload.')136 else137 puts('Profile is valid')138 end139 # determine user information140 if (config['token'].nil? && config['refresh_token'].nil?) || config['user'].nil?141 error.call('Please login via `inspec compliance login`')142 end143 # read profile name from inspec.yml144 profile_name = profile.params[:name]145 # read profile version from inspec.yml146 profile_version = profile.params[:version]147 # check that the profile is not uploaded already,148 # confirm upload to the user (overwrite with --force)149 if InspecPlugins::Compliance::API.exist?(config, "#{config['owner']}/#{profile_name}##{profile_version}") && !options['overwrite']150 error.call('Profile exists on the server, use --overwrite')151 end152 # abort if we found an error153 if error_count > 0154 puts "Found #{error_count} error(s)"155 exit 1156 end157 # if it is a directory, tar it to tmp directory158 generated = false159 if File.directory?(path)160 generated = true161 archive_path = Dir::Tmpname.create([profile_name, '.tar.gz']) {}162 puts "Generate temporary profile archive at #{archive_path}"163 profile.archive({ output: archive_path, ignore_errors: false, overwrite: true })164 else165 archive_path = path166 end167 puts "Start upload to #{config['owner']}/#{profile_name}"168 pname = ERB::Util.url_encode(profile_name)169 if InspecPlugins::Compliance::API.is_automate_server?(config) || InspecPlugins::Compliance::API.is_automate2_server?(config)170 puts 'Uploading to Chef Automate'171 else172 puts 'Uploading to Chef Compliance'173 end174 success, msg = InspecPlugins::Compliance::API.upload(config, config['owner'], pname, archive_path)175 # delete temp file if it was temporary generated176 File.delete(archive_path) if generated && File.exist?(archive_path)177 if success178 puts 'Successfully uploaded profile'179 else180 puts 'Error during profile upload:'181 puts msg182 exit 1183 end184 end185 desc 'version', 'displays the version of the Chef Compliance server'186 def version187 config = InspecPlugins::Compliance::Configuration.new188 info = InspecPlugins::Compliance::API.version(config)189 if !info.nil? && info['version']190 puts "Name: #{info['api']}"191 puts "Version: #{info['version']}"192 else193 puts 'Could not determine server version.'194 exit 1195 end196 rescue InspecPlugins::Compliance::ServerConfigurationMissing197 puts "\nServer configuration information is missing. Please login using `inspec compliance login`"198 exit 1199 end200 desc 'logout', 'user logout from Chef Compliance'201 def logout202 config = InspecPlugins::Compliance::Configuration.new203 unless config.supported?(:oidc) || config['token'].nil? || config['server_type'] == 'automate'204 config = InspecPlugins::Compliance::Configuration.new205 url = "#{config['server']}/logout"206 InspecPlugins::Compliance::HTTP.post(url, config['token'], config['insecure'], !config.supported?(:oidc))207 end208 success = config.destroy209 if success210 puts 'Successfully logged out'211 else212 puts 'Could not log out'213 end214 end215 private216 def loggedin(config)217 serverknown = !config['server'].nil?218 puts 'You need to login first with `inspec compliance login`' if !serverknown219 serverknown220 end221 end222 # register the subcommand to InSpec CLI registry223 # Inspec::Plugins::CLI.add_subcommand(InspecPlugins::ComplianceCLI, 'compliance', 'compliance SUBCOMMAND ...', 'Chef InspecPlugins::Compliance commands', {})224 end225end...

Full Screen

Full Screen

loggedin

Using AI Code Generation

copy

Full Screen

1InspecPlugins::Compliance::API.new.profiles('owner' => 'admin')2InspecPlugins::Compliance::API.new.profiles('owner' => 'admin', 'name' => 'linux-baseline')3InspecPlugins::Compliance::API.new.profiles('owner' => 'admin', 'name' => 'linux-baseline', 'version' => '2.0.0')4profiles = InspecPlugins::Compliance::API.new.profiles('owner' => 'admin', 'name' => 'linux-baseline', 'version' => '2.0.0')

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