OCF176: Update visibility control API tests and complete workflow with the access decision (restriction during the discovery process)

Proposers

  • Claudia Carballo González (i2CAT)
  • Cesar Cajas (i2CAT)
  • Jorge Moratinos (Telefonica) ...

Description

First version of the complete implementation of the Visibility Control API covering rules management and access decision

Demo or definition of done

Describe which high level conditions needs to be fulfilled to demonstrate this feature implementation is completed. You can reference external content (example, demo paper) listed in section "References" as [Ref-2].

References

  1. Reference name
  2. Author1, Author2, Author3, et. al., “My demo using feature,” in Conference-Name Demo Track, 20XX.

Acknowledgements

This work is funded by the European Commission through the project with Grant Agreement number . example HORIZON-JU-SNS-2022 FLEX-SCALE project with Grant Agreement number 101096909.

Merge request reports

Loading
+41 −2
Changes for services/TS29222_CAPIF_Discover_Service_API/service_apis/core/discoveredapis.py: 41 added lines, 2 removed lines.
Original line number Diff line number Diff line

import json
import os
import requests

from flask import current_app

@@ -11,6 +13,7 @@ from ..util import serialize_clean_camel_case
from ..vendor_specific import (filter_apis_with_vendor_specific_params,
                               find_attribute_in_body,
                               remove_vendor_specific_fields)
from ..encoder import CustomJSONEncoder

TOTAL_FEATURES = 4
SUPPORTED_FEATURES_HEX = "2"
@@ -20,7 +23,8 @@ def filter_fields(filtered_apis):
    key_filter = [
        "api_name", "api_id", "aef_profiles", "description",
        "supported_features", "shareable_info", "service_api_category",
        "api_supp_feats", "pub_api_path", "ccf_id", "api_status"
        "api_supp_feats", "pub_api_path", "ccf_id", "api_status",
        "api_prov_name"
    ]
    field_filtered_api = {}
    for key in filtered_apis.keys():
@@ -117,6 +121,42 @@ class DiscoverApisOperations(Resource):
            if len(json_docs) == 0:
                return not_found_error(detail="API Invoker " + api_invoker_id + " has no API Published that accomplish filter conditions", cause="No API Published accomplish filter conditions")

            # Visibility Control Integration

            try:
                visibility_control_url = os.getenv(
                    "VISIBILITY_CONTROL_URL",
                    "http://helper:8080/helper/visibility-control/decision/invokers/{}/discoverable-apis"
                ).format(api_invoker_id)
                visibility_payload = {
                    "serviceAPIDescriptions": json_docs
                }

                current_app.logger.debug("Calling visibility control for invoker: " + api_invoker_id)
                visibility_response = requests.post(
                    visibility_control_url,
                    data=json.dumps(visibility_payload, cls=CustomJSONEncoder),
                    headers={"Content-Type": "application/json"},
                    timeout=int(os.getenv("TIMEOUT", "10"))
                )

                if visibility_response.status_code == 200:
                    filtered_data = visibility_response.json()
                    json_docs = filtered_data.get("serviceAPIDescriptions", [])
                    current_app.logger.debug(f"Visibility control filtered {len(json_docs)} APIs for invoker {api_invoker_id}")
                else:
                    current_app.logger.warning(f"Visibility control returned status {visibility_response.status_code}: {visibility_response.text}")

            except requests.exceptions.RequestException as e:
                current_app.logger.warning(f"Visibility control unreachable for invoker {api_invoker_id}: {str(e)}")
            except Exception as e:
                current_app.logger.warning(f"Error filtering APIs for invoker {api_invoker_id}: {str(e)}")

            if len(json_docs) == 0:
                return not_found_error(detail="API Invoker " + api_invoker_id + " has no visible APIs after applying visibility rules", cause="No APIs visible after visibility filtering")

            # End of Visibility Control Integration
            
            apis_discovered = DiscoveredAPIs(service_api_descriptions=json_docs)
            res = make_response(object=serialize_clean_camel_case(apis_discovered), status=200)
            return res
@@ -125,4 +165,3 @@ class DiscoverApisOperations(Resource):
            exception = "An exception occurred in discover services"
            current_app.logger.error(exception + "::" + str(e))
            return internal_server_error(detail=exception, cause=str(e))
+38 −2
Changes for services/helper/helper_service/services/visibility_control/controllers/decision_controller.py: 38 added lines, 2 removed lines.
Original line number Diff line number Diff line
import connexion
import logging
from typing import Dict
from typing import Tuple
from typing import Union

from visibility_control.auth import cert_validation
from visibility_control.models.discovered_apis import DiscoveredAPIs  # noqa: E501
from visibility_control.models.discovery_request import DiscoveryRequest  # noqa: E501
from visibility_control.models.error import Error  # noqa: E501
from visibility_control import util
from visibility_control.core import visibility_control_core

logger = logging.getLogger(__name__)

def decision_invokers_api_invoker_id_discoverable_apis_get(api_invoker_id):  # noqa: E501
#@cert_validation()
def decision_invokers_api_invoker_id_discoverable_apis_post(api_invoker_id, body=None):  # noqa: E501
    """Get discoverable APIs filter for an invoker (global scope)

    Returns a filtered list of APIs for the API Invoker.  # noqa: E501

    :param api_invoker_id: CAPIF API Invoker identifier
    :type api_invoker_id: str
    :param body: List of all discovered APIs to be filtered
    :type body: DiscoveryRequest

    :rtype: Union[DiscoveredAPIs, Tuple[DiscoveredAPIs, int], Tuple[DiscoveredAPIs, int, Dict[str, str]]
    """
    return 'do some magic!'
    # Handle body parameter - it may come as None from Connexion
    if body is None:
        if connexion.request.is_json:
            body = connexion.request.get_json()
        else:
            return {'code': 400, 'message': 'JSON body required'}, 400

    if body is None or body.get('serviceAPIDescriptions') is None:
        return {'code': 400, 'message': 'Missing serviceAPIDescriptions in request body'}, 400

    logger.debug(
        f"Visibility decision request for invoker {api_invoker_id}, request body keys: {list(body.keys())}"
    )

    try:
        apis_list = body.get('serviceAPIDescriptions', [])
        logger.debug(
            f"Visibility decision input API count: {len(apis_list)} for invoker {api_invoker_id}"
        )
        result = visibility_control_core.get_discoverable_apis(api_invoker_id, apis_list)
        logger.debug(
            f"Visibility decision returned {len(result)} APIs for invoker {api_invoker_id}"
        )
        return {"serviceAPIDescriptions": result}, 200
    except Exception as e:
        logger.error(
            f"Visibility decision processing failed for invoker {api_invoker_id}: {str(e)}"
        )
        return {"error": str(e)}, 400
+265
Changes for services/helper/helper_service/services/visibility_control/core/visibility_control_core.py: 265 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,8 +4,10 @@ from db.db import get_mongo
from config import Config

from flask import request
import logging
from visibility_control.core.validate_user import ControlAccess

logger = logging.getLogger(__name__)
valid_user = ControlAccess()


@@ -268,3 +270,266 @@ def update_rule_patch(rule_id, body):
    # Return the fully updated object (excluding Mongo's internal _id)
    updated_rule = col.find_one({"ruleId": rule_id}, {"_id": 0})
    return updated_rule, 200


def get_discoverable_apis(api_invoker_id, all_apis):
    """
    Filter APIs based on visibility rules for a specific invoker.

    :param api_invoker_id: The ID of the API invoker
    :param all_apis: List of all discovered APIs (as dict objects)
    :return: List of APIs that the invoker is allowed to see
    """
    logger.debug(
        f"get_discoverable_apis called for invoker {api_invoker_id} with {len(all_apis)} discovered APIs"
    )
    db = get_mongo()
    rules_col = db.get_col_by_name("visibility_rules")

    rules = list(rules_col.find({
        "$or": [
            {"enabled": True},
            {"enabled": {"$exists": False}}
        ]
    }, {"_id": 0}))
    active_rules = [rule for rule in rules if _rule_is_active(rule)]

    logger.debug(
        f"Found {len(active_rules)} active visibility rules for invoker {api_invoker_id}"
    )

    if not active_rules:
        logger.debug(
            f"No active visibility rules for invoker {api_invoker_id}; default ALLOW applies"
        )
        return all_apis

    # Note_CCG: The rule can have ALLOW or DENY as default access. 
    # The point is to check the exceptions for the final decision (filtered APIs)
    # I think we don´t need this part ´has_allow_rules´ (To be checked)
    has_allow_rules = any(
        rule.get('default_access', rule.get('defaultAccess', 'ALLOW')) == 'ALLOW'
        for rule in active_rules
    )

    # Filter APIs based on rules
    discoverable_apis = []
    for api in all_apis:
        if _invoker_allowed_for_api(
            api_invoker_id,
            api,
            active_rules,
            default_allow_no_match=not has_allow_rules
        ):
            discoverable_apis.append(api)

    return discoverable_apis


def _invoker_allowed_for_api(api_invoker_id, api, rules, default_allow_no_match=True):
    """
    Note_CCG: Here we should only call the _rule_specificity (the list 
    with the API and the winner rule). Before (out of this method) execute the logic to match rules 
    with APIs and with the invoker. Then, we should check if the invoker is allowed or denied to discover 
    each API based on the default access of the winner rule and the invoker exception. Note than even 
    if a rule does not explicitly mention invoker_id, it can affect the Invoker.
    Check if an invoker is allowed to see an API based on the winner rule.
    
    :param api_invoker_id: The invoker ID
    :param api: The API description (dict)
    :param rules: List of active rules
    :param default_allow_no_match: Whether to allow APIs that do not match any rule
    :return: True if allowed, False otherwise
    """
    matching_rules = [rule for rule in rules if _rule_matches_api(rule, api)]
    if not matching_rules:
        logger.debug(
            f"No matching visibility rules for API {api.get('apiId') or api.get('api_id')} and invoker {api_invoker_id}; default_allow_no_match={default_allow_no_match}"
        )
        return default_allow_no_match

    #winner_rule = max(matching_rules, key=lambda rule: _rule_specificity(rule))
    winner_rule = max(
        matching_rules, 
        key=lambda rule: (
            _rule_specificity(rule), 
            _parse_datetime(rule.get('updatedAt', rule.get('startsAt', '1970-01-01T00:00:00Z')))
        )
    )
    default_access = _rule_default_access(winner_rule)
    allowed = default_access == 'ALLOW'
    invoker_exception = _rule_matches_invoker_exception(winner_rule, api_invoker_id)

    logger.debug(
        f"Winner rule for API {api.get('apiId') or api.get('api_id')} and invoker {api_invoker_id}: ruleId={winner_rule.get('ruleId')} default_access={default_access} invoker_exception={invoker_exception}"
    )

    if invoker_exception:
        return not allowed

    return allowed


def _rule_matches_api(rule, api):
    """
    Note_CCG: here we should have as result, for each API, the corresponding rules (zero, one or more)
    
    Check if a rule matches an API based on provider selector.

    :param rule: The visibility rule
    :param api: The API description (dict)
    :return: True if the rule applies to this API
    """
    provider_selector = rule.get('providerSelector', {})
    logger.debug(
        f"Checking API rule match for API {api.get('apiId') or api.get('api_id')} against rule {rule.get('ruleId')} selector={provider_selector}"
    )

    if not provider_selector:
        return True  # No selector = matches all APIs

    # Check apiProviderId against apiProvName
    if 'apiProviderId' in provider_selector:
        api_provider_ids = _as_list(provider_selector['apiProviderId'])
        api_provider_id = _get_first(api, 'apiProvName', 'api_prov_name')
        if not _matches_any(api_provider_id, api_provider_ids):
            return False

    # Check userName against apiProvName or provider username if present
    # if 'userName' in provider_selector:
    #     user_names = _as_list(provider_selector['userName'])
    #     api_user_name = _get_first(api, 'apiProvName', 'api_prov_name')
    #     if not _matches_any(api_user_name, user_names):
    #         return False

    # Check apiName
    if 'apiName' in provider_selector:
        api_names = _as_list(provider_selector['apiName'])
        api_name = _get_first(api, 'apiName', 'api_name')
        if not _matches_any(api_name, api_names):
            return False

    # Check apiId
    if 'apiId' in provider_selector:
        api_ids = _as_list(provider_selector['apiId'])
        api_id = _get_first(api, 'apiId', 'api_id')
        if not _matches_any(api_id, api_ids):
            return False

    # Check aefId in nested profiles
    if 'aefId' in provider_selector:
        aef_ids = _as_list(provider_selector['aefId'])
        aef_profiles = _get_first(api, 'aefProfiles', 'aef_profiles') or []
        aef_match = False
        for profile in aef_profiles:
            if _matches_any(_get_first(profile, 'aefId', 'aef_id'), aef_ids):
                aef_match = True
                break
        if not aef_match:
            return False

    return True


def _rule_matches_invoker_exception(rule, api_invoker_id):
    """
    Note_CCG: Here we should call _rule_matches_api and filter, before choosing the winner rule, 
    the rules that match with the Invoker.
    Consider that a rule can apply to an invoker even if the id is not explictly mentioned. 

    :param rule: The visibility rule
    :param api_invoker_id: The invoker ID
    :return: True if the invoker is in the exception selector
    """
    invoker_selector = rule.get('invokerExceptions') or rule.get('invokerSelector') or {}
    logger.debug(
        f"Checking invoker exception for invoker {api_invoker_id} on rule {rule.get('ruleId')} selector={invoker_selector}"
    )

    if not invoker_selector:
        return False

    invoker_ids = (
        invoker_selector.get('apiInvokerId') or
        invoker_selector.get('api_invoker_id') or
        invoker_selector.get('invokerId')
    )
    if invoker_ids is None:
        return False

    return _matches_any(api_invoker_id, _as_list(invoker_ids))


def _rule_specificity(rule):
    """
    Note_CCG: to call here _rule_matches_invoker and use the result as an output to execute the specificity 
    function. For each API, select the winner rule 
    (the one with higher specificity).
    ----
    Calculate rule specificity for ordering.
    Higher specificity = more specific selectors = higher priority.

    :param rule: The visibility rule
    :return: Specificity score
    """
    specificity = 0
    provider_selector = rule.get('providerSelector', {})

    # Each selector adds to specificity
    for selector in ['apiProviderId', 'userName', 'apiName', 'aefId', 'apiId']:
        if selector in provider_selector:
            specificity += len(_as_list(provider_selector[selector]))

    return specificity


def _rule_is_active(rule):
    now = datetime.now(timezone.utc)
    starts_at = _parse_datetime(rule.get('startsAt'))
    ends_at = _parse_datetime(rule.get('endsAt'))

    if starts_at and starts_at > now:
        return False
    if ends_at and ends_at < now:
        return False
    return True


def _parse_datetime(value):
    if not value:
        return None
    if isinstance(value, datetime):
        parsed = value
    else:
        try:
            parsed = datetime.fromisoformat(str(value).replace('Z', '+00:00'))
        except ValueError:
            return None
    if parsed.tzinfo is None:
        return parsed.replace(tzinfo=timezone.utc)
    return parsed.astimezone(timezone.utc)


def _rule_default_access(rule):
    return rule.get('default_access', rule.get('defaultAccess', 'ALLOW'))


def _get_first(source, *keys):
    for key in keys:
        if key in source:
            return source[key]
    return None


def _as_list(value):
    if value is None:
        return []
    if isinstance(value, list):
        return value
    return [value]


def _matches_any(value, allowed_values):
    if '*' in allowed_values:
        return True
    return value is not None and value in allowed_values
+3
Changes for services/helper/helper_service/services/visibility_control/models/discovery_request.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
class DiscoveryRequest:
    def __init__(self, *args, **kwargs):
        pass
+2 −2
Changes for services/helper/helper_service/services/visibility_control/openapi/openapi.yaml: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -27,10 +27,10 @@ tags:
  name: Decision
paths:
  /decision/invokers/{apiInvokerId}/discoverable-apis:
    get:
    post:
      description: |
        Returns a filtered list of APIs for the API Invoker.
      operationId: decision_invokers_api_invoker_id_discoverable_apis_get
      operationId: decision_invokers_api_invoker_id_discoverable_apis_post
      parameters:
      - description: CAPIF API Invoker identifier
        explode: false
Loading
Loading