diff --git a/lib/inspec/resources/package.rb b/lib/inspec/resources/package.rb index fd5b80288..68e4060e1 100644 --- a/lib/inspec/resources/package.rb +++ b/lib/inspec/resources/package.rb @@ -46,6 +46,8 @@ module Inspec::Resources @pkgman = HpuxPkg.new(inspec) elsif ["alpine"].include?(os[:name]) @pkgman = AlpinePkg.new(inspec) + elsif ["freebsd"].include?(os[:name]) + @pkgman = FreebsdPkg.new(inspec) else raise Inspec::Exceptions::ResourceSkipped, "The `package` resource is not supported on your OS yet." end @@ -274,6 +276,26 @@ module Inspec::Resources end end + class FreebsdPkg < PkgManagement + def info(package_name) + cmd = inspec.command("pkg info #{package_name}") + return {} if cmd.exit_status.to_i != 0 + + params = SimpleConfig.new( + cmd.stdout.chomp, + assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/, + multiple_values: false + ).params + + { + name: params["Name"], + installed: true, + version: params["Version"], + type: "pkg", + } + end + end + # Determines the installed packages on Windows using the Windows package registry entries. # @see: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/15/use-powershell-to-find-installed-software.aspx class WindowsPkg < PkgManagement diff --git a/test/fixtures/cmd/pkg-info-vim-console b/test/fixtures/cmd/pkg-info-vim-console new file mode 100644 index 000000000..b473c7f3e --- /dev/null +++ b/test/fixtures/cmd/pkg-info-vim-console @@ -0,0 +1,32 @@ +vim-console-8.1.1954 +Name : vim-console +Version : 8.1.1954 +Installed on : Mon Dec 16 05:18:03 2019 PST +Origin : editors/vim-console +Architecture : FreeBSD:11:amd64 +Prefix : /usr/local +Categories : editors +Licenses : VIM +Maintainer : adamw@FreeBSD.org +WWW : http://www.vim.org/ +Comment : Improved version of the vi editor (console only) +Options : + DEFAULT_VIMRC : on +Annotations : + FreeBSD_version: 1103000 + cpe : cpe:2.3:a:vim:vim:8.1:::::freebsd11:x64 + repo_type : binary + repository : FreeBSD +Flat size : 25.0MiB +Description : +Vim is a highly configurable text editor built to enable efficient text editing. +It is an improved version of the vi editor distributed with most UNIX systems. + +Vim is often called a "programmer's editor," and so useful for programming that +many consider it an entire IDE. It's not just for programmers, though. Vim is +perfect for all kinds of text editing, from composing email to editing +configuration files. + +WWW: http://www.vim.org/ +WWW: https://github.com/vim/vim + diff --git a/test/helpers/mock_loader.rb b/test/helpers/mock_loader.rb index 0b4df6e25..7f89484a1 100644 --- a/test/helpers/mock_loader.rb +++ b/test/helpers/mock_loader.rb @@ -13,8 +13,9 @@ class MockLoader debian7: { name: "debian", family: "debian", release: "7", arch: "x86_64" }, debian8: { name: "debian", family: "debian", release: "8", arch: "x86_64" }, debian10: { name: "debian", family: "debian", release: "buster/sid", arch: "x86_64" }, - freebsd9: { name: "freebsd", family: "freebsd", release: "9", arch: "amd64" }, freebsd10: { name: "freebsd", family: "freebsd", release: "10", arch: "amd64" }, + freebsd11: { name: "freebsd", family: "freebsd", release: "11", arch: "amd64" }, + freebsd12: { name: "freebsd", family: "freebsd", release: "12", arch: "amd64" }, osx104: { name: "mac_os_x", family: "darwin", release: "10.10.4", arch: nil }, ubuntu1204: { name: "ubuntu", family: "debian", release: "12.04", arch: "x86_64" }, ubuntu1404: { name: "ubuntu", family: "debian", release: "14.04", arch: "x86_64" }, @@ -275,7 +276,7 @@ class MockLoader "/path/to/systemctl show --no-pager --all dbus" => cmd.call("systemctl-show-all-dbus"), # services on macos "launchctl list" => cmd.call("launchctl-list"), - # services on freebsd 10 + # services on freebsd 11 "service -e" => cmd.call("service-e"), "service sendmail onestatus" => cmd.call("service-sendmail-onestatus"), # services for system 5 e.g. centos6, debian 6 @@ -353,6 +354,8 @@ class MockLoader "dpkg-query -W -f='${db:Status-Abbrev} ${Package} ${Version} ${Architecture}\\n'" => cmd.call("dpkg-query-W"), # rpm query all packages "rpm -qa --queryformat '%{NAME} %{VERSION}-%{RELEASE} %{ARCH}\\n'" => cmd.call("rpm-qa-queryformat"), + # pkg query all packages + "pkg info vim-console" => cmd.call("pkg-info-vim-console"), # port netstat on solaris 10 & 11 "netstat -an -f inet -f inet6" => cmd.call("s11-netstat-an-finet-finet6"), # xinetd configuration diff --git a/test/unit/resources/package_test.rb b/test/unit/resources/package_test.rb index b6c6c410f..913de379c 100644 --- a/test/unit/resources/package_test.rb +++ b/test/unit/resources/package_test.rb @@ -158,6 +158,15 @@ describe "Inspec::Resources::Package" do _(resource.info).must_equal pkg end + # freebsd + it "can parse FreeBSD packages" do + resource = MockLoader.new(:freebsd11).load_resource("package", "vim-console") + pkg = { name: "vim-console", installed: true, version: "8.1.1954", type: "pkg" } + _(resource.installed?).must_equal true + _(resource.version).must_equal "8.1.1954" + _(resource.info).must_equal pkg + end + # undefined it "verify package handling on unsupported os" do resource = MockLoader.new(:undefined).load_resource("package", "curl")