/
rule.go
202 lines (156 loc) · 4.69 KB
/
rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Code generated by go-swagger; DO NOT EDIT.
package models
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
"strconv"
strfmt "github.com/go-openapi/strfmt"
"github.com/go-openapi/errors"
"github.com/go-openapi/swag"
)
// Rule Rule swaggerRule is a single rule that will get checked on every HTTP request.
// swagger:model rule
type Rule struct {
// Authenticators is a list of authentication handlers that will try and authenticate the provided credentials.
// Authenticators are checked iteratively from index 0 to n and if the first authenticator to return a positive
// result will be the one used.
//
// If you want the rule to first check a specific authenticator before "falling back" to others, have that authenticator
// as the first item in the array.
Authenticators []*RuleHandler `json:"authenticators"`
// authorizer
Authorizer *RuleHandler `json:"authorizer,omitempty"`
// Description is a human readable description of this rule.
Description string `json:"description,omitempty"`
// ID is the unique id of the rule. It can be at most 190 characters long, but the layout of the ID is up to you.
// You will need this ID later on to update or delete the rule.
ID string `json:"id,omitempty"`
// match
Match *RuleMatch `json:"match,omitempty"`
// Mutators is a list of mutation handlers that transform the HTTP request. A common use case is generating a new set
// of credentials (e.g. JWT) which then will be forwarded to the upstream server.
//
// Mutations are performed iteratively from index 0 to n and should all succeed in order for the HTTP request to be forwarded.
Mutators []*RuleHandler `json:"mutators"`
// upstream
Upstream *Upstream `json:"upstream,omitempty"`
}
// Validate validates this rule
func (m *Rule) Validate(formats strfmt.Registry) error {
var res []error
if err := m.validateAuthenticators(formats); err != nil {
res = append(res, err)
}
if err := m.validateAuthorizer(formats); err != nil {
res = append(res, err)
}
if err := m.validateMatch(formats); err != nil {
res = append(res, err)
}
if err := m.validateMutators(formats); err != nil {
res = append(res, err)
}
if err := m.validateUpstream(formats); err != nil {
res = append(res, err)
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
func (m *Rule) validateAuthenticators(formats strfmt.Registry) error {
if swag.IsZero(m.Authenticators) { // not required
return nil
}
for i := 0; i < len(m.Authenticators); i++ {
if swag.IsZero(m.Authenticators[i]) { // not required
continue
}
if m.Authenticators[i] != nil {
if err := m.Authenticators[i].Validate(formats); err != nil {
if ve, ok := err.(*errors.Validation); ok {
return ve.ValidateName("authenticators" + "." + strconv.Itoa(i))
}
return err
}
}
}
return nil
}
func (m *Rule) validateAuthorizer(formats strfmt.Registry) error {
if swag.IsZero(m.Authorizer) { // not required
return nil
}
if m.Authorizer != nil {
if err := m.Authorizer.Validate(formats); err != nil {
if ve, ok := err.(*errors.Validation); ok {
return ve.ValidateName("authorizer")
}
return err
}
}
return nil
}
func (m *Rule) validateMatch(formats strfmt.Registry) error {
if swag.IsZero(m.Match) { // not required
return nil
}
if m.Match != nil {
if err := m.Match.Validate(formats); err != nil {
if ve, ok := err.(*errors.Validation); ok {
return ve.ValidateName("match")
}
return err
}
}
return nil
}
func (m *Rule) validateMutators(formats strfmt.Registry) error {
if swag.IsZero(m.Mutators) { // not required
return nil
}
for i := 0; i < len(m.Mutators); i++ {
if swag.IsZero(m.Mutators[i]) { // not required
continue
}
if m.Mutators[i] != nil {
if err := m.Mutators[i].Validate(formats); err != nil {
if ve, ok := err.(*errors.Validation); ok {
return ve.ValidateName("mutators" + "." + strconv.Itoa(i))
}
return err
}
}
}
return nil
}
func (m *Rule) validateUpstream(formats strfmt.Registry) error {
if swag.IsZero(m.Upstream) { // not required
return nil
}
if m.Upstream != nil {
if err := m.Upstream.Validate(formats); err != nil {
if ve, ok := err.(*errors.Validation); ok {
return ve.ValidateName("upstream")
}
return err
}
}
return nil
}
// MarshalBinary interface implementation
func (m *Rule) MarshalBinary() ([]byte, error) {
if m == nil {
return nil, nil
}
return swag.WriteJSON(m)
}
// UnmarshalBinary interface implementation
func (m *Rule) UnmarshalBinary(b []byte) error {
var res Rule
if err := swag.ReadJSON(b, &res); err != nil {
return err
}
*m = res
return nil
}