api

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAPointer = errors.New("result must be a pointer")

Functions

func Create

func Create[E CreatableEntity](
	ctx context.Context,
	client Client,
	customAttributes string,
	input AddInput,
) (map[string]any, error)

Create will push the chosen CreatableEntity with the attributes given in 'input' to the OpenCTI server. If the entity already exists on the platform, it will be updated instead. It returns a map containing the chosen 'customAttributes' structure, as per GraphQL syntax.

func Decode

func Decode(input, output any) error

Decode wraps mapstructure.Decode, using a custom DecoderConfig: It targets the "gocti" struct tag. It handles special types with a custom DecodeFunc.

func DecodeInterface

func DecodeInterface(input GraphQLInterface, output any) error

DecodeInterface converts a GraphQL Interface type into one of its implementations. It uses the data returned by [GraphQLInterface.Remainder] to complete the fields specific to the implementation.

func Delete

func Delete[E DeletableEntity](
	ctx context.Context,
	client Client,
	id string,
) (string, error)

Delete deletes the chosen DeletableEntity with the provided 'id' from the OpenCTI server. It returns the ID of the recently deleted entity.

func FormatAttributes

func FormatAttributes(attr string, offset int) string

func List

func List[E ListableEntity](
	ctx context.Context,
	client Client,
	customAttributes string,
	getAll bool,
	pageInfo *graphql.PageInfo,
	opts ...list.Option,
) ([]map[string]any, error)

List fetches a list of the chosen ListableEntity from the OpenCTI server It returns a slice of maps, each containing the chosen 'customAttributes' structure, as per GraphQL syntax. Pagination information can be retrieved by providing a non-nil pointer to a graphql.PageInfo.

func ParseAttributes

func ParseAttributes[Entity any](options ...ParseOption) string

ParseAttributes parses a struct type to build a GraphQL query segment matching its fields. The provided type must have a structure compatible with the GraphQL schema. Only fields marked with the "gocti" struct tag will be parsed. Field arguments can be added after the name tag (e.g. `gocti:"name,(id: \"0123456789\")"`). They should be added in second position, after the name, and before any decoding-specific tag value, like "remain", "omitempty", or "squash".

WARNING: Using ParseAttributes with large structs pointing to many other structs (typically non-truncated OpenCTI entities) can quickly lead to a very large returned string.

func Read

func Read[E ReadableEntity](
	ctx context.Context,
	client Client,
	customAttributes, id string,
) (map[string]any, error)

Read retrieves the chosen ReadableEntity with the provided 'id' from the OpenCTI server. It returns a map containing the chosen 'customAttributes' structure, as per GraphQL syntax.

func StructuredCreate

func StructuredCreate[E CreatableEntity, ReturnStruct any](
	ctx context.Context,
	client Client,
	customAttributes string,
	input AddInput,
) (ReturnStruct, error)

StructuredCreate is a wrapper around Create that returns the data as a struct instead of a map. The provided ReturnStruct type must have a structure compatible with the GraphQL schema of the target entity. Field name aliases can (and should) be set using the "gocti" tag (e.g. struct{ OpenCTIName string `gocti:"name"` }). WARNING: Golang default values will be returned for fields that are not set or have a 'null' value on the server.

func StructuredList

func StructuredList[E ListableEntity, ReturnStruct any](
	ctx context.Context,
	client Client,
	customAttributes string,
	getAll bool,
	pageInfo *graphql.PageInfo,
	opts ...list.Option,
) ([]ReturnStruct, error)

StructuredList is a wrapper around List that returns the data as a list of structs instead of a list of maps. The provided ReturnStruct type must have a structure compatible with the GraphQL schema of the target entity. Field name aliases can (and should) be set using the "gocti" tag (e.g. struct{ OpenCTIName string `gocti:"name"` }). WARNING: Golang default values will be returned for fields that are not set or have a 'null' value on the server.

func StructuredRead

func StructuredRead[E ReadableEntity, ReturnStruct any](
	ctx context.Context,
	client Client,
	customAttributes, id string,
) (ReturnStruct, error)

StructuredRead is a wrapper around Read that returns the data as a struct instead of a map. The provided ReturnStruct type must have a structure compatible with the GraphQL schema of the target entity. Field name aliases can (and should) be set using the "gocti" tag (e.g. struct{ OpenCTIName string `gocti:"name"` }). WARNING: Golang default values will be returned for fields that are not set or have a 'null' value on the server.

Types

type AddInput

type AddInput interface {
	Input() (map[string]any, error)
}

type Client

type Client interface {
	Query(ctx context.Context, query string, variables map[string]any) (map[string]any, error)
	Logger() *slog.Logger
	Defaults
}

type CreatableEntity

type CreatableEntity interface {
	DefaultProperties() string
	CreateQueryString(customAttributes string) string
	CreateResponseField() string
}

type Defaults

type Defaults interface {
	// DefaultPageSize returns the default pagination size for list queries and an ok expression indicating if the value
	// has been set (true) or not (false) by the user via env variables or [OpenCTIAPIClient] constructor options.
	// The zero value is returned if not set.
	DefaultPageSize() (int, bool)

	// DefaultOrderBy returns the default field name for ordering list queries and an ok expression indicating if the value
	// has been set (true) or not (false) by the user via env variables or [OpenCTIAPIClient] constructor options.
	// The zero value is returned if not set.
	DefaultOrderBy() (string, bool)

	// DefaultOrderMode returns the default mode for ordering list queries and an ok expression indicating if the value
	// has been set (true) or not (false) by the user via env variables or [OpenCTIAPIClient] constructor options.
	// The zero value is returned if not set.
	DefaultOrderMode() (string, bool)
}

Defaults defines the interface to access various configured default values.

type DeletableEntity

type DeletableEntity interface {
	DeleteQueryString() string
	DeleteResponseField() string
}

type EditInput

type EditInput struct {
	Key        string        `json:"key,omitempty"`
	ObjectPath string        `json:"object_path,omitempty"`
	Value      any           `json:"value,omitempty"`
	Operation  EditOperation `json:"operation,omitempty"`
}

type EditOperation

type EditOperation string
const (
	EditOperationAdd     EditOperation = "add"
	EditOperationReplace EditOperation = "replace"
	EditOperationRemove  EditOperation = "remove"
)

type File

type File struct {
	Name string
	Data []byte
	MIME string
}

type GraphQLInterface

type GraphQLInterface interface {
	// The [Implementations] method should return all types that implement this GraphQL interface.
	Implementations() []reflect.Type
	// The [Remainder] method should return the additional data to be stored in an implementation's
	// specific fields. In GraphQL, Interfaces (and Unions) have less fields than their
	// implementations. The additional data can be saved separately when decoding a GraphQL
	// response into a Golang struct by adding a extra field using the tag `gocti:",remain"`
	// The [Remainder] method allows [DecodeInterface] to retrieve this extra data for conversion.
	Remainder() map[string]any
}

GraphQLInterface is a type that represents a GraphQL Interface or Union.

type InterfaceTypeError

type InterfaceTypeError struct {
	Want     reflect.Kind
	Received reflect.Type
}

InterfaceTypeError is returned when a provided type for a generic function does not match the expected kind.

func (InterfaceTypeError) Error

func (err InterfaceTypeError) Error() string

type ListableEntity

type ListableEntity interface {
	DefaultProperties() string
	ListQueryString(customAttributes string) string
	ListResponseField() string
}

type MissingFieldError

type MissingFieldError struct {
	FieldName string
}

MissingFieldError is returned when a required field in a map is not found.

func (MissingFieldError) Error

func (err MissingFieldError) Error() string

type NotImplementingError

type NotImplementingError struct {
	InterfaceType      reflect.Type
	ImplementationType reflect.Type
}

NotImplementingError is returned when decoding a GraphQL interface into a type that does not implement it.

func (NotImplementingError) Error

func (e NotImplementingError) Error() string

type ParseOption

type ParseOption func(p *attributeParser)

func WithComments

func WithComments(comments bool) ParseOption

WithComments sets whether the parser should signal ignored recursion loops or exceeded maximum depth fields with a comment.

func WithMaxDepth

func WithMaxDepth(maxDepth int) ParseOption

WithMaxDepth sets the maximum depth at which the parser stops recursively exploring struct fields.

type ReadableEntity

type ReadableEntity interface {
	DefaultProperties() string
	ReadQueryString(customAttributes string) string
	ReadResponseField() string
}

type TypeAssertionError

type TypeAssertionError struct {
	Variable     any
	ExpectedType string
}

TypeAssertionError is returned when a required type assertion fails.

func (TypeAssertionError) Error

func (err TypeAssertionError) Error() string

type UnimplementedDecodingError

type UnimplementedDecodingError struct {
	// contains filtered or unexported fields
}

UnimplementedDecodingError is returned when a decoding hook 'fromType -> toType' is missing.

func (UnimplementedDecodingError) Error

Jump to

Keyboard shortcuts

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