From 8eab7b8543ecf623bd10b288ed01a3b20b12109e Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Fri, 28 Jan 2022 10:02:31 -0800 Subject: [PATCH] Helper/Nix: constructRunCommandLogPath: take RunCommandLog as input This way we ensure that it actually exists in the database, rather than blindly trusting user-generated input. --- src/lib/Hydra/Controller/Root.pm | 3 ++- src/lib/Hydra/Helper/Nix.pm | 7 ++++--- src/lib/Hydra/Plugin/RunCommand.pm | 2 +- t/Hydra/Plugin/RunCommand/basic.t | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib/Hydra/Controller/Root.pm b/src/lib/Hydra/Controller/Root.pm index 2683d6c9..3ce3609b 100644 --- a/src/lib/Hydra/Controller/Root.pm +++ b/src/lib/Hydra/Controller/Root.pm @@ -537,7 +537,8 @@ sub runcommandlog :Local :Args(1) { die if defined $tail && $tail !~ /^[0-9]+$/; - my $logFile = constructRunCommandLogPath($uuid); + my $runlog = $c->model('DB')->resultset('RunCommandLogs')->find({ uuid => $uuid }); + my $logFile = constructRunCommandLogPath($runlog); if (-f $logFile) { serveLogFile($c, $logFile, $tail); return; diff --git a/src/lib/Hydra/Helper/Nix.pm b/src/lib/Hydra/Helper/Nix.pm index d2c4908c..6aa754f7 100644 --- a/src/lib/Hydra/Helper/Nix.pm +++ b/src/lib/Hydra/Helper/Nix.pm @@ -591,12 +591,13 @@ sub isLocalStore { sub constructRunCommandLogPath { - my ($filename) = @_; + my ($runlog) = @_; + my $uuid = $runlog->uuid; my $hydra_path = Hydra::Model::DB::getHydraPath; - my $bucket = substr($filename, 0, 2); + my $bucket = substr($uuid, 0, 2); - return "$hydra_path/runcommand-logs/$bucket/$filename"; + return "$hydra_path/runcommand-logs/$bucket/$uuid"; } 1; diff --git a/src/lib/Hydra/Plugin/RunCommand.pm b/src/lib/Hydra/Plugin/RunCommand.pm index 9c13e4b7..75e6c2c9 100644 --- a/src/lib/Hydra/Plugin/RunCommand.pm +++ b/src/lib/Hydra/Plugin/RunCommand.pm @@ -163,7 +163,7 @@ sub buildFinished { $runlog->started(); - my $logPath = Hydra::Helper::Nix::constructRunCommandLogPath($runlog->uuid); + my $logPath = Hydra::Helper::Nix::constructRunCommandLogPath($runlog); my $dir = dirname($logPath); my $oldUmask = umask(); diff --git a/t/Hydra/Plugin/RunCommand/basic.t b/t/Hydra/Plugin/RunCommand/basic.t index 7148adaa..6e31aed3 100644 --- a/t/Hydra/Plugin/RunCommand/basic.t +++ b/t/Hydra/Plugin/RunCommand/basic.t @@ -60,7 +60,7 @@ subtest "Validate a run log was created" => sub { is($runlog->exit_code, 0, "This command should have succeeded."); subtest "Validate the run log file exists" => sub { - my $logPath = Hydra::Helper::Nix::constructRunCommandLogPath($runlog->uuid); + my $logPath = Hydra::Helper::Nix::constructRunCommandLogPath($runlog); ok(-f $logPath, "The run log was saved to a file."); ok(-z $logPath, "The run log was empty."); };