Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

portable filepath operations; misc tweaks #14

Merged
merged 1 commit into from Aug 2, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

portable filepath operations; misc tweaks

  • Loading branch information
kjk committed Aug 2, 2019
commit 70786f8011abdfec53df0bf244f1560ad226fa69
@@ -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 {
@@ -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
}
@@ -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 {
@@ -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 {
@@ -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()

@@ -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
}
@@ -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))
}
}

@@ -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
}
@@ -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 {
@@ -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
}
@@ -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
}
@@ -2,6 +2,7 @@ package client

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
@@ -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
@@ -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
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.