Skip to content
Closed
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
68 changes: 35 additions & 33 deletions cmd/api/src/database/graphschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ type OpenGraphSchema interface {
GetEnvironments(ctx context.Context) ([]model.SchemaEnvironment, error)
DeleteEnvironment(ctx context.Context, environmentId int32) error

CreateSchemaRelationshipFinding(ctx context.Context, extensionId int32, relationshipKindId int32, environmentId int32, name string, displayName string) (model.SchemaRelationshipFinding, error)
GetSchemaRelationshipFindingById(ctx context.Context, findingId int32) (model.SchemaRelationshipFinding, error)
GetSchemaRelationshipFindingByName(ctx context.Context, name string) (model.SchemaRelationshipFinding, error)
DeleteSchemaRelationshipFinding(ctx context.Context, findingId int32) error
CreateSchemaFinding(ctx context.Context, findingType model.SchemaFindingType, extensionId, kindId, environmentId int32, name, displayName string) (model.SchemaFinding, error)
GetSchemaFindingsBySchemaExtensionId(ctx context.Context, extensionId int32) ([]model.SchemaFinding, error)
GetSchemaFindingById(ctx context.Context, findingId int32) (model.SchemaFinding, error)
GetSchemaFindingByName(ctx context.Context, name string) (model.SchemaFinding, error)
DeleteSchemaFinding(ctx context.Context, findingId int32) error

CreateRemediation(ctx context.Context, findingId int32, shortDescription string, longDescription string, shortRemediation string, longRemediation string) (model.Remediation, error)
GetRemediationByFindingId(ctx context.Context, findingId int32) (model.Remediation, error)
Expand Down Expand Up @@ -763,61 +764,61 @@ func (s *BloodhoundDB) DeleteEnvironment(ctx context.Context, environmentId int3
return nil
}

// CreateSchemaRelationshipFinding - creates a new schema relationship finding.
func (s *BloodhoundDB) CreateSchemaRelationshipFinding(ctx context.Context, extensionId int32, relationshipKindId int32, environmentId int32, name string, displayName string) (model.SchemaRelationshipFinding, error) {
var finding model.SchemaRelationshipFinding
// CreateSchemaFinding - creates a new schema relationship finding.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// CreateSchemaFinding - creates a new schema relationship finding.
// CreateSchemaFinding - creates a new schema finding.

func (s *BloodhoundDB) CreateSchemaFinding(ctx context.Context, findingType model.SchemaFindingType, extensionId, kindId, environmentId int32, name, displayName string) (model.SchemaFinding, error) {
var finding model.SchemaFinding

if result := s.db.WithContext(ctx).Raw(fmt.Sprintf(`
INSERT INTO %s (schema_extension_id, relationship_kind_id, environment_id, name, display_name, created_at)
VALUES (?, ?, ?, ?, ?, NOW())
RETURNING id, schema_extension_id, relationship_kind_id, environment_id, name, display_name, created_at`,
INSERT INTO %s (type, schema_extension_id, kind_id, environment_id, name, display_name, created_at)
VALUES (?, ?, ?, ?, ?, ?, NOW())
RETURNING id, schema_extension_id, kind_id, environment_id, name, display_name, created_at`,
finding.TableName()),
extensionId, relationshipKindId, environmentId, name, displayName).Scan(&finding); result.Error != nil {
findingType, extensionId, kindId, environmentId, name, displayName).Scan(&finding); result.Error != nil {
if strings.Contains(result.Error.Error(), DuplicateKeyValueErrorString) {
return model.SchemaRelationshipFinding{}, fmt.Errorf("%w: %s", model.ErrDuplicateSchemaRelationshipFindingName, name)
return model.SchemaFinding{}, fmt.Errorf("%w: %s", model.ErrDuplicateSchemaFindingName, name)
}
return model.SchemaRelationshipFinding{}, CheckError(result)
return model.SchemaFinding{}, CheckError(result)
}
return finding, nil
}

// GetSchemaRelationshipFindingById - retrieves a schema relationship finding by id.
func (s *BloodhoundDB) GetSchemaRelationshipFindingById(ctx context.Context, findingId int32) (model.SchemaRelationshipFinding, error) {
var finding model.SchemaRelationshipFinding
// GetSchemaFindingById - retrieves a schema relationship finding by id.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// GetSchemaFindingById - retrieves a schema relationship finding by id.
// GetSchemaFindingById - retrieves a schema finding by id.

func (s *BloodhoundDB) GetSchemaFindingById(ctx context.Context, findingId int32) (model.SchemaFinding, error) {
var finding model.SchemaFinding

if result := s.db.WithContext(ctx).Raw(fmt.Sprintf(`
SELECT id, schema_extension_id, relationship_kind_id, environment_id, name, display_name, created_at
SELECT id, type, schema_extension_id, kind_id, environment_id, name, display_name, created_at
FROM %s WHERE id = ?`,
finding.TableName()),
findingId).Scan(&finding); result.Error != nil {
return model.SchemaRelationshipFinding{}, CheckError(result)
return model.SchemaFinding{}, CheckError(result)
} else if result.RowsAffected == 0 {
return model.SchemaRelationshipFinding{}, ErrNotFound
return model.SchemaFinding{}, ErrNotFound
}

return finding, nil
}

// GetSchemaRelationshipFindingByName - retrieves a schema relationship finding by finding name.
func (s *BloodhoundDB) GetSchemaRelationshipFindingByName(ctx context.Context, name string) (model.SchemaRelationshipFinding, error) {
var finding model.SchemaRelationshipFinding
// GetSchemaFindingByName - retrieves a schema relationship finding by finding name.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// GetSchemaFindingByName - retrieves a schema relationship finding by finding name.
// GetSchemaFindingByName - retrieves a schema finding by finding name.

func (s *BloodhoundDB) GetSchemaFindingByName(ctx context.Context, name string) (model.SchemaFinding, error) {
var finding model.SchemaFinding

if result := s.db.WithContext(ctx).Raw(fmt.Sprintf(`
SELECT id, schema_extension_id, relationship_kind_id, environment_id, name, display_name, created_at
SELECT id, type, schema_extension_id, kind_id, environment_id, name, display_name, created_at
FROM %s WHERE name = ?`,
finding.TableName()),
name).Scan(&finding); result.Error != nil {
return model.SchemaRelationshipFinding{}, CheckError(result)
return model.SchemaFinding{}, CheckError(result)
} else if result.RowsAffected == 0 {
return model.SchemaRelationshipFinding{}, ErrNotFound
return model.SchemaFinding{}, ErrNotFound
}

return finding, nil
}

// DeleteSchemaRelationshipFinding - deletes a schema relationship finding by id.
func (s *BloodhoundDB) DeleteSchemaRelationshipFinding(ctx context.Context, findingId int32) error {
var finding model.SchemaRelationshipFinding
// DeleteSchemaFinding - deletes a schema relationship finding by id.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// DeleteSchemaFinding - deletes a schema relationship finding by id.
// DeleteSchemaFinding - deletes a schema finding by id.

func (s *BloodhoundDB) DeleteSchemaFinding(ctx context.Context, findingId int32) error {
var finding model.SchemaFinding

if result := s.db.WithContext(ctx).Exec(fmt.Sprintf(`DELETE FROM %s WHERE id = ?`, finding.TableName()), findingId); result.Error != nil {
return CheckError(result)
Expand All @@ -828,12 +829,13 @@ func (s *BloodhoundDB) DeleteSchemaRelationshipFinding(ctx context.Context, find
return nil
}

// GetSchemaRelationshipFindingsBySchemaExtensionId - returns all findings by extension id.
func (s *BloodhoundDB) GetSchemaRelationshipFindingsBySchemaExtensionId(ctx context.Context, extensionId int32) ([]model.SchemaRelationshipFinding, error) {
var findings = make([]model.SchemaRelationshipFinding, 0)
// GetSchemaFindingsBySchemaExtensionId - returns all findings by extension id.
func (s *BloodhoundDB) GetSchemaFindingsBySchemaExtensionId(ctx context.Context, extensionId int32) ([]model.SchemaFinding, error) {
var findings []model.SchemaFinding

if result := s.db.WithContext(ctx).Raw(fmt.Sprintf(`
SELECT id, schema_extension_id, relationship_kind_id, environment_id, name, display_name, created_at
FROM %s WHERE schema_extension_id = ? ORDER BY id`, model.SchemaRelationshipFinding{}.TableName()), extensionId).Scan(&findings); result.Error != nil {
SELECT id, type, schema_extension_id, kind_id, environment_id, name, display_name, created_at
FROM %s WHERE schema_extension_id = ? ORDER BY id`, model.SchemaFinding{}.TableName()), extensionId).Scan(&findings); result.Error != nil {
return findings, CheckError(result)
}
return findings, nil
Expand Down
Loading