-
Notifications
You must be signed in to change notification settings - Fork 293
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
Schemas validation and easy creation #1159
Conversation
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.
Some minor comments, but otherwise this looks great!
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.
Fixed your comments
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.
Minor nits, otherwise looks good!
Important
This PR removes
add_text
method in Observable class. It breaks private plugins relying on this method to create observables.New features
Observables
Validators
Overview
Validators are now enforced for observables listed below. Validators ensure that provided value is relevant and that an for example an
IPv4
observable cannot be built from aUrl
.Note
Validators enforcement also impacts
url
observable creation which requires a scheme to be valid. This means thatexample.com/foobar
is no longer a validurl
.Implementation details
Value validation relies on pydandic
field_validator
decorator. Any observable implementation that requires validation must implementvalidate_value
class method with@field_validator("value")
decorator.validate_value
is also where refang of the value takes place. If the value is not valid, ValueError exception must be raised. Otherwise, the value (modified or not) must be returned as a string.Helper functions
To ease observables creation, new functions have been implemented in
observable
module:Guess observable type from value
Guess which observable type can validate
value
string. If the value can be validated by an observable, its type is returned as a string. If the value can't be guessed,None
is returned.Create an observable from string
Create an observable from the given string value without saving it in the database. It's up to the caller to then call
save()
on the returned object to save it in the database.Params
value
: a string defining the observable.type
: an optional string defining the type of the observable. Iftype
is not provided, it will be guessed by callingguess_type
.kwargs
: a dictionary defining further attributes depending on the type of the observable.Returns
If the function call succeeds, the created observable is returned. Otherwise, an
ValueError
exception is raised.Save an observable from string
Use
create
function and save observable in the database. Tag the resulting observable with the list of tags if defined.Params
value
: a string defining the observable.type
: an optional string defining the type of the observable. Iftype
is not provided, it will be guessed by callingguess_type
.tags
: an optional list of strings corresponding to the tags to add to the saved observable.kwargs
: a dictionary defining further attributes depending on the type of the observable.Returns
If the function call succeeds, the saved observable is returned. Otherwise, an
ValueError
exception is raised.Create observables from text
Create observables from the given text. Each line must contain one observable. Guess the type of the observable and create it. Created observable are not saved in the database.
text
: a string containing one observable per line.A tuple containing a list of created observables and a list of string for unguessable lines.
Save observables from text
Use
create_from_text
function and save observables in the database. Tag the resulting observables with the list of tags if defined.text
: a string containing one observable per line.tags
: an optional list of strings corresponding to the tags to add to the saved observables.A tuple containing a list of saved observables and a list of string for unguessable lines.
Create observables from file
Create observables from the given file. Each line must contain one observable. Guess the type of the observable and creates it. Created observables are not saved in the database.
file
: Represents a file in different ways:open
or based onio
classesA tuple containing a list of created observables and a list of string for unguessable lines.
Save observables from file
Use
create_from_file
function and save observables into the database. Tag the resulting observables with the list of tags if defined.file
: Represents a file in different ways:open
or based onio
classestags
: an optional list of strings corresponding to the tags to add to the saved observables.A tuple containing a list of saved observables and a list of string for unguessable lines.
Create observables from a url
Create observables from the given url. Each line must contain one observable. Guess the type of the observable and create it. Created observables are not saved in the database.
url
: a string defining the URL to fetch the content from.A tuple containing a list of created observables and a list of string for unguessable lines.
Save observables from a url
Use
create_from_url
function and save observables in the database. Tag the resulting observabls with the list of tags if defined.url
: a string defining the URL to fetch the content from.tags
: an optional list of strings corresponding to the tags to add to the saved observables.A tuple containing a list of saved observables and a list of string for unguessable lines.
Find an observable object
Find an observable in the database matching the string
value
and optional fields represented bykwargs
. This function automatically refangs defangedvalue
before querying the database. Return an observable object.Entities
Create an entity
Create an entity from the given name and type without saving it to the database.
name
: a string defining the name of the entitytype
: a string defining the type of the entitykwargs
: a dictionary defining further attributes depending on the type of the entity.If the function call succeeds, the created entity is returned. Otherwise, an
ValueError
exception is raised.Save an entity
Use
create
function and save entity in the database. Tag the resulting entity with the list of tags if defined.name
: a string defining the name of the entity to createtype
: a string defining the type of the entity to createtags
: an optional list of strings corresponding to the tags to add to the saved observables.kwargs
: a dictionary defining further attributes depending on the type of the entity.If the function call succeeds, the saved entity is returned. Otherwise, an
ValueError
exception is raised.Indicators
Create an indicator
Create an indicator from the given name, type, pattern and diamond without saving it to the database.
name
: a string defining the name of the indicatortype
: a string defining the type of the indicatorpattern
: a string defining the pattern of the indicatordiamond
a string defining the diamond model of the indicatorkwargs
: a dictionary defining further attributes depending on the type of the indicator.If the function call succeeds, the created indicator is returned. Otherwise, an
ValueError
exception is raised.Save an entity
Use
create
function and save indicator in the database. Tag the resulting indicator with the list of tags if defined.name
: a string defining the name of the indicatortype
: a string defining the type of the indicatorpattern
: a string defining the pattern of the indicatordiamond
a string defining the diamond model of the indicatortags
: an optional list of strings corresponding to the tags to add to the saved observables.kwargs
: a dictionary defining further attributes depending on the type of the entity.If the function call succeeds, the saved indicator is returned. Otherwise, an
ValueError
exception is raised.API
New observables endpoints have been implemented to support the new save and create functions:
Import observables from text
Save observables from a text containing one observable per line. Tag with defined tags.
POST api/v2/observables/import/text
Import observables from file
Save observables from a file containing one observable per line. Tag with defined tags.
POST api/v2/observables/import/file
Import observables from URL
Save observables from a text containing one observable per line. Tag with defined tags.
POST api/v2/observables/import/file