services

package
v0.0.0-...-0fdad32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 32 Imported by: 0

Documentation

Overview

Package services handles the logic behind all of the Web/API actions

Index

Constants

This section is empty.

Variables

View Source
var EvidenceCountForAllOperations string = fmt.Sprintf(`
	%s
	GROUP BY 
		operation_id`, getCountsFromEvidence)
View Source
var GetTopContributorsForEachOperation string = fmt.Sprintf(`
	SELECT
		t1.*
	FROM (%s
	GROUP BY
		operation_id,
		users.id) t1
		LEFT JOIN (
			%s	
			GROUP BY
				operation_id,
				users.id) t2 ON t1.operation_id = t2.operation_id
		AND t1.count < t2.count
	WHERE
		t2.count IS NULL`, getDataFromEvidence, getDataFromEvidence)

Functions

func AddUsersToGroup

func AddUsersToGroup(tx *database.Transactable, userSlugs []string, groupID int64) error

func CreateAPIKey

func CreateAPIKey(ctx context.Context, db *database.Connection, userSlug string) (*dtos.APIKey, error)

func CreateDefaultTag

func CreateDefaultTag(ctx context.Context, db *database.Connection, i CreateDefaultTagInput) (*dtos.DefaultTag, error)

CreateDefaultTag creates a single tag in the default_tags table. Admin only.

func CreateEvidence

func CreateEvidence(ctx context.Context, db *database.Connection, contentStore contentstore.Store, i CreateEvidenceInput) (*dtos.Evidence, error)

func CreateFindingCategory

func CreateFindingCategory(ctx context.Context, db *database.Connection, newCategory string) (*dtos.FindingCategory, error)

CreateFindingCategory adds a new finding category to the finding_categories table

func CreateHeadlessUser

func CreateHeadlessUser(ctx context.Context, db *database.Connection, i CreateUserInput) (*dtos.CreateUserOutput, error)

CreateHeadlessUser is really just CreateUser. The difference here is that _headless_ users will not have authentication, and instead rely on user-impersonation and API keys for access.

func CreateQuery

func CreateQuery(ctx context.Context, db *database.Connection, i CreateQueryInput) (*dtos.Query, error)

CreateQuery inserts a new query into the database

func CreateTag

func CreateTag(ctx context.Context, db *database.Connection, i CreateTagInput) (*dtos.Tag, error)

func CreateUser

CreateUser generates an entry in the users table in the database. No more is done here, but it is expected that the caller will, at a minimum, also want to create an entry in the authentication tables, so that the user can actually log in.

Note: CreateUserInput.Slug is a _suggestion_, and it may be altered to ensure uniqueness.

Returns a structure containing both the true slug (i.e. what it was mangled to, if it was infact mangled), plus the associated user_id value

func DeleteAuthScheme

func DeleteAuthScheme(ctx context.Context, db *database.Connection, i DeleteAuthSchemeInput) error

DeleteAuthScheme removes a user's association with a particular auth_scheme. This function applies for both admin related actions and plain user actions. If UserSlug is not provided, this will apply to the requesting user. If it is provided, then this triggers admin validation, and will apply to the provided user matching the given slug.

func DeleteAuthSchemeUsers

func DeleteAuthSchemeUsers(ctx context.Context, db *database.Connection, schemeCode string) error

DeleteAuthSchemeUsers removes/unlinks all users from a provided scheme

func DeleteDefaultTag

func DeleteDefaultTag(ctx context.Context, db *database.Connection, i DeleteDefaultTagInput) error

DeleteDefaultTag removes a single tag in the default_tags table by the tag id. Admin only.

func DeleteEvidence

func DeleteEvidence(ctx context.Context, db *database.Connection, contentStore contentstore.Store, i DeleteEvidenceInput) error

func DeleteFindingCategory

func DeleteFindingCategory(ctx context.Context, db *database.Connection, i DeleteFindingCategoryInput) error

DeleteFindingCategory removes an entry from the finding_categories table

func DeleteGlobalVar

func DeleteGlobalVar(ctx context.Context, db *database.Connection, name string) error

func DeleteOperation

func DeleteOperation(ctx context.Context, db *database.Connection, contentStore contentstore.Store, slug string) error

func DeleteOperationVar

func DeleteOperationVar(ctx context.Context, db *database.Connection, varSlug string, operationSlug string) error

func DeleteQuery

func DeleteQuery(ctx context.Context, db *database.Connection, i DeleteQueryInput) error

DeleteQuery removes a saved query for the given operation

func DeleteSessionsForUserSlug

func DeleteSessionsForUserSlug(ctx context.Context, db *database.Connection, userSlug string) error

DeleteSessionsForUserSlug finds all existing sessions for a given user, then removes them, effectively logging the user out of the service.

func DeleteTag

func DeleteTag(ctx context.Context, db *database.Connection, i DeleteTagInput) error

DeleteTag removes a tag and untags all evidence with the tag

func DeleteUser

func DeleteUser(ctx context.Context, db *database.Connection, slug string) error

DeleteUser provides the ability for a super admin to remove a user from the system. Doing so removes access only. Evidence and other contributions remain. Note that users are not able to delete their own accounts to prevent accidents. Also note that once a user has been deleted, they cannot be restored.

func DeleteUserGroup

func DeleteUserGroup(ctx context.Context, db *database.Connection, slug string) error

func ListAPIKeys

func ListAPIKeys(ctx context.Context, db *database.Connection, userSlug string) ([]*dtos.APIKey, error)

func ListActiveServices

func ListActiveServices(ctx context.Context, db *database.Connection) ([]*dtos.ActiveServiceWorker, error)

func ListAuthDetails

func ListAuthDetails(ctx context.Context, db *database.Connection, supportedAuthSchemes *[]dtos.SupportedAuthScheme) ([]*dtos.DetailedAuthenticationInfo, error)

func ListDefaultTags

func ListDefaultTags(ctx context.Context, db *database.Connection) ([]*dtos.DefaultTag, error)

ListDefaultTags provides a list of all of the tags in the default_tags table. Admin only.

func ListEvidenceCreatorsForOperation

func ListEvidenceCreatorsForOperation(ctx context.Context, db *database.Connection, i ListEvidenceCreatorsForOperationInput) ([]*dtos.User, error)

ListEvidenceCreatorsForOperation returns a list of all users that have (ever) created a piece of evidence for a given operation slug. Note that this won't return users that _had_ created evidence that has since been deleted

func ListEvidenceForFinding

func ListEvidenceForFinding(ctx context.Context, db *database.Connection, contentStore contentstore.Store, i ListEvidenceForFindingInput) ([]dtos.Evidence, error)

func ListEvidenceForOperation

func ListEvidenceForOperation(ctx context.Context, db *database.Connection, contentStore contentstore.Store, i ListEvidenceForOperationInput) ([]*dtos.Evidence, error)

ListEvidenceForOperation retrieves all evidence for a particular operation id matching a particular set of filters (e.g. tag:some_tag)

func ListFindingCategories

func ListFindingCategories(ctx context.Context, db *database.Connection, includeDeleted bool) (interface{}, error)

ListFindingCategories retrieves a list of all of the finding categories present in the database.

func ListGlobalVars

func ListGlobalVars(ctx context.Context, db *database.Connection) ([]*dtos.GlobalVar, error)

func ListOperationVars

func ListOperationVars(ctx context.Context, db *database.Connection, operationSlug string) ([]*dtos.OperationVar, error)

func ListOperations

func ListOperations(ctx context.Context, db *database.Connection) ([]*dtos.Operation, error)

ListOperations retrieves a list of all operations that the contextual user can see

func ListOperationsForAdmin

func ListOperationsForAdmin(ctx context.Context, db *database.Connection) ([]*dtos.Operation, error)

ListOperationsForAdmin is a specialized version of ListOperations where no operations are filtered For use in admin screens only

func ListQueriesForOperation

func ListQueriesForOperation(ctx context.Context, db *database.Connection, operationSlug string) ([]*dtos.Query, error)

ListQueriesForOperation retrieves all saved queries for a given operation id

func ListServiceWorker

func ListServiceWorker(ctx context.Context, db *database.Connection) ([]*dtos.ServiceWorker, error)

func ListTagDifference

ListTagDifference determines which tag values are common between two operations. This is done via relative comparison. That is, all of the tags in the "source" are compared against the "destination" returning only tags that are common, and tags that are in the source, but not in the destination. The opposite list (tags that exist in the destination, but not the source) is not generated.

func ListUserGroups

lists all user groups that can be added to an operation

func ListUserGroupsForAdmin

Lists all usergroups for an admin, with pagination

func ListUserGroupsForOperation

Lists all user groups for an operation; op admins and sys admins can view

func ListUsers

func ListUsers(ctx context.Context, db *database.Connection, i ListUsersInput) ([]*dtos.User, error)

func ListUsersForAdmin

ListUsersForAdmin retreives standard User (public) details, and aguments with some particular fields meant for admin review. For use in admin views only.

func LookupGlobalVar

func LookupGlobalVar(db *database.Connection, name string) (*models.GlobalVar, error)

func LookupOperationVar

func LookupOperationVar(db *database.Connection, varSlug string) (*models.OperationVar, error)

func MergeDefaultTags

func MergeDefaultTags(ctx context.Context, db *database.Connection, i []CreateDefaultTagInput) error

func ReadOperation

func ReadOperation(ctx context.Context, db *database.Connection, operationSlug string) (*dtos.Operation, error)

func ReadUser

func ReadUser(ctx context.Context, db *database.Connection, userSlug string, supportedAuthSchemes *[]dtos.SupportedAuthScheme) (*dtos.UserOwnView, error)

ReadUser retrieves a detailed view of a user. This is separate from the data retriving by listing users, or reading another user's profile (when not an admin)

func SanitizeSlug

func SanitizeSlug(slug string) string

SanitizeOperationSlug removes objectionable characters from a slug and returns the new slug. Current logic: only allow alphanumeric characters and hyphen, with hypen excluded at the start and end

func SelfOrSlugToUserID

func SelfOrSlugToUserID(ctx context.Context, db *database.Connection, slug string) (int64, error)

func SetFavoriteOperation

func SetFavoriteOperation(ctx context.Context, db *database.Connection, i SetFavoriteInput) error

func SetUserFlags

func SetUserFlags(ctx context.Context, db *database.Connection, i SetUserFlagsInput) error

SetUserFlags updates flags for the indicated user, namely: admin and disabled. Then removes all sessions for that user (logging them out)

NOTE: The flag is to _disable_ the user, which prevents access. To enable a user, set Disabled=false

func SortUsersInToGroups

func SortUsersInToGroups(slugMap SlugMap) ([]dtos.UserGroupAdminView, error)

func TestServiceWorker

func TestServiceWorker(ctx context.Context, db *database.Connection, serviceWorkerID int64) (*dtos.ServiceWorkerTestOutput, error)

func UpdateEvidence

func UpdateEvidence(ctx context.Context, db *database.Connection, contentStore contentstore.Store, i UpdateEvidenceInput) error

func UpdateFindingCategory

func UpdateFindingCategory(ctx context.Context, db *database.Connection, i UpdateFindingCategoryInput) error

UpdateFindingCategory updates the specified entry in the finding_categories table

func UpdateQuery

func UpdateQuery(ctx context.Context, db *database.Connection, i UpdateQueryInput) error

UpdateQuery modifies a query for the given operation

func UpdateTag

func UpdateTag(ctx context.Context, db *database.Connection, i UpdateTagInput) error

UpdateTag updates a tag's name and color

func UpsertQuery

func UpsertQuery(ctx context.Context, db *database.Connection, i UpsertQueryInput) (*dtos.Query, error)

Types

type AddEvidenceToFindingInput

type AddEvidenceToFindingInput struct {
	OperationSlug    string
	FindingUUID      string
	EvidenceToAdd    []string
	EvidenceToRemove []string
}

type BatchRunServiceWorkerInput

type BatchRunServiceWorkerInput struct {
	OperationSlug string
	EvidenceUUIDs []string
	WorkerNames   []string
}

type CreateDefaultTagInput

type CreateDefaultTagInput struct {
	Name        string
	ColorName   string
	Description *string
}

type CreateEvidenceInput

type CreateEvidenceInput struct {
	OperatorID    int64
	OperationSlug string
	Description   string
	Content       io.Reader
	ContentType   string
	TagIDs        []int64
	OccurredAt    time.Time
	AdjustedAt    *time.Time
}

type CreateFindingInput

type CreateFindingInput struct {
	OperationSlug string
	Category      string
	Title         string
	Description   string
}

type CreateGlobalVarInput

type CreateGlobalVarInput struct {
	Name    string
	OwnerID int64
	Value   string
}

type CreateOperationInput

type CreateOperationInput struct {
	Slug    string
	OwnerID int64
	Name    string
}

type CreateOperationVarInput

type CreateOperationVarInput struct {
	OperationSlug string
	Name          string
	VarSlug       string
	Value         string
}

type CreateQueryInput

type CreateQueryInput struct {
	OperationSlug string
	Name          string
	Query         string
	Type          string
}

CreateQueryInput provides a structure that holds the values needed to generate a new saved query

type CreateServiceWorkerInput

type CreateServiceWorkerInput struct {
	Name   string
	Config string
}

type CreateTagInput

type CreateTagInput struct {
	Name          string
	ColorName     string
	OperationSlug string
	Description   *string
}

type CreateUserGroupInput

type CreateUserGroupInput struct {
	Name      string
	Slug      string
	UserSlugs []string
}

type CreateUserInput

type CreateUserInput struct {
	FirstName string
	LastName  string
	Slug      string
	Email     string
	Headless  bool
}

type DeleteAPIKeyInput

type DeleteAPIKeyInput struct {
	AccessKey string
	UserSlug  string
}

type DeleteAuthSchemeInput

type DeleteAuthSchemeInput struct {
	UserSlug   string
	SchemeName string
}

type DeleteDefaultTagInput

type DeleteDefaultTagInput struct {
	ID int64
}

type DeleteEvidenceInput

type DeleteEvidenceInput struct {
	OperationSlug            string
	EvidenceUUID             string
	DeleteAssociatedFindings bool
}

type DeleteFindingCategoryInput

type DeleteFindingCategoryInput struct {
	FindingCategoryID int64
	DoDelete          bool
}

type DeleteFindingInput

type DeleteFindingInput struct {
	OperationSlug string
	FindingUUID   string
}

type DeleteGlobalVarInput

type DeleteGlobalVarInput struct {
	Name string
}

type DeleteOperationVarInput

type DeleteOperationVarInput struct {
	Name string
}

type DeleteQueryInput

type DeleteQueryInput struct {
	OperationSlug string
	ID            int64
}

type DeleteServiceWorkerInput

type DeleteServiceWorkerInput struct {
	ID       int64
	DoDelete bool
}

type DeleteTagInput

type DeleteTagInput struct {
	ID            int64
	OperationSlug string
}

type EditEvidenceMetadataInput

type EditEvidenceMetadataInput struct {
	OperationSlug string
	EvidenceUUID  string
	Source        string
	Body          string
}

type EvidenceCountWithID

type EvidenceCountWithID struct {
	dtos.EvidenceCount
	OperationID int64 `db:"operation_id" json:"operationId"`
}

type ExpandedTagUsageData

type ExpandedTagUsageData struct {
	TagID      int64
	TagName    string
	ColorName  string
	UsageDates []time.Time
}

type ListEvidenceCreatorsForOperationInput

type ListEvidenceCreatorsForOperationInput struct {
	OperationSlug string
}

type ListEvidenceForFindingInput

type ListEvidenceForFindingInput struct {
	OperationSlug string
	FindingUUID   string
}

type ListEvidenceForOperationInput

type ListEvidenceForOperationInput struct {
	OperationSlug string
	Filters       helpers.TimelineFilters
}

type ListFindingsForOperationInput

type ListFindingsForOperationInput struct {
	OperationSlug string
	Filters       helpers.TimelineFilters
}

type ListTagDifferenceForEvidenceInput

type ListTagDifferenceForEvidenceInput struct {
	ListTagsDifferenceInput
	SourceEvidenceUUID string
}

type ListTagsDifferenceInput

type ListTagsDifferenceInput struct {
	SourceOperationSlug      string
	DestinationOperationSlug string
}

type ListTagsForOperationInput

type ListTagsForOperationInput struct {
	OperationSlug string
}

type ListUserGroupsForAdminInput

type ListUserGroupsForAdminInput struct {
	UserGroupFilter
	IncludeDeleted bool
}

type ListUserGroupsForOperationInput

type ListUserGroupsForOperationInput struct {
	UserGroupFilter
	OperationSlug string
}

type ListUserGroupsInput

type ListUserGroupsInput struct {
	Query          string
	IncludeDeleted bool
	OperationSlug  string
}

type ListUsersForAdminInput

type ListUsersForAdminInput struct {
	UserFilter
	Pagination
	IncludeDeleted bool
}

type ListUsersForOperationInput

type ListUsersForOperationInput struct {
	Pagination
	UserFilter
	OperationSlug string
}

type ListUsersInput

type ListUsersInput struct {
	Query          string
	IncludeDeleted bool
}

type ModifyUserGroupInput

type ModifyUserGroupInput struct {
	Name          string
	Slug          string
	UsersToAdd    []string
	UsersToRemove []string
}

type MoveEvidenceInput

type MoveEvidenceInput struct {
	SourceOperationSlug string
	EvidenceUUID        string
	TargetOperationSlug string
}

type OperationWithID

type OperationWithID struct {
	Op *dtos.Operation
	ID int64
}

type Pagination

type Pagination struct {
	PageSize int64
	Page     int64

	TotalCount int64
	// contains filtered or unexported fields
}

func ParseRequestQueryPagination

func ParseRequestQueryPagination(dr dissectors.DissectedRequest, defaultMaxItems int64) Pagination

ParseRequestQueryPagination retreives the part of the request set aside for pagination Note that this retrieves the values and hopes for the best. Since this uses a DissectedRequest, it is the caller of the function to ensure no error occurred _after_ this has been called.

func (*Pagination) Select

func (p *Pagination) Select(ctx context.Context, db *database.Connection, resultSlice interface{}, sb sq.SelectBuilder) error

Select is a wrapper around database.Connection.Select. This performs a query that returns multiple rows. In addition, this counts the total number of rows matching this query, and saves the result inside the pagination structure.

This actually performs two queries: the intended query, plus a second query to discover the total number of matching rows. I think this works differently in other databases, but this seems to be the preferred route for mysql. See: https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_found-rows for more details

Note: It is possible to have the initial query succeed and the count query to fail. In order to prevent odd issues, you should always do an error check before using the resulting value.

Note 2: This is really only useful for communicating size back to the enduser. For other pagination techniques, you may want to use LIMIT and OFFSET directly

func (*Pagination) SetMaxItems

func (p *Pagination) SetMaxItems(maxItems int64) *Pagination

SetMaxItems sets the maximum number of items that can be returned in a request/page. This must be called before Select to have any effect

func (*Pagination) WrapData

func (p *Pagination) WrapData(data interface{}) *dtos.PaginationWrapper

WrapData is a small helper to turn the desired content of a request into a pagination result set

type ReadEvidenceInput

type ReadEvidenceInput struct {
	OperationSlug string
	EvidenceUUID  string
	LoadPreview   bool
	LoadMedia     bool
}

type ReadEvidenceMetadataInput

type ReadEvidenceMetadataInput struct {
	OperationSlug string
	EvidenceUUID  string
}

type ReadEvidenceOutput

type ReadEvidenceOutput struct {
	UUID        string     `json:"uuid"`
	Description string     `json:"description"`
	ContentType string     `json:"contentType"`
	OccurredAt  time.Time  `json:"occurredAt"`
	AdjustedAt  *time.Time `json:"adjustedAt"`
	Preview     io.Reader  `json:"-"`
	Media       io.Reader  `json:"-"`
}

type ReadFindingInput

type ReadFindingInput struct {
	OperationSlug string
	FindingUUID   string
}

type RunServiceWorkerInput

type RunServiceWorkerInput struct {
	OperationSlug string
	EvidenceUUID  string
	WorkerName    string
}

type SetFavoriteInput

type SetFavoriteInput struct {
	OperationSlug string
	IsFavorite    bool
}

type SetUserFlagsInput

type SetUserFlagsInput struct {
	Slug     string
	Disabled *bool
	Admin    *bool
}

type SetUserGroupOperationRoleInput

type SetUserGroupOperationRoleInput struct {
	OperationSlug string
	UserGroupSlug string
	Role          policy.OperationRole
}

type SetUserOperationRoleInput

type SetUserOperationRoleInput struct {
	OperationSlug string
	UserSlug      string
	Role          policy.OperationRole
}

type SlugMap

type SlugMap []struct {
	UserSlug  sql.NullString `db:"user_slug"`
	GroupSlug string         `db:"group_slug"`
	GroupName string         `db:"group_name"`
	Deleted   sql.NullString `db:"deleted"`
}

type TagUsageItem

type TagUsageItem struct {
	TagID      int64     `db:"id"`
	OccurredAt time.Time `db:"occurred_at"`
}

type TopContribWithID

type TopContribWithID struct {
	dtos.TopContrib
	OperationID int64 `db:"operation_id" json:"operationId"`
}

type UpdateDefaultTagInput

type UpdateDefaultTagInput struct {
	ID          int64
	Name        string
	ColorName   string
	Description *string
}

type UpdateEvidenceInput

type UpdateEvidenceInput struct {
	OperationSlug string
	EvidenceUUID  string
	Description   *string
	TagsToAdd     []int64
	TagsToRemove  []int64
	Content       io.Reader
	AdjustedAt    *time.Time
}

type UpdateFindingCategoryInput

type UpdateFindingCategoryInput struct {
	ID       int64
	Category string
}

type UpdateFindingInput

type UpdateFindingInput struct {
	OperationSlug string
	FindingUUID   string
	Category      string
	Title         string
	Description   string
	TicketLink    *string
	ReadyToReport bool
}

type UpdateGlobalVarInput

type UpdateGlobalVarInput struct {
	Name    string
	Value   string
	NewName string
}

type UpdateOperationInput

type UpdateOperationInput struct {
	OperationSlug string
	Name          string
}

type UpdateOperationVarInput

type UpdateOperationVarInput struct {
	Name          string
	Value         string
	VarSlug       string
	OperationSlug string
}

type UpdateQueryInput

type UpdateQueryInput struct {
	OperationSlug string
	ID            int64
	Name          string
	Query         string
}

type UpdateServiceWorkerInput

type UpdateServiceWorkerInput struct {
	ID     int64
	Name   string
	Config string
}

type UpdateTagInput

type UpdateTagInput struct {
	ID            int64
	OperationSlug string
	Name          string
	ColorName     string
	Description   *string
}

type UpdateUserProfileInput

type UpdateUserProfileInput struct {
	UserSlug  string
	FirstName string
	LastName  string
	Email     string
}

type UpsertEvidenceMetadataInput

type UpsertEvidenceMetadataInput struct {
	EditEvidenceMetadataInput
	Status     string
	Message    *string
	CanProcess *bool
}

type UpsertQueryInput

type UpsertQueryInput struct {
	CreateQueryInput
	ReplaceName bool
}

type UserFilter

type UserFilter struct {
	NameParts  []string
	UsersTable string
}

UserFilter provides a mechanism to alter queries such that users are filtered

func ParseRequestQueryUserFilter

func ParseRequestQueryUserFilter(dr dissectors.DissectedRequest) UserFilter

ParseRequestQueryUserFilter generates a UserFilter object from a given request. This expects that filtering is specified by the query parameter "name"

func (*UserFilter) AddWhere

func (uf *UserFilter) AddWhere(sb *sq.SelectBuilder)

AddWhere adds to the given SelectBuilder a Where clause that will apply the filtering

type UserGroupFilter

type UserGroupFilter struct {
	NameParts       []string
	UserGroupsTable string
}

UserFilter provides a mechanism to alter queries such that users are filtered

func ParseRequestQueryUserGroupFilter

func ParseRequestQueryUserGroupFilter(dr dissectors.DissectedRequest) UserGroupFilter

ParseRequestQueryUserFilter generates a UserFilter object from a given request. This expects that filtering is specified by the query parameter "name"

func (*UserGroupFilter) AddWhere

func (uf *UserGroupFilter) AddWhere(sb *sq.SelectBuilder)

AddWhere adds to the given SelectBuilder a Where clause that will apply the filtering

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL