Skip to content

Commit

Permalink
nit,feat,fix: Add --sort & Fix color & Change name
Browse files Browse the repository at this point in the history
  • Loading branch information
eugercek committed Aug 10, 2022
1 parent 96f5a8d commit c763684
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 23 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
build:
name: Linux
name: CI
runs-on: ubuntu-latest

steps:
Expand All @@ -21,15 +21,21 @@ jobs:
- run: go version

- name: Build for Linux
run: go build -o pol-exp .
run: go build -o polic .
env:
GOOS: linux

- name: Build for Windows
run: go build -o pol-exp.exe .
run: go build -o polic.exe .
env:
GOOS: windows

- name: Build for Mac
run: go build -o polic-mac
env:
GOOS: darwin

- uses: ncipollo/release-action@v1
with:
artifacts: "pol-exp*"
artifacts: "polic*"
token: ${{ github.token}}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
# Go workspace file
go.work

aws-iam-policy-expander
polic
.idea
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ Expand cryptic policies to achieve least privilege
## Expand policy file and save as

```sh
$ ./aws-iam-policy-expander -file=foo.json -out=out.json
$ ./polic --file=foo.json --out=out.json # Also can --sort
```

## Expand policy file and change the file

```sh
$ ./aws-iam-policy-expander -file=foo.json -inline
$ ./polic --file=foo.json --inline --sort
```

## Expand single action

```sh
$ ./aws-iam-policy-expander -single waf:Get*
$ ./polic -single waf:Get*
waf:GetByteMatchSet
waf:GetChangeToken
waf:GetChangeTokenStatus
Expand All @@ -43,7 +43,7 @@ waf:GetXssMatchSet
## Expand in a repl

```sh
$ ./aws-iam-policy-expander
$ ./polic
Enter an AWS action: (enter-something)
.... # results
Enter an AWS action: (enter-something)
Expand All @@ -53,4 +53,3 @@ Enter an AWS action: exit # Or <Ctrl-C>

# TODO

- Add sort option
12 changes: 10 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"flag"
"fmt"

"github.com/eugercek/aws-iam-policy-expander/cmd"
"github.com/eugercek/polic/cmd"
)

func Run() int {
Expand All @@ -13,6 +13,7 @@ func Run() int {
repl := flag.Bool("repl", false, "open in repl mode")
out := flag.String("out", "", "output file name (only for file flag)")
inline := flag.Bool("inline", false, "change the (input) policy file (only for file flag")
sorted := flag.Bool("sort", false, "make actions sorted in files")

flag.Parse()

Expand All @@ -22,6 +23,10 @@ func Run() int {
return 1
}

if *sorted {
fmt.Println("No need for sort, single is always sorted")
}

return cmd.Single(flag.Args()[0])
} else if !*single && *file != "" && !*repl {
var resultFile string
Expand All @@ -35,8 +40,11 @@ func Run() int {
return 1
}

return cmd.File(*file, resultFile)
return cmd.File(*file, resultFile, *sorted)
} else if !*single && *file == "" && *repl {
if *sorted {
fmt.Println("No need for sort, repl is always sorted")
}
return cmd.Repl()
} else {
fmt.Println("Wrong flag. Given")
Expand Down
2 changes: 1 addition & 1 deletion cmd/expander/fetch.go → cmd/expander/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ func ExpandAction(inp string) (ret []string, str string, err error) {
}
}

return ret, service, nil
return ret, folded, nil
}
15 changes: 11 additions & 4 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"io/ioutil"
"log"
"os"
"sort"
"strings"

"github.com/eugercek/aws-iam-policy-expander/cmd/expander"
"github.com/eugercek/aws-iam-policy-expander/cmd/policy"
"github.com/eugercek/polic/cmd/expander"
"github.com/eugercek/polic/cmd/policy"
)

func File(filename, resultFile string) int {
func File(filename, resultFile string, sortFlag bool) int {
file, err := os.Open(filename)

if err != nil {
Expand Down Expand Up @@ -44,7 +45,8 @@ func File(filename, resultFile string) int {
}
elems = st.NotAction
} else {
log.Fatal("Action or NotAction must be given.")
log.Println("Action or NotAction must be given.")
return 1
}

for _, str := range elems {
Expand All @@ -54,6 +56,11 @@ func File(filename, resultFile string) int {
} else {
actions = append(actions, str)
}

if sortFlag {
sort.Strings(actions)
}

setter(i, actions)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"

"github.com/eugercek/aws-iam-policy-expander/cmd/expander"
"github.com/eugercek/polic/cmd/expander"
)

func Repl() int {
Expand All @@ -24,7 +24,7 @@ func Repl() int {
}

for _, v := range actions {
fmt.Println(color(v, base))
fmt.Println(color(v, base[:len(base)-1]))
}
}
return 0
Expand Down
4 changes: 2 additions & 2 deletions cmd/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/eugercek/aws-iam-policy-expander/cmd/expander"
"github.com/eugercek/polic/cmd/expander"
)

func Single(action string) int {
Expand All @@ -16,7 +16,7 @@ func Single(action string) int {
}

for _, v := range actions {
fmt.Println(color(v, base))
fmt.Println(color(v, base[:len(base)-1]))
}
return 0
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/eugercek/aws-iam-policy-expander
module github.com/eugercek/polic

go 1.18
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"os"

"github.com/eugercek/aws-iam-policy-expander/cli"
"github.com/eugercek/polic/cli"
)

func main() {
Expand Down

0 comments on commit c763684

Please sign in to comment.