From 602c5e662a855c49faf2102bfe5957154a43db2f Mon Sep 17 00:00:00 2001 From: Luke Butters Date: Tue, 17 Nov 2026 09:41:04 +1100 Subject: [PATCH 1/3] Restrict Tentacle config file permissions and ownership on Linux +semver: major --- .../ConfigurationFilePermissionsFixture.cs | 57 ++++++++++++++ .../Commands/RunAgentCommand.cs | 26 +++++++ .../ConfigurationFilePermissions.cs | 75 +++++++++++++++++++ .../Instances/ApplicationInstanceManager.cs | 22 +++++- .../Startup/ServiceCommand.cs | 23 ++++++ 5 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs create mode 100644 source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs diff --git a/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs b/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs new file mode 100644 index 000000000..5665bf60d --- /dev/null +++ b/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs @@ -0,0 +1,57 @@ +#if NET +using System; +using System.IO; +using System.Runtime.Versioning; +using FluentAssertions; +using NUnit.Framework; +using Octopus.Tentacle.Configuration; + +namespace Octopus.Tentacle.Tests.Configuration +{ + [TestFixture] + [UnsupportedOSPlatform("windows")] + public class ConfigurationFilePermissionsFixture + { + [Test] + public void WhenAWorldReadableConfigIsRestrictedThenOnlyItsOwnerCanReadIt() + { + if (OperatingSystem.IsWindows()) + { + Assert.Ignore("Unix file modes only"); + return; + } + + // Arrange + var configurationFile = Path.GetTempFileName(); + File.SetUnixFileMode(configurationFile, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead); + + // Act + ConfigurationFilePermissions.RestrictToOwner(configurationFile); + + // Assert + File.GetUnixFileMode(configurationFile).Should().Be(UnixFileMode.UserRead | UnixFileMode.UserWrite); + } + + [Test] + public void WhenTheConfigIsASymlinkThenItIsNotGivenToTheServiceUser() + { + if (OperatingSystem.IsWindows()) + { + Assert.Ignore("Unix only"); + return; + } + + // Arrange + var target = Path.GetTempFileName(); + var configurationFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + File.CreateSymbolicLink(configurationFile, target); + + // Act + var error = ConfigurationFilePermissions.TryRestrictToServiceUser(configurationFile, Environment.UserName); + + // Assert + error.Should().Contain("symbolic link"); + } + } +} +#endif diff --git a/source/Octopus.Tentacle/Commands/RunAgentCommand.cs b/source/Octopus.Tentacle/Commands/RunAgentCommand.cs index 0ca205b43..412adb98a 100644 --- a/source/Octopus.Tentacle/Commands/RunAgentCommand.cs +++ b/source/Octopus.Tentacle/Commands/RunAgentCommand.cs @@ -125,6 +125,7 @@ protected override void Start() Environment.SetEnvironmentVariable(EnvironmentVariables.TentacleProxyPort, proxyConfiguration.Value.CustomProxyPort.ToString()); LogWarningIfNotRunningAsAdministrator(); + RestrictConfigurationToOwner(); proxyInitializer.Value.InitializeProxy(); @@ -139,6 +140,31 @@ protected override void Start() Runtime.WaitForUserToExit(); } + void RestrictConfigurationToOwner() + { +#if NET + var configurationPath = selector.Current.ConfigurationPath; + if (OperatingSystem.IsWindows() || string.IsNullOrWhiteSpace(configurationPath) || !File.Exists(configurationPath)) + { + return; + } + + try + { + var previousMode = File.GetUnixFileMode(configurationPath); + if (previousMode != ConfigurationFilePermissions.OwnerOnly) + { + ConfigurationFilePermissions.RestrictToOwner(configurationPath); + log.Info($"Restricted `{configurationPath}` from {Convert.ToString((int)previousMode, 8).PadLeft(4, '0')} to 0600"); + } + } + catch (Exception ex) when (ex is UnauthorizedAccessException or IOException) + { + log.Warn($"Unable to restrict `{configurationPath}` to its owner, so other users may be able to read it: {ex.Message}"); + } +#endif + } + void LogWarningIfNotRunningAsAdministrator() { if (!PlatformDetection.IsRunningOnWindows) return; diff --git a/source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs b/source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs new file mode 100644 index 000000000..164f85aee --- /dev/null +++ b/source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs @@ -0,0 +1,75 @@ +#if NET +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using Microsoft.Win32.SafeHandles; + +namespace Octopus.Tentacle.Configuration +{ + // The config holds protected settings, so only its owner may read it. The service user must therefore own it. + [UnsupportedOSPlatform("windows")] + static class ConfigurationFilePermissions + { + public const UnixFileMode OwnerOnly = UnixFileMode.UserRead | UnixFileMode.UserWrite; + const uint UnchangedGroup = uint.MaxValue; + + public static void RestrictToOwner(string configurationPath) + => File.SetUnixFileMode(configurationPath, OwnerOnly); + + public static string? TryRestrictToServiceUser(string configurationPath, string userName) + { + try + { + if (new FileInfo(configurationPath).LinkTarget != null) + { + return $"`{configurationPath}` is a symbolic link."; + } + + var userId = GetUserId(userName); + if (userId == null) + { + return $"Unable to find the user `{userName}`."; + } + + using var stream = new FileStream(configurationPath, FileMode.Open, FileAccess.Read); + File.SetUnixFileMode(stream.SafeFileHandle, OwnerOnly); + return fchown(stream.SafeFileHandle, userId.Value, UnchangedGroup) == 0 + ? null + : Marshal.GetLastPInvokeErrorMessage(); + } + catch (Exception ex) + { + return ex.Message; + } + } + + static uint? GetUserId(string userName) + { + if (userName.StartsWith("-", StringComparison.Ordinal)) + { + return null; + } + + try + { + var startInfo = new ProcessStartInfo("id") { UseShellExecute = false, RedirectStandardOutput = true }; + startInfo.ArgumentList.Add("-u"); + startInfo.ArgumentList.Add(userName); + using var process = Process.Start(startInfo)!; + var output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return process.ExitCode == 0 && uint.TryParse(output.Trim(), out var userId) ? userId : null; + } + catch (Exception) + { + return null; + } + } + + [DllImport("libc", SetLastError = true)] + static extern int fchown(SafeFileHandle fd, uint owner, uint group); + } +} +#endif diff --git a/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs b/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs index fb777649f..1cf998548 100644 --- a/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs +++ b/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs @@ -76,7 +76,7 @@ void EnsureConfigurationFileExists(string configurationFile, string homeDirector if (!fileSystem.FileExists(configurationFile)) { log.Info("Creating empty configuration file: " + configurationFile); - fileSystem.OverwriteFile(configurationFile, @""); + CreateEmptyConfigurationFile(configurationFile); } else { @@ -84,6 +84,26 @@ void EnsureConfigurationFileExists(string configurationFile, string homeDirector } } + void CreateEmptyConfigurationFile(string configurationFile) + { + const string emptyConfiguration = @""; +#if NET + if (!OperatingSystem.IsWindows()) + { + using var stream = new FileStream(configurationFile, new FileStreamOptions + { + Mode = FileMode.CreateNew, + Access = FileAccess.Write, + UnixCreateMode = ConfigurationFilePermissions.OwnerOnly + }); + using var writer = new StreamWriter(stream); + writer.Write(emptyConfiguration); + return; + } +#endif + fileSystem.OverwriteFile(configurationFile, emptyConfiguration); + } + (string ConfigurationFilePath, string HomeDirectory) ValidateConfigDirectory(string? configurationFile, string? homeDirectory) { // If home directory is not provided, we should try use the config file path if provided, otherwise fallback to cwd diff --git a/source/Octopus.Tentacle/Startup/ServiceCommand.cs b/source/Octopus.Tentacle/Startup/ServiceCommand.cs index b2535f3df..9cc10dec9 100644 --- a/source/Octopus.Tentacle/Startup/ServiceCommand.cs +++ b/source/Octopus.Tentacle/Startup/ServiceCommand.cs @@ -120,7 +120,30 @@ protected override void Start() serviceDescription, serviceConfigurationState); } + + RestrictConfigurationToServiceUser(instanceSelector.Current.ConfigurationPath); + } + } + + void RestrictConfigurationToServiceUser(string? configurationPath) + { +#if NET + var userName = serviceConfigurationState.Username; + if (!(serviceConfigurationState.Install || serviceConfigurationState.Reconfigure) + || OperatingSystem.IsWindows() + || string.IsNullOrWhiteSpace(configurationPath) + || string.IsNullOrWhiteSpace(userName) + || userName == "root") + { + return; + } + + var error = ConfigurationFilePermissions.TryRestrictToServiceUser(configurationPath, userName); + if (error != null) + { + log.Warn($"Unable to restrict `{configurationPath}` to `{userName}`: {error.TrimEnd('.')}. Only the service user should be able to read and write the configuration file, so check its owner and permissions."); } +#endif } } } From 3c5059931d6a08148e1823c98ef1359981a6da2b Mon Sep 17 00:00:00 2001 From: Luke Butters Date: Tue, 17 Nov 2026 09:50:42 +1100 Subject: [PATCH 2/3] Move config permission handling into ConfigurationFilePermissions and test the service user chown --- .../ConfigurationFilePermissionsFixture.cs | 41 ++++++++++ .../ConfigurationFilePermissionsFixture.cs | 42 +++++----- .../Commands/RunAgentCommand.cs | 27 +------ .../ConfigurationFilePermissions.cs | 79 +++++++++++++++++-- .../Instances/ApplicationInstanceManager.cs | 22 +----- .../Startup/ServiceCommand.cs | 26 +----- 6 files changed, 142 insertions(+), 95 deletions(-) create mode 100644 source/Octopus.Tentacle.Tests.Integration/Configuration/ConfigurationFilePermissionsFixture.cs diff --git a/source/Octopus.Tentacle.Tests.Integration/Configuration/ConfigurationFilePermissionsFixture.cs b/source/Octopus.Tentacle.Tests.Integration/Configuration/ConfigurationFilePermissionsFixture.cs new file mode 100644 index 000000000..131e18043 --- /dev/null +++ b/source/Octopus.Tentacle.Tests.Integration/Configuration/ConfigurationFilePermissionsFixture.cs @@ -0,0 +1,41 @@ +#if NET +using System; +using System.IO; +using System.Linq; +using System.Runtime.Versioning; +using System.Threading.Tasks; +using FluentAssertions; +using NUnit.Framework; +using Octopus.Tentacle.CommonTestUtils.Diagnostics; +using Octopus.Tentacle.Configuration; +using Octopus.Tentacle.Tests.Integration.Support.TestAttributes; +using Octopus.Tentacle.Tests.Integration.Util; +using Octopus.Tentacle.Util; + +namespace Octopus.Tentacle.Tests.Integration.Configuration +{ + [TestFixture] + [LinuxTest] + [NonParallelizable] + [UnsupportedOSPlatform("windows")] + public class ConfigurationFilePermissionsFixture + { + [Test] + [RequiresSudoOnLinux] + public async Task WhenRestrictedToTheServiceUserThenOnlyThatUserOwnsAndCanReadTheConfig() + { + var user = new LinuxTestUserPrincipal("octo-config-perm-test"); + var configurationFile = Path.GetTempFileName(); + File.SetUnixFileMode(configurationFile, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead); + var log = new InMemoryLog(); + + ConfigurationFilePermissions.RestrictToServiceUser(configurationFile, user.UserName, log); + + log.GetLog().Should().NotContain("Unable to restrict"); + File.GetUnixFileMode(configurationFile).Should().Be(ConfigurationFilePermissions.OwnerOnly); + var owner = await new CommandLineInvocation("/bin/bash", $"-c \"stat -c '%U' {configurationFile}\"").ExecuteCommandAsync(); + owner.Infos.Single().Should().Be(user.UserName); + } + } +} +#endif diff --git a/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs b/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs index 5665bf60d..6c44a27d8 100644 --- a/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs +++ b/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs @@ -5,6 +5,8 @@ using FluentAssertions; using NUnit.Framework; using Octopus.Tentacle.Configuration; +using Octopus.Tentacle.Tests.Support; +using Octopus.Tentacle.Util; namespace Octopus.Tentacle.Tests.Configuration { @@ -12,45 +14,47 @@ namespace Octopus.Tentacle.Tests.Configuration [UnsupportedOSPlatform("windows")] public class ConfigurationFilePermissionsFixture { - [Test] - public void WhenAWorldReadableConfigIsRestrictedThenOnlyItsOwnerCanReadIt() + [SetUp] + public void SkipOnWindows() { if (OperatingSystem.IsWindows()) { Assert.Ignore("Unix file modes only"); - return; } + } - // Arrange + [Test] + public void WhenAWorldReadableConfigIsRestrictedThenOnlyItsOwnerCanReadIt() + { var configurationFile = Path.GetTempFileName(); File.SetUnixFileMode(configurationFile, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead); - // Act - ConfigurationFilePermissions.RestrictToOwner(configurationFile); + ConfigurationFilePermissions.RestrictToOwner(configurationFile, new InMemoryLog()); - // Assert - File.GetUnixFileMode(configurationFile).Should().Be(UnixFileMode.UserRead | UnixFileMode.UserWrite); + File.GetUnixFileMode(configurationFile).Should().Be(ConfigurationFilePermissions.OwnerOnly); } [Test] - public void WhenTheConfigIsASymlinkThenItIsNotGivenToTheServiceUser() + public void WhenAnEmptyConfigIsCreatedThenOnlyItsOwnerCanReadIt() { - if (OperatingSystem.IsWindows()) - { - Assert.Ignore("Unix only"); - return; - } + var configurationFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + + ConfigurationFilePermissions.CreateEmpty(new OctopusPhysicalFileSystem(new InMemoryLog()), configurationFile, ""); + + File.GetUnixFileMode(configurationFile).Should().Be(ConfigurationFilePermissions.OwnerOnly); + } - // Arrange + [Test] + public void WhenTheConfigIsASymlinkThenItIsNotGivenToTheServiceUser() + { var target = Path.GetTempFileName(); var configurationFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); File.CreateSymbolicLink(configurationFile, target); + var log = new InMemoryLog(); - // Act - var error = ConfigurationFilePermissions.TryRestrictToServiceUser(configurationFile, Environment.UserName); + ConfigurationFilePermissions.RestrictToServiceUser(configurationFile, Environment.UserName, log); - // Assert - error.Should().Contain("symbolic link"); + log.GetLog().Should().Contain("symbolic link"); } } } diff --git a/source/Octopus.Tentacle/Commands/RunAgentCommand.cs b/source/Octopus.Tentacle/Commands/RunAgentCommand.cs index 412adb98a..4947e3c37 100644 --- a/source/Octopus.Tentacle/Commands/RunAgentCommand.cs +++ b/source/Octopus.Tentacle/Commands/RunAgentCommand.cs @@ -125,7 +125,7 @@ protected override void Start() Environment.SetEnvironmentVariable(EnvironmentVariables.TentacleProxyPort, proxyConfiguration.Value.CustomProxyPort.ToString()); LogWarningIfNotRunningAsAdministrator(); - RestrictConfigurationToOwner(); + ConfigurationFilePermissions.RestrictToOwner(selector.Current.ConfigurationPath, log); proxyInitializer.Value.InitializeProxy(); @@ -140,31 +140,6 @@ protected override void Start() Runtime.WaitForUserToExit(); } - void RestrictConfigurationToOwner() - { -#if NET - var configurationPath = selector.Current.ConfigurationPath; - if (OperatingSystem.IsWindows() || string.IsNullOrWhiteSpace(configurationPath) || !File.Exists(configurationPath)) - { - return; - } - - try - { - var previousMode = File.GetUnixFileMode(configurationPath); - if (previousMode != ConfigurationFilePermissions.OwnerOnly) - { - ConfigurationFilePermissions.RestrictToOwner(configurationPath); - log.Info($"Restricted `{configurationPath}` from {Convert.ToString((int)previousMode, 8).PadLeft(4, '0')} to 0600"); - } - } - catch (Exception ex) when (ex is UnauthorizedAccessException or IOException) - { - log.Warn($"Unable to restrict `{configurationPath}` to its owner, so other users may be able to read it: {ex.Message}"); - } -#endif - } - void LogWarningIfNotRunningAsAdministrator() { if (!PlatformDetection.IsRunningOnWindows) return; diff --git a/source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs b/source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs index 164f85aee..1db32e708 100644 --- a/source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs +++ b/source/Octopus.Tentacle/Configuration/ConfigurationFilePermissions.cs @@ -1,24 +1,89 @@ -#if NET using System; -using System.Diagnostics; using System.IO; +using Octopus.Tentacle.Core.Diagnostics; +using Octopus.Tentacle.Util; +#if NET +using System.Diagnostics; using System.Runtime.InteropServices; using System.Runtime.Versioning; using Microsoft.Win32.SafeHandles; +#endif namespace Octopus.Tentacle.Configuration { // The config holds protected settings, so only its owner may read it. The service user must therefore own it. - [UnsupportedOSPlatform("windows")] static class ConfigurationFilePermissions { +#if NET public const UnixFileMode OwnerOnly = UnixFileMode.UserRead | UnixFileMode.UserWrite; const uint UnchangedGroup = uint.MaxValue; +#endif + + public static void CreateEmpty(IOctopusFileSystem fileSystem, string configurationPath, string contents) + { +#if NET + if (!OperatingSystem.IsWindows()) + { + using var stream = new FileStream(configurationPath, new FileStreamOptions + { + Mode = FileMode.CreateNew, + Access = FileAccess.Write, + UnixCreateMode = OwnerOnly + }); + using var writer = new StreamWriter(stream); + writer.Write(contents); + return; + } +#endif + fileSystem.OverwriteFile(configurationPath, contents); + } + + public static void RestrictToOwner(string? configurationPath, ISystemLog log) + { +#if NET + if (OperatingSystem.IsWindows() || string.IsNullOrWhiteSpace(configurationPath) || !File.Exists(configurationPath)) + { + return; + } + + try + { + var previousMode = File.GetUnixFileMode(configurationPath); + if (previousMode != OwnerOnly) + { + File.SetUnixFileMode(configurationPath, OwnerOnly); + log.Info($"Restricted `{configurationPath}` from {Convert.ToString((int)previousMode, 8).PadLeft(4, '0')} to 0600"); + } + } + catch (Exception ex) when (ex is UnauthorizedAccessException or IOException) + { + log.Warn($"Unable to restrict `{configurationPath}` to its owner, so other users may be able to read it: {ex.Message}"); + } +#endif + } - public static void RestrictToOwner(string configurationPath) - => File.SetUnixFileMode(configurationPath, OwnerOnly); + public static void RestrictToServiceUser(string? configurationPath, string? userName, ISystemLog log) + { +#if NET + if (OperatingSystem.IsWindows() + || string.IsNullOrWhiteSpace(configurationPath) + || string.IsNullOrWhiteSpace(userName) + || userName == "root") + { + return; + } + + var error = TryRestrictToServiceUser(configurationPath, userName); + if (error != null) + { + log.Warn($"Unable to restrict `{configurationPath}` to `{userName}`: {error.TrimEnd('.')}. Only the service user should be able to read and write the configuration file, so check its owner and permissions."); + } +#endif + } - public static string? TryRestrictToServiceUser(string configurationPath, string userName) +#if NET + [UnsupportedOSPlatform("windows")] + static string? TryRestrictToServiceUser(string configurationPath, string userName) { try { @@ -70,6 +135,6 @@ public static void RestrictToOwner(string configurationPath) [DllImport("libc", SetLastError = true)] static extern int fchown(SafeFileHandle fd, uint owner, uint group); +#endif } } -#endif diff --git a/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs b/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs index 1cf998548..16b8a5026 100644 --- a/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs +++ b/source/Octopus.Tentacle/Configuration/Instances/ApplicationInstanceManager.cs @@ -76,7 +76,7 @@ void EnsureConfigurationFileExists(string configurationFile, string homeDirector if (!fileSystem.FileExists(configurationFile)) { log.Info("Creating empty configuration file: " + configurationFile); - CreateEmptyConfigurationFile(configurationFile); + ConfigurationFilePermissions.CreateEmpty(fileSystem, configurationFile, @""); } else { @@ -84,26 +84,6 @@ void EnsureConfigurationFileExists(string configurationFile, string homeDirector } } - void CreateEmptyConfigurationFile(string configurationFile) - { - const string emptyConfiguration = @""; -#if NET - if (!OperatingSystem.IsWindows()) - { - using var stream = new FileStream(configurationFile, new FileStreamOptions - { - Mode = FileMode.CreateNew, - Access = FileAccess.Write, - UnixCreateMode = ConfigurationFilePermissions.OwnerOnly - }); - using var writer = new StreamWriter(stream); - writer.Write(emptyConfiguration); - return; - } -#endif - fileSystem.OverwriteFile(configurationFile, emptyConfiguration); - } - (string ConfigurationFilePath, string HomeDirectory) ValidateConfigDirectory(string? configurationFile, string? homeDirectory) { // If home directory is not provided, we should try use the config file path if provided, otherwise fallback to cwd diff --git a/source/Octopus.Tentacle/Startup/ServiceCommand.cs b/source/Octopus.Tentacle/Startup/ServiceCommand.cs index 9cc10dec9..44d5033fa 100644 --- a/source/Octopus.Tentacle/Startup/ServiceCommand.cs +++ b/source/Octopus.Tentacle/Startup/ServiceCommand.cs @@ -121,29 +121,11 @@ protected override void Start() serviceConfigurationState); } - RestrictConfigurationToServiceUser(instanceSelector.Current.ConfigurationPath); - } - } - - void RestrictConfigurationToServiceUser(string? configurationPath) - { -#if NET - var userName = serviceConfigurationState.Username; - if (!(serviceConfigurationState.Install || serviceConfigurationState.Reconfigure) - || OperatingSystem.IsWindows() - || string.IsNullOrWhiteSpace(configurationPath) - || string.IsNullOrWhiteSpace(userName) - || userName == "root") - { - return; - } - - var error = ConfigurationFilePermissions.TryRestrictToServiceUser(configurationPath, userName); - if (error != null) - { - log.Warn($"Unable to restrict `{configurationPath}` to `{userName}`: {error.TrimEnd('.')}. Only the service user should be able to read and write the configuration file, so check its owner and permissions."); + if (serviceConfigurationState.Install || serviceConfigurationState.Reconfigure) + { + ConfigurationFilePermissions.RestrictToServiceUser(instanceSelector.Current.ConfigurationPath, serviceConfigurationState.Username, log); + } } -#endif } } } From abac45ed2f0b71cc9b743f305fa32fc6e4c46a83 Mon Sep 17 00:00:00 2001 From: Luke Butters Date: Tue, 17 Nov 2026 10:03:39 +1100 Subject: [PATCH 3/3] Use a non-root service user in the symlink test so it runs when the agent is root --- .../Configuration/ConfigurationFilePermissionsFixture.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs b/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs index 6c44a27d8..42ffaf598 100644 --- a/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs +++ b/source/Octopus.Tentacle.Tests/Configuration/ConfigurationFilePermissionsFixture.cs @@ -52,7 +52,7 @@ public void WhenTheConfigIsASymlinkThenItIsNotGivenToTheServiceUser() File.CreateSymbolicLink(configurationFile, target); var log = new InMemoryLog(); - ConfigurationFilePermissions.RestrictToServiceUser(configurationFile, Environment.UserName, log); + ConfigurationFilePermissions.RestrictToServiceUser(configurationFile, "nobody", log); log.GetLog().Should().Contain("symbolic link"); }