Added support for PAT credentials#359
Open
boeboe wants to merge 1 commit into
Open
Conversation
ecf227e to
67a682e
Compare
Adds a 'token' command group (set/show/path/clear) so users can authenticate mender-cli with a pre-issued PAT instead of running 'mender-cli login'. 'token show' decodes the JWT into a friendly table by default, with --json and --raw for machine/verbatim output. Signed-off-by: bartvanbos <bart.vanbos@octave.energy>
Author
|
@merlin-northern @kjaskiewiczz ... would you mind taking a quick look. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
mender-cli tokencommand group for managing PATsSummary
Adds a new
tokencommand group that lets users persist, inspect, and removethe locally-stored authentication token (typically a Personal Access Token
generated in the Mender UI) without needing to know the platform-specific
storage location.
Today, switching from
mender-cli loginto a long-lived PAT requires eitherpassing
--token-valueon every invocation or knowing exactly where on diskthe auth token cache lives (e.g.
$XDG_CACHE_HOME/mender/authtoken/~/.cache/mender/authtoken) and writing the file by hand with the rightpermissions. This change removes that friction.
Motivation
PATs are the recommended authentication method for CI pipelines, scripted
automation, and headless workstations where running an interactive
mender-cli login(and storing a username/password in.mender-clirc) isundesirable. Providing first-class CLI verbs for the token cache:
op read … | mender-cli token set),(
mender-cli token clear).What's new
A new
tokensubcommand with four verbs:token set0600to the cache.token showtoken show --jsontoken show --rawAuthorization: Bearer …headers and clipboard tools).token pathtoken clear--yes/-y.token sethighlights--tokenflag for overriding the path.managers and CI secrets:
op read "op://Personal/Mender PAT/credential" | mender-cli token setGET /api/management/v1/useradm/users/me. A 401/403 prints a soft warningbut the token is still saved (the user may be offline, on a VPN with split
DNS, etc.). Other errors are also reported as warnings.
expiry hint, e.g.
token expires in 6 days (at 2026-06-09T14:13:26Z).token showhighlightsThe default view groups the decoded JWT into a friendly table with
human-readable claim labels and RFC3339 timestamps:
The signature segment is intentionally not decoded or displayed — the CLI
is a relying party, not the issuer.
Implementation notes
encoding/base64.RawURLEncoding+encoding/json. Signature verificationis deliberately out of scope — the server is the authority on validity, and
token setexercises that via the existinguseradmclient.getDefaultAuthTokenPath(existing) is now shared betweenloginandevery
tokensubcommand.writeAuthTokenhelper centralises the0700 dir+0600 filewrite that previously lived inside
cmd/login.go.login.saveTokennowdelegates to it.
(*useradm.Client).Verify(token)plus a typedVerifyErrordistinguishes auth failures (401/403) from transport/other failures so
token setcan word its warning appropriately.SetTokenCmdaccepts injectedstdin,prompt, andverifiercollaborators so unit tests don't touch the network or a realterminal. The verify call defaults to
useradm.Client.Verifyinproduction.
(
getDefaultAuthTokenPathhonours$XDG_CACHE_HOMEand falls back to~/.cache/mender/authtoken). No keychain integration is introduced.Files
Backwards compatibility
mender-cli logincontinues to work unchanged; its underlying write pathis now shared with
token setbut produces a bit-identical file.--tokenflag is honoured by every new subcommand(
token setwrites to the override path;token showreads from it;token pathdeliberately ignores it and reports the default location).Testing
cmd/token_test.gocover:decodeJWThappy path + malformed/short/non-base64/non-JSON inputs,jwtExpiryover numeric and non-numeric claim values,humanDurationformatting,SetTokenCmd.Runwith piped stdin: verifies0600mode and trimmedcontents written to disk,
SetTokenCmd.Runrejects empty input.CGO_ENABLED=0 go test -tags nopkcs11 ./...eu.hosted.mender.io:printf '%s' "$PAT" | mender-cli token set→ file written0600,expected expiry hint printed, soft warning shown when the server
rejects the validation call.
mender-cli token show,--json,--rawall produce the documentedoutput.
mender-cli token pathprints the cache path.mender-cli token clear --yesremoves the file.Security considerations
token set(masked promptvia
gopass).0600under a0700parent, matching theexisting
loginbehaviour.token showomits the JWT signature segment by design.Documentation
README.md: new "Using a Personal Access Token (PAT)" section between"Configuration file" and "Autocompletion".
CHANGELOG.md: entry under## Unreleased→### Features.Out of scope (not in this PR)
--token-value(still requires the flagor the new
token set).These can land as follow-ups if there's appetite for them.