diff --git a/lib/vagrant/util/string_block_editor.rb b/lib/vagrant/util/string_block_editor.rb index c020f8816a0..1d15b8678c7 100644 --- a/lib/vagrant/util/string_block_editor.rb +++ b/lib/vagrant/util/string_block_editor.rb @@ -45,6 +45,16 @@ def keys end end + # This returns the keys (or ids) that are in the string regarding current user only (fix for issue 9070). + # + # @return [] + def cur_user_keys + regexp = /^#\s*VAGRANT-BEGIN:\s*(#{Process.uid}\s.+?)$\r?\n?(.*)$\r?\n?^#\s*VAGRANT-END:\s(\1)$/m + @value.scan(regexp).map do |match| + match[0] + end + end + # This deletes the block with the given key if it exists. def delete(key) key = Regexp.quote(key) diff --git a/plugins/hosts/linux/cap/nfs.rb b/plugins/hosts/linux/cap/nfs.rb index f6845bf2d21..65c05e17cfb 100644 --- a/plugins/hosts/linux/cap/nfs.rb +++ b/plugins/hosts/linux/cap/nfs.rb @@ -117,7 +117,10 @@ def self.nfs_prune(environment, ui, valid_ids) composite_ids = valid_ids.map do |v_id| "#{user} #{v_id}" end - remove_ids = editor.keys - composite_ids + + #Fix for issue 9070, pruning other users' NFS exports + #remove_ids = editor.keys - composite_ids + remove_ids = editor.cur_user_keys - composite_ids logger.debug("Known valid NFS export IDs: #{valid_ids}") logger.debug("Composite valid NFS export IDs with user: #{composite_ids}") diff --git a/test/unit/plugins/hosts/linux/cap/nfs_test.rb b/test/unit/plugins/hosts/linux/cap/nfs_test.rb index 263c671a008..161af345875 100644 --- a/test/unit/plugins/hosts/linux/cap/nfs_test.rb +++ b/test/unit/plugins/hosts/linux/cap/nfs_test.rb @@ -231,6 +231,39 @@ expect(exports_content).to include("/var") expect(exports_content).not_to include("/tmp") end + + it "should remove only own Process.uid entries that are no longer valid" do + invalid_id = SecureRandom.uuid + valid_id = SecureRandom.uuid + other_uid = Process.uid+1 + invalid_id2 = SecureRandom.uuid + valid_id2 = SecureRandom.uuid + content =<<-EOH +# VAGRANT-BEGIN: #{Process.uid} #{invalid_id} +"/tmp" 127.0.0.1(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=) +# VAGRANT-END: #{Process.uid} #{invalid_id} +# VAGRANT-BEGIN: #{Process.uid} #{valid_id} +"/var" 127.0.0.1(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=) +# VAGRANT-END: #{Process.uid} #{valid_id} +# VAGRANT-BEGIN: #{other_uid} #{invalid_id2} +"/data" 127.0.0.2(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=) +# VAGRANT-END: #{other_uid} #{invalid_id2} +# VAGRANT-BEGIN: #{other_uid} #{valid_id2} +"/somedir" 127.0.0.2(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=) +# VAGRANT-END: #{other_uid} #{valid_id2} +EOH + File.write(exports_path, content) + cap.nfs_prune(env, ui, [valid_id]) + exports_content = File.read(exports_path) + expect(exports_content).not_to include("#{Process.uid} #{invalid_id}") + expect(exports_content).to include("#{Process.uid} #{valid_id}") + expect(exports_content).to include("#{other_uid} #{valid_id2}") + expect(exports_content).to include("#{other_uid} #{invalid_id2}") + expect(exports_content).not_to include("/tmp") + expect(exports_content).to include("/var") + expect(exports_content).to include("/data") + expect(exports_content).to include("/somedir") + end end describe ".nfs_write_exports" do diff --git a/test/unit/vagrant/util/string_block_editor_test.rb b/test/unit/vagrant/util/string_block_editor_test.rb index 0fb9749b0bf..c3b628b1703 100644 --- a/test/unit/vagrant/util/string_block_editor_test.rb +++ b/test/unit/vagrant/util/string_block_editor_test.rb @@ -22,6 +22,26 @@ end end + describe "#cur_user_keys" do + it "should return only keys of current user" do + other_uid = Process.uid+1 + data = <