Repository navigation
feat: read the --token flag value from standard input with a dash - #773
Conversation
Codecov Reportโ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #773 +/- ##
==========================================
+ Coverage 78.95% 78.99% +0.03%
==========================================
Files 241 242 +1
Lines 18365 18387 +22
==========================================
+ Hits 14501 14524 +23
+ Misses 3864 3863 -1 โ View full report in Codecov by Harness. ๐ New features to boost your workflow:
|
srtaalej
left a comment
There was a problem hiding this comment.
Nice addition! Followed the testing steps and all four behave as described: piped tokens return invalid_auth for both api auth.test and auth login (so no double-read of stdin between the global and login --token flags), empty input errors with missing_input, and a pty on stdin errors immediately with the remediation. go test and make lint pass, and I confirmed with a throwaway test that InitConfig resolves --token - into Config.TokenFlag (with whitespace trimmed).
Holding off on approving for a couple of small things inline, mainly the generated docs.
| // TODO - next semver MAJOR can consider a new shorthand flag, right now -t and -T are used by other commands | ||
| cmd.PersistentFlags().StringVarP(&c.TeamFlag, "team", "w", "", "select workspace or organization by team name or ID") | ||
| cmd.PersistentFlags().StringVarP(&c.TokenFlag, "token", "", "", "set the access token associated with a team") | ||
| cmd.PersistentFlags().StringVarP(&c.TokenFlag, "token", "", "", "set the access token associated with a team\n use \"-\" to read the token from standard input") |
There was a problem hiding this comment.
The --token help text changes here (and in auth login) aren't reflected in docs/reference yet. Running slack docgen ./docs/reference updates 90 command pages (the global flags block on every page, plus the new auth login example). Could we regenerate them in this PR?
| } | ||
|
|
||
| // Read the token from standard input when requested | ||
| token, err := auth.ResolveTokenInput(clients, clients.Config.TokenFlag) |
There was a problem hiding this comment.
The global flag path is the headline use case (op read ... | slack api auth.test --token -), but only ResolveTokenInput and the auth login local flag are tested. Would be good to have a test that InitConfig swaps Config.TokenFlag from - to the stdin value, and returns the missing_input error, so a refactor that moves or drops this call gets caught.
There was a problem hiding this comment.
Added Test_InitConfig in commit a1bf205, great catch! It checks the stdin swap and the missing_input error.
| if clients.IO.IsStdinTTY() { | ||
| return "", slackerror.New(slackerror.ErrMissingInput). | ||
| WithMessage("No token was provided on standard input"). | ||
| WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("auth login --token - < token.txt", false)) |
There was a problem hiding this comment.
nit: this remediation always suggests auth login --token - < token.txt, but it's also shown for the global flag (e.g. slack api auth.test --token - from a terminal), where it points at the wrong command. Maybe something command-agnostic like Pipe a token into the command, e.g. echo $TOKEN | slack <command> --token -, or omit the command example?
There was a problem hiding this comment.
Reworded in commit a1bf205, thanks! The hint is now --token - < token.txt for every command.
WilliamBergamin
left a comment
There was a problem hiding this comment.
๐ @mwbrooks thanks for this! Reusing the - convention from blocks preview and failing fast on a terminal makes keeping tokens out of shell history really easy.
Left a few comments, nothing blocking. The --token - plus --blocks - clash and a test for the global path are the ones I'd look at first ๐
| } | ||
|
|
||
| // Read the token from standard input when requested | ||
| token, err := auth.ResolveTokenInput(clients, clients.Config.TokenFlag) |
There was a problem hiding this comment.
๐งช suggestion (non-blocking): I think the global --token - path is the one slack api and sandbox rely on, but the new tests only cover login's local flag and the helper. What do you think about a TestExecuteContext-style case that runs api auth.test --token - with mocked stdin and asserts the resolved Config.TokenFlag? That would also cover the error return just below.
There was a problem hiding this comment.
Added Test_InitConfig in commit a1bf205, thanks! It covers the stdin value and the error return.
| } | ||
|
|
||
| // Read the token from standard input when requested | ||
| token, err := auth.ResolveTokenInput(clients, clients.Config.TokenFlag) |
There was a problem hiding this comment.
๐ง issue (non-blocking): Since this reads stdin before any command runs, echo '[...]' | slack blocks preview --blocks - --token - uses the blocks JSON as the token and then fails with "No blocks were provided", which points at the wrong flag. I tried it with a local build. Could we error up front when both flags are -, naming the conflict? The PR notes mention the limit, so this would just make it loud.
There was a problem hiding this comment.
Fixed in commit a1bf205, great catch! --blocks - --token - now errors with mismatched_flags and names both flags. Thanks for trying it on a local build! ๐
| if clients.IO.IsStdinTTY() { | ||
| return "", slackerror.New(slackerror.ErrMissingInput). | ||
| WithMessage("No token was provided on standard input"). | ||
| WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("auth login --token - < token.txt", false)) |
There was a problem hiding this comment.
๐ suggestion (non-blocking): This remediation always suggests auth login --token - < token.txt, even when the global flag was used on slack api or sandbox. Maybe a command-neutral hint reads better there?
| WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("auth login --token - < token.txt", false)) | |
| WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("--token - < token.txt", false)) |
| // ResolveTokenInput returns the token read from standard input when the value | ||
| // is "-" and otherwise returns the value unchanged. Reading from standard input | ||
| // keeps the token out of shell history and process listings. | ||
| func ResolveTokenInput(clients *shared.ClientFactory, value string) (string, error) { |
There was a problem hiding this comment.
๐ช suggestion (non-blocking): This is almost the same shape as resolveBlocksInput and readStdinBlocks in cmd/blocks/preview.go (TTY check, ReadInAll, empty check, missing_input). What do you think about one shared helper, maybe on iostreams, that both flags call with their own messages? Totally fine as a follow-up too.
There was a problem hiding this comment.
Good idea! I'll keep this PR small and do the shared helper as a follow-up. Thanks!
| "github.com/slackapi/slack-cli/internal/style" | ||
| ) | ||
|
|
||
| // stdinTokenValue is the --token value that reads the token from standard input |
There was a problem hiding this comment.
๐ช nitpick (non-blocking): I don't think we need this comment, stdinTokenValue already says it. Same for the // Read the token from standard input when requested line in root.go, which restates the call below it.
| // stdinTokenValue is the --token value that reads the token from standard input |
| } | ||
| token, err := clients.IO.ReadInAll() | ||
| if err != nil { | ||
| return "", slackerror.Wrap(err, slackerror.ErrMissingInput) |
There was a problem hiding this comment.
๐งช suggestion (non-blocking): The ReadInAll error branch isn't hit by a test yet. A reader that returns an error in Test_ResolveTokenInput would pin the missing_input wrap.
There was a problem hiding this comment.
Added a read error case in commit a1bf205, thanks!
| if value != stdinTokenValue { | ||
| return value, nil | ||
| } | ||
| if clients.IO.IsStdinTTY() { |
There was a problem hiding this comment.
๐ฏ praise: Checking IsStdinTTY first so an interactive --token - fails right away instead of hanging is a really nice touch.
zimeg
left a comment
There was a problem hiding this comment.
๐ @mwbrooks LGTM! Reading --token - keeps the token out of shell history and the debug log, which records the full command, and /dev/null on standard input errors right away instead of hanging ๐
๐ quibble: The --blocks help text reads (use - to read from standard input), so perhaps we match that wording for both --token flags while the docs are regenerated?
- Error when --token - and --blocks - both read standard input - Use a command-neutral remediation and match the --blocks help wording - Test the global flag path in InitConfig and the stdin read error - Remove comments that restate the code
|
Thanks @WilliamBergamin, @zimeg, and @srtaalej for the careful reviews! Catching the |
Changelog
The
--tokenflag now reads the token from standard input when its value is-, which keeps tokens out of shell history and process listings. This works for both the global--tokenflag andslack auth login --token.Summary
This pull request lets developers and coding agents pass a token to the CLI on standard input, so a secret never has to appear in shell history or
psoutput.auth.ResolveTokenInput) handles both flags, using the same-convention asslack blocks preview --blocks -.missing_inputinstead of hanging or sending an empty token.Testing
echo "xoxp-fake" | slack api auth.test --token -sends the piped token and returnsinvalid_auth.echo "xoxp-fake" | slack auth login --token -validates the piped token and fails withinvalid_auth.printf '' | slack api auth.test --token -errors with "No token was provided on standard input".slack auth login --token -from an interactive terminal errors right away instead of waiting on input.Notes
--token -and--blocks -can't both be used on one command.Requirements