Sha256: e279e73eb81d8819dcff9f54de6c819e5c96330bccb192378475fec44599784e

Contents?: true

Size: 1.69 KB

Versions: 11

Compression:

Stored size: 1.69 KB

Contents

require "inspec/resources/command"

module Inspec::Resources
  class ZfsDataset < Inspec.resource(1)
    name "zfs_dataset"
    supports platform: "unix"
    desc "
      Use the zfs_dataset InSpec audit resource to test if the named
      ZFS Dataset is present and/or has certain properties.
    "
    example <<~EXAMPLE
      describe zfs_dataset('tank/tmp') do
        its('exec') { should eq('off') }
        its('setuid') { should eq('off') }
      end
    EXAMPLE

    def initialize(zfs_dataset)
      return skip_resource "The `zfs_dataset` resource is not supported on your OS yet." unless inspec.os.bsd? || inspec.os.linux?

      @zfs_dataset = zfs_dataset
      find_zfs = inspec.command("which zfs")
      @zfs_cmd = find_zfs.stdout.strip

      return skip_resource "zfs is not installed" if find_zfs.exit_status != 0

      @params = gather
    end

    # method called by 'it { should exist }'
    def exists?
      inspec.command("#{@zfs_cmd} get -Hp all #{@zfs_dataset}").exit_status == 0
    end

    def mounted?
      return false unless exists?

      inspec.mount(@params["mountpoint"]).mounted?
    end

    def resource_id
      @zfs_dataset || "ZFS Dataset"
    end

    def to_s
      "ZFS Dataset #{@zfs_dataset}"
    end

    def gather
      cmd = inspec.command("#{@zfs_cmd} get -Hp all #{@zfs_dataset}")
      return nil if cmd.exit_status.to_i != 0

      # parse data
      cmd.stdout.chomp.split("\n").each_with_object(Hash.new(0)) do |line, h|
        t = line.split("\t")
        h[t[1].to_s] = t[2].to_s
      end
    end

    # override method
    def exec
      @params["exec"]
    end

    # expose all parameters
    def method_missing(name)
      @params[name.to_s]
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
inspec-core-6.8.11 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.22.58 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.22.55 lib/inspec/resources/zfs_dataset.rb
inspec-core-6.8.1 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.22.40 lib/inspec/resources/zfs_dataset.rb
inspec-core-6.6.0 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.22.36 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.22.29 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.22.3 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.21.29 lib/inspec/resources/zfs_dataset.rb
inspec-core-5.18.14 lib/inspec/resources/zfs_dataset.rb