CrowdStrike Foundry Function Software Developer Kit for Python
Project description
Foundry Function as a Service Python SDK
foundry-fn-python
is a community-driven, open source project designed to enable the authoring of functions.
While not a formal CrowdStrike product, foundry-fn-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-foundry-function
Quickstart 💫
Code
Add the SDK to your project by following the installation instructions above,
then create your handler.py
:
import http
from crowdstrike.foundry.function import (
CSHandler,
HandlerBase,
Request,
Response,
run,
)
class Handler(HandlerBase): # *** (1) ***
def handler_init(self): # *** (2) ***
msg = f'initializing using configuration: {self.config()}' # *** (3) ***
self.logger().info(msg) # *** (4) ***
# *** (5) ***
def handle(self, request: Request) -> Response: # *** (6) ***
body = {
'body': request.body,
'context': request.context,
'method': request.method,
'url': request.url,
}
return Response( # *** (7) ***
body=body,
code=http.HTTPStatus.OK,
)
if __name__ == '__main__': # *** (8) ***
CSHandler.bootstrap(Handler)
run()
Handler(HandlerBase)
: Class containing the function's code. TheHandler
class must extendHandlerBase
, must be present inhandler.py
, and should not have an__init__
method. If an__init__
method is provided, it will be overridden internally as the framework constructs the handler.handler_init
: Initialization and bootstrapping method. The framework will invoke thehandler_init
following construction and basic initialization of the handler; invocation occurs exactly once at the start of the runtime. Implementing this function is optional and may be removed from the handler if no custom bootstrapping or setup is needed.self.config()
: Configuration dictionary of typedict[str, Any]
. This is populated as part of the bootstrapping process and is accessible within bothhandler_init
andhandle
. The same instance of the dictionary is returned on each invocation of this method.self.logger()
: Contextuallogging.Logger
instance. While the author is free to implement or import their own logger, the framework provides a version ofLogger
which outputs (in production) in JSON format and decorates the output with valuable contextual information. It is recommended that function authors use the providedLogger
unless they have good reason not to.handle()
: Called once on each inbound request. The business logic of the function should exist here.request
: Request payload and metadata. At the time of this writing, theRequest
object consists of:body
: The request payload as given in the Function Gatewaybody
payload 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.context
: Caller-supplied raw context.access_token
: Caller-supplied access token.
- Return from
handle()
: Returns aResponse
object. TheResponse
object contains fieldsbody
(payload of the response as adict
),code
(anint
representing an HTTP status code or a member ofhttp.HTTPStatus
),errors
(a list of anyAPIError
s), andheaders
(adict[str, list[str]]
of any special HTTP headers which should be present on the response). if __name__ == '__main__': ...
: Enables local testing of the function. Code placed in this block only serves to enable the author to run their function locally and is ignored in production.
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 handler.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": "/echo"
}'
Convenience Functionality 🧰
falconpy
Foundry Function Python ships with falconpy pre-integrated and a convenience constructor. While it is not strictly necessary to use the convenience function, it is recommended.
Important: Create a new instance of each falconpy
client you want on each request.
# omitting other imports
from falconpy.alerts import Alerts
from falconpy.event_streams import EventStreams
from crowdstrike.foundry.function.falconpy import falcon_client
class Handler(HandlerBase):
def handle(self, request: Request) -> Response:
# ... omitting other code ...
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# !!! create a new client instance on each request !!!
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
alerts_client = falcon_client(Alerts)
event_streams_client = falcon_client(EventStreams)
# ... omitting other code ...
WE STOP BREACHES
Project details
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
Hashes for crowdstrike-foundry-function-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b23c051db22ee3851b09f55581dfaced00bc48d00347dbc39e6ce6ac9bd9315 |
|
MD5 | e8c2baff1437e23c0a59213aa504b93f |
|
BLAKE2b-256 | 5c82b323dfabe9d37ca1a6b9b478934f8e57a90d2fac1bacad821fc2f5c3634b |
Hashes for crowdstrike_foundry_function-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 480ce01d7966fbe855cf8698de8f689784012e52083f09a68408980359b31a72 |
|
MD5 | 5f26a433123206952b982a142afae6b4 |
|
BLAKE2b-256 | aead3c72aca05bf4d35ebe9efe8014c5576517beb4a0c52be5e2f3cf6da195b9 |