Repository navigation
Restrict Tentacle config file permissions on Windows - #1374
Conversation
New configs are created readable only by SYSTEM, Administrators and the creating user. The agent stops existing configs inheriting their folder's permissions on start, and service --install/--reconfigure --username grants the service account Modify before the service is configured. +semver: major Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
||
| namespace Octopus.Tentacle.Tests.Configuration | ||
| { | ||
| [TestFixture] |
|
|
||
| string directory = null!; | ||
|
|
||
| [SetUp] |
There was a problem hiding this comment.
Removed, and added WindowsConfigurationFileBuilder
| } | ||
|
|
||
| [Test] | ||
| public void WhenAnEmptyConfigIsCreatedThenOnlySystemAdministratorsAndTheCurrentUserCanAccessIt() |
There was a problem hiding this comment.
We have a TentacleInstanceBuilder() can you test at that level?
Also if you test at that level you could run commands as a different user which might make it easier to test non admin users.
There was a problem hiding this comment.
Good call.
I've moved the "config is created restricted" test up to the TentacleInstanceBuilder level in the new WindowsConfigurationFilePermissionsTests (that runs the real "create-instance").
| @@ -35,40 +36,58 @@ | |||
| return; | |||
| } | |||
There was a problem hiding this comment.
Would it make sense to pull this method out here in this PR, its either make a new empty file one way or another.
There was a problem hiding this comment.
Maybe we want to call an interface?
There was a problem hiding this comment.
We have turned this into an interface with a Linux and Windows implementation
| @@ -35,40 +36,58 @@ | |||
| return; | |||
| } | |||
There was a problem hiding this comment.
Maybe we want to call an interface?
| 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}"); | ||
| } |
There was a problem hiding this comment.
I think pull this out like you did with windows.
There was a problem hiding this comment.
Pull out? Like, centralize the warning? or logic? Was this part of the interface split?
|
|
||
| #if NET | ||
| [UnsupportedOSPlatform("windows")] | ||
| static string? TryRestrictToServiceUser(string configurationPath, string userName) |
There was a problem hiding this comment.
Should this be pulled out similar to how windows pulls it out.
I think overall making the linux/windows paths mirror each other makes sense.
There was a problem hiding this comment.
With the split, they mirror each other.
| static readonly SecurityIdentifier LocalSystem = new(WellKnownSidType.LocalSystemSid, null); | ||
| static readonly SecurityIdentifier Administrators = new(WellKnownSidType.BuiltinAdministratorsSid, null); | ||
|
|
||
| public static void CreateEmpty(string configurationPath, string contents) |
There was a problem hiding this comment.
I think the name needs to describe what it is doing here, what is the important thing it is doing here that could be done with a one liner File.Create(path, contents)
There was a problem hiding this comment.
Let's rename to CreateRestrictedToCurrentUser
|
|
||
| // Runs as the account Tentacle runs as, so it can't lock that account out. An ACL that no longer inherits from | ||
| // its folder has already been restricted, by Tentacle or by an administrator, so it is left alone. | ||
| public static void RestrictToOwner(string configurationPath, ISystemLog log) |
There was a problem hiding this comment.
Do we know the service account that runs this tentacle, should we ensure it still has access to this file?
There was a problem hiding this comment.
The agent runs as the service account, so "current user" is the service account and keeps FullControl.
Explicit entries such as the one "service --install --username" adds are also kept, since only inherited entries are dropped.
To help, we renamed to RestrictToCurrentUser and added a comment.
Fixes: #1375
Fixes: LEV-2268
This is the Windows half of #1349. On Windows,
Tentacle.configinherits its folder's ACL. Under the defaultC:\Octopusthat givesAuthenticated Users: Modify, and the certificate and proxy password in it use DPAPILocalMachine, so any local user can read and decrypt them.This is a major version bump (
+semver: major): users other than SYSTEM, Administrators and the service account can no longer read the config.Changes
create-instancecreates the config with an ACL that doesn't inherit from the folder: SYSTEM, Administrators and the creating user get full control. The ACL is set when the file is created, so the file never has the folder's ACL.service --install/--reconfigure --username Xapplies the same restriction and grants X Modify. If X can't be resolved, nothing is changed and a warning is logged. Without--username, the file is left alone, because the service may keep an existing account that relies on the inherited ACL.ServiceCommandnow restricts the config before configuring the service, so--install --username X --startdoesn't start the service before X can read the file. This also moves the Linux chown earlier.Known gaps
Tentacle.exe agentby hand while the service is set to a non-admin account that relies on the inherited ACL, that account loses access. Linux has the same trade-off.LocalMachine.CurrentUserwould stop administrators runningTentacle.execommands against the config.[WindowsTest]tests only run on Windows CI.🤖 Generated with Claude Code