Skip to content

Commit

Permalink
Test go work multi module, tags, versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh-io committed Sep 8, 2023
1 parent df265cb commit 9b60d87
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 116 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ git clone https://github.com/hexa-org/policy-mapper.git
sh ./build.sh
```

### Tidy up and run tests
```shell
cd policy-mapper
go list -f '{{.Dir}}' -m | xargs -L1 go mod tidy -C
go work sync
go list -f '{{.Dir}}/...' -m | xargs go test
```

See [here](DEMO.md) more instructions on how to run the hexaMapper command line utility.

## Using Hexa-Mapper in Go Projects
Expand Down
224 changes: 114 additions & 110 deletions cmd/mapTool/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package main

import (
"encoding/json"
"flag"
"fmt"

"io"
"os"
"strings"

"github.com/hexa-org/policy-mapper/hexaIdql/pkg/hexapolicysupport"
"github.com/hexa-org/policy-mapper/mapper/formats/awsCedar"
"github.com/hexa-org/policy-mapper/mapper/formats/gcpBind"
"encoding/json"
"flag"
"fmt"
"github.com/hexa-org/policy-mapper/hexaIdql/pkg/hexapolicy"

"io"
"os"
"strings"

"github.com/hexa-org/policy-mapper/hexaIdql/pkg/hexapolicysupport"
"github.com/hexa-org/policy-mapper/mapper/formats/awsCedar"
"github.com/hexa-org/policy-mapper/mapper/formats/gcpBind"
)

var helpFlag bool
Expand All @@ -32,119 +33,122 @@ mapTool -t=<awsCedar|gcpbind> [-parse] [-o=<output>] <input>
`

func main() {
isForward := true
flag.BoolVar(&helpFlag, "help", false, "Help information")
flag.BoolVar(&helpFlag, "h", false, "Help information")
flag.BoolVar(&revFlag, "p", false, "Map platform policy to IDQL")
flag.BoolVar(&revFlag, "parse", false, "Map platform policy to IDQL")
flag.StringVar(&output, "o", "", "Output path, default console")
flag.StringVar(&output, "output", "", "Output path, default console")
flag.StringVar(&target, "t", "", "Platform awsCedar|gcpbind")
flag.StringVar(&target, "target", "", "Platform awsCedar|gcpbind")

flag.Parse()

input = flag.Arg(0)
fmt.Println("Input=\t" + input)
if helpFlag || target == "" || input == "" {
if target == "" {
fmt.Println("Error: Please provide a mapping platform target with the -t parameter.")
}
if input == "" {
fmt.Println("Error: No input source specified.")
}
fmt.Printf(helpText)
return
}
if revFlag {
isForward = false
}

if isForward {
idqlToPlatform(input)
} else {
platformToIdql(input)
}
x := hexapolicy.PolicyInfoSaurabh{Name: "Saurabh"}
fmt.Println(x)

isForward := true
flag.BoolVar(&helpFlag, "help", false, "Help information")
flag.BoolVar(&helpFlag, "h", false, "Help information")
flag.BoolVar(&revFlag, "p", false, "Map platform policy to IDQL")
flag.BoolVar(&revFlag, "parse", false, "Map platform policy to IDQL")
flag.StringVar(&output, "o", "", "Output path, default console")
flag.StringVar(&output, "output", "", "Output path, default console")
flag.StringVar(&target, "t", "", "Platform awsCedar|gcpbind")
flag.StringVar(&target, "target", "", "Platform awsCedar|gcpbind")

flag.Parse()

input = flag.Arg(0)
fmt.Println("Input=\t" + input)
if helpFlag || target == "" || input == "" {
if target == "" {
fmt.Println("Error: Please provide a mapping platform target with the -t parameter.")
}
if input == "" {
fmt.Println("Error: No input source specified.")
}
fmt.Printf(helpText)
return
}
if revFlag {
isForward = false
}

if isForward {
idqlToPlatform(input)
} else {
platformToIdql(input)
}
}

func reportError(err error) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

func idqlToPlatform(input string) {
fmt.Println("Idql to " + target + " requested")

policies, err := hexapolicysupport.ParsePolicyFile(input)
if err != nil {
reportError(err)
}

switch strings.ToLower(target) {
case "gcpbind":
gcpMapper := gcpBind.New(map[string]string{})
bindings := gcpMapper.MapPoliciesToBindings(policies)
MarshalJsonNoEscape(bindings, getOutput())

case "awscedar":
cMapper := awsCedar.New(map[string]string{})

cedar, err := cMapper.MapPoliciesToCedar(policies)
if err != nil {
reportError(err)
}
out := getOutput()
for _, v := range cedar.Policies {
policy := v.String()
out.Write([]byte(policy))
}
}
fmt.Println("Idql to " + target + " requested")

policies, err := hexapolicysupport.ParsePolicyFile(input)
if err != nil {
reportError(err)
}

switch strings.ToLower(target) {
case "gcpbind":
gcpMapper := gcpBind.New(map[string]string{})
bindings := gcpMapper.MapPoliciesToBindings(policies)
MarshalJsonNoEscape(bindings, getOutput())

case "awscedar":
cMapper := awsCedar.New(map[string]string{})

cedar, err := cMapper.MapPoliciesToCedar(policies)
if err != nil {
reportError(err)
}
out := getOutput()
for _, v := range cedar.Policies {
policy := v.String()
out.Write([]byte(policy))
}
}
}

func platformToIdql(input string) {
fmt.Println(target + " to IDQL requested")

switch strings.ToLower(target) {
case "gcpbind":
gcpMapper := gcpBind.New(map[string]string{})
assignments, err := gcpBind.ParseFile(input)
if err != nil {
reportError(err)
}
policies, err := gcpMapper.MapBindingAssignmentsToPolicy(assignments)
if err != nil {
reportError(err)
}
MarshalJsonNoEscape(policies, getOutput())

case "awscedar":
cMapper := awsCedar.New(map[string]string{})

policies, err := cMapper.ParseFile(input)
if err != nil {
reportError(err)
}
MarshalJsonNoEscape(policies, getOutput())
}
fmt.Println(target + " to IDQL requested")

switch strings.ToLower(target) {
case "gcpbind":
gcpMapper := gcpBind.New(map[string]string{})
assignments, err := gcpBind.ParseFile(input)
if err != nil {
reportError(err)
}
policies, err := gcpMapper.MapBindingAssignmentsToPolicy(assignments)
if err != nil {
reportError(err)
}
MarshalJsonNoEscape(policies, getOutput())

case "awscedar":
cMapper := awsCedar.New(map[string]string{})

policies, err := cMapper.ParseFile(input)
if err != nil {
reportError(err)
}
MarshalJsonNoEscape(policies, getOutput())
}
}

func getOutput() io.Writer {
if output != "" {
out, err := os.Create(output)
if err != nil {
reportError(err)
}
return out
} else {
return os.Stdout
}
if output != "" {
out, err := os.Create(output)
if err != nil {
reportError(err)
}
return out
} else {
return os.Stdout
}
}

func MarshalJsonNoEscape(t interface{}, out io.Writer) error {

encoder := json.NewEncoder(out)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
err := encoder.Encode(t)
return err
encoder := json.NewEncoder(out)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
err := encoder.Encode(t)
return err
}
8 changes: 4 additions & 4 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use (
./mapper/formats/gcpBind
)

replace github.com/hexa-org/policy-mapper/hexaIdql v0.6.0 => ./hexaIdql
//replace github.com/hexa-org/policy-mapper/hexaIdql v0.6.0 => ./hexaIdql

replace github.com/hexa-org/policy-mapper/mapper/formats/gcpBind v0.6.0 => ./mapper/formats/gcpBind
//replace github.com/hexa-org/policy-mapper/mapper/formats/gcpBind v0.6.0 => ./mapper/formats/gcpBind

replace github.com/hexa-org/policy-mapper/mapper/formats/awsCedar v0.6.0 => ./mapper/formats/awsCedar
//replace github.com/hexa-org/policy-mapper/mapper/formats/awsCedar v0.6.0 => ./mapper/formats/awsCedar

replace github.com/hexa-org/policy-mapper/mapper/conditionLangs/gcpcel v0.6.0 => ./mapper/conditionLangs/gcpcel
//replace github.com/hexa-org/policy-mapper/mapper/conditionLangs/gcpcel v0.6.0 => ./mapper/conditionLangs/gcpcel
4 changes: 4 additions & 0 deletions hexaIdql/pkg/hexapolicy/hexa_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (p *Policies) AddPolicies(policies Policies) {
}
}

type PolicyInfoSaurabh struct {
Name string
}

type PolicyInfo struct {
Meta MetaInfo `validate:"required"`
Subject SubjectInfo `validate:"required"`
Expand Down
2 changes: 0 additions & 2 deletions mapper/conditionLangs/gcpcel/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ github.com/google/cel-go v0.18.0/go.mod h1:PVAybmSnWkNMUZR/tEWFUiJ1Np4Hz0MHsZJcg
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/hexa-org/policy-mapper/hexaIdql v0.0.0-20230908012848-8ba92634a612 h1:An/R5c5QEbVWRSAvjP6T1et5h8E7BIoVQqEhh1vXDE8=
github.com/hexa-org/policy-mapper/hexaIdql v0.0.0-20230908012848-8ba92634a612/go.mod h1:mqzC3LjDSVimn8u7s98JWJjrYUm4fCR7ZlMsqI/7ABM=
github.com/hexa-org/policy-mapper/hexaIdql v0.0.0-20230908031135-56181d2b5c41 h1:aK9inuPOniXyAB/3ZubURvmMVscaB8cJOZ52brcW82U=
github.com/hexa-org/policy-mapper/hexaIdql v0.0.0-20230908031135-56181d2b5c41/go.mod h1:mqzC3LjDSVimn8u7s98JWJjrYUm4fCR7ZlMsqI/7ABM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down

0 comments on commit 9b60d87

Please sign in to comment.