Skip to content

Commit

Permalink
feat: make the encryption key info mode the default one
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Aug 27, 2021
1 parent aee7863 commit eed02cb
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/AWS.Deploy.CLI/Commands/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ private Command BuildServerModeCommand()
{
serverModeCommand.Add(new Option<int>(new[] { "--port" }, description: "Port the server mode will listen to."));
serverModeCommand.Add(new Option<int>(new[] { "--parent-pid" }, description: "The ID of the process that is launching server mode. Server mode will exit when the parent pid terminates."));
serverModeCommand.Add(new Option<bool>(new[] { "--encryption-keyinfo-stdin" }, description: "If set the cli reads encryption key info from stdin to use for decryption."));
serverModeCommand.Add(new Option<bool>(new[] { "--unsecure-mode" }, description: "If set the cli uses an unsecure mode without encryption."));
serverModeCommand.Add(_optionDiagnosticLogging);
}

Expand All @@ -493,7 +493,7 @@ private Command BuildServerModeCommand()
try
{
_toolInteractiveService.Diagnostics = input.Diagnostics;
var serverMode = new ServerModeCommand(_toolInteractiveService, input.Port, input.ParentPid, input.EncryptionKeyInfoStdIn);
var serverMode = new ServerModeCommand(_toolInteractiveService, input.Port, input.ParentPid, input.UnsecureMode);

await serverMode.ExecuteAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ServerModeCommandHandlerInput
{
public int Port { get; set; }
public int ParentPid { get; set; }
public bool EncryptionKeyInfoStdIn { get; set; }
public bool UnsecureMode { get; set; }
public bool Diagnostics { get; set; }
}
}
22 changes: 11 additions & 11 deletions src/AWS.Deploy.CLI/Commands/ServerModeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class ServerModeCommand
private readonly IToolInteractiveService _interactiveService;
private readonly int _port;
private readonly int? _parentPid;
private readonly bool _encryptionKeyInfoStdIn;
private readonly bool _noEncryptionKeyInfo;

public ServerModeCommand(IToolInteractiveService interactiveService, int port, int? parentPid, bool encryptionKeyInfoStdIn)
public ServerModeCommand(IToolInteractiveService interactiveService, int port, int? parentPid, bool noEncryptionKeyInfo)
{
_interactiveService = interactiveService;
_port = port;
_parentPid = parentPid;
_encryptionKeyInfoStdIn = encryptionKeyInfoStdIn;
_noEncryptionKeyInfo = noEncryptionKeyInfo;
}

public async Task ExecuteAsync(CancellationToken cancellationToken = default(CancellationToken))
Expand Down Expand Up @@ -85,9 +85,13 @@ private async Task ShutDownHost(IWebHost host, CancellationToken cancellationTok
private IEncryptionProvider CreateEncryptionProvider()
{
IEncryptionProvider encryptionProvider;
if (_encryptionKeyInfoStdIn)
if (_noEncryptionKeyInfo)
{
_interactiveService.WriteLine("Waiting on encryption key info from stdin");
encryptionProvider = new NoEncryptionProvider();
}
else
{
_interactiveService.WriteLine("Waiting on symmetric key from stdin");
var input = _interactiveService.ReadLine();
var keyInfo = EncryptionKeyInfo.ParseStdInKeyInfo(input);

Expand All @@ -108,17 +112,13 @@ private IEncryptionProvider CreateEncryptionProvider()
encryptionProvider = new AesEncryptionProvider(aes);
break;
case null:
throw new InvalidEncryptionKeyInfoException("Missing required \"Version\" property in encryption key info");
throw new InvalidEncryptionKeyInfoException("Missing required \"Version\" property in the symmetric key");
default:
throw new InvalidEncryptionKeyInfoException($"Unsupported encryption key info {keyInfo.Version}");
throw new InvalidEncryptionKeyInfoException($"Unsupported symmetric key {keyInfo.Version}");
}

_interactiveService.WriteLine("Encryption provider enabled");
}
else
{
encryptionProvider = new NoEncryptionProvider();
}

return encryptionProvider;
}
Expand Down
21 changes: 14 additions & 7 deletions src/AWS.Deploy.CLI/ServerMode/EncryptionKeyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class EncryptionKeyInfo
public string? Version { get; set; }

/// <summary>
/// Encryption key base 64 encoded
/// Encryption key base 64 encoded
/// </summary>
public string? Key { get; set; }

Expand All @@ -33,15 +33,22 @@ public class EncryptionKeyInfo

public static EncryptionKeyInfo ParseStdInKeyInfo(string input)
{
var json = Encoding.UTF8.GetString(Convert.FromBase64String(input));
var keyInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<EncryptionKeyInfo>(json);
try
{
var json = Encoding.UTF8.GetString(Convert.FromBase64String(input));
var keyInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<EncryptionKeyInfo>(json);

if(string.IsNullOrEmpty(keyInfo.Key))
{
throw new InvalidEncryptionKeyInfoException("The symmetric key is missing a \"Key\" attribute.");
}

if(string.IsNullOrEmpty(keyInfo.Key))
return keyInfo;
}
catch (Exception)
{
throw new InvalidEncryptionKeyInfoException("Encryption key info is missing \"Key\" property.");
throw new InvalidEncryptionKeyInfoException($"The symmetric key has not been passed to Stdin or is invalid.");
}

return keyInfo;
}
}
}
2 changes: 1 addition & 1 deletion src/AWS.Deploy.ServerMode.Client/ServerModeSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public async Task Start(CancellationToken cancellationToken)

var keyInfoStdin = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(keyInfo)));

var command = $"{deployToolRoot} server-mode --port {port} --parent-pid {currentProcessId} --encryption-keyinfo-stdin";
var command = $"{deployToolRoot} server-mode --port {port} --parent-pid {currentProcessId}";
var startServerTask = _commandLineWrapper.Run(command, keyInfoStdin);

_baseUrl = $"http://localhost:{port}";
Expand Down
2 changes: 1 addition & 1 deletion src/AWS.Deploy.ServerMode.ClientGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static async Task Main(string[] args)
{
// Start up the server mode to make the swagger.json file available.
var portNumber = 5678;
var serverCommand = new ServerModeCommand(new ConsoleInteractiveServiceImpl(), portNumber, null, false);
var serverCommand = new ServerModeCommand(new ConsoleInteractiveServiceImpl(), portNumber, null, true);
var cancelSource = new CancellationTokenSource();
_ = serverCommand.ExecuteAsync(cancelSource.Token);
try
Expand Down
6 changes: 3 additions & 3 deletions test/AWS.Deploy.CLI.IntegrationTests/ServerModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task GetRecommendations()
var portNumber = 4000;
using var httpClient = ServerModeHttpClientFactory.ConstructHttpClient(ResolveCredentials);

var serverCommand = new ServerModeCommand(_serviceProvider.GetRequiredService<IToolInteractiveService>(), portNumber, null, false);
var serverCommand = new ServerModeCommand(_serviceProvider.GetRequiredService<IToolInteractiveService>(), portNumber, null, true);
var cancelSource = new CancellationTokenSource();

var serverTask = serverCommand.ExecuteAsync(cancelSource.Token);
Expand Down Expand Up @@ -119,7 +119,7 @@ public async Task GetRecommendationsWithEncryptedCredentials()
await interactiveService.StdInWriter.WriteAsync(keyInfoStdin);
await interactiveService.StdInWriter.FlushAsync();

var serverCommand = new ServerModeCommand(interactiveService, portNumber, null, true);
var serverCommand = new ServerModeCommand(interactiveService, portNumber, null, false);
var cancelSource = new CancellationTokenSource();

var serverTask = serverCommand.ExecuteAsync(cancelSource.Token);
Expand Down Expand Up @@ -160,7 +160,7 @@ public async Task WebFargateDeploymentNoConfigChanges()
var portNumber = 4001;
using var httpClient = ServerModeHttpClientFactory.ConstructHttpClient(ResolveCredentials);

var serverCommand = new ServerModeCommand(_serviceProvider.GetRequiredService<IToolInteractiveService>(), portNumber, null, false);
var serverCommand = new ServerModeCommand(_serviceProvider.GetRequiredService<IToolInteractiveService>(), portNumber, null, true);
var cancelSource = new CancellationTokenSource();

var serverTask = serverCommand.ExecuteAsync(cancelSource.Token);
Expand Down
8 changes: 4 additions & 4 deletions test/AWS.Deploy.CLI.UnitTests/ServerModeAuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async Task AuthMissingEncryptionInfoVersion()
await interactiveService.StdInWriter.WriteAsync(keyInfoStdin);
await interactiveService.StdInWriter.FlushAsync();

var serverCommand = new ServerModeCommand(interactiveService, portNumber, null, true);
var serverCommand = new ServerModeCommand(interactiveService, portNumber, null, false);


var cancelSource = new CancellationTokenSource();
Expand All @@ -230,7 +230,7 @@ public async Task AuthMissingEncryptionInfoVersion()
}

Assert.NotNull(actualException);
Assert.Equal("Missing required \"Version\" property in encryption key info", actualException.Message);
Assert.Equal("Missing required \"Version\" property in the symmetric key", actualException.Message);
}

[Fact]
Expand All @@ -254,7 +254,7 @@ public async Task AuthEncryptionWithInvalidVersion()
await interactiveService.StdInWriter.WriteAsync(keyInfoStdin);
await interactiveService.StdInWriter.FlushAsync();

var serverCommand = new ServerModeCommand(interactiveService, portNumber, null, true);
var serverCommand = new ServerModeCommand(interactiveService, portNumber, null, false);


var cancelSource = new CancellationTokenSource();
Expand All @@ -273,7 +273,7 @@ public async Task AuthEncryptionWithInvalidVersion()
}

Assert.NotNull(actualException);
Assert.Equal("Unsupported encryption key info not-valid", actualException.Message);
Assert.Equal("Unsupported symmetric key not-valid", actualException.Message);
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions test/AWS.Deploy.CLI.UnitTests/ServerModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class ServerModeTests
[Fact]
public async Task TcpPortIsInUseTest()
{
var serverModeCommand1 = new ServerModeCommand(new TestToolInteractiveServiceImpl(), 1234, null, false);
var serverModeCommand2 = new ServerModeCommand(new TestToolInteractiveServiceImpl(), 1234, null, false);
var serverModeCommand1 = new ServerModeCommand(new TestToolInteractiveServiceImpl(), 1234, null, true);
var serverModeCommand2 = new ServerModeCommand(new TestToolInteractiveServiceImpl(), 1234, null, true);

var serverModeTask1 = serverModeCommand1.ExecuteAsync();
var serverModeTask2 = serverModeCommand2.ExecuteAsync();
Expand Down

0 comments on commit eed02cb

Please sign in to comment.