-
Notifications
You must be signed in to change notification settings - Fork 36
Add CLI tool to lock/unlock users #782
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
Merged
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
ce9685a
db: add Disabled property to UserRow struct
shiv-tyagi 200da8b
users: add method to check if a user is disabled
shiv-tyagi 1d2bdfa
pam: check if the user is disabled before creating session
shiv-tyagi dd99918
pam: add test case Error_when_user_is_disabled while selecting broker
shiv-tyagi aa56268
internal/users: implement logic to enable/disable user
shiv-tyagi a64fbb7
user service: add API methods to enable/disable user
shiv-tyagi 3ae47bc
user service: add tests for DisableUser/EnableUser API methods
shiv-tyagi 0b81790
create authctl cli tool
shiv-tyagi b147ffb
Make authctl print usage message when called without subcommand
adombeck b19feaa
authctl: Improve order of commands in usage message
adombeck 28f2a40
Rename DisableUser -> LockUser, EnableUser -> UnlockUser
adombeck e2a04ed
Avoid allUsers() in migration to lowercase names
adombeck 7f3c33b
Create databases for migration tests from SQLite dumps
adombeck f40d5d8
Add migration to add column 'locked' to users table
adombeck ebd8c06
Test migration to add 'locked' column to users table
adombeck 95fa68c
Update schema version in golden files
adombeck 541d781
Divide testdata for migrations into subdirectories
adombeck bcb57d2
Add authctl completion scripts for bash, zsh, fish
adombeck a662aeb
debian/install: Install shell completion scripts
adombeck 0f710f8
authctl: Hide the completion command from the usage message
adombeck 28800a2
Specify required arguments in usage message
adombeck 2bd59cb
Fix "Locking user" message
adombeck 8800966
authctl: Improve error messages printed for gRPC errors
adombeck 4472c19
authctl: Exit with the gRPC error code as exit code
adombeck 73e45c8
authctl: Avoid printing errors twice
adombeck 275812d
authctl: Avoid printing usage message on error
adombeck 08ed971
authctl: Simplify short usage string
adombeck 19542a3
authctl: Improve long description
adombeck c36380f
Fix authctl exiting with 0 when called with unknown command
adombeck e526eec
Return gRPC errors in API methods
adombeck 39734fd
authctl: Add tests for root command
adombeck 14c9ab3
authctl: Add integration test for `authctl user`
adombeck 1fd692c
refactor: Rename RunDaemon -> StartDaemon
adombeck 681d9fb
Improve error message
adombeck 0358df8
Add debug logs to testutils
adombeck 1ab59c8
authctl: Add integration test for `authctl user lock`
adombeck 4318e1c
Improve error messages
adombeck 62280a1
refactor: Inline extra args in BuildDaemon()
adombeck 90d6a71
refactor: Add WithCurrentUserAsRoot option for testutils.StartDaemon()
adombeck 7ec389a
refactor: Register cleanup of daemon process as part of StartDaemon()
adombeck fb1f746
authctl: Print no output on success
adombeck aba2ba6
Improve error message
adombeck 87f6a83
Further improve error message
adombeck b17cfff
Further improve error message
adombeck f642c13
pam/integration-tests/ssh: Add a simple lock/unlock test via SSH
3v1n0 e602f16
Don't leak to unauthenticated users whether a user account is locked
adombeck f57b189
refactor: Extract GoBuildFlags()
adombeck 7f44cbf
Pass GoBuildFlags to authctl built in tests
adombeck 778f1ac
refactor: Use WithCurrentUserAsRoot option for testutils.runAuthd()
adombeck 5d2a434
Use lowercase username in UpdateLockedFieldForUser
adombeck 5548b4f
Prefix socket URI with "unix://" if no scheme is set.
adombeck b07bbde
Ensure that we exit with a status code smaller than 256.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Package main implements Cobra commands for management operations on authd. | ||
| package main | ||
|
|
||
adombeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/ubuntu/authd/cmd/authctl/user" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| var rootCmd = &cobra.Command{ | ||
| Use: "authctl", | ||
| Short: "CLI tool to interact with authd", | ||
| Long: "authctl is a command-line tool to interact with the authd service for user and group management.", | ||
| PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
| // The command was successfully parsed, so we don't want cobra to print usage information on error. | ||
| cmd.SilenceUsage = true | ||
| }, | ||
| CompletionOptions: cobra.CompletionOptions{ | ||
| HiddenDefaultCmd: true, | ||
| }, | ||
| // We handle errors ourselves | ||
| SilenceErrors: true, | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { return cmd.Usage() }, | ||
| } | ||
|
|
||
| func init() { | ||
| // Disable command sorting by name. This makes cobra print the commands in the | ||
| // order they are added to the root command and adds the `help` and `completion` | ||
| // commands at the end. | ||
| cobra.EnableCommandSorting = false | ||
|
|
||
| rootCmd.AddCommand(user.UserCmd) | ||
| } | ||
|
|
||
| func main() { | ||
| if err := rootCmd.Execute(); err != nil { | ||
| s, ok := status.FromError(err) | ||
| if !ok { | ||
| // If the error is not a gRPC status, we print it as is. | ||
| fmt.Fprintln(os.Stderr, err.Error()) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // If the error is a gRPC status, we print the message and exit with the gRPC status code. | ||
| switch s.Code() { | ||
| case codes.PermissionDenied: | ||
| fmt.Fprintln(os.Stderr, "Permission denied:", s.Message()) | ||
| default: | ||
| fmt.Fprintln(os.Stderr, "Error:", s.Message()) | ||
| } | ||
| code := int(s.Code()) | ||
| if code < 0 || code > 255 { | ||
| // We cannot exit with a negative code or a code greater than 255, | ||
| // so we map it to 1 in that case. | ||
| code = 1 | ||
| } | ||
|
|
||
| os.Exit(code) | ||
| } | ||
| } | ||
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,68 @@ | ||
| package main_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "testing" | ||
|
|
||
| "github.com/ubuntu/authd/internal/testutils" | ||
| "github.com/ubuntu/authd/internal/testutils/golden" | ||
| ) | ||
|
|
||
| var authctlPath string | ||
|
|
||
| func TestRootCommand(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := map[string]struct { | ||
| args []string | ||
| expectedExitCode int | ||
| }{ | ||
| "Usage_message_when_no_args": {expectedExitCode: 0}, | ||
| "Help_command": {args: []string{"help"}, expectedExitCode: 0}, | ||
| "Help_flag": {args: []string{"--help"}, expectedExitCode: 0}, | ||
| "Completion_command": {args: []string{"completion"}, expectedExitCode: 0}, | ||
|
|
||
| "Error_on_invalid_command": {args: []string{"invalid-command"}, expectedExitCode: 1}, | ||
| "Error_on_invalid_flag": {args: []string{"--invalid-flag"}, expectedExitCode: 1}, | ||
| } | ||
|
|
||
| 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, tc.args...) | ||
| t.Logf("Running command: %s", cmd.String()) | ||
| outputBytes, err := cmd.CombinedOutput() | ||
| output := string(outputBytes) | ||
| exitCode := cmd.ProcessState.ExitCode() | ||
|
|
||
| if tc.expectedExitCode == 0 && err != nil { | ||
| t.Logf("Command output:\n%s", output) | ||
| t.Errorf("Expected no error, but got: %v", err) | ||
| } | ||
|
|
||
| if exitCode != tc.expectedExitCode { | ||
| t.Logf("Command output:\n%s", output) | ||
| t.Errorf("Expected exit code %d, got %d", tc.expectedExitCode, exitCode) | ||
| } | ||
|
|
||
| golden.CheckOrUpdate(t, output) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| var cleanup func() | ||
| var err error | ||
| authctlPath, cleanup, err = testutils.BuildAuthctl() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Setup: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer cleanup() | ||
|
|
||
| m.Run() | ||
| } |
16 changes: 16 additions & 0 deletions
16
cmd/authctl/testdata/golden/TestRootCommand/Completion_command
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,16 @@ | ||
| Generate the autocompletion script for authctl for the specified shell. | ||
| See each sub-command's help for details on how to use the generated script. | ||
|
|
||
| Usage: | ||
| authctl completion [command] | ||
|
|
||
| Available Commands: | ||
| bash Generate the autocompletion script for bash | ||
| zsh Generate the autocompletion script for zsh | ||
| fish Generate the autocompletion script for fish | ||
| powershell Generate the autocompletion script for powershell | ||
|
|
||
| Flags: | ||
| -h, --help help for completion | ||
|
|
||
| Use "authctl completion [command] --help" for more information about a command. |
14 changes: 14 additions & 0 deletions
14
cmd/authctl/testdata/golden/TestRootCommand/Error_on_invalid_command
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,14 @@ | ||
| Usage: | ||
| authctl [flags] | ||
| authctl [command] | ||
|
|
||
| Available Commands: | ||
| user Commands related to users | ||
| help Help about any command | ||
|
|
||
| Flags: | ||
| -h, --help help for authctl | ||
|
|
||
| Use "authctl [command] --help" for more information about a command. | ||
|
|
||
| unknown command "invalid-command" for "authctl" |
14 changes: 14 additions & 0 deletions
14
cmd/authctl/testdata/golden/TestRootCommand/Error_on_invalid_flag
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,14 @@ | ||
| Usage: | ||
| authctl [flags] | ||
| authctl [command] | ||
|
|
||
| Available Commands: | ||
| user Commands related to users | ||
| help Help about any command | ||
|
|
||
| Flags: | ||
| -h, --help help for authctl | ||
|
|
||
| Use "authctl [command] --help" for more information about a command. | ||
|
|
||
| unknown flag: --invalid-flag |
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,14 @@ | ||
| authctl is a command-line tool to interact with the authd service for user and group management. | ||
|
|
||
| Usage: | ||
| authctl [flags] | ||
| authctl [command] | ||
|
|
||
| Available Commands: | ||
| user Commands related to users | ||
| help Help about any command | ||
|
|
||
| Flags: | ||
| -h, --help help for authctl | ||
|
|
||
| Use "authctl [command] --help" for more information about a command. |
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,14 @@ | ||
| authctl is a command-line tool to interact with the authd service for user and group management. | ||
|
|
||
| Usage: | ||
| authctl [flags] | ||
| authctl [command] | ||
|
|
||
| Available Commands: | ||
| user Commands related to users | ||
| help Help about any command | ||
|
|
||
| Flags: | ||
| -h, --help help for authctl | ||
|
|
||
| Use "authctl [command] --help" for more information about a command. |
12 changes: 12 additions & 0 deletions
12
cmd/authctl/testdata/golden/TestRootCommand/Usage_message_when_no_args
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,12 @@ | ||
| Usage: | ||
| authctl [flags] | ||
| authctl [command] | ||
|
|
||
| Available Commands: | ||
| user Commands related to users | ||
| help Help about any command | ||
|
|
||
| Flags: | ||
| -h, --help help for authctl | ||
|
|
||
| Use "authctl [command] --help" for more information about a command. |
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,28 @@ | ||
| package user | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/ubuntu/authd/internal/proto/authd" | ||
| ) | ||
|
|
||
| // lockCmd is a command to lock (disable) a user. | ||
| var lockCmd = &cobra.Command{ | ||
| Use: "lock <user>", | ||
| Short: "Lock (disable) a user managed by authd", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| client, err := NewUserServiceClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = client.LockUser(context.Background(), &authd.LockUserRequest{Name: args[0]}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } |
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,17 @@ | ||
| users: | ||
| - name: user1 | ||
| uid: 1111 | ||
| gid: 11111 | ||
| gecos: |- | ||
| User1 gecos | ||
| On multiple lines | ||
| dir: /home/user1 | ||
| shell: /bin/bash | ||
| broker_id: broker-id | ||
| groups: | ||
| - name: group1 | ||
| gid: 11111 | ||
| ugid: "12345678" | ||
| users_to_groups: | ||
| - uid: 1111 | ||
| gid: 11111 |
Empty file.
14 changes: 14 additions & 0 deletions
14
cmd/authctl/user/testdata/golden/TestUserCommand/Error_on_invalid_command
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,14 @@ | ||
| Usage: | ||
| authctl user [flags] | ||
| authctl user [command] | ||
|
|
||
| Available Commands: | ||
| lock Lock (disable) a user managed by authd | ||
| unlock Unlock (enable) a user managed by authd | ||
|
|
||
| Flags: | ||
| -h, --help help for user | ||
|
|
||
| Use "authctl user [command] --help" for more information about a command. | ||
|
|
||
| unknown command "invalid-command" for "authctl user" |
14 changes: 14 additions & 0 deletions
14
cmd/authctl/user/testdata/golden/TestUserCommand/Error_on_invalid_flag
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,14 @@ | ||
| Usage: | ||
| authctl user [flags] | ||
| authctl user [command] | ||
|
|
||
| Available Commands: | ||
| lock Lock (disable) a user managed by authd | ||
| unlock Unlock (enable) a user managed by authd | ||
|
|
||
| Flags: | ||
| -h, --help help for user | ||
|
|
||
| Use "authctl user [command] --help" for more information about a command. | ||
|
|
||
| unknown flag: --invalid-flag |
14 changes: 14 additions & 0 deletions
14
cmd/authctl/user/testdata/golden/TestUserCommand/Help_flag
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,14 @@ | ||
| Commands related to users | ||
|
|
||
| Usage: | ||
| authctl user [flags] | ||
| authctl user [command] | ||
|
|
||
| Available Commands: | ||
| lock Lock (disable) a user managed by authd | ||
| unlock Unlock (enable) a user managed by authd | ||
|
|
||
| Flags: | ||
| -h, --help help for user | ||
|
|
||
| Use "authctl user [command] --help" for more information about a command. |
12 changes: 12 additions & 0 deletions
12
cmd/authctl/user/testdata/golden/TestUserCommand/Usage_message_when_no_args
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,12 @@ | ||
| Usage: | ||
| authctl user [flags] | ||
| authctl user [command] | ||
|
|
||
| Available Commands: | ||
| lock Lock (disable) a user managed by authd | ||
| unlock Unlock (enable) a user managed by authd | ||
|
|
||
| Flags: | ||
| -h, --help help for user | ||
|
|
||
| Use "authctl user [command] --help" for more information about a command. |
1 change: 1 addition & 0 deletions
1
cmd/authctl/user/testdata/golden/TestUserLockCommand/Error_locking_invalid_user
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package user | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/ubuntu/authd/internal/proto/authd" | ||
| ) | ||
|
|
||
| // unlockCmd is a command to unlock (enable) a user. | ||
| var unlockCmd = &cobra.Command{ | ||
| Use: "unlock <user>", | ||
| Short: "Unlock (enable) a user managed by authd", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| client, err := NewUserServiceClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = client.UnlockUser(context.Background(), &authd.UnlockUserRequest{Name: args[0]}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.