Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
bugfixes in job creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevgeny Pats committed Aug 4, 2019
1 parent ca5e96b commit 1f30c2f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 39 deletions.
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type job struct {
Namespace string `firestore:"namespace"`
StartedAt time.Time `firestore:"started_at,serverTimestamp"`
OrgId string `firestore:"org_id"`
V2 bool `firestore:"v2"`
Job
}

Expand Down
48 changes: 12 additions & 36 deletions client/commands.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package client

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -97,24 +94,16 @@ func (c *fuzzitClient) CreateTarget(targetConfig Target, seedPath string) (*fire
func (c *fuzzitClient) CreateJob(jobConfig Job, files []string) (*firestore.DocumentRef, error) {
ctx := context.Background()

for _, filename := range files {
if st, err := os.Stat(filename); err != nil || !st.Mode().IsRegular() {
return nil, errors.New(fmt.Sprintf("File %s doesn't exist...", filename))
}
}

collectionRef := c.firestoreClient.Collection("orgs/" + c.Org + "/targets/" + jobConfig.TargetId + "/jobs")
fullJob := job{}
fullJob.Job = jobConfig
fullJob.Completed = 0
fullJob.OrgId = c.Org
fullJob.Namespace = c.Namespace
fullJob.Status = "in progress"
doc, _, err := collectionRef.Add(ctx, fullJob)
if err != nil {
return nil, err
}
log.Println("Created new job ", doc.ID)
fullJob.V2 = true

jobRef := collectionRef.NewDoc()

fuzzerPath := files[0]
filename := filepath.Base(fuzzerPath)
Expand Down Expand Up @@ -144,34 +133,21 @@ func (c *fuzzitClient) CreateJob(jobConfig Job, files []string) (*firestore.Docu
fuzzerPath = tmpfile
}

storagePath := fmt.Sprintf("orgs/%s/targets/%s/jobs/%s/fuzzer", c.Org, jobConfig.TargetId, doc.ID)
err = c.uploadFile(fuzzerPath, storagePath, "application/gzip", "fuzzer.tar.gz")
storagePath := fmt.Sprintf("orgs/%s/targets/%s/jobs/%s/fuzzer", c.Org, jobConfig.TargetId, jobRef.ID)
log.Println("Uploading fuzzer...")
err := c.uploadFile(fuzzerPath, storagePath, "application/gzip", "fuzzer.tar.gz")
if err != nil {
return nil, err
}

jsonStr := fmt.Sprintf(`{"data": {"org_id": "%s", "target_id": "%s", "job_id": "%s"}}`, c.Org, jobConfig.TargetId, doc.ID)
req, err := http.NewRequest("POST",
"https://us-central1-fuzzit-b5fbf.cloudfunctions.net/startJob",
bytes.NewBufferString(jsonStr))
log.Println("Starting job")
_, err = jobRef.Set(ctx, fullJob)
if err != nil {
log.Printf("Please check that the target '%s' exists and you have sufficiant permissions",
jobConfig.TargetId)
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.IdToken)
req.Header.Set("Content-Type", "application/json")

res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
return nil, errors.New(string(bodyBytes))
}
fmt.Printf("Job %s started succesfully\n", doc.ID)
return doc, nil
log.Printf("Job %s started succesfully\n", jobRef.ID)
return jobRef, nil
}
1 change: 0 additions & 1 deletion client/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (c *fuzzitClient) uploadFile(filePath string, storagePath string, contentTy
return err
}

fmt.Println("uploading fuzzer...")
req, err := http.NewRequest("PUT", storageLink, data)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var jobCmd = &cobra.Command{
log.Fatalf("--type should be either fuzzing or sanity. Recieved: %s", newJob.Type)
}

log.Println("Creating job...")
log.Println("Creating job")
c, err := client.LoadFuzzitFromCache()
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var cfgFile string
var rootCmd = &cobra.Command{
Use: "Fuzzit",
Short: "Continuous fuzzing made simple CLI",
Version: "2.1.0",
Version: "2.2.0",
}

func Execute() {
Expand Down

0 comments on commit 1f30c2f

Please sign in to comment.