Skip to content

Add devproxy cert export to write the root certificate to a file - #2091

Open
waldekmastykarz wants to merge 30 commits into
dotnet:mainfrom
waldekmastykarz:cert-export-command
Open

waldekmastykarz wants to merge 30 commits into
dotnet:mainfrom
waldekmastykarz:cert-export-command

Conversation

@waldekmastykarz

@waldekmastykarz waldekmastykarz commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 40324d0d2f908a1c1824dbe9c4d24e4d7d4115ee

Dimension Assessment Rationale
Change confidence 9/10 ↑ Full local suite passes, including end-to-end host tests for review counterexamples.
↑ stderr routing is triggered by the real System.CommandLine parse, not token guessing.
↓ Plugin options before cert export can still put plugin startup logs on stdout (documented, accepted).
↑ All CI checks pass on this head (3 OSes).

Adds devproxy cert export to write the public root certificate to a file, so CI scripts can trust Dev Proxy without a running instance, an API token, or openssl pkcs12.

devproxy cert export [--format pem|crt|der] [--file <path>]
  • Resolves the shared root certificate (creating it on first run, same as cert ensure) and writes only the public certificate — never the private key.
  • pem / crt: PEM-encoded (same content as GET /proxy/rootCertificate?format=crt); der: binary.
  • --format defaults to the --file extension (.crt, .der), otherwise pem.
  • --file defaults to dev-proxy-ca.<format> in the current directory; supports ~/path tokens, creates parent directories, and overwrites existing files.
  • Prints only the absolute path to stdout (CA=$(devproxy cert export)); --output json returns { path, format }.
  • Updates the CI/CD and CLI reference skill docs to use it instead of openssl.

Deviation from the issue: the issue proposes --output <file>, but --output is already the global, recursive text|json output-format option, so this uses --file.

Known limitation: plugin options placed before cert export (e.g. devproxy --mocks-file x cert export) can make plugin startup logs appear on stdout before the result, because plugins initialize before the command is parsed. Plugin options don't affect the export, so omit them; this is noted in the CI/CD skill reference.

Node.js/CI docs on learn.microsoft.com live in the docs repo and will be updated separately.

Resolves #2085

Adds 'devproxy cert export [--format pem|crt|der] [--file <path>]', which
writes the public root certificate (creating it if needed) without a
running instance and prints the written path. Uses --file instead of
--output because --output is the global text/json output-format option.
Updates CI/CD skill docs to use it instead of openssl.

Closes dotnet#2085

Co-authored-by: Copilot App <[email protected]>
@waldekmastykarz
waldekmastykarz requested a review from a team as a code owner November 5, 2026 10:43
Copilot AI balanced review requested due to automatic review settings November 5, 2026 10:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The export can report success for an unpersisted, temporary root certificate that subsequent proxy runs replace, leaving clients unable to trust the proxy using the exported certificate.

3 open issues
What changed in this PR

Adds devproxy cert export to export the public root certificate for CI and local trust setup without a running proxy or API token.

Changes:

  • Supports PEM, CRT, and DER exports, with format inference, configurable file paths, and text or JSON output.
  • Adds tests for format selection, default filenames, and public-certificate encoding.
  • Updates CLI and CI/CD guidance to use the export command instead of OpenSSL.
File Description
skills/​dev-proxy/​references/​configuration.md Adds the export command to the CLI reference.
skills/​dev-proxy/​references/​ci-cd-integration.md Replaces OpenSSL export examples and documents command usage.
DevProxy/​Commands/​CertCommand.cs Implements certificate export options, file writing, and result output.
DevProxy.Tests/​CertCommandTests.cs Tests export formats, filenames, and public-only certificate encoding.

Review effort: Balanced

Comment thread DevProxy/Commands/CertCommand.cs Resolved
Comment thread DevProxy/Commands/CertCommand.cs Resolved
Comment thread DevProxy/Commands/CertCommand.cs Resolved
…export

- Add CertificateAuthority.IsRootPersisted and fail cert export when a new
  root couldn't be saved (it would change on the next run).
- Route all cert export diagnostics to stderr so stdout has only the path.
- Write --output json result directly to stdout, independent of log level.

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Certificate export’s fixed-position command detection misses valid option placements, allowing CA-creation logs to contaminate the path or JSON output on stdout.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Route export logs correctly when options precede the command

DevProxy/​Commands/​DevProxyCommand.cs:130

This fixed-position check misses valid invocations with recursive options, such as devproxy cert --no-color export or devproxy --output json cert export. The export action still runs, but a newly created CA's Saved new root certificate... Information log goes to stdout, contaminating the captured path or JSON result. Detect the invoked command independently of option placement, and apply export's stderr routing before the IsRootCommand logging branch so prefixed options work too.

3 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The export command can overwrite the stored CA certificate and private key with a public-only certificate, forcing root regeneration and invalidating existing trust.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

High severity Prevent certificate export from overwriting the stored CA

DevProxy/​Commands/​CertCommand.cs:178

With an absolute DEV_PROXY_CERT_PATH, devproxy cert export --file "$DEV_PROXY_CERT_PATH" passes the persistence check and then overwrites the stored root certificate and private key with a public-only certificate. The command reports success, but the next run cannot load the file as PKCS#12 and regenerates the root, invalidating existing trust and the exported certificate. Reject destinations that resolve to the CA's storage file before writing.

Medium severity Detect certificate export commands after response-file expansion

DevProxy/​Commands/​DevProxyCommand.cs:132

With cert export in export.rsp, devproxy @export.rsp executes the export through response-file expansion, but this raw-argument scan leaves IsCertExportCommand false. On a fresh installation, the root-certificate save message therefore goes to stdout alongside the result, breaking command substitution and JSON consumers. Detect the command from expanded arguments before configuring logging so response-file invocations also send diagnostics to stderr.

Review effort: Balanced

@waldekmastykarz

Copy link
Copy Markdown
Collaborator Author

Addressing the two previously-missed issues from the last Copilot review:

  • Export overwriting the stored CA: fixed. CertificateAuthority exposes RootCertificatePath, and cert export refuses to write when the destination is the same file. Matching is by normalized path, and by content when both files exist, so symlinked paths like /tmp → /private/tmp on macOS are also caught.
  • Response-file (@file.rsp) detection: won't fix. IsCertExportCommand uses the same raw-argv approach as the existing IsJwtCommand, IsConfigValidateCommand, and IsRootCommand, because logging is configured before System.CommandLine parses (and expands) arguments. Response-file invocations get a cosmetic log line on stdout at worst, the file is still written correctly, and it's not a realistic way to run a single short command. Fixing it properly means changing how every command is detected, which is out of scope here.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because response-file invocation can contaminate certificate-export stdout, while CI examples pin a version that lacks the export command.

No open issues 🎉

Review effort: Balanced

@waldekmastykarz

Copy link
Copy Markdown
Collaborator Author

Re: latest Copilot review.

  • Pinned CI examples: fixed. The pinned-v4.0.0 pipeline examples use openssl again. The "Trust the Root Certificate" section and CLI reference now document devproxy cert export as available in versions later than v4.0.0, with OpenSSL as the fallback.
  • Response files: unchanged. My reasoning is in the previous comment; this is the same limitation all the raw-argv Is*Command helpers have.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because response-file invocation of certificate export can mix creation diagnostics into stdout on fresh installations, breaking command substitution and JSON consumers.

No open issues 🎉

Review effort: Balanced

@waldekmastykarz waldekmastykarz added the pr-enhancement Product features, behavior fixes, and usability or compatibility improvements label Nov 21, 2026
waldekmastykarz and others added 3 commits November 24, 2026 09:55
Resolve CertCommandTests conflict by combining cert export tests with main's cert ensure tests and pass a temp CertificateAuthority to the updated CertCommand constructor.

Co-authored-by: Copilot App <[email protected]>
Preserve certificate export examples using the tuple-based help format.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 14:14
Copilot AI balanced review requested due to automatic review settings November 27, 2026 09:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because command-detection interactions can misclassify a plugin command and suppress diagnostics, hiding conversion errors.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Resolve DevProxyCommand.cs conflict: keep the extracted CreateRootOptions
structure, register the new ValidateCommand, and treat 'validate' as a
built-in command name in cert export detection.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 10:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The command detector mistakes the plugin-option value cert for a command, routing a valid JWT invocation’s result to stderr and leaving stdout empty for scripts.

1 open issue

Review effort: Balanced

Comment thread DevProxy/Commands/DevProxyCommand.cs Resolved
devproxy --mocks-file cert jwt create --scopes export was detected as
cert export, routing JWT output to stderr. Apply the unknown-option
value checks to tokens between cert and export too.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 11:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The command detector mistakes a mocks-file value named cert for the subcommand, causing first-run certificate diagnostics to pollute stdout and break valid export output.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread DevProxy/Commands/DevProxyCommand.cs Resolved
devproxy --mocks-file cert cert export was rejected because the first
cert was taken as the command. When cert follows an unknown option and
the command check fails, retry treating it as the option's value.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 11:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because an unpublished blocking finding shows that mock generation can be mistaken for certificate export, suppressing plugin errors.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@garrytrinder garrytrinder left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functional test results

  • PR: #2091
  • Tested commit: 1247803e21c326f3b4b10dc0c512097037179827
  • Compared base: 61346d968d73f38c94d3bcb7130f22c29f91171a
  • Environment: macOS 27.2.1, .NET SDK 10.0.103, OpenSSL 3.6.5. Source application/plugin builds passed with zero warnings/errors after locked restores. Isolated HOME, CFFIXED_USER_HOME, pre-created Library/Application Support, and unique DEV_PROXY_CERT_PATH; no system trust or proxy changes.

74 assertions passed; two assertions reproduced one PR-attributable diagnostic-suppression defect. This current-head retest supersedes my earlier functional approval. Automated suites were not rerun locally; current-head CI checks were inspected.

Scenario or test command Expected Actual Result
Default PEM/CRT/DER exports; extension inference; explicit format override Correct format, absolute path, matching public-only CA All certificate parses and output contracts matched; no private key exported PASS
Nested paths/spaces, home/app/data tokens, overwrite stale file and independent PFX copy Resolve paths, create parents, safely overwrite destination Expected destinations written; stored CA bytes unchanged PASS
Text/JSON; warning/debug/none logging; recursive options; response files Script-facing stdout contains only requested result Exact path or parseable {path,format} object; diagnostics on stderr PASS
Stored CA direct path, symlink, hard link, directory symlink, case alias Reject overwrite and preserve private key All rejected with exit 1, empty stdout, unchanged PFX PASS
Invalid format, blocked destination, unpersisted fresh root Usage exit 2 or runtime exit 1; actionable diagnostic; no false success Expected exit codes/errors; no ephemeral certificate exported PASS
Fresh and subsequent runs with standard/root/plugin options and response files Persist and reuse CA; clean output All passed, matching exported public certificate and unchanged persisted root PASS
--mocks-file config/cert/jwt/export/status cert export --output json Command-named filenames remain option values All fresh-root and reuse cases passed, including repeated cert PASS
Multiple plugin options, --allowed-errors 429 500, inline --mocks-file=cert, repeated-cert response file Correct export detection and clean JSON Passed with owning plugins configured; fresh/reused CAs remained consistent PASS
JWT after --failure-rate 10, --mocks-file cert/config, inline filename, and response file; JWT name cert, scope export JWT result remains on stdout, not routed as export diagnostics All six interaction cases passed; decoded scope matched export PASS
cert --help, JWT command, standard plugin list Adjacent command registration preserved Expected help, JWT claims, plugin JSON result PASS
Detached launch with --as-system-proxy false --port 0 --api-port 0 --install-cert false --no-first-run Live owned PID and authenticated API PID 44750, proxy port 55030, API port 55031; /proxy returned 200; PID-scoped status passed PASS
HTTPS mock request via assigned proxy with --cacert live.pem; certificate API default/pem/crt Exported CA verifies intercepted TLS and matches API Curl exited 0 without insecure flags; exact mock body; all API certificates matched CLI export PASS
Invalid cert/export HTTP files: --failure-rate 10 mocks from-http-responses cert export --mocks-file out.json Exit 1 and actionable errors identifying invalid files Head exited 1, wrote empty mocks, and printed nothing to either stdout or stderr FAIL
Valid cert/export HTTP files: same command Exit 0, generated mocks, confirmation diagnostic Head generated two correct mocks but suppressed every progress/success message FAIL
Both mock-generation reproductions on captured base in separately built clean checkout Establish whether suppression predates PR Base printed processing/confirmation messages and both invalid-file errors; identical inputs/options PASS
Current-head CI Automated validation passes All 13 checks successful, including macOS/Linux/Windows tests, builds, smoke, CodeQL PASS
Stop owned PID and cleanup Do not affect concurrent testers; preserve source stop --pid 44750 exit 0; process exit verified; owned fixture/logs and baseline worktree removed; source worktree clean PASS

Requested changes

Preserve plugin-command diagnostics when arguments contain cert export

  • Requested change: Do not select certificate-export logging for actual plugin commands such as mocks from-http-responses. Distinguish the plugin command path and its positional arguments from unknown option values, or route export diagnostics without globally disabling plugin logs. Add command-level regression coverage preserving the repeated-cert and JWT fixes.
  • Expected: With invalid input, exit 1 and stderr containing Error processing file .../cert and .../export, including Invalid HTTP response format. With valid input, preserve normal progress and Generated mock responses saved to out.json.
  • Actual: The head returns the same exit codes and writes the same mocks as base, but stdout and stderr are both empty for the prefixed plugin command. Users receive no explanation for failure or confirmation of generation.
  • Reproduction: Configure MockResponsePlugin and GenericRandomErrorPlugin, set validateSchemas:false, installCert:false, noFirstRun:true, and use an owned persisted CA. In a fixture directory, write invalid HTTP response into two files named cert and export. Run devproxy --config-file /absolute/test-config.json --failure-rate 10 mocks from-http-responses cert export --mocks-file out.json. Observe exit 1 and empty stdout/stderr. Remove --failure-rate 10 to see the errors on the same head, or run the identical invocation on base 61346d968d73f38c94d3bcb7130f22c29f91171a, which prints both errors. Replacing the files with HTTP/1.1 200 OK\nContent-Type: application/json\n\n{"fixture":2091}\n reproduces the suppressed success/progress messages.
  • Inline comment: Reproduction on the changed command detector.

Outcome

Request changes. Certificate-export scenarios and CI passed, but the plugin-command diagnostic regression is reproducible on the tested head and absent on the captured base. This assessment is functional testing, not a separate code review. No fixes, commits, pushes, or merges were made; cleanup is complete.

Comment thread DevProxy/Commands/DevProxyCommand.cs Resolved
waldekmastykarz and others added 2 commits November 27, 2026 13:13
Restore CRLF line endings in DevProxyCommand.cs to match main. Pass the
proxy configuration to CreateRootOptions for main's CI-aware install-cert
validator, and use ordinal StartsWith for the new CA1307 rule.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Command detection misinterprets option values and mocks arguments as cert export, causing JWT JSON output to be routed to stderr and mocks completion or error messages to be suppressed.

2 open issues

Review effort: Balanced

Comment thread DevProxy/Commands/DevProxyCommand.cs Resolved
Comment thread DevProxy/Commands/DevProxyCommand.cs Resolved
Plugin options and commands made the token-based cert export detector
misread option values and plugin command arguments as `cert export`,
silencing diagnostics or moving structured results to stderr.

The command now switches console logging to stderr when cert export
actually runs (after System.CommandLine parsing with all plugin options).
The early check only recognizes invocations that use built-in options, so
it can no longer misclassify other commands. Adds unit and end-to-end
regression tests, and makes a WebSocket relay test independent of the
order in which the two relay directions are captured.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 13:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The certificate export change has no active review issues blocking approval.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 27, 2026 14:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the certificate export change.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-enhancement Product features, behavior fixes, and usability or compatibility improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add devproxy cert export to write the root certificate to a file

3 participants