CrowdStrike Falcon Foundry Function Software Developer Kit for Python
Project description
Falcon Foundry Function as a Service Python SDK
falcon-foundry-python is a community-driven, open source project designed to enable the authoring of functions.
While not a formal CrowdStrike product, falcon-foundry-python is maintained by CrowdStrike and supported in partnership
with the open source developer community.
Installation ⚙️
Via pip
The SDK can be installed or updated via pip install:
python3 -m pip install crowdstrike-falcon-foundry
Quickstart 💫
Code
Add the SDK to your project by following the installation instructions above,
then create your handler.py:
import logging
from falconfoundry import (
FoundryAPIError,
FoundryRequest,
FoundryResponse,
FoundryFunction,
)
func = FoundryFunction.instance() # *** (1) ***
@func.handler(method='POST', path='/create') # *** (2) ***
def on_create(request: FoundryRequest, config: [dict[str, any], None],
logger: logging.Logger) -> FoundryResponse: # *** (3), (4), (5) ***
if len(request.body) == 0:
return FoundryResponse(
code=400,
errors=[FoundryAPIError(code=400, message='empty body')]
)
#####
# do something useful
#####
return FoundryResponse( # *** (6) ***
body={'hello': 'world'},
code=200,
)
@func.handler(method='PUT', path='/update')
def on_update(request: FoundryRequest) -> FoundryResponse: # *** (7) ***
# do stuff
return FoundryResponse(
# ...snip...
)
@func.handler(method='DELETE', path='/foo')
def on_delete(request: FoundryRequest, config: [dict[str, any], None]) -> FoundryResponse: # *** (8) ***
# do stuff
return FoundryResponse(
# ...snip...
)
if __name__ == '__main__':
func.run() # *** (9) ***
FoundryFunction: TheFoundryFunctionclass wraps the Foundry Function implementation. EachFoundryFunctioninstance consists of a number of handlers, with each handler corresponding to an endpoint. Only oneFoundryFunctionshould exist per Python implementation. MultipleFoundryFunctions will result in undefined behavior.@func.handler: The handler decorator defines a Python function/method as an endpoint. At a minimum, thehandlermust have amethodand apath. Themethodmust be one ofDELETE,GET,PATCH,POST, andPUT. Thepathcorresponds to theurlfield in the request. The SDK will provide any loaded configuration as an argument.- Methods decorated with
@handlermust take arguments in the order ofFoundryRequestanddict|None(i.e. the request and either the configuration or nothing; see example above), and must return aFoundryResponse. request: FoundryRequest payload and metadata. At the time of this writing, theFoundryRequestobject consists of:body: The request payload as given in the Function Gatewaybodypayload field. Will be deserialized as adict[str, Any].params: Contains request headers and query parameters.url: The request path relative to the function as a string.method: The request HTTP method or verb.access_token: Caller-supplied access token.
logger: Unless there is specific reason not to, the function author should use theLoggerprovided to the function. When deployed, the suppliedLoggerwill be formatted in a custom manner and will have fields injected to assist with working against our internal logging infrastructure. Failure to use the providedLoggercan thus make triage more difficult.- Return from a
@handlerfunction: Returns aFoundryResponseobject. TheFoundryResponseobject contains fieldsbody(payload of the response as adict),code(anintrepresenting an HTTP status code),errors(a list of anyFoundryAPIErrors), andheader(adict[str, list[str]]of any special HTTP headers which should be present on the response). If nocodeis provided but a list oferrorsis, thecodewill be derived from the greatest positive valid HTTP code present on the givenFoundryAPIErrors. on_update(request: FoundryRequest): If only one argument is provided, only aFoundryRequestwill be provided.on_delete(request: FoundryRequest, config: [dict[str, any], None]): If two arguments are provided, aFoundryRequestand config will be provided.func.run(): Runner method and general starting point of execution. Callingrun()causes theFoundryFunctionto finish initializing and start executing. Any code declared following this method may not necessarily be executed. As such, it is recommended to place this as the last line of your script.
Testing locally
The SDK provides an out-of-the-box runtime for executing the function. A basic HTTP server will be listening on port 8081.
cd my-project && python3 main.py
Requests can now be made against the executable.
curl -X POST 'http://localhost:8081' \
-H 'Content-Type: application/json' \
--data '{
"body": {
"foo": "bar"
},
"method": "POST",
"url": "/create"
}'
Working with falconpy
Foundry function authors should import falconpy explicitly as a requirement in their project when needed.
General usage
Important: Create a new instance of each falconpy client you want on each request.
# omitting other imports
from falconpy.alerts import Alerts
from falconfoundry import cloud, FoundryFunction
func = FoundryFunction.instance()
@func.handler(...)
def endpoint(request):
# ... omitting other code ...
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# !!! create a new client instance on each request !!!
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
falconpy_alerts = Alerts(access_token=request.access_token, base_url=cloud())
# ... omitting other code ...
WE STOP BREACHES
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file crowdstrike_falcon_foundry-1.0.0.tar.gz.
File metadata
- Download URL: crowdstrike_falcon_foundry-1.0.0.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2700603d3f737c77be800cd28d26400ac34e0ec7d83fe12489447967cdd5a5b3
|
|
| MD5 |
2bef7998dc465f6dd8b1c854f958230e
|
|
| BLAKE2b-256 |
65cc25f18d4a5112aa93bb0efe7da138c77d47b99f026da192b3ef9ee5a701f9
|
File details
Details for the file crowdstrike_falcon_foundry-1.0.0-py3-none-any.whl.
File metadata
- Download URL: crowdstrike_falcon_foundry-1.0.0-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e2860c6814d0f1ff8f42f2d32d46f657aa260b1a087e6eb3c78fdf73353a4be
|
|
| MD5 |
34f94fed779c8f868b48491896b19fd2
|
|
| BLAKE2b-256 |
56a3c8bd3fefb66d9d88125327ed6fdaf5b6feab508d73598760205a7014bfba
|