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

RSDK-9424 - CLI profile support #4631

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

stuqdog
Copy link
Member

@stuqdog stuqdog commented Dec 13, 2024

Adds support for API key-based profile login with the CLI.

A profile can be added with viam profiles add --key-id <id> --key <key> --profile-name <name>
A profile can be removed with viam profiles remove --profile-name <name>
A profile can be modified with viam profiles update --key-id <id> --key <key> --profile-name <name>
Existing profiles can be listed with viam profiles list

To specify a profile for a specific command, you can use the top level --profile flag, e.g. viam --profile <my-profile> organizations list will auth to my-profile and list its orgs.

To specify a profile for a shell session, you can set the VIAM_CLI_PROFILE_NAME env var. If no profile is specified with the --profile flag, the value of that env var (if it is set) will be used as a profile. If the --profile flag is set, this will override the env var.

To fall back to default behavior and ignore the env var, the top level --disable-profiles flag will cause profiles to be ignored. It will cause an error if both --disable-profiles and --profile are set.

Example:
Screenshot 2024-12-13 at 13 53 34

@stuqdog stuqdog requested review from njooma and benjirewis December 13, 2024 19:27
@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Dec 13, 2024
@stuqdog
Copy link
Member Author

stuqdog commented Dec 13, 2024

@thegreatco fyi

@stuqdog
Copy link
Member Author

stuqdog commented Dec 13, 2024

Note to reviewers: I did not add unit tests. The current testing suite is pretty ill-suited to this; the inject functions don't have access to a cli context and so creating a global args to parse profile is not really feasible. I think the example functionality should be sufficient to show how this works, and the risk of this working incorrectly is fairly low (you can always roll back or just not use a profile). but if there are any concerns about safety here please let me know!

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 13, 2024
@stuqdog stuqdog requested review from a team and purplenicole730 and removed request for a team December 19, 2024 14:39
Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

Looking good! Super cool feature. Have some initial questions + comments.

cli/app.go Outdated Show resolved Hide resolved
cli/app.go Outdated Show resolved Hide resolved
cli/app.go Outdated Show resolved Hide resolved
cli/app.go Outdated Show resolved Hide resolved
cli/app.go Outdated
},
&cli.BoolFlag{
Name: disableProfilesFlag,
Aliases: []string{"disable-profile"},
Copy link
Member

Choose a reason for hiding this comment

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

Why add this alias?

Copy link
Member Author

Choose a reason for hiding this comment

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

When I was testing I found myself waffling between the two and found it a little annoying when the CLI told me one was wrong, so I added the alias for simplicity of use. Definitely not necessary, happy to remove if you think it's unnecessary cluttering!

Copy link
Member

Choose a reason for hiding this comment

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

Seems fine to keep; maybe just a comment above saying it's not for backward compatibility (as most of our aliases are) but for ease of use.

cli/app.go Outdated Show resolved Hide resolved
cli/config.go Outdated Show resolved Hide resolved
cli/profile.go Outdated
ProfileName string
}

func whichProfile(args *globalArgs) (_ *string, profileSpecified bool) {
Copy link
Member

Choose a reason for hiding this comment

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

So I don't think you need named returns here. Also, is there a reason to return *string instead of just string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I went with named returns because I worried that the significance of the return bool was ambiguous. I guess not too big a concern given the limited use here and the fact that it's not exported, I'll remove!

Re: *string, we check on nil-ness in ConfigFromCache. It could certainly be a regular string and we could check on if it's empty or not! I don't have a particular preference, do you think one is preferred?

Copy link
Member

Choose a reason for hiding this comment

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

I guess not too big a concern given the limited use here and the fact that it's not exported, I'll remove!

Yeah that comma-ok pattern you're using (value, valueExists) is common enough that I don't think you need the named returns.

Re: *string, we check on nil-ness in ConfigFromCache. It could certainly be a regular string and we could check on if it's empty or not!

I had assumed you would check the bool return value (previously profileSpecified) and assume the string represented a profile only if that bool was true (as opposed to checking if whichProf != nil.) i.e. I think you have enough expressiveness of "existence" through the boolean return value such that the string only needs to be string and not *string. But, no super strong preference, and I may be misunderstanding what you're doing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh, so the string might represent a profile even if profileSpecified is false, if the VIAM_CLI_PROFILE_NAME env var is set. Even when we know we have a profile, we care about whether it was specified via flag or not. specifically, we want to fallback on default behavior if the profile doesn't exist and wasn't specified (maybe the env var was accidentally exported), but not if the profile doesn't exist and was specified (since the user specifically asked for particular behavior, we shouldn't assume they'd be okay with different behavior).

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha, maybe a comment above whichProfile explaining exactly what you've said there could be helpful. Thanks for the extra context!

cli/profile.go Show resolved Hide resolved
@stuqdog stuqdog requested a review from a team as a code owner December 20, 2024 13:14
@stuqdog stuqdog requested a review from benjirewis December 20, 2024 13:14
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 20, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 20, 2024
Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

LGTM! Up to you if you want to add unit tests; I think if you manually tested, it's fine.

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 20, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 20, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 23, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants