Skip to content
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

Origin target ip address validation #213

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion internal/ent/generated/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/ent/schema/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"go.infratographer.com/x/entx"
"go.infratographer.com/x/gidx"

"go.infratographer.com/load-balancer-api/internal/ent/schema/validations"
)

// Origin holds the schema definition for the Origin entity.
Expand Down Expand Up @@ -39,6 +41,7 @@ func (Origin) Fields() []ent.Field {
),
field.String("target").
NotEmpty().
Validate(validations.IPAddress).
// Comment("origin address").
Annotations(
entgql.OrderField("target"),
Expand Down
2 changes: 2 additions & 0 deletions internal/ent/schema/validations/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package validations contains validation functions for ent fields
package validations
6 changes: 6 additions & 0 deletions internal/ent/schema/validations/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package validations

import "errors"

// ErrInvalidIPAddress is returned when the given string is not a valid IP address
var ErrInvalidIPAddress = errors.New("invalid ip address")
15 changes: 15 additions & 0 deletions internal/ent/schema/validations/validations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Package validations contains validation functions for ent fields
package validations

import (
"net"
)

// IPAddress validates if the given string is a valid IP address
func IPAddress(ip string) error {
if net.ParseIP(ip) == nil {
return ErrInvalidIPAddress
}

return nil
}
11 changes: 11 additions & 0 deletions internal/graphapi/origin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ func TestMutate_OriginCreate(t *testing.T) {
Active: false,
},
},
{
TestName: "invalid target ip",
Input: graphclient.CreateLoadBalancerOriginInput{
Name: "original",
Target: "not a valid target ip",
PortNumber: 22,
PoolID: pool1.ID,
Active: newBool(false),
},
errorMsg: "invalid ip address",
},
}

for _, tt := range testCases {
Expand Down
3 changes: 1 addition & 2 deletions internal/graphapi/port.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.