Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package client

import (
"bytes"
"cloud.google.com/go/firestore"
"context"
"encoding/json"
"errors"
"fmt"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"time"

"cloud.google.com/go/firestore"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)

func (c *fuzzitClient) ReAuthenticate(force bool) error {
Expand Down Expand Up @@ -60,7 +61,7 @@ func (c *fuzzitClient) ReAuthenticate(force bool) error {
r, err = c.httpClient.Post(
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=AIzaSyCs_Sm1VOKZwJZmTXdOCvs1wyn91vYMNSY",
"application/json",
bytes.NewBuffer([]byte(fmt.Sprintf(`{"token": "%s", "returnSecureToken": true}`, c.CustomToken))))
bytes.NewBufferString(fmt.Sprintf(`{"token": "%s", "returnSecureToken": true}`, c.CustomToken)))
if err != nil {
return err
}
Expand Down
52 changes: 25 additions & 27 deletions client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package client

import (
"bytes"
"cloud.google.com/go/firestore"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/mholt/archiver"
"google.golang.org/api/iterator"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"

"cloud.google.com/go/firestore"
"github.com/google/uuid"
"github.com/mholt/archiver"
"google.golang.org/api/iterator"
)

func (c *fuzzitClient) GetResource(resource string) error {
Expand All @@ -25,19 +27,19 @@ func (c *fuzzitClient) GetResource(resource string) error {

ctx := context.Background()
rootColRef := "orgs/" + c.Org + "/"
r := rootColRef + resource
if (len(strings.Split(resource, "/")) % 2) == 0 {
r := rootColRef + resource
docRef := c.firestoreClient.Doc(rootColRef + resource)
docRef := c.firestoreClient.Doc(r)
if docRef == nil {
return fmt.Errorf("invalid resource %s", r)
}
docsnap, err := docRef.Get(ctx)
if !docsnap.Exists() {
return fmt.Errorf("resource %s doesn't exist", resource)
}
if err != nil {
return err
}
if !docsnap.Exists() {
return fmt.Errorf("resource %s doesn't exist", resource)
}

jsonString, err := json.MarshalIndent(docsnap.Data(), "", " ")
if err != nil {
Expand All @@ -46,7 +48,7 @@ func (c *fuzzitClient) GetResource(resource string) error {
fmt.Println(string(jsonString))
return nil
} else {
iter := c.firestoreClient.Collection(rootColRef + resource).Documents(ctx)
iter := c.firestoreClient.Collection(r).Documents(ctx)
querySize := 0
defer iter.Stop()

Expand Down Expand Up @@ -77,8 +79,7 @@ func (c *fuzzitClient) GetResource(resource string) error {
func (c *fuzzitClient) CreateTarget(targetConfig Target, seedPath string) (*firestore.DocumentRef, error) {
ctx := context.Background()
collectionRef := c.firestoreClient.Collection("orgs/" + c.Org + "/targets")
doc, _, err := collectionRef.Add(ctx,
targetConfig)
doc, _, err := collectionRef.Add(ctx, targetConfig)
if err != nil {
return nil, err
}
Expand All @@ -97,8 +98,8 @@ func (c *fuzzitClient) CreateJob(jobConfig Job, files []string) (*firestore.Docu
ctx := context.Background()

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

Expand All @@ -109,22 +110,21 @@ func (c *fuzzitClient) CreateJob(jobConfig Job, files []string) (*firestore.Docu
fullJob.OrgId = c.Org
fullJob.Namespace = c.Namespace
fullJob.Status = "in progress"
doc, _, err := collectionRef.Add(ctx,
fullJob)
doc, _, err := collectionRef.Add(ctx, fullJob)
if err != nil {
return nil, err
}
log.Println("Created new job ", doc.ID)

fuzzerPath := files[0]
splits := strings.Split(fuzzerPath, "/")
filename := splits[len(splits)-1]
filename := filepath.Base(fuzzerPath)
if !strings.HasSuffix(filename, ".tar.gz") {
tmpDir, err := ioutil.TempDir("", "fuzzit")
if err != nil {
return nil, err
}
_, err = copyFile(tmpDir+"/fuzzer", fuzzerPath)
dstPath := filepath.Join(tmpDir, "fuzzer")
_, err = copyFile(dstPath, fuzzerPath)
if err != nil {
return nil, err
}
Expand All @@ -133,9 +133,9 @@ func (c *fuzzitClient) CreateJob(jobConfig Job, files []string) (*firestore.Docu
if err != nil {
return nil, err
}
filesToArchive := append([]string{tmpDir + "/fuzzer"}, files[1:]...)
filesToArchive := append([]string{dstPath}, files[1:]...)

tmpfile := os.TempDir() + "/" + prefix.String() + ".tar.gz"
tmpfile := filepath.Join(os.TempDir(), prefix.String()+".tar.gz")
z := archiver.NewTarGz()
err = z.Archive(filesToArchive, tmpfile)
if err != nil {
Expand All @@ -150,10 +150,10 @@ func (c *fuzzitClient) CreateJob(jobConfig Job, files []string) (*firestore.Docu
return nil, err
}

jsonStr := []byte(fmt.Sprintf(`{"data": {"org_id": "%s", "target_id": "%s", "job_id": "%s"}}`, c.Org, jobConfig.TargetId, doc.ID))
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.NewBuffer(jsonStr))
bytes.NewBufferString(jsonStr))
if err != nil {
return nil, err
}
Expand All @@ -164,16 +164,14 @@ func (c *fuzzitClient) CreateJob(jobConfig Job, files []string) (*firestore.Docu
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)
}
bodyString := string(bodyBytes)
defer res.Body.Close()
return nil, fmt.Errorf(bodyString)
return nil, errors.New(string(bodyBytes))
}
fmt.Printf("Job %s started succesfully\n", doc.ID)
defer res.Body.Close()
return doc, nil
}
8 changes: 4 additions & 4 deletions client/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand All @@ -26,7 +27,7 @@ func (c *fuzzitClient) getStorageLink(storagePath string) (string, error) {
return "", fmt.Errorf("API Key is not valid")
}

res := storageLinkResponse{}
var res storageLinkResponse
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return "", err
Expand Down Expand Up @@ -57,14 +58,13 @@ func (c *fuzzitClient) uploadFile(filePath string, storagePath string, contentTy
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
return fmt.Errorf(bodyString)
return errors.New(string(bodyBytes))
}
defer res.Body.Close()
return nil
}