-
Notifications
You must be signed in to change notification settings - Fork 874
rate_limit: catch YAML errors instead of dying on reload #13600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| ''' | ||
| Test that a malformed rate_limit YAML file fails the reload instead of | ||
| terminating traffic_server. | ||
| ''' | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import shlex | ||
|
|
||
| Test.Summary = ''' | ||
| rate_limit: a malformed YAML config fails the reload without killing ATS. | ||
| ''' | ||
|
|
||
| Test.SkipUnless(Condition.PluginExists('rate_limit.so')) | ||
| Test.ContinueOnFail = True | ||
|
|
||
| server = Test.MakeOriginServer("server") | ||
| server.addResponse( | ||
| "sessionlog.json", { | ||
| "headers": "GET /health HTTP/1.1\r\nHost: reload.example.com\r\n\r\n", | ||
| "timestamp": "1469733493.993", | ||
| "body": "" | ||
| }, { | ||
| "headers": "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n", | ||
| "timestamp": "1469733493.993", | ||
| "body": "OK" | ||
| }) | ||
|
|
||
| ts = Test.MakeATSProcess("ts") | ||
|
|
||
| rate_limit_yaml = os.path.join(ts.Variables.CONFIGDIR, 'rate_limit.yaml') | ||
| ts.Disk.File( | ||
| rate_limit_yaml, typename="ats:config").AddLines([ | ||
| 'selector:', | ||
| ' - sni: reload.example.com', | ||
| ' limit: 100', | ||
| '', | ||
| ]) | ||
|
|
||
| # A selector entry with no "sni" key. Reading sni["sni"] on the const node | ||
| # throws YAML::InvalidNode before the "without a name" check can report it. | ||
| missing_sni = os.path.join(Test.RunDirectory, 'missing_sni.yaml') | ||
| with open(missing_sni, 'w') as f: | ||
| f.write('selector:\n - limit: 100\n') | ||
|
|
||
| # "percentage" is read as a uint32_t, so the fractional value that the | ||
| # documentation used to suggest throws YAML::TypedBadConversion. | ||
| bad_percentage = os.path.join(Test.RunDirectory, 'bad_percentage.yaml') | ||
| with open(bad_percentage, 'w') as f: | ||
| f.write('ip-rep:\n - name: test\n size: 15\n percentage: 0.9\n') | ||
|
|
||
| # A selector entry that is a sequence rather than a map, which is what one extra | ||
| # level of indentation produces. Nothing here throws, so only the IsMap() check | ||
| # keeps it from being accepted as a limiter named "null". | ||
| non_map = os.path.join(Test.RunDirectory, 'non_map.yaml') | ||
| with open(non_map, 'w') as f: | ||
| f.write('selector:\n - - sni: indented.example.com\n limit: 5\n') | ||
|
|
||
| ts.Disk.records_config.update({ | ||
| 'proxy.config.diags.debug.enabled': 1, | ||
| 'proxy.config.diags.debug.tags': 'rate_limit', | ||
| }) | ||
|
|
||
| ts.Disk.remap_config.AddLine(f'map / http://127.0.0.1:{server.Variables.Port}/') | ||
| ts.Disk.plugin_config.AddLine(f'rate_limit.so {rate_limit_yaml}') | ||
|
|
||
| BASE_URL = f"http://127.0.0.1:{ts.Variables.port}/health" | ||
| CURL = f"curl -s -o /dev/null -w '%{{http_code}}' -H 'Host: reload.example.com' '{BASE_URL}'" | ||
|
|
||
| tr = Test.AddTestRun("Start with a valid config") | ||
| tr.Processes.Default.StartBefore(server, ready=When.PortOpen(server.Variables.Port)) | ||
| tr.Processes.Default.StartBefore(ts) | ||
| tr.Processes.Default.Command = CURL | ||
| tr.Processes.Default.ReturnCode = 0 | ||
| tr.Processes.Default.Streams.stdout = Testers.ContainsExpression("200", "ATS should serve the request") | ||
| tr.StillRunningAfter = ts | ||
|
|
||
| # The plugin callback only fires when the file mtime advances, so sleep before | ||
| # each overwrite to make sure it does. | ||
| for description, bad_config in [("selector entry without an sni key", missing_sni), ("a fractional percentage", bad_percentage), | ||
| ("a selector entry that is not a map", non_map)]: | ||
| tr = Test.AddTestRun(f"Install {description}") | ||
| tr.Processes.Default.Command = f"sleep 2 && cp {shlex.quote(bad_config)} {shlex.quote(rate_limit_yaml)}" | ||
| tr.Processes.Default.ReturnCode = 0 | ||
| tr.StillRunningAfter = ts | ||
|
|
||
| Test.AddConfigReload(ts, delay_start=1, description=f"Reload with {description}") | ||
|
|
||
| tr = Test.AddTestRun(f"ATS survives {description}") | ||
| tr.Processes.Default.Command = f"sleep 2 && {CURL}" | ||
| tr.Processes.Default.ReturnCode = 0 | ||
| tr.Processes.Default.Streams.stdout = Testers.ContainsExpression("200", "ATS should still be serving traffic") | ||
| tr.StillRunningAfter = ts | ||
|
|
||
| # Every case has to be reported and rejected. Assigning here replaces the | ||
| # default "diags.log has no ERROR:" testers, since these errors are expected. | ||
| ts.Disk.diags_log.Content = Testers.ContainsExpression( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The test can't currently detect the thing it's guarding, which is why the Three gaps:
A direct assertion that no swap happened would close all three, since ts.Disk.traffic_out.Content = Testers.ExcludesExpression(
"Reloading YAML file", "No malformed config should ever be swapped in")
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the Dropped the The per-reload point stands, the existence checks still pass if one reload is rejected and another is not. The exclusion covers all three at once, so a silent accept fails now regardless of which one it was. |
||
| "selector node is not a map or without a name", "The malformed selector entries should be reported, not thrown") | ||
| ts.Disk.diags_log.Content += Testers.ContainsExpression( | ||
| "Failed to parse configuration file", "The bad percentage should be caught by the parser") | ||
| ts.Disk.diags_log.Content += Testers.ContainsExpression("Failed to reload YAML file", "The reloads should be rejected") | ||
|
|
||
| # sni_config_cont() logs this only after a parse succeeds, right before it swaps | ||
| # the new selector in. None of the three configs may get that far, so this is | ||
| # what separates "rejected, previous config kept" from "accepted and the limits | ||
| # silently dropped". The 200s above cannot tell those apart, because the origin | ||
| # answers either way. | ||
| ts.Disk.traffic_out.Content = Testers.ExcludesExpression("Reloading YAML file", "No malformed config should be swapped in") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise:
Making the whole parse the guarded region rather than just
LoadFileis the right call, and it fixes more than the two cases in the test --list["name"].as<std::string>()andipr["name"].as<std::string>()in thelistsandip-reploops throw on a non-scalar too, and were reaching the event loop the same way.The const-node diagnosis behind the
snikey check was also exactly right: the constoperator[]returns aZombieNode,IsSequence()goes throughType()which throwsInvalidNode, whileoperator bool()goes throughIsDefined()which returns false without throwing.