def test_insert_from_config(self):
     json_config = {}
     sinks = LadUtil.SinkConfiguration()
     msgs = sinks.insert_from_config(json_config)
     self.assertEqual(msgs, '')
     json_config = {'sink': [{'Name': 'bad case'}]}
     sinks = LadUtil.SinkConfiguration()
     msgs = sinks.insert_from_config(json_config)
     self.assertEqual(
         msgs, "Ignoring invalid sink definition {'Name': 'bad case'}")
 def setUp(self):
     self.config = \
         {
             "sink": [
                 {
                     "name": "sink1",
                     "type": "EventHub",
                     "sasURL": "https://sbnamespace.servicebus.windows.net/raw?sr=https%3a%2f%2fsb"
                               "namespace.servicebus.windows.net%2fraw%2f&sig=SIGNATURE%3d"
                               "&se=1804371161&skn=writer"
                 },
                 {
                     "name": "sink2",
                     "type": "JsonBlob"
                 },
                 {
                     "name": "sink3",
                     "type": "EventHub",
                     "sasURL": "https://sbnamespace2.servicebus.windows.net/raw?sr=https%3a%2f%2fsb"
                               "namespace.servicebus.windows.net%2fraw%2f&sig=SIGNATURE%3d"
                               "&se=99999999999&skn=writer"
                 }
             ]
         }
     self.sink_config = LadUtil.SinkConfiguration()
     self.sink_config.insert_from_config(self.config)
Beispiel #3
0
    def __init__(self, ext_settings, ext_dir, waagent_dir, deployment_id,
                 fetch_uuid, encrypt_string, logger_log, logger_error):
        """
        Constructor.
        :param ext_settings: A LadExtSettings (in Utils/lad_ext_settings.py) obj wrapping the Json extension settings.
        :param ext_dir: Extension directory (e.g., /var/lib/waagent/Microsoft.OSTCExtensions.LinuxDiagnostic-2.3.xxxx)
        :param waagent_dir: WAAgent directory (e.g., /var/lib/waagent)
        :param deployment_id: Deployment ID string (or None) that should be obtained & passed by the caller
                              from waagent's HostingEnvironmentCfg.xml.
        :param fetch_uuid: A function which fetches the UUID for the VM
        :param encrypt_string: A function which encrypts a string, given a cert_path
        :param logger_log: Normal logging function (e.g., hutil.log) that takes only one param for the logged msg.
        :param logger_error: Error logging function (e.g., hutil.error) that takes only one param for the logged msg.
        """
        self._ext_settings = ext_settings
        self._ext_dir = ext_dir
        self._waagent_dir = waagent_dir
        self._deployment_id = deployment_id
        self._fetch_uuid = fetch_uuid
        self._encrypt_secret = encrypt_string
        self._logger_log = logger_log
        self._logger_error = logger_error

        # Generated logging configs place holders
        self._fluentd_syslog_src_config = None
        self._fluentd_tail_src_config = None
        self._fluentd_out_mdsd_config = None
        self._rsyslog_config = None
        self._syslog_ng_config = None

        self._mdsd_config_xml_tree = ET.ElementTree(
            ET.fromstring(mxt.entire_xml_cfg_tmpl))
        self._sink_configs = LadUtil.SinkConfiguration()
        self._sink_configs.insert_from_config(
            self._ext_settings.read_protected_config('sinksConfig'))
        # If we decide to also read sinksConfig from ladCfg, do it first, so that private settings override

        # Get encryption settings
        thumbprint = ext_settings.get_handler_settings(
        )['protectedSettingsCertThumbprint']
        self._cert_path = os.path.join(waagent_dir, thumbprint + '.crt')
        self._pkey_path = os.path.join(waagent_dir, thumbprint + '.prv')
    def __init__(self, ext_settings, ext_dir, waagent_dir, deployment_id,
                 fetch_uuid, encrypt_string, logger_log, logger_error):
        """
        Constructor.
        :param ext_settings: A LadExtSettings (in Utils/lad_ext_settings.py) obj wrapping the Json extension settings.
        :param ext_dir: Extension directory (e.g., /var/lib/waagent/Microsoft.OSTCExtensions.LinuxDiagnostic-2.3.xxxx)
        :param waagent_dir: WAAgent directory (e.g., /var/lib/waagent)
        :param deployment_id: Deployment ID string (or None) that should be obtained & passed by the caller
                              from waagent's HostingEnvironmentCfg.xml.
        :param fetch_uuid: A function which fetches the UUID for the VM
        :param encrypt_string: A function which encrypts a string, given a cert_path
        :param logger_log: Normal logging function (e.g., hutil.log) that takes only one param for the logged msg.
        :param logger_error: Error logging function (e.g., hutil.error) that takes only one param for the logged msg.
        """
        self._ext_settings = ext_settings
        self._ext_dir = ext_dir
        self._waagent_dir = waagent_dir
        self._deployment_id = deployment_id
        self._fetch_uuid = fetch_uuid
        self._encrypt_secret = encrypt_string
        self._logger_log = logger_log
        self._logger_error = logger_error
        self._telegraf_me_url = metrics_constants.lad_metrics_extension_influx_udp_url
        self._telegraf_mdsd_url = metrics_constants.telegraf_influx_url
        self._enable_metrics_extension = False

        # Generated logging configs place holders
        self._fluentd_syslog_src_config = None
        self._fluentd_tail_src_config = None
        self._fluentd_out_mdsd_config = None
        self._rsyslog_config = None
        self._syslog_ng_config = None
        self._telegraf_config = None
        self._telegraf_namespaces = None

        self._mdsd_config_xml_tree = ET.ElementTree(
            ET.fromstring(mxt.entire_xml_cfg_tmpl))
        self._sink_configs = LadUtil.SinkConfiguration()
        self._sink_configs.insert_from_config(
            self._ext_settings.read_protected_config('sinksConfig'))
        # If we decide to also read sinksConfig from ladCfg, do it first, so that private settings override

        # Get encryption settings
        handlerSettings = ext_settings.get_handler_settings()

        if handlerSettings['protectedSettings'] is None:
            errorMsg = "Settings did not contain protectedSettings. For information on protected settings, " \
                          "visit https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/diagnostics-linux#protected-settings."
            self._logger_error(errorMsg)
            raise LadLoggingConfigException(errorMsg)

        if handlerSettings['protectedSettingsCertThumbprint'] is None:
            errorMsg = "Settings did not contain protectedSettingsCertThumbprint. For information on protected settings, " \
                          "visit https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/diagnostics-linux#protected-settings."
            self._logger_error(errorMsg)
            raise LadLoggingConfigException(errorMsg)

        thumbprint = handlerSettings['protectedSettingsCertThumbprint']

        self._cert_path = os.path.join(waagent_dir, thumbprint + '.crt')
        self._pkey_path = os.path.join(waagent_dir, thumbprint + '.prv')
    def setUp(self):
        """
        Create LadLoggingConfig objects for use by test cases
        """
        # "syslogEvents" LAD config example
        syslogEvents_json_ext_settings = """
            {
                "sinks": "SyslogJsonBlob,SyslogEventHub",
                "syslogEventConfiguration": {
                    "LOG_LOCAL0": "LOG_CRIT",
                    "LOG_USER": "******"
                }
            }
            """
        # "fileLogs" LAD config example
        fileLogs_json_ext_settings = """
            [
                {
                    "file": "/var/log/mydaemonlog1",
                    "table": "MyDaemon1Events",
                    "sinks": "Filelog1JsonBlob,FilelogEventHub"
                },
                {
                    "file": "/var/log/mydaemonlog2",
                    "table": "MyDaemon2Events",
                    "sinks": "Filelog2JsonBlob"
                }
            ]
            """
        # "sinksConfig" LAD config example
        sinksConfig_json_ext_settings = """
            {
                "sink": [
                    {
                        "name": "SyslogEventHub",
                        "type": "EventHub",
                        "sasURL": "https://fake&sas%url;for_syslog_eh"
                    },
                    {
                        "name": "SyslogJsonBlob",
                        "type": "JsonBlob"
                    },
                    {
                        "name": "FilelogEventHub",
                        "type": "EventHub",
                        "sasURL": "https://fake&sas%url;for_filelog_eh"
                    },
                    {
                        "name": "Filelog1JsonBlob",
                        "type": "JsonBlob"
                    },
                    {
                        "name": "Filelog2JsonBlob",
                        "type": "JsonBlob"
                    }
                ]
            }
            """

        sinksConfig = LadUtil.SinkConfiguration()
        sinksConfig.insert_from_config(
            json.loads(sinksConfig_json_ext_settings))
        syslogEvents = json.loads(syslogEvents_json_ext_settings)
        mock_pkey_path = "/waagent/dir/mock_pkey.prv"
        mock_cert_path = "/waagent/dir/mock_cert.crt"
        self.cfg_syslog = LadLoggingConfig(syslogEvents, None, sinksConfig,
                                           mock_pkey_path, mock_cert_path,
                                           mock_encrypt_secret)
        fileLogs = json.loads(fileLogs_json_ext_settings)
        self.cfg_filelog = LadLoggingConfig(None, fileLogs, sinksConfig,
                                            mock_pkey_path, mock_cert_path,
                                            mock_encrypt_secret)
        self.cfg_none = LadLoggingConfig(None, None, sinksConfig,
                                         mock_pkey_path, mock_cert_path,
                                         mock_encrypt_secret)

        # XPaths representations of expected XML outputs, for use with xmlunittests package
        self.oms_syslog_expected_xpaths = (
            './Sources/Source[@name="mdsd.syslog" and @dynamic_schema="true"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.syslog"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.syslog"]/RouteEvent[@dontUsePerNDayTable="true" and @eventName="LinuxSyslog" and @priority="High"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.syslog"]/RouteEvent[@dontUsePerNDayTable="true" and @eventName="SyslogJsonBlob" and @priority="High" and @storeType="JsonBlob"]',
            './EventStreamingAnnotations/EventStreamingAnnotation[@name="mdsd.syslog"]/EventPublisher/Key',  # TODO Perform CDATA validation
        )
        self.oms_filelog_expected_xpaths = (
            './Sources/Source[@name="mdsd.filelog.var.log.mydaemonlog1" and @dynamic_schema="true"]',
            './Sources/Source[@name="mdsd.filelog.var.log.mydaemonlog2" and @dynamic_schema="true"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.filelog.var.log.mydaemonlog1"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.filelog.var.log.mydaemonlog1"]/RouteEvent[@dontUsePerNDayTable="true" and @eventName="MyDaemon1Events" and @priority="High"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.filelog.var.log.mydaemonlog1"]/RouteEvent[@dontUsePerNDayTable="true" and @eventName="Filelog1JsonBlob" and @priority="High" and @storeType="JsonBlob"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.filelog.var.log.mydaemonlog2"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.filelog.var.log.mydaemonlog2"]/RouteEvent[@dontUsePerNDayTable="true" and @eventName="MyDaemon2Events" and @priority="High"]',
            './Events/MdsdEvents/MdsdEventSource[@source="mdsd.filelog.var.log.mydaemonlog2"]/RouteEvent[@dontUsePerNDayTable="true" and @eventName="Filelog2JsonBlob" and @priority="High" and @storeType="JsonBlob"]',
            './EventStreamingAnnotations/EventStreamingAnnotation[@name="mdsd.filelog.var.log.mydaemonlog1"]/EventPublisher/Key',  # TODO Perform CDATA validation
        )