Skip to content

Commit 1703aee

Browse files
Adds command to ensure the SSL certificate. Closes #1045 (#1047)
1 parent 765368d commit 1703aee

File tree

7 files changed

+101
-6
lines changed

7 files changed

+101
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace DevProxy.CommandHandlers;
6+
7+
public static class CertEnsureCommandHandler
8+
{
9+
public static async Task EnsureCertAsync(ILogger logger)
10+
{
11+
logger.LogTrace("EnsureCertAsync() called");
12+
13+
try
14+
{
15+
logger.LogInformation("Ensuring certificate exists and is trusted...");
16+
await ProxyEngine.ProxyServer.CertificateManager.EnsureRootCertificateAsync();
17+
logger.LogInformation("DONE");
18+
}
19+
catch (Exception ex)
20+
{
21+
logger.LogError(ex, "Error ensuring certificate");
22+
}
23+
24+
logger.LogTrace("EnsureCertAsync() finished");
25+
}
26+
}

dev-proxy/ProxyEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public class ProxyEngine(IProxyConfiguration config, ISet<UrlToWatch> urlsToWatc
2727
private readonly IPluginEvents _pluginEvents = pluginEvents ?? throw new ArgumentNullException(nameof(pluginEvents));
2828
private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger));
2929
private readonly IProxyConfiguration _config = config ?? throw new ArgumentNullException(nameof(config));
30-
private static readonly ProxyServer? _proxyServer;
30+
private static readonly ProxyServer _proxyServer;
31+
internal static ProxyServer ProxyServer => _proxyServer;
3132
private ExplicitProxyEndPoint? _explicitEndPoint;
3233
// lists of URLs to watch, used for intercepting requests
3334
private readonly ISet<UrlToWatch> _urlsToWatch = urlsToWatch ?? throw new ArgumentNullException(nameof(urlsToWatch));

dev-proxy/ProxyHost.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public RootCommand GetRootCommand(ILogger logger)
361361
outdatedCommand.SetHandler(async versionOnly => await OutdatedCommandHandler.CheckVersionAsync(versionOnly, logger), outdatedShortOption);
362362
command.Add(outdatedCommand);
363363

364-
var jwtCommand = new Command("jwt", "Manage JSON Web Tokens ");
364+
var jwtCommand = new Command("jwt", "Manage JSON Web Tokens");
365365
var jwtCreateCommand = new Command("create", "Create a new JWT token");
366366
var jwtNameOption = new Option<string>("--name", "The name of the user to create the token for.");
367367
jwtNameOption.AddAlias("-n");
@@ -465,6 +465,12 @@ public RootCommand GetRootCommand(ILogger logger)
465465

466466
command.Add(jwtCommand);
467467

468+
var certCommand = new Command("cert", "Manage the Dev Proxy certificate");
469+
var certEnsureCommand = new Command("ensure", "Ensure certificates are setup (creates root if required). Also makes root certificate trusted.");
470+
certEnsureCommand.SetHandler(async () => await CertEnsureCommandHandler.EnsureCertAsync(logger));
471+
certCommand.Add(certEnsureCommand);
472+
command.Add(certCommand);
473+
468474
return command;
469475
}
470476

scripts/Dockerfile_local

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
FROM ubuntu:24.04
2+
3+
ARG DEVPROXY_VERSION=0.26.0-beta.1
4+
ARG USERNAME=devproxy
5+
ENV DEVPROXY_VERSION=${DEVPROXY_VERSION}
6+
7+
EXPOSE 8000 8897
8+
9+
LABEL name="dev-proxy/dev-proxy:${DEVPROXY_VERSION}" \
10+
description="Dev Proxy is an API simulator that helps you effortlessly test your app beyond the happy path." \
11+
homepage="https://aka.ms/devproxy" \
12+
maintainers="Waldek Mastykarz <[email protected]>, \
13+
Garry Trinder <[email protected]>" \
14+
org.opencontainers.image.source=https://github.com/dotnet/dev-proxy \
15+
org.opencontainers.image.description="Dev Proxy is an API simulator that helps you effortlessly test your app beyond the happy path." \
16+
org.opencontainers.image.licenses=MIT
17+
18+
WORKDIR /app
19+
20+
COPY ./bld /app/devproxy
21+
22+
RUN apt -y update && apt -y upgrade && \
23+
apt install -y \
24+
curl unzip && \
25+
apt -y clean && \
26+
rm -rf /var/lib/apt/lists/* && \
27+
# Create a new user
28+
useradd -ms /bin/bash ${USERNAME} && \
29+
echo "export PATH=$PATH:$(pwd)/devproxy" >> /home/${USERNAME}/.bashrc && \
30+
# Create a directory for the configuration
31+
mkdir -p /home/${USERNAME}/.config && \
32+
chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.config
33+
34+
# Prevents error "Couldn't find a valid ICU package" when running Dev Proxy
35+
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 \
36+
# Required for .NET to properly resolve SpecialFolder.ApplicationData
37+
XDG_DATA_HOME=/home/${USERNAME}/.config
38+
39+
VOLUME /config
40+
WORKDIR /config
41+
42+
USER ${USERNAME}
43+
44+
RUN /app/devproxy/devproxy cert ensure
45+
46+
ENTRYPOINT ["/app/devproxy/devproxy", "--ip-address", "0.0.0.0"]

scripts/local-build.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
# The .NET Foundation licenses this file to you under the MIT license.
33
# See the LICENSE file in the project root for more information.
44

5-
$versionString = "v0.26.0-beta.1"
5+
. (Join-Path $PSScriptRoot "version.ps1")
66
$version = $versionString.Substring(1)
7+
$platform = "linux-arm64"
78

89
Remove-Item ../bld -Recurse -Force
910

10-
dotnet publish ../dev-proxy/dev-proxy.csproj -c Release -p:PublishSingleFile=true -r win-x64 --self-contained -o ../bld -p:InformationalVersion=$version
11-
dotnet build ../dev-proxy-plugins/dev-proxy-plugins.csproj -c Release -r win-x64 --no-self-contained -p:InformationalVersion=$version
12-
cp -R ../dev-proxy/bin/Release/net9.0/win-x64/plugins ../bld
11+
dotnet publish ../dev-proxy/dev-proxy.csproj -c Release -p:PublishSingleFile=true -r $platform --self-contained -o ../bld -p:InformationalVersion=$version
12+
dotnet build ../dev-proxy-plugins/dev-proxy-plugins.csproj -c Release -r $platform --no-self-contained -p:InformationalVersion=$version
13+
cp -R ../dev-proxy/bin/Release/net9.0/$platform/plugins ../bld
1314
pushd
1415

1516
cd ../bld

scripts/local-docker.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Licensed to the .NET Foundation under one or more agreements.
2+
# The .NET Foundation licenses this file to you under the MIT license.
3+
# See the LICENSE file in the project root for more information.
4+
5+
. (Join-Path $PSScriptRoot "version.ps1")
6+
. (Join-Path $PSScriptRoot "local-build.ps1")
7+
8+
$version = $versionString.Substring(1)
9+
10+
docker build --build-arg DEVPROXY_VERSION=$version -t dev-proxy-local:$version -f Dockerfile_local ..

scripts/version.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Licensed to the .NET Foundation under one or more agreements.
2+
# The .NET Foundation licenses this file to you under the MIT license.
3+
# See the LICENSE file in the project root for more information.
4+
5+
$script:versionString = "v0.26.0-beta.1"

0 commit comments

Comments
 (0)