Class: Compliance::ComplianceCLI

Inherits:
Inspec::BaseCLI show all
Defined in:
lib/bundles/inspec-compliance/cli.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Methods inherited from Inspec::BaseCLI

target_options

Instance Method Details

#exec(*tests) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/bundles/inspec-compliance/cli.rb', line 44

def exec(*tests)
  # iterate over tests and add compliance scheme
  tests = tests.map { |t| 'compliance://' + t }

  # execute profile from inspec exec implementation
  diagnose
  run_tests(tests, opts)
end

#login(server) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/bundles/inspec-compliance/cli.rb', line 16

def (server)
  success, msg = Compliance::API.(server, options['user'], options['password'])
  if success
    puts 'Successfully authenticated'
  else
    puts msg
  end
end

#logoutObject



135
136
137
138
139
140
141
# File 'lib/bundles/inspec-compliance/cli.rb', line 135

def logout
  if Compliance::API.logout
    puts 'Successfully logged out'
  else
    puts 'Could not log out'
  end
end

#profilesObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bundles/inspec-compliance/cli.rb', line 26

def profiles
  profiles = Compliance::API.profiles
  if !profiles.empty?
    # iterate over profiles
    headline('Available profiles:')
    profiles.each { |profile|
      li("#{profile[:org]}/#{profile[:name]}")
    }
  else
    puts 'Could not find any profiles'
  end
end

#upload(path) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bundles/inspec-compliance/cli.rb', line 56

def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity
  o = options.dup
  configure_logger(o)
  # check the profile, we only allow to upload valid profiles
  profile = Inspec::Profile.for_target(path, o)

  # start verification process
  error_count = 0
  error = lambda { |msg|
    error_count += 1
    puts msg
  }

  result = profile.check
  unless result[:summary][:valid]
    error.call('Profile check failed. Please fix the profile before upload.')
  else
    puts('Profile is valid')
  end

  # determine user information
  config = Compliance::Configuration.new
  if config['token'].nil? || config['user'].nil?
    error.call('Please login via `inspec compliance login`')
  end

  # owner
  owner = config['user']
  # read profile name from inspec.yml
  profile_name = profile.params[:name]

  # check that the profile is not uploaded already,
  # confirm upload to the user (overwrite with --force)
  if Compliance::API.exist?("#{owner}/#{profile_name}") && !options['overwrite']
    error.call('Profile exists on the server, use --overwrite')
  end

  # abort if we found an error
  if error_count > 0
    puts "Found #{error_count} error(s)"
    exit 1
  end

  # if it is a directory, tar it to tmp directory
  if File.directory?(path)
    file = Tempfile.new([profile_name, '.tar.gz'])
    archive_path = file.path
    puts "Generate temporary profile archive at #{archive_path}"
    profile.archive({ archive: archive_path, ignore_errors: false, overwrite: true })
  else
    archive_path = path
  end

  puts "Start upload to #{owner}/#{profile_name}"

  # upload the tar to Chef Compliance
  url = "#{config['server']}/owners/#{owner}/compliance/#{profile_name}/tar"

  puts "Uploading to #{url}"
  success, msg = Compliance::API.post_file(url, config['token'], '', archive_path)
  if success
    puts 'Successfully uploaded profile'
  else
    puts 'Error during profile upload:'
    puts msg
  end
end

#versionObject



125
126
127
128
129
130
131
132
# File 'lib/bundles/inspec-compliance/cli.rb', line 125

def version
  info = Compliance::API.version
  if !info.nil? && info['version']
    puts "Chef Compliance version: #{info['version']}"
  else
    puts 'Could not determine server version.'
  end
end