Commit f1a17e69 authored by Jorge Moratinos's avatar Jorge Moratinos
Browse files

Merge branch 'OCF175-how-to-add-new-api-to-helper-service-2' into 'staging'

Ocf175 how to add new api to helper service 2

See merge request !160
parents 266e8080 60a530da
Loading
Loading
Loading
Loading
Loading
+11
Changes for helm/capif/charts/ocf-helper/templates/ocf-helper-configmap.yaml: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,3 +27,14 @@ data:
    {{- if .Values.capifConfiguration }}
    capif_configuration: {{ .Values.capifConfiguration | toYaml | nindent 6 }}
    {{- end }}

    package_paths: {
      "helper_api": {
        "path": "/api",
        "openapi_file": "api/openapi/openapi.yaml"
      },
      "configuration_api": {
        "path": "/configuration",
        "openapi_file": "configuration/openapi/openapi.yaml"
      }
    }
 No newline at end of file
+1
Changes for services/docker-compose-capif.yml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ services:
    restart: unless-stopped
    volumes:
      - ${SERVICES_DIR}/helper/config.yaml:/usr/src/app/config.yaml
      - ${SERVICES_DIR}/helper/helper_service/certs:/usr/src/app/helper_service/certs
    extra_hosts:
      - host.docker.internal:host-gateway
      - fluent-bit:host-gateway
+28 −19
Changes for services/helper/config.yaml: 28 added lines, 19 removed lines.
Original line number Diff line number Diff line
mongo: {
  'user': 'root',
  'password': 'example',
  'db': 'capif',
  'invoker_col': 'invokerdetails',
  'provider_col': 'providerenrolmentdetails',
  'col_services': "serviceapidescriptions",
  'col_security': "security",
  'col_event': "eventsdetails",
  'col_capif_configuration': "capif_configuration",
  'host': 'mongo',
  'port': "27017"
}
mongo:
  user: root
  password: example
  db: capif
  invoker_col: invokerdetails
  provider_col: providerenrolmentdetails
  col_services: serviceapidescriptions
  col_security: security
  col_event: eventsdetails
  col_capif_configuration: capif_configuration
  host: mongo
  port: 27017


ca_factory: 
  url: vault
  port: 8200
  token: dev-only-token
  verify: False

ca_factory: {
  "url": "vault",
  "port": "8200",
  "token": "dev-only-token",
  "verify": False
}

capif_configuration: 
  config_description: Default CAPIF Configuration
@@ -36,3 +36,12 @@ capif_configuration:
      oauth: 1
      pki: 2
      psk: 3
  
package_paths:
  helper_api:
    path: /api
    openapi_file: api/openapi/openapi.yaml
  configuration_api:
    path: /configuration
    openapi_file: configuration/openapi/openapi.yaml
+50 −6
Changes for services/helper/helper_service/app.py: 50 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -3,17 +3,31 @@ import logging
import os
import requests
from config import Config
from controllers.helper_controller import helper_routes
from db.db import MongoDatabse
from pathlib import Path
from db.db import get_mongo
import connexion
import sys
from flask import Flask
from OpenSSL.crypto import FILETYPE_PEM, TYPE_RSA, PKey, X509Req, dump_certificate_request, dump_privatekey
from asgiref.wsgi import WsgiToAsgi

app = Flask(__name__)
# --- Paths setup: make 'services' discoverable so "import api..." works ---
BASE_DIR = Path(__file__).resolve().parent
SERVICES_DIR = BASE_DIR / "services"

# Insert services directory at front of sys.path
if SERVICES_DIR.is_dir():
    services_path_str = str(SERVICES_DIR)
    if services_path_str not in sys.path:
        sys.path.insert(0, services_path_str)
else:
    raise RuntimeError(f"Services directory not found at {SERVICES_DIR!s}")

app = connexion.App(__name__, specification_dir=str(SERVICES_DIR))
config = Config().get_config()

# Connect MongoDB and get TTL for superadmin certificate
db = MongoDatabse()
db = get_mongo()
capif_config = db.get_col_by_name("capif_configuration").find_one({})
ttl_superadmin_cert = capif_config["settings"]["certificates_expiry"].get("ttl_superadmin_cert", "43000h")

@@ -77,7 +91,37 @@ cert_file = open("certs/ca_root.crt", 'wb')
cert_file.write(bytes(ca_root, 'utf-8'))
cert_file.close()

app.register_blueprint(helper_routes)
app.logger.setLevel(numeric_level)

package_paths = config.get("package_paths", {})

if not package_paths:
    logger.error("No package paths defined in configuration.")
    raise Exception("No package paths defined in configuration.")

# Dynamically add all APIs defined in package_paths
for name, pkg in package_paths.items():
    openapi_file = pkg.get("openapi_file")
    base_path = pkg.get("path")

    if not openapi_file or not base_path:
        logger.warning(f"Skipping package_path '{name}' because 'openapi_file' or 'path' is missing.")
        continue

    # Build a readable title from the key, e.g. "helper_api" -> "Helper Api"
    title = name.replace("_", " ").title()

    logger.info(
        f"Adding API '{name}': openapi_file='{openapi_file}', base_path='{base_path}', title='{title}'"
    )

    app.add_api(
        openapi_file,             # relative to specification_dir (SERVICES_DIR)
        arguments={"title": title},
        pythonic_params=True,
        base_path=base_path
    )


app.app.logger.setLevel(numeric_level)

asgi_app = WsgiToAsgi(app)
 No newline at end of file
−199

File deleted.

Loading