You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

98 lines
2.7 KiB

  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package failure provides a verifier that always fails, adding a given message
  15. // to the multierror log. This can be used to turn any filter in to a defacto
  16. // verifier, by wrapping it in a filter and thus causing a verifier failure whenever
  17. // a request passes the filter.
  18. package failure
  19. import (
  20. "encoding/json"
  21. "fmt"
  22. "net/http"
  23. "github.com/google/martian"
  24. "github.com/google/martian/parse"
  25. "github.com/google/martian/verify"
  26. )
  27. func init() {
  28. parse.Register("failure.Verifier", verifierFromJSON)
  29. }
  30. type verifier struct {
  31. message string
  32. merr *martian.MultiError
  33. }
  34. type verifierJSON struct {
  35. Message string `json:"message"`
  36. Scope []parse.ModifierType `json:"scope"`
  37. }
  38. // NewVerifier returns a new failing verifier.
  39. func NewVerifier(message string) (verify.RequestVerifier, error) {
  40. return &verifier{
  41. message: message,
  42. merr: martian.NewMultiError(),
  43. }, nil
  44. }
  45. // ModifyRequest adds an error message containing the message field in the verifier to the verifier errors.
  46. // This means that any time a request hits the verifier it's treated as an error.
  47. func (v *verifier) ModifyRequest(req *http.Request) error {
  48. err := fmt.Errorf("request(%v) verification error: %s", req.URL, v.message)
  49. v.merr.Add(err)
  50. return nil
  51. }
  52. // VerifyRequests returns an error if any requests have hit the verifier.
  53. // If an error is returned it will be of type *martian.MultiError.
  54. func (v *verifier) VerifyRequests() error {
  55. if v.merr.Empty() {
  56. return nil
  57. }
  58. return v.merr
  59. }
  60. // ResetRequestVerifications clears all failed request verifications.
  61. func (v *verifier) ResetRequestVerifications() {
  62. v.merr = martian.NewMultiError()
  63. }
  64. // verifierFromJSON builds a failure.Verifier from JSON
  65. //
  66. // Example JSON:
  67. // {
  68. // "failure.Verifier": {
  69. // "scope": ["request", "response"],
  70. // "message": "Request passed a filter it should not have"
  71. // }
  72. // }
  73. func verifierFromJSON(b []byte) (*parse.Result, error) {
  74. msg := &verifierJSON{}
  75. if err := json.Unmarshal(b, msg); err != nil {
  76. return nil, err
  77. }
  78. v, err := NewVerifier(msg.Message)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return parse.NewResult(v, msg.Scope)
  83. }