Skip to content

Commit

Permalink
initial version of ruby routes generator
Browse files Browse the repository at this point in the history
  • Loading branch information
infosec-au committed Aug 13, 2018
1 parent 97b0a83 commit 3867486
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 12 deletions.
81 changes: 77 additions & 4 deletions command/routes/helper.go
Expand Up @@ -17,7 +17,9 @@
package routes

import (
"github.com/assetnote/commonspeak2/assets"
"github.com/assetnote/commonspeak2/log"
"github.com/icrowley/fake"
"cloud.google.com/go/bigquery"
"google.golang.org/api/iterator"
"golang.org/x/net/context"
Expand All @@ -32,17 +34,41 @@ func query(client *bigquery.Client, ctx context.Context, compiledSql string) (*b
return query.Read(ctx)
}

func cleanPathData(route string, framework string) string {

// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := assets.Asset(path)
if err != nil {
return nil, err
}
filterString := string(file[:])
lines := strings.Split(filterString, "\n")
return lines, nil
}



func cleanPathData(route string, framework string, numericalFilter []string, stringFilter []string) string {
switch framework {
case "rails":
rails_replacer := strings.NewReplacer(
// initial clean up
railsCleaner := strings.NewReplacer(
"'", "",
"\"", "",
"(", "",
")", "",
"*", ":",
)
route = rails_replacer.Replace(route)
route = railsCleaner.Replace(route)
// numerical replacements
numericalReplacer := strings.NewReplacer(numericalFilter...)
// make numerical replacements (random number 5 digits)
route = numericalReplacer.Replace(route)
// string replacements
stringReplacer := strings.NewReplacer(stringFilter...)
// make string replacements (random word lorem ipsum)
route = stringReplacer.Replace(route)
}
return route
}
Expand All @@ -54,6 +80,53 @@ func handleResults(w io.Writer, iter *bigquery.RowIterator, outputFile string, f
"Framework": framework,
"Source": "Github",
}

// obtain numerical parameter filters
numericalParameters, err := readLines("data/filters/numerical-parameters.txt")
if err != nil {
log.WithFields(fields).Debugf("Numerical parameters not found: %s", err.Error())
}

// obtain string parameter filters
stringParameters, stringErr := readLines("data/filters/string-parameters.txt")
if stringErr != nil {
log.WithFields(fields).Debugf("String parameters not found: %s", stringErr.Error())
}

numericalFilter := []string{}
stringFilter := []string{}

for _, parameter := range numericalParameters {
fmt.Println(parameter)
switch framework {
case "rails":
railsParam := fmt.Sprintf(":%s", parameter)
railsNumerical := fake.Digits()
numericalFilter = append(numericalFilter, railsParam, railsNumerical)
case "nodejs":
continue
case "tomcat":
continue
}
}

for _, parameter := range stringParameters {
fmt.Println(parameter)
switch framework {
case "rails":
railsParam := fmt.Sprintf(":%s", parameter)
railsString := fake.Word()
stringFilter = append(stringFilter, railsParam, railsString)
case "nodejs":
continue
case "tomcat":
continue
}
}

// fmt.Printf("%v",numericalFilter)
// fmt.Printf("%v",stringFilter)

file, err := os.Create(outputFile)
if err != nil {
fields["Filename"] = outputFile
Expand All @@ -75,7 +148,7 @@ func handleResults(w io.Writer, iter *bigquery.RowIterator, outputFile string, f
return err
}

parsedRoute := cleanPathData(row.Route.String(), framework)
parsedRoute := cleanPathData(row.Route.String(), framework, numericalFilter, stringFilter)

// Save to output file
fmt.Fprintf(file, "%s\n", parsedRoute)
Expand Down
10 changes: 5 additions & 5 deletions command/routes/routes.go
Expand Up @@ -76,7 +76,7 @@ func CmdStatus(c *cli.Context) error {
// Store generated templates in a string slice, if no
// source parameter is provided, the default is to
// run all sources available in the below switch statement
SQLTemplateStrings := make(map[string]string)
sqlTemplateStrings := make(map[string]string)
frameworkList := strings.Split(frameworks, ",")
for _, fw := range frameworkList {
switch fw {
Expand All @@ -93,7 +93,7 @@ func CmdStatus(c *cli.Context) error {
if verboseOpt {
log.WithFields(fields).Infof("Compiled SQL Template: %s", RailsCompiledSql)
}
SQLTemplateStrings["rails"] = RailsCompiledSql
sqlTemplateStrings["rails"] = RailsCompiledSql
log.WithFields(fields).Info("Generated SQL template for Rails routes.")
case "nodejs":
NodeSqlAsset, err := assets.Asset(NodeSqlAssetPath)
Expand All @@ -108,7 +108,7 @@ func CmdStatus(c *cli.Context) error {
if verboseOpt {
log.WithFields(fields).Infof("Compiled SQL Template: %s", NodeCompiledSql)
}
SQLTemplateStrings["nodejs"] = NodeCompiledSql
sqlTemplateStrings["nodejs"] = NodeCompiledSql
log.WithFields(fields).Info("Generated SQL template for NodeJS routes.")
case "tomcat":
TomcatSqlAsset, err := assets.Asset(TomcatSqlAssetPath)
Expand All @@ -123,7 +123,7 @@ func CmdStatus(c *cli.Context) error {
if verboseOpt {
log.WithFields(fields).Infof("Compiled SQL Template: %s", TomcatCompiledSql)
}
SQLTemplateStrings["tomcat"] = TomcatCompiledSql
sqlTemplateStrings["tomcat"] = TomcatCompiledSql
log.WithFields(fields).Info("Generated SQL template for Tomcat routes.")
}
}
Expand All @@ -136,7 +136,7 @@ func CmdStatus(c *cli.Context) error {
}

// Iterate over generated templates and obtain results
for framework, compiledSqlValue := range SQLTemplateStrings {
for framework, compiledSqlValue := range sqlTemplateStrings {
fields["Framework"] = framework
log.WithFields(fields).Info("Executing BigQuery SQL... this could take some time.")
rows, err := query(client, ctx, compiledSqlValue)
Expand Down
8 changes: 6 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion glide.yaml
Expand Up @@ -6,4 +6,5 @@ import:
- package: "github.com/sirupsen/logrus"
- package: "cloud.google.com/go/bigquery"
- package: "google.golang.org/api/iterator"
- package: "google.golang.org/api/option"
- package: "google.golang.org/api/option"
- package: "github.com/icrowley/fake"

0 comments on commit 3867486

Please sign in to comment.