Esempio n. 1
0
        protected void ValidateConfiguration()
        {
            // if no AntimalwareConfiguration XmlDocument was provided, use the AntimalwareConfigFile parameter
            if ((AntimalwareConfiguration == null) && (AntimalwareConfigFile != null))
            {
                if ((!string.IsNullOrWhiteSpace(AntimalwareConfigFile)) && File.Exists(AntimalwareConfigFile))
                {
                    AntimalwareConfiguration             = new XmlDocument();
                    AntimalwareConfiguration.XmlResolver = null;
                    AntimalwareConfiguration.Load(AntimalwareConfigFile);
                }
                else
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new Exception("ServiceExtensionCannotFindAntimalwareConfigFile"),
                                              string.Empty,
                                              ErrorCategory.InvalidData,
                                              null));
                }
            }

            // read values from xml file if provided
            if (AntimalwareConfiguration != null)
            {
                // check for antimalware enabled
                XmlNode antimalwareEnabledNode = AntimalwareConfiguration.SelectSingleNode("//AntimalwareConfig/AntimalwareEnabled");
                if ((antimalwareEnabledNode != null) && (antimalwareEnabledNode.InnerText != null))
                {
                    isAntimalwareEnabled = antimalwareEnabledNode.InnerText.ToUpperInvariant().Equals("TRUE");
                }

                // check for monitoring enabled
                XmlNode monitoringNode = AntimalwareConfiguration.SelectSingleNode("//AntimalwareConfig/Monitoring");
                if (monitoringNode != null)
                {
                    if (monitoringNode.InnerText != null)
                    {
                        switch (monitoringNode.InnerText.ToUpperInvariant())
                        {
                        case "ON": monitoringAction = MonitoringActionType.Enable; break;

                        case "OFF": monitoringAction = MonitoringActionType.Disable; break;

                        default: break;
                        }
                    }

                    // now remove the monitoring node from the xml document since it
                    // is not recognized by the schema used by the antimalware extension
                    monitoringNode.ParentNode.RemoveChild(monitoringNode);
                }

                // check for storage account name if present in the config file
                XmlNode storageAccountNameNode = AntimalwareConfiguration.SelectSingleNode("//AntimalwareConfig/StorageAccountName");
                if (storageAccountNameNode != null)
                {
                    if (storageAccountNameNode.InnerText != null)
                    {
                        monitoringStorageAccountName = storageAccountNameNode.InnerText;
                    }
                    // strip this node from the xml prior to passing to antimalware extension
                    storageAccountNameNode.ParentNode.RemoveChild(storageAccountNameNode);
                }
            }

            // error no configuration was provided - error if this is not a case of disable or uninstall
            if (!isAntimalwareEnabled &&
                ((!((Disable.IsPresent && Disable.ToBool()) || (Uninstall.IsPresent && Uninstall.ToBool())))))
            {
                ThrowTerminatingError(new ErrorRecord(
                                          new Exception("Configuration is required and must specify AntimalwareEnabled=true"),
                                          string.Empty,
                                          ErrorCategory.InvalidData,
                                          null));
            }

            // process Monitoring parameter if specified (will override any setting in xml config)
            if (Monitoring != null)
            {
                switch (Monitoring.ToUpperInvariant())
                {
                case "ON": monitoringAction = MonitoringActionType.Enable; break;

                case "OFF": monitoringAction = MonitoringActionType.Disable; break;

                default: break;
                }
            }
        }
Esempio n. 2
0
        protected void ValidateConfiguration()
        {
            // if no AntimalwareConfiguration XmlDocument was provided, use the AntimalwareConfigFile parameter
            if ((AntimalwareConfiguration == null) && (AntimalwareConfigFile != null))
            {
                if ((!string.IsNullOrWhiteSpace(AntimalwareConfigFile)) && File.Exists(AntimalwareConfigFile))
                {
                    AntimalwareConfiguration = File.ReadAllText(AntimalwareConfigFile);
                }
                else
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new Exception("ServiceExtensionCannotFindAntimalwareConfigFile"),
                                              string.Empty,
                                              ErrorCategory.InvalidData,
                                              null));
                }
            }

            // validate json schema and check for antimalwareenabled value
            if (AntimalwareConfiguration != null)
            {
                JsonSchema v2Schema = JsonSchema.Parse(MicrosoftAntimalwareConfigJsonSchema);
                JObject    json     = JObject.Parse(AntimalwareConfiguration);

                if (json.IsValid(v2Schema))
                {
                    JToken tok;
                    if (json != null)
                    {
                        if (json.TryGetValue("AntimalwareEnabled", out tok))
                        {
                            isAntimalwareEnabled = (bool)tok;
                        }
                    }
                }
                else
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new Exception("ServiceExtensionAntimalwareJsonConfigFailedSchemaValidation"),
                                              string.Empty,
                                              ErrorCategory.InvalidData,
                                              null));
                }
            }

            // error no configuration was provided - error if this is not a case of disable, uninstall, or noconfig
            if (!isAntimalwareEnabled &&
                ((!((Disable.IsPresent && Disable.ToBool()) ||
                    (Uninstall.IsPresent && Uninstall.ToBool()) ||
                    (NoConfig.IsPresent && NoConfig.ToBool())))))
            {
                ThrowTerminatingError(new ErrorRecord(
                                          new Exception("JSON configuration is required and must specify \"AntimalwareEnabled\"=true"),
                                          string.Empty,
                                          ErrorCategory.InvalidData,
                                          null));
            }

            // process Monitoring parameter if specified
            if (Monitoring != null)
            {
                switch (Monitoring.ToUpperInvariant())
                {
                case "ON": monitoringAction = MonitoringActionType.Enable; break;

                case "OFF": monitoringAction = MonitoringActionType.Disable; break;

                default: break;
                }
            }
        }