-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding new samples for the python callouts (#51)
* Adding use-case 5 * Adding cloud_log sample and refactoring some functions for better clarity * Adding tests for custom_response, adjusting dockerfile and adjusting formatting for the example * Adjusting the description of the custom_response --------- Co-authored-by: victorwoli <victorwo@ciandt.com>
- Loading branch information
1 parent
8a0f3ff
commit 2d0c68e
Showing
10 changed files
with
866 additions
and
4 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
callouts/python/extproc/example/add_custom_response/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
FROM marketplace.gcr.io/google/debian12 | ||
RUN apt-get update && apt-get install -y python3-pip python3-grpc-tools | ||
WORKDIR /home/callouts/python | ||
COPY ./extproc/service ./extproc/service | ||
COPY ./extproc/proto ./extproc/proto | ||
COPY ./extproc/ssl_creds ./extproc/ssl_creds | ||
COPY ./extproc/example/add_custom_response ./extproc/example/add_custom_response | ||
EXPOSE 443 | ||
EXPOSE 8080 | ||
EXPOSE 80 | ||
CMD [ "/usr/bin/python3", "-um", "extproc.example.add_custom_response.service_callout_example" ] |
Empty file.
68 changes: 68 additions & 0 deletions
68
callouts/python/extproc/example/add_custom_response/service_callout_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2024 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from grpc import ServicerContext | ||
from extproc.proto import service_pb2 | ||
from extproc.service import callout_server | ||
|
||
|
||
class CalloutServerExample(callout_server.CalloutServer): | ||
"""Example callout server. | ||
Provides a non-comprehensive set of responses for each of the possible | ||
callout interactions. | ||
On a request header callout we check if it contains a header called '{mock:}', if yes then it will | ||
generate a mock response, otherwise it will follow the standard flow and add a header | ||
'{header-request: request}'. On response header callouts, we respond with a mutation to add | ||
the header '{header-response: response}'. | ||
On a request body callout we check if it contains in the body 'mock', if yes then it will generate | ||
a mock response, otherwise it will follow the standard flow and provide a mutation to append '-added-body' | ||
to the body. On response body callouts we send a mutation to replace the body with 'new-body'. | ||
""" | ||
|
||
def on_request_body( | ||
self, body: service_pb2.HttpBody, context: ServicerContext | ||
) -> service_pb2.BodyResponse: | ||
"""Custom processor on the request body.""" | ||
return callout_server.add_body_mutation(body='-added-body') | ||
|
||
def on_response_body( | ||
self, body: service_pb2.HttpBody, context: ServicerContext | ||
) -> service_pb2.BodyResponse: | ||
"""Custom processor on the response body.""" | ||
return callout_server.add_body_mutation(body='new-body', clear_body=False) | ||
|
||
def on_request_headers( | ||
self, headers: service_pb2.HttpHeaders, context: ServicerContext | ||
) -> service_pb2.HeadersResponse: | ||
"""Custom processor on request headers.""" | ||
return callout_server.add_header_mutation( | ||
add=[('header-request', 'request')], remove=['foo'], | ||
clear_route_cache=True | ||
) | ||
|
||
def on_response_headers( | ||
self, headers: service_pb2.HttpHeaders, context: ServicerContext | ||
) -> service_pb2.HeadersResponse: | ||
"""Custom processor on response headers.""" | ||
return callout_server.add_header_mutation( | ||
add=[('header-response', 'response')] | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
# Run the gRPC service | ||
CalloutServerExample(port=443, insecure_port=8080, health_check_port=80).run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
FROM marketplace.gcr.io/google/debian12 | ||
RUN apt-get update && apt-get install -y python3-pip python3-grpc-tools | ||
RUN pip install google-cloud-logging --break-system-packages | ||
WORKDIR /home/callouts/python | ||
COPY ./extproc/service ./extproc/service | ||
COPY ./extproc/proto ./extproc/proto | ||
COPY ./extproc/ssl_creds ./extproc/ssl_creds | ||
COPY ./extproc/example/cloud_log ./extproc/example/cloud_log | ||
EXPOSE 443 | ||
EXPOSE 8080 | ||
EXPOSE 80 | ||
CMD [ "/usr/bin/python3", "-um", "extproc.example.cloud_log.service_callout_example" ] |
Empty file.
60 changes: 60 additions & 0 deletions
60
callouts/python/extproc/example/cloud_log/service_callout_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2024 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import google.cloud.logging | ||
|
||
from grpc import ServicerContext | ||
from extproc.proto import service_pb2 | ||
from extproc.service import callout_server | ||
|
||
|
||
class CalloutServerExample(callout_server.CalloutServer): | ||
"""Example callout server. | ||
Provides a non-comprehensive set of responses for each of the possible | ||
callout interactions. | ||
For request header callouts we check the content of the request and | ||
authorize the request or reject the request. | ||
The content being checked is if the header has the header header-check. | ||
The decision is logged to Cloud Logging. | ||
For request body callouts we check the content of the request and | ||
authorize the request or reject the request. | ||
The content being checked is if the body has the body body-check. | ||
The decision is logged to Cloud Logging. | ||
""" | ||
|
||
def on_request_headers( | ||
self, headers: service_pb2.HttpHeaders, context: ServicerContext | ||
) -> service_pb2.HeadersResponse: | ||
"""Custom processor on request headers.""" | ||
return callout_server.add_header_mutation( | ||
add=[('header-request', 'request')], | ||
clear_route_cache=True | ||
) | ||
|
||
def on_request_body( | ||
self, body: service_pb2.HttpBody, context: ServicerContext | ||
) -> service_pb2.BodyResponse: | ||
"""Custom processor on the request body.""" | ||
return callout_server.add_body_mutation(body='-added-body') | ||
|
||
if __name__ == '__main__': | ||
"""Sets up Google Cloud Logging for the cloud_log example""" | ||
client = google.cloud.logging.Client() | ||
client.setup_logging() | ||
|
||
# Run the gRPC service | ||
CalloutServerExample(port=443, insecure_port=8080, health_check_port=80).run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.