Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NFS exports pruning of other users. Issue 9070. #13112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/vagrant/util/string_block_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<Array<String>]
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)
Expand Down
5 changes: 4 additions & 1 deletion plugins/hosts/linux/cap/nfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
33 changes: 33 additions & 0 deletions test/unit/plugins/hosts/linux/cap/nfs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/unit/vagrant/util/string_block_editor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<DATA
# VAGRANT-BEGIN: #{Process.uid} foo
value
# VAGRANT-END: #{Process.uid} foo
# VAGRANT-BEGIN: #{Process.uid} foo2
value2
# VAGRANT-END: #{Process.uid} foo2
another
# VAGRANT-BEGIN: #{other_uid} bar
content
# VAGRANT-END: #{other_uid} bar
DATA

expect(described_class.new(data).cur_user_keys).to eq(["#{Process.uid} foo", "#{Process.uid} foo2"])
end
end

describe "#delete" do
it "should delete nothing if the key doesn't exist" do
data = "foo"
Expand Down