Skip to content

Commit

Permalink
feat: add ssl (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurasgkadri98 authored Aug 27, 2024
1 parent a358766 commit 30fb28e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
7 changes: 6 additions & 1 deletion configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
FLOWKIT_PYTHON_API_KEY: "flowkit-python-api-key"
FLOWKIT_PYTHON_API_KEY: flowkit-python-api-key
FLOWKIT_PYTHON_ENDPOINT: http://0.0.0.0:50052
FLOWKIT_PYTHON_WORKERS: 2
USE_SSL: False
#SSL_CERT_PUBLIC_KEY_FILE:
#SSL_CERT_PRIVATE_KEY_FILE:
5 changes: 1 addition & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ RUN pip install --no-cache-dir .[all]

EXPOSE 50052

# Set default number of workers
ENV WORKERS=4

# Use the environment variable in CMD
CMD ["sh", "-c", "allie-flowkit-python --host 0.0.0.0 --port 50052 --workers $WORKERS"]
CMD ["sh", "-c", "allie-flowkit-python"]
46 changes: 40 additions & 6 deletions src/allie/flowkit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,54 @@
"""Main module for the FlowKit service."""

try:
from allie.flowkit.config._config import CONFIG
import uvicorn
except ImportError:
raise ImportError("Please install uvicorn to run the service: pip install allie-flowkit-python[all]")
import argparse
from urllib.parse import urlparse


def parse_cli_args():
"""Parse the command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, required=False, help="The host to run the service on. By default 0.0.0.0")
parser.add_argument("--port", type=int, required=False, help="The port to run the service on. By default 50052")
parser.add_argument("--workers", type=int, required=False, help="The number of workers to use. By default 4")
parser.add_argument("--use-ssl", required=False, help="Enable SSL for the service. By default False")
parser.add_argument("--ssl-keyfile", type=str, required=False, help="The SSL key file path")
parser.add_argument("--ssl-certfile", type=str, required=False, help="The SSL certificate file path")
return parser.parse_args()


def substitute_empty_values(args):
"""Substitute the empty values with configuration values."""
host = args.host or urlparse(CONFIG.flowkit_python_endpoint).hostname
port = args.port or urlparse(CONFIG.flowkit_python_endpoint).port
workers = args.workers or CONFIG.flowkit_python_workers
use_ssl = (args.use_ssl.lower() == "true") if args.use_ssl is not None else CONFIG.use_ssl
ssl_keyfile = args.ssl_keyfile or CONFIG.ssl_cert_private_key_file
ssl_certfile = args.ssl_certfile or CONFIG.ssl_cert_public_key_file
return host, port, workers, use_ssl, ssl_keyfile, ssl_certfile


def main():
"""Run entrypoint for the FlowKit service."""
parse = argparse.ArgumentParser()
parse.add_argument("--host", type=str, default="0.0.0.0", help="The host to run the service on. By default 0.0.0.0")
parse.add_argument("--port", type=int, default=50052, help="The port to run the service on. By default 50052")
parse.add_argument("--workers", type=int, default=4, help="The number of workers to use. By default 4")
args = parse.parse_args()
uvicorn.run("allie.flowkit:flowkit_service", host=args.host, port=args.port, workers=args.workers)
# Parse the command line arguments
args = parse_cli_args()

# Substitute the empty values with configuration values
host, port, workers, use_ssl, ssl_keyfile, ssl_certfile = substitute_empty_values(args)

# Run the service
uvicorn.run(
"allie.flowkit.flowkit_service:flowkit_service",
host=host,
port=port,
workers=workers,
ssl_keyfile=ssl_keyfile if use_ssl else None,
ssl_certfile=ssl_certfile if use_ssl else None,
)


if __name__ == "__main__":
Expand Down
8 changes: 8 additions & 0 deletions src/allie/flowkit/config/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ def __init__(self):
"""
config_path = os.getenv("ALLIE_CONFIG_PATH", "config.yaml")
self._yaml = self._load_config(config_path)

# Define the configuration variables to be parsed from the YAML file
self.flowkit_python_api_key = self._yaml.get("FLOWKIT_PYTHON_API_KEY")
self.flowkit_python_endpoint = self._yaml.get("FLOWKIT_PYTHON_ENDPOINT", "http://localhost:50052")
self.flowkit_python_workers = self._yaml.get("FLOWKIT_PYTHON_WORKERS", 4)
self.use_ssl = self._yaml.get("USE_SSL", False)
self.ssl_cert_public_key_file = self._yaml.get("SSL_CERT_PUBLIC_KEY_FILE")
self.ssl_cert_private_key_file = self._yaml.get("SSL_CERT_PRIVATE_KEY_FILE")

# Check the mandatory configuration variables
if not self.flowkit_python_api_key:
raise ValueError("FLOWKIT_PYTHON_API_KEY is missing in the configuration file.")

Expand Down

0 comments on commit 30fb28e

Please sign in to comment.