Skip to content

PMM-15186 Fix for invalid TLS when using change. - #5707

Open
JiriCtvrtka wants to merge 12 commits into
mainfrom
PMM-15186-invalid-tls
Open

PMM-15186 Fix for invalid TLS when using change.#5707
JiriCtvrtka wants to merge 12 commits into
mainfrom
PMM-15186-invalid-tls

Conversation

@JiriCtvrtka

@JiriCtvrtka JiriCtvrtka commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

PMM-15186

Problem

Two separate issues made pmm-admin and pmm-agent fail against a PMM Server over HTTPS with an unhelpful message:

  1. pmm-admin mutated http.DefaultTransport in place when configuring TLS. Because go-openapi hands out the process-wide default transport, the TLS settings (including ServerName and InsecureSkipVerify) leaked into every other HTTP client in the process, and a --server-insecure-tls request could end up applied to, or overwritten by, an unrelated client.
  2. When PMM Server presented a certificate the client could not verify (the shipped certificate is issued for localhost only), the user saw a raw x509/tls error with no indication that --server-insecure-tls exists. Authentication errors were equally misleading: nginx auth_request accepts only 401 and 403, so PMM Server maps several gRPC codes — internal errors included — onto HTTP 401, and the CLI unconditionally reported all of them as "Please check username and password".

Solution

  • admin/commands/base/setup.go: clone the transport before reconfiguring it (defaultTransport.Clone()) and assign the clone back to the runtime, so TLS configuration no longer touches http.DefaultTransport. TLSNextProto is still set to keep HTTP/2 disabled, since it takes precedence over the ForceAttemptHTTP2 that Clone carries over.
  • --server-insecure-tls is now opt-in only and no longer dropped: when PMM Server parameters come from the local pmm-agent, an explicitly passed flag is OR-ed with the agent's ServerInsecureTLS instead of being overwritten by it. A malformed server URL reported by pmm-agent is now reported instead of being silently ignored.
  • New shared utils/servererror package (used by both pmm-admin and pmm-agent, which expose the same flag and talk to PMM Server over the same transport):
    • IsTLSCertificateError / WrapTLSError detect certificate verification failures (tls.CertificateVerificationError plus the bare x509 errors) and append a hint naming the host the certificate was checked against, suggesting --server-insecure-tls or a properly issued certificate. Nothing is added when validation is already disabled.
    • AuthHint distinguishes rejected credentials (gRPC Unauthenticated) from insufficient permissions (gRPC PermissionDenied / HTTP 403) and from server-side failures mapped onto HTTP 401, returning the appropriate hint for each. The gRPC code from the response payload is now carried on commands.Error as GRPCCode (excluded from JSON to keep the documented pmm-admin --json error shape).
  • admin/commands/servererror.go and agent/commands/setup.go render those hints, each owning its own punctuation and separator; the pmm-agent register path keeps its existing --force hint for HTTP 409.

Testing

Unit tests added for all new behaviour: utils/servererror/servererror_test.go, admin/commands/servererror_test.go, admin/cli/cli_test.go, admin/commands/base/setup_test.go (transport isolation and flag precedence) and agent/commands/setup_test.go.

Summary by CodeRabbit

  • Bug Fixes
    • Improved TLS certificate error messages with the affected server hostname and guidance for resolving certificate validation issues.
    • Suppressed unnecessary TLS guidance when certificate verification is intentionally disabled.
    • Added clearer authentication, permission, and server error messages during CLI operations and agent registration.
    • Preserved specific messaging for duplicate registrations and proxy-related errors.
  • Tests
    • Expanded coverage for TLS configuration, authentication failures, registration errors, and server response handling.

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.36364% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.57%. Comparing base (31318c7) to head (8db5a86).
⚠️ Report is 87 commits behind head on main.

Files with missing lines Patch % Lines
admin/commands/base/setup.go 71.42% 4 Missing ⚠️
admin/cli/cli.go 80.00% 1 Missing ⚠️
agent/commands/setup.go 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5707      +/-   ##
==========================================
+ Coverage   43.59%   45.57%   +1.98%     
==========================================
  Files         415      419       +4     
  Lines       43134    43356     +222     
==========================================
+ Hits        18804    19761     +957     
+ Misses      22454    21647     -807     
- Partials     1876     1948      +72     
Flag Coverage Δ
admin 36.08% <83.33%> (+1.29%) ⬆️
agent 51.25% <92.85%> (+2.20%) ⬆️
managed 44.98% <ø> (+2.00%) ⬆️
vmproxy 72.22% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JiriCtvrtka

Copy link
Copy Markdown
Contributor Author

@copilot review

@JiriCtvrtka
JiriCtvrtka marked this pull request as ready for review July 30, 2026 16:12
@JiriCtvrtka
JiriCtvrtka requested a review from a team as a code owner July 30, 2026 16:12
@JiriCtvrtka
JiriCtvrtka requested review from 4nte, ademidoff and maxkondr and removed request for a team July 30, 2026 16:12
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6339d560-c23e-4da4-b579-704d4a57db62

📥 Commits

Reviewing files that changed from the base of the PR and between e2c4cce and 6586737.

📒 Files selected for processing (1)
  • agent/commands/setup.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • agent/commands/setup.go

Walkthrough

The PR adds shared TLS and authentication error handling, preserves gRPC error codes for formatting, updates admin client TLS setup, and integrates the behavior into admin CLI operations and agent registration.

Changes

Server error handling

Layer / File(s) Summary
Shared TLS and authentication utilities
utils/servererror/*
Adds certificate-error detection, TLS guidance, and HTTP/gRPC authentication hints with comprehensive tests.
Command error model and formatting
admin/commands/base.go, admin/commands/servererror.go, admin/commands/*test.go
Adds internal gRPC code extraction and shared server error formatting while preserving the JSON error shape.
Admin client TLS setup
admin/commands/base/*
Validates agent server URLs, preserves TLS settings, configures server names, normalizes paths, and clones transports before modification.
Admin CLI error integration
admin/cli/*
Wraps transport TLS failures and uses shared server error formatting. Tests cover certificate errors and mismatched-certificate updates.
Agent registration error integration
agent/commands/setup*
Formats registration TLS, authentication, conflict, and nginx errors through the shared error utilities.

Estimated code review effort: 4 (Complex) | ~45 minutes

Suggested reviewers: ademidoff, 4nte, maxkondr

Poem

A rabbit checks the TLS gate,
And trims each error’s noisy weight.
Hostnames shine, hints softly start,
Auth codes map with careful art.
The agent hops through setup bright.
“Goodbye,” it says, “to cryptic night!”

Sequence Diagram(s)

sequenceDiagram
  participant AdminCLI
  participant AdminClient
  participant PMMServer
  participant servererror
  AdminCLI->>AdminClient: execute agent update
  AdminClient->>PMMServer: send HTTPS request
  PMMServer-->>AdminClient: certificate or API error
  AdminClient->>servererror: wrap or classify error
  servererror-->>AdminCLI: diagnostic message and guidance
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title identifies the ticket and the invalid TLS issue, but it does not cover the broader transport and error-handling changes.
Description check ✅ Passed The description clearly documents the problem, solution, and testing, and includes the ticket number; only the feature-build field is missing.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch PMM-15186-invalid-tls

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@admin/commands/servererror_test.go`:
- Around line 76-105: Update the gRPC code assignments in the “with gRPC code”
and “internal error mapped to 401” subtests to use the existing named constants
grpcUnauthenticated and grpcInternal from TestServerErrorMessage instead of
inline numeric comments; keep the expected assertions unchanged and avoid inline
comments.

In `@agent/commands/setup.go`:
- Around line 159-160: Update the nginxError detection in the surrounding
error-handling flow to use errors.As so wrapped nginxError values are
recognized. Remove the direct type assertion and its inline nolint directive,
while preserving the existing message update for matching errors.
- Around line 146-155: Update the errors.AsType[*mservice.RegisterNodeDefault]
handling to guard all e.Payload accesses with a nil check. Keep message
assignment, conflict text, and servererror.AuthHint processing inside the guard,
while preserving the existing behavior when Payload is present.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2d31f630-d5f5-4fb3-8f01-82b8ac19f9de

📥 Commits

Reviewing files that changed from the base of the PR and between a50c452 and e2c4cce.

📒 Files selected for processing (11)
  • admin/cli/cli.go
  • admin/cli/cli_test.go
  • admin/commands/base.go
  • admin/commands/base/setup.go
  • admin/commands/base/setup_test.go
  • admin/commands/servererror.go
  • admin/commands/servererror_test.go
  • agent/commands/setup.go
  • agent/commands/setup_test.go
  • utils/servererror/servererror.go
  • utils/servererror/servererror_test.go

Comment thread admin/commands/servererror_test.go
Comment thread agent/commands/setup.go
Comment thread agent/commands/setup.go Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant