-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #35270 - Support system image download
* Implement fetch and extract system image * Implement class for file extraction with isoinfo * Add capability for archive extraction * Separate logging and file writing tasks * Add additional API endpoint /tftp/system_image/ Co-Authored-By: Ewoud Kohl van Wijngaarden <[email protected]>
- Loading branch information
1 parent
befb442
commit a4fddf8
Showing
13 changed files
with
276 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Proxy | ||
class ArchiveExtract < Proxy::Util::CommandTask | ||
include Util | ||
|
||
SHELL_COMMAND = 'isoinfo' | ||
|
||
def initialize(image_path, file_in_image, dst_path) | ||
args = [ | ||
which(SHELL_COMMAND), | ||
# Print information from Rock Ridge extensions | ||
'-R', | ||
# Filename to read ISO-9660 image from | ||
'-i', image_path.to_s, | ||
# Extract specified file to stdout | ||
'-x', file_in_image.to_s | ||
] | ||
|
||
super(args, nil, dst_path) | ||
end | ||
|
||
def start | ||
lock = Proxy::FileLock.try_locking(File.join(File.dirname(@output), ".#{File.basename(@output)}.lock")) | ||
if lock.nil? | ||
false | ||
else | ||
super do | ||
Proxy::FileLock.unlock(lock) | ||
File.unlink(lock) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
require 'tftp/tftp_api' | ||
require 'tftp/tftp_system_image_api' | ||
|
||
map "/tftp" do | ||
run Proxy::TFTP::Api | ||
end | ||
|
||
map "/tftp/system_image" do | ||
run Proxy::TFTP::SystemImageApi | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Proxy::TFTP | ||
class SystemImageApi < ::Sinatra::Base | ||
helpers ::Proxy::Helpers | ||
|
||
get "/*" do | ||
file = Pathname.new(params[:splat].first).cleanpath | ||
root = Pathname.new(Proxy::TFTP::Plugin.settings.system_image_root).expand_path.cleanpath | ||
joined_path = File.join(root, file) | ||
log_halt(404, "Not found") unless File.exist?(joined_path) | ||
real_file = Pathname.new(joined_path).realpath | ||
log_halt(403, "Invalid or empty path") unless real_file.fnmatch?("#{root}/**") | ||
log_halt(403, "Directory listing not allowed") if File.directory?(real_file) | ||
log_halt(503, "Not a regular file") unless File.file?(real_file) | ||
send_file real_file | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'test_helper' | ||
require 'tempfile' | ||
require 'tftp/tftp_plugin' | ||
require 'tftp/tftp_system_image_api' | ||
|
||
ENV['RACK_ENV'] = 'test' | ||
|
||
class TftpBootImageApiTest < Test::Unit::TestCase | ||
include Rack::Test::Methods | ||
|
||
def app | ||
Proxy::TFTP::SystemImageApi.new | ||
end | ||
|
||
def setup | ||
@tempdir = Dir.mktmpdir 'tftpsystemimage-test' | ||
@osdir = Dir.mktmpdir nil, @tempdir | ||
@osdir_base = File.basename @osdir | ||
FileUtils.touch "#{@osdir}/valid_file" | ||
FileUtils.ln_s "#{@osdir}/valid_file", "#{@tempdir}/valid_symlink" | ||
FileUtils.ln_s "#{@tempdir}/does_not_exist", "#{@tempdir}/invalid_symlink" | ||
Proxy::TFTP::Plugin.load_test_settings(enable_system_image: true, system_image_root: @tempdir) | ||
end | ||
|
||
def teardown | ||
FileUtils.rm_rf(@tempdir) if @tempdir =~ /tftpsystemimage-test/ | ||
end | ||
|
||
def test_valid_file | ||
result = get "/#{@osdir_base}/valid_file" | ||
assert_equal 200, last_response.status | ||
assert_equal '', result.body | ||
end | ||
|
||
def test_valid_dir | ||
result = get "/#{@osdir_base}/" | ||
assert_equal 403, last_response.status | ||
assert_equal 'Directory listing not allowed', result.body | ||
end | ||
|
||
def test_valid_symlink | ||
result = get "/valid_symlink" | ||
assert_equal 200, last_response.status | ||
assert_equal '', result.body | ||
end | ||
|
||
def test_invalid_symlink | ||
result = get "/invalid_symlink" | ||
assert_equal 404, last_response.status | ||
assert_equal 'Not found', result.body | ||
end | ||
|
||
def test_empty_path | ||
result = get "/" | ||
assert_equal 403, last_response.status | ||
assert_equal 'Invalid or empty path', result.body | ||
end | ||
|
||
def test_dangerous_symlink | ||
another_dir = Dir.mktmpdir 'tftpsystemimage-test2' | ||
FileUtils.touch "#{another_dir}/secure_file" | ||
FileUtils.ln_s "#{another_dir}/secure_file", "#{@tempdir}/dangerous_symlink" | ||
result = get "/dangerous_symlink" | ||
assert_equal 403, last_response.status | ||
assert_equal 'Invalid or empty path', result.body | ||
ensure | ||
FileUtils.rm_rf(another_dir) if another_dir =~ /tftpimageboot-test/ | ||
end | ||
end |