Skip to content

Commit

Permalink
Simplify lookup command (#98)
Browse files Browse the repository at this point in the history
* Exact match if there is only 1 argument
* If no argument provided - match all
  • Loading branch information
cr7258 authored Feb 5, 2024
1 parent 37b003a commit ba68853
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,21 @@ Examples:

```shell script
# Search All Service Accounts
rbac-tool lookup -e '.*'
rbac-tool lookup
```

```shell script
# Search All Service Accounts That Contains myname
# Search Service Accounts that match myname exactly
rbac-tool lookup myname
```

```shell script
# Search All Service Accounts that contain myname
rbac-tool lookup -e '.*myname.*'
```

```shell script
# Lookup System Accounts (all accounts that start with system: )
rbac-tool lookup -e '^system:'
SUBJECT | SUBJECT TYPE | SCOPE | NAMESPACE | ROLE | BINDING
+-------------------------------------------------+--------------+-------------+-------------+----------------------------------------------------------------------+---------------------------------------------------+
Expand Down
20 changes: 13 additions & 7 deletions cmd/lookup_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ A Kubernetes RBAC lookup of Roles/ClusterRoles used by a given User/ServiceAccou
Examples:
# Search All Service Accounts
rbac-tool lookup -e '.*'
rbac-tool lookup
# Search Service Accounts that match myname exactly
rbac-tool lookup myname
# Search All Service Accounts that contain myname
rbac-tool lookup -e '.*myname.*'
Expand All @@ -47,14 +50,17 @@ rbac-tool lookup -ne '^system:.*'
var re *regexp.Regexp
var err error

if regex != "" {
re, err = regexp.Compile(regex)
} else {
if len(args) != 1 {
re, err = regexp.Compile(fmt.Sprintf(`.*`))
if regex == "" {
if len(args) == 1 {
// exact match
re, err = regexp.Compile(fmt.Sprintf(`^%v$`, args[0]))
} else {
re, err = regexp.Compile(fmt.Sprintf(`(?mi)%v`, args[0]))
// search all service accounts
re, err = regexp.Compile(fmt.Sprintf(`.*`))
}
} else {
// regex match
re, err = regexp.Compile(regex)
}

if err != nil {
Expand Down

0 comments on commit ba68853

Please sign in to comment.