-
Notifications
You must be signed in to change notification settings - Fork 24
/
apirule_types.go
248 lines (217 loc) · 9.38 KB
/
apirule_types.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Licensed 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.
*/
package v1beta1
import (
"istio.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// Status code describing APIRule.
type StatusCode string
const (
//StatusOK is set when the reconciliation finished succefully
StatusOK StatusCode = "OK"
//StatusSkipped is set when reconcilation of the APIRule component was skipped
StatusSkipped StatusCode = "SKIPPED"
//StatusError is set when an error happened during reconciliation of the APIRule
StatusError StatusCode = "ERROR"
//StatusWarning is set if an user action is required
StatusWarning StatusCode = "WARNING"
)
// Defines the desired state of ApiRule.
type APIRuleSpec struct {
// Specifies the URL of the exposed service.
// +kubebuilder:validation:MinLength=3
// +kubebuilder:validation:MaxLength=256
// +kubebuilder:validation:Pattern=^([a-zA-Z0-9][a-zA-Z0-9-_]*\.)*[a-zA-Z0-9]*[a-zA-Z0-9-_]*[[a-zA-Z0-9]+$
Host *string `json:"host"`
// Describes the service to expose.
// +optional
Service *Service `json:"service,omitempty"`
// Specifies the Istio Gateway to be used.
// +kubebuilder:validation:Pattern=`^[0-9a-z-_]+(\/[0-9a-z-_]+|(\.[0-9a-z-_]+)*)$`
Gateway *string `json:"gateway"`
// Specifies CORS headers configuration that will be sent downstream
// +optional
CorsPolicy *CorsPolicy `json:"corsPolicy,omitempty"`
// Represents the array of Oathkeeper access rules to be applied.
// +kubebuilder:validation:MinItems=1
Rules []Rule `json:"rules"`
// +optional
Timeout *Timeout `json:"timeout,omitempty"`
}
// Describes the observed state of ApiRule.
type APIRuleStatus struct {
LastProcessedTime *metav1.Time `json:"lastProcessedTime,omitempty"`
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
APIRuleStatus *APIRuleResourceStatus `json:"APIRuleStatus,omitempty"`
VirtualServiceStatus *APIRuleResourceStatus `json:"virtualServiceStatus,omitempty"`
// +optional
AccessRuleStatus *APIRuleResourceStatus `json:"accessRuleStatus,omitempty"`
// +optional
RequestAuthenticationStatus *APIRuleResourceStatus `json:"requestAuthenticationStatus,omitempty"`
// +optional
AuthorizationPolicyStatus *APIRuleResourceStatus `json:"authorizationPolicyStatus,omitempty"`
}
// APIRule is the Schema for ApiRule APIs.
// +kubebuilder:storageversion
// +kubebuilder:deprecatedversion:warning=APIRule version v1beta1 is deprecated in favor of v1beta2. Please migrate APIRule to v1beta2 as soon as possible.
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.APIRuleStatus.code"
// +kubebuilder:printcolumn:name="Host",type="string",JSONPath=".spec.host"
type APIRule struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec APIRuleSpec `json:"spec,omitempty"`
Status APIRuleStatus `json:"status,omitempty"`
}
// +kubebuilder:object:root=true
// APIRuleList contains a list of ApiRule
type APIRuleList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []APIRule `json:"items"`
}
// Service .
type Service struct {
// Specifies the name of the exposed service.
Name *string `json:"name"`
// Specifies the Namespace of the exposed service. If not defined, it defaults to the APIRule Namespace.
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
// +optional
Namespace *string `json:"namespace,omitempty"`
// Specifies the communication port of the exposed service.
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=65535
Port *uint32 `json:"port"`
// Specifies if the service is internal (in cluster) or external.
// +optional
IsExternal *bool `json:"external,omitempty"`
}
// Rule .
type Rule struct {
// Specifies the path of the exposed service.
// +kubebuilder:validation:Pattern=^([0-9a-zA-Z./*()?!\\_-]+)
Path string `json:"path"`
// Describes the service to expose. Overwrites the **spec** level service if defined.
// +optional
Service *Service `json:"service,omitempty"`
// Represents the list of allowed HTTP request methods available for the **spec.rules.path**.
// +kubebuilder:validation:MinItems=1
Methods []HttpMethod `json:"methods"`
// Specifies the list of access strategies.
// All strategies listed in [Oathkeeper documentation](https://www.ory.sh/docs/oathkeeper/pipeline/authn) are supported.
// +kubebuilder:validation:MinItems=1
AccessStrategies []*Authenticator `json:"accessStrategies"`
// Specifies the list of [Ory Oathkeeper](https://www.ory.sh/docs/oathkeeper/pipeline/mutator) mutators.
// +optional
Mutators []*Mutator `json:"mutators,omitempty"`
// +optional
Timeout *Timeout `json:"timeout,omitempty"`
}
// HttpMethod specifies the HTTP request method. The list of supported methods is defined in RFC 9910: HTTP Semantics and RFC 5789: PATCH Method for HTTP.
// +kubebuilder:validation:Enum=GET;HEAD;POST;PUT;DELETE;CONNECT;OPTIONS;TRACE;PATCH
type HttpMethod string
// Describes the status of APIRule.
type APIRuleResourceStatus struct {
Code StatusCode `json:"code,omitempty"`
Description string `json:"desc,omitempty"`
}
func init() {
SchemeBuilder.Register(&APIRule{}, &APIRuleList{})
}
// Represents a handler that authenticates provided credentials. See the corresponding type in the oathkeeper-maester project.
type Authenticator struct {
*Handler `json:",inline"`
}
// Mutator represents a handler that transforms the HTTP request before forwarding it. See the corresponding in the oathkeeper-maester project.
type Mutator struct {
*Handler `json:",inline"`
}
// Handler provides configuration for different Oathkeeper objects. It is used to either validate a request (Authenticator, Authorizer) or modify it (Mutator). See the corresponding type in the oathkeeper-maester project.
type Handler struct {
// Specifies the name of the handler.
Name string `json:"handler"`
// Configures the handler. Configuration keys vary per handler.
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
Config *runtime.RawExtension `json:"config,omitempty"`
}
// JwtConfig is an array of JwtAuthorization type used by raw field Config of Istio jwt Handler
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type JwtConfig struct {
Authentications []*JwtAuthentication `json:"authentications,omitempty"`
Authorizations []*JwtAuthorization `json:"authorizations,omitempty"`
}
func (j *JwtConfig) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}
// JwtAuthorization contains an array of required scopes
type JwtAuthorization struct {
RequiredScopes []string `json:"requiredScopes"`
Audiences []string `json:"audiences"`
}
// JwtAuthentication Config for Jwt Istio authentication
type JwtAuthentication struct {
Issuer string `json:"issuer"`
JwksUri string `json:"jwksUri"`
// +optional
FromHeaders []*JwtHeader `json:"fromHeaders,omitempty"`
// +optional
FromParams []string `json:"fromParams,omitempty"`
}
// JwtHeader for specifying from header for the Jwt token
type JwtHeader struct {
Name string `json:"name"`
// +optional
Prefix string `json:"prefix,omitempty"`
}
// Timeout for HTTP requests in seconds. The timeout can be configured up to 3900 seconds (65 minutes).
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=3900
type Timeout uint16 // We use unit16 instead of a time.Duration because there is a bug with duration that requires additional validation of the format. Issue: checking https://github.com/kubernetes/apiextensions-apiserver/issues/56
const (
Regex = "regex"
Exact = "exact"
Prefix = "prefix"
)
type StringMatch []map[string]string
func (s StringMatch) ToIstioStringMatchArray() (out []*v1beta1.StringMatch) {
for _, match := range s {
for key, value := range match {
switch key {
case Regex:
out = append(out, &v1beta1.StringMatch{MatchType: &v1beta1.StringMatch_Regex{Regex: value}})
case Exact:
out = append(out, &v1beta1.StringMatch{MatchType: &v1beta1.StringMatch_Exact{Exact: value}})
case Prefix:
fallthrough
default:
out = append(out, &v1beta1.StringMatch{MatchType: &v1beta1.StringMatch_Prefix{Prefix: value}})
}
}
}
return out
}
// CorsPolicy allows configuration of CORS headers received downstream. If this is not defined, the default values are applied.
// If CorsPolicy is configured, CORS headers received downstream will be only those defined on the APIRule
type CorsPolicy struct {
AllowHeaders []string `json:"allowHeaders,omitempty"`
AllowMethods []string `json:"allowMethods,omitempty"`
AllowOrigins StringMatch `json:"allowOrigins,omitempty"`
AllowCredentials *bool `json:"allowCredentials,omitempty"`
ExposeHeaders []string `json:"exposeHeaders,omitempty"`
// +kubebuilder:validation:Format=duration
MaxAge *metav1.Duration `json:"maxAge,omitempty"`
}