-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
cmd.go
166 lines (141 loc) · 4.17 KB
/
cmd.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package cmd
import (
"context"
"fmt"
"os"
// Initialize all known client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
aurora "github.com/logrusorgru/aurora/v3"
"github.com/spf13/cobra"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
"github.com/Ladicle/kubectl-rolesum/pkg/explorer"
brcmdutil "github.com/Ladicle/kubectl-rolesum/pkg/util/cmd"
"github.com/Ladicle/kubectl-rolesum/pkg/util/printer"
"github.com/Ladicle/kubectl-rolesum/pkg/util/subject"
)
var (
// set values via build flags
version string
commit string
rolesumExample = fmt.Sprintf(`%v
kubectl rolesum ci-bot
%v
kubectl rolesum -k Group developer`,
aurora.BrightBlack("# Summarize roles bound to the \"ci-bot\" ServiceAccount."),
aurora.BrightBlack("# Summarize roles bound to the \"developer\" Group."))
)
type Option struct {
SubjectKind string
SubjectName string
ShowOptionFlag bool
ClusterOnly bool
f cmdutil.Factory
}
func NewRolesumCmd() *cobra.Command {
opt := Option{}
cmd := &cobra.Command{
Use: fmt.Sprintf("rolesum [options] <%v>",
aurora.Yellow("SubjectName")),
Version: fmt.Sprintf("%v (commit %v)", version, commit),
DisableFlagsInUseLine: true,
SilenceUsage: true,
SilenceErrors: true,
Short: "Summarize RBAC roles for the specified subject",
Long: "Summarize RBAC roles for the specified subject",
Example: templates.Examples(rolesumExample),
Run: func(cmd *cobra.Command, args []string) {
brcmdutil.CheckErr(opt.Validate(cmd, args))
brcmdutil.CheckErr(opt.Run())
},
}
cmd.SetVersionTemplate(brcmdutil.VersionTemplate)
cmd.SetUsageTemplate(brcmdutil.UsageTemplate)
fsets := cmd.PersistentFlags()
cfgFlags := genericclioptions.NewConfigFlags(true)
cfgFlags.AddFlags(fsets)
matchVersionFlags := cmdutil.NewMatchVersionFlags(cfgFlags)
matchVersionFlags.AddFlags(fsets)
opt.f = cmdutil.NewFactory(matchVersionFlags)
cmd.Flags().StringVarP(&opt.SubjectKind, "subject-kind", "k", subject.KindSA, "Set SubjectKind to summarize")
cmd.Flags().BoolVarP(&opt.ClusterOnly, "cluster-only", "c", false, "Ignore namespaced Roles")
cmd.Flags().BoolVarP(&opt.ShowOptionFlag, "options", "o", false, "List of all options for this command")
return cmd
}
func (o *Option) Validate(cmd *cobra.Command, args []string) error {
if o.ShowOptionFlag {
cmd.SetUsageTemplate(brcmdutil.OptionTemplate)
fmt.Println(cmd.UsageString())
os.Exit(0)
}
if len(args) == 0 {
return fmt.Errorf("<%v> is required argument",
aurora.Cyan("SubjectName").Bold())
}
o.SubjectName = args[0]
switch o.SubjectKind {
case subject.KindGroup, subject.KindSA, subject.KindUser:
default:
return fmt.Errorf("\"%v\" is unknown SubjectKind",
aurora.Cyan(o.SubjectKind).Bold())
}
return nil
}
func (o *Option) Run() error {
ctx := context.Background()
sub := &rbacv1.Subject{
Name: o.SubjectName,
Kind: o.SubjectKind,
}
if !o.ClusterOnly {
k8sCfg := o.f.ToRawKubeConfigLoader()
ns, _, err := k8sCfg.Namespace()
if err != nil {
return err
}
sub.Namespace = ns
}
client, err := o.f.KubernetesClientSet()
if err != nil {
return err
}
exp := explorer.NewPolicyExplorer(client)
var nsp []*explorer.SubjectRole
if !o.ClusterOnly {
nsp, err = exp.NamespacedSbjRoles(ctx, sub)
if err != nil {
return err
}
}
clusterp, err := exp.ClusterSbjRoles(ctx, sub)
if err != nil {
return err
}
pp := printer.DefaultPrettyPrinter()
if sub.Kind == subject.KindSA {
sa, err := client.CoreV1().ServiceAccounts(sub.Namespace).
Get(ctx, sub.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return fmt.Errorf("ServiceAccount \"%v\" not found",
aurora.Cyan(fmt.Sprintf("%v/%v", sub.Namespace, sub.Name)).Bold())
} else if err != nil {
return err
}
pp.PrintSubject(sub)
pp.PrintSA(sa)
} else {
pp.PrintSubject(sub)
}
pp.BlankLine()
pp.PrintHeader("Policies")
if !o.ClusterOnly {
pp.PrintPolicies(nsp)
pp.BlankLine()
}
pp.PrintPolicies(clusterp)
return nil
}