-
Notifications
You must be signed in to change notification settings - Fork 282
feat(OpenGraph): API - Extension Bundle Upload Endpoint - BED-6723 #2161
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
Draft
LawsonWillard
wants to merge
72
commits into
main
Choose a base branch
from
BED-6723
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,863
−596
Draft
Changes from all commits
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
695ab80
update func name to align with others
LawsonWillard ed6bead
just generate
LawsonWillard e260134
Merge branch 'refs/heads/main' into BED-6791
LawsonWillard ff48cdc
wip
LawsonWillard b090df9
Merge branch 'main' into BED-6791
LawsonWillard 5bfbfbd
refactor node and edge functions and models to use graph in name to c…
LawsonWillard b9f301c
add ToSqlString for sort direction function
LawsonWillard 72de6cd
add GetSchemaNodeKinds function
LawsonWillard ecb4fc0
refactor SchemaNodeKind integration tests and add tests for GetSchema…
LawsonWillard ec18319
remove space
LawsonWillard 6dc3669
Merge branch 'main' into BED-6791
LawsonWillard 948c2cb
Merge branch 'main' into BED-6791
LawsonWillard 6b6f387
update GetGraphSchemaNodeKinds and tests to use new filtering/sorting
LawsonWillard 62ac318
update count function return value
LawsonWillard bc42d4e
update test
LawsonWillard 375300b
update test name
LawsonWillard 7ee0014
update comparison helper func
LawsonWillard b0d01a4
wip
LawsonWillard 2b78991
Merge branch 'main' into BED-6792
LawsonWillard 489b3ac
add GetGraphSchemaEdgeKinds and tests/mocks
LawsonWillard 4ffc208
Merge branch 'main' into BED-6792
LawsonWillard 8a8012e
rename function test
LawsonWillard 842f92e
update doc string
LawsonWillard 92ef455
refactor tests and add ability to update extension id in UpdateProper…
LawsonWillard 8f2fdaf
Add GetGraphSchemaProperties and corresponding tests
LawsonWillard 0a0c345
add new func to interface and generate mocks
LawsonWillard 7a95d7c
refactor by removing unnecesary structs in node kind integration tests
LawsonWillard d60e005
Merge branch 'main' into BED-6784
LawsonWillard b5a843c
add function that was missed in merge
LawsonWillard 0a01124
refactor edgeKind integration tests to not use unnecesary structs
LawsonWillard 66bc5eb
rabbit suggestions
LawsonWillard 28b7a60
Merge branch 'main' into BED-6784
LawsonWillard af11640
WIP Extensions Endpoint
LawsonWillard b159bb5
Merge branch 'main' into BED-6723
LawsonWillard c2a9ada
Merge branch 'refs/heads/BED-6784' into BED-6723
LawsonWillard 3117abc
wip
LawsonWillard 2e166ed
refactor diffing function for better clarity
LawsonWillard 6b9e6d6
refactor UpsertGraphSchemaExtension to reduce cyclomatic complexity
LawsonWillard f34f965
minor refactor to api resouce and api entry point func
LawsonWillard c68dcca
save generated test file
LawsonWillard eb043cb
Merge branch 'main' into BED-6723
LawsonWillard 9c3634c
ensure separation of service and API layer for opengraph
LawsonWillard 3167433
wip
LawsonWillard 76f9f81
Merge branch 'main' into BED-6723
LawsonWillard 23926ff
Merge branch 'refs/heads/main' into BED-6723
LawsonWillard 36b55d4
wip
LawsonWillard de9be78
Merge branch 'main' into BED-6723
LawsonWillard 0a37479
fix test
LawsonWillard cf27f95
Merge branch 'refs/heads/main' into BED-6723
LawsonWillard 6c5af88
add feature flag
LawsonWillard 583849f
add feature flag
LawsonWillard 1d4427f
update mocks
LawsonWillard 7a5ec49
create service layer mock
LawsonWillard 666b785
update api layer
LawsonWillard afa0fcd
update model
LawsonWillard 8fe8e2b
refactor open graph service layer and tests
LawsonWillard 8dc9362
update based on static analysis
LawsonWillard 07cf978
Merge branch 'main' into BED-6723
LawsonWillard dec52fc
push upsert logic down, tests are broken :/
LawsonWillard a1d26bd
Merge branch 'main' into BED-6723
LawsonWillard 88eafcb
Merge branch 'main' into BED-6723
LawsonWillard fa6c3c2
Merge branch 'refs/heads/main' into BED-6723
LawsonWillard 0419db4
add v8.6.0 migration and feature flag
LawsonWillard f575e01
migrate upsert logic to db layer and add initial tests
LawsonWillard 1e76403
add feature flag
LawsonWillard 3feca08
update entrypoint
LawsonWillard e58f808
update service layer logic, tests and mocks
LawsonWillard 5f17a95
update opengraph models
LawsonWillard 22f36b0
update api layer
LawsonWillard 09f0d9d
update v8.5.0
LawsonWillard a79bcf3
update upsert due to linting issues
LawsonWillard 25e73af
wip: add open api docs
LawsonWillard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright 2025 Specter Ops, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package v2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/specterops/bloodhound/cmd/api/src/api" | ||
| "github.com/specterops/bloodhound/cmd/api/src/auth" | ||
| ctx2 "github.com/specterops/bloodhound/cmd/api/src/ctx" | ||
| "github.com/specterops/bloodhound/cmd/api/src/model" | ||
| "github.com/specterops/bloodhound/cmd/api/src/model/appcfg" | ||
| "github.com/specterops/bloodhound/cmd/api/src/model/ingest" | ||
| bhUtils "github.com/specterops/bloodhound/cmd/api/src/utils" | ||
| "github.com/specterops/bloodhound/packages/go/headers" | ||
| "github.com/specterops/bloodhound/packages/go/mediatypes" | ||
| ) | ||
|
|
||
| //go:generate go run go.uber.org/mock/mockgen -copyright_file ../../../../../LICENSE.header -destination=./mocks/graphschemaextensions.go -package=mocks . OpenGraphSchemaService | ||
|
|
||
| type OpenGraphSchemaService interface { | ||
| UpsertOpenGraphExtension(ctx context.Context, graphSchema model.GraphSchema) (bool, error) | ||
| } | ||
|
|
||
| func (s Resources) OpenGraphSchemaIngest(response http.ResponseWriter, request *http.Request) { | ||
| var ( | ||
| ctx = request.Context() | ||
| err error | ||
| flag appcfg.FeatureFlag | ||
|
|
||
| updated bool | ||
|
|
||
| extractExtensionData func(file io.Reader) (model.GraphSchema, error) | ||
| graphSchemaPayload model.GraphSchema | ||
| ) | ||
|
|
||
| // TODO: what to return if feature flag is not enabled | ||
| if flag, err = s.DB.GetFlagByKey(ctx, appcfg.FeatureOpenGraphExtensionManagement); err != nil { | ||
| api.HandleDatabaseError(request, response, err) | ||
| } else if !flag.Enabled { | ||
| response.WriteHeader(http.StatusNotFound) | ||
| } else if user, isUser := auth.GetUserFromAuthCtx(ctx2.FromRequest(request).AuthCtx); !isUser { | ||
| api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, "No associated "+ | ||
| "user found", request), response) | ||
| } else if !user.Roles.Has(model.Role{Name: auth.RoleAdministrator}) { | ||
| api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusForbidden, "user does not "+ | ||
| "have sufficient permissions to create or update an extension", request), response) | ||
| } else if request.Body == nil { | ||
| api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, "open graph "+ | ||
| "extension payload cannot be empty", request), response) | ||
| } else { | ||
| request.Body = http.MaxBytesReader(response, request.Body, api.DefaultAPIPayloadReadLimitBytes) | ||
| defer request.Body.Close() | ||
| switch { | ||
| case bhUtils.HeaderMatches(request.Header, headers.ContentType.String(), mediatypes.ApplicationJson.String()): | ||
| extractExtensionData = extractExtensionDataFromJSON | ||
| case bhUtils.HeaderMatches(request.Header, headers.ContentType.String(), ingest.AllowedZipFileUploadTypes...): | ||
| fallthrough | ||
| // extractExtensionData = extractExtensionDataFromZipFile - will be needed for a future | ||
| default: | ||
| api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusUnsupportedMediaType, | ||
| fmt.Sprintf("%s; Content type must be application/json", | ||
| fmt.Errorf("invalid content-type: %s", request.Header[headers.ContentType.String()])), request), response) | ||
| return | ||
| } | ||
|
|
||
| if graphSchemaPayload, err = extractExtensionData(request.Body); err != nil { | ||
| api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, err.Error(), request), response) | ||
| return | ||
| } | ||
|
|
||
| if updated, err = s.openGraphSchemaService.UpsertOpenGraphExtension(ctx, graphSchemaPayload); err != nil { | ||
| switch { | ||
| // TODO: more error types (ex: validation) | ||
| default: | ||
| api.WriteErrorResponse(ctx, api.BuildErrorResponse(http.StatusInternalServerError, fmt.Sprintf("unable to update graph schema: %v", err), request), response) | ||
| return | ||
| } | ||
| } else if updated { | ||
| response.WriteHeader(http.StatusOK) | ||
| } else { | ||
| response.WriteHeader(http.StatusCreated) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // extractExtensionDataFromJSON - extracts a model.GraphSchema from the incoming payload. Will return an error if there | ||
| // are any extra fields or if the decoder fails to decode the payload. | ||
| func extractExtensionDataFromJSON(payload io.Reader) (model.GraphSchema, error) { | ||
| var ( | ||
| err error | ||
| decoder = json.NewDecoder(payload) | ||
| graphSchema model.GraphSchema | ||
| ) | ||
| decoder.DisallowUnknownFields() | ||
| if err = decoder.Decode(&graphSchema); err != nil { | ||
| return graphSchema, err | ||
| } | ||
| return graphSchema, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As this is configuration related, I think it might be a good idea to require a permission related to
"write" for this endpoint
Uh oh!
There was an error while loading. Please reload this page.
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.
Not sure which permission set would be best, this will be admin only, do you think its worth creating a new set of admin permissions for graph schema management?