Sha256: ae5e9bcf5e1fccf90543faaadac982626c975e5799a390a0b5fbad8e07130d36

Contents?: true

Size: 1.8 KB

Versions: 25

Compression:

Stored size: 1.8 KB

Contents

# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
# author: Christoph Hartmann
# author: Dominik Richter

# Usage:
#
# describe os_env('PATH') do
#   its('split') { should_not include('') }
#   its('split') { should_not include('.') }
# end

require 'utils/simpleconfig'

module Inspec::Resources
  class OsEnv < Inspec.resource(1)
    name 'os_env'
    desc 'Use the os_env InSpec audit resource to test the environment variables for the platform on which the system is running.'
    example "
      describe os_env('VARIABLE') do
        its('matcher') { should eq 1 }
      end
    "

    attr_reader :content
    def initialize(env = nil)
      @osenv = env
    end

    def split
      # we can't take advantage of `File::PATH_SEPARATOR` as code is
      # evaluated on the host machine
      path_separator = inspec.os.windows? ? ';' : ':'
      # -1 is required to catch cases like dir1::dir2:
      # where we have a trailing :
      content.nil? ? [] : content.split(path_separator, -1)
    end

    def content
      return @content if defined?(@content)
      @content = value_for(@osenv) unless @osenv.nil?
    end

    def to_s
      if @osenv.nil?
        'Environment variables'
      else
        "Environment variable #{@osenv}"
      end
    end

    private

    def value_for(env)
      command = if inspec.os.windows?
                  "${Env:#{env}}"
                else
                  'env'
                end

      out = inspec.command(command)

      unless out.exit_status == 0
        skip_resource "Can't read environment variables on #{inspec.os.name}. "\
          "Tried `#{command}` which returned #{out.exit_status}"
      end

      if inspec.os.windows?
        out.stdout.strip
      else
        params = SimpleConfig.new(out.stdout).params
        params[env]
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
inspec-1.51.25 lib/resources/os_env.rb
inspec-1.51.21 lib/resources/os_env.rb
inspec-1.51.18 lib/resources/os_env.rb
inspec-1.51.6 lib/resources/os_env.rb
inspec-1.51.0 lib/resources/os_env.rb
inspec-1.50.1 lib/resources/os_env.rb
inspec-1.49.2 lib/resources/os_env.rb
inspec-1.48.0 lib/resources/os_env.rb
inspec-1.47.0 lib/resources/os_env.rb
inspec-1.46.2 lib/resources/os_env.rb
inspec-1.45.13 lib/resources/os_env.rb
inspec-1.45.9 lib/resources/os_env.rb
inspec-1.44.8 lib/resources/os_env.rb
inspec-1.43.8 lib/resources/os_env.rb
inspec-1.43.5 lib/resources/os_env.rb
inspec-1.42.3 lib/resources/os_env.rb
inspec-1.41.0 lib/resources/os_env.rb
inspec-1.40.0 lib/resources/os_env.rb
inspec-1.39.1 lib/resources/os_env.rb
inspec-1.38.8 lib/resources/os_env.rb