-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdelete.go
More file actions
75 lines (62 loc) · 2.05 KB
/
delete.go
File metadata and controls
75 lines (62 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package group
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"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"
)
const warningMessage = `
WARNING: Deleting a group that still owns files on the filesystem can lead to
security issues. Any existing files owned by this group's GID may become
accessible to a different group that is later assigned the same GID.
`
// deleteGroupCmd is a command to delete a group from the authd database.
var deleteGroupCmd = &cobra.Command{
Use: "delete <group>",
Short: "Delete a group managed by authd",
Long: "Delete a group from the authd database.\n\n" + warningMessage + "\n\nThe command must be run as root.",
Example: ` # Delete group "staff" from the authd database
authctl group delete staff
# Delete group "staff" without confirmation prompt
authctl group delete --yes staff`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: completion.Groups,
RunE: runDeleteGroup,
}
var deleteGroupYes bool
func init() {
deleteGroupCmd.Flags().BoolVarP(&deleteGroupYes, "yes", "y", false, "Skip confirmation prompt")
}
func runDeleteGroup(cmd *cobra.Command, args []string) error {
name := args[0]
if !deleteGroupYes {
log.Warning(warningMessage)
fmt.Fprintf(os.Stderr, "Are you sure you want to delete group %q? [y/N] ", name)
reader := bufio.NewReader(os.Stdin)
answer, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("failed to read confirmation: %w", err)
}
answer = strings.TrimSpace(strings.ToLower(answer))
if answer != "y" && answer != "yes" {
log.Info("Aborted.")
return nil
}
}
c, err := client.NewUserServiceClient()
if err != nil {
return err
}
_, err = c.DeleteGroup(context.Background(), &authd.DeleteGroupRequest{Name: name})
if err != nil {
return err
}
log.Infof("Group %q has been deleted from the authd database.", name)
return nil
}