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

fix: exclude zero address as for inadmissable cases #650

Merged
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
4 changes: 2 additions & 2 deletions stackit/internal/services/iaas/network/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (r *networkResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Optional: true,
Computed: true,
Validators: []validator.String{
validate.IP(),
validate.IP(false),
},
},
"ipv4_nameservers": schema.ListAttribute{
Expand Down Expand Up @@ -252,7 +252,7 @@ func (r *networkResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Optional: true,
Computed: true,
Validators: []validator.String{
validate.IP(),
validate.IP(false),
},
},
"ipv6_nameservers": schema.ListAttribute{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (r *networkAreaRouteResource) Schema(_ context.Context, _ resource.SchemaRe
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
validate.IP(),
validate.IP(false),
},
},
"prefix": schema.StringAttribute{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (r *networkInterfaceResource) Schema(_ context.Context, _ resource.SchemaRe
Optional: true,
Computed: true,
Validators: []validator.String{
validate.IP(),
validate.IP(false),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
Expand Down
2 changes: 1 addition & 1 deletion stackit/internal/services/iaas/publicip/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (r *publicIpResource) Schema(_ context.Context, _ resource.SchemaRequest, r
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
validate.IP(),
validate.IP(false),
},
},
"network_interface_id": schema.StringAttribute{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (r *publicIpAssociateResource) Schema(_ context.Context, _ resource.SchemaR
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
validate.IP(),
validate.IP(false),
},
},
"network_interface_id": schema.StringAttribute{
Expand Down
8 changes: 6 additions & 2 deletions stackit/internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ func UUID() *Validator {
}
}

func IP() *Validator {
// IP returns a validator that checks, if the given string is a valid IP address.
// The allowZeroAddress parameter defines, if 0.0.0.0, resp. [::] should be considered valid.
func IP(allowZeroAddress bool) *Validator {
description := "value must be an IP address"

return &Validator{
description: description,
validate: func(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if net.ParseIP(req.ConfigValue.ValueString()) == nil {
ip := net.ParseIP(req.ConfigValue.ValueString())
invalidZeroAddress := !allowZeroAddress && (net.IPv4zero.Equal(ip) || net.IPv6zero.Equal(ip))
if ip == nil || invalidZeroAddress {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
description,
Expand Down
50 changes: 49 additions & 1 deletion stackit/internal/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,87 @@ func TestUUID(t *testing.T) {
func TestIP(t *testing.T) {
tests := []struct {
description string
invalidZero bool
input string
isValid bool
}{
{
"ok IP4",
false,
"111.222.111.222",
true,
},
{
"ok IP6",
false,
"2001:0db8:85a3:08d3::0370:7344",
true,
},
{
"too short",
false,
"0.1.2",
false,
},
{
"Empty",
false,
"",
false,
},
{
"Not an IP",
false,
"for-sure-not-an-IP",
false,
},
{
"valid ipv4 zero",
true,
"0.0.0.0",
true,
},
{
"invalid ipv4 zero",
false,
"0.0.0.0",
false,
},
{
"valid ipv6 zero",
true,
"::",
true,
},
{
"valid ipv6 zero short notation",
true,
"::0",
true,
},
bahkauv70 marked this conversation as resolved.
Show resolved Hide resolved
{
"valid ipv6 zero long notation",
true,
"0000:0000:0000:0000:0000:0000:0000:0000",
true,
},
{
"invalid ipv6 zero short notation",
false,
"::",
false,
},
{
"invalid ipv6 zero long notation",
false,
"0000:0000:0000:0000:0000:0000:0000:0000",
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
r := validator.StringResponse{}
IP().ValidateString(context.Background(), validator.StringRequest{
IP(tt.invalidZero).ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)

Expand Down
Loading