Adding and updating host address validation checks (#350)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-09-12 22:07:56 -07:00
1 parent 33e7b4538e
commit b998591add
8 files changed
+81 -9

No files matched your search

@@ -3,6 +3,7 @@ package hosts
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/boundary/internal/auth"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/hosts"
@@ -351,8 +352,10 @@ func validateCreateRequest(req *pbs.CreateHostRequest) error {
if err := handlers.StructToProto(req.GetItem().GetAttributes(), attrs); err != nil {
badFields["attributes"] = "Attribute fields do not match the expected format."
}
if attrs.GetAddress() == nil {
badFields["attributes.address"] = "This is a required field for this type."
if attrs.GetAddress() == nil ||
len(attrs.GetAddress().GetValue()) < static.MinHostAddressLength ||
len(attrs.GetAddress().GetValue()) > static.MaxHostAddressLength {
badFields["attributes.address"] = fmt.Sprintf("Address length must be between %d and %d characters.", static.MinHostAddressLength, static.MaxHostAddressLength)
}
}
return badFields
@@ -366,6 +369,19 @@ func validateUpdateRequest(req *pbs.UpdateHostRequest) error {
case host.StaticSubtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != host.StaticSubtype.String() {
badFields["type"] = "Cannot modify the resource type."
attrs := &pb.StaticHostAttributes{}
if err := handlers.StructToProto(req.GetItem().GetAttributes(), attrs); err != nil {
badFields["attributes"] = "Attribute fields do not match the expected format."
}
if handlers.MaskContains(req.GetUpdateMask().GetPaths(), "attributes.address") {
if attrs.GetAddress() == nil ||
len(strings.TrimSpace(attrs.GetAddress().GetValue())) < static.MinHostAddressLength ||
len(strings.TrimSpace(attrs.GetAddress().GetValue())) > static.MaxHostAddressLength {
badFields["attributes.address"] = fmt.Sprintf("Address length must be between %d and %d characters.", static.MinHostAddressLength, static.MaxHostAddressLength)
}
}
}
default:
badFields["id"] = "Improperly formatted identifier used."
@@ -304,6 +304,19 @@ func TestCreate(t *testing.T) {
},
errCode: codes.OK,
},
{
name: "Create with empty address",
req: &pbs.CreateHostRequest{Item: &pb.Host{
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "static",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"address": structpb.NewStringValue(""),
}},
}},
errCode: codes.InvalidArgument,
},
{
name: "Create without address",
req: &pbs.CreateHostRequest{Item: &pb.Host{
@@ -691,6 +704,36 @@ func TestUpdate(t *testing.T) {
res: nil,
errCode: codes.InvalidArgument,
},
{
name: "Cant unset address",
req: &pbs.UpdateHostRequest{
Id: hc.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"attributes.address"},
},
Item: &pb.Host{
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"address": structpb.NewNullValue(),
}},
}},
res: nil,
errCode: codes.InvalidArgument,
},
{
name: "Cant set address to empty string",
req: &pbs.UpdateHostRequest{
Id: hc.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"attributes.address"},
},
Item: &pb.Host{
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"address": structpb.NewStringValue(""),
}},
}},
res: nil,
errCode: codes.InvalidArgument,
},
{
name: "Cant specify Created Time",
req: &pbs.UpdateHostRequest{