Sha256: b4afd2cfcb440d9d92814a66c3428ea4111d12460ec6addfd04f7bf8335dc60e

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

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

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

require 'utils/simpleconfig'

class OsEnv < Inspec.resource(1)
  name 'os_env'

  attr_reader :content
  def initialize(env = nil)
    @osenv = env
    @content = nil
    @content = value_for(env) unless env.nil?
  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 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 #{os[:family]}. "\
        "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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
inspec-0.9.5 lib/resources/os_env.rb
inspec-0.9.4 lib/resources/os_env.rb
inspec-0.9.3 lib/resources/os_env.rb