Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #80 from EVOLVED-5G/CAPIF_backoffice_integration
Browse files Browse the repository at this point in the history
CAPIF update internal resources
  • Loading branch information
raxonros authored Mar 10, 2023
2 parents 40e9c13 + c8ee1b7 commit 82978a6
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 44 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [CAPIF Tool Release 2.0](#capif-tool-release-20)
- [CAPIF Tool Release 2.1](#capif-tool-release-21)
- [CAPIF Tool Release 3.0](#capif-tool-release-30)
- [CAPIF Tool Release 3.1](#capif-tool-release-31)


# Repository structure
Expand Down Expand Up @@ -250,6 +251,13 @@ Changes at Tests:
* Complete code refactor of all tests
* Complete test plan review, including all services (except auditing and logging)

# CAPIF Tool Release 3.1

* Delete a service automatically if the provider that contains the APF that published it is deleted
* Clear the security context of an invoker automatically if the invoker is deleted
* Delete automatically the entry in the security info of the security context if the provider that has the aef that published the service is deleted
* Delete automatically the entry in the security info of the security context if the service on which that context was created is deleted




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def onboarded_invokers_onboarding_id_delete(onboarding_id): # noqa: E501
if res.status_code == 204:
current_app.logger.info("Invoker Removed")
publisher_ops.publish_message("events", "API_INVOKER_UPDATED")
publisher_ops.publish_message("internal-messages", "invoker-removed:"+onboarding_id)
publisher_ops.publish_message("internal-messages", f"invoker-removed:{onboarding_id}")

return res

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import redis
import sys
from flask import current_app

class Publisher():

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
flask_jwt_extended
pyopenssl
rfc3987
redis
flask_executor
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0
rfc3987 == 1.3.8
redis == 4.5.1
flask_executor == 1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ def delete_api_provider_enrolment_details(self, api_prov_dom_id):
if isinstance(result, Response):
return result

apf_id = [ provider_func['api_prov_func_id'] for provider_func in result["api_prov_funcs"] if provider_func['api_prov_func_role'] == 'APF' ]
aef_id = [ provider_func['api_prov_func_id'] for provider_func in result["api_prov_funcs"] if provider_func['api_prov_func_role'] == 'AEF' ]
mycol.delete_one({'api_prov_dom_id': api_prov_dom_id})
out = "The provider matching apiProvDomainId " + api_prov_dom_id + " was offboarded."
current_app.logger.debug("Removed provider domain from database")
self.publish_ops.publish_message("internal-messages", f"provider-removed:{aef_id[0]}:{apf_id[0]}")
return make_response(object=out, status=204)

except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import redis
import sys

class Publisher():

def __init__(self):
self. r = redis.Redis(host='redis', port=6379, db=0)

def publish_message(self, channel, message):
self.r.publish(channel, message)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from abc import ABC, abstractmethod
from ..db.db import MongoDatabse
from .publisher import Publisher

class Resource(ABC):

def __init__(self):
self.db = MongoDatabse()
self.db = MongoDatabse()
self.publish_ops = Publisher()
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
flask_jwt_extended
pyopenssl
redis == 4.5.1
flask_executor == 1.0.0
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0
rfc3987 == 1.3.8

5 changes: 2 additions & 3 deletions services/TS29222_CAPIF_Auditing_API/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
elasticsearch == 8.4.3
flask_jwt_extended
pyopenssl
flask-mqtt
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0
4 changes: 2 additions & 2 deletions services/TS29222_CAPIF_Discover_Service_API/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
flask_jwt_extended
pyopenssl
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0
4 changes: 1 addition & 3 deletions services/TS29222_CAPIF_Events_API/capif_events/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from multiprocessing import Process
from threading import Thread
from flask_executor import Executor
from flask_apscheduler import APScheduler
from logging.handlers import RotatingFileHandler


Expand Down Expand Up @@ -61,8 +60,7 @@ def verbose_formatter():
configure_logging(app.app)
executor = Executor(app.app)
subscriber = Subscriber()
scheduler = APScheduler()
scheduler.init_app(app.app)


@app.app.before_first_request
def create_listener_message():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def listen(self):
self.notification.send_notifications(raw_message["data"].decode('utf-8'))

elif raw_message["type"] == "message" and raw_message["channel"].decode('utf-8') == "internal-messages":
message, invoker_id = raw_message["data"].decode('utf-8').split(":")
message, *invoker_id = raw_message["data"].decode('utf-8').split(":")
if message == "invoker-removed":
current_app.logger.debug("Recevived message, invoker remove, removing event subscriptions")
self.event_ops.delete_all_events(invoker_id)
self.event_ops.delete_all_events(invoker_id[0])



12 changes: 6 additions & 6 deletions services/TS29222_CAPIF_Events_API/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
flask_jwt_extended
pyopenssl
rfc3987
redis
flask_executor
Flask-APScheduler
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0
rfc3987 == 1.3.8
redis == 4.5.1
flask_executor == 1.0.0

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
elasticsearch == 8.4.3
flask_jwt_extended
pyopenssl
flask-mqtt
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from pymongo import MongoClient
from .config import Config
from logging.handlers import RotatingFileHandler
from .core.consumer_messager import Subscriber
from flask_executor import Executor


def configure_logging(app):
Expand Down Expand Up @@ -50,6 +52,12 @@ def verbose_formatter():
jwt = JWTManager(app.app)
configure_logging(app.app)

executor = Executor(app.app)
subscriber = Subscriber()

@app.app.before_first_request
def up_listener():
executor.submit(subscriber.listen)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def apf_id_service_apis_service_api_id_delete(service_api_id, apf_id): # noqa:
if res.status_code == 204:
current_app.logger.info("Removed service published")
publisher_ops.publish_message("events", "SERVICE_API_UNAVAILABLE")
publisher_ops.publish_message("internal-messages", f"service-removed:{service_api_id}")

return res

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# subscriber.py
import redis
import time
import sys
import json
import asyncio
from threading import Thread
from .internal_service_ops import InternalServiceOps
from flask import current_app

class Subscriber():

def __init__(self):
self.r = redis.Redis(host='redis', port=6379, db=0)
self.security_ops = InternalServiceOps()
self.p = self.r.pubsub()
self.p.subscribe("internal-messages")

def listen(self):
current_app.logger.info("Listening publish messages")
for raw_message in self.p.listen():
if raw_message["type"] == "message" and raw_message["channel"].decode('utf-8') == "internal-messages":
message, *ids = raw_message["data"].decode('utf-8').split(":")
if message == "provider-removed":
self.security_ops.delete_intern_service(ids[1])




Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from flask import current_app
from .resources import Resource

class InternalServiceOps(Resource):

def delete_intern_service(self, apf_id):

current_app.logger.info("Provider removed, removing services published by APF")
mycol = self.db.get_col_by_name(self.db.service_api_descriptions)
my_query = {'apf_id': apf_id}
mycol.delete_many(my_query)
current_app.logger.info("Removed service")
7 changes: 4 additions & 3 deletions services/TS29222_CAPIF_Publish_Service_API/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
flask_jwt_extended
pyopenssl
redis
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0
redis == 4.5.1
flask_executor == 1.0.0

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from .core.consumer_messager import Subscriber
from threading import Thread
from flask_executor import Executor
from flask_apscheduler import APScheduler
from logging.handlers import RotatingFileHandler
import sys

Expand Down Expand Up @@ -56,8 +55,6 @@ def main():

JWTManager(app.app)
subscriber = Subscriber()
scheduler = APScheduler()
scheduler.init_app(app.app)
configure_logging(app.app)

executor = Executor(app.app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ def __init__(self):
self.p.subscribe("internal-messages")

def listen(self):
current_app.logger.info("Listening security context messages")
for raw_message in self.p.listen():
if raw_message["type"] == "message" and raw_message["channel"].decode('utf-8') == "internal-messages":
message, invoker_id = raw_message["data"].decode('utf-8').split(":")
message, *ids = raw_message["data"].decode('utf-8').split(":")
if message == "invoker-removed":
self.security_ops.delete_intern_servicesecurity(invoker_id)
self.security_ops.delete_intern_servicesecurity(ids[0])
if message == "provider-removed" or message == "service-removed":
self.security_ops.update_intern_servicesecurity(ids[0])




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,19 @@ def delete_intern_servicesecurity(self, api_invoker_id):
mycol = self.db.get_col_by_name(self.db.security_info)
my_query = {'api_invoker_id': api_invoker_id}
mycol.delete_many(my_query)
current_app.logger.info("Removed security context")
current_app.logger.info("Removed security context")

def update_intern_servicesecurity(self, id):

current_app.logger.info("Provider Removed, updating security context")
security_col = self.db.get_col_by_name(self.db.security_info)


security_contexts = security_col.find({"$or":[{"security_info.aef_id":id}, {"security_info.api_id":id}]})

for security_context in security_contexts:
new_security_info = [info for info in security_context["security_info"] if info["aef_id"]!=id and info["api_id"] != id]
security_context["security_info"] = new_security_info
security_col.update_one({'api_invoker_id':security_context["api_invoker_id"]}, {"$set":security_context})

current_app.logger.info("Updated security context")
12 changes: 5 additions & 7 deletions services/TS29222_CAPIF_Security_API/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 2.0.3
pymongo == 4.0.1
flask_jwt_extended
pyjwt
rfc3987
redis
pyopenssl
flask_executor
Flask-APScheduler
flask_jwt_extended == 4.4.4
pyopenssl == 23.0.0
rfc3987 == 1.3.8
redis == 4.5.1
flask_executor == 1.0.0

0 comments on commit 82978a6

Please sign in to comment.