Skip to content

Commit

Permalink
add origin target ip address validation (#213)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>
  • Loading branch information
rizzza authored Aug 28, 2023
1 parent cdfadf7 commit 31c32cb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 3 deletions.
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.

0 comments on commit 31c32cb

Please sign in to comment.