Skip to content

Commit

Permalink
Merge pull request #17 from micovery/fix_healthmonitor
Browse files Browse the repository at this point in the history
fix: add missing validation for HealthMonitor
  • Loading branch information
micovery authored Oct 23, 2024
2 parents 8a02f64 + ed1cf35 commit 66c3c3f
Show file tree
Hide file tree
Showing 16 changed files with 448 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/yaml-first/petstore/apiproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TargetEndpoints:
- TargetEndpoint:
.name: default
HTTPTargetConnection:
URL: http://petstore.swagger.io/v1
URL: https://petstore.swagger.io/v2
Resources:
- Resource:
Type: oas
Expand Down
1 change: 1 addition & 0 deletions pkg/apigee/v1/apiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type APIProxy struct {
Spec *Deprecated `xml:"Spec"`
ConfigurationVersion *Deprecated `xml:"ConfigurationVersion"`
BasePaths *Deprecated `xml:"BasePaths"`
Basepaths *Deprecated `xml:"Basepaths"`
Policies *Deprecated `xml:"Policies"`
Resources *Deprecated `xml:"Resources"`
ProxyEndpoints *Deprecated `xml:"ProxyEndpoints"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/apigee/v1/apiproxymodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func TestNewAPIProxyModel(t *testing.T) {
{
"postclient",
},
{
"health-monitor-http",
},
{
"health-monitor-tcp",
},
}
for _, tt := range tests {
ttDir := filepath.Join("testdata", "yaml-first", tt.name)
Expand Down
37 changes: 37 additions & 0 deletions pkg/apigee/v1/header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type Header struct {
Name string `xml:"name,attr"`
Value string `xml:",cdata"`

UnknownNode AnyList `xml:",any"`
}

func ValidateHeader(v *Header, path string) []error {
if v == nil {
return nil
}

subPath := fmt.Sprintf("%s.Header", path)
if len(v.UnknownNode) > 0 {
return []error{&UnknownNodeError{subPath, v.UnknownNode[0]}}
}

return nil
}
33 changes: 33 additions & 0 deletions pkg/apigee/v1/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type HeadersList []*Header

func ValidateHeaders(v *HeadersList, path string) []error {
if v == nil {
return nil
}

for i, vv := range *v {
errs := ValidateHeader(vv, fmt.Sprintf("%s.Headers.%v", path, i))
if len(errs) > 0 {
return errs
}
}
return nil
}
43 changes: 43 additions & 0 deletions pkg/apigee/v1/healthmonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type HealthMonitor struct {
IsEnabled bool `xml:"Enforce,omitempty"`
IntervalInSec int `xml:"IntervalInSec"`
TCPMonitor *TCPMonitor `xml:"TCPMonitor,omitempty"`
HTTPMonitor *HTTPMonitor `xml:"HTTPMonitor,omitempty"`

UnknownNode AnyList `xml:",any"`
}

func ValidateHealthMonitor(v *HealthMonitor, path string) []error {
if v == nil {
return nil
}

subPath := fmt.Sprintf("%s.HealthMonitor", path)
if len(v.UnknownNode) > 0 {
return []error{&UnknownNodeError{subPath, v.UnknownNode[0]}}
}

var subErrors []error
subErrors = append(subErrors, ValidateTCPMonitor(v.TCPMonitor, subPath)...)
subErrors = append(subErrors, ValidateHTTPMonitor(v.HTTPMonitor, subPath)...)

return subErrors
}
41 changes: 41 additions & 0 deletions pkg/apigee/v1/httpmonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type HTTPMonitor struct {
Request *HttpMonitorRequest `xml:"Request"`
SuccessResponse *SuccessResponse `xml:"SuccessResponse,omitempty"`

UnknownNode AnyList `xml:",any"`
}

func ValidateHTTPMonitor(v *HTTPMonitor, path string) []error {
if v == nil {
return nil
}

subPath := fmt.Sprintf("%s.HTTPMonitor", path)
if len(v.UnknownNode) > 0 {
return []error{&UnknownNodeError{subPath, v.UnknownNode[0]}}
}

var subErrors []error
subErrors = append(subErrors, ValidateHttpMonitorRequest(v.Request, subPath)...)
subErrors = append(subErrors, ValidateSuccessResponse(v.SuccessResponse, subPath)...)

return subErrors
}
49 changes: 49 additions & 0 deletions pkg/apigee/v1/httpmonitor_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type HttpMonitorRequest struct {
ConnectTimeoutInSec int `xml:"ConnectTimeoutInSec,omitempty"`
SocketReadTimeoutInSec int `xml:"SocketReadTimeoutInSec,omitempty"`
Port int `xml:"Port,omitempty"`
Verb string `xml:"Verb,omitempty"`
Path string `xml:"Path,omitempty"`
UseTargetServerSSLInfo bool `xml:"UseTargetServerSSLInfo,omitempty"`
IncludeHealthCheckIdHeader bool `xml:"IncludeHealthCheckIdHeader,omitempty"`
Payload string `xml:"Payload,omitempty"`
Headers HeadersList `xml:"Header,omitempty"`
IsSSL bool `xml:"IsSSL,omitempty"`
TrustAllSSL bool `xml:"TrustAllSSL,omitempty"`

UnknownNode AnyList `xml:",any"`
}

func ValidateHttpMonitorRequest(v *HttpMonitorRequest, path string) []error {
if v == nil {
return nil
}

subPath := fmt.Sprintf("%s.Request", path)
if len(v.UnknownNode) > 0 {
return []error{&UnknownNodeError{subPath, v.UnknownNode[0]}}
}

var subErrors []error
subErrors = append(subErrors, ValidateHeaders(&v.Headers, subPath)...)

return subErrors
}
1 change: 1 addition & 0 deletions pkg/apigee/v1/httptargetconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type HTTPTargetConnection struct {
LoadBalancer *LoadBalancer `xml:"LoadBalancer,omitempty"`
SSLInfo *SSLInfo `xml:"SSLInfo,omitempty"`
Properties *Properties `xml:"Properties"`
HealthMonitor *HealthMonitor `xml:"HealthMonitor,omitempty"`

UnknownNode AnyList `xml:",any"`
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/apigee/v1/responsecode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type ResponseCode struct {
Value int `xml:",cdata"`

UnknownNode AnyList `xml:",any"`
}

func ValidateResponseCode(v *ResponseCode, path string) []error {
if v == nil {
return nil
}

subPath := fmt.Sprintf("%s.ResponseCode", path)
if len(v.UnknownNode) > 0 {
return []error{&UnknownNodeError{subPath, v.UnknownNode[0]}}
}

return nil
}
33 changes: 33 additions & 0 deletions pkg/apigee/v1/responsecodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type ResponseCodeList []*ResponseCode

func ValidateResponseCodes(v *ResponseCodeList, path string) []error {
if v == nil {
return nil
}

for i, vv := range *v {
errs := ValidateResponseCode(vv, fmt.Sprintf("%s.ResponseCodes.%v", path, i))
if len(errs) > 0 {
return errs
}
}
return nil
}
41 changes: 41 additions & 0 deletions pkg/apigee/v1/successresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type SuccessResponse struct {
ResponseCodes ResponseCodeList `xml:"ResponseCode,omitempty"`
Headers HeadersList `xml:"Header,omitempty"`

UnknownNode AnyList `xml:",any"`
}

func ValidateSuccessResponse(v *SuccessResponse, path string) []error {
if v == nil {
return nil
}

subPath := fmt.Sprintf("%s.SuccessResponse", path)
if len(v.UnknownNode) > 0 {
return []error{&UnknownNodeError{subPath, v.UnknownNode[0]}}
}

var subErrors []error
subErrors = append(subErrors, ValidateHeaders(&v.Headers, subPath)...)
subErrors = append(subErrors, ValidateResponseCodes(&v.ResponseCodes, subPath)...)

return subErrors
}
37 changes: 37 additions & 0 deletions pkg/apigee/v1/tcpmonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 Google LLC
//
// 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 v1

import "fmt"

type TCPMonitor struct {
ConnectTimeoutInSec int `xml:"IntervalInSec"`
Port int `xml:"Port,omitempty"`

UnknownNode AnyList `xml:",any"`
}

func ValidateTCPMonitor(v *TCPMonitor, path string) []error {
if v == nil {
return nil
}

subPath := fmt.Sprintf("%s.TCPMonitor", path)
if len(v.UnknownNode) > 0 {
return []error{&UnknownNodeError{subPath, v.UnknownNode[0]}}
}

return nil
}
Loading

0 comments on commit 66c3c3f

Please sign in to comment.