Skip to content

Commit

Permalink
feat: New supported template flag
Browse files Browse the repository at this point in the history
Feature: make templates directory configurable
  • Loading branch information
0xblackbird authored Jun 17, 2024
2 parents dc0cc8a + 82af50f commit 0ba44d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ Usage of ./misconfig-mapper:
Only check for existing instances (and skip checks for potential security misconfigurations). (default "false")
-target string
Specify your target domain name or company/organization name: "intigriti.com" or "intigriti" (files are also accepted)
-templates string
Specify the templates folder location (default "./templates")
-timeout int
Specify a timeout for each request sent in milliseconds. (default 7000)
-update-templates
Expand Down
38 changes: 21 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ type RequestContext struct {
]
*/

var exePath, _ = os.Executable()
var exeDir string = filepath.Dir(exePath)
var templatesDir string = filepath.Join(exeDir, `./templates/`)
var servicesPath string = filepath.Join(exeDir, `./templates/services.json`)
var selectedServices []Service

var suffixes = []string{
Expand All @@ -108,7 +104,7 @@ var suffixes = []string{
"prod",
"internal",
"dev",
"developement",
"development",
"devops",
"logs",
"logging",
Expand All @@ -134,12 +130,12 @@ var suffixes = []string{
// Add more suffixes as needed
}

func loadTemplates() ([]Service, error) {
func loadTemplates(servicesPath string) ([]Service, error) {
var services []Service

file, err := os.Open(servicesPath)
if err != nil {
fmt.Println("ERROR: Failed opening file \"", servicesPath, "\":", err)
fmt.Printf("ERROR: Failed opening file '%s': %s\n", servicesPath, err)
return services, err
}
defer file.Close()
Expand All @@ -154,10 +150,10 @@ func loadTemplates() ([]Service, error) {
}

// Pull latest services from GitHub
func updateTemplates(path *string, update bool) *error {
func updateTemplates(templatesDir, servicesPath string, update bool) *error {
fmt.Printf("[+] Info: Pulling latest services and saving in %v\n", servicesPath)

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(7000)*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 7*time.Second)
defer cancel()

req, err := http.NewRequestWithContext(ctx, `GET`, `https://raw.githubusercontent.com/intigriti/misconfig-mapper/main/templates/services.json`, nil)
Expand All @@ -181,7 +177,7 @@ func updateTemplates(path *string, update bool) *error {

var s *os.File
if update {
s, err = os.OpenFile(*path, os.O_EXCL|os.O_WRONLY, 0644)
s, err = os.OpenFile(servicesPath, os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(1, err)
return &err
Expand All @@ -194,7 +190,7 @@ func updateTemplates(path *string, update bool) *error {
_ = os.Mkdir(templatesDir, 0700)

// Create "services.json" file
s, err = os.Create(*path)
s, err = os.Create(servicesPath)
if err != nil {
return &err
}
Expand Down Expand Up @@ -485,6 +481,7 @@ func main() {
timeoutFlag := flag.Int("timeout", 7000, "Specify a timeout for each request sent in milliseconds.")
maxRedirectsFlag := flag.Int("max-redirects", 5, "Specify the max amount of redirects to follow.")
listServicesFlag := flag.Bool("list-services", false, "Print all services with their associated IDs")
templatesPath := flag.String("templates", "./templates", "Specify the templates folder location")
updateServicesFlag := flag.Bool("update-templates", false, "Pull the latest templates & update your current services.json file")
verboseFlag := flag.Bool("verbose", false, "Print verbose messages")

Expand All @@ -506,30 +503,37 @@ func main() {
fd := int(os.Stdout.Fd())
width, _, _ := term.GetSize(fd)

// build templates directory
serviceFilePath := filepath.Join(*templatesPath, `services.json`)

if *updateServicesFlag {
// If "services.json" already exists, update the templates
update := isFile(servicesPath)
update := isFile(serviceFilePath)

err := updateTemplates(&servicesPath, update)
err := updateTemplates(*templatesPath, serviceFilePath, update)
if err != nil {
fmt.Printf("[-] Error: Failed to update templates! (%v)\n", *err)
os.Exit(1)
}

os.Exit(0)
}

services, err := loadTemplates()
services, err := loadTemplates(serviceFilePath)
if err != nil {
fmt.Println("[-] Error: Failed to load services!")

err := updateTemplates(&servicesPath, false)
err := updateTemplates(*templatesPath, serviceFilePath, false)
if err != nil {
fmt.Printf("[-] Error: Failed to pull latest services! (%v)\n", *err)
os.Exit(0)
os.Exit(1)
}

// Reload new templates
services, _ = loadTemplates()
if _, err2 := loadTemplates(serviceFilePath); err2 != nil {
fmt.Println("[-] Error: Failed to load services: " + err2.Error())
os.Exit(1)
}
}

if *listServicesFlag {
Expand Down

0 comments on commit 0ba44d3

Please sign in to comment.