From f259e5706fd354b1bf6b54ea0a8705a5eaff48bf Mon Sep 17 00:00:00 2001 From: Patrick Ellis <319655+pje@users.noreply.github.com> Date: Thu, 16 Sep 2021 11:00:27 -0400 Subject: [PATCH] Ephemeral runner deletes local .runner,.credentials files after completion (#1344) Closes #1337 --- .../Configuration/ConfigurationManager.cs | 59 +++++++++++-------- src/Runner.Listener/Runner.cs | 8 ++- .../Configuration/ConfigurationManagerL0.cs | 4 +- src/Test/L0/Listener/RunnerL0.cs | 6 ++ 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/Runner.Listener/Configuration/ConfigurationManager.cs b/src/Runner.Listener/Configuration/ConfigurationManager.cs index 5787392460a..73c93cececc 100644 --- a/src/Runner.Listener/Configuration/ConfigurationManager.cs +++ b/src/Runner.Listener/Configuration/ConfigurationManager.cs @@ -22,6 +22,7 @@ public interface IConfigurationManager : IRunnerService bool IsConfigured(); Task ConfigureAsync(CommandSettings command); Task UnconfigureAsync(CommandSettings command); + void DeleteLocalRunnerConfig(); RunnerSettings LoadSettings(); } @@ -329,6 +330,38 @@ public async Task ConfigureAsync(CommandSettings command) #endif } + // Delete .runner and .credentials files + public void DeleteLocalRunnerConfig() + { + bool isConfigured = _store.IsConfigured(); + bool hasCredentials = _store.HasCredentials(); + //delete credential config files + var currentAction = "Removing .credentials"; + if (hasCredentials) + { + _store.DeleteCredential(); + var keyManager = HostContext.GetService(); + keyManager.DeleteKey(); + _term.WriteSuccessMessage("Removed .credentials"); + } + else + { + _term.WriteLine("Does not exist. Skipping " + currentAction); + } + + //delete settings config file + currentAction = "Removing .runner"; + if (isConfigured) + { + _store.DeleteSettings(); + _term.WriteSuccessMessage("Removed .runner"); + } + else + { + _term.WriteLine("Does not exist. Skipping " + currentAction); + } + } + public async Task UnconfigureAsync(CommandSettings command) { string currentAction = string.Empty; @@ -402,31 +435,7 @@ public async Task UnconfigureAsync(CommandSettings command) _term.WriteLine("Cannot connect to server, because config files are missing. Skipping removing runner from the server."); } - //delete credential config files - currentAction = "Removing .credentials"; - if (hasCredentials) - { - _store.DeleteCredential(); - var keyManager = HostContext.GetService(); - keyManager.DeleteKey(); - _term.WriteSuccessMessage("Removed .credentials"); - } - else - { - _term.WriteLine("Does not exist. Skipping " + currentAction); - } - - //delete settings config file - currentAction = "Removing .runner"; - if (isConfigured) - { - _store.DeleteSettings(); - _term.WriteSuccessMessage("Removed .runner"); - } - else - { - _term.WriteLine("Does not exist. Skipping " + currentAction); - } + DeleteLocalRunnerConfig(); } catch (Exception) { diff --git a/src/Runner.Listener/Runner.cs b/src/Runner.Listener/Runner.cs index f320f70b276..8f55cd34896 100644 --- a/src/Runner.Listener/Runner.cs +++ b/src/Runner.Listener/Runner.cs @@ -214,7 +214,7 @@ public async Task ExecuteCommand(CommandSettings command) var startupTypeAsString = command.GetStartupType(); if (string.IsNullOrEmpty(startupTypeAsString) && configuredAsService) { - // We need try our best to make the startup type accurate + // We need try our best to make the startup type accurate // The problem is coming from runner autoupgrade, which result an old version service host binary but a newer version runner binary // At that time the servicehost won't pass --startuptype to Runner.Listener while the runner is actually running as service. // We will guess the startup type only when the runner is configured as service and the guess will based on whether STDOUT/STDERR/STDIN been redirect or not @@ -478,6 +478,12 @@ private async Task RunAsync(RunnerSettings settings, bool runOnce = false) } messageQueueLoopTokenSource.Dispose(); + + if (settings.Ephemeral) + { + var configManager = HostContext.GetService(); + configManager.DeleteLocalRunnerConfig(); + } } } catch (TaskAgentAccessTokenExpiredException) diff --git a/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs b/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs index 2fff42c4e02..dbc0c05bbf0 100644 --- a/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs +++ b/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs @@ -161,7 +161,8 @@ public async Task CanEnsureConfigure() "--work", _expectedWorkFolder, "--auth", _expectedAuthType, "--token", _expectedToken, - "--labels", userLabels + "--labels", userLabels, + "--ephemeral", }); trace.Info("Constructed."); _store.Setup(x => x.IsConfigured()).Returns(false); @@ -179,6 +180,7 @@ public async Task CanEnsureConfigure() Assert.True(s.AgentName.Equals(_expectedAgentName)); Assert.True(s.PoolId.Equals(_secondRunnerGroupId)); Assert.True(s.WorkFolder.Equals(_expectedWorkFolder)); + Assert.True(s.Ephemeral.Equals(true)); // validate GetAgentPoolsAsync gets called twice with automation pool type _runnerServer.Verify(x => x.GetAgentPoolsAsync(It.IsAny(), It.Is(p => p == TaskAgentPoolType.Automation)), Times.Exactly(2)); diff --git a/src/Test/L0/Listener/RunnerL0.cs b/src/Test/L0/Listener/RunnerL0.cs index bc9fe562b5b..acaff40eb5f 100644 --- a/src/Test/L0/Listener/RunnerL0.cs +++ b/src/Test/L0/Listener/RunnerL0.cs @@ -149,6 +149,9 @@ public async void TestRunAsync() _messageListener.Verify(x => x.CreateSessionAsync(It.IsAny()), Times.Once()); _messageListener.Verify(x => x.DeleteSessionAsync(), Times.Once()); _messageListener.Verify(x => x.DeleteMessageAsync(It.IsAny()), Times.AtLeastOnce()); + + // verify that we didn't try to delete local settings file (since we're not ephemeral) + _configurationManager.Verify(x => x.DeleteLocalRunnerConfig(), Times.Never()); } } } @@ -312,6 +315,9 @@ public async void TestRunOnce() _messageListener.Verify(x => x.CreateSessionAsync(It.IsAny()), Times.Once()); _messageListener.Verify(x => x.DeleteSessionAsync(), Times.Once()); _messageListener.Verify(x => x.DeleteMessageAsync(It.IsAny()), Times.AtLeastOnce()); + + // verify that we did try to delete local settings file (since we're ephemeral) + _configurationManager.Verify(x => x.DeleteLocalRunnerConfig(), Times.Once()); } }