Exemple #1
0
        private static void AddAllowedServerVariable(AllowedServerVariablesSection serverVariablesSection, string name)
        {
            //
            // The list of server variables that are allowed to be overwritten in distributed inbound rules
            AllowedServerVariableCollection allowedServerVariables = serverVariablesSection.AllowedServerVariables;

            if (allowedServerVariables.Any(sv => sv.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }

            //
            // Try to add the server variable to the allowed list, otherwise inbound rules using it will not work
            try {
                AllowedServerVariable allowedServerVariable = allowedServerVariables.CreateElement();

                allowedServerVariable.Name = name;

                allowedServerVariables.Add(allowedServerVariable);
            }
            catch (FileLoadException e) {
                throw new LockedException(serverVariablesSection.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Exemple #2
0
        public static void UpdateRule(dynamic model, InboundRule rule, Site site, string path, string configPath = null)
        {
            InboundRulesSection           section = GetSection(site, path, configPath);
            AllowedServerVariablesSection serverVariablesSection = ServerVariablesHelper.GetSection(site, path, configPath);

            SetRule(model, rule, section, serverVariablesSection);
        }
        public static void UpdateFeatureSettings(dynamic model, Site site, string path, string configPath = null)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            AllowedServerVariablesSection section = ServerVariablesHelper.GetSection(site, path, configPath);

            try {
                if (model.entries != null)
                {
                    IEnumerable <dynamic> variables = model.entries as IEnumerable <dynamic>;

                    if (variables == null)
                    {
                        throw new ApiArgumentException("entries", ForbiddenArgumentException.EXPECTED_ARRAY);
                    }

                    List <string> variableList = new List <string>();

                    // Validate all verbs provided
                    foreach (dynamic variable in variables)
                    {
                        string var = DynamicHelper.Value(variable);

                        if (string.IsNullOrEmpty(var))
                        {
                            throw new ApiArgumentException("entries.item");
                        }

                        variableList.Add(var);
                    }

                    // Clear configuration's collection
                    section.AllowedServerVariables.Clear();

                    // Move from temp list to the configuration's collection
                    variableList.ForEach(v => section.AllowedServerVariables.Add(v));
                }


                if (model.metadata != null)
                {
                    DynamicHelper.If <OverrideMode>((object)model.metadata.override_mode, v => {
                        section.OverrideMode = v;
                    });
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Exemple #4
0
        public static InboundRule CreateRule(dynamic model, Site site, string path, string configPath = null)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.name)))
            {
                throw new ApiArgumentException("name");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.pattern)))
            {
                throw new ApiArgumentException("pattern");
            }

            if (model.action == null)
            {
                throw new ApiArgumentException("action");
            }

            if (!(model.action is JObject))
            {
                throw new ApiArgumentException("action", ApiArgumentException.EXPECTED_OBJECT);
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.action.url)))
            {
                throw new ApiArgumentException("action.url");
            }

            InboundRulesSection           section = GetSection(site, path, configPath);
            AllowedServerVariablesSection serverVariablesSection = ServerVariablesHelper.GetSection(site, path, configPath);

            var rule = (InboundRule)section.InboundRules.CreateElement();

            //
            // Defaults
            rule.PatternSyntax = PatternSyntax.ECMAScript;
            rule.Action.Type   = ActionType.Rewrite;

            SetRule(model, rule, section, serverVariablesSection);

            return(rule);
        }
Exemple #5
0
        private static void AssignRuleFromModel(dynamic model, InboundRule rule, InboundRulesSection section, AllowedServerVariablesSection serverVariablesSection)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            //
            // Name, check for already existing name
            string name = DynamicHelper.Value(model.name);

            if (!string.IsNullOrEmpty(name))
            {
                if (!name.Equals(rule.Name, StringComparison.OrdinalIgnoreCase) &&
                    section.InboundRules.Any(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new AlreadyExistsException("name");
                }

                rule.Name = name;
            }

            DynamicHelper.If((object)model.pattern, v => rule.Match.Pattern = v);
            DynamicHelper.If <bool>((object)model.ignore_case, v => rule.Match.IgnoreCase   = v);
            DynamicHelper.If <bool>((object)model.negate, v => rule.Match.Negate            = v);
            DynamicHelper.If <bool>((object)model.stop_processing, v => rule.StopProcessing = v);
            DynamicHelper.If((object)model.pattern_syntax, v => rule.PatternSyntax          = PatternSyntaxHelper.FromJsonModel(v));

            //
            // Action
            dynamic action = model.action;

            if (action != null)
            {
                if (!(action is JObject))
                {
                    throw new ApiArgumentException("action", ApiArgumentException.EXPECTED_OBJECT);
                }

                DynamicHelper.If((object)action.type, v => rule.Action.Type = ActionTypeHelper.FromJsonModel(v));
                DynamicHelper.If((object)action.url, v => rule.Action.Url   = v);
                DynamicHelper.If <bool>((object)action.append_query_string, v => rule.Action.AppendQueryString = v);
                DynamicHelper.If <bool>((object)action.log_rewritten_url, v => rule.Action.LogRewrittenUrl     = v);
                DynamicHelper.If <long>((object)action.status_code, v => rule.Action.StatusCode        = v);
                DynamicHelper.If <long>((object)action.sub_status_code, v => rule.Action.SubStatusCode = v);
                DynamicHelper.If((object)action.description, v => rule.Action.StatusDescription        = v);
                DynamicHelper.If((object)action.reason, v => rule.Action.StatusReason = v);
                DynamicHelper.If <RedirectType>((object)action.redirect_type, v => rule.Action.RedirectType = v);
            }

            //
            // Server variables
            if (model.server_variables != null)
            {
                IEnumerable <dynamic> serverVariables = model.server_variables as IEnumerable <dynamic>;

                if (serverVariables == null)
                {
                    throw new ApiArgumentException("server_variables", ApiArgumentException.EXPECTED_ARRAY);
                }

                rule.ServerVariableAssignments.Clear();

                foreach (dynamic serverVariable in serverVariables)
                {
                    if (!(serverVariable is JObject))
                    {
                        throw new ApiArgumentException("server_variables.item");
                    }

                    string svName    = DynamicHelper.Value(serverVariable.name);
                    string svValue   = DynamicHelper.Value(serverVariable.value);
                    bool   svReplace = DynamicHelper.To <bool>(serverVariable.replace) ?? false;

                    if (string.IsNullOrEmpty(svName))
                    {
                        throw new ApiArgumentException("server_variables.item.name", "Required");
                    }

                    if (string.IsNullOrEmpty(svValue))
                    {
                        throw new ApiArgumentException("server_variables.item.value", "Required");
                    }

                    var svAssignment = rule.ServerVariableAssignments.CreateElement();
                    svAssignment.Name    = svName;
                    svAssignment.Value   = svValue;
                    svAssignment.Replace = svReplace;

                    AddAllowedServerVariable(serverVariablesSection, svAssignment.Name);

                    rule.ServerVariableAssignments.Add(svAssignment);
                }
            }

            DynamicHelper.If((object)model.condition_match_constraints, v => rule.Conditions.LogicalGrouping = LogicalGroupingHelper.FromJsonModel(v));
            DynamicHelper.If <bool>((object)model.track_all_captures, v => rule.Conditions.TrackAllCaptures  = v);

            //
            // Conditions
            if (model.conditions != null)
            {
                IEnumerable <dynamic> conditions = model.conditions as IEnumerable <dynamic>;

                if (conditions == null)
                {
                    throw new ApiArgumentException("conditions", ApiArgumentException.EXPECTED_ARRAY);
                }

                rule.Conditions.Clear();

                foreach (dynamic condition in conditions)
                {
                    if (!(condition is JObject))
                    {
                        throw new ApiArgumentException("conditions.item");
                    }

                    string input        = DynamicHelper.Value(condition.input);
                    string rawMatchType = DynamicHelper.Value(condition.match_type);

                    if (string.IsNullOrEmpty(input))
                    {
                        throw new ApiArgumentException("conditions.item.input", "Required");
                    }

                    if (string.IsNullOrEmpty(rawMatchType))
                    {
                        throw new ApiArgumentException("conditions.item.match_type", "Required");
                    }

                    MatchType matchType = MatchTypeHelper.FromJsonModel(rawMatchType);

                    var con = rule.Conditions.CreateElement();
                    con.Input      = input;
                    con.MatchType  = matchType;
                    con.Pattern    = DynamicHelper.Value(condition.pattern);
                    con.Negate     = DynamicHelper.To <bool>(condition.negate);
                    con.IgnoreCase = DynamicHelper.To <bool>(condition.ignore_case);

                    rule.Conditions.Add(con);
                }
            }

            if (rule.Schema.HasAttribute(InboundRule.ResponseCacheDirectiveAttribute))
            {
                DynamicHelper.If((object)model.response_cache_directive, v => rule.ResponseCacheDirective = ResponseCacheDirectiveHelper.FromJsonModel(v));
            }

            //
            // Check set to valid state
            if ((rule.Action.Type == ActionType.Redirect || rule.Action.Type == ActionType.Rewrite) && string.IsNullOrEmpty(rule.Action.Url))
            {
                throw new ApiArgumentException("action.url");
            }

            UpdatePriority(model, rule, section);
        }
Exemple #6
0
 private static void SetRule(dynamic model, InboundRule rule, InboundRulesSection section, AllowedServerVariablesSection serverVariablesSection)
 {
     try {
         AssignRuleFromModel(model, rule, section, serverVariablesSection);
     }
     catch (FileLoadException e) {
         throw new LockedException(section.SectionPath, e);
     }
     catch (DirectoryNotFoundException e) {
         throw new ConfigScopeNotFoundException(e);
     }
 }