Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Update grpc #36

Merged
merged 8 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ __pycache__/
/*.egg-info
/*.egg
/*.eggs

# Vim
*.swp
*.swo
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
SHELL := /bin/bash

PROTOC ?= $(shell which protoc)
GRPC_PYTHON_PLUGIN ?= $(shell which grpc_python_plugin)
# This builds using 'python -m grpc_tools.protoc' and not the protoc binary
# pip install grpcio grpcio-tools to install this module
leodido marked this conversation as resolved.
Show resolved Hide resolved

PROTOS := protos/schema.proto protos/output.proto
PROTO_URLS := https://raw.githubusercontent.com/falcosecurity/falco/master/userspace/falco/schema.proto https://raw.githubusercontent.com/falcosecurity/falco/master/userspace/falco/output.proto
PROTO_SHAS := a1f427c114b945d0880b55058862b74015d036aa722985ca6e5474ab4ed19f69 4ce2f3e6d6ebc07a74535c4f21da73e44c6ef848ab83627b1ac987058be5ece9
PROTOS := protos/schema.proto protos/outputs.proto protos/version.proto
PROTO_URLS := https://raw.githubusercontent.com/falcosecurity/falco/master/userspace/falco/schema.proto https://raw.githubusercontent.com/falcosecurity/falco/master/userspace/falco/outputs.proto https://raw.githubusercontent.com/falcosecurity/falco/master/userspace/falco/version.proto
PROTO_SHAS := 1adf7fbb2b92793a3cf490204314af7788ffd81655c4cedb40587a22db9c1915 5e3bdc564c4d38f7d70a8fe50e6022a733ed93197edff6b824a24c6a45fed6c3 fc470546c00273bafe20b53ab6b7e0784206b8f6f9a705df92994e89035a5dc4

PROTO_DIRS := $(dir ${PROTOS})
PROTO_DIRS_INCLUDES := $(patsubst %/, -I %, ${PROTO_DIRS})
Expand All @@ -26,8 +26,9 @@ $(1):
@curl --silent -Lo $(1) $(2)
@echo $(3) $(1) | sha256sum -c
@sed -i '/option go_package/d' $(1)
${PROTOC} ${PROTO_DIRS_INCLUDES} --python_out=${SCHEMA_OUT_DIR} --grpc_out=${GRPC_OUT_DIR} --plugin=protoc-gen-grpc=${GRPC_PYTHON_PLUGIN} $(1)
python -m grpc_tools.protoc -Iprotos --python_out=${SCHEMA_OUT_DIR} --grpc_python_out=${GRPC_OUT_DIR} $(1)
endef

$(foreach PROTO,$(PROTOS),\
$(eval $(call download_rule,$(PROTO),$(firstword $(PROTO_URLS)),$(firstword $(PROTO_SHAS))))\
$(eval PROTO_URLS := $(wordlist 2,$(words $(PROTO_URLS)),$(PROTO_URLS)))\
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
```python
import falco
client = falco.Client(endpoint="localhost:5060", client_crt="/tmp/client.crt", client_key="/tmp/client.key", ca_root="/tmp/ca.crt")
for event in client.subscribe(falco.Request(keepalive=True)):
for event in client.sub()):
print(event)
```

Expand Down
2 changes: 1 addition & 1 deletion examples/get_events.py → examples/tls_sub_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
output_format=args.output_format,
)

for event in c.subscribe(falco.Request(keepalive=True)):
for event in c.sub():
print(event)
16 changes: 16 additions & 0 deletions examples/unixsocket_get_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import argparse

import falco

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output-format", "-o", dest="output_format", default=None, help="output_format")
args = parser.parse_args()

c = falco.Client(
endpoint="unix:///var/run/falco.sock",
output_format=args.output_format,
)

for event in c.get():
print(event)
15 changes: 15 additions & 0 deletions examples/unixsocket_get_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import argparse

import falco

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output-format", "-o", dest="output_format", default=None, help="output_format")
args = parser.parse_args()

c = falco.Client(
endpoint="unix:///var/run/falco.sock",
output_format=args.output_format,
)

print(c.version())
16 changes: 16 additions & 0 deletions examples/unixsocket_sub_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import argparse

import falco

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output-format", "-o", dest="output_format", default=None, help="output_format")
args = parser.parse_args()

c = falco.Client(
endpoint="unix:///var/run/falco.sock",
output_format=args.output_format,
)

for event in c.sub():
print(event)
2 changes: 1 addition & 1 deletion falco/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__title__ = "falco"
__description__ = "Python client and SDK for Falco."
__url__ = "https://github.com/falcosecurity/client-py"
__version__ = "0.1.0"
__version__ = "0.2.0"
__author__ = "The Falco Authors"
__author_email__ = "cncf-falco-dev@lists.cncf.io"
__license__ = "Apache 2.0"
Expand Down
61 changes: 51 additions & 10 deletions falco/client.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
import grpc

from falco.client_credentials import get_grpc_channel_credentials
from falco.domain import Response
from falco.svc.output_pb2_grpc import serviceStub
from falco.domain import Response, Request
from falco.svc.outputs_pb2_grpc import serviceStub as outputsServiceStub
from falco.svc.version_pb2_grpc import serviceStub as versionServiceStub
from falco.schema.version_pb2 import request as versionRequest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion the Client should only use domain models

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a stab at the refactor you're suggesting and got pretty confused. Would you be willing to throw a patch together to move the code into the domain model?

from falco.schema.outputs_pb2 import request as outputsRequest


class InvalidFormat(Exception):
pass

class TLSConfigError(Exception):
pass

class RequestGenerator:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be a function (and should belong to the outputs domain model)

def __init__(self):
pass

def EmptyRequests(self):
while True:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen examples where they put a sleep here.

yield outputsRequest()

class Client:
def __init__(self, endpoint, client_crt, client_key, ca_root, output_format=None, *args, **kw):
self._client = serviceStub(
grpc.secure_channel(
def __init__(self, endpoint, client_crt=None, client_key=None, ca_root=None, output_format=None, *args, **kw):
if endpoint.startswith("unix:///"):
nibalizer marked this conversation as resolved.
Show resolved Hide resolved
channel = grpc.insecure_channel(
endpoint,
options=[("grpc.max_receive_message_length", 1024 * 1024 * 512)],
Copy link
Member

@leodido leodido Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this value for receiving messages exact?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copy and pasted from the tls connection below. I didn't find us setting a maximum size in the falco source code to match this with.

)

else:
if None in [client_crt, client_key, ca_root]:
raise TLSConfigError("Error: Must provide valid paths to all of the TLS data: client certificate, client key, and CA certificate")
channel = grpc.secure_channel(
endpoint,
credentials=get_grpc_channel_credentials(client_crt, client_key, ca_root),
options=[("grpc.max_receive_message_length", 1024 * 1024 * 512)],
),
)
)

self._outputs_client = outputsServiceStub(channel)
self._version_client = versionServiceStub(channel)
self.output_format = output_format

@property
Expand All @@ -31,14 +54,32 @@ def output_format(self, o):

self._output_format = o

def subscribe(self, request): # TODO: test
pb_req = request.to_proto()
def sub(self): # TODO: test

requests = RequestGenerator()
responses = self._client.sub(requests.EmptyRequests())
for pb_resp in responses:
resp = Response.from_proto(pb_resp)

if self.output_format:
yield getattr(resp, Response.SERIALIZERS[self.output_format])()
continue

for pb_resp in self._client.subscribe(pb_req):
yield resp

def get(self):

request = outputsRequest()
responses = self._client.get(request)
for pb_resp in responses:
resp = Response.from_proto(pb_resp)

if self.output_format:
yield getattr(resp, Response.SERIALIZERS[self.output_format])()
continue

yield resp

def version(self):

return self._versionClient.version(versionRequest())
14 changes: 4 additions & 10 deletions falco/domain/request.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
from falco.schema.output_pb2 import request
from falco.schema.outputs_pb2 import request


class Request:
__slots__ = ("keepalive",)

def __init__(self, keepalive=None):
self.keepalive = keepalive

def __repr__(self):
return f"{self.__class__.__name__}(keepalive={self.keepalive})"
__slots__ = ()

@classmethod
def from_proto(cls, pb_request):
return cls(keepalive=pb_request.keepalive)
return cls()

def to_proto(self):
return request(keepalive=self.keepalive)
return request()
2 changes: 1 addition & 1 deletion falco/domain/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from dateutil import tz

from falco.schema.output_pb2 import response
from falco.schema.outputs_pb2 import response
from falco.schema.schema_pb2 import priority, source
from falco.utils import pb_timestamp_from_datetime

Expand Down
Loading