Commit 071c7e47 authored by Laura Dominguez's avatar Laura Dominguez
Browse files

Re-Key implementation

parent 29547d98
Loading
Loading
Loading
Loading
+39 −0
Changes for src/ccips/client/DeviceClient.py: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ from common.Settings import get_service_host, get_service_port_grpc
from common.tools.client.RetryDecorator import retry, delay_exponential
from common.tools.grpc.Tools import grpc_message_to_json_string
from ccips.service.handler import StorageHandler
from ccips.service.notification import SADBExpireNotification

LOGGER = logging.getLogger(__name__)

@@ -105,3 +106,41 @@ class DeviceClient:
        self.check_connection()
        LOGGER.debug("process_rekey_notification payload=%s", grpc_message_to_json_string(notification))
        return None

    @retry(max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION)
    def process_rekey(self, handler_id: str, ipsec_name: str) -> Tuple[Any, Optional[Exception]]:
        self.check_connection()
        try:
            parsed_id = uuid.UUID(str(handler_id))
        except (TypeError, ValueError):
            return None, ValueError("Invalid handler_id")

        if not ipsec_name or not ipsec_name.strip():
            return None, ValueError("Invalid ipsec_name")

        LOGGER.info("process_rekey handler_id=%s ipsec_name=%s", parsed_id, ipsec_name.strip())
        try:
            handler = self.storage._get_handler(parsed_id)
            if handler is None:
                return {"status": "Handler not found; rekey ignored"}, None

            notification = SADBExpireNotification()
            notification.EventType = "sadb-expire"
            notification.IPsecName = ipsec_name.strip()
            notification.SoftLifeTime = True
            result = handler.process_rekey(notification, parsed_id)
            status = result.get("status")
            if status in {"already_processed", "spi_mismatch", "not_found"}:
                return result, None
            if status != "processed":
                return None, RuntimeError(f"Unexpected rekey result: {result}")

            return {
                "status": "Rekey processed successfully",
                "old_spi": result["old_spi"],
                "new_spi": result["new_spi"],
                "ipsec_name": result["ipsec_name"],
            }, None
        except Exception as exc:
            LOGGER.exception("CCIPS rekey failed for handler %s", parsed_id)
            return None, exc
+2 −15
Changes for src/ccips/service/config.py: 2 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -148,20 +148,6 @@ def normalize_ipsec_config_type(value: Any) -> IPsecConfigType:
        ) from None


class SPIManager:
    def __init__(self):
        self.cspi = 0
        self.lock = threading.Lock()

    def get_new_spi(self) -> int:
        with self.lock:
            self.cspi += 1
            return self.cspi


spi_manager = SPIManager()


class IpsecConfig:
    def __init__(self) -> None:
        self._lock = threading.RLock()
@@ -236,7 +222,8 @@ class IpsecConfig:
        raise ValueError(f"Unsupported IPsec config type: {conf_type!r}")

    def set_new_spi(self) -> None:
        self.spi = spi_manager.get_new_spi()
        with self._lock:
            self.spi += 1

    def create_sad_config(self) -> Tuple[str, str]:
        conf_type = self._validated_conf_type()
+207 −117

File changed.

Preview size limit exceeded, changes collapsed.

+2 −1
Changes for src/nbi/service/ccips/__init__.py: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
# limitations under the License.

from nbi.service.NbiApplication import NbiApplication
from nbi.service.ccips.routes import CcipsCreateRoute, CcipsListRoute, CcipsRoute
from nbi.service.ccips.routes import CcipsCreateRoute, CcipsListRoute, CcipsRekeyRoute, CcipsRoute

URL_PREFIX = '/ccips'

@@ -22,3 +22,4 @@ def register_ccips(nbi_app: NbiApplication):
    nbi_app.add_rest_api_resource(CcipsListRoute, URL_PREFIX + '/ccips-all', endpoint='ccips.list')
    nbi_app.add_rest_api_resource(CcipsCreateRoute, URL_PREFIX, endpoint='ccips.create')
    nbi_app.add_rest_api_resource(CcipsRoute, URL_PREFIX + '/<handler_id>', endpoint='ccips.route')
    nbi_app.add_rest_api_resource(CcipsRekeyRoute, URL_PREFIX + '/process_rekey/<handler_id>/<path:ipsec_name>', endpoint='ccips.rekey')
+17 −0

File changed.

Preview size limit exceeded, changes collapsed.