Skip to content

Commit

Permalink
Merge f162386 into a35d370
Browse files Browse the repository at this point in the history
  • Loading branch information
zenspider committed Jun 12, 2019
2 parents a35d370 + f162386 commit eb1e643
Show file tree
Hide file tree
Showing 39 changed files with 136 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -22,7 +22,7 @@ before_install:
- gem --version
- bundle --version
env:
- SLOW=1
- SLOW=1 NO_AWS=1
- CI_ENABLE_COVERAGE=true SLOW=1
script: bundle exec rake $SUITE
matrix:
Expand All @@ -31,7 +31,7 @@ matrix:
- os: osx
env: CI_ENABLE_COVERAGE=true SLOW=1
- os: linux
env: SLOW=1
env: SLOW=1 NO_AWS=1
include:
- rvm: 2.6.3
- rvm: 2.5.5
Expand Down
1 change: 0 additions & 1 deletion inspec-bin/bin/inspec
Expand Up @@ -7,6 +7,5 @@ Encoding.default_internal = Encoding::UTF_8
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require "inspec"
require "inspec/cli"
Inspec::InspecCLI.start(ARGV)
2 changes: 2 additions & 0 deletions lib/fetchers/mock.rb
@@ -1,3 +1,5 @@
require "inspec/fetcher"

module Fetchers
class Mock < Inspec.fetcher(1)
name "mock"
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/backend.rb
Expand Up @@ -2,6 +2,8 @@

require "train"
require "inspec/config"
require "inspec/version"
require "inspec/resource"

module Inspec
module Backend
Expand Down
5 changes: 4 additions & 1 deletion lib/inspec/base_cli.rb
@@ -1,7 +1,8 @@
require "thor"
require "inspec/log"
require "inspec/profile_vendor"
require "inspec/ui"
require "inspec/config"
require "inspec/utils/deprecation/global_method"

# Allow end of options during array type parsing
# https://github.com/erikhuda/thor/issues/631
Expand Down Expand Up @@ -233,6 +234,8 @@ def pretty_handle_exception(exception)
end

def vendor_deps(path, opts)
require "inspec/profile_vendor"

profile_path = path || Dir.pwd
profile_vendor = Inspec::ProfileVendor.new(profile_path)

Expand Down
60 changes: 47 additions & 13 deletions lib/inspec/cli.rb
@@ -1,19 +1,33 @@
# Copyright 2015 Dominik Richter

require "logger"
require "thor"
require "json"
require "pp"
require "inspec/utils/json_log"
require "inspec/utils/latest_version"
require "inspec/base_cli"
require "inspec/plugin/v1"
require "inspec/plugin/v2"
require "inspec/runner_mock"
require "inspec/env_printer"
require "inspec/schema"
require "inspec/config"
require "inspec/utils/deprecation/deprecator"
require "inspec/dist"
require "inspec/backend"
require "inspec/dependencies/cache"

module Inspec # TODO: move this somewhere "better"?
autoload :BaseCLI, "inspec/base_cli"
autoload :Deprecation, "inspec/utils/deprecation"
autoload :Exceptions, "inspec/exceptions"
autoload :Fetcher, "inspec/fetcher"
autoload :Formatters, "inspec/formatters"
autoload :Globals, "inspec/globals"
autoload :Impact, "inspec/impact"
autoload :Impact, "inspec/impact"
autoload :InputRegistry, "inspec/input_registry"
autoload :Profile, "inspec/profile"
autoload :Reporters, "inspec/reporters"
autoload :Resource, "inspec/resource"
autoload :Rule, "inspec/rule"
autoload :Runner, "inspec/runner"
autoload :Runner, "inspec/runner"
autoload :Shell, "inspec/shell"
autoload :SourceReader, "inspec/source_reader"
autoload :Telemetry, "inspec/utils/telemetry"
autoload :V1, "inspec/plugin/v1"
autoload :V2, "inspec/plugin/v2"
autoload :VERSION, "inspec/version"
end

class Inspec::InspecCLI < Inspec::BaseCLI
class_option :log_level, aliases: :l, type: :string,
Expand Down Expand Up @@ -50,6 +64,9 @@ class Inspec::InspecCLI < Inspec::BaseCLI
desc: "A list of controls to include. Ignore all other tests."
profile_options
def json(target)
require "inspec/resources"
require "json"

o = config
diagnose(o)
o["log_location"] = $stderr
Expand Down Expand Up @@ -86,6 +103,8 @@ def json(target)
option :format, type: :string
profile_options
def check(path) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
require "inspec/resources"

o = config
diagnose(o)
o["log_location"] ||= STDERR if o["format"] == "json"
Expand Down Expand Up @@ -142,6 +161,8 @@ def check(path) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
option :overwrite, type: :boolean, default: false,
desc: "Overwrite existing vendored dependencies and lockfile."
def vendor(path = nil)
require "inspec/resources"

o = config
configure_logger(o)
o[:logger] = Logger.new($stdout)
Expand All @@ -163,6 +184,8 @@ def vendor(path = nil)
option :ignore_errors, type: :boolean, default: false,
desc: "Ignore profile warnings."
def archive(path)
require "inspec/resources"

o = config
diagnose(o)

Expand Down Expand Up @@ -347,6 +370,8 @@ def env(shell = nil)

desc "schema NAME", "print the JSON schema", hide: true
def schema(name)
require "inspec/schema"

puts Inspec::Schema.json(name)
rescue StandardError => e
puts e
Expand All @@ -360,8 +385,10 @@ def version
v = { version: Inspec::VERSION }
puts v.to_json
else
require "inspec/utils/latest_version"
puts Inspec::VERSION
# display outdated version
# TODO: remove this. Don't notify of update to a gem when they install omnibus
latest = LatestInSpecVersion.new.latest || Inspec::VERSION
if Gem::Version.new(Inspec::VERSION) < Gem::Version.new(latest)
puts "\nYour version of #{Inspec::Dist::PRODUCT_NAME} is out of date! The latest version is #{latest}."
Expand All @@ -370,6 +397,11 @@ def version
end
map %w{-v --version} => :version

desc "nothing", "does nothing"
def nothing
puts "you did nothing"
end

private

def run_command(opts)
Expand Down Expand Up @@ -423,6 +455,8 @@ def run_command(opts)
#---------------------------------------------------------------------#
# Plugin Loading
#---------------------------------------------------------------------#
require "inspec/plugin/v2"

begin
# Load v2 plugins. Manually check for plugin disablement.
omit_core = ARGV.delete("--disable-core-plugins")
Expand Down
1 change: 1 addition & 0 deletions lib/inspec/fetcher.rb
Expand Up @@ -35,6 +35,7 @@ def self.fetcher(version)
end
end

# TODO: remove. require up, not down.
require "fetchers/local"
require "fetchers/url"
require "fetchers/git"
Expand Down
1 change: 1 addition & 0 deletions lib/inspec/file_provider.rb
@@ -1,4 +1,5 @@
require "rubygems/package"
require "pathname"
require "zlib"
require "zip"

Expand Down
1 change: 1 addition & 0 deletions lib/inspec/input_registry.rb
Expand Up @@ -3,6 +3,7 @@
require "inspec/objects/input"
require "inspec/secrets"
require "inspec/exceptions"
require "inspec/plugin/v2"

module Inspec
# The InputRegistry's responsibilities include:
Expand Down
2 changes: 1 addition & 1 deletion lib/inspec/metadata.rb
Expand Up @@ -4,7 +4,6 @@
require "rubygems/version"
require "rubygems/requirement"
require "semverse"
require "erb"

require "inspec/version"
require "inspec/utils/spdx"
Expand Down Expand Up @@ -200,6 +199,7 @@ def self.finalize(metadata, profile_id, options, logger = nil)
end

def self.from_yaml(ref, content, profile_id, logger = nil)
require "erb"
res = Metadata.new(ref, logger)
res.params = YAML.load(ERB.new(content).result)
res.content = content
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/plugin/v1/plugin_types/resource.rb
@@ -1,3 +1,5 @@
require "inspec/exceptions"

module Inspec
module ResourceBehaviors
def to_s
Expand Down
9 changes: 2 additions & 7 deletions lib/inspec/profile.rb
Expand Up @@ -2,15 +2,10 @@

require "forwardable"
require "openssl"
require "pathname"
require "inspec/input_registry"
require "inspec/polyfill"
require "inspec/cached_fetcher"
require "inspec/file_provider"
require "inspec/cached_fetcher" # TODO: split or rename
require "inspec/source_reader"
require "inspec/metadata"
require "inspec/backend"
require "inspec/rule"
require "inspec/log"
require "inspec/profile_context"
require "inspec/runtime_profile"
require "inspec/method_source"
Expand Down
1 change: 1 addition & 0 deletions lib/inspec/resource.rb
@@ -1,5 +1,6 @@
# copyright: 2015, Vulcano Security GmbH
require "inspec/plugin/v1"
require "inspec/utils/deprecation/global_method" # for resources

module Inspec
class ProfileNotFound < StandardError; end
Expand Down
4 changes: 3 additions & 1 deletion lib/inspec/resources.rb
Expand Up @@ -2,7 +2,9 @@

# Detect if we are running the stripped-down inspec-core
# This relies on AWS being stripped from the inspec-core gem
inspec_core_only = !File.exist?(File.join(File.dirname(__FILE__), "..", "resource_support", "aws.rb"))
inspec_core_only = ENV["NO_AWS"] || !File.exist?(File.join(File.dirname(__FILE__), "..", "resource_support", "aws.rb"))

require "rspec/matchers"

# Do not attempt to load cloud resources if we are in inspec-core mode
unless inspec_core_only
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/resources/command.rb
@@ -1,5 +1,7 @@
# copyright: 2015, Vulcano Security GmbH

require "inspec/resource"

module Inspec::Resources
class Cmd < Inspec.resource(1)
name "command"
Expand Down
1 change: 1 addition & 0 deletions lib/inspec/runner.rb
Expand Up @@ -9,6 +9,7 @@
require "inspec/config"
require "inspec/dependencies/cache"
require "inspec/dist"
require "inspec/resources"
require "inspec/reporters"
require "inspec/runner_rspec"
# spec requirements
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/runner_rspec.rb
@@ -1,6 +1,8 @@
require "rspec/core"
require "rspec/its"
require "inspec/formatters"
require "matchers/matchers"
require "inspec/rspec_extensions"

# There be dragons!! Or borgs, or something...
# This file and all its contents cannot be unit-tested. both test-suits
Expand Down
14 changes: 4 additions & 10 deletions lib/inspec/ui.rb
@@ -1,6 +1,3 @@
require "tty-table"
require "tty-prompt"

module Inspec
# Provides simple terminal UI interaction primitives for CLI commands and plugins.
class UI
Expand Down Expand Up @@ -158,6 +155,8 @@ def list_item(str, opts = { print: true })
# t << ['', '', 1]
# end
def table(opts = { print: true })
require "inspec/ui_table_helper"

the_table = TableHelper.new
yield(the_table)

Expand All @@ -174,13 +173,6 @@ def table(opts = { print: true })
print_or_return(result, opts[:print])
end

class TableHelper < TTY::Table
def header=(ary)
cells = ary.dup.map { |label| { value: label, alignment: :center } }
@header = TTY::Table::Header.new(cells)
end
end

#=========================================================================#
# Exit Codes
#=========================================================================#
Expand Down Expand Up @@ -212,6 +204,8 @@ def prompt
unless interactive?
raise Inspec::UserInteractionRequired, "Somthing is trying to ask the user a question, but interactivity is disabled."
end
require "tty-prompt"

@prompt ||= TTY::Prompt.new
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/inspec/ui_table_helper.rb
@@ -0,0 +1,12 @@
require "tty-table"

module Inspec
class UI
class TableHelper < TTY::Table
def header=(ary)
cells = ary.dup.map { |label| { value: label, alignment: :center } }
@header = TTY::Table::Header.new(cells)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/inspec/utils/pkey_reader.rb
@@ -1,3 +1,5 @@
require "inspec/objects/input"

module PkeyReader
def read_pkey(filecontent, passphrase)
raise_if_unset(passphrase)
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/inspec-habitat/test/unit/profile_test.rb
@@ -1,6 +1,7 @@
require "mixlib/log"
require "fileutils"
require "minitest/autorun"
require "inspec/backend"
require_relative "../../lib/inspec-habitat/profile.rb"

class InspecPlugins::Habitat::ProfileTest < Minitest::Test
Expand Down
2 changes: 2 additions & 0 deletions test/functional/inspec_archive_test.rb
@@ -1,5 +1,7 @@
require "functional/helper"
require "tmpdir"
require "zip"
require "rubygems/package"

describe "inspec archive" do
include FunctionalHelper
Expand Down
1 change: 1 addition & 0 deletions test/functional/inspec_exec_test.rb
Expand Up @@ -217,6 +217,7 @@
let(:out) { inspec("exec " + File.join(profile_path, "aws-profile")) }
let(:stdout) { out.stdout.force_encoding(Encoding::UTF_8) }
it "exits with an error" do
skip if ENV["NO_AWS"]
stdout.must_include "Resource `aws_iam_users` is not supported on platform"
stdout.must_include "Resource `aws_iam_access_keys` is not supported on platform"
stdout.must_include "Resource `aws_s3_bucket` is not supported on platform"
Expand Down

0 comments on commit eb1e643

Please sign in to comment.