-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from boanlab/ml-engine-rework
Ml engine rework
- Loading branch information
Showing
13 changed files
with
250 additions
and
34 deletions.
There are no files selected for viewing
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,45 @@ | ||
name: ci-test-py | ||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
py-pip-ai-sentryflow: | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.11' | ||
cache: 'pip' | ||
|
||
- name: check Python pip3 | ||
- run: pip install -r requirements.txt | ||
- run: pip test | ||
working-directory: ai-engine | ||
|
||
py-lint-ai-sentryflow: | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.11' | ||
cache: 'pip' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
working-directory: ai-engine | ||
|
||
- name: Lint with Ruff | ||
run: | | ||
pip install ruff | ||
ruff --output-format=github . | ||
continue-on-error: true | ||
working-directory: ai-engine |
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,6 @@ | ||
.idea | ||
.git | ||
.gitignore | ||
protobuf | ||
Dockerfile | ||
__pycache__/ |
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,3 @@ | ||
.idea/ | ||
__pycache__/ | ||
protobuf/ |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
IMAGE_NAME = 5GSEC/sentryflow-ai-engine | ||
TAG = v0.1 | ||
IMAGE_NAME = 5gsec/sentryflow-ai-engine | ||
TAG = v0.0.1 | ||
|
||
.PHONY: build | ||
|
||
build: | ||
docker build -t $(IMAGE_NAME):$(TAG) -f ./Dockerfile | ||
docker build -t $(IMAGE_NAME):$(TAG) -f ./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 |
---|---|---|
@@ -1,37 +1,94 @@ | ||
from pymongo import MongoClient | ||
import os | ||
import grpc | ||
|
||
from stringlifier.api import Stringlifier | ||
from flask import Flask | ||
from concurrent import futures | ||
|
||
from protobuf import sentryflow_metrics_pb2_grpc | ||
from protobuf import sentryflow_metrics_pb2 | ||
|
||
|
||
class HandlerServer: | ||
""" | ||
Class for gRPC Servers | ||
""" | ||
def __init__(self): | ||
try: | ||
self.listen_addr = os.environ["AI_ENGINE_ADDRESS"] | ||
except KeyError: | ||
self.listen_addr = "0.0.0.0:5000" | ||
|
||
self.server = None | ||
self.grpc_servers = list() | ||
|
||
def init_grpc_servers(self): | ||
""" | ||
init_grpc_servers method that initializes and registers gRPC servers | ||
:return: None | ||
""" | ||
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) | ||
self.grpc_servers.append(APIClassificationServer()) # @todo: make this configurable | ||
|
||
grpc_server: GRPCServer | ||
for grpc_server in self.grpc_servers: | ||
grpc_server.register(self.server) | ||
|
||
def serve(self): | ||
""" | ||
serve method that starts serving gRPC servers, this is blocking function. | ||
:return: None | ||
""" | ||
self.server.add_insecure_port(self.listen_addr) | ||
|
||
print("[INFO] Starting to serve on {}".format(self.listen_addr)) | ||
self.server.start() | ||
self.server.wait_for_termination() | ||
|
||
app = Flask(__name__) | ||
s = Stringlifier() | ||
|
||
@app.route('/api_metrics') | ||
def api_metrics(): | ||
# Connect to MongoDB | ||
client = MongoClient('mongodb://mongo:27017') | ||
class GRPCServer: | ||
""" | ||
Abstract class for an individual gRPC Server | ||
""" | ||
def register(self, server): | ||
""" | ||
register method that registers gRPC service to target server | ||
:param server: The server | ||
:return: None | ||
""" | ||
pass | ||
|
||
# Access the numbat database | ||
db = client.numbat | ||
|
||
# Access the access-logs collection | ||
collection = db['access-logs'] | ||
class APIClassificationServer(sentryflow_metrics_pb2_grpc.SentryFlowMetricsServicer, GRPCServer): | ||
""" | ||
Class for API Classification Server using Stringlifier | ||
""" | ||
|
||
# Retrieve all documents from the collection | ||
logs = list(collection.find({})) | ||
def __init__(self): | ||
self.stringlifier = Stringlifier() | ||
print("[Init] Successfully initialized APIClassificationServer") | ||
|
||
# Close the MongoDB connection | ||
client.close() | ||
def register(self, server): | ||
sentryflow_metrics_pb2_grpc.add_SentryFlowMetricsServicer_to_server(self, server) | ||
|
||
paths = list() | ||
def GetAPIClassification(self, request_iterator, context): | ||
""" | ||
GetAPIClassification method that runs multiple API ML Classification at once | ||
:param request_iterator: The requests | ||
:param context: The context | ||
:return: The results | ||
""" | ||
|
||
# Print out all entries | ||
for log in logs: | ||
paths.append(log["path"]) | ||
for req in request_iterator: | ||
paths = req.paths | ||
ml_results = self.stringlifier(paths) | ||
print("{} -> {}".format(paths, ml_results)) | ||
|
||
parsed = s(paths) | ||
print(set(parsed)) | ||
results = [sentryflow_metrics_pb2.APIClassificationSingleResponse(merged=ml_result, fields=[]) for ml_result | ||
in ml_results] | ||
yield sentryflow_metrics_pb2.APIClassificationResponse(response=results) | ||
|
||
return str(set(parsed)) | ||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', port=5000) | ||
hs = HandlerServer() | ||
hs.init_grpc_servers() | ||
hs.serve() |
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,24 @@ | ||
import os | ||
import uuid | ||
|
||
import grpc | ||
|
||
from protobuf import sentryflow_metrics_pb2_grpc | ||
from protobuf import sentryflow_metrics_pb2 | ||
|
||
if __name__ == "__main__": | ||
try: | ||
listen_addr = os.environ["AI_ENGINE_ADDRESS"] | ||
except KeyError: | ||
listen_addr = "0.0.0.0:5000" | ||
|
||
with grpc.insecure_channel(listen_addr) as channel: | ||
stub = sentryflow_metrics_pb2_grpc.SentryFlowMetricsStub(channel) | ||
req = sentryflow_metrics_pb2.APIClassificationRequest(paths=["/api/test", "/api/test/" + str(uuid.uuid4())]) | ||
|
||
try: | ||
response_stream = stub.GetAPIClassification(req) | ||
for response in response_stream: | ||
print("Response: ", str(response)) | ||
except grpc.RpcError as e: | ||
print("Error occurred during RPC:", e) |
Binary file not shown.
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
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
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,18 @@ | ||
syntax = "proto3"; | ||
|
||
package protobuf; | ||
|
||
option go_package = "sentryflow/protobuf"; | ||
|
||
message APIClassificationRequest { | ||
string path = 1; | ||
} | ||
|
||
message APIClassificationResponse { | ||
string merged = 1; | ||
repeated string fields = 2; | ||
} | ||
|
||
service SentryFlowMetrics { | ||
rpc GetAPIClassification(stream APIClassificationRequest) returns (stream APIClassificationResponse); | ||
} |
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
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