-
Notifications
You must be signed in to change notification settings - Fork 175
First pass at a simple integration test #1353
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,23 +131,44 @@ func (s Resources) ProcessFileUpload(response http.ResponseWriter, request *http | |
defer request.Body.Close() | ||
} | ||
|
||
if !IsValidContentTypeForUpload(request.Header) { | ||
contentType, err := ParseUploadContentType(request.Header) | ||
if err != nil { | ||
api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, "Content type must be application/json or application/zip", request), response) | ||
} else if fileUploadJobID, err := strconv.Atoi(fileUploadJobIdString); err != nil { | ||
return | ||
} | ||
|
||
fileUploadJobID, err := strconv.Atoi(fileUploadJobIdString) | ||
if err != nil { | ||
api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, api.ErrorResponseDetailsIDMalformed, request), response) | ||
} else if ingestJob, err := ingest.GetIngestJobByID(request.Context(), s.DB, int64(fileUploadJobID)); err != nil { | ||
return | ||
} | ||
|
||
ingestJob, err := ingest.GetIngestJobByID(request.Context(), s.DB, int64(fileUploadJobID)) | ||
if err != nil { | ||
api.HandleDatabaseError(request, response, err) | ||
} else if fileName, fileType, err := ingest.SaveIngestFile(s.Config.TempDirectory(), request); errors.Is(err, ingest.ErrInvalidJSON) { | ||
return | ||
} | ||
|
||
fileName, fileType, err := ingest.SaveIngestFile(s.Config.TempDirectory(), contentType, request.Body) | ||
if errors.Is(err, ingest.ErrInvalidJSON) { | ||
api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, fmt.Sprintf("Error saving ingest file: %v", err), request), response) | ||
return | ||
} else if err != nil { | ||
api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusInternalServerError, fmt.Sprintf("Error saving ingest file: %v", err), request), response) | ||
} else if _, err = ingest.CreateIngestTask(request.Context(), s.DB, fileName, fileType, requestId, int64(fileUploadJobID)); err != nil { | ||
return | ||
} | ||
|
||
if _, err = ingest.CreateIngestTask(request.Context(), s.DB, fileName, fileType, requestId, int64(fileUploadJobID)); err != nil { | ||
api.HandleDatabaseError(request, response, err) | ||
} else if err = ingest.TouchIngestJobLastIngest(request.Context(), s.DB, ingestJob); err != nil { | ||
return | ||
} | ||
|
||
if err = ingest.TouchIngestJobLastIngest(request.Context(), s.DB, ingestJob); err != nil { | ||
api.HandleDatabaseError(request, response, err) | ||
} else { | ||
response.WriteHeader(http.StatusAccepted) | ||
return | ||
} | ||
|
||
response.WriteHeader(http.StatusAccepted) | ||
} | ||
|
||
func (s Resources) EndFileUploadJob(response http.ResponseWriter, request *http.Request) { | ||
|
@@ -172,13 +193,20 @@ func (s Resources) ListAcceptedFileUploadTypes(response http.ResponseWriter, req | |
api.WriteBasicResponse(request.Context(), ingestModel.AllowedFileUploadTypes, http.StatusOK, response) | ||
} | ||
|
||
func IsValidContentTypeForUpload(header http.Header) bool { | ||
func ParseUploadContentType(header http.Header) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be private, I'm not sure why the previous was public tbh, but we can fix that here. |
||
rawValue := header.Get(headers.ContentType.String()) | ||
if rawValue == "" { | ||
return false | ||
} else if parsed, _, err := mime.ParseMediaType(rawValue); err != nil { | ||
return false | ||
} else { | ||
return slices.Contains(ingestModel.AllowedFileUploadTypes, parsed) | ||
return "", fmt.Errorf("missing Content-Type header") | ||
} | ||
|
||
parsed, _, err := mime.ParseMediaType(rawValue) | ||
if err != nil { | ||
return "", fmt.Errorf("invalid Content-Type format: %w", err) | ||
} | ||
|
||
if !slices.Contains(ingestModel.AllowedFileUploadTypes, parsed) { | ||
return "", fmt.Errorf("unsupported Content-Type: %s", parsed) | ||
} | ||
|
||
return parsed, nil | ||
Comment on lines
+199
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning this to a pipeline style is preferred right now. There are no current edge cases in this logic |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ingest | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/specterops/bloodhound/src/model" | ||
) | ||
|
||
func TestIntegration_SaveIngestFile_JSON(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's name this |
||
// TODO: Make this | ||
body := io.NopCloser(bytes.NewBufferString(`{"meta":{"methods":46067,"type":"computers","count":0,"vers ion":6},"data":[]}`)) | ||
|
||
// TODO: Make this take it a FS? | ||
location, fileType, err := SaveIngestFile("/tmp", "application/json", body) | ||
if err != nil { | ||
t.Fatalf("SaveIngestFile failed: %v", err) | ||
} | ||
|
||
if fileType != model.FileTypeJson { | ||
t.Errorf("expected fileType %v, got %v", model.FileTypeJson, fileType) | ||
} | ||
|
||
info, statErr := os.Stat(location) | ||
if statErr != nil { | ||
t.Fatalf("output file not found: %v", statErr) | ||
} | ||
|
||
if info.Size() == 0 { | ||
t.Error("file saved is empty") | ||
} | ||
Comment on lines
+17
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have access to testify for |
||
|
||
// TODO: Make this unnecessary | ||
os.Remove(location) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like us to go back to using a pipeline approach (if/else chaining) for this set of requirements, primarily to keep it in line with other endpoints. It does come with the upside of not requiring any explicit returns.