Skip to content

Commit

Permalink
Removed some debug code
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Hunt <[email protected]>
  • Loading branch information
independentid committed Dec 21, 2023
1 parent ef9cb4e commit bcbe533
Showing 1 changed file with 111 additions and 112 deletions.
223 changes: 111 additions & 112 deletions cmd/mapTool/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package main

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

"github.com/hexa-org/policy-mapper/pkg/hexapolicy"
"github.com/hexa-org/policy-mapper/pkg/hexapolicysupport"
"github.com/hexa-org/policy-mapper/pkg/hexapolicysupport"

"io"
"os"
"strings"
"io"
"os"
"strings"

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

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

func main() {
x := hexapolicy.PolicyInfoSaurabhV2{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)
}
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)
if err != nil {
return
}
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
}

0 comments on commit bcbe533

Please sign in to comment.