Skip to content

Fix/auth status 401 diagnostics - #94

Open
brandonc wants to merge 5 commits into
mainfrom
fix/auth-status-401-diagnostics
Open

Fix/auth status 401 diagnostics#94
brandonc wants to merge 5 commits into
mainfrom
fix/auth-status-401-diagnostics

Conversation

@brandonc

Copy link
Copy Markdown
Collaborator

Description

#86 but with some tweaks by me

Example Output

PR Checklist

  • Run npx changie new or install changie to prepare a new changelog entry for the next set of release notes.
  • Ensure any command changes are sensitive to these global flags:
    • --json — Force machine readable output to stdout. Does not apply to stderr.
    • --markdown — Force markdown output to stdout. Does not apply to stderr.
    • --dry-run — Don't make any actual writes or other mutations. Describe what would have changed to stderr.
    • --quiet — Only render essential content.
  • Get the logging interface from the context and add debug logging for interesting conditions and nonfatal situations.
  • Run make gen/screenshot if the root command output changes.
  • [xx] Add the Autocomplete field to positional arguments and flags to assist shell autocomplete.

PCI review checklist

  • I have documented a clear reason for, and description of, the change I am making.

  • If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.

  • If applicable, I've documented the impact of any changes to security controls.

    Examples of changes to security controls include using new access control methods, adding or removing logging pipelines, etc.

jordanenglish and others added 5 commits July 17, 2026 01:57
…uthorized"

`auth status` collapsed three distinct failures into one opaque
"Unauthorized for <host>" line: no token configured, a token the server
rejected (401), and the request never reaching the server (network/DNS,
or some other HTTP status). Users could not tell an expired token from a
connectivity blip, and the message suggested no remedy.

Classify the /account/details error with errors.As on the go-tfe
*APIError (it arrives wrapped in a *url.Error) and print a cause-specific,
actionable message:

  - no token       -> run `tfctl auth login`
  - rejected (401) -> token expired/revoked (auth login), or an SSO
                      session that has lapsed on SSO-protected Terraform
                      Enterprise (re-authenticate in the browser)
  - other status   -> "<host> returned HTTP N" (not an auth problem)
  - unreachable    -> "could not reach <host>: <err>"

JSON/agent output gains a machine-readable `reason` field so scripts can
branch on the cause.

@Maed223 Maed223 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.

Looking good, just caught a tiny error formatting omission.

case reasonRejected:
fmt.Fprintf(w, "%s Token for %s was invalid (HTTP 401).\n", icon, hostname)
fmt.Fprintf(w, " - The token may be expired, revoked, or disabled: run '%s auth login' to create a new one.\n", version.Name)
if !strings.HasSuffix(opts.Profile.GetHostname(), ".terraform.io") {

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.

nit: Looks like we could just use the passed hostname param, correct me if I'm missing a nuance here.

if !strings.HasSuffix(opts.Profile.GetHostname(), ".terraform.io") {
fmt.Fprintf(w, " - Your Terraform Enterprise SSO session may have expired: sign in again, then retry.\n")
}
fmt.Fprintf(w, " - Ensure you are using the intended token configuration by adding '--debug' to this command")

@Maed223 Maed223 Jul 29, 2026

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.

Missing a newline at the end here, looks possible that lines later appended here would be glued right next to this one.

@Maed223 Maed223 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.

Meant to approve since my review only has nits

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.

3 participants