Commit c04e4b9d authored by Carlos Natalino's avatar Carlos Natalino
Browse files

Including the files.

parent 4fdeccc1
Loading
Loading
Loading
Loading
+88 −0
Original line number Diff line number Diff line
# build, tag and push the Docker image to the gitlab registry
build opticalcentralizedattackdetector:
  variables:
    IMAGE_NAME: 'opticalcentralizedattackdetector' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: build
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  after_script:
    - docker rmi $(docker images --quiet --filter=dangling=true)
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"' 
    - changes:
      - src/$IMAGE_NAME/**/*.{py,in,yml}
      - src/$IMAGE_NAME/Dockerfile
      - src/$IMAGE_NAME/tests/*.py
      - src/$IMAGE_NAME/tests/Dockerfile
      - manifests/$IMAGE_NAME.yaml
      - .gitlab-ci.yml

# apply unit test to the opticalcentralizedattackdetector component
unit test opticalcentralizedattackdetector:
  variables:
    IMAGE_NAME: 'opticalcentralizedattackdetector' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: unit_test
  needs:
    - build opticalcentralizedattackdetector
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi
    - if docker container ls | grep influxdb; then docker rm -f influxdb; else echo "influxdb image is not in the system"; fi
    - if docker container ls | grep $IMAGE_NAME; then docker rm -f $IMAGE_NAME; else echo "$IMAGE_NAME image is not in the system"; fi
  script:
    - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker run --name influxdb -d -p 8086:8086 -e INFLUXDB_DB=$INFLUXDB_DATABASE -e INFLUXDB_ADMIN_USER=$INFLUXDB_USER -e INFLUXDB_ADMIN_PASSWORD=$INFLUXDB_PASSWORD -e INXLUXDB_HTTP_AUTH_ENABLED=True --network=teraflowbridge --rm influxdb:1.8
    - docker run --name $IMAGE_NAME -d -p 7070:7070 --env INFLUXDB_USER=$INFLUXDB_USER --env INFLUXDB_PASSWORD=$INFLUXDB_PASSWORD --env INFLUXDB_DATABASE=$INFLUXDB_DATABASE --env INFLUXDB_HOSTNAME=influxdb -v "$PWD/src/$IMAGE_NAME/tests:/opt/results" --network=teraflowbridge  --rm $CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG
    - docker ps -a
    - docker exec -i $IMAGE_NAME bash -c "pytest --junitxml=/opt/results/report.xml"
  after_script:
    - docker rm -f $IMAGE_NAME
    - docker rm -f  influxdb
    - docker network rm teraflowbridge
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"' 
    - changes:
      - src/$IMAGE_NAME/**/*.{py,in,yml}
      - src/$IMAGE_NAME/Dockerfile
      - src/$IMAGE_NAME/tests/*.py
      - src/$IMAGE_NAME/tests/Dockerfile
      - manifests/$IMAGE_NAME.yaml
      - .gitlab-ci.yml
  artifacts:
      when: always
      reports:
        junit: src/$IMAGE_NAME/tests/report.xml

# Deployment of the opticalcentralizedattackdetector service in Kubernetes Cluster
deploy opticalcentralizedattackdetector:
  variables:
    IMAGE_NAME: 'opticalcentralizedattackdetector' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: deploy
  needs:
    - unit test opticalcentralizedattackdetector
    # - integ_test execute
  script:
    - 'sed -i "s/$IMAGE_NAME:.*/$IMAGE_NAME:$IMAGE_TAG/" manifests/$IMAGE_NAME.yaml'
    - kubectl version
    - kubectl get all
    - kubectl apply -f "manifests/$IMAGE_NAME.yaml"
    - kubectl get all
  # environment:
  #   name: test
  #   url: https://example.com
  #   kubernetes:
  #     namespace: test
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
      when: manual    
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
      when: manual
 No newline at end of file
+12 −0
Original line number Diff line number Diff line
import logging

# General settings
LOG_LEVEL = logging.WARNING

# gRPC settings
GRPC_SERVICE_PORT = 10005
GRPC_MAX_WORKERS  = 10
GRPC_GRACE_PERIOD = 60

# Prometheus settings
METRICS_PORT = 9192
+35 −0
Original line number Diff line number Diff line
FROM python:3-slim

# Install dependencies
RUN apt-get --yes --quiet --quiet update && \
    apt-get --yes --quiet --quiet install wget g++ && \
    rm -rf /var/lib/apt/lists/*

# Set Python to show logs as they occur
ENV PYTHONUNBUFFERED=0

# Download the gRPC health probe
RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \
    wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
    chmod +x /bin/grpc_health_probe

# Get generic Python packages
RUN python3 -m pip install --upgrade pip setuptools wheel pip-tools

# Set working directory
WORKDIR /var/teraflow

# Create module sub-folders
RUN mkdir -p /var/teraflow/opticalcentralizedattackdetector

# Get Python packages per module
COPY opticalcentralizedattackdetector/requirements.in opticalcentralizedattackdetector/requirements.in
RUN pip-compile --output-file=opticalcentralizedattackdetector/requirements.txt opticalcentralizedattackdetector/requirements.in
RUN python3 -m pip install -r opticalcentralizedattackdetector/requirements.txt

# Add files into working directory
COPY common/. common
COPY opticalcentralizedattackdetector/. opticalcentralizedattackdetector

# Start opticalcentralizedattackdetector service
ENTRYPOINT ["python", "-m", "opticalcentralizedattackdetector.service"]
+0 −0

Empty file added.

+55 −0
Original line number Diff line number Diff line
import grpc, logging
from common.tools.client.RetryDecorator import retry, delay_exponential
from opticalcentralizedattackdetector.proto.context_pb2 import Empty, Service
from opticalcentralizedattackdetector.proto.monitoring_pb2 import KpiList
from opticalcentralizedattackdetector.proto.optical_centralized_attack_detector_pb2_grpc import OpticalCentralizedAttackDetectorServiceStub

LOGGER = logging.getLogger(__name__)
MAX_RETRIES = 15
DELAY_FUNCTION = delay_exponential(initial=0.01, increment=2.0, maximum=5.0)

class OpticalCentralizedAttackDetectorClient:
    def __init__(self, address, port):
        self.endpoint = '{:s}:{:s}'.format(str(address), str(port))
        LOGGER.debug('Creating channel to {:s}...'.format(str(self.endpoint)))
        self.channel = None
        self.stub = None
        self.connect()
        LOGGER.debug('Channel created')

    def connect(self):
        self.channel = grpc.insecure_channel(self.endpoint)
        self.stub = OpticalCentralizedAttackDetectorServiceStub(self.channel)

    def close(self):
        if(self.channel is not None): self.channel.close()
        self.channel = None
        self.stub = None

    @retry(exceptions=set(), max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect')
    def NotifyServiceUpdate(self, request : Service) -> Empty:
        LOGGER.debug('NotifyServiceUpdate request: {:s}'.format(str(request)))
        response = self.stub.NotifyServiceUpdate(request)
        LOGGER.debug('NotifyServiceUpdate result: {:s}'.format(str(response)))
        return response

    @retry(exceptions=set(), max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect')
    def DetectAttack(self, request : Empty) -> Empty:
        LOGGER.debug('DetectAttack request: {:s}'.format(str(request)))
        response = self.stub.DetectAttack(request)
        LOGGER.debug('DetectAttack result: {:s}'.format(str(response)))
        return response

    @retry(exceptions=set(), max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect')
    def ReportSummarizedKpi(self, request : KpiList) -> Empty:
        LOGGER.debug('ReportSummarizedKpi request: {:s}'.format(str(request)))
        response = self.stub.ReportSummarizedKpi(request)
        LOGGER.debug('ReportSummarizedKpi result: {:s}'.format(str(response)))
        return response

    @retry(exceptions=set(), max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect')
    def ReportKpi(self, request : KpiList) -> Empty:
        LOGGER.debug('ReportKpi request: {:s}'.format(str(request)))
        response = self.stub.ReportKpi(request)
        LOGGER.debug('ReportKpi result: {:s}'.format(str(response)))
        return response
Loading