Skip to content

Commit

Permalink
fixed the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWoolfenden committed Aug 4, 2022
1 parent 18d281a commit 63c6ff2
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 158 deletions.
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ module github.com/jameswoolfenden/pike
go 1.17

require (
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
github.com/hashicorp/hcl/v2 v2.13.0
github.com/urfave/cli/v2 v2.10.3
)

require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
golang.org/x/text v0.3.7 // indirect
)
58 changes: 56 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,71 @@
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws=
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/hashicorp/hcl/v2 v2.13.0 h1:0Apadu1w6M11dyGFxWnmhhcMjkbAiKCv7G1r/2QgCNc=
github.com/hashicorp/hcl/v2 v2.13.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli/v2 v2.10.3 h1:oi571Fxz5aHugfBAJd5nkwSk3fzATXtMlpxdLylSCMo=
github.com/urfave/cli/v2 v2.10.3/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36 changes: 11 additions & 25 deletions src/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,34 @@ package pike
import (
"encoding/json"
"log"

"github.com/hashicorp/hcl/hcl/ast"
)

// GetAWSPermissions for AWS resources
func GetAWSPermissions(result template) []string {
myAttributes := GetAttributes(result)
func GetAWSPermissions(result ResourceV2) []string {

var Permissions []string
switch result.Resource.name {
switch result.Name {
case "aws_s3_bucket":
Permissions = GetPermissionMap(aws_s3_bucket, myAttributes)
Permissions = GetPermissionMap(aws_s3_bucket, result.Attributes)
case "aws_instance":
Permissions = GetPermissionMap(aws_instance, myAttributes)
Permissions = GetPermissionMap(aws_instance, result.Attributes)
case "aws_security_group":
Permissions = GetPermissionMap(aws_security_group, myAttributes)
Permissions = GetPermissionMap(aws_security_group, result.Attributes)
case "aws_lambda_function":
Permissions = GetPermissionMap(aws_lambda_function, myAttributes)
Permissions = GetPermissionMap(aws_lambda_function, result.Attributes)
case "aws_vpc":
Permissions = GetPermissionMap(aws_vpc, myAttributes)
Permissions = GetPermissionMap(aws_vpc, result.Attributes)
case "aws_subnet":
Permissions = GetPermissionMap(aws_subnet, myAttributes)
Permissions = GetPermissionMap(aws_subnet, result.Attributes)
case "aws_network_acl":
Permissions = GetPermissionMap(aws_network_acl, myAttributes)
Permissions = GetPermissionMap(aws_network_acl, result.Attributes)
default:
log.Printf("%s %s not found", result.Template, result.Resource.name)
log.Printf("%s not implemented", result.Name)
}

return Permissions
}

// GetAttributes gets the name of the important attributes for this resource
func GetAttributes(result template) []string {
temp := result.Resource.code.Val.(*ast.ObjectType)
attributes := temp.List.Items
var myAttributes []string
for _, item := range attributes {
mytemp := item.Keys
myAttributes = append(myAttributes, mytemp[0].Token.Text)
}
return myAttributes
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
Expand Down
20 changes: 0 additions & 20 deletions src/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,6 @@ import (
// }
// }

func TestGetAttributes(t *testing.T) {
type args struct {
result template
}
tests := []struct {
name string
args args
want []string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetAttributes(tt.args.result); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAttributes() = %v, want %v", got, tt.want)
}
})
}
}

func Test_contains(t *testing.T) {
type args struct {
s []string
Expand Down
61 changes: 39 additions & 22 deletions src/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,56 @@ import (
"log"
"strings"

"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/v2/hclsyntax"

"github.com/hashicorp/hcl/v2/hclparse"
)

// GetResources retrieves all the resources in a tf file
func GetResources(file string) ([]Resource, error) {

var results []Resource
func GetResources(file string) ([]ResourceV2, error) {

src, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}

myCode, err := hcl.Parse(string(src))
parser := hclparse.NewParser()
parsedFile, fileDiags := parser.ParseHCL(src, file)

if err != nil {
log.Printf("failed to parse %s", file)
if fileDiags != nil {
return nil, fileDiags
}

if myCode == nil {
return nil, errors.New("parsing error: no code parsed")
}
Tree := myCode.Node.(*ast.ObjectList)

for _, item := range Tree.Items {
var temp Resource
temp.name = strings.Trim(item.Keys[1].Token.Text, "\"")
temp.path = file
temp.code = *item
results = append(results, temp)
var Resources []ResourceV2

temp := parsedFile.Body.(*hclsyntax.Body)

for _, block := range temp.Blocks {
var resource ResourceV2
resource.TypeName = block.Type

if resource.TypeName == "terraform" || resource.TypeName == "output" || resource.TypeName == "provider" {
continue
}

if block.Labels != nil {
resource.Name = block.Labels[0]

if len(block.Labels) > 1 {
resource.ResourceName = block.Labels[1]
}
}

var attributes []string
for _, attribute := range block.Body.Attributes {
attributes = append(attributes, attribute.Name)
}
resource.Attributes = attributes
resource.Provider = GetHCLType(block.Labels[0])
Resources = append(Resources, resource)
}

// resources, filename, code
return results, nil
return Resources, nil
}

// GetProvider retrieves the provider from the resource
Expand All @@ -52,7 +67,7 @@ func GetProvider(resource string) string {
}

// GetPermission determines the IAM permissions required and returns a list of permission
func GetPermission(result template) (Sorted, error) {
func GetPermission(result ResourceV2) (Sorted, error) {
var myPermission Sorted
switch result.Provider {
case "aws":
Expand All @@ -61,6 +76,8 @@ func GetPermission(result template) (Sorted, error) {
return myPermission, errors.New("not implemented")
case "gcp", "google":
myPermission.GCP = GetGCPPermissions(result)
case "provider":
return myPermission, nil
default:
if result.Provider != "" {
log.Printf("Provider %s was not found", result.Provider)
Expand Down
22 changes: 0 additions & 22 deletions src/data_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pike

import (
//"io/fs"
"reflect"
"testing"
)

Expand Down Expand Up @@ -47,23 +45,3 @@ func TestGetProvider(t *testing.T) {
})
}
}

func TestGetPermission(t *testing.T) {
type args struct {
result template
}
tests := []struct {
name string
args args
want []interface{}
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, _ := GetPermission(tt.args.result); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetPermission() = %v, want %v", got, tt.want)
}
})
}
}
9 changes: 4 additions & 5 deletions src/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
)

// GetGCPPermissions for GCP resources
func GetGCPPermissions(result template) []string {
myAttributes := GetAttributes(result)
func GetGCPPermissions(result ResourceV2) []string {
var Permissions []string
switch result.Resource.name {
switch result.Name {
case "google_compute_instance":
Permissions = GetPermissionMap(google_compute_instance, myAttributes)
Permissions = GetPermissionMap(google_compute_instance, result.Attributes)

default:
log.Printf("%s %s not yet implemented", result.Template, result.Resource.name)
log.Printf("%s not yet implemented", result.Name)
}

return Permissions
Expand Down
5 changes: 4 additions & 1 deletion src/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func TestGetPolicy(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
GetPolicy(tt.args.actions)
got := GetPolicy(tt.args.actions)
if got == nil {
t.Errorf("GetPolicy() = %v, want %v", got, tt.want)
}
})
}
}
Expand Down
24 changes: 9 additions & 15 deletions src/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"log"
"path/filepath"
"strings"
)

// Scan looks for resources in a given directory
Expand All @@ -19,28 +20,21 @@ func Scan(dirname string) error {
return err2
}

var results []template

var resources []ResourceV2
for _, file := range files {

resources, err := GetResources(file)
resource, err := GetResources(file)

if err != nil {
//parse the other files
log.Print(err)
}

for _, resource := range resources {
hcltype := GetHCLType(resource)
provider := GetProvider(resource.name)
result := template{resource, provider, hcltype}
results = append(results, result)
}
resources = append(resources, resource...)
}
var PermissionBag Sorted

for _, result := range results {
newPerms, err := GetPermission(result)
for _, resource := range resources {
newPerms, err := GetPermission(resource)

if err != nil {
return err
Expand Down Expand Up @@ -97,7 +91,7 @@ func stringInSlice(a string, list []string) bool {
return false
}

// GetHCLType gets the template type
func GetHCLType(hcl Resource) string {
return hcl.code.Keys[0].Token.Text
//GetHCLType gets the resource Name
func GetHCLType(resourceName string) string {
return strings.Split(resourceName, "_")[0]
}
Loading

0 comments on commit 63c6ff2

Please sign in to comment.