diff --git a/.kitchen.yml b/.kitchen.yml index 77358c1e..17cadf86 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -85,6 +85,7 @@ suites: - dock-appearance - show-all-files - updates-disabled + - plist-creation - name: power-management provisioner: diff --git a/CHANGELOG.md b/CHANGELOG.md index 9047311d..bc505a40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. ## [2.9.0] - 2018-12-06 ### Added - Added templates for bug reports, feature requests, and pull requests to adhere to adhere with Github's [recommended community standards](https://opensource.guide). +- Adds support for owner/group in the plist resource. Allows for plist files to be created under a specific owner. Defaults to root/wheel for compatablity with earlier versions of the cookbook. ## [2.8.1] - 2018-11-29 ### Fixed diff --git a/documentation/resource_plist.md b/documentation/resource_plist.md index c5b84073..92a512ea 100644 --- a/documentation/resource_plist.md +++ b/documentation/resource_plist.md @@ -31,6 +31,8 @@ plist 'description' do value TrueClass, FalseClass, String, Integer, Float action Symbol # defaults to :set if not specified encoding String # defaults to 'binary' if not specified. + owner String # defaults to 'root' if not specified. + group String # defaults to 'wheel' if not specified. end ``` diff --git a/resources/plist.rb b/resources/plist.rb index c2b5944a..f4a851c6 100644 --- a/resources/plist.rb +++ b/resources/plist.rb @@ -4,6 +4,9 @@ property :entry, String, desired_state: true property :value, [TrueClass, FalseClass, String, Integer, Float], desired_state: true property :encoding, String, desired_state: true, default: 'binary' +property :owner, String, desired_state: true, default: 'root' +property :group, String, desired_state: true, default: 'wheel' +property :mode, [String, Integer] load_current_value do |desired| current_value_does_not_exist! unless ::File.exist? desired.path @@ -14,20 +17,23 @@ file_type_cmd = shell_out '/usr/bin/file', '--brief', '--mime-encoding', '--preserve-date', desired.path encoding file_type_cmd.stdout.chomp + + file_owner_cmd = shell_out('/usr/bin/stat', '-f', '%Su', desired.path) + owner file_owner_cmd.stdout.chomp + + file_group_cmd = shell_out('/usr/bin/stat', '-f', '%Sg', desired.path) + group file_group_cmd.stdout.chomp end action :set do converge_if_changed :path do converge_by "create new plist: '#{new_resource.path}'" do file new_resource.path do - content <<-EOF - - - - - - -EOF + empty_plist = {}.to_plist + content empty_plist + owner new_resource.owner + group new_resource.group + mode new_resource.mode if property_is_set?(:mode) end end end @@ -60,4 +66,20 @@ end end end + + converge_if_changed :owner do + converge_by "update owner to #{new_resource.owner}" do + file new_resource.path do + owner new_resource.owner + end + end + end + + converge_if_changed :group do + converge_by "update group to #{new_resource.group}" do + file new_resource.path do + group new_resource.group + end + end + end end diff --git a/test/cookbooks/macos_test/recipes/preferences.rb b/test/cookbooks/macos_test/recipes/preferences.rb index 66379db0..5c0635f2 100644 --- a/test/cookbooks/macos_test/recipes/preferences.rb +++ b/test/cookbooks/macos_test/recipes/preferences.rb @@ -1,17 +1,45 @@ +dock_plist = '/Users/vagrant/Library/Preferences/com.apple.dock.plist' +macoscookbook_plist = '/Users/vagrant/com.microsoft.macoscookbook.plist' + plist 'show hidden files' do path '/Users/vagrant/Library/Preferences/com.apple.finder.plist' entry 'AppleShowAllFiles' value true + owner 'vagrant' + group 'staff' end plist 'put the Dock on the left side' do - path '/Users/vagrant/Library/Preferences/com.apple.dock.plist' + path dock_plist entry 'orientation' value 'left' + owner 'vagrant' + group 'staff' end plist 'disable window animations and Get Info animations' do - path '/Users/vagrant/Library/Preferences/com.apple.dock.plist' + path dock_plist entry 'DisableAllAnimations' value true + owner 'vagrant' + group 'staff' +end + +plist 'create a plist that does not exist to test plist creation' do + path macoscookbook_plist + entry 'PokeballEatenByDog' + value true + owner 'vagrant' + group 'staff' + encoding 'us-ascii' + mode '0600' +end + +plist 'add another value to the new plist' do + path macoscookbook_plist + entry 'CaughtEmAll' + value false + owner 'vagrant' + group 'staff' + encoding 'us-ascii' end diff --git a/test/integration/default/controls/dock_test.rb b/test/integration/default/controls/dock_test.rb index 8514766c..e8501e7a 100644 --- a/test/integration/default/controls/dock_test.rb +++ b/test/integration/default/controls/dock_test.rb @@ -13,4 +13,9 @@ describe command("/usr/libexec/PlistBuddy -c 'Print :DisableAllAnimations' #{user_home}/Library/Preferences/com.apple.dock.plist") do its('stdout') { should match 'true' } end + + describe file("#{user_home}/Library/Preferences/com.apple.dock.plist") do + its('owner') { should eq 'vagrant' } + its('group') { should eq 'staff' } + end end diff --git a/test/integration/default/controls/general_test.rb b/test/integration/default/controls/general_test.rb index 7aceec4c..295c6ef4 100644 --- a/test/integration/default/controls/general_test.rb +++ b/test/integration/default/controls/general_test.rb @@ -10,4 +10,31 @@ describe command("/usr/libexec/PlistBuddy -c 'Print :AppleShowAllFiles' #{finder_plist}") do its('stdout') { should match 'true' } end + + describe file(finder_plist) do + its('owner') { should eq 'vagrant' } + its('group') { should eq 'staff' } + its('mode') { should cmp '0600' } + end +end + +control 'plist-creation' do + title 'arbitrary plist creation' + desc 'creation and modification of a property list' + + macos_cookbook_plist = '/Users/vagrant/com.microsoft.macoscookbook.plist' + + describe command("/usr/libexec/PlistBuddy -c 'Print :PokeballEatenByDog' #{macos_cookbook_plist}") do + its('stdout') { should match 'true' } + end + + describe command("/usr/libexec/PlistBuddy -c 'Print :CaughtEmAll' #{macos_cookbook_plist}") do + its('stdout') { should match 'false' } + end + + describe file(macos_cookbook_plist) do + its('owner') { should eq 'vagrant' } + its('group') { should eq 'staff' } + its('mode') { should cmp '0600' } + end end