This commit adds the `certificates` config in both RO and MON charms.
Changes:
- Add `certificates` config option in mon and ro charms
- Use charmed-osm/ops-lib-charmed-osm library in all charms
Change-Id: I14e3cf7ad2c846c4a2af57f317d4151119ce5fbb
Signed-off-by: David Garcia <david.garcia@canonical.com>
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
#!/usr/bin/env python3
-# Copyright 2021 Canonical Ltd.
+# Copyright 2020 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
# osm-charmers@lists.launchpad.net
##
cryptography
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
import sys
+
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
description: MON will use Keystone backend
type: boolean
default: false
+ certificates:
+ type: string
+ description: |
+ comma-separated list of <name>:<content> certificates.
+ Where:
+ name: name of the file for the certificate
+ content: base64 content of the certificate
+ The path for the files is /certs.
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
# pylint: disable=E0213
+import base64
import logging
-from typing import NoReturn
+from typing import NoReturn, Optional
from ops.main import main
from opslib.osm.interfaces.keystone import KeystoneClient
from opslib.osm.interfaces.mongo import MongoClient
from opslib.osm.interfaces.prometheus import PrometheusClient
-from opslib.osm.pod import ContainerV3Builder, PodSpecV3Builder
+from opslib.osm.pod import ContainerV3Builder, FilesV3Builder, PodSpecV3Builder
from opslib.osm.validator import ModelValidator, validator
PORT = 8000
+def _check_certificate_data(name: str, content: str):
+ if not name or not content:
+ raise ValueError("certificate name and content must be a non-empty string")
+
+
+def _extract_certificates(certs_config: str):
+ certificates = {}
+ if certs_config:
+ cert_list = certs_config.split(",")
+ for cert in cert_list:
+ name, content = cert.split(":")
+ _check_certificate_data(name, content)
+ certificates[name] = content
+ return certificates
+
+
+def decode(content: str):
+ return base64.b64decode(content.encode("utf-8")).decode("utf-8")
+
+
class ConfigModel(ModelValidator):
keystone_enabled: bool
vca_host: str
grafana_url: str
grafana_user: str
grafana_password: str
+ certificates: Optional[str]
@validator("log_level")
def validate_log_level(cls, v):
raise ValueError("value must be INFO or DEBUG")
return v
+ @validator("certificates")
+ def validate_certificates(cls, v):
+ # Raises an exception if it cannot extract the certificates
+ _extract_certificates(v)
+ return v
+
+ @property
+ def certificates_dict(cls):
+ return _extract_certificates(cls.certificates) if cls.certificates else {}
+
class MonCharm(CharmedOsmBase):
def __init__(self, *args) -> NoReturn:
if missing_relations:
raise RelationsMissing(missing_relations)
+ def _build_cert_files(
+ self,
+ config: ConfigModel,
+ ):
+ cert_files_builder = FilesV3Builder()
+ for name, content in config.certificates_dict.items():
+ cert_files_builder.add_file(name, decode(content), mode=0o600)
+ return cert_files_builder.build()
+
def build_pod_spec(self, image_info):
# Validate config
config = ConfigModel(**dict(self.config))
pod_spec_builder = PodSpecV3Builder()
# Build Container
container_builder = ContainerV3Builder(self.app.name, image_info)
+ certs_files = self._build_cert_files(config)
+ if certs_files:
+ container_builder.add_volume_config("certs", "/certs", certs_files)
container_builder.add_port(name=self.app.name, port=PORT)
container_builder.add_envs(
{
import sys
+
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
# osm-charmers@lists.launchpad.net
##
+import base64
import sys
from typing import NoReturn
import unittest
from ops.testing import Harness
+def encode(content: str):
+ return base64.b64encode(content.encode("ascii")).decode("utf-8")
+
+
+certificate_pem = encode(
+ """
+-----BEGIN CERTIFICATE-----
+MIIDazCCAlOgAwIBAgIUf1b0s3UKtrxHXH2rge7UaQyfJAMwDQYJKoZIhvcNAQEL
+BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
+GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMTAzMjIxNzEyMjdaFw0zMTAz
+MjAxNzEyMjdaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
+HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
+AQUAA4IBDwAwggEKAoIBAQCgCfCBgYAN6ON0yHDXuW407rFtJVRf0u46Jrp0Dk7J
+kkSZ1e7Kq14r7yFHazEBWv78oOdwBocvWrd8leLuf3bYGcHR65hRy6A/fbYm5Aje
+cKpwlFwaqfR4BLelwJl79jZ2rJX738cCBVrIk1nAVdOxGrXV4MTWUaKR2c+uKKvc
+OKRT+5VqCeP4N5FWeATZ/KqGu8uV9E9WhFgwIZyStemLyLaDbn5PmAQ6S9oeR5jJ
+o2gEEp/lDKvsqOWs76KFumSKa9hQs5Dw2lj0mb1UoyYK1gYc4ubzVChJadv44AU8
+MYtIjlFn1X1P+RjaKZNUIAGXkoLwYn6SizF6y6LiuFS9AgMBAAGjUzBRMB0GA1Ud
+DgQWBBRl+/23CB+FXczeAZRQyYcfOdy9YDAfBgNVHSMEGDAWgBRl+/23CB+FXcze
+AZRQyYcfOdy9YDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAd
+dkeDym6lRN8kWFtfu3IyiLF8G8sn91qNbH3Yr4TuTBhgcjYyW6PgisSbrNgA9ysE
+GoaF7ohb8GeVfCsQdK23+NpAlj/+DZ3OnGcxwXj1RUAz4yr9kanV1yuEtr1q2xJI
+UaECWr8HZlwGBAKNTGx2EXT2/2aFzgULpDcxzTKD+MRpKpMUrWhf9ULvVrclvHWe
+POLYhobUFuBHuo6rt5Rcq16j67zCX9EVTlAE3o2OECIWByK22sXdeOidYMpTkl4q
+8FrOqjNsx5d+SBPJBv/pqtBm4bA47Vx1P8tbWOQ4bXS0UmXgwpeBOU/O/ot30+KS
+JnKEy+dYyvVBKg77sRHw
+-----END CERTIFICATE-----
+"""
+)
+
+
class TestCharm(unittest.TestCase):
"""Prometheus Charm unit tests."""
"collector_interval": 30,
"evaluator_interval": 30,
"keystone_enabled": True,
+ "certificates": f"cert1:{certificate_pem}",
}
self.harness.update_config(self.config)
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
import sys
+
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
##
pydantic # TODO: remove it
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
type: string
description: "Openmano Tenant"
default: "osm"
+ certificates:
+ type: string
+ description: |
+ comma-separated list of <name>:<content> certificates.
+ Where:
+ name: name of the file for the certificate
+ content: base64 content of the certificate
+ The path for the files is /certs.
# osm-charmers@lists.launchpad.net
##
-git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
# pylint: disable=E0213
+import base64
import logging
-from typing import NoReturn
+from typing import NoReturn, Optional
from ops.main import main
from opslib.osm.charm import CharmedOsmBase, RelationsMissing
from opslib.osm.interfaces.kafka import KafkaClient
from opslib.osm.interfaces.mongo import MongoClient
from opslib.osm.interfaces.mysql import MysqlClient
-from opslib.osm.pod import (
- ContainerV3Builder,
- PodSpecV3Builder,
-)
+from opslib.osm.pod import ContainerV3Builder, FilesV3Builder, PodSpecV3Builder
from opslib.osm.validator import ModelValidator, validator
-
logger = logging.getLogger(__name__)
PORT = 9090
+def _check_certificate_data(name: str, content: str):
+ if not name or not content:
+ raise ValueError("certificate name and content must be a non-empty string")
+
+
+def _extract_certificates(certs_config: str):
+ certificates = {}
+ if certs_config:
+ cert_list = certs_config.split(",")
+ for cert in cert_list:
+ name, content = cert.split(":")
+ _check_certificate_data(name, content)
+ certificates[name] = content
+ return certificates
+
+
+def decode(content: str):
+ return base64.b64decode(content.encode("utf-8")).decode("utf-8")
+
+
class ConfigModel(ModelValidator):
enable_ng_ro: bool
database_commonkey: str
vim_database: str
ro_database: str
openmano_tenant: str
+ certificates: Optional[str]
@validator("log_level")
def validate_log_level(cls, v):
raise ValueError("value must be INFO or DEBUG")
return v
+ @validator("certificates")
+ def validate_certificates(cls, v):
+ # Raises an exception if it cannot extract the certificates
+ _extract_certificates(v)
+ return v
+
+ @property
+ def certificates_dict(cls):
+ return _extract_certificates(cls.certificates) if cls.certificates else {}
+
class RoCharm(CharmedOsmBase):
"""GrafanaCharm Charm."""
if missing_relations:
raise RelationsMissing(missing_relations)
+ def _build_cert_files(
+ self,
+ config: ConfigModel,
+ ):
+ cert_files_builder = FilesV3Builder()
+ for name, content in config.certificates_dict.items():
+ cert_files_builder.add_file(name, decode(content), mode=0o600)
+ return cert_files_builder.build()
+
def build_pod_spec(self, image_info):
# Validate config
config = ConfigModel(**dict(self.config))
pod_spec_builder = PodSpecV3Builder()
# Build Container
container_builder = ContainerV3Builder(self.app.name, image_info)
+ certs_files = self._build_cert_files(config)
+ if certs_files:
+ container_builder.add_volume_config("certs", "/certs", certs_files)
container_builder.add_port(name=self.app.name, port=PORT)
container_builder.add_http_readiness_probe(
"/ro/" if config.enable_ng_ro else "/openmano/tenants",
import mock
+
+class OCIImageResourceErrorMock(Exception):
+ pass
+
+
sys.path.append("src")
oci_image = mock.MagicMock()
+oci_image.OCIImageResourceError = OCIImageResourceErrorMock
sys.modules["oci_image"] = oci_image
+sys.modules["oci_image"].OCIImageResource().fetch.return_value = {}
# osm-charmers@lists.launchpad.net
##
-import sys
+import base64
from typing import NoReturn
import unittest
from ops.testing import Harness
+def encode(content: str):
+ return base64.b64encode(content.encode("ascii")).decode("utf-8")
+
+
+certificate_pem = encode(
+ """
+-----BEGIN CERTIFICATE-----
+MIIDazCCAlOgAwIBAgIUf1b0s3UKtrxHXH2rge7UaQyfJAMwDQYJKoZIhvcNAQEL
+BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
+GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMTAzMjIxNzEyMjdaFw0zMTAz
+MjAxNzEyMjdaMEUxCzAJBgNVBAY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
"""Test setup"""
- self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
self.harness = Harness(RoCharm)
self.harness.set_leader(is_leader=True)
self.harness.begin()
"vim_database": "db_name",
"ro_database": "ro_db_name",
"openmano_tenant": "mano",
+ "certificates": f"cert1:{certificate_pem}",
}
self.harness.update_config(self.config)