Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions DevProxy/Commands/CertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sealed class CertCommand : Command
private readonly ILoggerFactory _loggerFactory;
private readonly Option<bool> _forceOption = new("--force", "-f")
{
Description = "Don't prompt for confirmation when removing the certificate"
Description = "Don't prompt for confirmation when removing the certificate. Required for non-interactive use (CI, piped stdin, automation)."
Comment thread
waldekmastykarz marked this conversation as resolved.
};

public CertCommand(ILogger<CertCommand> logger, ILoggerFactory loggerFactory) :
Expand Down Expand Up @@ -74,7 +74,7 @@ private async Task EnsureCertAsync()
_logger.LogTrace("EnsureCertAsync() finished");
}

public void RemoveCert(ParseResult parseResult)
public int RemoveCert(ParseResult parseResult)
{
_logger.LogTrace("RemoveCert() called");

Expand All @@ -83,10 +83,17 @@ public void RemoveCert(ParseResult parseResult)
var isForced = parseResult.GetValue(_forceOption);
if (!isForced)
{
if (Console.IsInputRedirected ||
Environment.GetEnvironmentVariable("CI") is not null)
{
_logger.LogError("Confirmation required but running in non-interactive mode. Use --force to skip confirmation.");
return 1;
}

var isConfirmed = PromptConfirmation("Do you want to remove the root certificate", acceptByDefault: false);
if (!isConfirmed)
{
return;
return 0;
}
}

Expand All @@ -111,10 +118,12 @@ public void RemoveCert(ParseResult parseResult)
}

_logger.LogInformation("DONE");
return 0;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error removing certificate");
return 1;
}
finally
{
Expand Down
16 changes: 14 additions & 2 deletions DevProxy/Proxy/ProxyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,20 @@ private void FirstRunSetup()

Console.WriteLine();
Console.WriteLine("Dev Proxy uses a self-signed certificate to intercept and inspect HTTPS traffic.");
Console.Write("Update the certificate in your Keychain so that it's trusted by your browser? (Y/n): ");
var answer = Console.ReadLine()?.Trim();

string? answer;
if (Console.IsInputRedirected ||
Environment.GetEnvironmentVariable("CI") is not null)
{
// Non-interactive mode, default to trusting the certificate
_logger.LogInformation("Non-interactive mode detected. Defaulting to trusting the certificate.");
answer = "y";
}
else
{
Console.Write("Update the certificate in your Keychain so that it's trusted by your browser? (Y/n): ");
answer = Console.ReadLine()?.Trim();
}

if (string.Equals(answer, "n", StringComparison.OrdinalIgnoreCase))
{
Expand Down
Loading