-
Notifications
You must be signed in to change notification settings - Fork 34
Add authctl user set-shell command
#1165
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
Open
adombeck
wants to merge
14
commits into
main
Choose a base branch
from
939-set-shell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,282
−138
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
93315ac
Add gRPC method SetShell
adombeck 7af7110
Test SetShell methods in db and users packages
adombeck b0cfa01
Add `authctl user set-shell` command
adombeck 45ab5d4
docs: Generate docs for `authctl user set-shell`
adombeck 6e3c5d5
Rename TestIsRequestFromRoot -> TestCheckRequestIsFromRoot
adombeck e379217
Add TestCheckRequestIsFromRootOrUID
adombeck eb9383d
Test the SetShell method of the user service
adombeck b8f6038
Add envutils
adombeck 4551e08
Fix authctl tests not producing coverage data
adombeck 9591b09
Improve error message
adombeck 07f98a7
SetShell: Only allow root to set a user's shell
adombeck 7941b7e
SetShell: Allow root to set an invalid shell
adombeck 5c758e9
SetUserID: Improve warning messages
adombeck ee00943
TestSetUserID: Fix entire warning message replaced
adombeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package user | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/canonical/authd/cmd/authctl/internal/client" | ||
| "github.com/canonical/authd/cmd/authctl/internal/completion" | ||
| "github.com/canonical/authd/cmd/authctl/internal/log" | ||
| "github.com/canonical/authd/internal/proto/authd" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var setShellCmd = &cobra.Command{ | ||
| Use: "set-shell <name> <shell>", | ||
| Short: "Set the login shell for a user", | ||
| Args: cobra.ExactArgs(2), | ||
| ValidArgsFunction: setShellCompletionFunc, | ||
| RunE: runSetShell, | ||
| } | ||
|
|
||
| func runSetShell(cmd *cobra.Command, args []string) error { | ||
| name := args[0] | ||
| shell := args[1] | ||
|
|
||
| svc, err := client.NewUserServiceClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := svc.SetShell(context.Background(), &authd.SetShellRequest{ | ||
| Name: name, | ||
| Shell: shell, | ||
| }) | ||
| if resp == nil { | ||
| return err | ||
| } | ||
|
|
||
| // Print any warnings returned by the server. | ||
| for _, warning := range resp.Warnings { | ||
| log.Warning(warning) | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func setShellCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| if len(args) == 0 { | ||
| return completion.Users(cmd, args, toComplete) | ||
| } | ||
|
|
||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package user_test | ||
|
|
||
| import ( | ||
| "os/exec" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/canonical/authd/internal/testutils" | ||
| "google.golang.org/grpc/codes" | ||
| ) | ||
|
|
||
| func TestSetShellCommand(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| daemonSocket := testutils.StartAuthd(t, daemonPath, | ||
| testutils.WithGroupFile(filepath.Join("testdata", "empty.group")), | ||
| testutils.WithPreviousDBState("one_user_and_group"), | ||
| testutils.WithCurrentUserAsRoot, | ||
| ) | ||
|
|
||
| authctlEnv := []string{ | ||
| "AUTHD_SOCKET=" + daemonSocket, | ||
| testutils.CoverDirEnv(), | ||
| } | ||
|
|
||
| tests := map[string]struct { | ||
| args []string | ||
|
|
||
| expectedExitCode int | ||
| }{ | ||
| "Set_shell_success": {args: []string{"set-shell", "user1", "/bin/bash"}, expectedExitCode: 0}, | ||
|
|
||
| "Error_when_user_does_not_exist": { | ||
| args: []string{"set-shell", "invaliduser", "/bin/bash"}, | ||
| expectedExitCode: int(codes.NotFound), | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| //nolint:gosec // G204 it's safe to use exec.Command with a variable here | ||
| cmd := exec.Command(authctlPath, append([]string{"user"}, tc.args...)...) | ||
| cmd.Env = authctlEnv | ||
| testutils.CheckCommand(t, cmd, tc.expectedExitCode) | ||
| }) | ||
| } | ||
| } |
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
1 change: 1 addition & 0 deletions
1
cmd/authctl/user/testdata/golden/TestSetShellCommand/Error_when_user_does_not_exist
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Error: user "invaliduser" not found |
Empty file.
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ## authctl user set-shell | ||
|
|
||
| Set the login shell for a user | ||
|
|
||
| ``` | ||
| authctl user set-shell <name> <shell> [flags] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for set-shell | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [authctl user](authctl_user.md) - Commands related to users | ||
|
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I thought this was addressed already as part of #782 (comment) :)