Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

engflow_auth: Add export subcommand #17

Merged
merged 1 commit into from
Jul 31, 2024
Merged

engflow_auth: Add export subcommand #17

merged 1 commit into from
Jul 31, 2024

Conversation

minor-fixes
Copy link
Contributor

This change adds an export subcommand which prints the (*oauth2.Token).AccessToken field for the specified cluster to stdout, when:

  • a token is stored for the specified cluster
  • the token is not expired

This isn't any less secure than using engflow_auth get, which is another variation on the same intent; since the token storage currently used is backed by the OS keychain, only the current user should be able to fetch tokens they've stored.

Tested: unit tests added
Bug: linear/CUS-367

Base automatically changed from scott/cli_migration to main July 29, 2024 22:35
ctx := cliCtx.Context

if cliCtx.NArg() != 1 {
return autherr.CodedErrorf(autherr.CodeBadParams, "expected exactly 1 positional argument; got %d positional arguments", cliCtx.NArg())
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: give the user a hint on what they should pass here.

Suggested change
return autherr.CodedErrorf(autherr.CodeBadParams, "expected exactly 1 positional argument; got %d positional arguments", cliCtx.NArg())
return autherr.CodedErrorf(autherr.CodeBadParams, "expected exactly 1 positional argument, a cluster host name; got %d", cliCtx.NArg())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor

Choose a reason for hiding this comment

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

Not seeing the difference in this commit. Was it added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, looks like I got bamboozled by VSCode - fixed


token, err := r.tokenStore.Load(ctx, clusterURL.Host)
if err != nil {
if reauthErr := new(autherr.CodedError); errors.As(err, &reauthErr) && reauthErr.Code == autherr.CodeReauthRequired {
Copy link
Contributor

Choose a reason for hiding this comment

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

No point in allocating a new value with new then replacing with errors.As. Use nil instead.

Suggested change
if reauthErr := new(autherr.CodedError); errors.As(err, &reauthErr) && reauthErr.Code == autherr.CodeReauthRequired {
if reauthErr := (*autherr.CodedError)(nil); errors.As(err, &reauthErr) && reauthErr.Code == autherr.CodeReauthRequired {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

return autherr.ReauthRequired(clusterURL.Host)
}

if _, err := fmt.Fprintf(cliCtx.App.Writer, "%s\n", token.AccessToken); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a serialization format, so let's think about this carefully. It's quite likely that whatever's printed here will be read and written by different version of this program. If we only print the token, there's no way to extend the format and include more information.

I'd suggest instead that we stick this in a JSON structure and comment not to make any backward-incompatible changes. That will let us add new fields later on if we need to.

An example of something that might be nice: we could include the cluster URL so there's no need to specify it in the corresponding import command.

Copy link
Contributor

@rogerhu rogerhu Jul 30, 2024

Choose a reason for hiding this comment

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

Right - the token really has an affinity to the cluster URL as well.

Does it also include the expiry time too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done; added tests to make this more clear

ctx := cliCtx.Context

if cliCtx.NArg() != 1 {
return autherr.CodedErrorf(autherr.CodeBadParams, "expected exactly 1 positional argument; got %d positional arguments", cliCtx.NArg())
Copy link
Contributor

Choose a reason for hiding this comment

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

Not seeing the difference in this commit. Was it added?

This change adds an `export` subcommand which prints the
`(*oauth2.Token).AccessToken` field for the specified cluster to stdout,
when:
* a token is stored for the specified cluster
* the token is not expired

This isn't any less secure than using `engflow_auth get`, which is
another variation on the same intent; since the token storage currently
used is backed by the OS keychain, only the current user should be able
to fetch tokens they've stored.

Tested: unit tests added
Bug: linear/CUS-367
@jayconrod
Copy link
Contributor

A gentle request for easier reviews: please don't force-push after someone has reviewed. It makes it difficult to see what has changed between reviews. Normally, I can click Show changes since your last review, but after a force-push, that doesn't work because the old commits are gone.

If you need to update the branch, git merge works best, since it preserves the old commits. git rebase plus new commits requires a force-push, so it makes the review harder, but it's still possible to look at the last couple commits directly. git commit --amend makes everything harder though.

We squash and merge everything, so the end result of all these is the same. It's just a convenience for reviewers.

@minor-fixes minor-fixes merged commit e78edce into main Jul 31, 2024
4 checks passed
@minor-fixes minor-fixes deleted the scott/CUS-367 branch July 31, 2024 00:43
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